blob: d962f887d3f445c71a2b481979d13d86325286eb [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>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053044#include <linux/parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080045#include "smack.h"
46
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020047#define TRANS_TRUE "TRUE"
48#define TRANS_TRUE_SIZE 4
49
Casey Schauflerc6739442013-05-22 18:42:56 -070050#define SMK_CONNECTING 0
51#define SMK_RECEIVING 1
52#define SMK_SENDING 2
53
Casey Schaufler69f287a2014-12-12 17:08:40 -080054#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -070055LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler69f287a2014-12-12 17:08:40 -080056#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Rohit1a5b4722014-10-15 17:40:41 +053057static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080058int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070059
Casey Schauflerd166c802014-08-27 14:51:27 -070060#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070061static char *smk_bu_mess[] = {
62 "Bringup Error", /* Unused */
63 "Bringup", /* SMACK_BRINGUP_ALLOW */
64 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
65 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
66};
67
Vivek Trivedi3bf27892015-06-22 15:36:06 +053068static const match_table_t tokens = {
69 {Opt_fsdefault, SMK_FSDEFAULT "%s"},
70 {Opt_fsfloor, SMK_FSFLOOR "%s"},
71 {Opt_fshat, SMK_FSHAT "%s"},
72 {Opt_fsroot, SMK_FSROOT "%s"},
73 {Opt_fstransmute, SMK_FSTRANS "%s"},
74 {Opt_error, NULL},
75};
76
Casey Schauflerd166c802014-08-27 14:51:27 -070077static void smk_bu_mode(int mode, char *s)
78{
79 int i = 0;
80
81 if (mode & MAY_READ)
82 s[i++] = 'r';
83 if (mode & MAY_WRITE)
84 s[i++] = 'w';
85 if (mode & MAY_EXEC)
86 s[i++] = 'x';
87 if (mode & MAY_APPEND)
88 s[i++] = 'a';
89 if (mode & MAY_TRANSMUTE)
90 s[i++] = 't';
91 if (mode & MAY_LOCK)
92 s[i++] = 'l';
93 if (i == 0)
94 s[i++] = '-';
95 s[i] = '\0';
96}
97#endif
98
99#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200100static int smk_bu_note(char *note, struct smack_known *sskp,
101 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700102{
103 char acc[SMK_NUM_ACCESS_TYPE + 1];
104
105 if (rc <= 0)
106 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700107 if (rc > SMACK_UNCONFINED_OBJECT)
108 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700109
110 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700111 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200112 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700113 return 0;
114}
115#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200116#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700117#endif
118
119#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200120static int smk_bu_current(char *note, struct smack_known *oskp,
121 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700122{
123 struct task_smack *tsp = current_security();
124 char acc[SMK_NUM_ACCESS_TYPE + 1];
125
126 if (rc <= 0)
127 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700128 if (rc > SMACK_UNCONFINED_OBJECT)
129 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700130
131 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700132 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200133 tsp->smk_task->smk_known, oskp->smk_known,
134 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700135 return 0;
136}
137#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200138#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700139#endif
140
141#ifdef CONFIG_SECURITY_SMACK_BRINGUP
142static int smk_bu_task(struct task_struct *otp, int mode, int rc)
143{
144 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300145 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700146 char acc[SMK_NUM_ACCESS_TYPE + 1];
147
148 if (rc <= 0)
149 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700150 if (rc > SMACK_UNCONFINED_OBJECT)
151 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700152
153 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700154 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300155 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700156 current->comm, otp->comm);
157 return 0;
158}
159#else
160#define smk_bu_task(otp, mode, RC) (RC)
161#endif
162
163#ifdef CONFIG_SECURITY_SMACK_BRINGUP
164static int smk_bu_inode(struct inode *inode, int mode, int rc)
165{
166 struct task_smack *tsp = current_security();
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700167 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700168 char acc[SMK_NUM_ACCESS_TYPE + 1];
169
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700170 if (isp->smk_flags & SMK_INODE_IMPURE)
171 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
172 inode->i_sb->s_id, inode->i_ino, current->comm);
173
Casey Schauflerd166c802014-08-27 14:51:27 -0700174 if (rc <= 0)
175 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700176 if (rc > SMACK_UNCONFINED_OBJECT)
177 rc = 0;
178 if (rc == SMACK_UNCONFINED_SUBJECT &&
179 (mode & (MAY_WRITE | MAY_APPEND)))
180 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700181
182 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700183
184 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
185 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700186 inode->i_sb->s_id, inode->i_ino, current->comm);
187 return 0;
188}
189#else
190#define smk_bu_inode(inode, mode, RC) (RC)
191#endif
192
193#ifdef CONFIG_SECURITY_SMACK_BRINGUP
194static int smk_bu_file(struct file *file, int mode, int rc)
195{
196 struct task_smack *tsp = current_security();
197 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800198 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700199 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700200 char acc[SMK_NUM_ACCESS_TYPE + 1];
201
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700202 if (isp->smk_flags & SMK_INODE_IMPURE)
203 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
204 inode->i_sb->s_id, inode->i_ino, current->comm);
205
Casey Schauflerd166c802014-08-27 14:51:27 -0700206 if (rc <= 0)
207 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700208 if (rc > SMACK_UNCONFINED_OBJECT)
209 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700210
211 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700212 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800213 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400214 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700215 current->comm);
216 return 0;
217}
218#else
219#define smk_bu_file(file, mode, RC) (RC)
220#endif
221
222#ifdef CONFIG_SECURITY_SMACK_BRINGUP
223static int smk_bu_credfile(const struct cred *cred, struct file *file,
224 int mode, int rc)
225{
226 struct task_smack *tsp = cred->security;
227 struct smack_known *sskp = tsp->smk_task;
228 struct inode *inode = file->f_inode;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700229 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700230 char acc[SMK_NUM_ACCESS_TYPE + 1];
231
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700232 if (isp->smk_flags & SMK_INODE_IMPURE)
233 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
234 inode->i_sb->s_id, inode->i_ino, current->comm);
235
Casey Schauflerd166c802014-08-27 14:51:27 -0700236 if (rc <= 0)
237 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700238 if (rc > SMACK_UNCONFINED_OBJECT)
239 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700240
241 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700242 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200243 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400244 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700245 current->comm);
246 return 0;
247}
248#else
249#define smk_bu_credfile(cred, file, mode, RC) (RC)
250#endif
251
Casey Schauflere114e472008-02-04 22:29:50 -0800252/**
253 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100254 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800255 * @ip: a pointer to the inode
256 * @dp: a pointer to the dentry
257 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200258 * Returns a pointer to the master list entry for the Smack label,
259 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800260 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700261static struct smack_known *smk_fetch(const char *name, struct inode *ip,
262 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800263{
264 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700265 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700266 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800267
268 if (ip->i_op->getxattr == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200269 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800270
Casey Schauflerf7112e62012-05-06 15:22:02 -0700271 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
272 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200273 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800274
Casey Schauflerf7112e62012-05-06 15:22:02 -0700275 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200276 if (rc < 0)
277 skp = ERR_PTR(rc);
278 else if (rc == 0)
279 skp = NULL;
280 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700281 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700282
283 kfree(buffer);
284
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700285 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800286}
287
288/**
289 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200290 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800291 *
292 * Returns the new blob or NULL if there's no memory available
293 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200294struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800295{
296 struct inode_smack *isp;
297
Rohit1a5b4722014-10-15 17:40:41 +0530298 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800299 if (isp == NULL)
300 return NULL;
301
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200302 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800303 isp->smk_flags = 0;
304 mutex_init(&isp->smk_lock);
305
306 return isp;
307}
308
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800309/**
310 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100311 * @task: a pointer to the Smack label for the running task
312 * @forked: a pointer to the Smack label for the forked task
313 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800314 *
315 * Returns the new blob or NULL if there's no memory available
316 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700317static struct task_smack *new_task_smack(struct smack_known *task,
318 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800319{
320 struct task_smack *tsp;
321
322 tsp = kzalloc(sizeof(struct task_smack), gfp);
323 if (tsp == NULL)
324 return NULL;
325
326 tsp->smk_task = task;
327 tsp->smk_forked = forked;
328 INIT_LIST_HEAD(&tsp->smk_rules);
329 mutex_init(&tsp->smk_rules_lock);
330
331 return tsp;
332}
333
334/**
335 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100336 * @nhead: new rules header pointer
337 * @ohead: old rules header pointer
338 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800339 *
340 * Returns 0 on success, -ENOMEM on error
341 */
342static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
343 gfp_t gfp)
344{
345 struct smack_rule *nrp;
346 struct smack_rule *orp;
347 int rc = 0;
348
349 INIT_LIST_HEAD(nhead);
350
351 list_for_each_entry_rcu(orp, ohead, list) {
352 nrp = kzalloc(sizeof(struct smack_rule), gfp);
353 if (nrp == NULL) {
354 rc = -ENOMEM;
355 break;
356 }
357 *nrp = *orp;
358 list_add_rcu(&nrp->list, nhead);
359 }
360 return rc;
361}
362
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100363/**
364 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
365 * @mode - input mode in form of PTRACE_MODE_*
366 *
367 * Returns a converted MAY_* mode usable by smack rules
368 */
369static inline unsigned int smk_ptrace_mode(unsigned int mode)
370{
371 switch (mode) {
372 case PTRACE_MODE_READ:
373 return MAY_READ;
374 case PTRACE_MODE_ATTACH:
375 return MAY_READWRITE;
376 }
377
378 return 0;
379}
380
381/**
382 * smk_ptrace_rule_check - helper for ptrace access
383 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200384 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100385 * @mode: ptrace attachment mode (PTRACE_MODE_*)
386 * @func: name of the function that called us, used for audit
387 *
388 * Returns 0 on access granted, -error on error
389 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200390static int smk_ptrace_rule_check(struct task_struct *tracer,
391 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100392 unsigned int mode, const char *func)
393{
394 int rc;
395 struct smk_audit_info ad, *saip = NULL;
396 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200397 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100398
399 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
400 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
401 smk_ad_setfield_u_tsk(&ad, tracer);
402 saip = &ad;
403 }
404
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300405 rcu_read_lock();
406 tsp = __task_cred(tracer)->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200407 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100408
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100409 if ((mode & PTRACE_MODE_ATTACH) &&
410 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
411 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200412 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100413 rc = 0;
414 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
415 rc = -EACCES;
416 else if (capable(CAP_SYS_PTRACE))
417 rc = 0;
418 else
419 rc = -EACCES;
420
421 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200422 smack_log(tracer_known->smk_known,
423 tracee_known->smk_known,
424 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100425
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300426 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100427 return rc;
428 }
429
430 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200431 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300432
433 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100434 return rc;
435}
436
Casey Schauflere114e472008-02-04 22:29:50 -0800437/*
438 * LSM hooks.
439 * We he, that is fun!
440 */
441
442/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000443 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800444 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100445 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800446 *
447 * Returns 0 if access is OK, an error code otherwise
448 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100449 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800450 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000451static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800452{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700453 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800454
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300455 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200456
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700457 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100458}
Casey Schauflere114e472008-02-04 22:29:50 -0800459
David Howells5cd9c582008-08-14 11:37:28 +0100460/**
461 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
462 * @ptp: parent task pointer
463 *
464 * Returns 0 if access is OK, an error code otherwise
465 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100466 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100467 */
468static int smack_ptrace_traceme(struct task_struct *ptp)
469{
470 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700471 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100472
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100473 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200474
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200475 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800476 return rc;
477}
478
479/**
480 * smack_syslog - Smack approval on syslog
481 * @type: message type
482 *
Casey Schauflere114e472008-02-04 22:29:50 -0800483 * Returns 0 on success, error code otherwise.
484 */
Eric Paris12b30522010-11-15 18:36:29 -0500485static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800486{
Eric Paris12b30522010-11-15 18:36:29 -0500487 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700488 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800489
Casey Schaufler1880eff2012-06-05 15:28:30 -0700490 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800491 return 0;
492
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800493 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800494 rc = -EACCES;
495
496 return rc;
497}
498
499
500/*
501 * Superblock Hooks.
502 */
503
504/**
505 * smack_sb_alloc_security - allocate a superblock blob
506 * @sb: the superblock getting the blob
507 *
508 * Returns 0 on success or -ENOMEM on error.
509 */
510static int smack_sb_alloc_security(struct super_block *sb)
511{
512 struct superblock_smack *sbsp;
513
514 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
515
516 if (sbsp == NULL)
517 return -ENOMEM;
518
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200519 sbsp->smk_root = &smack_known_floor;
520 sbsp->smk_default = &smack_known_floor;
521 sbsp->smk_floor = &smack_known_floor;
522 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700523 /*
524 * smk_initialized will be zero from kzalloc.
525 */
Casey Schauflere114e472008-02-04 22:29:50 -0800526 sb->s_security = sbsp;
527
528 return 0;
529}
530
531/**
532 * smack_sb_free_security - free a superblock blob
533 * @sb: the superblock getting the blob
534 *
535 */
536static void smack_sb_free_security(struct super_block *sb)
537{
538 kfree(sb->s_security);
539 sb->s_security = NULL;
540}
541
542/**
543 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800544 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800545 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800546 *
547 * Returns 0 on success or -ENOMEM on error.
548 *
549 * Copy the Smack specific mount options out of the mount
550 * options list.
551 */
Eric Parise0007522008-03-05 10:31:54 -0500552static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800553{
554 char *cp, *commap, *otheropts, *dp;
555
Casey Schauflere114e472008-02-04 22:29:50 -0800556 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
557 if (otheropts == NULL)
558 return -ENOMEM;
559
560 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
561 if (strstr(cp, SMK_FSDEFAULT) == cp)
562 dp = smackopts;
563 else if (strstr(cp, SMK_FSFLOOR) == cp)
564 dp = smackopts;
565 else if (strstr(cp, SMK_FSHAT) == cp)
566 dp = smackopts;
567 else if (strstr(cp, SMK_FSROOT) == cp)
568 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700569 else if (strstr(cp, SMK_FSTRANS) == cp)
570 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800571 else
572 dp = otheropts;
573
574 commap = strchr(cp, ',');
575 if (commap != NULL)
576 *commap = '\0';
577
578 if (*dp != '\0')
579 strcat(dp, ",");
580 strcat(dp, cp);
581 }
582
583 strcpy(orig, otheropts);
584 free_page((unsigned long)otheropts);
585
586 return 0;
587}
588
589/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530590 * smack_parse_opts_str - parse Smack specific mount options
591 * @options: mount options string
592 * @opts: where to store converted mount opts
593 *
594 * Returns 0 on success or -ENOMEM on error.
595 *
596 * converts Smack specific mount options to generic security option format
597 */
598static int smack_parse_opts_str(char *options,
599 struct security_mnt_opts *opts)
600{
601 char *p;
602 char *fsdefault = NULL, *fsfloor = NULL;
603 char *fshat = NULL, *fsroot = NULL, *fstransmute = NULL;
604 int rc = -ENOMEM, num_mnt_opts = 0;
605
606 opts->num_mnt_opts = 0;
607
608 if (!options)
609 return 0;
610
611 while ((p = strsep(&options, ",")) != NULL) {
612 int token;
613 substring_t args[MAX_OPT_ARGS];
614
615 if (!*p)
616 continue;
617
618 token = match_token(p, tokens, args);
619
620 switch (token) {
621 case Opt_fsdefault:
622 if (fsdefault)
623 goto out_opt_err;
624 fsdefault = match_strdup(&args[0]);
625 if (!fsdefault)
626 goto out_err;
627 break;
628 case Opt_fsfloor:
629 if (fsfloor)
630 goto out_opt_err;
631 fsfloor = match_strdup(&args[0]);
632 if (!fsfloor)
633 goto out_err;
634 break;
635 case Opt_fshat:
636 if (fshat)
637 goto out_opt_err;
638 fshat = match_strdup(&args[0]);
639 if (!fshat)
640 goto out_err;
641 break;
642 case Opt_fsroot:
643 if (fsroot)
644 goto out_opt_err;
645 fsroot = match_strdup(&args[0]);
646 if (!fsroot)
647 goto out_err;
648 break;
649 case Opt_fstransmute:
650 if (fstransmute)
651 goto out_opt_err;
652 fstransmute = match_strdup(&args[0]);
653 if (!fstransmute)
654 goto out_err;
655 break;
656 default:
657 rc = -EINVAL;
658 pr_warn("Smack: unknown mount option\n");
659 goto out_err;
660 }
661 }
662
663 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
664 if (!opts->mnt_opts)
665 goto out_err;
666
667 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
668 GFP_ATOMIC);
669 if (!opts->mnt_opts_flags) {
670 kfree(opts->mnt_opts);
671 goto out_err;
672 }
673
674 if (fsdefault) {
675 opts->mnt_opts[num_mnt_opts] = fsdefault;
676 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
677 }
678 if (fsfloor) {
679 opts->mnt_opts[num_mnt_opts] = fsfloor;
680 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
681 }
682 if (fshat) {
683 opts->mnt_opts[num_mnt_opts] = fshat;
684 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
685 }
686 if (fsroot) {
687 opts->mnt_opts[num_mnt_opts] = fsroot;
688 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
689 }
690 if (fstransmute) {
691 opts->mnt_opts[num_mnt_opts] = fstransmute;
692 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
693 }
694
695 opts->num_mnt_opts = num_mnt_opts;
696 return 0;
697
698out_opt_err:
699 rc = -EINVAL;
700 pr_warn("Smack: duplicate mount options\n");
701
702out_err:
703 kfree(fsdefault);
704 kfree(fsfloor);
705 kfree(fshat);
706 kfree(fsroot);
707 kfree(fstransmute);
708 return rc;
709}
710
711/**
712 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800713 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530714 * @opts: Smack mount options
715 * @kern_flags: mount option from kernel space or user space
716 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800717 *
718 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530719 *
720 * Allow filesystems with binary mount data to explicitly set Smack mount
721 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800722 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530723static int smack_set_mnt_opts(struct super_block *sb,
724 struct security_mnt_opts *opts,
725 unsigned long kern_flags,
726 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800727{
728 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000729 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800730 struct superblock_smack *sp = sb->s_security;
731 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800732 struct smack_known *skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530733 int i;
734 int num_opts = opts->num_mnt_opts;
Casey Schauflere830b392013-05-22 18:43:07 -0700735 int transmute = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800736
Casey Schauflere830b392013-05-22 18:43:07 -0700737 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800738 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700739
Casey Schauflere114e472008-02-04 22:29:50 -0800740 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800741
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530742 for (i = 0; i < num_opts; i++) {
743 switch (opts->mnt_opts_flags[i]) {
744 case FSDEFAULT_MNT:
745 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200746 if (IS_ERR(skp))
747 return PTR_ERR(skp);
748 sp->smk_default = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530749 break;
750 case FSFLOOR_MNT:
751 skp = smk_import_entry(opts->mnt_opts[i], 0);
752 if (IS_ERR(skp))
753 return PTR_ERR(skp);
754 sp->smk_floor = skp;
755 break;
756 case FSHAT_MNT:
757 skp = smk_import_entry(opts->mnt_opts[i], 0);
758 if (IS_ERR(skp))
759 return PTR_ERR(skp);
760 sp->smk_hat = skp;
761 break;
762 case FSROOT_MNT:
763 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200764 if (IS_ERR(skp))
765 return PTR_ERR(skp);
766 sp->smk_root = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530767 break;
768 case FSTRANS_MNT:
769 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200770 if (IS_ERR(skp))
771 return PTR_ERR(skp);
772 sp->smk_root = skp;
773 transmute = 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530774 break;
775 default:
776 break;
Casey Schauflere114e472008-02-04 22:29:50 -0800777 }
778 }
779
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800780 if (!smack_privileged(CAP_MAC_ADMIN)) {
781 /*
782 * Unprivileged mounts don't get to specify Smack values.
783 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530784 if (num_opts)
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800785 return -EPERM;
786 /*
787 * Unprivileged mounts get root and default from the caller.
788 */
789 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200790 sp->smk_root = skp;
791 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800792 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530793
Casey Schauflere114e472008-02-04 22:29:50 -0800794 /*
795 * Initialize the root inode.
796 */
797 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100798 if (isp == NULL) {
799 isp = new_inode_smack(sp->smk_root);
800 if (isp == NULL)
801 return -ENOMEM;
802 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700803 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800804 isp->smk_inode = sp->smk_root;
805
Casey Schauflere830b392013-05-22 18:43:07 -0700806 if (transmute)
807 isp->smk_flags |= SMK_INODE_TRANSMUTE;
808
Casey Schauflere114e472008-02-04 22:29:50 -0800809 return 0;
810}
811
812/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530813 * smack_sb_kern_mount - Smack specific mount processing
814 * @sb: the file system superblock
815 * @flags: the mount flags
816 * @data: the smack mount options
817 *
818 * Returns 0 on success, an error code on failure
819 */
820static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
821{
822 int rc = 0;
823 char *options = data;
824 struct security_mnt_opts opts;
825
826 security_init_mnt_opts(&opts);
827
828 if (!options)
829 goto out;
830
831 rc = smack_parse_opts_str(options, &opts);
832 if (rc)
833 goto out_err;
834
835out:
836 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
837
838out_err:
839 security_free_mnt_opts(&opts);
840 return rc;
841}
842
843/**
Casey Schauflere114e472008-02-04 22:29:50 -0800844 * smack_sb_statfs - Smack check on statfs
845 * @dentry: identifies the file system in question
846 *
847 * Returns 0 if current can read the floor of the filesystem,
848 * and error code otherwise
849 */
850static int smack_sb_statfs(struct dentry *dentry)
851{
852 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200853 int rc;
854 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800855
Eric Parisa2694342011-04-25 13:10:27 -0400856 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200857 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
858
859 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700860 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200861 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800862}
863
Casey Schauflere114e472008-02-04 22:29:50 -0800864/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800865 * BPRM hooks
866 */
867
Casey Schauflerce8a4322011-09-29 18:21:01 -0700868/**
869 * smack_bprm_set_creds - set creds for exec
870 * @bprm: the exec information
871 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100872 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700873 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800874static int smack_bprm_set_creds(struct linux_binprm *bprm)
875{
Al Viro496ad9a2013-01-23 17:07:38 -0500876 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300877 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800878 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800879 int rc;
880
Casey Schaufler676dac42010-12-02 06:43:39 -0800881 if (bprm->cred_prepared)
882 return 0;
883
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300884 isp = inode->i_security;
885 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800886 return 0;
887
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100888 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
889 struct task_struct *tracer;
890 rc = 0;
891
892 rcu_read_lock();
893 tracer = ptrace_parent(current);
894 if (likely(tracer != NULL))
895 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200896 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100897 PTRACE_MODE_ATTACH,
898 __func__);
899 rcu_read_unlock();
900
901 if (rc != 0)
902 return rc;
903 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300904 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800905
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300906 bsp->smk_task = isp->smk_task;
907 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800908
909 return 0;
910}
911
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300912/**
913 * smack_bprm_committing_creds - Prepare to install the new credentials
914 * from bprm.
915 *
916 * @bprm: binprm for exec
917 */
918static void smack_bprm_committing_creds(struct linux_binprm *bprm)
919{
920 struct task_smack *bsp = bprm->cred->security;
921
922 if (bsp->smk_task != bsp->smk_forked)
923 current->pdeath_signal = 0;
924}
925
926/**
927 * smack_bprm_secureexec - Return the decision to use secureexec.
928 * @bprm: binprm for exec
929 *
930 * Returns 0 on success.
931 */
932static int smack_bprm_secureexec(struct linux_binprm *bprm)
933{
934 struct task_smack *tsp = current_security();
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300935
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700936 if (tsp->smk_task != tsp->smk_forked)
937 return 1;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300938
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700939 return 0;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300940}
941
Casey Schaufler676dac42010-12-02 06:43:39 -0800942/*
Casey Schauflere114e472008-02-04 22:29:50 -0800943 * Inode hooks
944 */
945
946/**
947 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800948 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800949 *
950 * Returns 0 if it gets a blob, -ENOMEM otherwise
951 */
952static int smack_inode_alloc_security(struct inode *inode)
953{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700954 struct smack_known *skp = smk_of_current();
955
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200956 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800957 if (inode->i_security == NULL)
958 return -ENOMEM;
959 return 0;
960}
961
962/**
963 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800964 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800965 *
966 * Clears the blob pointer in inode
967 */
968static void smack_inode_free_security(struct inode *inode)
969{
Rohit1a5b4722014-10-15 17:40:41 +0530970 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800971 inode->i_security = NULL;
972}
973
974/**
975 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200976 * @inode: the newly created inode
977 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500978 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800979 * @name: where to put the attribute name
980 * @value: where to put the attribute value
981 * @len: where to put the length of the attribute
982 *
983 * Returns 0 if it all works out, -ENOMEM if there's no memory
984 */
985static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900986 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500987 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800988{
Casey Schaufler2267b132012-03-13 19:14:19 -0700989 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700990 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200991 struct smack_known *isp = smk_of_inode(inode);
992 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800993 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800994
Tetsuo Handa95489062013-07-25 05:44:02 +0900995 if (name)
996 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800997
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100998 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800999 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001000 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1001 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001002 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001003
1004 /*
1005 * If the access rule allows transmutation and
1006 * the directory requests transmutation then
1007 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001008 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001009 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001010 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001011 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001012 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001013 issp->smk_flags |= SMK_INODE_CHANGED;
1014 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001015
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001016 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001017 if (*value == NULL)
1018 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001019
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001020 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001021 }
Casey Schauflere114e472008-02-04 22:29:50 -08001022
1023 return 0;
1024}
1025
1026/**
1027 * smack_inode_link - Smack check on link
1028 * @old_dentry: the existing object
1029 * @dir: unused
1030 * @new_dentry: the new object
1031 *
1032 * Returns 0 if access is permitted, an error code otherwise
1033 */
1034static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1035 struct dentry *new_dentry)
1036{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001037 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001038 struct smk_audit_info ad;
1039 int rc;
1040
Eric Parisa2694342011-04-25 13:10:27 -04001041 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001043
David Howellsc6f493d2015-03-17 22:26:22 +00001044 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001045 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001046 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001047
David Howells88025652015-01-29 12:02:32 +00001048 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001049 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001050 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1051 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001052 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001053 }
1054
1055 return rc;
1056}
1057
1058/**
1059 * smack_inode_unlink - Smack check on inode deletion
1060 * @dir: containing directory object
1061 * @dentry: file to unlink
1062 *
1063 * Returns 0 if current can write the containing directory
1064 * and the object, error code otherwise
1065 */
1066static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1067{
David Howellsc6f493d2015-03-17 22:26:22 +00001068 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001069 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001070 int rc;
1071
Eric Parisa2694342011-04-25 13:10:27 -04001072 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001073 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1074
Casey Schauflere114e472008-02-04 22:29:50 -08001075 /*
1076 * You need write access to the thing you're unlinking
1077 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001078 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001079 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001080 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001081 /*
1082 * You also need write access to the containing directory
1083 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001084 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 smk_ad_setfield_u_fs_inode(&ad, dir);
1086 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001087 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001088 }
Casey Schauflere114e472008-02-04 22:29:50 -08001089 return rc;
1090}
1091
1092/**
1093 * smack_inode_rmdir - Smack check on directory deletion
1094 * @dir: containing directory object
1095 * @dentry: directory to unlink
1096 *
1097 * Returns 0 if current can write the containing directory
1098 * and the directory, error code otherwise
1099 */
1100static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1101{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001103 int rc;
1104
Eric Parisa2694342011-04-25 13:10:27 -04001105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1107
Casey Schauflere114e472008-02-04 22:29:50 -08001108 /*
1109 * You need write access to the thing you're removing
1110 */
David Howellsc6f493d2015-03-17 22:26:22 +00001111 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1112 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001113 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001114 /*
1115 * You also need write access to the containing directory
1116 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001117 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 smk_ad_setfield_u_fs_inode(&ad, dir);
1119 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001120 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001121 }
Casey Schauflere114e472008-02-04 22:29:50 -08001122
1123 return rc;
1124}
1125
1126/**
1127 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001128 * @old_inode: unused
1129 * @old_dentry: the old object
1130 * @new_inode: unused
1131 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001132 *
1133 * Read and write access is required on both the old and
1134 * new directories.
1135 *
1136 * Returns 0 if access is permitted, an error code otherwise
1137 */
1138static int smack_inode_rename(struct inode *old_inode,
1139 struct dentry *old_dentry,
1140 struct inode *new_inode,
1141 struct dentry *new_dentry)
1142{
1143 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001144 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 struct smk_audit_info ad;
1146
Eric Parisa2694342011-04-25 13:10:27 -04001147 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001149
David Howellsc6f493d2015-03-17 22:26:22 +00001150 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001151 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001152 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001153
David Howells88025652015-01-29 12:02:32 +00001154 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001155 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001156 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1157 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001158 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001159 }
Casey Schauflere114e472008-02-04 22:29:50 -08001160 return rc;
1161}
1162
1163/**
1164 * smack_inode_permission - Smack version of permission()
1165 * @inode: the inode in question
1166 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001167 *
1168 * This is the important Smack hook.
1169 *
1170 * Returns 0 if access is permitted, -EACCES otherwise
1171 */
Al Viroe74f71e2011-06-20 19:38:15 -04001172static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001173{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001174 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001175 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001176 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001177
1178 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001179 /*
1180 * No permission to check. Existence test. Yup, it's there.
1181 */
1182 if (mask == 0)
1183 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001184
1185 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001186 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001187 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001189 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001190 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1191 rc = smk_bu_inode(inode, mask, rc);
1192 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001193}
1194
1195/**
1196 * smack_inode_setattr - Smack check for setting attributes
1197 * @dentry: the object
1198 * @iattr: for the force flag
1199 *
1200 * Returns 0 if access is permitted, an error code otherwise
1201 */
1202static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1203{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001204 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001205 int rc;
1206
Casey Schauflere114e472008-02-04 22:29:50 -08001207 /*
1208 * Need to allow for clearing the setuid bit.
1209 */
1210 if (iattr->ia_valid & ATTR_FORCE)
1211 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001212 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001213 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001214
David Howellsc6f493d2015-03-17 22:26:22 +00001215 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1216 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001217 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001218}
1219
1220/**
1221 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001222 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001223 * @dentry: the object
1224 *
1225 * Returns 0 if access is permitted, an error code otherwise
1226 */
Al Viro3f7036a2015-03-08 19:28:30 -04001227static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001228{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001229 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001230 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001231 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001232
Eric Parisf48b7392011-04-25 12:54:27 -04001233 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001234 smk_ad_setfield_u_fs_path(&ad, *path);
1235 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1236 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001237 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001238}
1239
1240/**
1241 * smack_inode_setxattr - Smack check for setting xattrs
1242 * @dentry: the object
1243 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001244 * @value: value of the attribute
1245 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001246 * @flags: unused
1247 *
1248 * This protects the Smack attribute explicitly.
1249 *
1250 * Returns 0 if access is permitted, an error code otherwise
1251 */
David Howells8f0cfa52008-04-29 00:59:41 -07001252static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1253 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001254{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001255 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001256 struct smack_known *skp;
1257 int check_priv = 0;
1258 int check_import = 0;
1259 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001260 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001261
Casey Schaufler19760ad2013-12-16 16:27:26 -08001262 /*
1263 * Check label validity here so import won't fail in post_setxattr
1264 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001265 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1266 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001267 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1268 check_priv = 1;
1269 check_import = 1;
1270 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1271 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1272 check_priv = 1;
1273 check_import = 1;
1274 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001275 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001276 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001277 if (size != TRANS_TRUE_SIZE ||
1278 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1279 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001280 } else
1281 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1282
Casey Schaufler19760ad2013-12-16 16:27:26 -08001283 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1284 rc = -EPERM;
1285
1286 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001287 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001288 if (IS_ERR(skp))
1289 rc = PTR_ERR(skp);
1290 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001291 (skp == &smack_known_star || skp == &smack_known_web)))
1292 rc = -EINVAL;
1293 }
1294
Eric Parisa2694342011-04-25 13:10:27 -04001295 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001296 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1297
Casey Schauflerd166c802014-08-27 14:51:27 -07001298 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001299 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1300 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001301 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001302
1303 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001304}
1305
1306/**
1307 * smack_inode_post_setxattr - Apply the Smack update approved above
1308 * @dentry: object
1309 * @name: attribute name
1310 * @value: attribute value
1311 * @size: attribute size
1312 * @flags: unused
1313 *
1314 * Set the pointer in the inode blob to the entry found
1315 * in the master label list.
1316 */
David Howells8f0cfa52008-04-29 00:59:41 -07001317static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1318 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001319{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001320 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001321 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001322
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001323 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1324 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1325 return;
1326 }
1327
Casey Schaufler676dac42010-12-02 06:43:39 -08001328 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001329 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001330 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001331 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001332 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001333 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001334 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001335 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001336 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001337 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001338 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001339 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001340 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001341 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001342 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001343 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001344 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001345 isp->smk_mmap = &smack_known_invalid;
1346 }
Casey Schauflere114e472008-02-04 22:29:50 -08001347
1348 return;
1349}
1350
Casey Schauflerce8a4322011-09-29 18:21:01 -07001351/**
Casey Schauflere114e472008-02-04 22:29:50 -08001352 * smack_inode_getxattr - Smack check on getxattr
1353 * @dentry: the object
1354 * @name: unused
1355 *
1356 * Returns 0 if access is permitted, an error code otherwise
1357 */
David Howells8f0cfa52008-04-29 00:59:41 -07001358static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001359{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001361 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001362
Eric Parisa2694342011-04-25 13:10:27 -04001363 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001364 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1365
David Howellsc6f493d2015-03-17 22:26:22 +00001366 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1367 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001368 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001369}
1370
Casey Schauflerce8a4322011-09-29 18:21:01 -07001371/**
Casey Schauflere114e472008-02-04 22:29:50 -08001372 * smack_inode_removexattr - Smack check on removexattr
1373 * @dentry: the object
1374 * @name: name of the attribute
1375 *
1376 * Removing the Smack attribute requires CAP_MAC_ADMIN
1377 *
1378 * Returns 0 if access is permitted, an error code otherwise
1379 */
David Howells8f0cfa52008-04-29 00:59:41 -07001380static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001381{
Casey Schaufler676dac42010-12-02 06:43:39 -08001382 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001383 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001384 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001385
Casey Schauflerbcdca222008-02-23 15:24:04 -08001386 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1387 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001388 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001389 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001390 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301391 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001392 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001393 rc = -EPERM;
1394 } else
1395 rc = cap_inode_removexattr(dentry, name);
1396
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001397 if (rc != 0)
1398 return rc;
1399
Eric Parisa2694342011-04-25 13:10:27 -04001400 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001401 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001402
David Howellsc6f493d2015-03-17 22:26:22 +00001403 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1404 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001405 if (rc != 0)
1406 return rc;
1407
David Howellsc6f493d2015-03-17 22:26:22 +00001408 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001409 /*
1410 * Don't do anything special for these.
1411 * XATTR_NAME_SMACKIPIN
1412 * XATTR_NAME_SMACKIPOUT
1413 * XATTR_NAME_SMACKEXEC
1414 */
1415 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001416 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001417 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001418 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001419 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1420 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001421
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001422 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001423}
1424
1425/**
1426 * smack_inode_getsecurity - get smack xattrs
1427 * @inode: the object
1428 * @name: attribute name
1429 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001430 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001431 *
1432 * Returns the size of the attribute or an error code
1433 */
1434static int smack_inode_getsecurity(const struct inode *inode,
1435 const char *name, void **buffer,
1436 bool alloc)
1437{
1438 struct socket_smack *ssp;
1439 struct socket *sock;
1440 struct super_block *sbp;
1441 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001442 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001443 int ilen;
1444 int rc = 0;
1445
1446 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1447 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001448 ilen = strlen(isp->smk_known);
1449 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001450 return ilen;
1451 }
1452
1453 /*
1454 * The rest of the Smack xattrs are only on sockets.
1455 */
1456 sbp = ip->i_sb;
1457 if (sbp->s_magic != SOCKFS_MAGIC)
1458 return -EOPNOTSUPP;
1459
1460 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001461 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001462 return -EOPNOTSUPP;
1463
1464 ssp = sock->sk->sk_security;
1465
1466 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001467 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001468 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001469 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001470 else
1471 return -EOPNOTSUPP;
1472
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001473 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001474 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001475 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001476 rc = ilen;
1477 }
1478
1479 return rc;
1480}
1481
1482
1483/**
1484 * smack_inode_listsecurity - list the Smack attributes
1485 * @inode: the object
1486 * @buffer: where they go
1487 * @buffer_size: size of buffer
1488 *
1489 * Returns 0 on success, -EINVAL otherwise
1490 */
1491static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1492 size_t buffer_size)
1493{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001494 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001495
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001496 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001497 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001498
1499 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001500}
1501
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001502/**
1503 * smack_inode_getsecid - Extract inode's security id
1504 * @inode: inode to extract the info from
1505 * @secid: where result will be saved
1506 */
1507static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1508{
1509 struct inode_smack *isp = inode->i_security;
1510
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001511 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001512}
1513
Casey Schauflere114e472008-02-04 22:29:50 -08001514/*
1515 * File Hooks
1516 */
1517
1518/**
1519 * smack_file_permission - Smack check on file operations
1520 * @file: unused
1521 * @mask: unused
1522 *
1523 * Returns 0
1524 *
1525 * Should access checks be done on each read or write?
1526 * UNICOS and SELinux say yes.
1527 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1528 *
1529 * I'll say no for now. Smack does not do the frequent
1530 * label changing that SELinux does.
1531 */
1532static int smack_file_permission(struct file *file, int mask)
1533{
1534 return 0;
1535}
1536
1537/**
1538 * smack_file_alloc_security - assign a file security blob
1539 * @file: the object
1540 *
1541 * The security blob for a file is a pointer to the master
1542 * label list, so no allocation is done.
1543 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001544 * f_security is the owner security information. It
1545 * isn't used on file access checks, it's for send_sigio.
1546 *
Casey Schauflere114e472008-02-04 22:29:50 -08001547 * Returns 0
1548 */
1549static int smack_file_alloc_security(struct file *file)
1550{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001551 struct smack_known *skp = smk_of_current();
1552
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001553 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001554 return 0;
1555}
1556
1557/**
1558 * smack_file_free_security - clear a file security blob
1559 * @file: the object
1560 *
1561 * The security blob for a file is a pointer to the master
1562 * label list, so no memory is freed.
1563 */
1564static void smack_file_free_security(struct file *file)
1565{
1566 file->f_security = NULL;
1567}
1568
1569/**
1570 * smack_file_ioctl - Smack check on ioctls
1571 * @file: the object
1572 * @cmd: what to do
1573 * @arg: unused
1574 *
1575 * Relies heavily on the correct use of the ioctl command conventions.
1576 *
1577 * Returns 0 if allowed, error code otherwise
1578 */
1579static int smack_file_ioctl(struct file *file, unsigned int cmd,
1580 unsigned long arg)
1581{
1582 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001583 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001584 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001585
Eric Parisf48b7392011-04-25 12:54:27 -04001586 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001587 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001588
Casey Schauflerd166c802014-08-27 14:51:27 -07001589 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001590 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001591 rc = smk_bu_file(file, MAY_WRITE, rc);
1592 }
Casey Schauflere114e472008-02-04 22:29:50 -08001593
Casey Schauflerd166c802014-08-27 14:51:27 -07001594 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001595 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001596 rc = smk_bu_file(file, MAY_READ, rc);
1597 }
Casey Schauflere114e472008-02-04 22:29:50 -08001598
1599 return rc;
1600}
1601
1602/**
1603 * smack_file_lock - Smack check on file locking
1604 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001605 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001606 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001607 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001608 */
1609static int smack_file_lock(struct file *file, unsigned int cmd)
1610{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001611 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001612 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001613 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001614
Eric Paris92f42502011-04-25 13:15:55 -04001615 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1616 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001617 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001618 rc = smk_bu_file(file, MAY_LOCK, rc);
1619 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001620}
1621
1622/**
1623 * smack_file_fcntl - Smack check on fcntl
1624 * @file: the object
1625 * @cmd: what action to check
1626 * @arg: unused
1627 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001628 * Generally these operations are harmless.
1629 * File locking operations present an obvious mechanism
1630 * for passing information, so they require write access.
1631 *
Casey Schauflere114e472008-02-04 22:29:50 -08001632 * Returns 0 if current has access, error code otherwise
1633 */
1634static int smack_file_fcntl(struct file *file, unsigned int cmd,
1635 unsigned long arg)
1636{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001637 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001638 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001639 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001640
Casey Schauflere114e472008-02-04 22:29:50 -08001641 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001642 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001643 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001644 case F_SETLK:
1645 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001646 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1647 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001648 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001649 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001650 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001651 case F_SETOWN:
1652 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001653 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1654 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001655 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001656 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001657 break;
1658 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001659 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001660 }
1661
1662 return rc;
1663}
1664
1665/**
Al Viroe5467852012-05-30 13:30:51 -04001666 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001667 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1668 * if mapping anonymous memory.
1669 * @file contains the file structure for file to map (may be NULL).
1670 * @reqprot contains the protection requested by the application.
1671 * @prot contains the protection that will be applied by the kernel.
1672 * @flags contains the operational flags.
1673 * Return 0 if permission is granted.
1674 */
Al Viroe5467852012-05-30 13:30:51 -04001675static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001676 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001677 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001678{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001679 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001680 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001681 struct smack_rule *srp;
1682 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001683 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001684 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001685 int may;
1686 int mmay;
1687 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001688 int rc;
1689
Al Viro496ad9a2013-01-23 17:07:38 -05001690 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001691 return 0;
1692
Al Viro496ad9a2013-01-23 17:07:38 -05001693 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001694 if (isp->smk_mmap == NULL)
1695 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001696 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001697
1698 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001699 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001700 rc = 0;
1701
1702 rcu_read_lock();
1703 /*
1704 * For each Smack rule associated with the subject
1705 * label verify that the SMACK64MMAP also has access
1706 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001707 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001708 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001709 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001710 /*
1711 * Matching labels always allows access.
1712 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001713 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001714 continue;
1715 /*
1716 * If there is a matching local rule take
1717 * that into account as well.
1718 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001719 may = smk_access_entry(srp->smk_subject->smk_known,
1720 okp->smk_known,
1721 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001722 if (may == -ENOENT)
1723 may = srp->smk_access;
1724 else
1725 may &= srp->smk_access;
1726 /*
1727 * If may is zero the SMACK64MMAP subject can't
1728 * possibly have less access.
1729 */
1730 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001731 continue;
1732
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001733 /*
1734 * Fetch the global list entry.
1735 * If there isn't one a SMACK64MMAP subject
1736 * can't have as much access as current.
1737 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001738 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1739 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001740 if (mmay == -ENOENT) {
1741 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001742 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001743 }
1744 /*
1745 * If there is a local entry it modifies the
1746 * potential access, too.
1747 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001748 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1749 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001750 if (tmay != -ENOENT)
1751 mmay &= tmay;
1752
1753 /*
1754 * If there is any access available to current that is
1755 * not available to a SMACK64MMAP subject
1756 * deny access.
1757 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001758 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001759 rc = -EACCES;
1760 break;
1761 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001762 }
1763
1764 rcu_read_unlock();
1765
1766 return rc;
1767}
1768
1769/**
Casey Schauflere114e472008-02-04 22:29:50 -08001770 * smack_file_set_fowner - set the file security blob value
1771 * @file: object in question
1772 *
Casey Schauflere114e472008-02-04 22:29:50 -08001773 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001774static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001775{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001776 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001777}
1778
1779/**
1780 * smack_file_send_sigiotask - Smack on sigio
1781 * @tsk: The target task
1782 * @fown: the object the signal come from
1783 * @signum: unused
1784 *
1785 * Allow a privileged task to get signals even if it shouldn't
1786 *
1787 * Returns 0 if a subject with the object's smack could
1788 * write to the task, an error code otherwise.
1789 */
1790static int smack_file_send_sigiotask(struct task_struct *tsk,
1791 struct fown_struct *fown, int signum)
1792{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001793 struct smack_known *skp;
1794 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001795 struct file *file;
1796 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001797 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001798
1799 /*
1800 * struct fown_struct is never outside the context of a struct file
1801 */
1802 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001803
Etienne Bassetecfcc532009-04-08 20:40:06 +02001804 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001805 skp = file->f_security;
1806 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1807 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001808 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001809 rc = 0;
1810
1811 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1812 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001813 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001814 return rc;
1815}
1816
1817/**
1818 * smack_file_receive - Smack file receive check
1819 * @file: the object
1820 *
1821 * Returns 0 if current has access, error code otherwise
1822 */
1823static int smack_file_receive(struct file *file)
1824{
Casey Schauflerd166c802014-08-27 14:51:27 -07001825 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001826 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001827 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001828 struct inode *inode = file_inode(file);
Casey Schauflere114e472008-02-04 22:29:50 -08001829
Seung-Woo Kim97775822015-04-17 15:25:04 +09001830 if (unlikely(IS_PRIVATE(inode)))
1831 return 0;
1832
Casey Schaufler4482a442013-12-30 17:37:45 -08001833 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001834 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001835 /*
1836 * This code relies on bitmasks.
1837 */
1838 if (file->f_mode & FMODE_READ)
1839 may = MAY_READ;
1840 if (file->f_mode & FMODE_WRITE)
1841 may |= MAY_WRITE;
1842
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001843 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001844 rc = smk_bu_file(file, may, rc);
1845 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001846}
1847
Casey Schaufler531f1d42011-09-19 12:41:42 -07001848/**
Eric Paris83d49852012-04-04 13:45:40 -04001849 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001850 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001851 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001852 *
1853 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001854 * Allow the open only if the task has read access. There are
1855 * many read operations (e.g. fstat) that you can do with an
1856 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001857 *
1858 * Returns 0
1859 */
Eric Paris83d49852012-04-04 13:45:40 -04001860static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001861{
Casey Schauflera6834c02014-04-21 11:10:26 -07001862 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001863 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001864 struct smk_audit_info ad;
1865 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001866
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001867 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001868 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001869
Casey Schauflera6834c02014-04-21 11:10:26 -07001870 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1871 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001872 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001873 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001874
1875 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001876}
1877
Casey Schauflere114e472008-02-04 22:29:50 -08001878/*
1879 * Task hooks
1880 */
1881
1882/**
David Howellsee18d642009-09-02 09:14:21 +01001883 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1884 * @new: the new credentials
1885 * @gfp: the atomicity of any memory allocations
1886 *
1887 * Prepare a blank set of credentials for modification. This must allocate all
1888 * the memory the LSM module might require such that cred_transfer() can
1889 * complete without error.
1890 */
1891static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1892{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001893 struct task_smack *tsp;
1894
1895 tsp = new_task_smack(NULL, NULL, gfp);
1896 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001897 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001898
1899 cred->security = tsp;
1900
David Howellsee18d642009-09-02 09:14:21 +01001901 return 0;
1902}
1903
1904
1905/**
David Howellsf1752ee2008-11-14 10:39:17 +11001906 * smack_cred_free - "free" task-level security credentials
1907 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001908 *
Casey Schauflere114e472008-02-04 22:29:50 -08001909 */
David Howellsf1752ee2008-11-14 10:39:17 +11001910static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001911{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001912 struct task_smack *tsp = cred->security;
1913 struct smack_rule *rp;
1914 struct list_head *l;
1915 struct list_head *n;
1916
1917 if (tsp == NULL)
1918 return;
1919 cred->security = NULL;
1920
1921 list_for_each_safe(l, n, &tsp->smk_rules) {
1922 rp = list_entry(l, struct smack_rule, list);
1923 list_del(&rp->list);
1924 kfree(rp);
1925 }
1926 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001927}
1928
1929/**
David Howellsd84f4f92008-11-14 10:39:23 +11001930 * smack_cred_prepare - prepare new set of credentials for modification
1931 * @new: the new credentials
1932 * @old: the original credentials
1933 * @gfp: the atomicity of any memory allocations
1934 *
1935 * Prepare a new set of credentials for modification.
1936 */
1937static int smack_cred_prepare(struct cred *new, const struct cred *old,
1938 gfp_t gfp)
1939{
Casey Schaufler676dac42010-12-02 06:43:39 -08001940 struct task_smack *old_tsp = old->security;
1941 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001942 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001943
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001944 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001945 if (new_tsp == NULL)
1946 return -ENOMEM;
1947
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001948 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1949 if (rc != 0)
1950 return rc;
1951
Casey Schaufler676dac42010-12-02 06:43:39 -08001952 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001953 return 0;
1954}
1955
Randy Dunlap251a2a92009-02-18 11:42:33 -08001956/**
David Howellsee18d642009-09-02 09:14:21 +01001957 * smack_cred_transfer - Transfer the old credentials to the new credentials
1958 * @new: the new credentials
1959 * @old: the original credentials
1960 *
1961 * Fill in a set of blank credentials from another set of credentials.
1962 */
1963static void smack_cred_transfer(struct cred *new, const struct cred *old)
1964{
Casey Schaufler676dac42010-12-02 06:43:39 -08001965 struct task_smack *old_tsp = old->security;
1966 struct task_smack *new_tsp = new->security;
1967
1968 new_tsp->smk_task = old_tsp->smk_task;
1969 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001970 mutex_init(&new_tsp->smk_rules_lock);
1971 INIT_LIST_HEAD(&new_tsp->smk_rules);
1972
1973
1974 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001975}
1976
1977/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001978 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001979 * @new: points to the set of credentials to be modified.
1980 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001981 *
1982 * Set the security data for a kernel service.
1983 */
1984static int smack_kernel_act_as(struct cred *new, u32 secid)
1985{
Casey Schaufler676dac42010-12-02 06:43:39 -08001986 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001987 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001988
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001989 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001990 return -EINVAL;
1991
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001992 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001993 return 0;
1994}
1995
1996/**
1997 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001998 * @new: points to the set of credentials to be modified
1999 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002000 *
2001 * Set the file creation context in a set of credentials to the same
2002 * as the objective context of the specified inode
2003 */
2004static int smack_kernel_create_files_as(struct cred *new,
2005 struct inode *inode)
2006{
2007 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002008 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002009
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002010 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002011 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002012 return 0;
2013}
2014
2015/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002016 * smk_curacc_on_task - helper to log task related access
2017 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002018 * @access: the access requested
2019 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002020 *
2021 * Return 0 if access is permitted
2022 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002023static int smk_curacc_on_task(struct task_struct *p, int access,
2024 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002025{
2026 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002027 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002028 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002029
Casey Schaufler531f1d42011-09-19 12:41:42 -07002030 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002031 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002032 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002033 rc = smk_bu_task(p, access, rc);
2034 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002035}
2036
2037/**
Casey Schauflere114e472008-02-04 22:29:50 -08002038 * smack_task_setpgid - Smack check on setting pgid
2039 * @p: the task object
2040 * @pgid: unused
2041 *
2042 * Return 0 if write access is permitted
2043 */
2044static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2045{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002046 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002047}
2048
2049/**
2050 * smack_task_getpgid - Smack access check for getpgid
2051 * @p: the object task
2052 *
2053 * Returns 0 if current can read the object task, error code otherwise
2054 */
2055static int smack_task_getpgid(struct task_struct *p)
2056{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002057 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002058}
2059
2060/**
2061 * smack_task_getsid - Smack access check for getsid
2062 * @p: the object task
2063 *
2064 * Returns 0 if current can read the object task, error code otherwise
2065 */
2066static int smack_task_getsid(struct task_struct *p)
2067{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002068 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002069}
2070
2071/**
2072 * smack_task_getsecid - get the secid of the task
2073 * @p: the object task
2074 * @secid: where to put the result
2075 *
2076 * Sets the secid to contain a u32 version of the smack label.
2077 */
2078static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2079{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002080 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002081
2082 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002083}
2084
2085/**
2086 * smack_task_setnice - Smack check on setting nice
2087 * @p: the task object
2088 * @nice: unused
2089 *
2090 * Return 0 if write access is permitted
2091 */
2092static int smack_task_setnice(struct task_struct *p, int nice)
2093{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002094 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002095}
2096
2097/**
2098 * smack_task_setioprio - Smack check on setting ioprio
2099 * @p: the task object
2100 * @ioprio: unused
2101 *
2102 * Return 0 if write access is permitted
2103 */
2104static int smack_task_setioprio(struct task_struct *p, int ioprio)
2105{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002106 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002107}
2108
2109/**
2110 * smack_task_getioprio - Smack check on reading ioprio
2111 * @p: the task object
2112 *
2113 * Return 0 if read access is permitted
2114 */
2115static int smack_task_getioprio(struct task_struct *p)
2116{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002117 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002118}
2119
2120/**
2121 * smack_task_setscheduler - Smack check on setting scheduler
2122 * @p: the task object
2123 * @policy: unused
2124 * @lp: unused
2125 *
2126 * Return 0 if read access is permitted
2127 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002128static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002129{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002130 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002131}
2132
2133/**
2134 * smack_task_getscheduler - Smack check on reading scheduler
2135 * @p: the task object
2136 *
2137 * Return 0 if read access is permitted
2138 */
2139static int smack_task_getscheduler(struct task_struct *p)
2140{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002141 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002142}
2143
2144/**
2145 * smack_task_movememory - Smack check on moving memory
2146 * @p: the task object
2147 *
2148 * Return 0 if write access is permitted
2149 */
2150static int smack_task_movememory(struct task_struct *p)
2151{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002152 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002153}
2154
2155/**
2156 * smack_task_kill - Smack check on signal delivery
2157 * @p: the task object
2158 * @info: unused
2159 * @sig: unused
2160 * @secid: identifies the smack to use in lieu of current's
2161 *
2162 * Return 0 if write access is permitted
2163 *
2164 * The secid behavior is an artifact of an SELinux hack
2165 * in the USB code. Someday it may go away.
2166 */
2167static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2168 int sig, u32 secid)
2169{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002170 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002171 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002172 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002173 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002174
2175 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2176 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002177 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002178 * Sending a signal requires that the sender
2179 * can write the receiver.
2180 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002181 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002182 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002183 rc = smk_bu_task(p, MAY_WRITE, rc);
2184 return rc;
2185 }
Casey Schauflere114e472008-02-04 22:29:50 -08002186 /*
2187 * If the secid isn't 0 we're dealing with some USB IO
2188 * specific behavior. This is not clean. For one thing
2189 * we can't take privilege into account.
2190 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002191 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002192 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2193 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002194 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002195}
2196
2197/**
2198 * smack_task_wait - Smack access check for waiting
2199 * @p: task to wait for
2200 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002201 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002202 */
2203static int smack_task_wait(struct task_struct *p)
2204{
Casey Schauflere114e472008-02-04 22:29:50 -08002205 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002206 * Allow the operation to succeed.
2207 * Zombies are bad.
2208 * In userless environments (e.g. phones) programs
2209 * get marked with SMACK64EXEC and even if the parent
2210 * and child shouldn't be talking the parent still
2211 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002212 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002213 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002214}
2215
2216/**
2217 * smack_task_to_inode - copy task smack into the inode blob
2218 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002219 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002220 *
2221 * Sets the smack pointer in the inode security blob
2222 */
2223static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2224{
2225 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002226 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002227
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002228 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002229}
2230
2231/*
2232 * Socket hooks.
2233 */
2234
2235/**
2236 * smack_sk_alloc_security - Allocate a socket blob
2237 * @sk: the socket
2238 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002239 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002240 *
2241 * Assign Smack pointers to current
2242 *
2243 * Returns 0 on success, -ENOMEM is there's no memory
2244 */
2245static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2246{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002247 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002248 struct socket_smack *ssp;
2249
2250 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2251 if (ssp == NULL)
2252 return -ENOMEM;
2253
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002254 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002255 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002256 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002257
2258 sk->sk_security = ssp;
2259
2260 return 0;
2261}
2262
2263/**
2264 * smack_sk_free_security - Free a socket blob
2265 * @sk: the socket
2266 *
2267 * Clears the blob pointer
2268 */
2269static void smack_sk_free_security(struct sock *sk)
2270{
2271 kfree(sk->sk_security);
2272}
2273
2274/**
Paul Moore07feee82009-03-27 17:10:54 -04002275* smack_host_label - check host based restrictions
2276* @sip: the object end
2277*
2278* looks for host based access restrictions
2279*
2280* This version will only be appropriate for really small sets of single label
2281* hosts. The caller is responsible for ensuring that the RCU read lock is
2282* taken before calling this function.
2283*
2284* Returns the label of the far end or NULL if it's not special.
2285*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002286static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002287{
2288 struct smk_netlbladdr *snp;
2289 struct in_addr *siap = &sip->sin_addr;
2290
2291 if (siap->s_addr == 0)
2292 return NULL;
2293
2294 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2295 /*
2296 * we break after finding the first match because
2297 * the list is sorted from longest to shortest mask
2298 * so we have found the most specific match
2299 */
2300 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002301 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2302 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002303 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002304 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002305 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002306 }
Paul Moore07feee82009-03-27 17:10:54 -04002307
2308 return NULL;
2309}
2310
2311/**
Casey Schauflere114e472008-02-04 22:29:50 -08002312 * smack_netlabel - Set the secattr on a socket
2313 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002314 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002315 *
2316 * Convert the outbound smack value (smk_out) to a
2317 * secattr and attach it to the socket.
2318 *
2319 * Returns 0 on success or an error code
2320 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002321static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002322{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002323 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002324 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002325 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002326
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002327 /*
2328 * Usually the netlabel code will handle changing the
2329 * packet labeling based on the label.
2330 * The case of a single label host is different, because
2331 * a single label host should never get a labeled packet
2332 * even though the label is usually associated with a packet
2333 * label.
2334 */
2335 local_bh_disable();
2336 bh_lock_sock_nested(sk);
2337
2338 if (ssp->smk_out == smack_net_ambient ||
2339 labeled == SMACK_UNLABELED_SOCKET)
2340 netlbl_sock_delattr(sk);
2341 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002342 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002343 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002344 }
2345
2346 bh_unlock_sock(sk);
2347 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002348
Casey Schauflere114e472008-02-04 22:29:50 -08002349 return rc;
2350}
2351
2352/**
Paul Moore07feee82009-03-27 17:10:54 -04002353 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2354 * @sk: the socket
2355 * @sap: the destination address
2356 *
2357 * Set the correct secattr for the given socket based on the destination
2358 * address and perform any outbound access checks needed.
2359 *
2360 * Returns 0 on success or an error code.
2361 *
2362 */
2363static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2364{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002365 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002366 int rc;
2367 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002368 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002369 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002370 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002371
2372 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002373 hkp = smack_host_label(sap);
2374 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002375#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002376 struct lsm_network_audit net;
2377
Eric Paris48c62af2012-04-02 13:15:44 -04002378 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2379 ad.a.u.net->family = sap->sin_family;
2380 ad.a.u.net->dport = sap->sin_port;
2381 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002382#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002383 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002384 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002385 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2386 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002387 } else {
2388 sk_lbl = SMACK_CIPSO_SOCKET;
2389 rc = 0;
2390 }
2391 rcu_read_unlock();
2392 if (rc != 0)
2393 return rc;
2394
2395 return smack_netlabel(sk, sk_lbl);
2396}
2397
Casey Schaufler69f287a2014-12-12 17:08:40 -08002398#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Paul Moore07feee82009-03-27 17:10:54 -04002399/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002400 * smk_ipv6_port_label - Smack port access table management
2401 * @sock: socket
2402 * @address: address
2403 *
2404 * Create or update the port list entry
2405 */
2406static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2407{
2408 struct sock *sk = sock->sk;
2409 struct sockaddr_in6 *addr6;
2410 struct socket_smack *ssp = sock->sk->sk_security;
2411 struct smk_port_label *spp;
2412 unsigned short port = 0;
2413
2414 if (address == NULL) {
2415 /*
2416 * This operation is changing the Smack information
2417 * on the bound socket. Take the changes to the port
2418 * as well.
2419 */
2420 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2421 if (sk != spp->smk_sock)
2422 continue;
2423 spp->smk_in = ssp->smk_in;
2424 spp->smk_out = ssp->smk_out;
2425 return;
2426 }
2427 /*
2428 * A NULL address is only used for updating existing
2429 * bound entries. If there isn't one, it's OK.
2430 */
2431 return;
2432 }
2433
2434 addr6 = (struct sockaddr_in6 *)address;
2435 port = ntohs(addr6->sin6_port);
2436 /*
2437 * This is a special case that is safely ignored.
2438 */
2439 if (port == 0)
2440 return;
2441
2442 /*
2443 * Look for an existing port list entry.
2444 * This is an indication that a port is getting reused.
2445 */
2446 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2447 if (spp->smk_port != port)
2448 continue;
2449 spp->smk_port = port;
2450 spp->smk_sock = sk;
2451 spp->smk_in = ssp->smk_in;
2452 spp->smk_out = ssp->smk_out;
2453 return;
2454 }
2455
2456 /*
2457 * A new port entry is required.
2458 */
2459 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2460 if (spp == NULL)
2461 return;
2462
2463 spp->smk_port = port;
2464 spp->smk_sock = sk;
2465 spp->smk_in = ssp->smk_in;
2466 spp->smk_out = ssp->smk_out;
2467
2468 list_add(&spp->list, &smk_ipv6_port_list);
2469 return;
2470}
2471
2472/**
2473 * smk_ipv6_port_check - check Smack port access
2474 * @sock: socket
2475 * @address: address
2476 *
2477 * Create or update the port list entry
2478 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002479static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002480 int act)
2481{
2482 __be16 *bep;
2483 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002484 struct smk_port_label *spp;
2485 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002486 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002487 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002488 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002489 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002490 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002491#ifdef CONFIG_AUDIT
2492 struct lsm_network_audit net;
2493#endif
2494
2495 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002496 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002497 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002498 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002499 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002500 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002501 }
2502
2503 /*
2504 * Get the IP address and port from the address.
2505 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002506 port = ntohs(address->sin6_port);
2507 bep = (__be16 *)(&address->sin6_addr);
2508 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002509
2510 /*
2511 * It's remote, so port lookup does no good.
2512 */
2513 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2514 goto auditout;
2515
2516 /*
2517 * It's local so the send check has to have passed.
2518 */
2519 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002520 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002521 goto auditout;
2522 }
2523
2524 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2525 if (spp->smk_port != port)
2526 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002527 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002528 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002529 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002530 break;
2531 }
2532
2533auditout:
2534
2535#ifdef CONFIG_AUDIT
2536 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2537 ad.a.u.net->family = sk->sk_family;
2538 ad.a.u.net->dport = port;
2539 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002540 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002541 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002542 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002543#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002544 rc = smk_access(skp, object, MAY_WRITE, &ad);
2545 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2546 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002547}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002548#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002549
2550/**
Casey Schauflere114e472008-02-04 22:29:50 -08002551 * smack_inode_setsecurity - set smack xattrs
2552 * @inode: the object
2553 * @name: attribute name
2554 * @value: attribute value
2555 * @size: size of the attribute
2556 * @flags: unused
2557 *
2558 * Sets the named attribute in the appropriate blob
2559 *
2560 * Returns 0 on success, or an error code
2561 */
2562static int smack_inode_setsecurity(struct inode *inode, const char *name,
2563 const void *value, size_t size, int flags)
2564{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002565 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002566 struct inode_smack *nsp = inode->i_security;
2567 struct socket_smack *ssp;
2568 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002569 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002570
Casey Schauflerf7112e62012-05-06 15:22:02 -07002571 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302572 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002573
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002574 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002575 if (IS_ERR(skp))
2576 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002577
2578 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002579 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002580 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002581 return 0;
2582 }
2583 /*
2584 * The rest of the Smack xattrs are only on sockets.
2585 */
2586 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2587 return -EOPNOTSUPP;
2588
2589 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002590 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002591 return -EOPNOTSUPP;
2592
2593 ssp = sock->sk->sk_security;
2594
2595 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002596 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002597 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002598 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002599 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002600 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2601 if (rc != 0)
2602 printk(KERN_WARNING
2603 "Smack: \"%s\" netlbl error %d.\n",
2604 __func__, -rc);
2605 }
Casey Schauflere114e472008-02-04 22:29:50 -08002606 } else
2607 return -EOPNOTSUPP;
2608
Casey Schaufler69f287a2014-12-12 17:08:40 -08002609#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07002610 if (sock->sk->sk_family == PF_INET6)
2611 smk_ipv6_port_label(sock, NULL);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002612#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002613
Casey Schauflere114e472008-02-04 22:29:50 -08002614 return 0;
2615}
2616
2617/**
2618 * smack_socket_post_create - finish socket setup
2619 * @sock: the socket
2620 * @family: protocol family
2621 * @type: unused
2622 * @protocol: unused
2623 * @kern: unused
2624 *
2625 * Sets the netlabel information on the socket
2626 *
2627 * Returns 0 on success, and error code otherwise
2628 */
2629static int smack_socket_post_create(struct socket *sock, int family,
2630 int type, int protocol, int kern)
2631{
Marcin Lis74123012015-01-22 15:40:33 +01002632 struct socket_smack *ssp;
2633
2634 if (sock->sk == NULL)
2635 return 0;
2636
2637 /*
2638 * Sockets created by kernel threads receive web label.
2639 */
2640 if (unlikely(current->flags & PF_KTHREAD)) {
2641 ssp = sock->sk->sk_security;
2642 ssp->smk_in = &smack_known_web;
2643 ssp->smk_out = &smack_known_web;
2644 }
2645
2646 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002647 return 0;
2648 /*
2649 * Set the outbound netlbl.
2650 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002651 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2652}
2653
Casey Schaufler69f287a2014-12-12 17:08:40 -08002654#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002655/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002656 * smack_socket_bind - record port binding information.
2657 * @sock: the socket
2658 * @address: the port address
2659 * @addrlen: size of the address
2660 *
2661 * Records the label bound to a port.
2662 *
2663 * Returns 0
2664 */
2665static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2666 int addrlen)
2667{
Casey Schaufler69f287a2014-12-12 17:08:40 -08002668#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002669 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2670 smk_ipv6_port_label(sock, address);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002671#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002672
2673 return 0;
2674}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002675#endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002676
2677/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002678 * smack_socket_connect - connect access check
2679 * @sock: the socket
2680 * @sap: the other end
2681 * @addrlen: size of sap
2682 *
2683 * Verifies that a connection may be possible
2684 *
2685 * Returns 0 on success, and error code otherwise
2686 */
2687static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2688 int addrlen)
2689{
Casey Schauflerc6739442013-05-22 18:42:56 -07002690 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002691
Casey Schauflerc6739442013-05-22 18:42:56 -07002692 if (sock->sk == NULL)
2693 return 0;
2694
2695 switch (sock->sk->sk_family) {
2696 case PF_INET:
2697 if (addrlen < sizeof(struct sockaddr_in))
2698 return -EINVAL;
2699 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2700 break;
2701 case PF_INET6:
2702 if (addrlen < sizeof(struct sockaddr_in6))
2703 return -EINVAL;
Casey Schaufler69f287a2014-12-12 17:08:40 -08002704#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002705 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2706 SMK_CONNECTING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002707#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002708 break;
2709 }
2710 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002711}
2712
2713/**
2714 * smack_flags_to_may - convert S_ to MAY_ values
2715 * @flags: the S_ value
2716 *
2717 * Returns the equivalent MAY_ value
2718 */
2719static int smack_flags_to_may(int flags)
2720{
2721 int may = 0;
2722
2723 if (flags & S_IRUGO)
2724 may |= MAY_READ;
2725 if (flags & S_IWUGO)
2726 may |= MAY_WRITE;
2727 if (flags & S_IXUGO)
2728 may |= MAY_EXEC;
2729
2730 return may;
2731}
2732
2733/**
2734 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2735 * @msg: the object
2736 *
2737 * Returns 0
2738 */
2739static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2740{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002741 struct smack_known *skp = smk_of_current();
2742
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002743 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002744 return 0;
2745}
2746
2747/**
2748 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2749 * @msg: the object
2750 *
2751 * Clears the blob pointer
2752 */
2753static void smack_msg_msg_free_security(struct msg_msg *msg)
2754{
2755 msg->security = NULL;
2756}
2757
2758/**
2759 * smack_of_shm - the smack pointer for the shm
2760 * @shp: the object
2761 *
2762 * Returns a pointer to the smack value
2763 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002764static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002765{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002766 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002767}
2768
2769/**
2770 * smack_shm_alloc_security - Set the security blob for shm
2771 * @shp: the object
2772 *
2773 * Returns 0
2774 */
2775static int smack_shm_alloc_security(struct shmid_kernel *shp)
2776{
2777 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002778 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002779
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002780 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002781 return 0;
2782}
2783
2784/**
2785 * smack_shm_free_security - Clear the security blob for shm
2786 * @shp: the object
2787 *
2788 * Clears the blob pointer
2789 */
2790static void smack_shm_free_security(struct shmid_kernel *shp)
2791{
2792 struct kern_ipc_perm *isp = &shp->shm_perm;
2793
2794 isp->security = NULL;
2795}
2796
2797/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002798 * smk_curacc_shm : check if current has access on shm
2799 * @shp : the object
2800 * @access : access requested
2801 *
2802 * Returns 0 if current has the requested access, error code otherwise
2803 */
2804static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2805{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002806 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002807 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002808 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002809
2810#ifdef CONFIG_AUDIT
2811 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2812 ad.a.u.ipc_id = shp->shm_perm.id;
2813#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002814 rc = smk_curacc(ssp, access, &ad);
2815 rc = smk_bu_current("shm", ssp, access, rc);
2816 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002817}
2818
2819/**
Casey Schauflere114e472008-02-04 22:29:50 -08002820 * smack_shm_associate - Smack access check for shm
2821 * @shp: the object
2822 * @shmflg: access requested
2823 *
2824 * Returns 0 if current has the requested access, error code otherwise
2825 */
2826static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2827{
Casey Schauflere114e472008-02-04 22:29:50 -08002828 int may;
2829
2830 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002831 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002832}
2833
2834/**
2835 * smack_shm_shmctl - Smack access check for shm
2836 * @shp: the object
2837 * @cmd: what it wants to do
2838 *
2839 * Returns 0 if current has the requested access, error code otherwise
2840 */
2841static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2842{
Casey Schauflere114e472008-02-04 22:29:50 -08002843 int may;
2844
2845 switch (cmd) {
2846 case IPC_STAT:
2847 case SHM_STAT:
2848 may = MAY_READ;
2849 break;
2850 case IPC_SET:
2851 case SHM_LOCK:
2852 case SHM_UNLOCK:
2853 case IPC_RMID:
2854 may = MAY_READWRITE;
2855 break;
2856 case IPC_INFO:
2857 case SHM_INFO:
2858 /*
2859 * System level information.
2860 */
2861 return 0;
2862 default:
2863 return -EINVAL;
2864 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002865 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002866}
2867
2868/**
2869 * smack_shm_shmat - Smack access for shmat
2870 * @shp: the object
2871 * @shmaddr: unused
2872 * @shmflg: access requested
2873 *
2874 * Returns 0 if current has the requested access, error code otherwise
2875 */
2876static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2877 int shmflg)
2878{
Casey Schauflere114e472008-02-04 22:29:50 -08002879 int may;
2880
2881 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002882 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002883}
2884
2885/**
2886 * smack_of_sem - the smack pointer for the sem
2887 * @sma: the object
2888 *
2889 * Returns a pointer to the smack value
2890 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002891static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002892{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002893 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002894}
2895
2896/**
2897 * smack_sem_alloc_security - Set the security blob for sem
2898 * @sma: the object
2899 *
2900 * Returns 0
2901 */
2902static int smack_sem_alloc_security(struct sem_array *sma)
2903{
2904 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002905 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002906
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002907 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002908 return 0;
2909}
2910
2911/**
2912 * smack_sem_free_security - Clear the security blob for sem
2913 * @sma: the object
2914 *
2915 * Clears the blob pointer
2916 */
2917static void smack_sem_free_security(struct sem_array *sma)
2918{
2919 struct kern_ipc_perm *isp = &sma->sem_perm;
2920
2921 isp->security = NULL;
2922}
2923
2924/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002925 * smk_curacc_sem : check if current has access on sem
2926 * @sma : the object
2927 * @access : access requested
2928 *
2929 * Returns 0 if current has the requested access, error code otherwise
2930 */
2931static int smk_curacc_sem(struct sem_array *sma, int access)
2932{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002933 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002934 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002935 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002936
2937#ifdef CONFIG_AUDIT
2938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2939 ad.a.u.ipc_id = sma->sem_perm.id;
2940#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002941 rc = smk_curacc(ssp, access, &ad);
2942 rc = smk_bu_current("sem", ssp, access, rc);
2943 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002944}
2945
2946/**
Casey Schauflere114e472008-02-04 22:29:50 -08002947 * smack_sem_associate - Smack access check for sem
2948 * @sma: the object
2949 * @semflg: access requested
2950 *
2951 * Returns 0 if current has the requested access, error code otherwise
2952 */
2953static int smack_sem_associate(struct sem_array *sma, int semflg)
2954{
Casey Schauflere114e472008-02-04 22:29:50 -08002955 int may;
2956
2957 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002958 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002959}
2960
2961/**
2962 * smack_sem_shmctl - Smack access check for sem
2963 * @sma: the object
2964 * @cmd: what it wants to do
2965 *
2966 * Returns 0 if current has the requested access, error code otherwise
2967 */
2968static int smack_sem_semctl(struct sem_array *sma, int cmd)
2969{
Casey Schauflere114e472008-02-04 22:29:50 -08002970 int may;
2971
2972 switch (cmd) {
2973 case GETPID:
2974 case GETNCNT:
2975 case GETZCNT:
2976 case GETVAL:
2977 case GETALL:
2978 case IPC_STAT:
2979 case SEM_STAT:
2980 may = MAY_READ;
2981 break;
2982 case SETVAL:
2983 case SETALL:
2984 case IPC_RMID:
2985 case IPC_SET:
2986 may = MAY_READWRITE;
2987 break;
2988 case IPC_INFO:
2989 case SEM_INFO:
2990 /*
2991 * System level information
2992 */
2993 return 0;
2994 default:
2995 return -EINVAL;
2996 }
2997
Etienne Bassetecfcc532009-04-08 20:40:06 +02002998 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002999}
3000
3001/**
3002 * smack_sem_semop - Smack checks of semaphore operations
3003 * @sma: the object
3004 * @sops: unused
3005 * @nsops: unused
3006 * @alter: unused
3007 *
3008 * Treated as read and write in all cases.
3009 *
3010 * Returns 0 if access is allowed, error code otherwise
3011 */
3012static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
3013 unsigned nsops, int alter)
3014{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003015 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003016}
3017
3018/**
3019 * smack_msg_alloc_security - Set the security blob for msg
3020 * @msq: the object
3021 *
3022 * Returns 0
3023 */
3024static int smack_msg_queue_alloc_security(struct msg_queue *msq)
3025{
3026 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003027 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003028
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003029 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003030 return 0;
3031}
3032
3033/**
3034 * smack_msg_free_security - Clear the security blob for msg
3035 * @msq: the object
3036 *
3037 * Clears the blob pointer
3038 */
3039static void smack_msg_queue_free_security(struct msg_queue *msq)
3040{
3041 struct kern_ipc_perm *kisp = &msq->q_perm;
3042
3043 kisp->security = NULL;
3044}
3045
3046/**
3047 * smack_of_msq - the smack pointer for the msq
3048 * @msq: the object
3049 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003050 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08003051 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003052static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08003053{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003054 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08003055}
3056
3057/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003058 * smk_curacc_msq : helper to check if current has access on msq
3059 * @msq : the msq
3060 * @access : access requested
3061 *
3062 * return 0 if current has access, error otherwise
3063 */
3064static int smk_curacc_msq(struct msg_queue *msq, int access)
3065{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003066 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003067 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003068 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003069
3070#ifdef CONFIG_AUDIT
3071 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3072 ad.a.u.ipc_id = msq->q_perm.id;
3073#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003074 rc = smk_curacc(msp, access, &ad);
3075 rc = smk_bu_current("msq", msp, access, rc);
3076 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003077}
3078
3079/**
Casey Schauflere114e472008-02-04 22:29:50 -08003080 * smack_msg_queue_associate - Smack access check for msg_queue
3081 * @msq: the object
3082 * @msqflg: access requested
3083 *
3084 * Returns 0 if current has the requested access, error code otherwise
3085 */
3086static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
3087{
Casey Schauflere114e472008-02-04 22:29:50 -08003088 int may;
3089
3090 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003091 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003092}
3093
3094/**
3095 * smack_msg_queue_msgctl - Smack access check for msg_queue
3096 * @msq: the object
3097 * @cmd: what it wants to do
3098 *
3099 * Returns 0 if current has the requested access, error code otherwise
3100 */
3101static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
3102{
Casey Schauflere114e472008-02-04 22:29:50 -08003103 int may;
3104
3105 switch (cmd) {
3106 case IPC_STAT:
3107 case MSG_STAT:
3108 may = MAY_READ;
3109 break;
3110 case IPC_SET:
3111 case IPC_RMID:
3112 may = MAY_READWRITE;
3113 break;
3114 case IPC_INFO:
3115 case MSG_INFO:
3116 /*
3117 * System level information
3118 */
3119 return 0;
3120 default:
3121 return -EINVAL;
3122 }
3123
Etienne Bassetecfcc532009-04-08 20:40:06 +02003124 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003125}
3126
3127/**
3128 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3129 * @msq: the object
3130 * @msg: unused
3131 * @msqflg: access requested
3132 *
3133 * Returns 0 if current has the requested access, error code otherwise
3134 */
3135static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3136 int msqflg)
3137{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003138 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003139
Etienne Bassetecfcc532009-04-08 20:40:06 +02003140 may = smack_flags_to_may(msqflg);
3141 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003142}
3143
3144/**
3145 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3146 * @msq: the object
3147 * @msg: unused
3148 * @target: unused
3149 * @type: unused
3150 * @mode: unused
3151 *
3152 * Returns 0 if current has read and write access, error code otherwise
3153 */
3154static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3155 struct task_struct *target, long type, int mode)
3156{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003157 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003158}
3159
3160/**
3161 * smack_ipc_permission - Smack access for ipc_permission()
3162 * @ipp: the object permissions
3163 * @flag: access requested
3164 *
3165 * Returns 0 if current has read and write access, error code otherwise
3166 */
3167static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3168{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003169 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003170 int may = smack_flags_to_may(flag);
3171 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003172 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003173
Etienne Bassetecfcc532009-04-08 20:40:06 +02003174#ifdef CONFIG_AUDIT
3175 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3176 ad.a.u.ipc_id = ipp->id;
3177#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003178 rc = smk_curacc(iskp, may, &ad);
3179 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003180 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003181}
3182
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003183/**
3184 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003185 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003186 * @secid: where result will be saved
3187 */
3188static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3189{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003190 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003191
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003192 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003193}
3194
Casey Schauflere114e472008-02-04 22:29:50 -08003195/**
3196 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003197 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003198 * @inode: the object
3199 *
3200 * Set the inode's security blob if it hasn't been done already.
3201 */
3202static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3203{
3204 struct super_block *sbp;
3205 struct superblock_smack *sbsp;
3206 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003207 struct smack_known *skp;
3208 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003209 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003210 char trattr[TRANS_TRUE_SIZE];
3211 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003212 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003213 struct dentry *dp;
3214
3215 if (inode == NULL)
3216 return;
3217
3218 isp = inode->i_security;
3219
3220 mutex_lock(&isp->smk_lock);
3221 /*
3222 * If the inode is already instantiated
3223 * take the quick way out
3224 */
3225 if (isp->smk_flags & SMK_INODE_INSTANT)
3226 goto unlockandout;
3227
3228 sbp = inode->i_sb;
3229 sbsp = sbp->s_security;
3230 /*
3231 * We're going to use the superblock default label
3232 * if there's no label on the file.
3233 */
3234 final = sbsp->smk_default;
3235
3236 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003237 * If this is the root inode the superblock
3238 * may be in the process of initialization.
3239 * If that is the case use the root value out
3240 * of the superblock.
3241 */
3242 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003243 switch (sbp->s_magic) {
3244 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003245 /*
3246 * The cgroup filesystem is never mounted,
3247 * so there's no opportunity to set the mount
3248 * options.
3249 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003250 sbsp->smk_root = &smack_known_star;
3251 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003252 isp->smk_inode = sbsp->smk_root;
3253 break;
3254 case TMPFS_MAGIC:
3255 /*
3256 * What about shmem/tmpfs anonymous files with dentry
3257 * obtained from d_alloc_pseudo()?
3258 */
3259 isp->smk_inode = smk_of_current();
3260 break;
3261 default:
3262 isp->smk_inode = sbsp->smk_root;
3263 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003264 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003265 isp->smk_flags |= SMK_INODE_INSTANT;
3266 goto unlockandout;
3267 }
3268
3269 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003270 * This is pretty hackish.
3271 * Casey says that we shouldn't have to do
3272 * file system specific code, but it does help
3273 * with keeping it simple.
3274 */
3275 switch (sbp->s_magic) {
3276 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003277 case PIPEFS_MAGIC:
3278 case SOCKFS_MAGIC:
3279 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003280 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003281 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003282 * that the smack file system doesn't do
3283 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003284 *
Casey Schauflere114e472008-02-04 22:29:50 -08003285 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003286 *
3287 * Socket access is controlled by the socket
3288 * structures associated with the task involved.
3289 *
3290 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003291 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003292 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003293 break;
3294 case DEVPTS_SUPER_MAGIC:
3295 /*
3296 * devpts seems content with the label of the task.
3297 * Programs that change smack have to treat the
3298 * pty with respect.
3299 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003300 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003301 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003302 case PROC_SUPER_MAGIC:
3303 /*
3304 * Casey says procfs appears not to care.
3305 * The superblock default suffices.
3306 */
3307 break;
3308 case TMPFS_MAGIC:
3309 /*
3310 * Device labels should come from the filesystem,
3311 * but watch out, because they're volitile,
3312 * getting recreated on every reboot.
3313 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003314 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003315 /*
3316 * No break.
3317 *
3318 * If a smack value has been set we want to use it,
3319 * but since tmpfs isn't giving us the opportunity
3320 * to set mount options simulate setting the
3321 * superblock default.
3322 */
3323 default:
3324 /*
3325 * This isn't an understood special case.
3326 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003327 */
3328
3329 /*
3330 * UNIX domain sockets use lower level socket data.
3331 */
3332 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003333 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003334 break;
3335 }
3336 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003337 * No xattr support means, alas, no SMACK label.
3338 * Use the aforeapplied default.
3339 * It would be curious if the label of the task
3340 * does not match that assigned.
3341 */
3342 if (inode->i_op->getxattr == NULL)
3343 break;
3344 /*
3345 * Get the dentry for xattr.
3346 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003347 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003348 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003349 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003350 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003351
3352 /*
3353 * Transmuting directory
3354 */
3355 if (S_ISDIR(inode->i_mode)) {
3356 /*
3357 * If this is a new directory and the label was
3358 * transmuted when the inode was initialized
3359 * set the transmute attribute on the directory
3360 * and mark the inode.
3361 *
3362 * If there is a transmute attribute on the
3363 * directory mark the inode.
3364 */
3365 if (isp->smk_flags & SMK_INODE_CHANGED) {
3366 isp->smk_flags &= ~SMK_INODE_CHANGED;
3367 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003368 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003369 TRANS_TRUE, TRANS_TRUE_SIZE,
3370 0);
3371 } else {
3372 rc = inode->i_op->getxattr(dp,
3373 XATTR_NAME_SMACKTRANSMUTE, trattr,
3374 TRANS_TRUE_SIZE);
3375 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3376 TRANS_TRUE_SIZE) != 0)
3377 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003378 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003379 if (rc >= 0)
3380 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003381 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003382 /*
3383 * Don't let the exec or mmap label be "*" or "@".
3384 */
3385 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003386 if (IS_ERR(skp) || skp == &smack_known_star ||
3387 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003388 skp = NULL;
3389 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003390
Casey Schaufler19760ad2013-12-16 16:27:26 -08003391 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003392 if (IS_ERR(skp) || skp == &smack_known_star ||
3393 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003394 skp = NULL;
3395 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003396
Casey Schauflere114e472008-02-04 22:29:50 -08003397 dput(dp);
3398 break;
3399 }
3400
3401 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003402 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003403 else
3404 isp->smk_inode = final;
3405
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003406 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003407
3408unlockandout:
3409 mutex_unlock(&isp->smk_lock);
3410 return;
3411}
3412
3413/**
3414 * smack_getprocattr - Smack process attribute access
3415 * @p: the object task
3416 * @name: the name of the attribute in /proc/.../attr
3417 * @value: where to put the result
3418 *
3419 * Places a copy of the task Smack into value
3420 *
3421 * Returns the length of the smack label or an error code
3422 */
3423static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3424{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003425 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003426 char *cp;
3427 int slen;
3428
3429 if (strcmp(name, "current") != 0)
3430 return -EINVAL;
3431
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003432 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003433 if (cp == NULL)
3434 return -ENOMEM;
3435
3436 slen = strlen(cp);
3437 *value = cp;
3438 return slen;
3439}
3440
3441/**
3442 * smack_setprocattr - Smack process attribute setting
3443 * @p: the object task
3444 * @name: the name of the attribute in /proc/.../attr
3445 * @value: the value to set
3446 * @size: the size of the value
3447 *
3448 * Sets the Smack value of the task. Only setting self
3449 * is permitted and only with privilege
3450 *
3451 * Returns the length of the smack label or an error code
3452 */
3453static int smack_setprocattr(struct task_struct *p, char *name,
3454 void *value, size_t size)
3455{
Casey Schaufler676dac42010-12-02 06:43:39 -08003456 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003457 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003458 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003459
Casey Schauflere114e472008-02-04 22:29:50 -08003460 /*
3461 * Changing another process' Smack value is too dangerous
3462 * and supports no sane use case.
3463 */
3464 if (p != current)
3465 return -EPERM;
3466
Casey Schaufler1880eff2012-06-05 15:28:30 -07003467 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003468 return -EPERM;
3469
Casey Schauflerf7112e62012-05-06 15:22:02 -07003470 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003471 return -EINVAL;
3472
3473 if (strcmp(name, "current") != 0)
3474 return -EINVAL;
3475
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003476 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003477 if (IS_ERR(skp))
3478 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003479
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003480 /*
3481 * No process is ever allowed the web ("@") label.
3482 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003483 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003484 return -EPERM;
3485
David Howellsd84f4f92008-11-14 10:39:23 +11003486 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003487 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003488 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003489
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003490 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003491 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003492
David Howellsd84f4f92008-11-14 10:39:23 +11003493 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003494 return size;
3495}
3496
3497/**
3498 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003499 * @sock: one sock
3500 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003501 * @newsk: unused
3502 *
3503 * Return 0 if a subject with the smack of sock could access
3504 * an object with the smack of other, otherwise an error code
3505 */
David S. Miller3610cda2011-01-05 15:38:53 -08003506static int smack_unix_stream_connect(struct sock *sock,
3507 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003508{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003509 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003510 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003511 struct socket_smack *ssp = sock->sk_security;
3512 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003513 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003514 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003515 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003516#ifdef CONFIG_AUDIT
3517 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003518#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003519
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003520 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3521 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003522 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003523#ifdef CONFIG_AUDIT
3524 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3525 smk_ad_setfield_u_net_sk(&ad, other);
3526#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003527 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3528 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003529 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003530 okp = osp->smk_out;
3531 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003532 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003533 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003534 MAY_WRITE, rc);
3535 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003536 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003537
Casey Schaufler975d5e52011-09-26 14:43:39 -07003538 /*
3539 * Cross reference the peer labels for SO_PEERSEC.
3540 */
3541 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003542 nsp->smk_packet = ssp->smk_out;
3543 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003544 }
3545
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003546 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003547}
3548
3549/**
3550 * smack_unix_may_send - Smack access on UDS
3551 * @sock: one socket
3552 * @other: the other socket
3553 *
3554 * Return 0 if a subject with the smack of sock could access
3555 * an object with the smack of other, otherwise an error code
3556 */
3557static int smack_unix_may_send(struct socket *sock, struct socket *other)
3558{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003559 struct socket_smack *ssp = sock->sk->sk_security;
3560 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003561 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003562 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003563
Kees Cook923e9a12012-04-10 13:26:44 -07003564#ifdef CONFIG_AUDIT
3565 struct lsm_network_audit net;
3566
Eric Paris48c62af2012-04-02 13:15:44 -04003567 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003568 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003569#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003570
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003571 if (smack_privileged(CAP_MAC_OVERRIDE))
3572 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003573
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003574 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3575 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003576 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003577}
3578
3579/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003580 * smack_socket_sendmsg - Smack check based on destination host
3581 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003582 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003583 * @size: the size of the message
3584 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003585 * Return 0 if the current subject can write to the destination host.
3586 * For IPv4 this is only a question if the destination is a single label host.
3587 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003588 */
3589static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3590 int size)
3591{
3592 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003593#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003594 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003595#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003596 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003597
3598 /*
3599 * Perfectly reasonable for this to be NULL
3600 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003601 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003602 return 0;
3603
Casey Schauflerc6739442013-05-22 18:42:56 -07003604 switch (sip->sin_family) {
3605 case AF_INET:
3606 rc = smack_netlabel_send(sock->sk, sip);
3607 break;
3608 case AF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003609#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07003610 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003611#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003612 break;
3613 }
3614 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003615}
3616
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003617/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003618 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003619 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003620 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003621 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003622 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003623 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003624static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3625 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003626{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003627 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003628 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003629 int acat;
3630 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003631
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003632 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003633 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003634 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003635 * If there are flags but no level netlabel isn't
3636 * behaving the way we expect it to.
3637 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003638 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003639 * Without guidance regarding the smack value
3640 * for the packet fall back on the network
3641 * ambient value.
3642 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003643 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003644 list_for_each_entry(skp, &smack_known_list, list) {
3645 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003646 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003647 /*
3648 * Compare the catsets. Use the netlbl APIs.
3649 */
3650 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3651 if ((skp->smk_netlabel.flags &
3652 NETLBL_SECATTR_MLS_CAT) == 0)
3653 found = 1;
3654 break;
3655 }
3656 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003657 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3658 acat + 1);
3659 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003660 skp->smk_netlabel.attr.mls.cat,
3661 kcat + 1);
3662 if (acat < 0 || kcat < 0)
3663 break;
3664 }
3665 if (acat == kcat) {
3666 found = 1;
3667 break;
3668 }
Casey Schauflere114e472008-02-04 22:29:50 -08003669 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003670 rcu_read_unlock();
3671
3672 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003673 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003674
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003675 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003676 return &smack_known_web;
3677 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003678 }
3679 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3680 /*
3681 * Looks like a fallback, which gives us a secid.
3682 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003683 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003684 /*
3685 * This has got to be a bug because it is
3686 * impossible to specify a fallback without
3687 * specifying the label, which will ensure
3688 * it has a secid, and the only way to get a
3689 * secid is from a fallback.
3690 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003691 BUG_ON(skp == NULL);
3692 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003693 }
3694 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003695 * Without guidance regarding the smack value
3696 * for the packet fall back on the network
3697 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003698 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003699 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003700}
3701
Casey Schaufler69f287a2014-12-12 17:08:40 -08003702#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003703static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003704{
Casey Schauflerc6739442013-05-22 18:42:56 -07003705 u8 nexthdr;
3706 int offset;
3707 int proto = -EINVAL;
3708 struct ipv6hdr _ipv6h;
3709 struct ipv6hdr *ip6;
3710 __be16 frag_off;
3711 struct tcphdr _tcph, *th;
3712 struct udphdr _udph, *uh;
3713 struct dccp_hdr _dccph, *dh;
3714
3715 sip->sin6_port = 0;
3716
3717 offset = skb_network_offset(skb);
3718 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3719 if (ip6 == NULL)
3720 return -EINVAL;
3721 sip->sin6_addr = ip6->saddr;
3722
3723 nexthdr = ip6->nexthdr;
3724 offset += sizeof(_ipv6h);
3725 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3726 if (offset < 0)
3727 return -EINVAL;
3728
3729 proto = nexthdr;
3730 switch (proto) {
3731 case IPPROTO_TCP:
3732 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3733 if (th != NULL)
3734 sip->sin6_port = th->source;
3735 break;
3736 case IPPROTO_UDP:
3737 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3738 if (uh != NULL)
3739 sip->sin6_port = uh->source;
3740 break;
3741 case IPPROTO_DCCP:
3742 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3743 if (dh != NULL)
3744 sip->sin6_port = dh->dccph_sport;
3745 break;
3746 }
3747 return proto;
3748}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003749#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003750
Casey Schauflere114e472008-02-04 22:29:50 -08003751/**
3752 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3753 * @sk: socket
3754 * @skb: packet
3755 *
3756 * Returns 0 if the packet should be delivered, an error code otherwise
3757 */
3758static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3759{
3760 struct netlbl_lsm_secattr secattr;
3761 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003762 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003763 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003764 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003765#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003766 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003767#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003768#if IS_ENABLED(CONFIG_IPV6)
3769 struct sockaddr_in6 sadd;
3770 int proto;
3771#endif /* CONFIG_IPV6 */
3772
Casey Schauflerc6739442013-05-22 18:42:56 -07003773 switch (sk->sk_family) {
3774 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003775#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3776 /*
3777 * If there is a secmark use it rather than the CIPSO label.
3778 * If there is no secmark fall back to CIPSO.
3779 * The secmark is assumed to reflect policy better.
3780 */
3781 if (skb && skb->secmark != 0) {
3782 skp = smack_from_secid(skb->secmark);
3783 goto access_check;
3784 }
3785#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003786 /*
3787 * Translate what netlabel gave us.
3788 */
3789 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003790
Casey Schauflerc6739442013-05-22 18:42:56 -07003791 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3792 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003793 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003794 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003795 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003796
Casey Schauflerc6739442013-05-22 18:42:56 -07003797 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003798
Casey Schaufler69f287a2014-12-12 17:08:40 -08003799#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3800access_check:
3801#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003802#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003803 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3804 ad.a.u.net->family = sk->sk_family;
3805 ad.a.u.net->netif = skb->skb_iif;
3806 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003807#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003808 /*
3809 * Receiving a packet requires that the other end
3810 * be able to write here. Read access is not required.
3811 * This is the simplist possible security model
3812 * for networking.
3813 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003814 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3815 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003816 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003817 if (rc != 0)
3818 netlbl_skbuff_err(skb, rc, 0);
3819 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003820#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003821 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003822 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3823 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3824 break;
3825#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3826 if (skb && skb->secmark != 0)
3827 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003828 else
Casey Schaufler69f287a2014-12-12 17:08:40 -08003829 skp = smack_net_ambient;
3830#ifdef CONFIG_AUDIT
3831 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3832 ad.a.u.net->family = sk->sk_family;
3833 ad.a.u.net->netif = skb->skb_iif;
3834 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3835#endif /* CONFIG_AUDIT */
3836 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3837 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3838 MAY_WRITE, rc);
3839#else /* CONFIG_SECURITY_SMACK_NETFILTER */
3840 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3841#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003842 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003843#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003844 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003845
Paul Moorea8134292008-10-10 10:16:31 -04003846 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003847}
3848
3849/**
3850 * smack_socket_getpeersec_stream - pull in packet label
3851 * @sock: the socket
3852 * @optval: user's destination
3853 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003854 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003855 *
3856 * returns zero on success, an error code otherwise
3857 */
3858static int smack_socket_getpeersec_stream(struct socket *sock,
3859 char __user *optval,
3860 int __user *optlen, unsigned len)
3861{
3862 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003863 char *rcp = "";
3864 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003865 int rc = 0;
3866
3867 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003868 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003869 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003870 slen = strlen(rcp) + 1;
3871 }
Casey Schauflere114e472008-02-04 22:29:50 -08003872
3873 if (slen > len)
3874 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003875 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003876 rc = -EFAULT;
3877
3878 if (put_user(slen, optlen) != 0)
3879 rc = -EFAULT;
3880
3881 return rc;
3882}
3883
3884
3885/**
3886 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003887 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003888 * @skb: packet data
3889 * @secid: pointer to where to put the secid of the packet
3890 *
3891 * Sets the netlabel socket state on sk from parent
3892 */
3893static int smack_socket_getpeersec_dgram(struct socket *sock,
3894 struct sk_buff *skb, u32 *secid)
3895
3896{
3897 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003898 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003899 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003900 int family = PF_UNSPEC;
3901 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003902 int rc;
3903
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003904 if (skb != NULL) {
3905 if (skb->protocol == htons(ETH_P_IP))
3906 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003907#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003908 else if (skb->protocol == htons(ETH_P_IPV6))
3909 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003910#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003911 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003912 if (family == PF_UNSPEC && sock != NULL)
3913 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003914
Casey Schaufler69f287a2014-12-12 17:08:40 -08003915 switch (family) {
3916 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003917 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003918 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003919 break;
3920 case PF_INET:
3921#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3922 s = skb->secmark;
3923 if (s != 0)
3924 break;
3925#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003926 /*
3927 * Translate what netlabel gave us.
3928 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003929 if (sock != NULL && sock->sk != NULL)
3930 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003931 netlbl_secattr_init(&secattr);
3932 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3933 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003934 skp = smack_from_secattr(&secattr, ssp);
3935 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003936 }
3937 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003938 break;
3939#if IS_ENABLED(CONFIG_IPV6)
3940 case PF_INET6:
3941#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3942 s = skb->secmark;
3943#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3944 break;
3945#endif /* CONFIG_IPV6 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003946 }
3947 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003948 if (s == 0)
3949 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003950 return 0;
3951}
3952
3953/**
Paul Moore07feee82009-03-27 17:10:54 -04003954 * smack_sock_graft - Initialize a newly created socket with an existing sock
3955 * @sk: child sock
3956 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003957 *
Paul Moore07feee82009-03-27 17:10:54 -04003958 * Set the smk_{in,out} state of an existing sock based on the process that
3959 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003960 */
3961static void smack_sock_graft(struct sock *sk, struct socket *parent)
3962{
3963 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003964 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003965
Paul Moore07feee82009-03-27 17:10:54 -04003966 if (sk == NULL ||
3967 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003968 return;
3969
3970 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003971 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003972 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003973 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003974}
3975
3976/**
3977 * smack_inet_conn_request - Smack access check on connect
3978 * @sk: socket involved
3979 * @skb: packet
3980 * @req: unused
3981 *
3982 * Returns 0 if a task with the packet label could write to
3983 * the socket, otherwise an error code
3984 */
3985static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3986 struct request_sock *req)
3987{
Paul Moore07feee82009-03-27 17:10:54 -04003988 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003989 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003990 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003991 struct netlbl_lsm_secattr secattr;
3992 struct sockaddr_in addr;
3993 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003994 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003995 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003996 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003997#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003998 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003999#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004000
Casey Schaufler69f287a2014-12-12 17:08:40 -08004001#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004002 if (family == PF_INET6) {
4003 /*
4004 * Handle mapped IPv4 packets arriving
4005 * via IPv6 sockets. Don't set up netlabel
4006 * processing on IPv6.
4007 */
4008 if (skb->protocol == htons(ETH_P_IP))
4009 family = PF_INET;
4010 else
4011 return 0;
4012 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004013#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004014
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004015#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4016 /*
4017 * If there is a secmark use it rather than the CIPSO label.
4018 * If there is no secmark fall back to CIPSO.
4019 * The secmark is assumed to reflect policy better.
4020 */
4021 if (skb && skb->secmark != 0) {
4022 skp = smack_from_secid(skb->secmark);
4023 goto access_check;
4024 }
4025#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4026
Paul Moore07feee82009-03-27 17:10:54 -04004027 netlbl_secattr_init(&secattr);
4028 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004029 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004030 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004031 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004032 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004033 netlbl_secattr_destroy(&secattr);
4034
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004035#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4036access_check:
4037#endif
4038
Etienne Bassetecfcc532009-04-08 20:40:06 +02004039#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004040 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4041 ad.a.u.net->family = family;
4042 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004043 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4044#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004045 /*
Paul Moore07feee82009-03-27 17:10:54 -04004046 * Receiving a packet requires that the other end be able to write
4047 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004048 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004049 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4050 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004051 if (rc != 0)
4052 return rc;
4053
4054 /*
4055 * Save the peer's label in the request_sock so we can later setup
4056 * smk_packet in the child socket so that SO_PEERCRED can report it.
4057 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004058 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004059
4060 /*
4061 * We need to decide if we want to label the incoming connection here
4062 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004063 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004064 */
4065 hdr = ip_hdr(skb);
4066 addr.sin_addr.s_addr = hdr->saddr;
4067 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004068 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004069 rcu_read_unlock();
4070
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004071 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004072 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004073 else
Paul Moore07feee82009-03-27 17:10:54 -04004074 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004075
4076 return rc;
4077}
4078
Paul Moore07feee82009-03-27 17:10:54 -04004079/**
4080 * smack_inet_csk_clone - Copy the connection information to the new socket
4081 * @sk: the new socket
4082 * @req: the connection's request_sock
4083 *
4084 * Transfer the connection's peer label to the newly created socket.
4085 */
4086static void smack_inet_csk_clone(struct sock *sk,
4087 const struct request_sock *req)
4088{
4089 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004090 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004091
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004092 if (req->peer_secid != 0) {
4093 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004094 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004095 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004096 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004097}
4098
Casey Schauflere114e472008-02-04 22:29:50 -08004099/*
4100 * Key management security hooks
4101 *
4102 * Casey has not tested key support very heavily.
4103 * The permission check is most likely too restrictive.
4104 * If you care about keys please have a look.
4105 */
4106#ifdef CONFIG_KEYS
4107
4108/**
4109 * smack_key_alloc - Set the key security blob
4110 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004111 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004112 * @flags: unused
4113 *
4114 * No allocation required
4115 *
4116 * Returns 0
4117 */
David Howellsd84f4f92008-11-14 10:39:23 +11004118static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004119 unsigned long flags)
4120{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004121 struct smack_known *skp = smk_of_task(cred->security);
4122
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004123 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004124 return 0;
4125}
4126
4127/**
4128 * smack_key_free - Clear the key security blob
4129 * @key: the object
4130 *
4131 * Clear the blob pointer
4132 */
4133static void smack_key_free(struct key *key)
4134{
4135 key->security = NULL;
4136}
4137
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004138/**
Casey Schauflere114e472008-02-04 22:29:50 -08004139 * smack_key_permission - Smack access on a key
4140 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004141 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004142 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004143 *
4144 * Return 0 if the task has read and write to the object,
4145 * an error code otherwise
4146 */
4147static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004148 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004149{
4150 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004151 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004152 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004153 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004154 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004155
4156 keyp = key_ref_to_ptr(key_ref);
4157 if (keyp == NULL)
4158 return -EINVAL;
4159 /*
4160 * If the key hasn't been initialized give it access so that
4161 * it may do so.
4162 */
4163 if (keyp->security == NULL)
4164 return 0;
4165 /*
4166 * This should not occur
4167 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004168 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004169 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004170#ifdef CONFIG_AUDIT
4171 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4172 ad.a.u.key_struct.key = keyp->serial;
4173 ad.a.u.key_struct.key_desc = keyp->description;
4174#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004175 if (perm & KEY_NEED_READ)
4176 request = MAY_READ;
4177 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4178 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004179 rc = smk_access(tkp, keyp->security, request, &ad);
4180 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4181 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004182}
José Bollo7fc5f362015-02-17 15:41:22 +01004183
4184/*
4185 * smack_key_getsecurity - Smack label tagging the key
4186 * @key points to the key to be queried
4187 * @_buffer points to a pointer that should be set to point to the
4188 * resulting string (if no label or an error occurs).
4189 * Return the length of the string (including terminating NUL) or -ve if
4190 * an error.
4191 * May also return 0 (and a NULL buffer pointer) if there is no label.
4192 */
4193static int smack_key_getsecurity(struct key *key, char **_buffer)
4194{
4195 struct smack_known *skp = key->security;
4196 size_t length;
4197 char *copy;
4198
4199 if (key->security == NULL) {
4200 *_buffer = NULL;
4201 return 0;
4202 }
4203
4204 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4205 if (copy == NULL)
4206 return -ENOMEM;
4207 length = strlen(copy) + 1;
4208
4209 *_buffer = copy;
4210 return length;
4211}
4212
Casey Schauflere114e472008-02-04 22:29:50 -08004213#endif /* CONFIG_KEYS */
4214
4215/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004216 * Smack Audit hooks
4217 *
4218 * Audit requires a unique representation of each Smack specific
4219 * rule. This unique representation is used to distinguish the
4220 * object to be audited from remaining kernel objects and also
4221 * works as a glue between the audit hooks.
4222 *
4223 * Since repository entries are added but never deleted, we'll use
4224 * the smack_known label address related to the given audit rule as
4225 * the needed unique representation. This also better fits the smack
4226 * model where nearly everything is a label.
4227 */
4228#ifdef CONFIG_AUDIT
4229
4230/**
4231 * smack_audit_rule_init - Initialize a smack audit rule
4232 * @field: audit rule fields given from user-space (audit.h)
4233 * @op: required testing operator (=, !=, >, <, ...)
4234 * @rulestr: smack label to be audited
4235 * @vrule: pointer to save our own audit rule representation
4236 *
4237 * Prepare to audit cases where (@field @op @rulestr) is true.
4238 * The label to be audited is created if necessay.
4239 */
4240static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4241{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004242 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004243 char **rule = (char **)vrule;
4244 *rule = NULL;
4245
4246 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4247 return -EINVAL;
4248
Al Viro5af75d82008-12-16 05:59:26 -05004249 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004250 return -EINVAL;
4251
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004252 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004253 if (IS_ERR(skp))
4254 return PTR_ERR(skp);
4255
4256 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004257
4258 return 0;
4259}
4260
4261/**
4262 * smack_audit_rule_known - Distinguish Smack audit rules
4263 * @krule: rule of interest, in Audit kernel representation format
4264 *
4265 * This is used to filter Smack rules from remaining Audit ones.
4266 * If it's proved that this rule belongs to us, the
4267 * audit_rule_match hook will be called to do the final judgement.
4268 */
4269static int smack_audit_rule_known(struct audit_krule *krule)
4270{
4271 struct audit_field *f;
4272 int i;
4273
4274 for (i = 0; i < krule->field_count; i++) {
4275 f = &krule->fields[i];
4276
4277 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4278 return 1;
4279 }
4280
4281 return 0;
4282}
4283
4284/**
4285 * smack_audit_rule_match - Audit given object ?
4286 * @secid: security id for identifying the object to test
4287 * @field: audit rule flags given from user-space
4288 * @op: required testing operator
4289 * @vrule: smack internal rule presentation
4290 * @actx: audit context associated with the check
4291 *
4292 * The core Audit hook. It's used to take the decision of
4293 * whether to audit or not to audit a given object.
4294 */
4295static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4296 struct audit_context *actx)
4297{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004298 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004299 char *rule = vrule;
4300
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004301 if (unlikely(!rule)) {
4302 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004303 return -ENOENT;
4304 }
4305
4306 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4307 return 0;
4308
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004309 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004310
4311 /*
4312 * No need to do string comparisons. If a match occurs,
4313 * both pointers will point to the same smack_known
4314 * label.
4315 */
Al Viro5af75d82008-12-16 05:59:26 -05004316 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004317 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004318 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004319 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004320
4321 return 0;
4322}
4323
4324/**
4325 * smack_audit_rule_free - free smack rule representation
4326 * @vrule: rule to be freed.
4327 *
4328 * No memory was allocated.
4329 */
4330static void smack_audit_rule_free(void *vrule)
4331{
4332 /* No-op */
4333}
4334
4335#endif /* CONFIG_AUDIT */
4336
Randy Dunlap251a2a92009-02-18 11:42:33 -08004337/**
David Quigley746df9b2013-05-22 12:50:35 -04004338 * smack_ismaclabel - check if xattr @name references a smack MAC label
4339 * @name: Full xattr name to check.
4340 */
4341static int smack_ismaclabel(const char *name)
4342{
4343 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4344}
4345
4346
4347/**
Casey Schauflere114e472008-02-04 22:29:50 -08004348 * smack_secid_to_secctx - return the smack label for a secid
4349 * @secid: incoming integer
4350 * @secdata: destination
4351 * @seclen: how long it is
4352 *
4353 * Exists for networking code.
4354 */
4355static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4356{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004357 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004358
Eric Parisd5630b92010-10-13 16:24:48 -04004359 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004360 *secdata = skp->smk_known;
4361 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004362 return 0;
4363}
4364
Randy Dunlap251a2a92009-02-18 11:42:33 -08004365/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004366 * smack_secctx_to_secid - return the secid for a smack label
4367 * @secdata: smack label
4368 * @seclen: how long result is
4369 * @secid: outgoing integer
4370 *
4371 * Exists for audit and networking code.
4372 */
David Howellse52c17642008-04-29 20:52:51 +01004373static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004374{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004375 struct smack_known *skp = smk_find_entry(secdata);
4376
4377 if (skp)
4378 *secid = skp->smk_secid;
4379 else
4380 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004381 return 0;
4382}
4383
Randy Dunlap251a2a92009-02-18 11:42:33 -08004384/**
Casey Schauflere114e472008-02-04 22:29:50 -08004385 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004386 * @secdata: unused
4387 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004388 *
4389 * Exists to make sure nothing gets done, and properly
4390 */
4391static void smack_release_secctx(char *secdata, u32 seclen)
4392{
4393}
4394
David P. Quigley1ee65e32009-09-03 14:25:57 -04004395static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4396{
4397 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4398}
4399
4400static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4401{
4402 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4403}
4404
4405static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4406{
4407 int len = 0;
4408 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4409
4410 if (len < 0)
4411 return len;
4412 *ctxlen = len;
4413 return 0;
4414}
4415
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004416struct security_hook_list smack_hooks[] = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004417 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4418 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4419 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004420
Casey Schauflere20b0432015-05-02 15:11:36 -07004421 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4422 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4423 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4424 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4425 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304426 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4427 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
Casey Schauflere114e472008-02-04 22:29:50 -08004428
Casey Schauflere20b0432015-05-02 15:11:36 -07004429 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4430 LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
4431 LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004432
Casey Schauflere20b0432015-05-02 15:11:36 -07004433 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4434 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4435 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4436 LSM_HOOK_INIT(inode_link, smack_inode_link),
4437 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4438 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4439 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4440 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4441 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4442 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4443 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4444 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4445 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4446 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4447 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4448 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4449 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4450 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004451
Casey Schauflere20b0432015-05-02 15:11:36 -07004452 LSM_HOOK_INIT(file_permission, smack_file_permission),
4453 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4454 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4455 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4456 LSM_HOOK_INIT(file_lock, smack_file_lock),
4457 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4458 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4459 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4460 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4461 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4462 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004463
Casey Schauflere20b0432015-05-02 15:11:36 -07004464 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004465
Casey Schauflere20b0432015-05-02 15:11:36 -07004466 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4467 LSM_HOOK_INIT(cred_free, smack_cred_free),
4468 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4469 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4470 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4471 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4472 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4473 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4474 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4475 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4476 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4477 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4478 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4479 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4480 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4481 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4482 LSM_HOOK_INIT(task_kill, smack_task_kill),
4483 LSM_HOOK_INIT(task_wait, smack_task_wait),
4484 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004485
Casey Schauflere20b0432015-05-02 15:11:36 -07004486 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4487 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004488
Casey Schauflere20b0432015-05-02 15:11:36 -07004489 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4490 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004491
Casey Schauflere20b0432015-05-02 15:11:36 -07004492 LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
4493 LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
4494 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4495 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4496 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4497 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004498
Casey Schauflere20b0432015-05-02 15:11:36 -07004499 LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
4500 LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
4501 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4502 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4503 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004504
Casey Schauflere20b0432015-05-02 15:11:36 -07004505 LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
4506 LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
4507 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4508 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4509 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004510
Casey Schauflere20b0432015-05-02 15:11:36 -07004511 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004512
Casey Schauflere20b0432015-05-02 15:11:36 -07004513 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4514 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004515
Casey Schauflere20b0432015-05-02 15:11:36 -07004516 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4517 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004518
Casey Schauflere20b0432015-05-02 15:11:36 -07004519 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Casey Schaufler69f287a2014-12-12 17:08:40 -08004520#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schauflere20b0432015-05-02 15:11:36 -07004521 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler69f287a2014-12-12 17:08:40 -08004522#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflere20b0432015-05-02 15:11:36 -07004523 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4524 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4525 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4526 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4527 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4528 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4529 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4530 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4531 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4532 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004533
Casey Schauflere114e472008-02-04 22:29:50 -08004534 /* key management security hooks */
4535#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004536 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4537 LSM_HOOK_INIT(key_free, smack_key_free),
4538 LSM_HOOK_INIT(key_permission, smack_key_permission),
4539 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004540#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004541
4542 /* Audit hooks */
4543#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004544 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4545 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4546 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4547 LSM_HOOK_INIT(audit_rule_free, smack_audit_rule_free),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004548#endif /* CONFIG_AUDIT */
4549
Casey Schauflere20b0432015-05-02 15:11:36 -07004550 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4551 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4552 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4553 LSM_HOOK_INIT(release_secctx, smack_release_secctx),
4554 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4555 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4556 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflere114e472008-02-04 22:29:50 -08004557};
4558
Etienne Basset7198e2e2009-03-24 20:53:24 +01004559
Casey Schaufler86812bb2012-04-17 18:55:46 -07004560static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004561{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004562 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004563 * Initialize rule list locks
4564 */
4565 mutex_init(&smack_known_huh.smk_rules_lock);
4566 mutex_init(&smack_known_hat.smk_rules_lock);
4567 mutex_init(&smack_known_floor.smk_rules_lock);
4568 mutex_init(&smack_known_star.smk_rules_lock);
4569 mutex_init(&smack_known_invalid.smk_rules_lock);
4570 mutex_init(&smack_known_web.smk_rules_lock);
4571 /*
4572 * Initialize rule lists
4573 */
4574 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4575 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4576 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4577 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4578 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4579 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4580 /*
4581 * Create the known labels list
4582 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004583 smk_insert_entry(&smack_known_huh);
4584 smk_insert_entry(&smack_known_hat);
4585 smk_insert_entry(&smack_known_star);
4586 smk_insert_entry(&smack_known_floor);
4587 smk_insert_entry(&smack_known_invalid);
4588 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004589}
4590
Casey Schauflere114e472008-02-04 22:29:50 -08004591/**
4592 * smack_init - initialize the smack system
4593 *
4594 * Returns 0
4595 */
4596static __init int smack_init(void)
4597{
David Howellsd84f4f92008-11-14 10:39:23 +11004598 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004599 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004600
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004601 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004602 return 0;
4603
Casey Schaufler69f287a2014-12-12 17:08:40 -08004604 smack_enabled = 1;
4605
Rohit1a5b4722014-10-15 17:40:41 +05304606 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4607 if (!smack_inode_cache)
4608 return -ENOMEM;
4609
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004610 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4611 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304612 if (tsp == NULL) {
4613 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004614 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304615 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004616
Casey Schauflere114e472008-02-04 22:29:50 -08004617 printk(KERN_INFO "Smack: Initializing.\n");
4618
4619 /*
4620 * Set the security state for the initial task.
4621 */
David Howellsd84f4f92008-11-14 10:39:23 +11004622 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004623 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004624
Casey Schaufler86812bb2012-04-17 18:55:46 -07004625 /* initialize the smack_known_list */
4626 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004627
4628 /*
4629 * Register with LSM
4630 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004631 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
Casey Schauflere114e472008-02-04 22:29:50 -08004632
4633 return 0;
4634}
4635
4636/*
4637 * Smack requires early initialization in order to label
4638 * all processes and objects when they are created.
4639 */
4640security_initcall(smack_init);