blob: 5eae42c8d0d5dcf2a05601f33b794f597cc4594b [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{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700439 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800440
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300441 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200442
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700443 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100444}
Casey Schauflere114e472008-02-04 22:29:50 -0800445
David Howells5cd9c582008-08-14 11:37:28 +0100446/**
447 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
448 * @ptp: parent task pointer
449 *
450 * Returns 0 if access is OK, an error code otherwise
451 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100452 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100453 */
454static int smack_ptrace_traceme(struct task_struct *ptp)
455{
456 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700457 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100458
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100459 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200460
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200461 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800462 return rc;
463}
464
465/**
466 * smack_syslog - Smack approval on syslog
467 * @type: message type
468 *
Casey Schauflere114e472008-02-04 22:29:50 -0800469 * Returns 0 on success, error code otherwise.
470 */
Eric Paris12b30522010-11-15 18:36:29 -0500471static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800472{
Eric Paris12b30522010-11-15 18:36:29 -0500473 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700474 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800475
Casey Schaufler1880eff2012-06-05 15:28:30 -0700476 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800477 return 0;
478
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800479 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800480 rc = -EACCES;
481
482 return rc;
483}
484
485
486/*
487 * Superblock Hooks.
488 */
489
490/**
491 * smack_sb_alloc_security - allocate a superblock blob
492 * @sb: the superblock getting the blob
493 *
494 * Returns 0 on success or -ENOMEM on error.
495 */
496static int smack_sb_alloc_security(struct super_block *sb)
497{
498 struct superblock_smack *sbsp;
499
500 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
501
502 if (sbsp == NULL)
503 return -ENOMEM;
504
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200505 sbsp->smk_root = &smack_known_floor;
506 sbsp->smk_default = &smack_known_floor;
507 sbsp->smk_floor = &smack_known_floor;
508 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700509 /*
510 * smk_initialized will be zero from kzalloc.
511 */
Casey Schauflere114e472008-02-04 22:29:50 -0800512 sb->s_security = sbsp;
513
514 return 0;
515}
516
517/**
518 * smack_sb_free_security - free a superblock blob
519 * @sb: the superblock getting the blob
520 *
521 */
522static void smack_sb_free_security(struct super_block *sb)
523{
524 kfree(sb->s_security);
525 sb->s_security = NULL;
526}
527
528/**
529 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800530 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800531 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800532 *
533 * Returns 0 on success or -ENOMEM on error.
534 *
535 * Copy the Smack specific mount options out of the mount
536 * options list.
537 */
Eric Parise0007522008-03-05 10:31:54 -0500538static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800539{
540 char *cp, *commap, *otheropts, *dp;
541
Casey Schauflere114e472008-02-04 22:29:50 -0800542 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
543 if (otheropts == NULL)
544 return -ENOMEM;
545
546 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
547 if (strstr(cp, SMK_FSDEFAULT) == cp)
548 dp = smackopts;
549 else if (strstr(cp, SMK_FSFLOOR) == cp)
550 dp = smackopts;
551 else if (strstr(cp, SMK_FSHAT) == cp)
552 dp = smackopts;
553 else if (strstr(cp, SMK_FSROOT) == cp)
554 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700555 else if (strstr(cp, SMK_FSTRANS) == cp)
556 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800557 else
558 dp = otheropts;
559
560 commap = strchr(cp, ',');
561 if (commap != NULL)
562 *commap = '\0';
563
564 if (*dp != '\0')
565 strcat(dp, ",");
566 strcat(dp, cp);
567 }
568
569 strcpy(orig, otheropts);
570 free_page((unsigned long)otheropts);
571
572 return 0;
573}
574
575/**
576 * smack_sb_kern_mount - Smack specific mount processing
577 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100578 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800579 * @data: the smack mount options
580 *
581 * Returns 0 on success, an error code on failure
582 */
James Morris12204e22008-12-19 10:44:42 +1100583static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800584{
585 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000586 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800587 struct superblock_smack *sp = sb->s_security;
588 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800589 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800590 char *op;
591 char *commap;
Casey Schauflere830b392013-05-22 18:43:07 -0700592 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800593 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800594
Casey Schauflere830b392013-05-22 18:43:07 -0700595 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800596 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700597
Casey Schauflere114e472008-02-04 22:29:50 -0800598 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800599
600 for (op = data; op != NULL; op = commap) {
601 commap = strchr(op, ',');
602 if (commap != NULL)
603 *commap++ = '\0';
604
605 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
606 op += strlen(SMK_FSHAT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200607 skp = smk_import_entry(op, 0);
608 if (skp != NULL) {
609 sp->smk_hat = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800610 specified = 1;
611 }
Casey Schauflere114e472008-02-04 22:29:50 -0800612 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
613 op += strlen(SMK_FSFLOOR);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200614 skp = smk_import_entry(op, 0);
615 if (skp != NULL) {
616 sp->smk_floor = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800617 specified = 1;
618 }
Casey Schauflere114e472008-02-04 22:29:50 -0800619 } else if (strncmp(op, SMK_FSDEFAULT,
620 strlen(SMK_FSDEFAULT)) == 0) {
621 op += strlen(SMK_FSDEFAULT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200622 skp = smk_import_entry(op, 0);
623 if (skp != NULL) {
624 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800625 specified = 1;
626 }
Casey Schauflere114e472008-02-04 22:29:50 -0800627 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
628 op += strlen(SMK_FSROOT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200629 skp = smk_import_entry(op, 0);
630 if (skp != NULL) {
631 sp->smk_root = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800632 specified = 1;
633 }
Casey Schauflere830b392013-05-22 18:43:07 -0700634 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
635 op += strlen(SMK_FSTRANS);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200636 skp = smk_import_entry(op, 0);
637 if (skp != NULL) {
638 sp->smk_root = skp;
Casey Schauflere830b392013-05-22 18:43:07 -0700639 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800640 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700641 }
Casey Schauflere114e472008-02-04 22:29:50 -0800642 }
643 }
644
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800645 if (!smack_privileged(CAP_MAC_ADMIN)) {
646 /*
647 * Unprivileged mounts don't get to specify Smack values.
648 */
649 if (specified)
650 return -EPERM;
651 /*
652 * Unprivileged mounts get root and default from the caller.
653 */
654 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200655 sp->smk_root = skp;
656 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800657 }
Casey Schauflere114e472008-02-04 22:29:50 -0800658 /*
659 * Initialize the root inode.
660 */
661 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100662 if (isp == NULL) {
663 isp = new_inode_smack(sp->smk_root);
664 if (isp == NULL)
665 return -ENOMEM;
666 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700667 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800668 isp->smk_inode = sp->smk_root;
669
Casey Schauflere830b392013-05-22 18:43:07 -0700670 if (transmute)
671 isp->smk_flags |= SMK_INODE_TRANSMUTE;
672
Casey Schauflere114e472008-02-04 22:29:50 -0800673 return 0;
674}
675
676/**
677 * smack_sb_statfs - Smack check on statfs
678 * @dentry: identifies the file system in question
679 *
680 * Returns 0 if current can read the floor of the filesystem,
681 * and error code otherwise
682 */
683static int smack_sb_statfs(struct dentry *dentry)
684{
685 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200686 int rc;
687 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800688
Eric Parisa2694342011-04-25 13:10:27 -0400689 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200690 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
691
692 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700693 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200694 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800695}
696
Casey Schauflere114e472008-02-04 22:29:50 -0800697/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800698 * BPRM hooks
699 */
700
Casey Schauflerce8a4322011-09-29 18:21:01 -0700701/**
702 * smack_bprm_set_creds - set creds for exec
703 * @bprm: the exec information
704 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100705 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700706 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800707static int smack_bprm_set_creds(struct linux_binprm *bprm)
708{
Al Viro496ad9a2013-01-23 17:07:38 -0500709 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300710 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800711 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800712 int rc;
713
Casey Schaufler676dac42010-12-02 06:43:39 -0800714 if (bprm->cred_prepared)
715 return 0;
716
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300717 isp = inode->i_security;
718 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800719 return 0;
720
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100721 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
722 struct task_struct *tracer;
723 rc = 0;
724
725 rcu_read_lock();
726 tracer = ptrace_parent(current);
727 if (likely(tracer != NULL))
728 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200729 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100730 PTRACE_MODE_ATTACH,
731 __func__);
732 rcu_read_unlock();
733
734 if (rc != 0)
735 return rc;
736 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300737 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800738
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300739 bsp->smk_task = isp->smk_task;
740 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800741
742 return 0;
743}
744
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300745/**
746 * smack_bprm_committing_creds - Prepare to install the new credentials
747 * from bprm.
748 *
749 * @bprm: binprm for exec
750 */
751static void smack_bprm_committing_creds(struct linux_binprm *bprm)
752{
753 struct task_smack *bsp = bprm->cred->security;
754
755 if (bsp->smk_task != bsp->smk_forked)
756 current->pdeath_signal = 0;
757}
758
759/**
760 * smack_bprm_secureexec - Return the decision to use secureexec.
761 * @bprm: binprm for exec
762 *
763 * Returns 0 on success.
764 */
765static int smack_bprm_secureexec(struct linux_binprm *bprm)
766{
767 struct task_smack *tsp = current_security();
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300768
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700769 if (tsp->smk_task != tsp->smk_forked)
770 return 1;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300771
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700772 return 0;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300773}
774
Casey Schaufler676dac42010-12-02 06:43:39 -0800775/*
Casey Schauflere114e472008-02-04 22:29:50 -0800776 * Inode hooks
777 */
778
779/**
780 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800781 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800782 *
783 * Returns 0 if it gets a blob, -ENOMEM otherwise
784 */
785static int smack_inode_alloc_security(struct inode *inode)
786{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700787 struct smack_known *skp = smk_of_current();
788
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200789 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800790 if (inode->i_security == NULL)
791 return -ENOMEM;
792 return 0;
793}
794
795/**
796 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800797 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800798 *
799 * Clears the blob pointer in inode
800 */
801static void smack_inode_free_security(struct inode *inode)
802{
Rohit1a5b4722014-10-15 17:40:41 +0530803 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800804 inode->i_security = NULL;
805}
806
807/**
808 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200809 * @inode: the newly created inode
810 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500811 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800812 * @name: where to put the attribute name
813 * @value: where to put the attribute value
814 * @len: where to put the length of the attribute
815 *
816 * Returns 0 if it all works out, -ENOMEM if there's no memory
817 */
818static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900819 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500820 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800821{
Casey Schaufler2267b132012-03-13 19:14:19 -0700822 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700823 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200824 struct smack_known *isp = smk_of_inode(inode);
825 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800826 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800827
Tetsuo Handa95489062013-07-25 05:44:02 +0900828 if (name)
829 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800830
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100831 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800832 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200833 may = smk_access_entry(skp->smk_known, dsp->smk_known,
834 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800835 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200836
837 /*
838 * If the access rule allows transmutation and
839 * the directory requests transmutation then
840 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700841 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200842 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800843 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700844 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200845 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700846 issp->smk_flags |= SMK_INODE_CHANGED;
847 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200848
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200849 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800850 if (*value == NULL)
851 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800852
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200853 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100854 }
Casey Schauflere114e472008-02-04 22:29:50 -0800855
856 return 0;
857}
858
859/**
860 * smack_inode_link - Smack check on link
861 * @old_dentry: the existing object
862 * @dir: unused
863 * @new_dentry: the new object
864 *
865 * Returns 0 if access is permitted, an error code otherwise
866 */
867static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
868 struct dentry *new_dentry)
869{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200870 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200871 struct smk_audit_info ad;
872 int rc;
873
Eric Parisa2694342011-04-25 13:10:27 -0400874 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200875 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800876
David Howellsc6f493d2015-03-17 22:26:22 +0000877 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200878 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000879 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800880
David Howells88025652015-01-29 12:02:32 +0000881 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +0000882 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200883 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
884 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000885 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800886 }
887
888 return rc;
889}
890
891/**
892 * smack_inode_unlink - Smack check on inode deletion
893 * @dir: containing directory object
894 * @dentry: file to unlink
895 *
896 * Returns 0 if current can write the containing directory
897 * and the object, error code otherwise
898 */
899static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
900{
David Howellsc6f493d2015-03-17 22:26:22 +0000901 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200902 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800903 int rc;
904
Eric Parisa2694342011-04-25 13:10:27 -0400905 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200906 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
907
Casey Schauflere114e472008-02-04 22:29:50 -0800908 /*
909 * You need write access to the thing you're unlinking
910 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200911 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700912 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200913 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800914 /*
915 * You also need write access to the containing directory
916 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400917 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200918 smk_ad_setfield_u_fs_inode(&ad, dir);
919 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700920 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200921 }
Casey Schauflere114e472008-02-04 22:29:50 -0800922 return rc;
923}
924
925/**
926 * smack_inode_rmdir - Smack check on directory deletion
927 * @dir: containing directory object
928 * @dentry: directory to unlink
929 *
930 * Returns 0 if current can write the containing directory
931 * and the directory, error code otherwise
932 */
933static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
934{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200935 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800936 int rc;
937
Eric Parisa2694342011-04-25 13:10:27 -0400938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200939 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
940
Casey Schauflere114e472008-02-04 22:29:50 -0800941 /*
942 * You need write access to the thing you're removing
943 */
David Howellsc6f493d2015-03-17 22:26:22 +0000944 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
945 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200946 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800947 /*
948 * You also need write access to the containing directory
949 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400950 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200951 smk_ad_setfield_u_fs_inode(&ad, dir);
952 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700953 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200954 }
Casey Schauflere114e472008-02-04 22:29:50 -0800955
956 return rc;
957}
958
959/**
960 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200961 * @old_inode: unused
962 * @old_dentry: the old object
963 * @new_inode: unused
964 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -0800965 *
966 * Read and write access is required on both the old and
967 * new directories.
968 *
969 * Returns 0 if access is permitted, an error code otherwise
970 */
971static int smack_inode_rename(struct inode *old_inode,
972 struct dentry *old_dentry,
973 struct inode *new_inode,
974 struct dentry *new_dentry)
975{
976 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200977 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200978 struct smk_audit_info ad;
979
Eric Parisa2694342011-04-25 13:10:27 -0400980 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200981 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800982
David Howellsc6f493d2015-03-17 22:26:22 +0000983 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200984 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000985 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800986
David Howells88025652015-01-29 12:02:32 +0000987 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +0000988 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200989 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
990 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000991 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800992 }
Casey Schauflere114e472008-02-04 22:29:50 -0800993 return rc;
994}
995
996/**
997 * smack_inode_permission - Smack version of permission()
998 * @inode: the inode in question
999 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001000 *
1001 * This is the important Smack hook.
1002 *
1003 * Returns 0 if access is permitted, -EACCES otherwise
1004 */
Al Viroe74f71e2011-06-20 19:38:15 -04001005static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001006{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001007 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001008 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001009 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001010
1011 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001012 /*
1013 * No permission to check. Existence test. Yup, it's there.
1014 */
1015 if (mask == 0)
1016 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001017
1018 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001019 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001020 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001021 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001022 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001023 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1024 rc = smk_bu_inode(inode, mask, rc);
1025 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001026}
1027
1028/**
1029 * smack_inode_setattr - Smack check for setting attributes
1030 * @dentry: the object
1031 * @iattr: for the force flag
1032 *
1033 * Returns 0 if access is permitted, an error code otherwise
1034 */
1035static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1036{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001037 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001038 int rc;
1039
Casey Schauflere114e472008-02-04 22:29:50 -08001040 /*
1041 * Need to allow for clearing the setuid bit.
1042 */
1043 if (iattr->ia_valid & ATTR_FORCE)
1044 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001045 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001046 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001047
David Howellsc6f493d2015-03-17 22:26:22 +00001048 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1049 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001050 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001051}
1052
1053/**
1054 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001055 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001056 * @dentry: the object
1057 *
1058 * Returns 0 if access is permitted, an error code otherwise
1059 */
Al Viro3f7036a2015-03-08 19:28:30 -04001060static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001061{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001062 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001063 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001064 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001065
Eric Parisf48b7392011-04-25 12:54:27 -04001066 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001067 smk_ad_setfield_u_fs_path(&ad, *path);
1068 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1069 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001070 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001071}
1072
1073/**
1074 * smack_inode_setxattr - Smack check for setting xattrs
1075 * @dentry: the object
1076 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001077 * @value: value of the attribute
1078 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001079 * @flags: unused
1080 *
1081 * This protects the Smack attribute explicitly.
1082 *
1083 * Returns 0 if access is permitted, an error code otherwise
1084 */
David Howells8f0cfa52008-04-29 00:59:41 -07001085static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1086 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001087{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001088 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001089 struct smack_known *skp;
1090 int check_priv = 0;
1091 int check_import = 0;
1092 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001093 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001094
Casey Schaufler19760ad2013-12-16 16:27:26 -08001095 /*
1096 * Check label validity here so import won't fail in post_setxattr
1097 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001098 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1099 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001100 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1101 check_priv = 1;
1102 check_import = 1;
1103 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1104 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1105 check_priv = 1;
1106 check_import = 1;
1107 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001108 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001109 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001110 if (size != TRANS_TRUE_SIZE ||
1111 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1112 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001113 } else
1114 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1115
Casey Schaufler19760ad2013-12-16 16:27:26 -08001116 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1117 rc = -EPERM;
1118
1119 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001120 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001121 if (skp == NULL || (check_star &&
1122 (skp == &smack_known_star || skp == &smack_known_web)))
1123 rc = -EINVAL;
1124 }
1125
Eric Parisa2694342011-04-25 13:10:27 -04001126 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001127 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1128
Casey Schauflerd166c802014-08-27 14:51:27 -07001129 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001130 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1131 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001132 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001133
1134 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001135}
1136
1137/**
1138 * smack_inode_post_setxattr - Apply the Smack update approved above
1139 * @dentry: object
1140 * @name: attribute name
1141 * @value: attribute value
1142 * @size: attribute size
1143 * @flags: unused
1144 *
1145 * Set the pointer in the inode blob to the entry found
1146 * in the master label list.
1147 */
David Howells8f0cfa52008-04-29 00:59:41 -07001148static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1149 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001150{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001151 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001152 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001153
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001154 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1155 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1156 return;
1157 }
1158
Casey Schaufler676dac42010-12-02 06:43:39 -08001159 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001160 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001161 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001162 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001163 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001164 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001165 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001166 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001167 if (skp != NULL)
1168 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001169 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001170 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001171 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001172 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001173 if (skp != NULL)
1174 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001175 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001176 isp->smk_mmap = &smack_known_invalid;
1177 }
Casey Schauflere114e472008-02-04 22:29:50 -08001178
1179 return;
1180}
1181
Casey Schauflerce8a4322011-09-29 18:21:01 -07001182/**
Casey Schauflere114e472008-02-04 22:29:50 -08001183 * smack_inode_getxattr - Smack check on getxattr
1184 * @dentry: the object
1185 * @name: unused
1186 *
1187 * Returns 0 if access is permitted, an error code otherwise
1188 */
David Howells8f0cfa52008-04-29 00:59:41 -07001189static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001190{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001191 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001192 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001193
Eric Parisa2694342011-04-25 13:10:27 -04001194 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001195 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1196
David Howellsc6f493d2015-03-17 22:26:22 +00001197 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1198 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001199 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001200}
1201
Casey Schauflerce8a4322011-09-29 18:21:01 -07001202/**
Casey Schauflere114e472008-02-04 22:29:50 -08001203 * smack_inode_removexattr - Smack check on removexattr
1204 * @dentry: the object
1205 * @name: name of the attribute
1206 *
1207 * Removing the Smack attribute requires CAP_MAC_ADMIN
1208 *
1209 * Returns 0 if access is permitted, an error code otherwise
1210 */
David Howells8f0cfa52008-04-29 00:59:41 -07001211static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001212{
Casey Schaufler676dac42010-12-02 06:43:39 -08001213 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001214 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001215 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001216
Casey Schauflerbcdca222008-02-23 15:24:04 -08001217 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1218 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001219 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001220 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001221 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301222 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001223 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001224 rc = -EPERM;
1225 } else
1226 rc = cap_inode_removexattr(dentry, name);
1227
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001228 if (rc != 0)
1229 return rc;
1230
Eric Parisa2694342011-04-25 13:10:27 -04001231 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001232 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001233
David Howellsc6f493d2015-03-17 22:26:22 +00001234 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1235 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001236 if (rc != 0)
1237 return rc;
1238
David Howellsc6f493d2015-03-17 22:26:22 +00001239 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001240 /*
1241 * Don't do anything special for these.
1242 * XATTR_NAME_SMACKIPIN
1243 * XATTR_NAME_SMACKIPOUT
1244 * XATTR_NAME_SMACKEXEC
1245 */
1246 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001247 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001248 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001249 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001250 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1251 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001252
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001253 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001254}
1255
1256/**
1257 * smack_inode_getsecurity - get smack xattrs
1258 * @inode: the object
1259 * @name: attribute name
1260 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001261 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001262 *
1263 * Returns the size of the attribute or an error code
1264 */
1265static int smack_inode_getsecurity(const struct inode *inode,
1266 const char *name, void **buffer,
1267 bool alloc)
1268{
1269 struct socket_smack *ssp;
1270 struct socket *sock;
1271 struct super_block *sbp;
1272 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001273 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001274 int ilen;
1275 int rc = 0;
1276
1277 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1278 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001279 ilen = strlen(isp->smk_known);
1280 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001281 return ilen;
1282 }
1283
1284 /*
1285 * The rest of the Smack xattrs are only on sockets.
1286 */
1287 sbp = ip->i_sb;
1288 if (sbp->s_magic != SOCKFS_MAGIC)
1289 return -EOPNOTSUPP;
1290
1291 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001292 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001293 return -EOPNOTSUPP;
1294
1295 ssp = sock->sk->sk_security;
1296
1297 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001298 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001299 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001300 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001301 else
1302 return -EOPNOTSUPP;
1303
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001304 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001305 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001306 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001307 rc = ilen;
1308 }
1309
1310 return rc;
1311}
1312
1313
1314/**
1315 * smack_inode_listsecurity - list the Smack attributes
1316 * @inode: the object
1317 * @buffer: where they go
1318 * @buffer_size: size of buffer
1319 *
1320 * Returns 0 on success, -EINVAL otherwise
1321 */
1322static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1323 size_t buffer_size)
1324{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001325 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001326
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001327 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001328 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001329
1330 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001331}
1332
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001333/**
1334 * smack_inode_getsecid - Extract inode's security id
1335 * @inode: inode to extract the info from
1336 * @secid: where result will be saved
1337 */
1338static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1339{
1340 struct inode_smack *isp = inode->i_security;
1341
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001342 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001343}
1344
Casey Schauflere114e472008-02-04 22:29:50 -08001345/*
1346 * File Hooks
1347 */
1348
1349/**
1350 * smack_file_permission - Smack check on file operations
1351 * @file: unused
1352 * @mask: unused
1353 *
1354 * Returns 0
1355 *
1356 * Should access checks be done on each read or write?
1357 * UNICOS and SELinux say yes.
1358 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1359 *
1360 * I'll say no for now. Smack does not do the frequent
1361 * label changing that SELinux does.
1362 */
1363static int smack_file_permission(struct file *file, int mask)
1364{
1365 return 0;
1366}
1367
1368/**
1369 * smack_file_alloc_security - assign a file security blob
1370 * @file: the object
1371 *
1372 * The security blob for a file is a pointer to the master
1373 * label list, so no allocation is done.
1374 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001375 * f_security is the owner security information. It
1376 * isn't used on file access checks, it's for send_sigio.
1377 *
Casey Schauflere114e472008-02-04 22:29:50 -08001378 * Returns 0
1379 */
1380static int smack_file_alloc_security(struct file *file)
1381{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001382 struct smack_known *skp = smk_of_current();
1383
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001384 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001385 return 0;
1386}
1387
1388/**
1389 * smack_file_free_security - clear a file security blob
1390 * @file: the object
1391 *
1392 * The security blob for a file is a pointer to the master
1393 * label list, so no memory is freed.
1394 */
1395static void smack_file_free_security(struct file *file)
1396{
1397 file->f_security = NULL;
1398}
1399
1400/**
1401 * smack_file_ioctl - Smack check on ioctls
1402 * @file: the object
1403 * @cmd: what to do
1404 * @arg: unused
1405 *
1406 * Relies heavily on the correct use of the ioctl command conventions.
1407 *
1408 * Returns 0 if allowed, error code otherwise
1409 */
1410static int smack_file_ioctl(struct file *file, unsigned int cmd,
1411 unsigned long arg)
1412{
1413 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001414 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001415 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001416
Eric Parisf48b7392011-04-25 12:54:27 -04001417 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001418 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001419
Casey Schauflerd166c802014-08-27 14:51:27 -07001420 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001421 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001422 rc = smk_bu_file(file, MAY_WRITE, rc);
1423 }
Casey Schauflere114e472008-02-04 22:29:50 -08001424
Casey Schauflerd166c802014-08-27 14:51:27 -07001425 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001426 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001427 rc = smk_bu_file(file, MAY_READ, rc);
1428 }
Casey Schauflere114e472008-02-04 22:29:50 -08001429
1430 return rc;
1431}
1432
1433/**
1434 * smack_file_lock - Smack check on file locking
1435 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001436 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001437 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001438 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001439 */
1440static int smack_file_lock(struct file *file, unsigned int cmd)
1441{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001442 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001443 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001444 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001445
Eric Paris92f42502011-04-25 13:15:55 -04001446 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1447 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001448 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001449 rc = smk_bu_file(file, MAY_LOCK, rc);
1450 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001451}
1452
1453/**
1454 * smack_file_fcntl - Smack check on fcntl
1455 * @file: the object
1456 * @cmd: what action to check
1457 * @arg: unused
1458 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001459 * Generally these operations are harmless.
1460 * File locking operations present an obvious mechanism
1461 * for passing information, so they require write access.
1462 *
Casey Schauflere114e472008-02-04 22:29:50 -08001463 * Returns 0 if current has access, error code otherwise
1464 */
1465static int smack_file_fcntl(struct file *file, unsigned int cmd,
1466 unsigned long arg)
1467{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001468 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001469 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001470 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001471
Casey Schauflere114e472008-02-04 22:29:50 -08001472 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001473 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001474 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001475 case F_SETLK:
1476 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001477 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1478 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001479 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001480 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001481 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001482 case F_SETOWN:
1483 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001484 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1485 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001486 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001487 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001488 break;
1489 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001490 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001491 }
1492
1493 return rc;
1494}
1495
1496/**
Al Viroe5467852012-05-30 13:30:51 -04001497 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001498 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1499 * if mapping anonymous memory.
1500 * @file contains the file structure for file to map (may be NULL).
1501 * @reqprot contains the protection requested by the application.
1502 * @prot contains the protection that will be applied by the kernel.
1503 * @flags contains the operational flags.
1504 * Return 0 if permission is granted.
1505 */
Al Viroe5467852012-05-30 13:30:51 -04001506static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001507 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001508 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001509{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001510 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001511 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001512 struct smack_rule *srp;
1513 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001514 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001515 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001516 int may;
1517 int mmay;
1518 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001519 int rc;
1520
Al Viro496ad9a2013-01-23 17:07:38 -05001521 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001522 return 0;
1523
Al Viro496ad9a2013-01-23 17:07:38 -05001524 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001525 if (isp->smk_mmap == NULL)
1526 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001527 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001528
1529 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001530 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001531 rc = 0;
1532
1533 rcu_read_lock();
1534 /*
1535 * For each Smack rule associated with the subject
1536 * label verify that the SMACK64MMAP also has access
1537 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001538 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001539 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001540 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001541 /*
1542 * Matching labels always allows access.
1543 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001544 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001545 continue;
1546 /*
1547 * If there is a matching local rule take
1548 * that into account as well.
1549 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001550 may = smk_access_entry(srp->smk_subject->smk_known,
1551 okp->smk_known,
1552 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001553 if (may == -ENOENT)
1554 may = srp->smk_access;
1555 else
1556 may &= srp->smk_access;
1557 /*
1558 * If may is zero the SMACK64MMAP subject can't
1559 * possibly have less access.
1560 */
1561 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001562 continue;
1563
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001564 /*
1565 * Fetch the global list entry.
1566 * If there isn't one a SMACK64MMAP subject
1567 * can't have as much access as current.
1568 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001569 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1570 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001571 if (mmay == -ENOENT) {
1572 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001573 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001574 }
1575 /*
1576 * If there is a local entry it modifies the
1577 * potential access, too.
1578 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001579 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1580 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001581 if (tmay != -ENOENT)
1582 mmay &= tmay;
1583
1584 /*
1585 * If there is any access available to current that is
1586 * not available to a SMACK64MMAP subject
1587 * deny access.
1588 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001589 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001590 rc = -EACCES;
1591 break;
1592 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001593 }
1594
1595 rcu_read_unlock();
1596
1597 return rc;
1598}
1599
1600/**
Casey Schauflere114e472008-02-04 22:29:50 -08001601 * smack_file_set_fowner - set the file security blob value
1602 * @file: object in question
1603 *
Casey Schauflere114e472008-02-04 22:29:50 -08001604 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001605static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001606{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001607 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001608}
1609
1610/**
1611 * smack_file_send_sigiotask - Smack on sigio
1612 * @tsk: The target task
1613 * @fown: the object the signal come from
1614 * @signum: unused
1615 *
1616 * Allow a privileged task to get signals even if it shouldn't
1617 *
1618 * Returns 0 if a subject with the object's smack could
1619 * write to the task, an error code otherwise.
1620 */
1621static int smack_file_send_sigiotask(struct task_struct *tsk,
1622 struct fown_struct *fown, int signum)
1623{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001624 struct smack_known *skp;
1625 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001626 struct file *file;
1627 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001628 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001629
1630 /*
1631 * struct fown_struct is never outside the context of a struct file
1632 */
1633 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001634
Etienne Bassetecfcc532009-04-08 20:40:06 +02001635 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001636 skp = file->f_security;
1637 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1638 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001639 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001640 rc = 0;
1641
1642 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1643 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001644 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001645 return rc;
1646}
1647
1648/**
1649 * smack_file_receive - Smack file receive check
1650 * @file: the object
1651 *
1652 * Returns 0 if current has access, error code otherwise
1653 */
1654static int smack_file_receive(struct file *file)
1655{
Casey Schauflerd166c802014-08-27 14:51:27 -07001656 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001657 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001658 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001659 struct inode *inode = file_inode(file);
Casey Schauflere114e472008-02-04 22:29:50 -08001660
Casey Schaufler4482a442013-12-30 17:37:45 -08001661 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001662 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001663 /*
1664 * This code relies on bitmasks.
1665 */
1666 if (file->f_mode & FMODE_READ)
1667 may = MAY_READ;
1668 if (file->f_mode & FMODE_WRITE)
1669 may |= MAY_WRITE;
1670
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001671 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001672 rc = smk_bu_file(file, may, rc);
1673 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001674}
1675
Casey Schaufler531f1d42011-09-19 12:41:42 -07001676/**
Eric Paris83d49852012-04-04 13:45:40 -04001677 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001678 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001679 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001680 *
1681 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001682 * Allow the open only if the task has read access. There are
1683 * many read operations (e.g. fstat) that you can do with an
1684 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001685 *
1686 * Returns 0
1687 */
Eric Paris83d49852012-04-04 13:45:40 -04001688static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001689{
Casey Schauflera6834c02014-04-21 11:10:26 -07001690 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001691 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001692 struct smk_audit_info ad;
1693 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001694
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001695 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001696 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001697
Casey Schauflera6834c02014-04-21 11:10:26 -07001698 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1699 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001700 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001701 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001702
1703 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001704}
1705
Casey Schauflere114e472008-02-04 22:29:50 -08001706/*
1707 * Task hooks
1708 */
1709
1710/**
David Howellsee18d642009-09-02 09:14:21 +01001711 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1712 * @new: the new credentials
1713 * @gfp: the atomicity of any memory allocations
1714 *
1715 * Prepare a blank set of credentials for modification. This must allocate all
1716 * the memory the LSM module might require such that cred_transfer() can
1717 * complete without error.
1718 */
1719static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1720{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001721 struct task_smack *tsp;
1722
1723 tsp = new_task_smack(NULL, NULL, gfp);
1724 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001725 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001726
1727 cred->security = tsp;
1728
David Howellsee18d642009-09-02 09:14:21 +01001729 return 0;
1730}
1731
1732
1733/**
David Howellsf1752ee2008-11-14 10:39:17 +11001734 * smack_cred_free - "free" task-level security credentials
1735 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001736 *
Casey Schauflere114e472008-02-04 22:29:50 -08001737 */
David Howellsf1752ee2008-11-14 10:39:17 +11001738static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001739{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001740 struct task_smack *tsp = cred->security;
1741 struct smack_rule *rp;
1742 struct list_head *l;
1743 struct list_head *n;
1744
1745 if (tsp == NULL)
1746 return;
1747 cred->security = NULL;
1748
1749 list_for_each_safe(l, n, &tsp->smk_rules) {
1750 rp = list_entry(l, struct smack_rule, list);
1751 list_del(&rp->list);
1752 kfree(rp);
1753 }
1754 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001755}
1756
1757/**
David Howellsd84f4f92008-11-14 10:39:23 +11001758 * smack_cred_prepare - prepare new set of credentials for modification
1759 * @new: the new credentials
1760 * @old: the original credentials
1761 * @gfp: the atomicity of any memory allocations
1762 *
1763 * Prepare a new set of credentials for modification.
1764 */
1765static int smack_cred_prepare(struct cred *new, const struct cred *old,
1766 gfp_t gfp)
1767{
Casey Schaufler676dac42010-12-02 06:43:39 -08001768 struct task_smack *old_tsp = old->security;
1769 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001770 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001771
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001772 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001773 if (new_tsp == NULL)
1774 return -ENOMEM;
1775
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001776 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1777 if (rc != 0)
1778 return rc;
1779
Casey Schaufler676dac42010-12-02 06:43:39 -08001780 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001781 return 0;
1782}
1783
Randy Dunlap251a2a92009-02-18 11:42:33 -08001784/**
David Howellsee18d642009-09-02 09:14:21 +01001785 * smack_cred_transfer - Transfer the old credentials to the new credentials
1786 * @new: the new credentials
1787 * @old: the original credentials
1788 *
1789 * Fill in a set of blank credentials from another set of credentials.
1790 */
1791static void smack_cred_transfer(struct cred *new, const struct cred *old)
1792{
Casey Schaufler676dac42010-12-02 06:43:39 -08001793 struct task_smack *old_tsp = old->security;
1794 struct task_smack *new_tsp = new->security;
1795
1796 new_tsp->smk_task = old_tsp->smk_task;
1797 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001798 mutex_init(&new_tsp->smk_rules_lock);
1799 INIT_LIST_HEAD(&new_tsp->smk_rules);
1800
1801
1802 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001803}
1804
1805/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001806 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001807 * @new: points to the set of credentials to be modified.
1808 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001809 *
1810 * Set the security data for a kernel service.
1811 */
1812static int smack_kernel_act_as(struct cred *new, u32 secid)
1813{
Casey Schaufler676dac42010-12-02 06:43:39 -08001814 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001815 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001816
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001817 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001818 return -EINVAL;
1819
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001820 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001821 return 0;
1822}
1823
1824/**
1825 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001826 * @new: points to the set of credentials to be modified
1827 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001828 *
1829 * Set the file creation context in a set of credentials to the same
1830 * as the objective context of the specified inode
1831 */
1832static int smack_kernel_create_files_as(struct cred *new,
1833 struct inode *inode)
1834{
1835 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001836 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001837
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001838 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001839 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001840 return 0;
1841}
1842
1843/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001844 * smk_curacc_on_task - helper to log task related access
1845 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001846 * @access: the access requested
1847 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001848 *
1849 * Return 0 if access is permitted
1850 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001851static int smk_curacc_on_task(struct task_struct *p, int access,
1852 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001853{
1854 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001855 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001856 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001857
Casey Schaufler531f1d42011-09-19 12:41:42 -07001858 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001859 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001860 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001861 rc = smk_bu_task(p, access, rc);
1862 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001863}
1864
1865/**
Casey Schauflere114e472008-02-04 22:29:50 -08001866 * smack_task_setpgid - Smack check on setting pgid
1867 * @p: the task object
1868 * @pgid: unused
1869 *
1870 * Return 0 if write access is permitted
1871 */
1872static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1873{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001874 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001875}
1876
1877/**
1878 * smack_task_getpgid - Smack access check for getpgid
1879 * @p: the object task
1880 *
1881 * Returns 0 if current can read the object task, error code otherwise
1882 */
1883static int smack_task_getpgid(struct task_struct *p)
1884{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001885 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001886}
1887
1888/**
1889 * smack_task_getsid - Smack access check for getsid
1890 * @p: the object task
1891 *
1892 * Returns 0 if current can read the object task, error code otherwise
1893 */
1894static int smack_task_getsid(struct task_struct *p)
1895{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001896 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001897}
1898
1899/**
1900 * smack_task_getsecid - get the secid of the task
1901 * @p: the object task
1902 * @secid: where to put the result
1903 *
1904 * Sets the secid to contain a u32 version of the smack label.
1905 */
1906static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1907{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001908 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001909
1910 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001911}
1912
1913/**
1914 * smack_task_setnice - Smack check on setting nice
1915 * @p: the task object
1916 * @nice: unused
1917 *
1918 * Return 0 if write access is permitted
1919 */
1920static int smack_task_setnice(struct task_struct *p, int nice)
1921{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001922 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001923}
1924
1925/**
1926 * smack_task_setioprio - Smack check on setting ioprio
1927 * @p: the task object
1928 * @ioprio: unused
1929 *
1930 * Return 0 if write access is permitted
1931 */
1932static int smack_task_setioprio(struct task_struct *p, int ioprio)
1933{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001934 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001935}
1936
1937/**
1938 * smack_task_getioprio - Smack check on reading ioprio
1939 * @p: the task object
1940 *
1941 * Return 0 if read access is permitted
1942 */
1943static int smack_task_getioprio(struct task_struct *p)
1944{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001945 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001946}
1947
1948/**
1949 * smack_task_setscheduler - Smack check on setting scheduler
1950 * @p: the task object
1951 * @policy: unused
1952 * @lp: unused
1953 *
1954 * Return 0 if read access is permitted
1955 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001956static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001957{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001958 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001959}
1960
1961/**
1962 * smack_task_getscheduler - Smack check on reading scheduler
1963 * @p: the task object
1964 *
1965 * Return 0 if read access is permitted
1966 */
1967static int smack_task_getscheduler(struct task_struct *p)
1968{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001969 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001970}
1971
1972/**
1973 * smack_task_movememory - Smack check on moving memory
1974 * @p: the task object
1975 *
1976 * Return 0 if write access is permitted
1977 */
1978static int smack_task_movememory(struct task_struct *p)
1979{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001980 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001981}
1982
1983/**
1984 * smack_task_kill - Smack check on signal delivery
1985 * @p: the task object
1986 * @info: unused
1987 * @sig: unused
1988 * @secid: identifies the smack to use in lieu of current's
1989 *
1990 * Return 0 if write access is permitted
1991 *
1992 * The secid behavior is an artifact of an SELinux hack
1993 * in the USB code. Someday it may go away.
1994 */
1995static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1996 int sig, u32 secid)
1997{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001998 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001999 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002000 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002001 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002002
2003 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2004 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002005 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002006 * Sending a signal requires that the sender
2007 * can write the receiver.
2008 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002009 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002010 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002011 rc = smk_bu_task(p, MAY_WRITE, rc);
2012 return rc;
2013 }
Casey Schauflere114e472008-02-04 22:29:50 -08002014 /*
2015 * If the secid isn't 0 we're dealing with some USB IO
2016 * specific behavior. This is not clean. For one thing
2017 * we can't take privilege into account.
2018 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002019 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002020 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2021 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002022 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002023}
2024
2025/**
2026 * smack_task_wait - Smack access check for waiting
2027 * @p: task to wait for
2028 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002029 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002030 */
2031static int smack_task_wait(struct task_struct *p)
2032{
Casey Schauflere114e472008-02-04 22:29:50 -08002033 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002034 * Allow the operation to succeed.
2035 * Zombies are bad.
2036 * In userless environments (e.g. phones) programs
2037 * get marked with SMACK64EXEC and even if the parent
2038 * and child shouldn't be talking the parent still
2039 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002040 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002041 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002042}
2043
2044/**
2045 * smack_task_to_inode - copy task smack into the inode blob
2046 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002047 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002048 *
2049 * Sets the smack pointer in the inode security blob
2050 */
2051static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2052{
2053 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002054 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002055
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002056 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002057}
2058
2059/*
2060 * Socket hooks.
2061 */
2062
2063/**
2064 * smack_sk_alloc_security - Allocate a socket blob
2065 * @sk: the socket
2066 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002067 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002068 *
2069 * Assign Smack pointers to current
2070 *
2071 * Returns 0 on success, -ENOMEM is there's no memory
2072 */
2073static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2074{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002075 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002076 struct socket_smack *ssp;
2077
2078 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2079 if (ssp == NULL)
2080 return -ENOMEM;
2081
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002082 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002083 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002084 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002085
2086 sk->sk_security = ssp;
2087
2088 return 0;
2089}
2090
2091/**
2092 * smack_sk_free_security - Free a socket blob
2093 * @sk: the socket
2094 *
2095 * Clears the blob pointer
2096 */
2097static void smack_sk_free_security(struct sock *sk)
2098{
2099 kfree(sk->sk_security);
2100}
2101
2102/**
Paul Moore07feee82009-03-27 17:10:54 -04002103* smack_host_label - check host based restrictions
2104* @sip: the object end
2105*
2106* looks for host based access restrictions
2107*
2108* This version will only be appropriate for really small sets of single label
2109* hosts. The caller is responsible for ensuring that the RCU read lock is
2110* taken before calling this function.
2111*
2112* Returns the label of the far end or NULL if it's not special.
2113*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002114static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002115{
2116 struct smk_netlbladdr *snp;
2117 struct in_addr *siap = &sip->sin_addr;
2118
2119 if (siap->s_addr == 0)
2120 return NULL;
2121
2122 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2123 /*
2124 * we break after finding the first match because
2125 * the list is sorted from longest to shortest mask
2126 * so we have found the most specific match
2127 */
2128 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002129 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2130 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002131 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002132 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002133 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002134 }
Paul Moore07feee82009-03-27 17:10:54 -04002135
2136 return NULL;
2137}
2138
2139/**
Casey Schauflere114e472008-02-04 22:29:50 -08002140 * smack_netlabel - Set the secattr on a socket
2141 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002142 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002143 *
2144 * Convert the outbound smack value (smk_out) to a
2145 * secattr and attach it to the socket.
2146 *
2147 * Returns 0 on success or an error code
2148 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002149static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002150{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002151 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002152 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002153 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002154
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002155 /*
2156 * Usually the netlabel code will handle changing the
2157 * packet labeling based on the label.
2158 * The case of a single label host is different, because
2159 * a single label host should never get a labeled packet
2160 * even though the label is usually associated with a packet
2161 * label.
2162 */
2163 local_bh_disable();
2164 bh_lock_sock_nested(sk);
2165
2166 if (ssp->smk_out == smack_net_ambient ||
2167 labeled == SMACK_UNLABELED_SOCKET)
2168 netlbl_sock_delattr(sk);
2169 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002170 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002171 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002172 }
2173
2174 bh_unlock_sock(sk);
2175 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002176
Casey Schauflere114e472008-02-04 22:29:50 -08002177 return rc;
2178}
2179
2180/**
Paul Moore07feee82009-03-27 17:10:54 -04002181 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2182 * @sk: the socket
2183 * @sap: the destination address
2184 *
2185 * Set the correct secattr for the given socket based on the destination
2186 * address and perform any outbound access checks needed.
2187 *
2188 * Returns 0 on success or an error code.
2189 *
2190 */
2191static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2192{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002193 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002194 int rc;
2195 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002196 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002197 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002198 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002199
2200 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002201 hkp = smack_host_label(sap);
2202 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002203#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002204 struct lsm_network_audit net;
2205
Eric Paris48c62af2012-04-02 13:15:44 -04002206 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2207 ad.a.u.net->family = sap->sin_family;
2208 ad.a.u.net->dport = sap->sin_port;
2209 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002210#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002211 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002212 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002213 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2214 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002215 } else {
2216 sk_lbl = SMACK_CIPSO_SOCKET;
2217 rc = 0;
2218 }
2219 rcu_read_unlock();
2220 if (rc != 0)
2221 return rc;
2222
2223 return smack_netlabel(sk, sk_lbl);
2224}
2225
Casey Schaufler69f287a2014-12-12 17:08:40 -08002226#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Paul Moore07feee82009-03-27 17:10:54 -04002227/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002228 * smk_ipv6_port_label - Smack port access table management
2229 * @sock: socket
2230 * @address: address
2231 *
2232 * Create or update the port list entry
2233 */
2234static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2235{
2236 struct sock *sk = sock->sk;
2237 struct sockaddr_in6 *addr6;
2238 struct socket_smack *ssp = sock->sk->sk_security;
2239 struct smk_port_label *spp;
2240 unsigned short port = 0;
2241
2242 if (address == NULL) {
2243 /*
2244 * This operation is changing the Smack information
2245 * on the bound socket. Take the changes to the port
2246 * as well.
2247 */
2248 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2249 if (sk != spp->smk_sock)
2250 continue;
2251 spp->smk_in = ssp->smk_in;
2252 spp->smk_out = ssp->smk_out;
2253 return;
2254 }
2255 /*
2256 * A NULL address is only used for updating existing
2257 * bound entries. If there isn't one, it's OK.
2258 */
2259 return;
2260 }
2261
2262 addr6 = (struct sockaddr_in6 *)address;
2263 port = ntohs(addr6->sin6_port);
2264 /*
2265 * This is a special case that is safely ignored.
2266 */
2267 if (port == 0)
2268 return;
2269
2270 /*
2271 * Look for an existing port list entry.
2272 * This is an indication that a port is getting reused.
2273 */
2274 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2275 if (spp->smk_port != port)
2276 continue;
2277 spp->smk_port = port;
2278 spp->smk_sock = sk;
2279 spp->smk_in = ssp->smk_in;
2280 spp->smk_out = ssp->smk_out;
2281 return;
2282 }
2283
2284 /*
2285 * A new port entry is required.
2286 */
2287 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2288 if (spp == NULL)
2289 return;
2290
2291 spp->smk_port = port;
2292 spp->smk_sock = sk;
2293 spp->smk_in = ssp->smk_in;
2294 spp->smk_out = ssp->smk_out;
2295
2296 list_add(&spp->list, &smk_ipv6_port_list);
2297 return;
2298}
2299
2300/**
2301 * smk_ipv6_port_check - check Smack port access
2302 * @sock: socket
2303 * @address: address
2304 *
2305 * Create or update the port list entry
2306 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002307static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002308 int act)
2309{
2310 __be16 *bep;
2311 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002312 struct smk_port_label *spp;
2313 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002314 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002315 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002316 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002317 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002318 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002319#ifdef CONFIG_AUDIT
2320 struct lsm_network_audit net;
2321#endif
2322
2323 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002324 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002325 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002326 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002327 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002328 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002329 }
2330
2331 /*
2332 * Get the IP address and port from the address.
2333 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002334 port = ntohs(address->sin6_port);
2335 bep = (__be16 *)(&address->sin6_addr);
2336 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002337
2338 /*
2339 * It's remote, so port lookup does no good.
2340 */
2341 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2342 goto auditout;
2343
2344 /*
2345 * It's local so the send check has to have passed.
2346 */
2347 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002348 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002349 goto auditout;
2350 }
2351
2352 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2353 if (spp->smk_port != port)
2354 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002355 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002356 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002357 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002358 break;
2359 }
2360
2361auditout:
2362
2363#ifdef CONFIG_AUDIT
2364 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2365 ad.a.u.net->family = sk->sk_family;
2366 ad.a.u.net->dport = port;
2367 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002368 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002369 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002370 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002371#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002372 rc = smk_access(skp, object, MAY_WRITE, &ad);
2373 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2374 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002375}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002376#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002377
2378/**
Casey Schauflere114e472008-02-04 22:29:50 -08002379 * smack_inode_setsecurity - set smack xattrs
2380 * @inode: the object
2381 * @name: attribute name
2382 * @value: attribute value
2383 * @size: size of the attribute
2384 * @flags: unused
2385 *
2386 * Sets the named attribute in the appropriate blob
2387 *
2388 * Returns 0 on success, or an error code
2389 */
2390static int smack_inode_setsecurity(struct inode *inode, const char *name,
2391 const void *value, size_t size, int flags)
2392{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002393 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002394 struct inode_smack *nsp = inode->i_security;
2395 struct socket_smack *ssp;
2396 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002397 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002398
Casey Schauflerf7112e62012-05-06 15:22:02 -07002399 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302400 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002401
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002402 skp = smk_import_entry(value, size);
2403 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002404 return -EINVAL;
2405
2406 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002407 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002408 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002409 return 0;
2410 }
2411 /*
2412 * The rest of the Smack xattrs are only on sockets.
2413 */
2414 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2415 return -EOPNOTSUPP;
2416
2417 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002418 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002419 return -EOPNOTSUPP;
2420
2421 ssp = sock->sk->sk_security;
2422
2423 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002424 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002425 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002426 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002427 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002428 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2429 if (rc != 0)
2430 printk(KERN_WARNING
2431 "Smack: \"%s\" netlbl error %d.\n",
2432 __func__, -rc);
2433 }
Casey Schauflere114e472008-02-04 22:29:50 -08002434 } else
2435 return -EOPNOTSUPP;
2436
Casey Schaufler69f287a2014-12-12 17:08:40 -08002437#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07002438 if (sock->sk->sk_family == PF_INET6)
2439 smk_ipv6_port_label(sock, NULL);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002440#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002441
Casey Schauflere114e472008-02-04 22:29:50 -08002442 return 0;
2443}
2444
2445/**
2446 * smack_socket_post_create - finish socket setup
2447 * @sock: the socket
2448 * @family: protocol family
2449 * @type: unused
2450 * @protocol: unused
2451 * @kern: unused
2452 *
2453 * Sets the netlabel information on the socket
2454 *
2455 * Returns 0 on success, and error code otherwise
2456 */
2457static int smack_socket_post_create(struct socket *sock, int family,
2458 int type, int protocol, int kern)
2459{
Marcin Lis74123012015-01-22 15:40:33 +01002460 struct socket_smack *ssp;
2461
2462 if (sock->sk == NULL)
2463 return 0;
2464
2465 /*
2466 * Sockets created by kernel threads receive web label.
2467 */
2468 if (unlikely(current->flags & PF_KTHREAD)) {
2469 ssp = sock->sk->sk_security;
2470 ssp->smk_in = &smack_known_web;
2471 ssp->smk_out = &smack_known_web;
2472 }
2473
2474 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002475 return 0;
2476 /*
2477 * Set the outbound netlbl.
2478 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002479 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2480}
2481
Casey Schaufler69f287a2014-12-12 17:08:40 -08002482#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002483/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002484 * smack_socket_bind - record port binding information.
2485 * @sock: the socket
2486 * @address: the port address
2487 * @addrlen: size of the address
2488 *
2489 * Records the label bound to a port.
2490 *
2491 * Returns 0
2492 */
2493static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2494 int addrlen)
2495{
Casey Schaufler69f287a2014-12-12 17:08:40 -08002496#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002497 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2498 smk_ipv6_port_label(sock, address);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002499#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002500
2501 return 0;
2502}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002503#endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002504
2505/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002506 * smack_socket_connect - connect access check
2507 * @sock: the socket
2508 * @sap: the other end
2509 * @addrlen: size of sap
2510 *
2511 * Verifies that a connection may be possible
2512 *
2513 * Returns 0 on success, and error code otherwise
2514 */
2515static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2516 int addrlen)
2517{
Casey Schauflerc6739442013-05-22 18:42:56 -07002518 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002519
Casey Schauflerc6739442013-05-22 18:42:56 -07002520 if (sock->sk == NULL)
2521 return 0;
2522
2523 switch (sock->sk->sk_family) {
2524 case PF_INET:
2525 if (addrlen < sizeof(struct sockaddr_in))
2526 return -EINVAL;
2527 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2528 break;
2529 case PF_INET6:
2530 if (addrlen < sizeof(struct sockaddr_in6))
2531 return -EINVAL;
Casey Schaufler69f287a2014-12-12 17:08:40 -08002532#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002533 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2534 SMK_CONNECTING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002535#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002536 break;
2537 }
2538 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002539}
2540
2541/**
2542 * smack_flags_to_may - convert S_ to MAY_ values
2543 * @flags: the S_ value
2544 *
2545 * Returns the equivalent MAY_ value
2546 */
2547static int smack_flags_to_may(int flags)
2548{
2549 int may = 0;
2550
2551 if (flags & S_IRUGO)
2552 may |= MAY_READ;
2553 if (flags & S_IWUGO)
2554 may |= MAY_WRITE;
2555 if (flags & S_IXUGO)
2556 may |= MAY_EXEC;
2557
2558 return may;
2559}
2560
2561/**
2562 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2563 * @msg: the object
2564 *
2565 * Returns 0
2566 */
2567static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2568{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002569 struct smack_known *skp = smk_of_current();
2570
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002571 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002572 return 0;
2573}
2574
2575/**
2576 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2577 * @msg: the object
2578 *
2579 * Clears the blob pointer
2580 */
2581static void smack_msg_msg_free_security(struct msg_msg *msg)
2582{
2583 msg->security = NULL;
2584}
2585
2586/**
2587 * smack_of_shm - the smack pointer for the shm
2588 * @shp: the object
2589 *
2590 * Returns a pointer to the smack value
2591 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002592static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002593{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002594 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002595}
2596
2597/**
2598 * smack_shm_alloc_security - Set the security blob for shm
2599 * @shp: the object
2600 *
2601 * Returns 0
2602 */
2603static int smack_shm_alloc_security(struct shmid_kernel *shp)
2604{
2605 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002606 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002607
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002608 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002609 return 0;
2610}
2611
2612/**
2613 * smack_shm_free_security - Clear the security blob for shm
2614 * @shp: the object
2615 *
2616 * Clears the blob pointer
2617 */
2618static void smack_shm_free_security(struct shmid_kernel *shp)
2619{
2620 struct kern_ipc_perm *isp = &shp->shm_perm;
2621
2622 isp->security = NULL;
2623}
2624
2625/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002626 * smk_curacc_shm : check if current has access on shm
2627 * @shp : the object
2628 * @access : access requested
2629 *
2630 * Returns 0 if current has the requested access, error code otherwise
2631 */
2632static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2633{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002634 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002635 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002636 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002637
2638#ifdef CONFIG_AUDIT
2639 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2640 ad.a.u.ipc_id = shp->shm_perm.id;
2641#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002642 rc = smk_curacc(ssp, access, &ad);
2643 rc = smk_bu_current("shm", ssp, access, rc);
2644 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002645}
2646
2647/**
Casey Schauflere114e472008-02-04 22:29:50 -08002648 * smack_shm_associate - Smack access check for shm
2649 * @shp: the object
2650 * @shmflg: access requested
2651 *
2652 * Returns 0 if current has the requested access, error code otherwise
2653 */
2654static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2655{
Casey Schauflere114e472008-02-04 22:29:50 -08002656 int may;
2657
2658 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002659 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002660}
2661
2662/**
2663 * smack_shm_shmctl - Smack access check for shm
2664 * @shp: the object
2665 * @cmd: what it wants to do
2666 *
2667 * Returns 0 if current has the requested access, error code otherwise
2668 */
2669static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2670{
Casey Schauflere114e472008-02-04 22:29:50 -08002671 int may;
2672
2673 switch (cmd) {
2674 case IPC_STAT:
2675 case SHM_STAT:
2676 may = MAY_READ;
2677 break;
2678 case IPC_SET:
2679 case SHM_LOCK:
2680 case SHM_UNLOCK:
2681 case IPC_RMID:
2682 may = MAY_READWRITE;
2683 break;
2684 case IPC_INFO:
2685 case SHM_INFO:
2686 /*
2687 * System level information.
2688 */
2689 return 0;
2690 default:
2691 return -EINVAL;
2692 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002693 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002694}
2695
2696/**
2697 * smack_shm_shmat - Smack access for shmat
2698 * @shp: the object
2699 * @shmaddr: unused
2700 * @shmflg: access requested
2701 *
2702 * Returns 0 if current has the requested access, error code otherwise
2703 */
2704static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2705 int shmflg)
2706{
Casey Schauflere114e472008-02-04 22:29:50 -08002707 int may;
2708
2709 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002710 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002711}
2712
2713/**
2714 * smack_of_sem - the smack pointer for the sem
2715 * @sma: the object
2716 *
2717 * Returns a pointer to the smack value
2718 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002719static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002720{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002721 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002722}
2723
2724/**
2725 * smack_sem_alloc_security - Set the security blob for sem
2726 * @sma: the object
2727 *
2728 * Returns 0
2729 */
2730static int smack_sem_alloc_security(struct sem_array *sma)
2731{
2732 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002733 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002734
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002735 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002736 return 0;
2737}
2738
2739/**
2740 * smack_sem_free_security - Clear the security blob for sem
2741 * @sma: the object
2742 *
2743 * Clears the blob pointer
2744 */
2745static void smack_sem_free_security(struct sem_array *sma)
2746{
2747 struct kern_ipc_perm *isp = &sma->sem_perm;
2748
2749 isp->security = NULL;
2750}
2751
2752/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002753 * smk_curacc_sem : check if current has access on sem
2754 * @sma : the object
2755 * @access : access requested
2756 *
2757 * Returns 0 if current has the requested access, error code otherwise
2758 */
2759static int smk_curacc_sem(struct sem_array *sma, int access)
2760{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002761 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002762 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002763 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002764
2765#ifdef CONFIG_AUDIT
2766 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2767 ad.a.u.ipc_id = sma->sem_perm.id;
2768#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002769 rc = smk_curacc(ssp, access, &ad);
2770 rc = smk_bu_current("sem", ssp, access, rc);
2771 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002772}
2773
2774/**
Casey Schauflere114e472008-02-04 22:29:50 -08002775 * smack_sem_associate - Smack access check for sem
2776 * @sma: the object
2777 * @semflg: access requested
2778 *
2779 * Returns 0 if current has the requested access, error code otherwise
2780 */
2781static int smack_sem_associate(struct sem_array *sma, int semflg)
2782{
Casey Schauflere114e472008-02-04 22:29:50 -08002783 int may;
2784
2785 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002786 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002787}
2788
2789/**
2790 * smack_sem_shmctl - Smack access check for sem
2791 * @sma: the object
2792 * @cmd: what it wants to do
2793 *
2794 * Returns 0 if current has the requested access, error code otherwise
2795 */
2796static int smack_sem_semctl(struct sem_array *sma, int cmd)
2797{
Casey Schauflere114e472008-02-04 22:29:50 -08002798 int may;
2799
2800 switch (cmd) {
2801 case GETPID:
2802 case GETNCNT:
2803 case GETZCNT:
2804 case GETVAL:
2805 case GETALL:
2806 case IPC_STAT:
2807 case SEM_STAT:
2808 may = MAY_READ;
2809 break;
2810 case SETVAL:
2811 case SETALL:
2812 case IPC_RMID:
2813 case IPC_SET:
2814 may = MAY_READWRITE;
2815 break;
2816 case IPC_INFO:
2817 case SEM_INFO:
2818 /*
2819 * System level information
2820 */
2821 return 0;
2822 default:
2823 return -EINVAL;
2824 }
2825
Etienne Bassetecfcc532009-04-08 20:40:06 +02002826 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002827}
2828
2829/**
2830 * smack_sem_semop - Smack checks of semaphore operations
2831 * @sma: the object
2832 * @sops: unused
2833 * @nsops: unused
2834 * @alter: unused
2835 *
2836 * Treated as read and write in all cases.
2837 *
2838 * Returns 0 if access is allowed, error code otherwise
2839 */
2840static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2841 unsigned nsops, int alter)
2842{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002843 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002844}
2845
2846/**
2847 * smack_msg_alloc_security - Set the security blob for msg
2848 * @msq: the object
2849 *
2850 * Returns 0
2851 */
2852static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2853{
2854 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002855 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002856
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002857 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002858 return 0;
2859}
2860
2861/**
2862 * smack_msg_free_security - Clear the security blob for msg
2863 * @msq: the object
2864 *
2865 * Clears the blob pointer
2866 */
2867static void smack_msg_queue_free_security(struct msg_queue *msq)
2868{
2869 struct kern_ipc_perm *kisp = &msq->q_perm;
2870
2871 kisp->security = NULL;
2872}
2873
2874/**
2875 * smack_of_msq - the smack pointer for the msq
2876 * @msq: the object
2877 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002878 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002879 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002880static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002881{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002882 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002883}
2884
2885/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002886 * smk_curacc_msq : helper to check if current has access on msq
2887 * @msq : the msq
2888 * @access : access requested
2889 *
2890 * return 0 if current has access, error otherwise
2891 */
2892static int smk_curacc_msq(struct msg_queue *msq, int access)
2893{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002894 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002895 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002896 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002897
2898#ifdef CONFIG_AUDIT
2899 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2900 ad.a.u.ipc_id = msq->q_perm.id;
2901#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002902 rc = smk_curacc(msp, access, &ad);
2903 rc = smk_bu_current("msq", msp, access, rc);
2904 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002905}
2906
2907/**
Casey Schauflere114e472008-02-04 22:29:50 -08002908 * smack_msg_queue_associate - Smack access check for msg_queue
2909 * @msq: the object
2910 * @msqflg: access requested
2911 *
2912 * Returns 0 if current has the requested access, error code otherwise
2913 */
2914static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2915{
Casey Schauflere114e472008-02-04 22:29:50 -08002916 int may;
2917
2918 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002919 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002920}
2921
2922/**
2923 * smack_msg_queue_msgctl - Smack access check for msg_queue
2924 * @msq: the object
2925 * @cmd: what it wants to do
2926 *
2927 * Returns 0 if current has the requested access, error code otherwise
2928 */
2929static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2930{
Casey Schauflere114e472008-02-04 22:29:50 -08002931 int may;
2932
2933 switch (cmd) {
2934 case IPC_STAT:
2935 case MSG_STAT:
2936 may = MAY_READ;
2937 break;
2938 case IPC_SET:
2939 case IPC_RMID:
2940 may = MAY_READWRITE;
2941 break;
2942 case IPC_INFO:
2943 case MSG_INFO:
2944 /*
2945 * System level information
2946 */
2947 return 0;
2948 default:
2949 return -EINVAL;
2950 }
2951
Etienne Bassetecfcc532009-04-08 20:40:06 +02002952 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002953}
2954
2955/**
2956 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2957 * @msq: the object
2958 * @msg: unused
2959 * @msqflg: access requested
2960 *
2961 * Returns 0 if current has the requested access, error code otherwise
2962 */
2963static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2964 int msqflg)
2965{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002966 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002967
Etienne Bassetecfcc532009-04-08 20:40:06 +02002968 may = smack_flags_to_may(msqflg);
2969 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002970}
2971
2972/**
2973 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2974 * @msq: the object
2975 * @msg: unused
2976 * @target: unused
2977 * @type: unused
2978 * @mode: unused
2979 *
2980 * Returns 0 if current has read and write access, error code otherwise
2981 */
2982static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2983 struct task_struct *target, long type, int mode)
2984{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002985 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002986}
2987
2988/**
2989 * smack_ipc_permission - Smack access for ipc_permission()
2990 * @ipp: the object permissions
2991 * @flag: access requested
2992 *
2993 * Returns 0 if current has read and write access, error code otherwise
2994 */
2995static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2996{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002997 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002998 int may = smack_flags_to_may(flag);
2999 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003000 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003001
Etienne Bassetecfcc532009-04-08 20:40:06 +02003002#ifdef CONFIG_AUDIT
3003 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3004 ad.a.u.ipc_id = ipp->id;
3005#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003006 rc = smk_curacc(iskp, may, &ad);
3007 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003008 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003009}
3010
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003011/**
3012 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003013 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003014 * @secid: where result will be saved
3015 */
3016static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3017{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003018 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003019
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003020 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003021}
3022
Casey Schauflere114e472008-02-04 22:29:50 -08003023/**
3024 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003025 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003026 * @inode: the object
3027 *
3028 * Set the inode's security blob if it hasn't been done already.
3029 */
3030static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3031{
3032 struct super_block *sbp;
3033 struct superblock_smack *sbsp;
3034 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003035 struct smack_known *skp;
3036 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003037 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003038 char trattr[TRANS_TRUE_SIZE];
3039 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003040 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003041 struct dentry *dp;
3042
3043 if (inode == NULL)
3044 return;
3045
3046 isp = inode->i_security;
3047
3048 mutex_lock(&isp->smk_lock);
3049 /*
3050 * If the inode is already instantiated
3051 * take the quick way out
3052 */
3053 if (isp->smk_flags & SMK_INODE_INSTANT)
3054 goto unlockandout;
3055
3056 sbp = inode->i_sb;
3057 sbsp = sbp->s_security;
3058 /*
3059 * We're going to use the superblock default label
3060 * if there's no label on the file.
3061 */
3062 final = sbsp->smk_default;
3063
3064 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003065 * If this is the root inode the superblock
3066 * may be in the process of initialization.
3067 * If that is the case use the root value out
3068 * of the superblock.
3069 */
3070 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003071 switch (sbp->s_magic) {
3072 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003073 /*
3074 * The cgroup filesystem is never mounted,
3075 * so there's no opportunity to set the mount
3076 * options.
3077 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003078 sbsp->smk_root = &smack_known_star;
3079 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003080 isp->smk_inode = sbsp->smk_root;
3081 break;
3082 case TMPFS_MAGIC:
3083 /*
3084 * What about shmem/tmpfs anonymous files with dentry
3085 * obtained from d_alloc_pseudo()?
3086 */
3087 isp->smk_inode = smk_of_current();
3088 break;
3089 default:
3090 isp->smk_inode = sbsp->smk_root;
3091 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003092 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003093 isp->smk_flags |= SMK_INODE_INSTANT;
3094 goto unlockandout;
3095 }
3096
3097 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003098 * This is pretty hackish.
3099 * Casey says that we shouldn't have to do
3100 * file system specific code, but it does help
3101 * with keeping it simple.
3102 */
3103 switch (sbp->s_magic) {
3104 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003105 case PIPEFS_MAGIC:
3106 case SOCKFS_MAGIC:
3107 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003108 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003109 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003110 * that the smack file system doesn't do
3111 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003112 *
Casey Schauflere114e472008-02-04 22:29:50 -08003113 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003114 *
3115 * Socket access is controlled by the socket
3116 * structures associated with the task involved.
3117 *
3118 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003119 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003120 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003121 break;
3122 case DEVPTS_SUPER_MAGIC:
3123 /*
3124 * devpts seems content with the label of the task.
3125 * Programs that change smack have to treat the
3126 * pty with respect.
3127 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003128 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003129 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003130 case PROC_SUPER_MAGIC:
3131 /*
3132 * Casey says procfs appears not to care.
3133 * The superblock default suffices.
3134 */
3135 break;
3136 case TMPFS_MAGIC:
3137 /*
3138 * Device labels should come from the filesystem,
3139 * but watch out, because they're volitile,
3140 * getting recreated on every reboot.
3141 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003142 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003143 /*
3144 * No break.
3145 *
3146 * If a smack value has been set we want to use it,
3147 * but since tmpfs isn't giving us the opportunity
3148 * to set mount options simulate setting the
3149 * superblock default.
3150 */
3151 default:
3152 /*
3153 * This isn't an understood special case.
3154 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003155 */
3156
3157 /*
3158 * UNIX domain sockets use lower level socket data.
3159 */
3160 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003161 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003162 break;
3163 }
3164 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003165 * No xattr support means, alas, no SMACK label.
3166 * Use the aforeapplied default.
3167 * It would be curious if the label of the task
3168 * does not match that assigned.
3169 */
3170 if (inode->i_op->getxattr == NULL)
3171 break;
3172 /*
3173 * Get the dentry for xattr.
3174 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003175 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003176 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3177 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003178 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003179
3180 /*
3181 * Transmuting directory
3182 */
3183 if (S_ISDIR(inode->i_mode)) {
3184 /*
3185 * If this is a new directory and the label was
3186 * transmuted when the inode was initialized
3187 * set the transmute attribute on the directory
3188 * and mark the inode.
3189 *
3190 * If there is a transmute attribute on the
3191 * directory mark the inode.
3192 */
3193 if (isp->smk_flags & SMK_INODE_CHANGED) {
3194 isp->smk_flags &= ~SMK_INODE_CHANGED;
3195 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003196 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003197 TRANS_TRUE, TRANS_TRUE_SIZE,
3198 0);
3199 } else {
3200 rc = inode->i_op->getxattr(dp,
3201 XATTR_NAME_SMACKTRANSMUTE, trattr,
3202 TRANS_TRUE_SIZE);
3203 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3204 TRANS_TRUE_SIZE) != 0)
3205 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003206 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003207 if (rc >= 0)
3208 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003209 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003210 /*
3211 * Don't let the exec or mmap label be "*" or "@".
3212 */
3213 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3214 if (skp == &smack_known_star || skp == &smack_known_web)
3215 skp = NULL;
3216 isp->smk_task = skp;
3217 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3218 if (skp == &smack_known_star || skp == &smack_known_web)
3219 skp = NULL;
3220 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003221
Casey Schauflere114e472008-02-04 22:29:50 -08003222 dput(dp);
3223 break;
3224 }
3225
3226 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003227 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003228 else
3229 isp->smk_inode = final;
3230
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003231 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003232
3233unlockandout:
3234 mutex_unlock(&isp->smk_lock);
3235 return;
3236}
3237
3238/**
3239 * smack_getprocattr - Smack process attribute access
3240 * @p: the object task
3241 * @name: the name of the attribute in /proc/.../attr
3242 * @value: where to put the result
3243 *
3244 * Places a copy of the task Smack into value
3245 *
3246 * Returns the length of the smack label or an error code
3247 */
3248static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3249{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003250 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003251 char *cp;
3252 int slen;
3253
3254 if (strcmp(name, "current") != 0)
3255 return -EINVAL;
3256
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003257 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003258 if (cp == NULL)
3259 return -ENOMEM;
3260
3261 slen = strlen(cp);
3262 *value = cp;
3263 return slen;
3264}
3265
3266/**
3267 * smack_setprocattr - Smack process attribute setting
3268 * @p: the object task
3269 * @name: the name of the attribute in /proc/.../attr
3270 * @value: the value to set
3271 * @size: the size of the value
3272 *
3273 * Sets the Smack value of the task. Only setting self
3274 * is permitted and only with privilege
3275 *
3276 * Returns the length of the smack label or an error code
3277 */
3278static int smack_setprocattr(struct task_struct *p, char *name,
3279 void *value, size_t size)
3280{
Casey Schaufler676dac42010-12-02 06:43:39 -08003281 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003282 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003283 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003284
Casey Schauflere114e472008-02-04 22:29:50 -08003285 /*
3286 * Changing another process' Smack value is too dangerous
3287 * and supports no sane use case.
3288 */
3289 if (p != current)
3290 return -EPERM;
3291
Casey Schaufler1880eff2012-06-05 15:28:30 -07003292 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003293 return -EPERM;
3294
Casey Schauflerf7112e62012-05-06 15:22:02 -07003295 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003296 return -EINVAL;
3297
3298 if (strcmp(name, "current") != 0)
3299 return -EINVAL;
3300
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003301 skp = smk_import_entry(value, size);
3302 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003303 return -EINVAL;
3304
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003305 /*
3306 * No process is ever allowed the web ("@") label.
3307 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003308 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003309 return -EPERM;
3310
David Howellsd84f4f92008-11-14 10:39:23 +11003311 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003312 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003313 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003314
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003315 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003316 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003317
David Howellsd84f4f92008-11-14 10:39:23 +11003318 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003319 return size;
3320}
3321
3322/**
3323 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003324 * @sock: one sock
3325 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003326 * @newsk: unused
3327 *
3328 * Return 0 if a subject with the smack of sock could access
3329 * an object with the smack of other, otherwise an error code
3330 */
David S. Miller3610cda2011-01-05 15:38:53 -08003331static int smack_unix_stream_connect(struct sock *sock,
3332 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003333{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003334 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003335 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003336 struct socket_smack *ssp = sock->sk_security;
3337 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003338 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003339 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003340 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003341#ifdef CONFIG_AUDIT
3342 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003343#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003344
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003345 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3346 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003347 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003348#ifdef CONFIG_AUDIT
3349 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3350 smk_ad_setfield_u_net_sk(&ad, other);
3351#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003352 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3353 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003354 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003355 okp = osp->smk_out;
3356 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003357 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003358 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003359 MAY_WRITE, rc);
3360 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003361 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003362
Casey Schaufler975d5e52011-09-26 14:43:39 -07003363 /*
3364 * Cross reference the peer labels for SO_PEERSEC.
3365 */
3366 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003367 nsp->smk_packet = ssp->smk_out;
3368 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003369 }
3370
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003371 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003372}
3373
3374/**
3375 * smack_unix_may_send - Smack access on UDS
3376 * @sock: one socket
3377 * @other: the other socket
3378 *
3379 * Return 0 if a subject with the smack of sock could access
3380 * an object with the smack of other, otherwise an error code
3381 */
3382static int smack_unix_may_send(struct socket *sock, struct socket *other)
3383{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003384 struct socket_smack *ssp = sock->sk->sk_security;
3385 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003386 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003387 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003388
Kees Cook923e9a12012-04-10 13:26:44 -07003389#ifdef CONFIG_AUDIT
3390 struct lsm_network_audit net;
3391
Eric Paris48c62af2012-04-02 13:15:44 -04003392 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003393 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003394#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003395
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003396 if (smack_privileged(CAP_MAC_OVERRIDE))
3397 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003398
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003399 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3400 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003401 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003402}
3403
3404/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003405 * smack_socket_sendmsg - Smack check based on destination host
3406 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003407 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003408 * @size: the size of the message
3409 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003410 * Return 0 if the current subject can write to the destination host.
3411 * For IPv4 this is only a question if the destination is a single label host.
3412 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003413 */
3414static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3415 int size)
3416{
3417 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003418#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003419 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003420#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003421 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003422
3423 /*
3424 * Perfectly reasonable for this to be NULL
3425 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003426 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003427 return 0;
3428
Casey Schauflerc6739442013-05-22 18:42:56 -07003429 switch (sip->sin_family) {
3430 case AF_INET:
3431 rc = smack_netlabel_send(sock->sk, sip);
3432 break;
3433 case AF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003434#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07003435 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003436#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003437 break;
3438 }
3439 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003440}
3441
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003442/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003443 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003444 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003445 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003446 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003447 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003448 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003449static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3450 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003451{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003452 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003453 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003454 int acat;
3455 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003456
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003457 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003458 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003459 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003460 * If there are flags but no level netlabel isn't
3461 * behaving the way we expect it to.
3462 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003463 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003464 * Without guidance regarding the smack value
3465 * for the packet fall back on the network
3466 * ambient value.
3467 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003468 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003469 list_for_each_entry(skp, &smack_known_list, list) {
3470 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003471 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003472 /*
3473 * Compare the catsets. Use the netlbl APIs.
3474 */
3475 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3476 if ((skp->smk_netlabel.flags &
3477 NETLBL_SECATTR_MLS_CAT) == 0)
3478 found = 1;
3479 break;
3480 }
3481 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003482 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3483 acat + 1);
3484 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003485 skp->smk_netlabel.attr.mls.cat,
3486 kcat + 1);
3487 if (acat < 0 || kcat < 0)
3488 break;
3489 }
3490 if (acat == kcat) {
3491 found = 1;
3492 break;
3493 }
Casey Schauflere114e472008-02-04 22:29:50 -08003494 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003495 rcu_read_unlock();
3496
3497 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003498 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003499
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003500 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003501 return &smack_known_web;
3502 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003503 }
3504 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3505 /*
3506 * Looks like a fallback, which gives us a secid.
3507 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003508 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003509 /*
3510 * This has got to be a bug because it is
3511 * impossible to specify a fallback without
3512 * specifying the label, which will ensure
3513 * it has a secid, and the only way to get a
3514 * secid is from a fallback.
3515 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003516 BUG_ON(skp == NULL);
3517 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003518 }
3519 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003520 * Without guidance regarding the smack value
3521 * for the packet fall back on the network
3522 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003523 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003524 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003525}
3526
Casey Schaufler69f287a2014-12-12 17:08:40 -08003527#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003528static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003529{
Casey Schauflerc6739442013-05-22 18:42:56 -07003530 u8 nexthdr;
3531 int offset;
3532 int proto = -EINVAL;
3533 struct ipv6hdr _ipv6h;
3534 struct ipv6hdr *ip6;
3535 __be16 frag_off;
3536 struct tcphdr _tcph, *th;
3537 struct udphdr _udph, *uh;
3538 struct dccp_hdr _dccph, *dh;
3539
3540 sip->sin6_port = 0;
3541
3542 offset = skb_network_offset(skb);
3543 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3544 if (ip6 == NULL)
3545 return -EINVAL;
3546 sip->sin6_addr = ip6->saddr;
3547
3548 nexthdr = ip6->nexthdr;
3549 offset += sizeof(_ipv6h);
3550 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3551 if (offset < 0)
3552 return -EINVAL;
3553
3554 proto = nexthdr;
3555 switch (proto) {
3556 case IPPROTO_TCP:
3557 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3558 if (th != NULL)
3559 sip->sin6_port = th->source;
3560 break;
3561 case IPPROTO_UDP:
3562 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3563 if (uh != NULL)
3564 sip->sin6_port = uh->source;
3565 break;
3566 case IPPROTO_DCCP:
3567 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3568 if (dh != NULL)
3569 sip->sin6_port = dh->dccph_sport;
3570 break;
3571 }
3572 return proto;
3573}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003574#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003575
Casey Schauflere114e472008-02-04 22:29:50 -08003576/**
3577 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3578 * @sk: socket
3579 * @skb: packet
3580 *
3581 * Returns 0 if the packet should be delivered, an error code otherwise
3582 */
3583static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3584{
3585 struct netlbl_lsm_secattr secattr;
3586 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003587 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003588 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003589 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003590#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003591 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003592#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003593#if IS_ENABLED(CONFIG_IPV6)
3594 struct sockaddr_in6 sadd;
3595 int proto;
3596#endif /* CONFIG_IPV6 */
3597
Casey Schauflerc6739442013-05-22 18:42:56 -07003598 switch (sk->sk_family) {
3599 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003600#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3601 /*
3602 * If there is a secmark use it rather than the CIPSO label.
3603 * If there is no secmark fall back to CIPSO.
3604 * The secmark is assumed to reflect policy better.
3605 */
3606 if (skb && skb->secmark != 0) {
3607 skp = smack_from_secid(skb->secmark);
3608 goto access_check;
3609 }
3610#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003611 /*
3612 * Translate what netlabel gave us.
3613 */
3614 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003615
Casey Schauflerc6739442013-05-22 18:42:56 -07003616 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3617 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003618 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003619 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003620 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003621
Casey Schauflerc6739442013-05-22 18:42:56 -07003622 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003623
Casey Schaufler69f287a2014-12-12 17:08:40 -08003624#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3625access_check:
3626#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003627#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003628 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3629 ad.a.u.net->family = sk->sk_family;
3630 ad.a.u.net->netif = skb->skb_iif;
3631 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003632#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003633 /*
3634 * Receiving a packet requires that the other end
3635 * be able to write here. Read access is not required.
3636 * This is the simplist possible security model
3637 * for networking.
3638 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003639 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3640 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003641 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003642 if (rc != 0)
3643 netlbl_skbuff_err(skb, rc, 0);
3644 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003645#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003646 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003647 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3648 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3649 break;
3650#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3651 if (skb && skb->secmark != 0)
3652 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003653 else
Casey Schaufler69f287a2014-12-12 17:08:40 -08003654 skp = smack_net_ambient;
3655#ifdef CONFIG_AUDIT
3656 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3657 ad.a.u.net->family = sk->sk_family;
3658 ad.a.u.net->netif = skb->skb_iif;
3659 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3660#endif /* CONFIG_AUDIT */
3661 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3662 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3663 MAY_WRITE, rc);
3664#else /* CONFIG_SECURITY_SMACK_NETFILTER */
3665 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3666#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003667 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003668#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003669 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003670
Paul Moorea8134292008-10-10 10:16:31 -04003671 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003672}
3673
3674/**
3675 * smack_socket_getpeersec_stream - pull in packet label
3676 * @sock: the socket
3677 * @optval: user's destination
3678 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003679 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003680 *
3681 * returns zero on success, an error code otherwise
3682 */
3683static int smack_socket_getpeersec_stream(struct socket *sock,
3684 char __user *optval,
3685 int __user *optlen, unsigned len)
3686{
3687 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003688 char *rcp = "";
3689 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003690 int rc = 0;
3691
3692 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003693 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003694 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003695 slen = strlen(rcp) + 1;
3696 }
Casey Schauflere114e472008-02-04 22:29:50 -08003697
3698 if (slen > len)
3699 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003700 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003701 rc = -EFAULT;
3702
3703 if (put_user(slen, optlen) != 0)
3704 rc = -EFAULT;
3705
3706 return rc;
3707}
3708
3709
3710/**
3711 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003712 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003713 * @skb: packet data
3714 * @secid: pointer to where to put the secid of the packet
3715 *
3716 * Sets the netlabel socket state on sk from parent
3717 */
3718static int smack_socket_getpeersec_dgram(struct socket *sock,
3719 struct sk_buff *skb, u32 *secid)
3720
3721{
3722 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003723 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003724 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003725 int family = PF_UNSPEC;
3726 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003727 int rc;
3728
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003729 if (skb != NULL) {
3730 if (skb->protocol == htons(ETH_P_IP))
3731 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003732#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003733 else if (skb->protocol == htons(ETH_P_IPV6))
3734 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003735#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003736 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003737 if (family == PF_UNSPEC && sock != NULL)
3738 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003739
Casey Schaufler69f287a2014-12-12 17:08:40 -08003740 switch (family) {
3741 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003742 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003743 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003744 break;
3745 case PF_INET:
3746#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3747 s = skb->secmark;
3748 if (s != 0)
3749 break;
3750#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003751 /*
3752 * Translate what netlabel gave us.
3753 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003754 if (sock != NULL && sock->sk != NULL)
3755 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003756 netlbl_secattr_init(&secattr);
3757 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3758 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003759 skp = smack_from_secattr(&secattr, ssp);
3760 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003761 }
3762 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003763 break;
3764#if IS_ENABLED(CONFIG_IPV6)
3765 case PF_INET6:
3766#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3767 s = skb->secmark;
3768#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3769 break;
3770#endif /* CONFIG_IPV6 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003771 }
3772 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003773 if (s == 0)
3774 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003775 return 0;
3776}
3777
3778/**
Paul Moore07feee82009-03-27 17:10:54 -04003779 * smack_sock_graft - Initialize a newly created socket with an existing sock
3780 * @sk: child sock
3781 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003782 *
Paul Moore07feee82009-03-27 17:10:54 -04003783 * Set the smk_{in,out} state of an existing sock based on the process that
3784 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003785 */
3786static void smack_sock_graft(struct sock *sk, struct socket *parent)
3787{
3788 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003789 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003790
Paul Moore07feee82009-03-27 17:10:54 -04003791 if (sk == NULL ||
3792 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003793 return;
3794
3795 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003796 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003797 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003798 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003799}
3800
3801/**
3802 * smack_inet_conn_request - Smack access check on connect
3803 * @sk: socket involved
3804 * @skb: packet
3805 * @req: unused
3806 *
3807 * Returns 0 if a task with the packet label could write to
3808 * the socket, otherwise an error code
3809 */
3810static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3811 struct request_sock *req)
3812{
Paul Moore07feee82009-03-27 17:10:54 -04003813 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003814 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003815 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003816 struct netlbl_lsm_secattr secattr;
3817 struct sockaddr_in addr;
3818 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003819 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003820 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003821 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003822#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003823 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003824#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003825
Casey Schaufler69f287a2014-12-12 17:08:40 -08003826#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003827 if (family == PF_INET6) {
3828 /*
3829 * Handle mapped IPv4 packets arriving
3830 * via IPv6 sockets. Don't set up netlabel
3831 * processing on IPv6.
3832 */
3833 if (skb->protocol == htons(ETH_P_IP))
3834 family = PF_INET;
3835 else
3836 return 0;
3837 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003838#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003839
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003840#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3841 /*
3842 * If there is a secmark use it rather than the CIPSO label.
3843 * If there is no secmark fall back to CIPSO.
3844 * The secmark is assumed to reflect policy better.
3845 */
3846 if (skb && skb->secmark != 0) {
3847 skp = smack_from_secid(skb->secmark);
3848 goto access_check;
3849 }
3850#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3851
Paul Moore07feee82009-03-27 17:10:54 -04003852 netlbl_secattr_init(&secattr);
3853 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003854 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003855 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003856 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003857 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003858 netlbl_secattr_destroy(&secattr);
3859
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003860#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3861access_check:
3862#endif
3863
Etienne Bassetecfcc532009-04-08 20:40:06 +02003864#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003865 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3866 ad.a.u.net->family = family;
3867 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003868 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3869#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003870 /*
Paul Moore07feee82009-03-27 17:10:54 -04003871 * Receiving a packet requires that the other end be able to write
3872 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003873 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003874 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3875 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003876 if (rc != 0)
3877 return rc;
3878
3879 /*
3880 * Save the peer's label in the request_sock so we can later setup
3881 * smk_packet in the child socket so that SO_PEERCRED can report it.
3882 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003883 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003884
3885 /*
3886 * We need to decide if we want to label the incoming connection here
3887 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003888 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003889 */
3890 hdr = ip_hdr(skb);
3891 addr.sin_addr.s_addr = hdr->saddr;
3892 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003893 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003894 rcu_read_unlock();
3895
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003896 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003897 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003898 else
Paul Moore07feee82009-03-27 17:10:54 -04003899 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003900
3901 return rc;
3902}
3903
Paul Moore07feee82009-03-27 17:10:54 -04003904/**
3905 * smack_inet_csk_clone - Copy the connection information to the new socket
3906 * @sk: the new socket
3907 * @req: the connection's request_sock
3908 *
3909 * Transfer the connection's peer label to the newly created socket.
3910 */
3911static void smack_inet_csk_clone(struct sock *sk,
3912 const struct request_sock *req)
3913{
3914 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003915 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003916
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003917 if (req->peer_secid != 0) {
3918 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003919 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003920 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003921 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003922}
3923
Casey Schauflere114e472008-02-04 22:29:50 -08003924/*
3925 * Key management security hooks
3926 *
3927 * Casey has not tested key support very heavily.
3928 * The permission check is most likely too restrictive.
3929 * If you care about keys please have a look.
3930 */
3931#ifdef CONFIG_KEYS
3932
3933/**
3934 * smack_key_alloc - Set the key security blob
3935 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003936 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003937 * @flags: unused
3938 *
3939 * No allocation required
3940 *
3941 * Returns 0
3942 */
David Howellsd84f4f92008-11-14 10:39:23 +11003943static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003944 unsigned long flags)
3945{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003946 struct smack_known *skp = smk_of_task(cred->security);
3947
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003948 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003949 return 0;
3950}
3951
3952/**
3953 * smack_key_free - Clear the key security blob
3954 * @key: the object
3955 *
3956 * Clear the blob pointer
3957 */
3958static void smack_key_free(struct key *key)
3959{
3960 key->security = NULL;
3961}
3962
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003963/**
Casey Schauflere114e472008-02-04 22:29:50 -08003964 * smack_key_permission - Smack access on a key
3965 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003966 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003967 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08003968 *
3969 * Return 0 if the task has read and write to the object,
3970 * an error code otherwise
3971 */
3972static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00003973 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003974{
3975 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003976 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003977 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003978 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07003979 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003980
3981 keyp = key_ref_to_ptr(key_ref);
3982 if (keyp == NULL)
3983 return -EINVAL;
3984 /*
3985 * If the key hasn't been initialized give it access so that
3986 * it may do so.
3987 */
3988 if (keyp->security == NULL)
3989 return 0;
3990 /*
3991 * This should not occur
3992 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003993 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003994 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003995#ifdef CONFIG_AUDIT
3996 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3997 ad.a.u.key_struct.key = keyp->serial;
3998 ad.a.u.key_struct.key_desc = keyp->description;
3999#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004000 if (perm & KEY_NEED_READ)
4001 request = MAY_READ;
4002 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4003 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004004 rc = smk_access(tkp, keyp->security, request, &ad);
4005 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4006 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004007}
José Bollo7fc5f362015-02-17 15:41:22 +01004008
4009/*
4010 * smack_key_getsecurity - Smack label tagging the key
4011 * @key points to the key to be queried
4012 * @_buffer points to a pointer that should be set to point to the
4013 * resulting string (if no label or an error occurs).
4014 * Return the length of the string (including terminating NUL) or -ve if
4015 * an error.
4016 * May also return 0 (and a NULL buffer pointer) if there is no label.
4017 */
4018static int smack_key_getsecurity(struct key *key, char **_buffer)
4019{
4020 struct smack_known *skp = key->security;
4021 size_t length;
4022 char *copy;
4023
4024 if (key->security == NULL) {
4025 *_buffer = NULL;
4026 return 0;
4027 }
4028
4029 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4030 if (copy == NULL)
4031 return -ENOMEM;
4032 length = strlen(copy) + 1;
4033
4034 *_buffer = copy;
4035 return length;
4036}
4037
Casey Schauflere114e472008-02-04 22:29:50 -08004038#endif /* CONFIG_KEYS */
4039
4040/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004041 * Smack Audit hooks
4042 *
4043 * Audit requires a unique representation of each Smack specific
4044 * rule. This unique representation is used to distinguish the
4045 * object to be audited from remaining kernel objects and also
4046 * works as a glue between the audit hooks.
4047 *
4048 * Since repository entries are added but never deleted, we'll use
4049 * the smack_known label address related to the given audit rule as
4050 * the needed unique representation. This also better fits the smack
4051 * model where nearly everything is a label.
4052 */
4053#ifdef CONFIG_AUDIT
4054
4055/**
4056 * smack_audit_rule_init - Initialize a smack audit rule
4057 * @field: audit rule fields given from user-space (audit.h)
4058 * @op: required testing operator (=, !=, >, <, ...)
4059 * @rulestr: smack label to be audited
4060 * @vrule: pointer to save our own audit rule representation
4061 *
4062 * Prepare to audit cases where (@field @op @rulestr) is true.
4063 * The label to be audited is created if necessay.
4064 */
4065static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4066{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004067 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004068 char **rule = (char **)vrule;
4069 *rule = NULL;
4070
4071 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4072 return -EINVAL;
4073
Al Viro5af75d82008-12-16 05:59:26 -05004074 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004075 return -EINVAL;
4076
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004077 skp = smk_import_entry(rulestr, 0);
4078 if (skp)
4079 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004080
4081 return 0;
4082}
4083
4084/**
4085 * smack_audit_rule_known - Distinguish Smack audit rules
4086 * @krule: rule of interest, in Audit kernel representation format
4087 *
4088 * This is used to filter Smack rules from remaining Audit ones.
4089 * If it's proved that this rule belongs to us, the
4090 * audit_rule_match hook will be called to do the final judgement.
4091 */
4092static int smack_audit_rule_known(struct audit_krule *krule)
4093{
4094 struct audit_field *f;
4095 int i;
4096
4097 for (i = 0; i < krule->field_count; i++) {
4098 f = &krule->fields[i];
4099
4100 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4101 return 1;
4102 }
4103
4104 return 0;
4105}
4106
4107/**
4108 * smack_audit_rule_match - Audit given object ?
4109 * @secid: security id for identifying the object to test
4110 * @field: audit rule flags given from user-space
4111 * @op: required testing operator
4112 * @vrule: smack internal rule presentation
4113 * @actx: audit context associated with the check
4114 *
4115 * The core Audit hook. It's used to take the decision of
4116 * whether to audit or not to audit a given object.
4117 */
4118static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4119 struct audit_context *actx)
4120{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004121 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004122 char *rule = vrule;
4123
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004124 if (unlikely(!rule)) {
4125 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004126 return -ENOENT;
4127 }
4128
4129 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4130 return 0;
4131
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004132 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004133
4134 /*
4135 * No need to do string comparisons. If a match occurs,
4136 * both pointers will point to the same smack_known
4137 * label.
4138 */
Al Viro5af75d82008-12-16 05:59:26 -05004139 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004140 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004141 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004142 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004143
4144 return 0;
4145}
4146
4147/**
4148 * smack_audit_rule_free - free smack rule representation
4149 * @vrule: rule to be freed.
4150 *
4151 * No memory was allocated.
4152 */
4153static void smack_audit_rule_free(void *vrule)
4154{
4155 /* No-op */
4156}
4157
4158#endif /* CONFIG_AUDIT */
4159
Randy Dunlap251a2a92009-02-18 11:42:33 -08004160/**
David Quigley746df9b2013-05-22 12:50:35 -04004161 * smack_ismaclabel - check if xattr @name references a smack MAC label
4162 * @name: Full xattr name to check.
4163 */
4164static int smack_ismaclabel(const char *name)
4165{
4166 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4167}
4168
4169
4170/**
Casey Schauflere114e472008-02-04 22:29:50 -08004171 * smack_secid_to_secctx - return the smack label for a secid
4172 * @secid: incoming integer
4173 * @secdata: destination
4174 * @seclen: how long it is
4175 *
4176 * Exists for networking code.
4177 */
4178static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4179{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004180 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004181
Eric Parisd5630b92010-10-13 16:24:48 -04004182 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004183 *secdata = skp->smk_known;
4184 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004185 return 0;
4186}
4187
Randy Dunlap251a2a92009-02-18 11:42:33 -08004188/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004189 * smack_secctx_to_secid - return the secid for a smack label
4190 * @secdata: smack label
4191 * @seclen: how long result is
4192 * @secid: outgoing integer
4193 *
4194 * Exists for audit and networking code.
4195 */
David Howellse52c17642008-04-29 20:52:51 +01004196static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004197{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004198 struct smack_known *skp = smk_find_entry(secdata);
4199
4200 if (skp)
4201 *secid = skp->smk_secid;
4202 else
4203 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004204 return 0;
4205}
4206
Randy Dunlap251a2a92009-02-18 11:42:33 -08004207/**
Casey Schauflere114e472008-02-04 22:29:50 -08004208 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004209 * @secdata: unused
4210 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004211 *
4212 * Exists to make sure nothing gets done, and properly
4213 */
4214static void smack_release_secctx(char *secdata, u32 seclen)
4215{
4216}
4217
David P. Quigley1ee65e32009-09-03 14:25:57 -04004218static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4219{
4220 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4221}
4222
4223static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4224{
4225 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4226}
4227
4228static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4229{
4230 int len = 0;
4231 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4232
4233 if (len < 0)
4234 return len;
4235 *ctxlen = len;
4236 return 0;
4237}
4238
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004239struct security_hook_list smack_hooks[] = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004240 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4241 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4242 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004243
Casey Schauflere20b0432015-05-02 15:11:36 -07004244 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4245 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4246 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4247 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4248 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Casey Schauflere114e472008-02-04 22:29:50 -08004249
Casey Schauflere20b0432015-05-02 15:11:36 -07004250 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4251 LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
4252 LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004253
Casey Schauflere20b0432015-05-02 15:11:36 -07004254 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4255 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4256 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4257 LSM_HOOK_INIT(inode_link, smack_inode_link),
4258 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4259 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4260 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4261 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4262 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4263 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4264 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4265 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4266 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4267 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4268 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4269 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4270 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4271 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004272
Casey Schauflere20b0432015-05-02 15:11:36 -07004273 LSM_HOOK_INIT(file_permission, smack_file_permission),
4274 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4275 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4276 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4277 LSM_HOOK_INIT(file_lock, smack_file_lock),
4278 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4279 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4280 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4281 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4282 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4283 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004284
Casey Schauflere20b0432015-05-02 15:11:36 -07004285 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004286
Casey Schauflere20b0432015-05-02 15:11:36 -07004287 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4288 LSM_HOOK_INIT(cred_free, smack_cred_free),
4289 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4290 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4291 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4292 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4293 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4294 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4295 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4296 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4297 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4298 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4299 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4300 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4301 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4302 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4303 LSM_HOOK_INIT(task_kill, smack_task_kill),
4304 LSM_HOOK_INIT(task_wait, smack_task_wait),
4305 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004306
Casey Schauflere20b0432015-05-02 15:11:36 -07004307 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4308 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004309
Casey Schauflere20b0432015-05-02 15:11:36 -07004310 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4311 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004312
Casey Schauflere20b0432015-05-02 15:11:36 -07004313 LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
4314 LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
4315 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4316 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4317 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4318 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004319
Casey Schauflere20b0432015-05-02 15:11:36 -07004320 LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
4321 LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
4322 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4323 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4324 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004325
Casey Schauflere20b0432015-05-02 15:11:36 -07004326 LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
4327 LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
4328 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4329 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4330 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004331
Casey Schauflere20b0432015-05-02 15:11:36 -07004332 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004333
Casey Schauflere20b0432015-05-02 15:11:36 -07004334 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4335 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004336
Casey Schauflere20b0432015-05-02 15:11:36 -07004337 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4338 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004339
Casey Schauflere20b0432015-05-02 15:11:36 -07004340 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Casey Schaufler69f287a2014-12-12 17:08:40 -08004341#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schauflere20b0432015-05-02 15:11:36 -07004342 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler69f287a2014-12-12 17:08:40 -08004343#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflere20b0432015-05-02 15:11:36 -07004344 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4345 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4346 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4347 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4348 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4349 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4350 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4351 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4352 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4353 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004354
Casey Schauflere114e472008-02-04 22:29:50 -08004355 /* key management security hooks */
4356#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004357 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4358 LSM_HOOK_INIT(key_free, smack_key_free),
4359 LSM_HOOK_INIT(key_permission, smack_key_permission),
4360 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004361#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004362
4363 /* Audit hooks */
4364#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004365 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4366 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4367 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4368 LSM_HOOK_INIT(audit_rule_free, smack_audit_rule_free),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004369#endif /* CONFIG_AUDIT */
4370
Casey Schauflere20b0432015-05-02 15:11:36 -07004371 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4372 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4373 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4374 LSM_HOOK_INIT(release_secctx, smack_release_secctx),
4375 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4376 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4377 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflere114e472008-02-04 22:29:50 -08004378};
4379
Etienne Basset7198e2e2009-03-24 20:53:24 +01004380
Casey Schaufler86812bb2012-04-17 18:55:46 -07004381static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004382{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004383 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004384 * Initialize rule list locks
4385 */
4386 mutex_init(&smack_known_huh.smk_rules_lock);
4387 mutex_init(&smack_known_hat.smk_rules_lock);
4388 mutex_init(&smack_known_floor.smk_rules_lock);
4389 mutex_init(&smack_known_star.smk_rules_lock);
4390 mutex_init(&smack_known_invalid.smk_rules_lock);
4391 mutex_init(&smack_known_web.smk_rules_lock);
4392 /*
4393 * Initialize rule lists
4394 */
4395 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4396 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4397 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4398 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4399 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4400 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4401 /*
4402 * Create the known labels list
4403 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004404 smk_insert_entry(&smack_known_huh);
4405 smk_insert_entry(&smack_known_hat);
4406 smk_insert_entry(&smack_known_star);
4407 smk_insert_entry(&smack_known_floor);
4408 smk_insert_entry(&smack_known_invalid);
4409 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004410}
4411
Casey Schauflere114e472008-02-04 22:29:50 -08004412/**
4413 * smack_init - initialize the smack system
4414 *
4415 * Returns 0
4416 */
4417static __init int smack_init(void)
4418{
David Howellsd84f4f92008-11-14 10:39:23 +11004419 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004420 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004421
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004422 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004423 return 0;
4424
Casey Schaufler69f287a2014-12-12 17:08:40 -08004425 smack_enabled = 1;
4426
Rohit1a5b4722014-10-15 17:40:41 +05304427 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4428 if (!smack_inode_cache)
4429 return -ENOMEM;
4430
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004431 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4432 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304433 if (tsp == NULL) {
4434 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004435 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304436 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004437
Casey Schauflere114e472008-02-04 22:29:50 -08004438 printk(KERN_INFO "Smack: Initializing.\n");
4439
4440 /*
4441 * Set the security state for the initial task.
4442 */
David Howellsd84f4f92008-11-14 10:39:23 +11004443 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004444 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004445
Casey Schaufler86812bb2012-04-17 18:55:46 -07004446 /* initialize the smack_known_list */
4447 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004448
4449 /*
4450 * Register with LSM
4451 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004452 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
Casey Schauflere114e472008-02-04 22:29:50 -08004453
4454 return 0;
4455}
4456
4457/*
4458 * Smack requires early initialization in order to label
4459 * all processes and objects when they are created.
4460 */
4461security_initcall(smack_init);