blob: b644757886bc5314a7f861624868a8bdc30fdf9b [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
19 */
20
21#include <linux/xattr.h>
22#include <linux/pagemap.h>
23#include <linux/mount.h>
24#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070030#include <linux/dccp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080032#include <linux/mutex.h>
33#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080034#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070035#include <net/ip.h>
36#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100037#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070038#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050039#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030040#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000041#include <linux/msg.h>
42#include <linux/shm.h>
43#include <linux/binfmts.h>
Casey Schauflere114e472008-02-04 22:29:50 -080044#include "smack.h"
45
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020046#define TRANS_TRUE "TRUE"
47#define TRANS_TRUE_SIZE 4
48
Casey Schauflerc6739442013-05-22 18:42:56 -070049#define SMK_CONNECTING 0
50#define SMK_RECEIVING 1
51#define SMK_SENDING 2
52
Casey Schaufler69f287a2014-12-12 17:08:40 -080053#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -070054LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler69f287a2014-12-12 17:08:40 -080055#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Rohit1a5b4722014-10-15 17:40:41 +053056static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080057int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070058
Casey Schauflerd166c802014-08-27 14:51:27 -070059#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070060static char *smk_bu_mess[] = {
61 "Bringup Error", /* Unused */
62 "Bringup", /* SMACK_BRINGUP_ALLOW */
63 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
64 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
65};
66
Casey Schauflerd166c802014-08-27 14:51:27 -070067static void smk_bu_mode(int mode, char *s)
68{
69 int i = 0;
70
71 if (mode & MAY_READ)
72 s[i++] = 'r';
73 if (mode & MAY_WRITE)
74 s[i++] = 'w';
75 if (mode & MAY_EXEC)
76 s[i++] = 'x';
77 if (mode & MAY_APPEND)
78 s[i++] = 'a';
79 if (mode & MAY_TRANSMUTE)
80 s[i++] = 't';
81 if (mode & MAY_LOCK)
82 s[i++] = 'l';
83 if (i == 0)
84 s[i++] = '-';
85 s[i] = '\0';
86}
87#endif
88
89#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020090static int smk_bu_note(char *note, struct smack_known *sskp,
91 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -070092{
93 char acc[SMK_NUM_ACCESS_TYPE + 1];
94
95 if (rc <= 0)
96 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070097 if (rc > SMACK_UNCONFINED_OBJECT)
98 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -070099
100 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700101 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200102 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700103 return 0;
104}
105#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200106#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700107#endif
108
109#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200110static int smk_bu_current(char *note, struct smack_known *oskp,
111 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700112{
113 struct task_smack *tsp = current_security();
114 char acc[SMK_NUM_ACCESS_TYPE + 1];
115
116 if (rc <= 0)
117 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700118 if (rc > SMACK_UNCONFINED_OBJECT)
119 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700120
121 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700122 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200123 tsp->smk_task->smk_known, oskp->smk_known,
124 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700125 return 0;
126}
127#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200128#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700129#endif
130
131#ifdef CONFIG_SECURITY_SMACK_BRINGUP
132static int smk_bu_task(struct task_struct *otp, int mode, int rc)
133{
134 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300135 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700136 char acc[SMK_NUM_ACCESS_TYPE + 1];
137
138 if (rc <= 0)
139 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700140 if (rc > SMACK_UNCONFINED_OBJECT)
141 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700142
143 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700144 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300145 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700146 current->comm, otp->comm);
147 return 0;
148}
149#else
150#define smk_bu_task(otp, mode, RC) (RC)
151#endif
152
153#ifdef CONFIG_SECURITY_SMACK_BRINGUP
154static int smk_bu_inode(struct inode *inode, int mode, int rc)
155{
156 struct task_smack *tsp = current_security();
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700157 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700158 char acc[SMK_NUM_ACCESS_TYPE + 1];
159
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700160 if (isp->smk_flags & SMK_INODE_IMPURE)
161 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
162 inode->i_sb->s_id, inode->i_ino, current->comm);
163
Casey Schauflerd166c802014-08-27 14:51:27 -0700164 if (rc <= 0)
165 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700166 if (rc > SMACK_UNCONFINED_OBJECT)
167 rc = 0;
168 if (rc == SMACK_UNCONFINED_SUBJECT &&
169 (mode & (MAY_WRITE | MAY_APPEND)))
170 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700171
172 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700173
174 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
175 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700176 inode->i_sb->s_id, inode->i_ino, current->comm);
177 return 0;
178}
179#else
180#define smk_bu_inode(inode, mode, RC) (RC)
181#endif
182
183#ifdef CONFIG_SECURITY_SMACK_BRINGUP
184static int smk_bu_file(struct file *file, int mode, int rc)
185{
186 struct task_smack *tsp = current_security();
187 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800188 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700189 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700190 char acc[SMK_NUM_ACCESS_TYPE + 1];
191
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700192 if (isp->smk_flags & SMK_INODE_IMPURE)
193 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
194 inode->i_sb->s_id, inode->i_ino, current->comm);
195
Casey Schauflerd166c802014-08-27 14:51:27 -0700196 if (rc <= 0)
197 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700198 if (rc > SMACK_UNCONFINED_OBJECT)
199 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700200
201 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700202 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800203 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400204 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700205 current->comm);
206 return 0;
207}
208#else
209#define smk_bu_file(file, mode, RC) (RC)
210#endif
211
212#ifdef CONFIG_SECURITY_SMACK_BRINGUP
213static int smk_bu_credfile(const struct cred *cred, struct file *file,
214 int mode, int rc)
215{
216 struct task_smack *tsp = cred->security;
217 struct smack_known *sskp = tsp->smk_task;
218 struct inode *inode = file->f_inode;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700219 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700220 char acc[SMK_NUM_ACCESS_TYPE + 1];
221
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700222 if (isp->smk_flags & SMK_INODE_IMPURE)
223 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
224 inode->i_sb->s_id, inode->i_ino, current->comm);
225
Casey Schauflerd166c802014-08-27 14:51:27 -0700226 if (rc <= 0)
227 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700228 if (rc > SMACK_UNCONFINED_OBJECT)
229 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700230
231 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700232 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200233 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400234 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700235 current->comm);
236 return 0;
237}
238#else
239#define smk_bu_credfile(cred, file, mode, RC) (RC)
240#endif
241
Casey Schauflere114e472008-02-04 22:29:50 -0800242/**
243 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100244 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800245 * @ip: a pointer to the inode
246 * @dp: a pointer to the dentry
247 *
248 * Returns a pointer to the master list entry for the Smack label
249 * or NULL if there was no label to fetch.
250 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700251static struct smack_known *smk_fetch(const char *name, struct inode *ip,
252 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800253{
254 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700255 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700256 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800257
258 if (ip->i_op->getxattr == NULL)
259 return NULL;
260
Casey Schauflerf7112e62012-05-06 15:22:02 -0700261 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
262 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800263 return NULL;
264
Casey Schauflerf7112e62012-05-06 15:22:02 -0700265 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
266 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700267 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700268
269 kfree(buffer);
270
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700271 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800272}
273
274/**
275 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200276 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800277 *
278 * Returns the new blob or NULL if there's no memory available
279 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200280struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800281{
282 struct inode_smack *isp;
283
Rohit1a5b4722014-10-15 17:40:41 +0530284 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800285 if (isp == NULL)
286 return NULL;
287
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200288 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800289 isp->smk_flags = 0;
290 mutex_init(&isp->smk_lock);
291
292 return isp;
293}
294
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800295/**
296 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100297 * @task: a pointer to the Smack label for the running task
298 * @forked: a pointer to the Smack label for the forked task
299 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800300 *
301 * Returns the new blob or NULL if there's no memory available
302 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700303static struct task_smack *new_task_smack(struct smack_known *task,
304 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800305{
306 struct task_smack *tsp;
307
308 tsp = kzalloc(sizeof(struct task_smack), gfp);
309 if (tsp == NULL)
310 return NULL;
311
312 tsp->smk_task = task;
313 tsp->smk_forked = forked;
314 INIT_LIST_HEAD(&tsp->smk_rules);
315 mutex_init(&tsp->smk_rules_lock);
316
317 return tsp;
318}
319
320/**
321 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100322 * @nhead: new rules header pointer
323 * @ohead: old rules header pointer
324 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800325 *
326 * Returns 0 on success, -ENOMEM on error
327 */
328static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
329 gfp_t gfp)
330{
331 struct smack_rule *nrp;
332 struct smack_rule *orp;
333 int rc = 0;
334
335 INIT_LIST_HEAD(nhead);
336
337 list_for_each_entry_rcu(orp, ohead, list) {
338 nrp = kzalloc(sizeof(struct smack_rule), gfp);
339 if (nrp == NULL) {
340 rc = -ENOMEM;
341 break;
342 }
343 *nrp = *orp;
344 list_add_rcu(&nrp->list, nhead);
345 }
346 return rc;
347}
348
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100349/**
350 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
351 * @mode - input mode in form of PTRACE_MODE_*
352 *
353 * Returns a converted MAY_* mode usable by smack rules
354 */
355static inline unsigned int smk_ptrace_mode(unsigned int mode)
356{
357 switch (mode) {
358 case PTRACE_MODE_READ:
359 return MAY_READ;
360 case PTRACE_MODE_ATTACH:
361 return MAY_READWRITE;
362 }
363
364 return 0;
365}
366
367/**
368 * smk_ptrace_rule_check - helper for ptrace access
369 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200370 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100371 * @mode: ptrace attachment mode (PTRACE_MODE_*)
372 * @func: name of the function that called us, used for audit
373 *
374 * Returns 0 on access granted, -error on error
375 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200376static int smk_ptrace_rule_check(struct task_struct *tracer,
377 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100378 unsigned int mode, const char *func)
379{
380 int rc;
381 struct smk_audit_info ad, *saip = NULL;
382 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200383 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100384
385 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
386 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
387 smk_ad_setfield_u_tsk(&ad, tracer);
388 saip = &ad;
389 }
390
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300391 rcu_read_lock();
392 tsp = __task_cred(tracer)->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200393 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100394
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100395 if ((mode & PTRACE_MODE_ATTACH) &&
396 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
397 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200398 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100399 rc = 0;
400 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
401 rc = -EACCES;
402 else if (capable(CAP_SYS_PTRACE))
403 rc = 0;
404 else
405 rc = -EACCES;
406
407 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200408 smack_log(tracer_known->smk_known,
409 tracee_known->smk_known,
410 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100411
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300412 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100413 return rc;
414 }
415
416 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200417 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300418
419 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100420 return rc;
421}
422
Casey Schauflere114e472008-02-04 22:29:50 -0800423/*
424 * LSM hooks.
425 * We he, that is fun!
426 */
427
428/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000429 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800430 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100431 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800432 *
433 * Returns 0 if access is OK, an error code otherwise
434 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100435 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800436 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000437static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800438{
439 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700440 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800441
Ingo Molnar9e488582009-05-07 19:26:19 +1000442 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800443 if (rc != 0)
444 return rc;
445
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300446 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200447
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200448 rc = smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100449 return rc;
450}
Casey Schauflere114e472008-02-04 22:29:50 -0800451
David Howells5cd9c582008-08-14 11:37:28 +0100452/**
453 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
454 * @ptp: parent task pointer
455 *
456 * Returns 0 if access is OK, an error code otherwise
457 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100458 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100459 */
460static int smack_ptrace_traceme(struct task_struct *ptp)
461{
462 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700463 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100464
465 rc = cap_ptrace_traceme(ptp);
466 if (rc != 0)
467 return rc;
468
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100469 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200470
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200471 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800472 return rc;
473}
474
475/**
476 * smack_syslog - Smack approval on syslog
477 * @type: message type
478 *
Casey Schauflere114e472008-02-04 22:29:50 -0800479 * Returns 0 on success, error code otherwise.
480 */
Eric Paris12b30522010-11-15 18:36:29 -0500481static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800482{
Eric Paris12b30522010-11-15 18:36:29 -0500483 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700484 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800485
Casey Schaufler1880eff2012-06-05 15:28:30 -0700486 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800487 return 0;
488
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800489 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800490 rc = -EACCES;
491
492 return rc;
493}
494
495
496/*
497 * Superblock Hooks.
498 */
499
500/**
501 * smack_sb_alloc_security - allocate a superblock blob
502 * @sb: the superblock getting the blob
503 *
504 * Returns 0 on success or -ENOMEM on error.
505 */
506static int smack_sb_alloc_security(struct super_block *sb)
507{
508 struct superblock_smack *sbsp;
509
510 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
511
512 if (sbsp == NULL)
513 return -ENOMEM;
514
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200515 sbsp->smk_root = &smack_known_floor;
516 sbsp->smk_default = &smack_known_floor;
517 sbsp->smk_floor = &smack_known_floor;
518 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700519 /*
520 * smk_initialized will be zero from kzalloc.
521 */
Casey Schauflere114e472008-02-04 22:29:50 -0800522 sb->s_security = sbsp;
523
524 return 0;
525}
526
527/**
528 * smack_sb_free_security - free a superblock blob
529 * @sb: the superblock getting the blob
530 *
531 */
532static void smack_sb_free_security(struct super_block *sb)
533{
534 kfree(sb->s_security);
535 sb->s_security = NULL;
536}
537
538/**
539 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800540 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800541 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800542 *
543 * Returns 0 on success or -ENOMEM on error.
544 *
545 * Copy the Smack specific mount options out of the mount
546 * options list.
547 */
Eric Parise0007522008-03-05 10:31:54 -0500548static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800549{
550 char *cp, *commap, *otheropts, *dp;
551
Casey Schauflere114e472008-02-04 22:29:50 -0800552 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
553 if (otheropts == NULL)
554 return -ENOMEM;
555
556 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
557 if (strstr(cp, SMK_FSDEFAULT) == cp)
558 dp = smackopts;
559 else if (strstr(cp, SMK_FSFLOOR) == cp)
560 dp = smackopts;
561 else if (strstr(cp, SMK_FSHAT) == cp)
562 dp = smackopts;
563 else if (strstr(cp, SMK_FSROOT) == cp)
564 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700565 else if (strstr(cp, SMK_FSTRANS) == cp)
566 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800567 else
568 dp = otheropts;
569
570 commap = strchr(cp, ',');
571 if (commap != NULL)
572 *commap = '\0';
573
574 if (*dp != '\0')
575 strcat(dp, ",");
576 strcat(dp, cp);
577 }
578
579 strcpy(orig, otheropts);
580 free_page((unsigned long)otheropts);
581
582 return 0;
583}
584
585/**
586 * smack_sb_kern_mount - Smack specific mount processing
587 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100588 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800589 * @data: the smack mount options
590 *
591 * Returns 0 on success, an error code on failure
592 */
James Morris12204e22008-12-19 10:44:42 +1100593static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800594{
595 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000596 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800597 struct superblock_smack *sp = sb->s_security;
598 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800599 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800600 char *op;
601 char *commap;
Casey Schauflere830b392013-05-22 18:43:07 -0700602 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800603 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800604
Casey Schauflere830b392013-05-22 18:43:07 -0700605 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800606 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700607
Casey Schauflere114e472008-02-04 22:29:50 -0800608 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800609
610 for (op = data; op != NULL; op = commap) {
611 commap = strchr(op, ',');
612 if (commap != NULL)
613 *commap++ = '\0';
614
615 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
616 op += strlen(SMK_FSHAT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200617 skp = smk_import_entry(op, 0);
618 if (skp != NULL) {
619 sp->smk_hat = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800620 specified = 1;
621 }
Casey Schauflere114e472008-02-04 22:29:50 -0800622 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
623 op += strlen(SMK_FSFLOOR);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200624 skp = smk_import_entry(op, 0);
625 if (skp != NULL) {
626 sp->smk_floor = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800627 specified = 1;
628 }
Casey Schauflere114e472008-02-04 22:29:50 -0800629 } else if (strncmp(op, SMK_FSDEFAULT,
630 strlen(SMK_FSDEFAULT)) == 0) {
631 op += strlen(SMK_FSDEFAULT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200632 skp = smk_import_entry(op, 0);
633 if (skp != NULL) {
634 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800635 specified = 1;
636 }
Casey Schauflere114e472008-02-04 22:29:50 -0800637 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
638 op += strlen(SMK_FSROOT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200639 skp = smk_import_entry(op, 0);
640 if (skp != NULL) {
641 sp->smk_root = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800642 specified = 1;
643 }
Casey Schauflere830b392013-05-22 18:43:07 -0700644 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
645 op += strlen(SMK_FSTRANS);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200646 skp = smk_import_entry(op, 0);
647 if (skp != NULL) {
648 sp->smk_root = skp;
Casey Schauflere830b392013-05-22 18:43:07 -0700649 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800650 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700651 }
Casey Schauflere114e472008-02-04 22:29:50 -0800652 }
653 }
654
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800655 if (!smack_privileged(CAP_MAC_ADMIN)) {
656 /*
657 * Unprivileged mounts don't get to specify Smack values.
658 */
659 if (specified)
660 return -EPERM;
661 /*
662 * Unprivileged mounts get root and default from the caller.
663 */
664 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200665 sp->smk_root = skp;
666 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800667 }
Casey Schauflere114e472008-02-04 22:29:50 -0800668 /*
669 * Initialize the root inode.
670 */
671 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100672 if (isp == NULL) {
673 isp = new_inode_smack(sp->smk_root);
674 if (isp == NULL)
675 return -ENOMEM;
676 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700677 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800678 isp->smk_inode = sp->smk_root;
679
Casey Schauflere830b392013-05-22 18:43:07 -0700680 if (transmute)
681 isp->smk_flags |= SMK_INODE_TRANSMUTE;
682
Casey Schauflere114e472008-02-04 22:29:50 -0800683 return 0;
684}
685
686/**
687 * smack_sb_statfs - Smack check on statfs
688 * @dentry: identifies the file system in question
689 *
690 * Returns 0 if current can read the floor of the filesystem,
691 * and error code otherwise
692 */
693static int smack_sb_statfs(struct dentry *dentry)
694{
695 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200696 int rc;
697 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800698
Eric Parisa2694342011-04-25 13:10:27 -0400699 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200700 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
701
702 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700703 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200704 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800705}
706
Casey Schauflere114e472008-02-04 22:29:50 -0800707/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800708 * BPRM hooks
709 */
710
Casey Schauflerce8a4322011-09-29 18:21:01 -0700711/**
712 * smack_bprm_set_creds - set creds for exec
713 * @bprm: the exec information
714 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100715 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700716 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800717static int smack_bprm_set_creds(struct linux_binprm *bprm)
718{
Al Viro496ad9a2013-01-23 17:07:38 -0500719 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300720 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800721 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800722 int rc;
723
724 rc = cap_bprm_set_creds(bprm);
725 if (rc != 0)
726 return rc;
727
728 if (bprm->cred_prepared)
729 return 0;
730
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300731 isp = inode->i_security;
732 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800733 return 0;
734
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100735 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
736 struct task_struct *tracer;
737 rc = 0;
738
739 rcu_read_lock();
740 tracer = ptrace_parent(current);
741 if (likely(tracer != NULL))
742 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200743 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100744 PTRACE_MODE_ATTACH,
745 __func__);
746 rcu_read_unlock();
747
748 if (rc != 0)
749 return rc;
750 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300751 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800752
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300753 bsp->smk_task = isp->smk_task;
754 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800755
756 return 0;
757}
758
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300759/**
760 * smack_bprm_committing_creds - Prepare to install the new credentials
761 * from bprm.
762 *
763 * @bprm: binprm for exec
764 */
765static void smack_bprm_committing_creds(struct linux_binprm *bprm)
766{
767 struct task_smack *bsp = bprm->cred->security;
768
769 if (bsp->smk_task != bsp->smk_forked)
770 current->pdeath_signal = 0;
771}
772
773/**
774 * smack_bprm_secureexec - Return the decision to use secureexec.
775 * @bprm: binprm for exec
776 *
777 * Returns 0 on success.
778 */
779static int smack_bprm_secureexec(struct linux_binprm *bprm)
780{
781 struct task_smack *tsp = current_security();
782 int ret = cap_bprm_secureexec(bprm);
783
784 if (!ret && (tsp->smk_task != tsp->smk_forked))
785 ret = 1;
786
787 return ret;
788}
789
Casey Schaufler676dac42010-12-02 06:43:39 -0800790/*
Casey Schauflere114e472008-02-04 22:29:50 -0800791 * Inode hooks
792 */
793
794/**
795 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800796 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800797 *
798 * Returns 0 if it gets a blob, -ENOMEM otherwise
799 */
800static int smack_inode_alloc_security(struct inode *inode)
801{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700802 struct smack_known *skp = smk_of_current();
803
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200804 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800805 if (inode->i_security == NULL)
806 return -ENOMEM;
807 return 0;
808}
809
810/**
811 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800812 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800813 *
814 * Clears the blob pointer in inode
815 */
816static void smack_inode_free_security(struct inode *inode)
817{
Rohit1a5b4722014-10-15 17:40:41 +0530818 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800819 inode->i_security = NULL;
820}
821
822/**
823 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200824 * @inode: the newly created inode
825 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500826 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800827 * @name: where to put the attribute name
828 * @value: where to put the attribute value
829 * @len: where to put the length of the attribute
830 *
831 * Returns 0 if it all works out, -ENOMEM if there's no memory
832 */
833static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900834 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500835 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800836{
Casey Schaufler2267b132012-03-13 19:14:19 -0700837 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700838 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200839 struct smack_known *isp = smk_of_inode(inode);
840 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800841 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800842
Tetsuo Handa95489062013-07-25 05:44:02 +0900843 if (name)
844 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800845
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100846 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800847 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200848 may = smk_access_entry(skp->smk_known, dsp->smk_known,
849 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800850 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200851
852 /*
853 * If the access rule allows transmutation and
854 * the directory requests transmutation then
855 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700856 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200857 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800858 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700859 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200860 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700861 issp->smk_flags |= SMK_INODE_CHANGED;
862 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200863
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200864 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800865 if (*value == NULL)
866 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800867
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200868 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100869 }
Casey Schauflere114e472008-02-04 22:29:50 -0800870
871 return 0;
872}
873
874/**
875 * smack_inode_link - Smack check on link
876 * @old_dentry: the existing object
877 * @dir: unused
878 * @new_dentry: the new object
879 *
880 * Returns 0 if access is permitted, an error code otherwise
881 */
882static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
883 struct dentry *new_dentry)
884{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200885 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200886 struct smk_audit_info ad;
887 int rc;
888
Eric Parisa2694342011-04-25 13:10:27 -0400889 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200890 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800891
David Howellsc6f493d2015-03-17 22:26:22 +0000892 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200893 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000894 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800895
David Howells88025652015-01-29 12:02:32 +0000896 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +0000897 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200898 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
899 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000900 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800901 }
902
903 return rc;
904}
905
906/**
907 * smack_inode_unlink - Smack check on inode deletion
908 * @dir: containing directory object
909 * @dentry: file to unlink
910 *
911 * Returns 0 if current can write the containing directory
912 * and the object, error code otherwise
913 */
914static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
915{
David Howellsc6f493d2015-03-17 22:26:22 +0000916 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200917 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800918 int rc;
919
Eric Parisa2694342011-04-25 13:10:27 -0400920 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200921 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
922
Casey Schauflere114e472008-02-04 22:29:50 -0800923 /*
924 * You need write access to the thing you're unlinking
925 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200926 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700927 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200928 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800929 /*
930 * You also need write access to the containing directory
931 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400932 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200933 smk_ad_setfield_u_fs_inode(&ad, dir);
934 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700935 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200936 }
Casey Schauflere114e472008-02-04 22:29:50 -0800937 return rc;
938}
939
940/**
941 * smack_inode_rmdir - Smack check on directory deletion
942 * @dir: containing directory object
943 * @dentry: directory to unlink
944 *
945 * Returns 0 if current can write the containing directory
946 * and the directory, error code otherwise
947 */
948static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
949{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200950 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800951 int rc;
952
Eric Parisa2694342011-04-25 13:10:27 -0400953 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200954 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
955
Casey Schauflere114e472008-02-04 22:29:50 -0800956 /*
957 * You need write access to the thing you're removing
958 */
David Howellsc6f493d2015-03-17 22:26:22 +0000959 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
960 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200961 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800962 /*
963 * You also need write access to the containing directory
964 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400965 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200966 smk_ad_setfield_u_fs_inode(&ad, dir);
967 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700968 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200969 }
Casey Schauflere114e472008-02-04 22:29:50 -0800970
971 return rc;
972}
973
974/**
975 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200976 * @old_inode: unused
977 * @old_dentry: the old object
978 * @new_inode: unused
979 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -0800980 *
981 * Read and write access is required on both the old and
982 * new directories.
983 *
984 * Returns 0 if access is permitted, an error code otherwise
985 */
986static int smack_inode_rename(struct inode *old_inode,
987 struct dentry *old_dentry,
988 struct inode *new_inode,
989 struct dentry *new_dentry)
990{
991 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200992 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200993 struct smk_audit_info ad;
994
Eric Parisa2694342011-04-25 13:10:27 -0400995 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200996 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800997
David Howellsc6f493d2015-03-17 22:26:22 +0000998 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200999 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001000 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001001
David Howells88025652015-01-29 12:02:32 +00001002 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001003 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001004 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1005 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001006 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001007 }
Casey Schauflere114e472008-02-04 22:29:50 -08001008 return rc;
1009}
1010
1011/**
1012 * smack_inode_permission - Smack version of permission()
1013 * @inode: the inode in question
1014 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001015 *
1016 * This is the important Smack hook.
1017 *
1018 * Returns 0 if access is permitted, -EACCES otherwise
1019 */
Al Viroe74f71e2011-06-20 19:38:15 -04001020static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001021{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001022 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001023 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001024 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001025
1026 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001027 /*
1028 * No permission to check. Existence test. Yup, it's there.
1029 */
1030 if (mask == 0)
1031 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001032
1033 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001034 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001035 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001037 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001038 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1039 rc = smk_bu_inode(inode, mask, rc);
1040 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001041}
1042
1043/**
1044 * smack_inode_setattr - Smack check for setting attributes
1045 * @dentry: the object
1046 * @iattr: for the force flag
1047 *
1048 * Returns 0 if access is permitted, an error code otherwise
1049 */
1050static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1051{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001052 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001053 int rc;
1054
Casey Schauflere114e472008-02-04 22:29:50 -08001055 /*
1056 * Need to allow for clearing the setuid bit.
1057 */
1058 if (iattr->ia_valid & ATTR_FORCE)
1059 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001060 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001061 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001062
David Howellsc6f493d2015-03-17 22:26:22 +00001063 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1064 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001065 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001066}
1067
1068/**
1069 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001070 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001071 * @dentry: the object
1072 *
1073 * Returns 0 if access is permitted, an error code otherwise
1074 */
Al Viro3f7036a2015-03-08 19:28:30 -04001075static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001076{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001077 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001078 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001079 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001080
Eric Parisf48b7392011-04-25 12:54:27 -04001081 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001082 smk_ad_setfield_u_fs_path(&ad, *path);
1083 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1084 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001085 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001086}
1087
1088/**
1089 * smack_inode_setxattr - Smack check for setting xattrs
1090 * @dentry: the object
1091 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001092 * @value: value of the attribute
1093 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001094 * @flags: unused
1095 *
1096 * This protects the Smack attribute explicitly.
1097 *
1098 * Returns 0 if access is permitted, an error code otherwise
1099 */
David Howells8f0cfa52008-04-29 00:59:41 -07001100static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1101 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001102{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001104 struct smack_known *skp;
1105 int check_priv = 0;
1106 int check_import = 0;
1107 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001108 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001109
Casey Schaufler19760ad2013-12-16 16:27:26 -08001110 /*
1111 * Check label validity here so import won't fail in post_setxattr
1112 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001113 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1114 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001115 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1116 check_priv = 1;
1117 check_import = 1;
1118 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1119 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1120 check_priv = 1;
1121 check_import = 1;
1122 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001123 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001124 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001125 if (size != TRANS_TRUE_SIZE ||
1126 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1127 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001128 } else
1129 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1130
Casey Schaufler19760ad2013-12-16 16:27:26 -08001131 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1132 rc = -EPERM;
1133
1134 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001135 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001136 if (skp == NULL || (check_star &&
1137 (skp == &smack_known_star || skp == &smack_known_web)))
1138 rc = -EINVAL;
1139 }
1140
Eric Parisa2694342011-04-25 13:10:27 -04001141 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001142 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1143
Casey Schauflerd166c802014-08-27 14:51:27 -07001144 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001145 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1146 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001147 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001148
1149 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001150}
1151
1152/**
1153 * smack_inode_post_setxattr - Apply the Smack update approved above
1154 * @dentry: object
1155 * @name: attribute name
1156 * @value: attribute value
1157 * @size: attribute size
1158 * @flags: unused
1159 *
1160 * Set the pointer in the inode blob to the entry found
1161 * in the master label list.
1162 */
David Howells8f0cfa52008-04-29 00:59:41 -07001163static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1164 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001165{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001166 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001167 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001168
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001169 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1170 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1171 return;
1172 }
1173
Casey Schaufler676dac42010-12-02 06:43:39 -08001174 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001175 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001176 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001177 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001178 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001179 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001180 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001181 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001182 if (skp != NULL)
1183 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001184 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001185 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001186 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001187 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001188 if (skp != NULL)
1189 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001190 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001191 isp->smk_mmap = &smack_known_invalid;
1192 }
Casey Schauflere114e472008-02-04 22:29:50 -08001193
1194 return;
1195}
1196
Casey Schauflerce8a4322011-09-29 18:21:01 -07001197/**
Casey Schauflere114e472008-02-04 22:29:50 -08001198 * smack_inode_getxattr - Smack check on getxattr
1199 * @dentry: the object
1200 * @name: unused
1201 *
1202 * Returns 0 if access is permitted, an error code otherwise
1203 */
David Howells8f0cfa52008-04-29 00:59:41 -07001204static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001205{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001206 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001207 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001208
Eric Parisa2694342011-04-25 13:10:27 -04001209 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001210 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1211
David Howellsc6f493d2015-03-17 22:26:22 +00001212 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1213 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001214 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001215}
1216
Casey Schauflerce8a4322011-09-29 18:21:01 -07001217/**
Casey Schauflere114e472008-02-04 22:29:50 -08001218 * smack_inode_removexattr - Smack check on removexattr
1219 * @dentry: the object
1220 * @name: name of the attribute
1221 *
1222 * Removing the Smack attribute requires CAP_MAC_ADMIN
1223 *
1224 * Returns 0 if access is permitted, an error code otherwise
1225 */
David Howells8f0cfa52008-04-29 00:59:41 -07001226static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001227{
Casey Schaufler676dac42010-12-02 06:43:39 -08001228 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001229 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001230 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001231
Casey Schauflerbcdca222008-02-23 15:24:04 -08001232 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1233 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001234 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001235 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001236 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301237 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001238 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001239 rc = -EPERM;
1240 } else
1241 rc = cap_inode_removexattr(dentry, name);
1242
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001243 if (rc != 0)
1244 return rc;
1245
Eric Parisa2694342011-04-25 13:10:27 -04001246 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001247 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001248
David Howellsc6f493d2015-03-17 22:26:22 +00001249 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1250 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001251 if (rc != 0)
1252 return rc;
1253
David Howellsc6f493d2015-03-17 22:26:22 +00001254 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001255 /*
1256 * Don't do anything special for these.
1257 * XATTR_NAME_SMACKIPIN
1258 * XATTR_NAME_SMACKIPOUT
1259 * XATTR_NAME_SMACKEXEC
1260 */
1261 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001262 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001263 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001264 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001265 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1266 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001267
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001268 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001269}
1270
1271/**
1272 * smack_inode_getsecurity - get smack xattrs
1273 * @inode: the object
1274 * @name: attribute name
1275 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001276 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001277 *
1278 * Returns the size of the attribute or an error code
1279 */
1280static int smack_inode_getsecurity(const struct inode *inode,
1281 const char *name, void **buffer,
1282 bool alloc)
1283{
1284 struct socket_smack *ssp;
1285 struct socket *sock;
1286 struct super_block *sbp;
1287 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001288 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001289 int ilen;
1290 int rc = 0;
1291
1292 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1293 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001294 ilen = strlen(isp->smk_known);
1295 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001296 return ilen;
1297 }
1298
1299 /*
1300 * The rest of the Smack xattrs are only on sockets.
1301 */
1302 sbp = ip->i_sb;
1303 if (sbp->s_magic != SOCKFS_MAGIC)
1304 return -EOPNOTSUPP;
1305
1306 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001307 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001308 return -EOPNOTSUPP;
1309
1310 ssp = sock->sk->sk_security;
1311
1312 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001313 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001314 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001315 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001316 else
1317 return -EOPNOTSUPP;
1318
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001319 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001320 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001321 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001322 rc = ilen;
1323 }
1324
1325 return rc;
1326}
1327
1328
1329/**
1330 * smack_inode_listsecurity - list the Smack attributes
1331 * @inode: the object
1332 * @buffer: where they go
1333 * @buffer_size: size of buffer
1334 *
1335 * Returns 0 on success, -EINVAL otherwise
1336 */
1337static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1338 size_t buffer_size)
1339{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001340 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001341
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001342 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001343 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001344
1345 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001346}
1347
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001348/**
1349 * smack_inode_getsecid - Extract inode's security id
1350 * @inode: inode to extract the info from
1351 * @secid: where result will be saved
1352 */
1353static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1354{
1355 struct inode_smack *isp = inode->i_security;
1356
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001357 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001358}
1359
Casey Schauflere114e472008-02-04 22:29:50 -08001360/*
1361 * File Hooks
1362 */
1363
1364/**
1365 * smack_file_permission - Smack check on file operations
1366 * @file: unused
1367 * @mask: unused
1368 *
1369 * Returns 0
1370 *
1371 * Should access checks be done on each read or write?
1372 * UNICOS and SELinux say yes.
1373 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1374 *
1375 * I'll say no for now. Smack does not do the frequent
1376 * label changing that SELinux does.
1377 */
1378static int smack_file_permission(struct file *file, int mask)
1379{
1380 return 0;
1381}
1382
1383/**
1384 * smack_file_alloc_security - assign a file security blob
1385 * @file: the object
1386 *
1387 * The security blob for a file is a pointer to the master
1388 * label list, so no allocation is done.
1389 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001390 * f_security is the owner security information. It
1391 * isn't used on file access checks, it's for send_sigio.
1392 *
Casey Schauflere114e472008-02-04 22:29:50 -08001393 * Returns 0
1394 */
1395static int smack_file_alloc_security(struct file *file)
1396{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001397 struct smack_known *skp = smk_of_current();
1398
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001399 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001400 return 0;
1401}
1402
1403/**
1404 * smack_file_free_security - clear a file security blob
1405 * @file: the object
1406 *
1407 * The security blob for a file is a pointer to the master
1408 * label list, so no memory is freed.
1409 */
1410static void smack_file_free_security(struct file *file)
1411{
1412 file->f_security = NULL;
1413}
1414
1415/**
1416 * smack_file_ioctl - Smack check on ioctls
1417 * @file: the object
1418 * @cmd: what to do
1419 * @arg: unused
1420 *
1421 * Relies heavily on the correct use of the ioctl command conventions.
1422 *
1423 * Returns 0 if allowed, error code otherwise
1424 */
1425static int smack_file_ioctl(struct file *file, unsigned int cmd,
1426 unsigned long arg)
1427{
1428 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001429 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001430 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001431
Eric Parisf48b7392011-04-25 12:54:27 -04001432 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001433 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001434
Casey Schauflerd166c802014-08-27 14:51:27 -07001435 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001436 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001437 rc = smk_bu_file(file, MAY_WRITE, rc);
1438 }
Casey Schauflere114e472008-02-04 22:29:50 -08001439
Casey Schauflerd166c802014-08-27 14:51:27 -07001440 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001441 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001442 rc = smk_bu_file(file, MAY_READ, rc);
1443 }
Casey Schauflere114e472008-02-04 22:29:50 -08001444
1445 return rc;
1446}
1447
1448/**
1449 * smack_file_lock - Smack check on file locking
1450 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001451 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001452 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001453 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001454 */
1455static int smack_file_lock(struct file *file, unsigned int cmd)
1456{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001457 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001458 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001459 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001460
Eric Paris92f42502011-04-25 13:15:55 -04001461 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1462 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001463 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001464 rc = smk_bu_file(file, MAY_LOCK, rc);
1465 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001466}
1467
1468/**
1469 * smack_file_fcntl - Smack check on fcntl
1470 * @file: the object
1471 * @cmd: what action to check
1472 * @arg: unused
1473 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001474 * Generally these operations are harmless.
1475 * File locking operations present an obvious mechanism
1476 * for passing information, so they require write access.
1477 *
Casey Schauflere114e472008-02-04 22:29:50 -08001478 * Returns 0 if current has access, error code otherwise
1479 */
1480static int smack_file_fcntl(struct file *file, unsigned int cmd,
1481 unsigned long arg)
1482{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001483 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001484 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001485 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001486
Casey Schauflere114e472008-02-04 22:29:50 -08001487 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001488 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001489 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001490 case F_SETLK:
1491 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001492 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1493 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001494 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001495 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001496 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001497 case F_SETOWN:
1498 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001499 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1500 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001501 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001502 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001503 break;
1504 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001505 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001506 }
1507
1508 return rc;
1509}
1510
1511/**
Al Viroe5467852012-05-30 13:30:51 -04001512 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001513 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1514 * if mapping anonymous memory.
1515 * @file contains the file structure for file to map (may be NULL).
1516 * @reqprot contains the protection requested by the application.
1517 * @prot contains the protection that will be applied by the kernel.
1518 * @flags contains the operational flags.
1519 * Return 0 if permission is granted.
1520 */
Al Viroe5467852012-05-30 13:30:51 -04001521static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001522 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001523 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001524{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001525 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001526 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001527 struct smack_rule *srp;
1528 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001529 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001530 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001531 int may;
1532 int mmay;
1533 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001534 int rc;
1535
Al Viro496ad9a2013-01-23 17:07:38 -05001536 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001537 return 0;
1538
Al Viro496ad9a2013-01-23 17:07:38 -05001539 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001540 if (isp->smk_mmap == NULL)
1541 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001542 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001543
1544 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001545 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001546 rc = 0;
1547
1548 rcu_read_lock();
1549 /*
1550 * For each Smack rule associated with the subject
1551 * label verify that the SMACK64MMAP also has access
1552 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001553 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001554 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001555 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001556 /*
1557 * Matching labels always allows access.
1558 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001559 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001560 continue;
1561 /*
1562 * If there is a matching local rule take
1563 * that into account as well.
1564 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001565 may = smk_access_entry(srp->smk_subject->smk_known,
1566 okp->smk_known,
1567 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001568 if (may == -ENOENT)
1569 may = srp->smk_access;
1570 else
1571 may &= srp->smk_access;
1572 /*
1573 * If may is zero the SMACK64MMAP subject can't
1574 * possibly have less access.
1575 */
1576 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001577 continue;
1578
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001579 /*
1580 * Fetch the global list entry.
1581 * If there isn't one a SMACK64MMAP subject
1582 * can't have as much access as current.
1583 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001584 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1585 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001586 if (mmay == -ENOENT) {
1587 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001588 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001589 }
1590 /*
1591 * If there is a local entry it modifies the
1592 * potential access, too.
1593 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001594 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1595 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001596 if (tmay != -ENOENT)
1597 mmay &= tmay;
1598
1599 /*
1600 * If there is any access available to current that is
1601 * not available to a SMACK64MMAP subject
1602 * deny access.
1603 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001604 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001605 rc = -EACCES;
1606 break;
1607 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001608 }
1609
1610 rcu_read_unlock();
1611
1612 return rc;
1613}
1614
1615/**
Casey Schauflere114e472008-02-04 22:29:50 -08001616 * smack_file_set_fowner - set the file security blob value
1617 * @file: object in question
1618 *
Casey Schauflere114e472008-02-04 22:29:50 -08001619 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001620static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001621{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001622 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001623}
1624
1625/**
1626 * smack_file_send_sigiotask - Smack on sigio
1627 * @tsk: The target task
1628 * @fown: the object the signal come from
1629 * @signum: unused
1630 *
1631 * Allow a privileged task to get signals even if it shouldn't
1632 *
1633 * Returns 0 if a subject with the object's smack could
1634 * write to the task, an error code otherwise.
1635 */
1636static int smack_file_send_sigiotask(struct task_struct *tsk,
1637 struct fown_struct *fown, int signum)
1638{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001639 struct smack_known *skp;
1640 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001641 struct file *file;
1642 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001643 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001644
1645 /*
1646 * struct fown_struct is never outside the context of a struct file
1647 */
1648 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001649
Etienne Bassetecfcc532009-04-08 20:40:06 +02001650 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001651 skp = file->f_security;
1652 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1653 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001654 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001655 rc = 0;
1656
1657 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1658 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001659 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001660 return rc;
1661}
1662
1663/**
1664 * smack_file_receive - Smack file receive check
1665 * @file: the object
1666 *
1667 * Returns 0 if current has access, error code otherwise
1668 */
1669static int smack_file_receive(struct file *file)
1670{
Casey Schauflerd166c802014-08-27 14:51:27 -07001671 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001672 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001673 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001674 struct inode *inode = file_inode(file);
Casey Schauflere114e472008-02-04 22:29:50 -08001675
Casey Schaufler4482a442013-12-30 17:37:45 -08001676 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001677 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001678 /*
1679 * This code relies on bitmasks.
1680 */
1681 if (file->f_mode & FMODE_READ)
1682 may = MAY_READ;
1683 if (file->f_mode & FMODE_WRITE)
1684 may |= MAY_WRITE;
1685
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001686 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001687 rc = smk_bu_file(file, may, rc);
1688 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001689}
1690
Casey Schaufler531f1d42011-09-19 12:41:42 -07001691/**
Eric Paris83d49852012-04-04 13:45:40 -04001692 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001693 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001694 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001695 *
1696 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001697 * Allow the open only if the task has read access. There are
1698 * many read operations (e.g. fstat) that you can do with an
1699 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001700 *
1701 * Returns 0
1702 */
Eric Paris83d49852012-04-04 13:45:40 -04001703static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001704{
Casey Schauflera6834c02014-04-21 11:10:26 -07001705 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001706 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001707 struct smk_audit_info ad;
1708 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001709
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001710 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001711 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001712
Casey Schauflera6834c02014-04-21 11:10:26 -07001713 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1714 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001715 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001716 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001717
1718 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001719}
1720
Casey Schauflere114e472008-02-04 22:29:50 -08001721/*
1722 * Task hooks
1723 */
1724
1725/**
David Howellsee18d642009-09-02 09:14:21 +01001726 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1727 * @new: the new credentials
1728 * @gfp: the atomicity of any memory allocations
1729 *
1730 * Prepare a blank set of credentials for modification. This must allocate all
1731 * the memory the LSM module might require such that cred_transfer() can
1732 * complete without error.
1733 */
1734static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1735{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001736 struct task_smack *tsp;
1737
1738 tsp = new_task_smack(NULL, NULL, gfp);
1739 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001740 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001741
1742 cred->security = tsp;
1743
David Howellsee18d642009-09-02 09:14:21 +01001744 return 0;
1745}
1746
1747
1748/**
David Howellsf1752ee2008-11-14 10:39:17 +11001749 * smack_cred_free - "free" task-level security credentials
1750 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001751 *
Casey Schauflere114e472008-02-04 22:29:50 -08001752 */
David Howellsf1752ee2008-11-14 10:39:17 +11001753static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001754{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001755 struct task_smack *tsp = cred->security;
1756 struct smack_rule *rp;
1757 struct list_head *l;
1758 struct list_head *n;
1759
1760 if (tsp == NULL)
1761 return;
1762 cred->security = NULL;
1763
1764 list_for_each_safe(l, n, &tsp->smk_rules) {
1765 rp = list_entry(l, struct smack_rule, list);
1766 list_del(&rp->list);
1767 kfree(rp);
1768 }
1769 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001770}
1771
1772/**
David Howellsd84f4f92008-11-14 10:39:23 +11001773 * smack_cred_prepare - prepare new set of credentials for modification
1774 * @new: the new credentials
1775 * @old: the original credentials
1776 * @gfp: the atomicity of any memory allocations
1777 *
1778 * Prepare a new set of credentials for modification.
1779 */
1780static int smack_cred_prepare(struct cred *new, const struct cred *old,
1781 gfp_t gfp)
1782{
Casey Schaufler676dac42010-12-02 06:43:39 -08001783 struct task_smack *old_tsp = old->security;
1784 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001785 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001786
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001787 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001788 if (new_tsp == NULL)
1789 return -ENOMEM;
1790
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001791 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1792 if (rc != 0)
1793 return rc;
1794
Casey Schaufler676dac42010-12-02 06:43:39 -08001795 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001796 return 0;
1797}
1798
Randy Dunlap251a2a92009-02-18 11:42:33 -08001799/**
David Howellsee18d642009-09-02 09:14:21 +01001800 * smack_cred_transfer - Transfer the old credentials to the new credentials
1801 * @new: the new credentials
1802 * @old: the original credentials
1803 *
1804 * Fill in a set of blank credentials from another set of credentials.
1805 */
1806static void smack_cred_transfer(struct cred *new, const struct cred *old)
1807{
Casey Schaufler676dac42010-12-02 06:43:39 -08001808 struct task_smack *old_tsp = old->security;
1809 struct task_smack *new_tsp = new->security;
1810
1811 new_tsp->smk_task = old_tsp->smk_task;
1812 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001813 mutex_init(&new_tsp->smk_rules_lock);
1814 INIT_LIST_HEAD(&new_tsp->smk_rules);
1815
1816
1817 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001818}
1819
1820/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001821 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001822 * @new: points to the set of credentials to be modified.
1823 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001824 *
1825 * Set the security data for a kernel service.
1826 */
1827static int smack_kernel_act_as(struct cred *new, u32 secid)
1828{
Casey Schaufler676dac42010-12-02 06:43:39 -08001829 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001830 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001831
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001832 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001833 return -EINVAL;
1834
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001835 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001836 return 0;
1837}
1838
1839/**
1840 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001841 * @new: points to the set of credentials to be modified
1842 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001843 *
1844 * Set the file creation context in a set of credentials to the same
1845 * as the objective context of the specified inode
1846 */
1847static int smack_kernel_create_files_as(struct cred *new,
1848 struct inode *inode)
1849{
1850 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001851 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001852
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001853 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001854 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001855 return 0;
1856}
1857
1858/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001859 * smk_curacc_on_task - helper to log task related access
1860 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001861 * @access: the access requested
1862 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001863 *
1864 * Return 0 if access is permitted
1865 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001866static int smk_curacc_on_task(struct task_struct *p, int access,
1867 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001868{
1869 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001870 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001871 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001872
Casey Schaufler531f1d42011-09-19 12:41:42 -07001873 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001874 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001875 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001876 rc = smk_bu_task(p, access, rc);
1877 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001878}
1879
1880/**
Casey Schauflere114e472008-02-04 22:29:50 -08001881 * smack_task_setpgid - Smack check on setting pgid
1882 * @p: the task object
1883 * @pgid: unused
1884 *
1885 * Return 0 if write access is permitted
1886 */
1887static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1888{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001889 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001890}
1891
1892/**
1893 * smack_task_getpgid - Smack access check for getpgid
1894 * @p: the object task
1895 *
1896 * Returns 0 if current can read the object task, error code otherwise
1897 */
1898static int smack_task_getpgid(struct task_struct *p)
1899{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001900 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001901}
1902
1903/**
1904 * smack_task_getsid - Smack access check for getsid
1905 * @p: the object task
1906 *
1907 * Returns 0 if current can read the object task, error code otherwise
1908 */
1909static int smack_task_getsid(struct task_struct *p)
1910{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001911 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001912}
1913
1914/**
1915 * smack_task_getsecid - get the secid of the task
1916 * @p: the object task
1917 * @secid: where to put the result
1918 *
1919 * Sets the secid to contain a u32 version of the smack label.
1920 */
1921static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1922{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001923 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001924
1925 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001926}
1927
1928/**
1929 * smack_task_setnice - Smack check on setting nice
1930 * @p: the task object
1931 * @nice: unused
1932 *
1933 * Return 0 if write access is permitted
1934 */
1935static int smack_task_setnice(struct task_struct *p, int nice)
1936{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001937 int rc;
1938
1939 rc = cap_task_setnice(p, nice);
1940 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001941 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001942 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001943}
1944
1945/**
1946 * smack_task_setioprio - Smack check on setting ioprio
1947 * @p: the task object
1948 * @ioprio: unused
1949 *
1950 * Return 0 if write access is permitted
1951 */
1952static int smack_task_setioprio(struct task_struct *p, int ioprio)
1953{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001954 int rc;
1955
1956 rc = cap_task_setioprio(p, ioprio);
1957 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001958 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001959 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001960}
1961
1962/**
1963 * smack_task_getioprio - Smack check on reading ioprio
1964 * @p: the task object
1965 *
1966 * Return 0 if read access is permitted
1967 */
1968static int smack_task_getioprio(struct task_struct *p)
1969{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001970 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001971}
1972
1973/**
1974 * smack_task_setscheduler - Smack check on setting scheduler
1975 * @p: the task object
1976 * @policy: unused
1977 * @lp: unused
1978 *
1979 * Return 0 if read access is permitted
1980 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001981static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001982{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001983 int rc;
1984
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001985 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001986 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001987 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001988 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001989}
1990
1991/**
1992 * smack_task_getscheduler - Smack check on reading scheduler
1993 * @p: the task object
1994 *
1995 * Return 0 if read access is permitted
1996 */
1997static int smack_task_getscheduler(struct task_struct *p)
1998{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001999 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002000}
2001
2002/**
2003 * smack_task_movememory - Smack check on moving memory
2004 * @p: the task object
2005 *
2006 * Return 0 if write access is permitted
2007 */
2008static int smack_task_movememory(struct task_struct *p)
2009{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002010 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002011}
2012
2013/**
2014 * smack_task_kill - Smack check on signal delivery
2015 * @p: the task object
2016 * @info: unused
2017 * @sig: unused
2018 * @secid: identifies the smack to use in lieu of current's
2019 *
2020 * Return 0 if write access is permitted
2021 *
2022 * The secid behavior is an artifact of an SELinux hack
2023 * in the USB code. Someday it may go away.
2024 */
2025static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2026 int sig, u32 secid)
2027{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002028 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002029 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002030 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002031 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002032
2033 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2034 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002035 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002036 * Sending a signal requires that the sender
2037 * can write the receiver.
2038 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002039 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002040 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002041 rc = smk_bu_task(p, MAY_WRITE, rc);
2042 return rc;
2043 }
Casey Schauflere114e472008-02-04 22:29:50 -08002044 /*
2045 * If the secid isn't 0 we're dealing with some USB IO
2046 * specific behavior. This is not clean. For one thing
2047 * we can't take privilege into account.
2048 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002049 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002050 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2051 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002052 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002053}
2054
2055/**
2056 * smack_task_wait - Smack access check for waiting
2057 * @p: task to wait for
2058 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002059 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002060 */
2061static int smack_task_wait(struct task_struct *p)
2062{
Casey Schauflere114e472008-02-04 22:29:50 -08002063 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002064 * Allow the operation to succeed.
2065 * Zombies are bad.
2066 * In userless environments (e.g. phones) programs
2067 * get marked with SMACK64EXEC and even if the parent
2068 * and child shouldn't be talking the parent still
2069 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002070 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002071 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002072}
2073
2074/**
2075 * smack_task_to_inode - copy task smack into the inode blob
2076 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002077 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002078 *
2079 * Sets the smack pointer in the inode security blob
2080 */
2081static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2082{
2083 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002084 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002085
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002086 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002087}
2088
2089/*
2090 * Socket hooks.
2091 */
2092
2093/**
2094 * smack_sk_alloc_security - Allocate a socket blob
2095 * @sk: the socket
2096 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002097 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002098 *
2099 * Assign Smack pointers to current
2100 *
2101 * Returns 0 on success, -ENOMEM is there's no memory
2102 */
2103static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2104{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002105 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002106 struct socket_smack *ssp;
2107
2108 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2109 if (ssp == NULL)
2110 return -ENOMEM;
2111
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002112 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002113 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002114 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002115
2116 sk->sk_security = ssp;
2117
2118 return 0;
2119}
2120
2121/**
2122 * smack_sk_free_security - Free a socket blob
2123 * @sk: the socket
2124 *
2125 * Clears the blob pointer
2126 */
2127static void smack_sk_free_security(struct sock *sk)
2128{
2129 kfree(sk->sk_security);
2130}
2131
2132/**
Paul Moore07feee82009-03-27 17:10:54 -04002133* smack_host_label - check host based restrictions
2134* @sip: the object end
2135*
2136* looks for host based access restrictions
2137*
2138* This version will only be appropriate for really small sets of single label
2139* hosts. The caller is responsible for ensuring that the RCU read lock is
2140* taken before calling this function.
2141*
2142* Returns the label of the far end or NULL if it's not special.
2143*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002144static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002145{
2146 struct smk_netlbladdr *snp;
2147 struct in_addr *siap = &sip->sin_addr;
2148
2149 if (siap->s_addr == 0)
2150 return NULL;
2151
2152 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2153 /*
2154 * we break after finding the first match because
2155 * the list is sorted from longest to shortest mask
2156 * so we have found the most specific match
2157 */
2158 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002159 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2160 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002161 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002162 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002163 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002164 }
Paul Moore07feee82009-03-27 17:10:54 -04002165
2166 return NULL;
2167}
2168
2169/**
Casey Schauflere114e472008-02-04 22:29:50 -08002170 * smack_netlabel - Set the secattr on a socket
2171 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002172 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002173 *
2174 * Convert the outbound smack value (smk_out) to a
2175 * secattr and attach it to the socket.
2176 *
2177 * Returns 0 on success or an error code
2178 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002179static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002180{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002181 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002182 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002183 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002184
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002185 /*
2186 * Usually the netlabel code will handle changing the
2187 * packet labeling based on the label.
2188 * The case of a single label host is different, because
2189 * a single label host should never get a labeled packet
2190 * even though the label is usually associated with a packet
2191 * label.
2192 */
2193 local_bh_disable();
2194 bh_lock_sock_nested(sk);
2195
2196 if (ssp->smk_out == smack_net_ambient ||
2197 labeled == SMACK_UNLABELED_SOCKET)
2198 netlbl_sock_delattr(sk);
2199 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002200 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002201 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002202 }
2203
2204 bh_unlock_sock(sk);
2205 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002206
Casey Schauflere114e472008-02-04 22:29:50 -08002207 return rc;
2208}
2209
2210/**
Paul Moore07feee82009-03-27 17:10:54 -04002211 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2212 * @sk: the socket
2213 * @sap: the destination address
2214 *
2215 * Set the correct secattr for the given socket based on the destination
2216 * address and perform any outbound access checks needed.
2217 *
2218 * Returns 0 on success or an error code.
2219 *
2220 */
2221static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2222{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002223 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002224 int rc;
2225 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002226 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002227 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002228 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002229
2230 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002231 hkp = smack_host_label(sap);
2232 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002233#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002234 struct lsm_network_audit net;
2235
Eric Paris48c62af2012-04-02 13:15:44 -04002236 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2237 ad.a.u.net->family = sap->sin_family;
2238 ad.a.u.net->dport = sap->sin_port;
2239 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002240#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002241 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002242 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002243 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2244 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002245 } else {
2246 sk_lbl = SMACK_CIPSO_SOCKET;
2247 rc = 0;
2248 }
2249 rcu_read_unlock();
2250 if (rc != 0)
2251 return rc;
2252
2253 return smack_netlabel(sk, sk_lbl);
2254}
2255
Casey Schaufler69f287a2014-12-12 17:08:40 -08002256#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Paul Moore07feee82009-03-27 17:10:54 -04002257/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002258 * smk_ipv6_port_label - Smack port access table management
2259 * @sock: socket
2260 * @address: address
2261 *
2262 * Create or update the port list entry
2263 */
2264static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2265{
2266 struct sock *sk = sock->sk;
2267 struct sockaddr_in6 *addr6;
2268 struct socket_smack *ssp = sock->sk->sk_security;
2269 struct smk_port_label *spp;
2270 unsigned short port = 0;
2271
2272 if (address == NULL) {
2273 /*
2274 * This operation is changing the Smack information
2275 * on the bound socket. Take the changes to the port
2276 * as well.
2277 */
2278 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2279 if (sk != spp->smk_sock)
2280 continue;
2281 spp->smk_in = ssp->smk_in;
2282 spp->smk_out = ssp->smk_out;
2283 return;
2284 }
2285 /*
2286 * A NULL address is only used for updating existing
2287 * bound entries. If there isn't one, it's OK.
2288 */
2289 return;
2290 }
2291
2292 addr6 = (struct sockaddr_in6 *)address;
2293 port = ntohs(addr6->sin6_port);
2294 /*
2295 * This is a special case that is safely ignored.
2296 */
2297 if (port == 0)
2298 return;
2299
2300 /*
2301 * Look for an existing port list entry.
2302 * This is an indication that a port is getting reused.
2303 */
2304 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2305 if (spp->smk_port != port)
2306 continue;
2307 spp->smk_port = port;
2308 spp->smk_sock = sk;
2309 spp->smk_in = ssp->smk_in;
2310 spp->smk_out = ssp->smk_out;
2311 return;
2312 }
2313
2314 /*
2315 * A new port entry is required.
2316 */
2317 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2318 if (spp == NULL)
2319 return;
2320
2321 spp->smk_port = port;
2322 spp->smk_sock = sk;
2323 spp->smk_in = ssp->smk_in;
2324 spp->smk_out = ssp->smk_out;
2325
2326 list_add(&spp->list, &smk_ipv6_port_list);
2327 return;
2328}
2329
2330/**
2331 * smk_ipv6_port_check - check Smack port access
2332 * @sock: socket
2333 * @address: address
2334 *
2335 * Create or update the port list entry
2336 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002337static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002338 int act)
2339{
2340 __be16 *bep;
2341 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002342 struct smk_port_label *spp;
2343 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002344 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002345 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002346 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002347 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002348 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002349#ifdef CONFIG_AUDIT
2350 struct lsm_network_audit net;
2351#endif
2352
2353 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002354 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002355 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002356 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002357 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002358 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002359 }
2360
2361 /*
2362 * Get the IP address and port from the address.
2363 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002364 port = ntohs(address->sin6_port);
2365 bep = (__be16 *)(&address->sin6_addr);
2366 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002367
2368 /*
2369 * It's remote, so port lookup does no good.
2370 */
2371 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2372 goto auditout;
2373
2374 /*
2375 * It's local so the send check has to have passed.
2376 */
2377 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002378 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002379 goto auditout;
2380 }
2381
2382 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2383 if (spp->smk_port != port)
2384 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002385 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002386 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002387 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002388 break;
2389 }
2390
2391auditout:
2392
2393#ifdef CONFIG_AUDIT
2394 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2395 ad.a.u.net->family = sk->sk_family;
2396 ad.a.u.net->dport = port;
2397 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002398 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002399 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002400 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002401#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002402 rc = smk_access(skp, object, MAY_WRITE, &ad);
2403 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2404 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002405}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002406#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002407
2408/**
Casey Schauflere114e472008-02-04 22:29:50 -08002409 * smack_inode_setsecurity - set smack xattrs
2410 * @inode: the object
2411 * @name: attribute name
2412 * @value: attribute value
2413 * @size: size of the attribute
2414 * @flags: unused
2415 *
2416 * Sets the named attribute in the appropriate blob
2417 *
2418 * Returns 0 on success, or an error code
2419 */
2420static int smack_inode_setsecurity(struct inode *inode, const char *name,
2421 const void *value, size_t size, int flags)
2422{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002423 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002424 struct inode_smack *nsp = inode->i_security;
2425 struct socket_smack *ssp;
2426 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002427 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002428
Casey Schauflerf7112e62012-05-06 15:22:02 -07002429 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302430 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002431
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002432 skp = smk_import_entry(value, size);
2433 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002434 return -EINVAL;
2435
2436 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002437 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002438 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002439 return 0;
2440 }
2441 /*
2442 * The rest of the Smack xattrs are only on sockets.
2443 */
2444 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2445 return -EOPNOTSUPP;
2446
2447 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002448 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002449 return -EOPNOTSUPP;
2450
2451 ssp = sock->sk->sk_security;
2452
2453 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002454 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002455 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002456 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002457 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002458 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2459 if (rc != 0)
2460 printk(KERN_WARNING
2461 "Smack: \"%s\" netlbl error %d.\n",
2462 __func__, -rc);
2463 }
Casey Schauflere114e472008-02-04 22:29:50 -08002464 } else
2465 return -EOPNOTSUPP;
2466
Casey Schaufler69f287a2014-12-12 17:08:40 -08002467#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07002468 if (sock->sk->sk_family == PF_INET6)
2469 smk_ipv6_port_label(sock, NULL);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002470#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002471
Casey Schauflere114e472008-02-04 22:29:50 -08002472 return 0;
2473}
2474
2475/**
2476 * smack_socket_post_create - finish socket setup
2477 * @sock: the socket
2478 * @family: protocol family
2479 * @type: unused
2480 * @protocol: unused
2481 * @kern: unused
2482 *
2483 * Sets the netlabel information on the socket
2484 *
2485 * Returns 0 on success, and error code otherwise
2486 */
2487static int smack_socket_post_create(struct socket *sock, int family,
2488 int type, int protocol, int kern)
2489{
Marcin Lis74123012015-01-22 15:40:33 +01002490 struct socket_smack *ssp;
2491
2492 if (sock->sk == NULL)
2493 return 0;
2494
2495 /*
2496 * Sockets created by kernel threads receive web label.
2497 */
2498 if (unlikely(current->flags & PF_KTHREAD)) {
2499 ssp = sock->sk->sk_security;
2500 ssp->smk_in = &smack_known_web;
2501 ssp->smk_out = &smack_known_web;
2502 }
2503
2504 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002505 return 0;
2506 /*
2507 * Set the outbound netlbl.
2508 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002509 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2510}
2511
Casey Schaufler69f287a2014-12-12 17:08:40 -08002512#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002513/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002514 * smack_socket_bind - record port binding information.
2515 * @sock: the socket
2516 * @address: the port address
2517 * @addrlen: size of the address
2518 *
2519 * Records the label bound to a port.
2520 *
2521 * Returns 0
2522 */
2523static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2524 int addrlen)
2525{
Casey Schaufler69f287a2014-12-12 17:08:40 -08002526#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002527 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2528 smk_ipv6_port_label(sock, address);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002529#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002530
2531 return 0;
2532}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002533#endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002534
2535/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002536 * smack_socket_connect - connect access check
2537 * @sock: the socket
2538 * @sap: the other end
2539 * @addrlen: size of sap
2540 *
2541 * Verifies that a connection may be possible
2542 *
2543 * Returns 0 on success, and error code otherwise
2544 */
2545static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2546 int addrlen)
2547{
Casey Schauflerc6739442013-05-22 18:42:56 -07002548 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002549
Casey Schauflerc6739442013-05-22 18:42:56 -07002550 if (sock->sk == NULL)
2551 return 0;
2552
2553 switch (sock->sk->sk_family) {
2554 case PF_INET:
2555 if (addrlen < sizeof(struct sockaddr_in))
2556 return -EINVAL;
2557 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2558 break;
2559 case PF_INET6:
2560 if (addrlen < sizeof(struct sockaddr_in6))
2561 return -EINVAL;
Casey Schaufler69f287a2014-12-12 17:08:40 -08002562#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002563 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2564 SMK_CONNECTING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002565#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002566 break;
2567 }
2568 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002569}
2570
2571/**
2572 * smack_flags_to_may - convert S_ to MAY_ values
2573 * @flags: the S_ value
2574 *
2575 * Returns the equivalent MAY_ value
2576 */
2577static int smack_flags_to_may(int flags)
2578{
2579 int may = 0;
2580
2581 if (flags & S_IRUGO)
2582 may |= MAY_READ;
2583 if (flags & S_IWUGO)
2584 may |= MAY_WRITE;
2585 if (flags & S_IXUGO)
2586 may |= MAY_EXEC;
2587
2588 return may;
2589}
2590
2591/**
2592 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2593 * @msg: the object
2594 *
2595 * Returns 0
2596 */
2597static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2598{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002599 struct smack_known *skp = smk_of_current();
2600
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002601 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002602 return 0;
2603}
2604
2605/**
2606 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2607 * @msg: the object
2608 *
2609 * Clears the blob pointer
2610 */
2611static void smack_msg_msg_free_security(struct msg_msg *msg)
2612{
2613 msg->security = NULL;
2614}
2615
2616/**
2617 * smack_of_shm - the smack pointer for the shm
2618 * @shp: the object
2619 *
2620 * Returns a pointer to the smack value
2621 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002622static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002623{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002624 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002625}
2626
2627/**
2628 * smack_shm_alloc_security - Set the security blob for shm
2629 * @shp: the object
2630 *
2631 * Returns 0
2632 */
2633static int smack_shm_alloc_security(struct shmid_kernel *shp)
2634{
2635 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002636 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002637
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002638 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002639 return 0;
2640}
2641
2642/**
2643 * smack_shm_free_security - Clear the security blob for shm
2644 * @shp: the object
2645 *
2646 * Clears the blob pointer
2647 */
2648static void smack_shm_free_security(struct shmid_kernel *shp)
2649{
2650 struct kern_ipc_perm *isp = &shp->shm_perm;
2651
2652 isp->security = NULL;
2653}
2654
2655/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002656 * smk_curacc_shm : check if current has access on shm
2657 * @shp : the object
2658 * @access : access requested
2659 *
2660 * Returns 0 if current has the requested access, error code otherwise
2661 */
2662static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2663{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002664 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002665 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002666 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002667
2668#ifdef CONFIG_AUDIT
2669 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2670 ad.a.u.ipc_id = shp->shm_perm.id;
2671#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002672 rc = smk_curacc(ssp, access, &ad);
2673 rc = smk_bu_current("shm", ssp, access, rc);
2674 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002675}
2676
2677/**
Casey Schauflere114e472008-02-04 22:29:50 -08002678 * smack_shm_associate - Smack access check for shm
2679 * @shp: the object
2680 * @shmflg: access requested
2681 *
2682 * Returns 0 if current has the requested access, error code otherwise
2683 */
2684static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2685{
Casey Schauflere114e472008-02-04 22:29:50 -08002686 int may;
2687
2688 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002689 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002690}
2691
2692/**
2693 * smack_shm_shmctl - Smack access check for shm
2694 * @shp: the object
2695 * @cmd: what it wants to do
2696 *
2697 * Returns 0 if current has the requested access, error code otherwise
2698 */
2699static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2700{
Casey Schauflere114e472008-02-04 22:29:50 -08002701 int may;
2702
2703 switch (cmd) {
2704 case IPC_STAT:
2705 case SHM_STAT:
2706 may = MAY_READ;
2707 break;
2708 case IPC_SET:
2709 case SHM_LOCK:
2710 case SHM_UNLOCK:
2711 case IPC_RMID:
2712 may = MAY_READWRITE;
2713 break;
2714 case IPC_INFO:
2715 case SHM_INFO:
2716 /*
2717 * System level information.
2718 */
2719 return 0;
2720 default:
2721 return -EINVAL;
2722 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002723 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002724}
2725
2726/**
2727 * smack_shm_shmat - Smack access for shmat
2728 * @shp: the object
2729 * @shmaddr: unused
2730 * @shmflg: access requested
2731 *
2732 * Returns 0 if current has the requested access, error code otherwise
2733 */
2734static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2735 int shmflg)
2736{
Casey Schauflere114e472008-02-04 22:29:50 -08002737 int may;
2738
2739 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002740 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002741}
2742
2743/**
2744 * smack_of_sem - the smack pointer for the sem
2745 * @sma: the object
2746 *
2747 * Returns a pointer to the smack value
2748 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002749static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002750{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002751 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002752}
2753
2754/**
2755 * smack_sem_alloc_security - Set the security blob for sem
2756 * @sma: the object
2757 *
2758 * Returns 0
2759 */
2760static int smack_sem_alloc_security(struct sem_array *sma)
2761{
2762 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002763 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002764
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002765 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002766 return 0;
2767}
2768
2769/**
2770 * smack_sem_free_security - Clear the security blob for sem
2771 * @sma: the object
2772 *
2773 * Clears the blob pointer
2774 */
2775static void smack_sem_free_security(struct sem_array *sma)
2776{
2777 struct kern_ipc_perm *isp = &sma->sem_perm;
2778
2779 isp->security = NULL;
2780}
2781
2782/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002783 * smk_curacc_sem : check if current has access on sem
2784 * @sma : the object
2785 * @access : access requested
2786 *
2787 * Returns 0 if current has the requested access, error code otherwise
2788 */
2789static int smk_curacc_sem(struct sem_array *sma, int access)
2790{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002791 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002792 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002793 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002794
2795#ifdef CONFIG_AUDIT
2796 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2797 ad.a.u.ipc_id = sma->sem_perm.id;
2798#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002799 rc = smk_curacc(ssp, access, &ad);
2800 rc = smk_bu_current("sem", ssp, access, rc);
2801 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002802}
2803
2804/**
Casey Schauflere114e472008-02-04 22:29:50 -08002805 * smack_sem_associate - Smack access check for sem
2806 * @sma: the object
2807 * @semflg: access requested
2808 *
2809 * Returns 0 if current has the requested access, error code otherwise
2810 */
2811static int smack_sem_associate(struct sem_array *sma, int semflg)
2812{
Casey Schauflere114e472008-02-04 22:29:50 -08002813 int may;
2814
2815 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002816 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002817}
2818
2819/**
2820 * smack_sem_shmctl - Smack access check for sem
2821 * @sma: the object
2822 * @cmd: what it wants to do
2823 *
2824 * Returns 0 if current has the requested access, error code otherwise
2825 */
2826static int smack_sem_semctl(struct sem_array *sma, int cmd)
2827{
Casey Schauflere114e472008-02-04 22:29:50 -08002828 int may;
2829
2830 switch (cmd) {
2831 case GETPID:
2832 case GETNCNT:
2833 case GETZCNT:
2834 case GETVAL:
2835 case GETALL:
2836 case IPC_STAT:
2837 case SEM_STAT:
2838 may = MAY_READ;
2839 break;
2840 case SETVAL:
2841 case SETALL:
2842 case IPC_RMID:
2843 case IPC_SET:
2844 may = MAY_READWRITE;
2845 break;
2846 case IPC_INFO:
2847 case SEM_INFO:
2848 /*
2849 * System level information
2850 */
2851 return 0;
2852 default:
2853 return -EINVAL;
2854 }
2855
Etienne Bassetecfcc532009-04-08 20:40:06 +02002856 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002857}
2858
2859/**
2860 * smack_sem_semop - Smack checks of semaphore operations
2861 * @sma: the object
2862 * @sops: unused
2863 * @nsops: unused
2864 * @alter: unused
2865 *
2866 * Treated as read and write in all cases.
2867 *
2868 * Returns 0 if access is allowed, error code otherwise
2869 */
2870static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2871 unsigned nsops, int alter)
2872{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002873 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002874}
2875
2876/**
2877 * smack_msg_alloc_security - Set the security blob for msg
2878 * @msq: the object
2879 *
2880 * Returns 0
2881 */
2882static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2883{
2884 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002885 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002886
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002887 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002888 return 0;
2889}
2890
2891/**
2892 * smack_msg_free_security - Clear the security blob for msg
2893 * @msq: the object
2894 *
2895 * Clears the blob pointer
2896 */
2897static void smack_msg_queue_free_security(struct msg_queue *msq)
2898{
2899 struct kern_ipc_perm *kisp = &msq->q_perm;
2900
2901 kisp->security = NULL;
2902}
2903
2904/**
2905 * smack_of_msq - the smack pointer for the msq
2906 * @msq: the object
2907 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002908 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002909 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002910static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002911{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002912 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002913}
2914
2915/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002916 * smk_curacc_msq : helper to check if current has access on msq
2917 * @msq : the msq
2918 * @access : access requested
2919 *
2920 * return 0 if current has access, error otherwise
2921 */
2922static int smk_curacc_msq(struct msg_queue *msq, int access)
2923{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002924 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002925 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002926 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002927
2928#ifdef CONFIG_AUDIT
2929 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2930 ad.a.u.ipc_id = msq->q_perm.id;
2931#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002932 rc = smk_curacc(msp, access, &ad);
2933 rc = smk_bu_current("msq", msp, access, rc);
2934 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002935}
2936
2937/**
Casey Schauflere114e472008-02-04 22:29:50 -08002938 * smack_msg_queue_associate - Smack access check for msg_queue
2939 * @msq: the object
2940 * @msqflg: access requested
2941 *
2942 * Returns 0 if current has the requested access, error code otherwise
2943 */
2944static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2945{
Casey Schauflere114e472008-02-04 22:29:50 -08002946 int may;
2947
2948 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002949 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002950}
2951
2952/**
2953 * smack_msg_queue_msgctl - Smack access check for msg_queue
2954 * @msq: the object
2955 * @cmd: what it wants to do
2956 *
2957 * Returns 0 if current has the requested access, error code otherwise
2958 */
2959static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2960{
Casey Schauflere114e472008-02-04 22:29:50 -08002961 int may;
2962
2963 switch (cmd) {
2964 case IPC_STAT:
2965 case MSG_STAT:
2966 may = MAY_READ;
2967 break;
2968 case IPC_SET:
2969 case IPC_RMID:
2970 may = MAY_READWRITE;
2971 break;
2972 case IPC_INFO:
2973 case MSG_INFO:
2974 /*
2975 * System level information
2976 */
2977 return 0;
2978 default:
2979 return -EINVAL;
2980 }
2981
Etienne Bassetecfcc532009-04-08 20:40:06 +02002982 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002983}
2984
2985/**
2986 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2987 * @msq: the object
2988 * @msg: unused
2989 * @msqflg: access requested
2990 *
2991 * Returns 0 if current has the requested access, error code otherwise
2992 */
2993static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2994 int msqflg)
2995{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002996 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002997
Etienne Bassetecfcc532009-04-08 20:40:06 +02002998 may = smack_flags_to_may(msqflg);
2999 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003000}
3001
3002/**
3003 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3004 * @msq: the object
3005 * @msg: unused
3006 * @target: unused
3007 * @type: unused
3008 * @mode: unused
3009 *
3010 * Returns 0 if current has read and write access, error code otherwise
3011 */
3012static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3013 struct task_struct *target, long type, int mode)
3014{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003015 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003016}
3017
3018/**
3019 * smack_ipc_permission - Smack access for ipc_permission()
3020 * @ipp: the object permissions
3021 * @flag: access requested
3022 *
3023 * Returns 0 if current has read and write access, error code otherwise
3024 */
3025static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3026{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003027 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003028 int may = smack_flags_to_may(flag);
3029 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003030 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003031
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032#ifdef CONFIG_AUDIT
3033 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3034 ad.a.u.ipc_id = ipp->id;
3035#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003036 rc = smk_curacc(iskp, may, &ad);
3037 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003038 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003039}
3040
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003041/**
3042 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003043 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003044 * @secid: where result will be saved
3045 */
3046static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3047{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003048 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003049
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003050 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003051}
3052
Casey Schauflere114e472008-02-04 22:29:50 -08003053/**
3054 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003055 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003056 * @inode: the object
3057 *
3058 * Set the inode's security blob if it hasn't been done already.
3059 */
3060static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3061{
3062 struct super_block *sbp;
3063 struct superblock_smack *sbsp;
3064 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003065 struct smack_known *skp;
3066 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003067 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003068 char trattr[TRANS_TRUE_SIZE];
3069 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003070 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003071 struct dentry *dp;
3072
3073 if (inode == NULL)
3074 return;
3075
3076 isp = inode->i_security;
3077
3078 mutex_lock(&isp->smk_lock);
3079 /*
3080 * If the inode is already instantiated
3081 * take the quick way out
3082 */
3083 if (isp->smk_flags & SMK_INODE_INSTANT)
3084 goto unlockandout;
3085
3086 sbp = inode->i_sb;
3087 sbsp = sbp->s_security;
3088 /*
3089 * We're going to use the superblock default label
3090 * if there's no label on the file.
3091 */
3092 final = sbsp->smk_default;
3093
3094 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003095 * If this is the root inode the superblock
3096 * may be in the process of initialization.
3097 * If that is the case use the root value out
3098 * of the superblock.
3099 */
3100 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003101 switch (sbp->s_magic) {
3102 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003103 /*
3104 * The cgroup filesystem is never mounted,
3105 * so there's no opportunity to set the mount
3106 * options.
3107 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003108 sbsp->smk_root = &smack_known_star;
3109 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003110 isp->smk_inode = sbsp->smk_root;
3111 break;
3112 case TMPFS_MAGIC:
3113 /*
3114 * What about shmem/tmpfs anonymous files with dentry
3115 * obtained from d_alloc_pseudo()?
3116 */
3117 isp->smk_inode = smk_of_current();
3118 break;
3119 default:
3120 isp->smk_inode = sbsp->smk_root;
3121 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003122 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003123 isp->smk_flags |= SMK_INODE_INSTANT;
3124 goto unlockandout;
3125 }
3126
3127 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003128 * This is pretty hackish.
3129 * Casey says that we shouldn't have to do
3130 * file system specific code, but it does help
3131 * with keeping it simple.
3132 */
3133 switch (sbp->s_magic) {
3134 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003135 case PIPEFS_MAGIC:
3136 case SOCKFS_MAGIC:
3137 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003138 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003139 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003140 * that the smack file system doesn't do
3141 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003142 *
Casey Schauflere114e472008-02-04 22:29:50 -08003143 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003144 *
3145 * Socket access is controlled by the socket
3146 * structures associated with the task involved.
3147 *
3148 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003149 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003150 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003151 break;
3152 case DEVPTS_SUPER_MAGIC:
3153 /*
3154 * devpts seems content with the label of the task.
3155 * Programs that change smack have to treat the
3156 * pty with respect.
3157 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003158 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003159 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003160 case PROC_SUPER_MAGIC:
3161 /*
3162 * Casey says procfs appears not to care.
3163 * The superblock default suffices.
3164 */
3165 break;
3166 case TMPFS_MAGIC:
3167 /*
3168 * Device labels should come from the filesystem,
3169 * but watch out, because they're volitile,
3170 * getting recreated on every reboot.
3171 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003172 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003173 /*
3174 * No break.
3175 *
3176 * If a smack value has been set we want to use it,
3177 * but since tmpfs isn't giving us the opportunity
3178 * to set mount options simulate setting the
3179 * superblock default.
3180 */
3181 default:
3182 /*
3183 * This isn't an understood special case.
3184 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003185 */
3186
3187 /*
3188 * UNIX domain sockets use lower level socket data.
3189 */
3190 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003191 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003192 break;
3193 }
3194 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003195 * No xattr support means, alas, no SMACK label.
3196 * Use the aforeapplied default.
3197 * It would be curious if the label of the task
3198 * does not match that assigned.
3199 */
3200 if (inode->i_op->getxattr == NULL)
3201 break;
3202 /*
3203 * Get the dentry for xattr.
3204 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003205 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003206 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3207 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003208 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003209
3210 /*
3211 * Transmuting directory
3212 */
3213 if (S_ISDIR(inode->i_mode)) {
3214 /*
3215 * If this is a new directory and the label was
3216 * transmuted when the inode was initialized
3217 * set the transmute attribute on the directory
3218 * and mark the inode.
3219 *
3220 * If there is a transmute attribute on the
3221 * directory mark the inode.
3222 */
3223 if (isp->smk_flags & SMK_INODE_CHANGED) {
3224 isp->smk_flags &= ~SMK_INODE_CHANGED;
3225 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003226 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003227 TRANS_TRUE, TRANS_TRUE_SIZE,
3228 0);
3229 } else {
3230 rc = inode->i_op->getxattr(dp,
3231 XATTR_NAME_SMACKTRANSMUTE, trattr,
3232 TRANS_TRUE_SIZE);
3233 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3234 TRANS_TRUE_SIZE) != 0)
3235 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003236 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003237 if (rc >= 0)
3238 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003239 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003240 /*
3241 * Don't let the exec or mmap label be "*" or "@".
3242 */
3243 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3244 if (skp == &smack_known_star || skp == &smack_known_web)
3245 skp = NULL;
3246 isp->smk_task = skp;
3247 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3248 if (skp == &smack_known_star || skp == &smack_known_web)
3249 skp = NULL;
3250 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003251
Casey Schauflere114e472008-02-04 22:29:50 -08003252 dput(dp);
3253 break;
3254 }
3255
3256 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003257 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003258 else
3259 isp->smk_inode = final;
3260
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003261 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003262
3263unlockandout:
3264 mutex_unlock(&isp->smk_lock);
3265 return;
3266}
3267
3268/**
3269 * smack_getprocattr - Smack process attribute access
3270 * @p: the object task
3271 * @name: the name of the attribute in /proc/.../attr
3272 * @value: where to put the result
3273 *
3274 * Places a copy of the task Smack into value
3275 *
3276 * Returns the length of the smack label or an error code
3277 */
3278static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3279{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003280 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003281 char *cp;
3282 int slen;
3283
3284 if (strcmp(name, "current") != 0)
3285 return -EINVAL;
3286
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003287 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003288 if (cp == NULL)
3289 return -ENOMEM;
3290
3291 slen = strlen(cp);
3292 *value = cp;
3293 return slen;
3294}
3295
3296/**
3297 * smack_setprocattr - Smack process attribute setting
3298 * @p: the object task
3299 * @name: the name of the attribute in /proc/.../attr
3300 * @value: the value to set
3301 * @size: the size of the value
3302 *
3303 * Sets the Smack value of the task. Only setting self
3304 * is permitted and only with privilege
3305 *
3306 * Returns the length of the smack label or an error code
3307 */
3308static int smack_setprocattr(struct task_struct *p, char *name,
3309 void *value, size_t size)
3310{
Casey Schaufler676dac42010-12-02 06:43:39 -08003311 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003312 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003313 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003314
Casey Schauflere114e472008-02-04 22:29:50 -08003315 /*
3316 * Changing another process' Smack value is too dangerous
3317 * and supports no sane use case.
3318 */
3319 if (p != current)
3320 return -EPERM;
3321
Casey Schaufler1880eff2012-06-05 15:28:30 -07003322 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003323 return -EPERM;
3324
Casey Schauflerf7112e62012-05-06 15:22:02 -07003325 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003326 return -EINVAL;
3327
3328 if (strcmp(name, "current") != 0)
3329 return -EINVAL;
3330
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003331 skp = smk_import_entry(value, size);
3332 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003333 return -EINVAL;
3334
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003335 /*
3336 * No process is ever allowed the web ("@") label.
3337 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003338 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003339 return -EPERM;
3340
David Howellsd84f4f92008-11-14 10:39:23 +11003341 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003342 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003343 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003344
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003345 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003346 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003347
David Howellsd84f4f92008-11-14 10:39:23 +11003348 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003349 return size;
3350}
3351
3352/**
3353 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003354 * @sock: one sock
3355 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003356 * @newsk: unused
3357 *
3358 * Return 0 if a subject with the smack of sock could access
3359 * an object with the smack of other, otherwise an error code
3360 */
David S. Miller3610cda2011-01-05 15:38:53 -08003361static int smack_unix_stream_connect(struct sock *sock,
3362 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003363{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003364 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003365 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003366 struct socket_smack *ssp = sock->sk_security;
3367 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003368 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003369 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003370 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003371#ifdef CONFIG_AUDIT
3372 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003373#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003374
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003375 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3376 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003377 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003378#ifdef CONFIG_AUDIT
3379 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3380 smk_ad_setfield_u_net_sk(&ad, other);
3381#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003382 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3383 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003384 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003385 okp = osp->smk_out;
3386 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003387 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003388 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003389 MAY_WRITE, rc);
3390 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003391 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003392
Casey Schaufler975d5e52011-09-26 14:43:39 -07003393 /*
3394 * Cross reference the peer labels for SO_PEERSEC.
3395 */
3396 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003397 nsp->smk_packet = ssp->smk_out;
3398 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003399 }
3400
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003401 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003402}
3403
3404/**
3405 * smack_unix_may_send - Smack access on UDS
3406 * @sock: one socket
3407 * @other: the other socket
3408 *
3409 * Return 0 if a subject with the smack of sock could access
3410 * an object with the smack of other, otherwise an error code
3411 */
3412static int smack_unix_may_send(struct socket *sock, struct socket *other)
3413{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003414 struct socket_smack *ssp = sock->sk->sk_security;
3415 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003416 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003417 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003418
Kees Cook923e9a12012-04-10 13:26:44 -07003419#ifdef CONFIG_AUDIT
3420 struct lsm_network_audit net;
3421
Eric Paris48c62af2012-04-02 13:15:44 -04003422 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003423 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003424#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003425
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003426 if (smack_privileged(CAP_MAC_OVERRIDE))
3427 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003428
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003429 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3430 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003431 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003432}
3433
3434/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003435 * smack_socket_sendmsg - Smack check based on destination host
3436 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003437 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003438 * @size: the size of the message
3439 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003440 * Return 0 if the current subject can write to the destination host.
3441 * For IPv4 this is only a question if the destination is a single label host.
3442 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003443 */
3444static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3445 int size)
3446{
3447 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003448#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003449 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003450#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003451 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003452
3453 /*
3454 * Perfectly reasonable for this to be NULL
3455 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003456 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003457 return 0;
3458
Casey Schauflerc6739442013-05-22 18:42:56 -07003459 switch (sip->sin_family) {
3460 case AF_INET:
3461 rc = smack_netlabel_send(sock->sk, sip);
3462 break;
3463 case AF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003464#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07003465 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003466#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003467 break;
3468 }
3469 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003470}
3471
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003472/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003473 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003474 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003475 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003476 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003477 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003478 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003479static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3480 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003481{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003482 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003483 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003484 int acat;
3485 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003486
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003487 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003488 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003489 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003490 * If there are flags but no level netlabel isn't
3491 * behaving the way we expect it to.
3492 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003493 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003494 * Without guidance regarding the smack value
3495 * for the packet fall back on the network
3496 * ambient value.
3497 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003498 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003499 list_for_each_entry(skp, &smack_known_list, list) {
3500 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003501 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003502 /*
3503 * Compare the catsets. Use the netlbl APIs.
3504 */
3505 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3506 if ((skp->smk_netlabel.flags &
3507 NETLBL_SECATTR_MLS_CAT) == 0)
3508 found = 1;
3509 break;
3510 }
3511 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003512 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3513 acat + 1);
3514 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003515 skp->smk_netlabel.attr.mls.cat,
3516 kcat + 1);
3517 if (acat < 0 || kcat < 0)
3518 break;
3519 }
3520 if (acat == kcat) {
3521 found = 1;
3522 break;
3523 }
Casey Schauflere114e472008-02-04 22:29:50 -08003524 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003525 rcu_read_unlock();
3526
3527 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003528 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003529
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003530 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003531 return &smack_known_web;
3532 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003533 }
3534 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3535 /*
3536 * Looks like a fallback, which gives us a secid.
3537 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003538 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003539 /*
3540 * This has got to be a bug because it is
3541 * impossible to specify a fallback without
3542 * specifying the label, which will ensure
3543 * it has a secid, and the only way to get a
3544 * secid is from a fallback.
3545 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003546 BUG_ON(skp == NULL);
3547 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003548 }
3549 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003550 * Without guidance regarding the smack value
3551 * for the packet fall back on the network
3552 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003553 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003554 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003555}
3556
Casey Schaufler69f287a2014-12-12 17:08:40 -08003557#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003558static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003559{
Casey Schauflerc6739442013-05-22 18:42:56 -07003560 u8 nexthdr;
3561 int offset;
3562 int proto = -EINVAL;
3563 struct ipv6hdr _ipv6h;
3564 struct ipv6hdr *ip6;
3565 __be16 frag_off;
3566 struct tcphdr _tcph, *th;
3567 struct udphdr _udph, *uh;
3568 struct dccp_hdr _dccph, *dh;
3569
3570 sip->sin6_port = 0;
3571
3572 offset = skb_network_offset(skb);
3573 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3574 if (ip6 == NULL)
3575 return -EINVAL;
3576 sip->sin6_addr = ip6->saddr;
3577
3578 nexthdr = ip6->nexthdr;
3579 offset += sizeof(_ipv6h);
3580 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3581 if (offset < 0)
3582 return -EINVAL;
3583
3584 proto = nexthdr;
3585 switch (proto) {
3586 case IPPROTO_TCP:
3587 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3588 if (th != NULL)
3589 sip->sin6_port = th->source;
3590 break;
3591 case IPPROTO_UDP:
3592 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3593 if (uh != NULL)
3594 sip->sin6_port = uh->source;
3595 break;
3596 case IPPROTO_DCCP:
3597 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3598 if (dh != NULL)
3599 sip->sin6_port = dh->dccph_sport;
3600 break;
3601 }
3602 return proto;
3603}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003604#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003605
Casey Schauflere114e472008-02-04 22:29:50 -08003606/**
3607 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3608 * @sk: socket
3609 * @skb: packet
3610 *
3611 * Returns 0 if the packet should be delivered, an error code otherwise
3612 */
3613static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3614{
3615 struct netlbl_lsm_secattr secattr;
3616 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003617 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003618 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003619 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003620#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003621 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003622#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003623#if IS_ENABLED(CONFIG_IPV6)
3624 struct sockaddr_in6 sadd;
3625 int proto;
3626#endif /* CONFIG_IPV6 */
3627
Casey Schauflerc6739442013-05-22 18:42:56 -07003628 switch (sk->sk_family) {
3629 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003630#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3631 /*
3632 * If there is a secmark use it rather than the CIPSO label.
3633 * If there is no secmark fall back to CIPSO.
3634 * The secmark is assumed to reflect policy better.
3635 */
3636 if (skb && skb->secmark != 0) {
3637 skp = smack_from_secid(skb->secmark);
3638 goto access_check;
3639 }
3640#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003641 /*
3642 * Translate what netlabel gave us.
3643 */
3644 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003645
Casey Schauflerc6739442013-05-22 18:42:56 -07003646 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3647 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003648 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003649 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003650 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003651
Casey Schauflerc6739442013-05-22 18:42:56 -07003652 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003653
Casey Schaufler69f287a2014-12-12 17:08:40 -08003654#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3655access_check:
3656#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003657#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003658 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3659 ad.a.u.net->family = sk->sk_family;
3660 ad.a.u.net->netif = skb->skb_iif;
3661 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003662#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003663 /*
3664 * Receiving a packet requires that the other end
3665 * be able to write here. Read access is not required.
3666 * This is the simplist possible security model
3667 * for networking.
3668 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003669 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3670 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003671 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003672 if (rc != 0)
3673 netlbl_skbuff_err(skb, rc, 0);
3674 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003675#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003676 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003677 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3678 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3679 break;
3680#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3681 if (skb && skb->secmark != 0)
3682 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003683 else
Casey Schaufler69f287a2014-12-12 17:08:40 -08003684 skp = smack_net_ambient;
3685#ifdef CONFIG_AUDIT
3686 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3687 ad.a.u.net->family = sk->sk_family;
3688 ad.a.u.net->netif = skb->skb_iif;
3689 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3690#endif /* CONFIG_AUDIT */
3691 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3692 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3693 MAY_WRITE, rc);
3694#else /* CONFIG_SECURITY_SMACK_NETFILTER */
3695 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3696#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003697 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003698#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003699 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003700
Paul Moorea8134292008-10-10 10:16:31 -04003701 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003702}
3703
3704/**
3705 * smack_socket_getpeersec_stream - pull in packet label
3706 * @sock: the socket
3707 * @optval: user's destination
3708 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003709 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003710 *
3711 * returns zero on success, an error code otherwise
3712 */
3713static int smack_socket_getpeersec_stream(struct socket *sock,
3714 char __user *optval,
3715 int __user *optlen, unsigned len)
3716{
3717 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003718 char *rcp = "";
3719 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003720 int rc = 0;
3721
3722 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003723 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003724 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003725 slen = strlen(rcp) + 1;
3726 }
Casey Schauflere114e472008-02-04 22:29:50 -08003727
3728 if (slen > len)
3729 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003730 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003731 rc = -EFAULT;
3732
3733 if (put_user(slen, optlen) != 0)
3734 rc = -EFAULT;
3735
3736 return rc;
3737}
3738
3739
3740/**
3741 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003742 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003743 * @skb: packet data
3744 * @secid: pointer to where to put the secid of the packet
3745 *
3746 * Sets the netlabel socket state on sk from parent
3747 */
3748static int smack_socket_getpeersec_dgram(struct socket *sock,
3749 struct sk_buff *skb, u32 *secid)
3750
3751{
3752 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003753 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003754 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003755 int family = PF_UNSPEC;
3756 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003757 int rc;
3758
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003759 if (skb != NULL) {
3760 if (skb->protocol == htons(ETH_P_IP))
3761 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003762#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003763 else if (skb->protocol == htons(ETH_P_IPV6))
3764 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003765#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003766 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003767 if (family == PF_UNSPEC && sock != NULL)
3768 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003769
Casey Schaufler69f287a2014-12-12 17:08:40 -08003770 switch (family) {
3771 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003772 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003773 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003774 break;
3775 case PF_INET:
3776#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3777 s = skb->secmark;
3778 if (s != 0)
3779 break;
3780#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003781 /*
3782 * Translate what netlabel gave us.
3783 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003784 if (sock != NULL && sock->sk != NULL)
3785 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003786 netlbl_secattr_init(&secattr);
3787 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3788 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003789 skp = smack_from_secattr(&secattr, ssp);
3790 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003791 }
3792 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003793 break;
3794#if IS_ENABLED(CONFIG_IPV6)
3795 case PF_INET6:
3796#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3797 s = skb->secmark;
3798#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3799 break;
3800#endif /* CONFIG_IPV6 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003801 }
3802 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003803 if (s == 0)
3804 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003805 return 0;
3806}
3807
3808/**
Paul Moore07feee82009-03-27 17:10:54 -04003809 * smack_sock_graft - Initialize a newly created socket with an existing sock
3810 * @sk: child sock
3811 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003812 *
Paul Moore07feee82009-03-27 17:10:54 -04003813 * Set the smk_{in,out} state of an existing sock based on the process that
3814 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003815 */
3816static void smack_sock_graft(struct sock *sk, struct socket *parent)
3817{
3818 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003819 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003820
Paul Moore07feee82009-03-27 17:10:54 -04003821 if (sk == NULL ||
3822 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003823 return;
3824
3825 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003826 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003827 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003828 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003829}
3830
3831/**
3832 * smack_inet_conn_request - Smack access check on connect
3833 * @sk: socket involved
3834 * @skb: packet
3835 * @req: unused
3836 *
3837 * Returns 0 if a task with the packet label could write to
3838 * the socket, otherwise an error code
3839 */
3840static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3841 struct request_sock *req)
3842{
Paul Moore07feee82009-03-27 17:10:54 -04003843 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003844 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003845 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003846 struct netlbl_lsm_secattr secattr;
3847 struct sockaddr_in addr;
3848 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003849 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003850 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003851 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003852#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003853 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003854#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003855
Casey Schaufler69f287a2014-12-12 17:08:40 -08003856#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003857 if (family == PF_INET6) {
3858 /*
3859 * Handle mapped IPv4 packets arriving
3860 * via IPv6 sockets. Don't set up netlabel
3861 * processing on IPv6.
3862 */
3863 if (skb->protocol == htons(ETH_P_IP))
3864 family = PF_INET;
3865 else
3866 return 0;
3867 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003868#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003869
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003870#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3871 /*
3872 * If there is a secmark use it rather than the CIPSO label.
3873 * If there is no secmark fall back to CIPSO.
3874 * The secmark is assumed to reflect policy better.
3875 */
3876 if (skb && skb->secmark != 0) {
3877 skp = smack_from_secid(skb->secmark);
3878 goto access_check;
3879 }
3880#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3881
Paul Moore07feee82009-03-27 17:10:54 -04003882 netlbl_secattr_init(&secattr);
3883 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003884 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003885 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003886 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003887 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003888 netlbl_secattr_destroy(&secattr);
3889
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003890#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3891access_check:
3892#endif
3893
Etienne Bassetecfcc532009-04-08 20:40:06 +02003894#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003895 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3896 ad.a.u.net->family = family;
3897 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003898 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3899#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003900 /*
Paul Moore07feee82009-03-27 17:10:54 -04003901 * Receiving a packet requires that the other end be able to write
3902 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003903 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003904 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3905 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003906 if (rc != 0)
3907 return rc;
3908
3909 /*
3910 * Save the peer's label in the request_sock so we can later setup
3911 * smk_packet in the child socket so that SO_PEERCRED can report it.
3912 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003913 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003914
3915 /*
3916 * We need to decide if we want to label the incoming connection here
3917 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003918 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003919 */
3920 hdr = ip_hdr(skb);
3921 addr.sin_addr.s_addr = hdr->saddr;
3922 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003923 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003924 rcu_read_unlock();
3925
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003926 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003927 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003928 else
Paul Moore07feee82009-03-27 17:10:54 -04003929 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003930
3931 return rc;
3932}
3933
Paul Moore07feee82009-03-27 17:10:54 -04003934/**
3935 * smack_inet_csk_clone - Copy the connection information to the new socket
3936 * @sk: the new socket
3937 * @req: the connection's request_sock
3938 *
3939 * Transfer the connection's peer label to the newly created socket.
3940 */
3941static void smack_inet_csk_clone(struct sock *sk,
3942 const struct request_sock *req)
3943{
3944 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003945 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003946
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003947 if (req->peer_secid != 0) {
3948 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003949 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003950 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003951 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003952}
3953
Casey Schauflere114e472008-02-04 22:29:50 -08003954/*
3955 * Key management security hooks
3956 *
3957 * Casey has not tested key support very heavily.
3958 * The permission check is most likely too restrictive.
3959 * If you care about keys please have a look.
3960 */
3961#ifdef CONFIG_KEYS
3962
3963/**
3964 * smack_key_alloc - Set the key security blob
3965 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003966 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003967 * @flags: unused
3968 *
3969 * No allocation required
3970 *
3971 * Returns 0
3972 */
David Howellsd84f4f92008-11-14 10:39:23 +11003973static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003974 unsigned long flags)
3975{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003976 struct smack_known *skp = smk_of_task(cred->security);
3977
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003978 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003979 return 0;
3980}
3981
3982/**
3983 * smack_key_free - Clear the key security blob
3984 * @key: the object
3985 *
3986 * Clear the blob pointer
3987 */
3988static void smack_key_free(struct key *key)
3989{
3990 key->security = NULL;
3991}
3992
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003993/**
Casey Schauflere114e472008-02-04 22:29:50 -08003994 * smack_key_permission - Smack access on a key
3995 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003996 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003997 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08003998 *
3999 * Return 0 if the task has read and write to the object,
4000 * an error code otherwise
4001 */
4002static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004003 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004004{
4005 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004006 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004007 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004008 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004009 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004010
4011 keyp = key_ref_to_ptr(key_ref);
4012 if (keyp == NULL)
4013 return -EINVAL;
4014 /*
4015 * If the key hasn't been initialized give it access so that
4016 * it may do so.
4017 */
4018 if (keyp->security == NULL)
4019 return 0;
4020 /*
4021 * This should not occur
4022 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004023 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004024 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004025#ifdef CONFIG_AUDIT
4026 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4027 ad.a.u.key_struct.key = keyp->serial;
4028 ad.a.u.key_struct.key_desc = keyp->description;
4029#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004030 if (perm & KEY_NEED_READ)
4031 request = MAY_READ;
4032 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4033 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004034 rc = smk_access(tkp, keyp->security, request, &ad);
4035 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4036 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004037}
José Bollo7fc5f362015-02-17 15:41:22 +01004038
4039/*
4040 * smack_key_getsecurity - Smack label tagging the key
4041 * @key points to the key to be queried
4042 * @_buffer points to a pointer that should be set to point to the
4043 * resulting string (if no label or an error occurs).
4044 * Return the length of the string (including terminating NUL) or -ve if
4045 * an error.
4046 * May also return 0 (and a NULL buffer pointer) if there is no label.
4047 */
4048static int smack_key_getsecurity(struct key *key, char **_buffer)
4049{
4050 struct smack_known *skp = key->security;
4051 size_t length;
4052 char *copy;
4053
4054 if (key->security == NULL) {
4055 *_buffer = NULL;
4056 return 0;
4057 }
4058
4059 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4060 if (copy == NULL)
4061 return -ENOMEM;
4062 length = strlen(copy) + 1;
4063
4064 *_buffer = copy;
4065 return length;
4066}
4067
Casey Schauflere114e472008-02-04 22:29:50 -08004068#endif /* CONFIG_KEYS */
4069
4070/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004071 * Smack Audit hooks
4072 *
4073 * Audit requires a unique representation of each Smack specific
4074 * rule. This unique representation is used to distinguish the
4075 * object to be audited from remaining kernel objects and also
4076 * works as a glue between the audit hooks.
4077 *
4078 * Since repository entries are added but never deleted, we'll use
4079 * the smack_known label address related to the given audit rule as
4080 * the needed unique representation. This also better fits the smack
4081 * model where nearly everything is a label.
4082 */
4083#ifdef CONFIG_AUDIT
4084
4085/**
4086 * smack_audit_rule_init - Initialize a smack audit rule
4087 * @field: audit rule fields given from user-space (audit.h)
4088 * @op: required testing operator (=, !=, >, <, ...)
4089 * @rulestr: smack label to be audited
4090 * @vrule: pointer to save our own audit rule representation
4091 *
4092 * Prepare to audit cases where (@field @op @rulestr) is true.
4093 * The label to be audited is created if necessay.
4094 */
4095static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4096{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004097 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004098 char **rule = (char **)vrule;
4099 *rule = NULL;
4100
4101 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4102 return -EINVAL;
4103
Al Viro5af75d82008-12-16 05:59:26 -05004104 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004105 return -EINVAL;
4106
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004107 skp = smk_import_entry(rulestr, 0);
4108 if (skp)
4109 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004110
4111 return 0;
4112}
4113
4114/**
4115 * smack_audit_rule_known - Distinguish Smack audit rules
4116 * @krule: rule of interest, in Audit kernel representation format
4117 *
4118 * This is used to filter Smack rules from remaining Audit ones.
4119 * If it's proved that this rule belongs to us, the
4120 * audit_rule_match hook will be called to do the final judgement.
4121 */
4122static int smack_audit_rule_known(struct audit_krule *krule)
4123{
4124 struct audit_field *f;
4125 int i;
4126
4127 for (i = 0; i < krule->field_count; i++) {
4128 f = &krule->fields[i];
4129
4130 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4131 return 1;
4132 }
4133
4134 return 0;
4135}
4136
4137/**
4138 * smack_audit_rule_match - Audit given object ?
4139 * @secid: security id for identifying the object to test
4140 * @field: audit rule flags given from user-space
4141 * @op: required testing operator
4142 * @vrule: smack internal rule presentation
4143 * @actx: audit context associated with the check
4144 *
4145 * The core Audit hook. It's used to take the decision of
4146 * whether to audit or not to audit a given object.
4147 */
4148static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4149 struct audit_context *actx)
4150{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004151 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004152 char *rule = vrule;
4153
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004154 if (unlikely(!rule)) {
4155 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004156 return -ENOENT;
4157 }
4158
4159 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4160 return 0;
4161
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004162 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004163
4164 /*
4165 * No need to do string comparisons. If a match occurs,
4166 * both pointers will point to the same smack_known
4167 * label.
4168 */
Al Viro5af75d82008-12-16 05:59:26 -05004169 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004170 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004171 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004172 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004173
4174 return 0;
4175}
4176
4177/**
4178 * smack_audit_rule_free - free smack rule representation
4179 * @vrule: rule to be freed.
4180 *
4181 * No memory was allocated.
4182 */
4183static void smack_audit_rule_free(void *vrule)
4184{
4185 /* No-op */
4186}
4187
4188#endif /* CONFIG_AUDIT */
4189
Randy Dunlap251a2a92009-02-18 11:42:33 -08004190/**
David Quigley746df9b2013-05-22 12:50:35 -04004191 * smack_ismaclabel - check if xattr @name references a smack MAC label
4192 * @name: Full xattr name to check.
4193 */
4194static int smack_ismaclabel(const char *name)
4195{
4196 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4197}
4198
4199
4200/**
Casey Schauflere114e472008-02-04 22:29:50 -08004201 * smack_secid_to_secctx - return the smack label for a secid
4202 * @secid: incoming integer
4203 * @secdata: destination
4204 * @seclen: how long it is
4205 *
4206 * Exists for networking code.
4207 */
4208static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4209{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004210 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004211
Eric Parisd5630b92010-10-13 16:24:48 -04004212 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004213 *secdata = skp->smk_known;
4214 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004215 return 0;
4216}
4217
Randy Dunlap251a2a92009-02-18 11:42:33 -08004218/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004219 * smack_secctx_to_secid - return the secid for a smack label
4220 * @secdata: smack label
4221 * @seclen: how long result is
4222 * @secid: outgoing integer
4223 *
4224 * Exists for audit and networking code.
4225 */
David Howellse52c17642008-04-29 20:52:51 +01004226static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004227{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004228 struct smack_known *skp = smk_find_entry(secdata);
4229
4230 if (skp)
4231 *secid = skp->smk_secid;
4232 else
4233 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004234 return 0;
4235}
4236
Randy Dunlap251a2a92009-02-18 11:42:33 -08004237/**
Casey Schauflere114e472008-02-04 22:29:50 -08004238 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004239 * @secdata: unused
4240 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004241 *
4242 * Exists to make sure nothing gets done, and properly
4243 */
4244static void smack_release_secctx(char *secdata, u32 seclen)
4245{
4246}
4247
David P. Quigley1ee65e32009-09-03 14:25:57 -04004248static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4249{
4250 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4251}
4252
4253static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4254{
4255 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4256}
4257
4258static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4259{
4260 int len = 0;
4261 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4262
4263 if (len < 0)
4264 return len;
4265 *ctxlen = len;
4266 return 0;
4267}
4268
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02004269struct security_operations smack_ops = {
4270 .name = "smack",
4271
Ingo Molnar9e488582009-05-07 19:26:19 +10004272 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01004273 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08004274 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08004275
4276 .sb_alloc_security = smack_sb_alloc_security,
4277 .sb_free_security = smack_sb_free_security,
4278 .sb_copy_data = smack_sb_copy_data,
4279 .sb_kern_mount = smack_sb_kern_mount,
4280 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08004281
Casey Schaufler676dac42010-12-02 06:43:39 -08004282 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03004283 .bprm_committing_creds = smack_bprm_committing_creds,
4284 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08004285
Casey Schauflere114e472008-02-04 22:29:50 -08004286 .inode_alloc_security = smack_inode_alloc_security,
4287 .inode_free_security = smack_inode_free_security,
4288 .inode_init_security = smack_inode_init_security,
4289 .inode_link = smack_inode_link,
4290 .inode_unlink = smack_inode_unlink,
4291 .inode_rmdir = smack_inode_rmdir,
4292 .inode_rename = smack_inode_rename,
4293 .inode_permission = smack_inode_permission,
4294 .inode_setattr = smack_inode_setattr,
4295 .inode_getattr = smack_inode_getattr,
4296 .inode_setxattr = smack_inode_setxattr,
4297 .inode_post_setxattr = smack_inode_post_setxattr,
4298 .inode_getxattr = smack_inode_getxattr,
4299 .inode_removexattr = smack_inode_removexattr,
4300 .inode_getsecurity = smack_inode_getsecurity,
4301 .inode_setsecurity = smack_inode_setsecurity,
4302 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004303 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004304
4305 .file_permission = smack_file_permission,
4306 .file_alloc_security = smack_file_alloc_security,
4307 .file_free_security = smack_file_free_security,
4308 .file_ioctl = smack_file_ioctl,
4309 .file_lock = smack_file_lock,
4310 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04004311 .mmap_file = smack_mmap_file,
4312 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08004313 .file_set_fowner = smack_file_set_fowner,
4314 .file_send_sigiotask = smack_file_send_sigiotask,
4315 .file_receive = smack_file_receive,
4316
Eric Paris83d49852012-04-04 13:45:40 -04004317 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07004318
David Howellsee18d642009-09-02 09:14:21 +01004319 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11004320 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11004321 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01004322 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11004323 .kernel_act_as = smack_kernel_act_as,
4324 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08004325 .task_setpgid = smack_task_setpgid,
4326 .task_getpgid = smack_task_getpgid,
4327 .task_getsid = smack_task_getsid,
4328 .task_getsecid = smack_task_getsecid,
4329 .task_setnice = smack_task_setnice,
4330 .task_setioprio = smack_task_setioprio,
4331 .task_getioprio = smack_task_getioprio,
4332 .task_setscheduler = smack_task_setscheduler,
4333 .task_getscheduler = smack_task_getscheduler,
4334 .task_movememory = smack_task_movememory,
4335 .task_kill = smack_task_kill,
4336 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08004337 .task_to_inode = smack_task_to_inode,
4338
4339 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004340 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004341
4342 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4343 .msg_msg_free_security = smack_msg_msg_free_security,
4344
4345 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4346 .msg_queue_free_security = smack_msg_queue_free_security,
4347 .msg_queue_associate = smack_msg_queue_associate,
4348 .msg_queue_msgctl = smack_msg_queue_msgctl,
4349 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4350 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4351
4352 .shm_alloc_security = smack_shm_alloc_security,
4353 .shm_free_security = smack_shm_free_security,
4354 .shm_associate = smack_shm_associate,
4355 .shm_shmctl = smack_shm_shmctl,
4356 .shm_shmat = smack_shm_shmat,
4357
4358 .sem_alloc_security = smack_sem_alloc_security,
4359 .sem_free_security = smack_sem_free_security,
4360 .sem_associate = smack_sem_associate,
4361 .sem_semctl = smack_sem_semctl,
4362 .sem_semop = smack_sem_semop,
4363
Casey Schauflere114e472008-02-04 22:29:50 -08004364 .d_instantiate = smack_d_instantiate,
4365
4366 .getprocattr = smack_getprocattr,
4367 .setprocattr = smack_setprocattr,
4368
4369 .unix_stream_connect = smack_unix_stream_connect,
4370 .unix_may_send = smack_unix_may_send,
4371
4372 .socket_post_create = smack_socket_post_create,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004373#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schauflerc6739442013-05-22 18:42:56 -07004374 .socket_bind = smack_socket_bind,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004375#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004376 .socket_connect = smack_socket_connect,
4377 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08004378 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4379 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4380 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4381 .sk_alloc_security = smack_sk_alloc_security,
4382 .sk_free_security = smack_sk_free_security,
4383 .sock_graft = smack_sock_graft,
4384 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04004385 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004386
Casey Schauflere114e472008-02-04 22:29:50 -08004387 /* key management security hooks */
4388#ifdef CONFIG_KEYS
4389 .key_alloc = smack_key_alloc,
4390 .key_free = smack_key_free,
4391 .key_permission = smack_key_permission,
José Bollo7fc5f362015-02-17 15:41:22 +01004392 .key_getsecurity = smack_key_getsecurity,
Casey Schauflere114e472008-02-04 22:29:50 -08004393#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004394
4395 /* Audit hooks */
4396#ifdef CONFIG_AUDIT
4397 .audit_rule_init = smack_audit_rule_init,
4398 .audit_rule_known = smack_audit_rule_known,
4399 .audit_rule_match = smack_audit_rule_match,
4400 .audit_rule_free = smack_audit_rule_free,
4401#endif /* CONFIG_AUDIT */
4402
David Quigley746df9b2013-05-22 12:50:35 -04004403 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08004404 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004405 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08004406 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04004407 .inode_notifysecctx = smack_inode_notifysecctx,
4408 .inode_setsecctx = smack_inode_setsecctx,
4409 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08004410};
4411
Etienne Basset7198e2e2009-03-24 20:53:24 +01004412
Casey Schaufler86812bb2012-04-17 18:55:46 -07004413static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004414{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004415 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004416 * Initialize rule list locks
4417 */
4418 mutex_init(&smack_known_huh.smk_rules_lock);
4419 mutex_init(&smack_known_hat.smk_rules_lock);
4420 mutex_init(&smack_known_floor.smk_rules_lock);
4421 mutex_init(&smack_known_star.smk_rules_lock);
4422 mutex_init(&smack_known_invalid.smk_rules_lock);
4423 mutex_init(&smack_known_web.smk_rules_lock);
4424 /*
4425 * Initialize rule lists
4426 */
4427 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4428 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4429 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4430 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4431 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4432 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4433 /*
4434 * Create the known labels list
4435 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004436 smk_insert_entry(&smack_known_huh);
4437 smk_insert_entry(&smack_known_hat);
4438 smk_insert_entry(&smack_known_star);
4439 smk_insert_entry(&smack_known_floor);
4440 smk_insert_entry(&smack_known_invalid);
4441 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004442}
4443
Casey Schauflere114e472008-02-04 22:29:50 -08004444/**
4445 * smack_init - initialize the smack system
4446 *
4447 * Returns 0
4448 */
4449static __init int smack_init(void)
4450{
David Howellsd84f4f92008-11-14 10:39:23 +11004451 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004452 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004453
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004454 if (!security_module_enable(&smack_ops))
4455 return 0;
4456
Casey Schaufler69f287a2014-12-12 17:08:40 -08004457 smack_enabled = 1;
4458
Rohit1a5b4722014-10-15 17:40:41 +05304459 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4460 if (!smack_inode_cache)
4461 return -ENOMEM;
4462
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004463 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4464 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304465 if (tsp == NULL) {
4466 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004467 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304468 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004469
Casey Schauflere114e472008-02-04 22:29:50 -08004470 printk(KERN_INFO "Smack: Initializing.\n");
4471
4472 /*
4473 * Set the security state for the initial task.
4474 */
David Howellsd84f4f92008-11-14 10:39:23 +11004475 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004476 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004477
Casey Schaufler86812bb2012-04-17 18:55:46 -07004478 /* initialize the smack_known_list */
4479 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004480
4481 /*
4482 * Register with LSM
4483 */
4484 if (register_security(&smack_ops))
4485 panic("smack: Unable to register with kernel.\n");
4486
4487 return 0;
4488}
4489
4490/*
4491 * Smack requires early initialization in order to label
4492 * all processes and objects when they are created.
4493 */
4494security_initcall(smack_init);