blob: f09b8c7cf1e78e93b5b9e31df0185cd562973c8b [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
Seung-Woo Kim97775822015-04-17 15:25:04 +09001661 if (unlikely(IS_PRIVATE(inode)))
1662 return 0;
1663
Casey Schaufler4482a442013-12-30 17:37:45 -08001664 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001665 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001666 /*
1667 * This code relies on bitmasks.
1668 */
1669 if (file->f_mode & FMODE_READ)
1670 may = MAY_READ;
1671 if (file->f_mode & FMODE_WRITE)
1672 may |= MAY_WRITE;
1673
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001674 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001675 rc = smk_bu_file(file, may, rc);
1676 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001677}
1678
Casey Schaufler531f1d42011-09-19 12:41:42 -07001679/**
Eric Paris83d49852012-04-04 13:45:40 -04001680 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001681 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001682 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001683 *
1684 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001685 * Allow the open only if the task has read access. There are
1686 * many read operations (e.g. fstat) that you can do with an
1687 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001688 *
1689 * Returns 0
1690 */
Eric Paris83d49852012-04-04 13:45:40 -04001691static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001692{
Casey Schauflera6834c02014-04-21 11:10:26 -07001693 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001694 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001695 struct smk_audit_info ad;
1696 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001697
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001698 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001699 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001700
Casey Schauflera6834c02014-04-21 11:10:26 -07001701 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1702 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001703 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001704 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001705
1706 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001707}
1708
Casey Schauflere114e472008-02-04 22:29:50 -08001709/*
1710 * Task hooks
1711 */
1712
1713/**
David Howellsee18d642009-09-02 09:14:21 +01001714 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1715 * @new: the new credentials
1716 * @gfp: the atomicity of any memory allocations
1717 *
1718 * Prepare a blank set of credentials for modification. This must allocate all
1719 * the memory the LSM module might require such that cred_transfer() can
1720 * complete without error.
1721 */
1722static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1723{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001724 struct task_smack *tsp;
1725
1726 tsp = new_task_smack(NULL, NULL, gfp);
1727 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001728 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001729
1730 cred->security = tsp;
1731
David Howellsee18d642009-09-02 09:14:21 +01001732 return 0;
1733}
1734
1735
1736/**
David Howellsf1752ee2008-11-14 10:39:17 +11001737 * smack_cred_free - "free" task-level security credentials
1738 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001739 *
Casey Schauflere114e472008-02-04 22:29:50 -08001740 */
David Howellsf1752ee2008-11-14 10:39:17 +11001741static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001742{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001743 struct task_smack *tsp = cred->security;
1744 struct smack_rule *rp;
1745 struct list_head *l;
1746 struct list_head *n;
1747
1748 if (tsp == NULL)
1749 return;
1750 cred->security = NULL;
1751
1752 list_for_each_safe(l, n, &tsp->smk_rules) {
1753 rp = list_entry(l, struct smack_rule, list);
1754 list_del(&rp->list);
1755 kfree(rp);
1756 }
1757 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001758}
1759
1760/**
David Howellsd84f4f92008-11-14 10:39:23 +11001761 * smack_cred_prepare - prepare new set of credentials for modification
1762 * @new: the new credentials
1763 * @old: the original credentials
1764 * @gfp: the atomicity of any memory allocations
1765 *
1766 * Prepare a new set of credentials for modification.
1767 */
1768static int smack_cred_prepare(struct cred *new, const struct cred *old,
1769 gfp_t gfp)
1770{
Casey Schaufler676dac42010-12-02 06:43:39 -08001771 struct task_smack *old_tsp = old->security;
1772 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001773 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001774
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001775 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001776 if (new_tsp == NULL)
1777 return -ENOMEM;
1778
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001779 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1780 if (rc != 0)
1781 return rc;
1782
Casey Schaufler676dac42010-12-02 06:43:39 -08001783 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001784 return 0;
1785}
1786
Randy Dunlap251a2a92009-02-18 11:42:33 -08001787/**
David Howellsee18d642009-09-02 09:14:21 +01001788 * smack_cred_transfer - Transfer the old credentials to the new credentials
1789 * @new: the new credentials
1790 * @old: the original credentials
1791 *
1792 * Fill in a set of blank credentials from another set of credentials.
1793 */
1794static void smack_cred_transfer(struct cred *new, const struct cred *old)
1795{
Casey Schaufler676dac42010-12-02 06:43:39 -08001796 struct task_smack *old_tsp = old->security;
1797 struct task_smack *new_tsp = new->security;
1798
1799 new_tsp->smk_task = old_tsp->smk_task;
1800 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001801 mutex_init(&new_tsp->smk_rules_lock);
1802 INIT_LIST_HEAD(&new_tsp->smk_rules);
1803
1804
1805 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001806}
1807
1808/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001809 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001810 * @new: points to the set of credentials to be modified.
1811 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001812 *
1813 * Set the security data for a kernel service.
1814 */
1815static int smack_kernel_act_as(struct cred *new, u32 secid)
1816{
Casey Schaufler676dac42010-12-02 06:43:39 -08001817 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001818 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001819
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001820 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001821 return -EINVAL;
1822
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001823 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001824 return 0;
1825}
1826
1827/**
1828 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001829 * @new: points to the set of credentials to be modified
1830 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001831 *
1832 * Set the file creation context in a set of credentials to the same
1833 * as the objective context of the specified inode
1834 */
1835static int smack_kernel_create_files_as(struct cred *new,
1836 struct inode *inode)
1837{
1838 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001839 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001840
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001841 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001842 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001843 return 0;
1844}
1845
1846/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001847 * smk_curacc_on_task - helper to log task related access
1848 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001849 * @access: the access requested
1850 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001851 *
1852 * Return 0 if access is permitted
1853 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001854static int smk_curacc_on_task(struct task_struct *p, int access,
1855 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001856{
1857 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001858 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001859 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001860
Casey Schaufler531f1d42011-09-19 12:41:42 -07001861 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001862 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001863 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001864 rc = smk_bu_task(p, access, rc);
1865 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001866}
1867
1868/**
Casey Schauflere114e472008-02-04 22:29:50 -08001869 * smack_task_setpgid - Smack check on setting pgid
1870 * @p: the task object
1871 * @pgid: unused
1872 *
1873 * Return 0 if write access is permitted
1874 */
1875static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1876{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001877 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001878}
1879
1880/**
1881 * smack_task_getpgid - Smack access check for getpgid
1882 * @p: the object task
1883 *
1884 * Returns 0 if current can read the object task, error code otherwise
1885 */
1886static int smack_task_getpgid(struct task_struct *p)
1887{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001888 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001889}
1890
1891/**
1892 * smack_task_getsid - Smack access check for getsid
1893 * @p: the object task
1894 *
1895 * Returns 0 if current can read the object task, error code otherwise
1896 */
1897static int smack_task_getsid(struct task_struct *p)
1898{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001899 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001900}
1901
1902/**
1903 * smack_task_getsecid - get the secid of the task
1904 * @p: the object task
1905 * @secid: where to put the result
1906 *
1907 * Sets the secid to contain a u32 version of the smack label.
1908 */
1909static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1910{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001911 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001912
1913 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001914}
1915
1916/**
1917 * smack_task_setnice - Smack check on setting nice
1918 * @p: the task object
1919 * @nice: unused
1920 *
1921 * Return 0 if write access is permitted
1922 */
1923static int smack_task_setnice(struct task_struct *p, int nice)
1924{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001925 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001926}
1927
1928/**
1929 * smack_task_setioprio - Smack check on setting ioprio
1930 * @p: the task object
1931 * @ioprio: unused
1932 *
1933 * Return 0 if write access is permitted
1934 */
1935static int smack_task_setioprio(struct task_struct *p, int ioprio)
1936{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001937 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001938}
1939
1940/**
1941 * smack_task_getioprio - Smack check on reading ioprio
1942 * @p: the task object
1943 *
1944 * Return 0 if read access is permitted
1945 */
1946static int smack_task_getioprio(struct task_struct *p)
1947{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001948 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001949}
1950
1951/**
1952 * smack_task_setscheduler - Smack check on setting scheduler
1953 * @p: the task object
1954 * @policy: unused
1955 * @lp: unused
1956 *
1957 * Return 0 if read access is permitted
1958 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001959static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001960{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07001961 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001962}
1963
1964/**
1965 * smack_task_getscheduler - Smack check on reading scheduler
1966 * @p: the task object
1967 *
1968 * Return 0 if read access is permitted
1969 */
1970static int smack_task_getscheduler(struct task_struct *p)
1971{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001972 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001973}
1974
1975/**
1976 * smack_task_movememory - Smack check on moving memory
1977 * @p: the task object
1978 *
1979 * Return 0 if write access is permitted
1980 */
1981static int smack_task_movememory(struct task_struct *p)
1982{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001983 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001984}
1985
1986/**
1987 * smack_task_kill - Smack check on signal delivery
1988 * @p: the task object
1989 * @info: unused
1990 * @sig: unused
1991 * @secid: identifies the smack to use in lieu of current's
1992 *
1993 * Return 0 if write access is permitted
1994 *
1995 * The secid behavior is an artifact of an SELinux hack
1996 * in the USB code. Someday it may go away.
1997 */
1998static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1999 int sig, u32 secid)
2000{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002001 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002002 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002003 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002004 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002005
2006 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2007 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002008 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002009 * Sending a signal requires that the sender
2010 * can write the receiver.
2011 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002012 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002013 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002014 rc = smk_bu_task(p, MAY_WRITE, rc);
2015 return rc;
2016 }
Casey Schauflere114e472008-02-04 22:29:50 -08002017 /*
2018 * If the secid isn't 0 we're dealing with some USB IO
2019 * specific behavior. This is not clean. For one thing
2020 * we can't take privilege into account.
2021 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002022 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002023 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2024 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002025 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002026}
2027
2028/**
2029 * smack_task_wait - Smack access check for waiting
2030 * @p: task to wait for
2031 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002032 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002033 */
2034static int smack_task_wait(struct task_struct *p)
2035{
Casey Schauflere114e472008-02-04 22:29:50 -08002036 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002037 * Allow the operation to succeed.
2038 * Zombies are bad.
2039 * In userless environments (e.g. phones) programs
2040 * get marked with SMACK64EXEC and even if the parent
2041 * and child shouldn't be talking the parent still
2042 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002043 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002044 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002045}
2046
2047/**
2048 * smack_task_to_inode - copy task smack into the inode blob
2049 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002050 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002051 *
2052 * Sets the smack pointer in the inode security blob
2053 */
2054static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2055{
2056 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002057 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002058
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002059 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002060}
2061
2062/*
2063 * Socket hooks.
2064 */
2065
2066/**
2067 * smack_sk_alloc_security - Allocate a socket blob
2068 * @sk: the socket
2069 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002070 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002071 *
2072 * Assign Smack pointers to current
2073 *
2074 * Returns 0 on success, -ENOMEM is there's no memory
2075 */
2076static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2077{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002078 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002079 struct socket_smack *ssp;
2080
2081 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2082 if (ssp == NULL)
2083 return -ENOMEM;
2084
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002085 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002086 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002087 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002088
2089 sk->sk_security = ssp;
2090
2091 return 0;
2092}
2093
2094/**
2095 * smack_sk_free_security - Free a socket blob
2096 * @sk: the socket
2097 *
2098 * Clears the blob pointer
2099 */
2100static void smack_sk_free_security(struct sock *sk)
2101{
2102 kfree(sk->sk_security);
2103}
2104
2105/**
Paul Moore07feee82009-03-27 17:10:54 -04002106* smack_host_label - check host based restrictions
2107* @sip: the object end
2108*
2109* looks for host based access restrictions
2110*
2111* This version will only be appropriate for really small sets of single label
2112* hosts. The caller is responsible for ensuring that the RCU read lock is
2113* taken before calling this function.
2114*
2115* Returns the label of the far end or NULL if it's not special.
2116*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002117static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002118{
2119 struct smk_netlbladdr *snp;
2120 struct in_addr *siap = &sip->sin_addr;
2121
2122 if (siap->s_addr == 0)
2123 return NULL;
2124
2125 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2126 /*
2127 * we break after finding the first match because
2128 * the list is sorted from longest to shortest mask
2129 * so we have found the most specific match
2130 */
2131 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002132 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2133 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002134 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002135 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002136 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002137 }
Paul Moore07feee82009-03-27 17:10:54 -04002138
2139 return NULL;
2140}
2141
2142/**
Casey Schauflere114e472008-02-04 22:29:50 -08002143 * smack_netlabel - Set the secattr on a socket
2144 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002145 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002146 *
2147 * Convert the outbound smack value (smk_out) to a
2148 * secattr and attach it to the socket.
2149 *
2150 * Returns 0 on success or an error code
2151 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002152static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002153{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002154 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002155 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002156 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002157
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002158 /*
2159 * Usually the netlabel code will handle changing the
2160 * packet labeling based on the label.
2161 * The case of a single label host is different, because
2162 * a single label host should never get a labeled packet
2163 * even though the label is usually associated with a packet
2164 * label.
2165 */
2166 local_bh_disable();
2167 bh_lock_sock_nested(sk);
2168
2169 if (ssp->smk_out == smack_net_ambient ||
2170 labeled == SMACK_UNLABELED_SOCKET)
2171 netlbl_sock_delattr(sk);
2172 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002173 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002174 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002175 }
2176
2177 bh_unlock_sock(sk);
2178 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002179
Casey Schauflere114e472008-02-04 22:29:50 -08002180 return rc;
2181}
2182
2183/**
Paul Moore07feee82009-03-27 17:10:54 -04002184 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2185 * @sk: the socket
2186 * @sap: the destination address
2187 *
2188 * Set the correct secattr for the given socket based on the destination
2189 * address and perform any outbound access checks needed.
2190 *
2191 * Returns 0 on success or an error code.
2192 *
2193 */
2194static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2195{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002196 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002197 int rc;
2198 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002199 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002200 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002201 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002202
2203 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002204 hkp = smack_host_label(sap);
2205 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002206#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002207 struct lsm_network_audit net;
2208
Eric Paris48c62af2012-04-02 13:15:44 -04002209 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2210 ad.a.u.net->family = sap->sin_family;
2211 ad.a.u.net->dport = sap->sin_port;
2212 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002213#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002214 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002215 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002216 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2217 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002218 } else {
2219 sk_lbl = SMACK_CIPSO_SOCKET;
2220 rc = 0;
2221 }
2222 rcu_read_unlock();
2223 if (rc != 0)
2224 return rc;
2225
2226 return smack_netlabel(sk, sk_lbl);
2227}
2228
Casey Schaufler69f287a2014-12-12 17:08:40 -08002229#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Paul Moore07feee82009-03-27 17:10:54 -04002230/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002231 * smk_ipv6_port_label - Smack port access table management
2232 * @sock: socket
2233 * @address: address
2234 *
2235 * Create or update the port list entry
2236 */
2237static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2238{
2239 struct sock *sk = sock->sk;
2240 struct sockaddr_in6 *addr6;
2241 struct socket_smack *ssp = sock->sk->sk_security;
2242 struct smk_port_label *spp;
2243 unsigned short port = 0;
2244
2245 if (address == NULL) {
2246 /*
2247 * This operation is changing the Smack information
2248 * on the bound socket. Take the changes to the port
2249 * as well.
2250 */
2251 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2252 if (sk != spp->smk_sock)
2253 continue;
2254 spp->smk_in = ssp->smk_in;
2255 spp->smk_out = ssp->smk_out;
2256 return;
2257 }
2258 /*
2259 * A NULL address is only used for updating existing
2260 * bound entries. If there isn't one, it's OK.
2261 */
2262 return;
2263 }
2264
2265 addr6 = (struct sockaddr_in6 *)address;
2266 port = ntohs(addr6->sin6_port);
2267 /*
2268 * This is a special case that is safely ignored.
2269 */
2270 if (port == 0)
2271 return;
2272
2273 /*
2274 * Look for an existing port list entry.
2275 * This is an indication that a port is getting reused.
2276 */
2277 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2278 if (spp->smk_port != port)
2279 continue;
2280 spp->smk_port = port;
2281 spp->smk_sock = sk;
2282 spp->smk_in = ssp->smk_in;
2283 spp->smk_out = ssp->smk_out;
2284 return;
2285 }
2286
2287 /*
2288 * A new port entry is required.
2289 */
2290 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2291 if (spp == NULL)
2292 return;
2293
2294 spp->smk_port = port;
2295 spp->smk_sock = sk;
2296 spp->smk_in = ssp->smk_in;
2297 spp->smk_out = ssp->smk_out;
2298
2299 list_add(&spp->list, &smk_ipv6_port_list);
2300 return;
2301}
2302
2303/**
2304 * smk_ipv6_port_check - check Smack port access
2305 * @sock: socket
2306 * @address: address
2307 *
2308 * Create or update the port list entry
2309 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002310static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002311 int act)
2312{
2313 __be16 *bep;
2314 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002315 struct smk_port_label *spp;
2316 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002317 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002318 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002319 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002320 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002321 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002322#ifdef CONFIG_AUDIT
2323 struct lsm_network_audit net;
2324#endif
2325
2326 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002327 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002328 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002329 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002330 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002331 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002332 }
2333
2334 /*
2335 * Get the IP address and port from the address.
2336 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002337 port = ntohs(address->sin6_port);
2338 bep = (__be16 *)(&address->sin6_addr);
2339 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002340
2341 /*
2342 * It's remote, so port lookup does no good.
2343 */
2344 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2345 goto auditout;
2346
2347 /*
2348 * It's local so the send check has to have passed.
2349 */
2350 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002351 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002352 goto auditout;
2353 }
2354
2355 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2356 if (spp->smk_port != port)
2357 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002358 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002359 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002360 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002361 break;
2362 }
2363
2364auditout:
2365
2366#ifdef CONFIG_AUDIT
2367 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2368 ad.a.u.net->family = sk->sk_family;
2369 ad.a.u.net->dport = port;
2370 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002371 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002372 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002373 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002374#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002375 rc = smk_access(skp, object, MAY_WRITE, &ad);
2376 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2377 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002378}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002379#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002380
2381/**
Casey Schauflere114e472008-02-04 22:29:50 -08002382 * smack_inode_setsecurity - set smack xattrs
2383 * @inode: the object
2384 * @name: attribute name
2385 * @value: attribute value
2386 * @size: size of the attribute
2387 * @flags: unused
2388 *
2389 * Sets the named attribute in the appropriate blob
2390 *
2391 * Returns 0 on success, or an error code
2392 */
2393static int smack_inode_setsecurity(struct inode *inode, const char *name,
2394 const void *value, size_t size, int flags)
2395{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002396 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002397 struct inode_smack *nsp = inode->i_security;
2398 struct socket_smack *ssp;
2399 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002400 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002401
Casey Schauflerf7112e62012-05-06 15:22:02 -07002402 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302403 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002404
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002405 skp = smk_import_entry(value, size);
2406 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002407 return -EINVAL;
2408
2409 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002410 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002411 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002412 return 0;
2413 }
2414 /*
2415 * The rest of the Smack xattrs are only on sockets.
2416 */
2417 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2418 return -EOPNOTSUPP;
2419
2420 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002421 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002422 return -EOPNOTSUPP;
2423
2424 ssp = sock->sk->sk_security;
2425
2426 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002427 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002428 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002429 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002430 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002431 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2432 if (rc != 0)
2433 printk(KERN_WARNING
2434 "Smack: \"%s\" netlbl error %d.\n",
2435 __func__, -rc);
2436 }
Casey Schauflere114e472008-02-04 22:29:50 -08002437 } else
2438 return -EOPNOTSUPP;
2439
Casey Schaufler69f287a2014-12-12 17:08:40 -08002440#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07002441 if (sock->sk->sk_family == PF_INET6)
2442 smk_ipv6_port_label(sock, NULL);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002443#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002444
Casey Schauflere114e472008-02-04 22:29:50 -08002445 return 0;
2446}
2447
2448/**
2449 * smack_socket_post_create - finish socket setup
2450 * @sock: the socket
2451 * @family: protocol family
2452 * @type: unused
2453 * @protocol: unused
2454 * @kern: unused
2455 *
2456 * Sets the netlabel information on the socket
2457 *
2458 * Returns 0 on success, and error code otherwise
2459 */
2460static int smack_socket_post_create(struct socket *sock, int family,
2461 int type, int protocol, int kern)
2462{
Marcin Lis74123012015-01-22 15:40:33 +01002463 struct socket_smack *ssp;
2464
2465 if (sock->sk == NULL)
2466 return 0;
2467
2468 /*
2469 * Sockets created by kernel threads receive web label.
2470 */
2471 if (unlikely(current->flags & PF_KTHREAD)) {
2472 ssp = sock->sk->sk_security;
2473 ssp->smk_in = &smack_known_web;
2474 ssp->smk_out = &smack_known_web;
2475 }
2476
2477 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002478 return 0;
2479 /*
2480 * Set the outbound netlbl.
2481 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002482 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2483}
2484
Casey Schaufler69f287a2014-12-12 17:08:40 -08002485#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002486/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002487 * smack_socket_bind - record port binding information.
2488 * @sock: the socket
2489 * @address: the port address
2490 * @addrlen: size of the address
2491 *
2492 * Records the label bound to a port.
2493 *
2494 * Returns 0
2495 */
2496static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2497 int addrlen)
2498{
Casey Schaufler69f287a2014-12-12 17:08:40 -08002499#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002500 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2501 smk_ipv6_port_label(sock, address);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002502#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002503
2504 return 0;
2505}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002506#endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002507
2508/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002509 * smack_socket_connect - connect access check
2510 * @sock: the socket
2511 * @sap: the other end
2512 * @addrlen: size of sap
2513 *
2514 * Verifies that a connection may be possible
2515 *
2516 * Returns 0 on success, and error code otherwise
2517 */
2518static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2519 int addrlen)
2520{
Casey Schauflerc6739442013-05-22 18:42:56 -07002521 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002522
Casey Schauflerc6739442013-05-22 18:42:56 -07002523 if (sock->sk == NULL)
2524 return 0;
2525
2526 switch (sock->sk->sk_family) {
2527 case PF_INET:
2528 if (addrlen < sizeof(struct sockaddr_in))
2529 return -EINVAL;
2530 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2531 break;
2532 case PF_INET6:
2533 if (addrlen < sizeof(struct sockaddr_in6))
2534 return -EINVAL;
Casey Schaufler69f287a2014-12-12 17:08:40 -08002535#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002536 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2537 SMK_CONNECTING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002538#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002539 break;
2540 }
2541 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002542}
2543
2544/**
2545 * smack_flags_to_may - convert S_ to MAY_ values
2546 * @flags: the S_ value
2547 *
2548 * Returns the equivalent MAY_ value
2549 */
2550static int smack_flags_to_may(int flags)
2551{
2552 int may = 0;
2553
2554 if (flags & S_IRUGO)
2555 may |= MAY_READ;
2556 if (flags & S_IWUGO)
2557 may |= MAY_WRITE;
2558 if (flags & S_IXUGO)
2559 may |= MAY_EXEC;
2560
2561 return may;
2562}
2563
2564/**
2565 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2566 * @msg: the object
2567 *
2568 * Returns 0
2569 */
2570static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2571{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002572 struct smack_known *skp = smk_of_current();
2573
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002574 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002575 return 0;
2576}
2577
2578/**
2579 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2580 * @msg: the object
2581 *
2582 * Clears the blob pointer
2583 */
2584static void smack_msg_msg_free_security(struct msg_msg *msg)
2585{
2586 msg->security = NULL;
2587}
2588
2589/**
2590 * smack_of_shm - the smack pointer for the shm
2591 * @shp: the object
2592 *
2593 * Returns a pointer to the smack value
2594 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002595static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002596{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002597 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002598}
2599
2600/**
2601 * smack_shm_alloc_security - Set the security blob for shm
2602 * @shp: the object
2603 *
2604 * Returns 0
2605 */
2606static int smack_shm_alloc_security(struct shmid_kernel *shp)
2607{
2608 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002609 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002610
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002611 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002612 return 0;
2613}
2614
2615/**
2616 * smack_shm_free_security - Clear the security blob for shm
2617 * @shp: the object
2618 *
2619 * Clears the blob pointer
2620 */
2621static void smack_shm_free_security(struct shmid_kernel *shp)
2622{
2623 struct kern_ipc_perm *isp = &shp->shm_perm;
2624
2625 isp->security = NULL;
2626}
2627
2628/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002629 * smk_curacc_shm : check if current has access on shm
2630 * @shp : the object
2631 * @access : access requested
2632 *
2633 * Returns 0 if current has the requested access, error code otherwise
2634 */
2635static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2636{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002637 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002638 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002639 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002640
2641#ifdef CONFIG_AUDIT
2642 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2643 ad.a.u.ipc_id = shp->shm_perm.id;
2644#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002645 rc = smk_curacc(ssp, access, &ad);
2646 rc = smk_bu_current("shm", ssp, access, rc);
2647 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002648}
2649
2650/**
Casey Schauflere114e472008-02-04 22:29:50 -08002651 * smack_shm_associate - Smack access check for shm
2652 * @shp: the object
2653 * @shmflg: access requested
2654 *
2655 * Returns 0 if current has the requested access, error code otherwise
2656 */
2657static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2658{
Casey Schauflere114e472008-02-04 22:29:50 -08002659 int may;
2660
2661 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002662 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002663}
2664
2665/**
2666 * smack_shm_shmctl - Smack access check for shm
2667 * @shp: the object
2668 * @cmd: what it wants to do
2669 *
2670 * Returns 0 if current has the requested access, error code otherwise
2671 */
2672static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2673{
Casey Schauflere114e472008-02-04 22:29:50 -08002674 int may;
2675
2676 switch (cmd) {
2677 case IPC_STAT:
2678 case SHM_STAT:
2679 may = MAY_READ;
2680 break;
2681 case IPC_SET:
2682 case SHM_LOCK:
2683 case SHM_UNLOCK:
2684 case IPC_RMID:
2685 may = MAY_READWRITE;
2686 break;
2687 case IPC_INFO:
2688 case SHM_INFO:
2689 /*
2690 * System level information.
2691 */
2692 return 0;
2693 default:
2694 return -EINVAL;
2695 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002696 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002697}
2698
2699/**
2700 * smack_shm_shmat - Smack access for shmat
2701 * @shp: the object
2702 * @shmaddr: unused
2703 * @shmflg: access requested
2704 *
2705 * Returns 0 if current has the requested access, error code otherwise
2706 */
2707static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2708 int shmflg)
2709{
Casey Schauflere114e472008-02-04 22:29:50 -08002710 int may;
2711
2712 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002713 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002714}
2715
2716/**
2717 * smack_of_sem - the smack pointer for the sem
2718 * @sma: the object
2719 *
2720 * Returns a pointer to the smack value
2721 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002722static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002723{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002724 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002725}
2726
2727/**
2728 * smack_sem_alloc_security - Set the security blob for sem
2729 * @sma: the object
2730 *
2731 * Returns 0
2732 */
2733static int smack_sem_alloc_security(struct sem_array *sma)
2734{
2735 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002736 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002737
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002738 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002739 return 0;
2740}
2741
2742/**
2743 * smack_sem_free_security - Clear the security blob for sem
2744 * @sma: the object
2745 *
2746 * Clears the blob pointer
2747 */
2748static void smack_sem_free_security(struct sem_array *sma)
2749{
2750 struct kern_ipc_perm *isp = &sma->sem_perm;
2751
2752 isp->security = NULL;
2753}
2754
2755/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002756 * smk_curacc_sem : check if current has access on sem
2757 * @sma : the object
2758 * @access : access requested
2759 *
2760 * Returns 0 if current has the requested access, error code otherwise
2761 */
2762static int smk_curacc_sem(struct sem_array *sma, int access)
2763{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002764 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002765 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002766 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002767
2768#ifdef CONFIG_AUDIT
2769 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2770 ad.a.u.ipc_id = sma->sem_perm.id;
2771#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002772 rc = smk_curacc(ssp, access, &ad);
2773 rc = smk_bu_current("sem", ssp, access, rc);
2774 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002775}
2776
2777/**
Casey Schauflere114e472008-02-04 22:29:50 -08002778 * smack_sem_associate - Smack access check for sem
2779 * @sma: the object
2780 * @semflg: access requested
2781 *
2782 * Returns 0 if current has the requested access, error code otherwise
2783 */
2784static int smack_sem_associate(struct sem_array *sma, int semflg)
2785{
Casey Schauflere114e472008-02-04 22:29:50 -08002786 int may;
2787
2788 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002789 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002790}
2791
2792/**
2793 * smack_sem_shmctl - Smack access check for sem
2794 * @sma: the object
2795 * @cmd: what it wants to do
2796 *
2797 * Returns 0 if current has the requested access, error code otherwise
2798 */
2799static int smack_sem_semctl(struct sem_array *sma, int cmd)
2800{
Casey Schauflere114e472008-02-04 22:29:50 -08002801 int may;
2802
2803 switch (cmd) {
2804 case GETPID:
2805 case GETNCNT:
2806 case GETZCNT:
2807 case GETVAL:
2808 case GETALL:
2809 case IPC_STAT:
2810 case SEM_STAT:
2811 may = MAY_READ;
2812 break;
2813 case SETVAL:
2814 case SETALL:
2815 case IPC_RMID:
2816 case IPC_SET:
2817 may = MAY_READWRITE;
2818 break;
2819 case IPC_INFO:
2820 case SEM_INFO:
2821 /*
2822 * System level information
2823 */
2824 return 0;
2825 default:
2826 return -EINVAL;
2827 }
2828
Etienne Bassetecfcc532009-04-08 20:40:06 +02002829 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002830}
2831
2832/**
2833 * smack_sem_semop - Smack checks of semaphore operations
2834 * @sma: the object
2835 * @sops: unused
2836 * @nsops: unused
2837 * @alter: unused
2838 *
2839 * Treated as read and write in all cases.
2840 *
2841 * Returns 0 if access is allowed, error code otherwise
2842 */
2843static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2844 unsigned nsops, int alter)
2845{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002846 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002847}
2848
2849/**
2850 * smack_msg_alloc_security - Set the security blob for msg
2851 * @msq: the object
2852 *
2853 * Returns 0
2854 */
2855static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2856{
2857 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002858 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002859
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002860 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002861 return 0;
2862}
2863
2864/**
2865 * smack_msg_free_security - Clear the security blob for msg
2866 * @msq: the object
2867 *
2868 * Clears the blob pointer
2869 */
2870static void smack_msg_queue_free_security(struct msg_queue *msq)
2871{
2872 struct kern_ipc_perm *kisp = &msq->q_perm;
2873
2874 kisp->security = NULL;
2875}
2876
2877/**
2878 * smack_of_msq - the smack pointer for the msq
2879 * @msq: the object
2880 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002881 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002882 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002883static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002884{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002885 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002886}
2887
2888/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002889 * smk_curacc_msq : helper to check if current has access on msq
2890 * @msq : the msq
2891 * @access : access requested
2892 *
2893 * return 0 if current has access, error otherwise
2894 */
2895static int smk_curacc_msq(struct msg_queue *msq, int access)
2896{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002897 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002898 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002899 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002900
2901#ifdef CONFIG_AUDIT
2902 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2903 ad.a.u.ipc_id = msq->q_perm.id;
2904#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002905 rc = smk_curacc(msp, access, &ad);
2906 rc = smk_bu_current("msq", msp, access, rc);
2907 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002908}
2909
2910/**
Casey Schauflere114e472008-02-04 22:29:50 -08002911 * smack_msg_queue_associate - Smack access check for msg_queue
2912 * @msq: the object
2913 * @msqflg: access requested
2914 *
2915 * Returns 0 if current has the requested access, error code otherwise
2916 */
2917static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2918{
Casey Schauflere114e472008-02-04 22:29:50 -08002919 int may;
2920
2921 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002922 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002923}
2924
2925/**
2926 * smack_msg_queue_msgctl - Smack access check for msg_queue
2927 * @msq: the object
2928 * @cmd: what it wants to do
2929 *
2930 * Returns 0 if current has the requested access, error code otherwise
2931 */
2932static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2933{
Casey Schauflere114e472008-02-04 22:29:50 -08002934 int may;
2935
2936 switch (cmd) {
2937 case IPC_STAT:
2938 case MSG_STAT:
2939 may = MAY_READ;
2940 break;
2941 case IPC_SET:
2942 case IPC_RMID:
2943 may = MAY_READWRITE;
2944 break;
2945 case IPC_INFO:
2946 case MSG_INFO:
2947 /*
2948 * System level information
2949 */
2950 return 0;
2951 default:
2952 return -EINVAL;
2953 }
2954
Etienne Bassetecfcc532009-04-08 20:40:06 +02002955 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002956}
2957
2958/**
2959 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2960 * @msq: the object
2961 * @msg: unused
2962 * @msqflg: access requested
2963 *
2964 * Returns 0 if current has the requested access, error code otherwise
2965 */
2966static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2967 int msqflg)
2968{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002969 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002970
Etienne Bassetecfcc532009-04-08 20:40:06 +02002971 may = smack_flags_to_may(msqflg);
2972 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002973}
2974
2975/**
2976 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2977 * @msq: the object
2978 * @msg: unused
2979 * @target: unused
2980 * @type: unused
2981 * @mode: unused
2982 *
2983 * Returns 0 if current has read and write access, error code otherwise
2984 */
2985static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2986 struct task_struct *target, long type, int mode)
2987{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002988 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002989}
2990
2991/**
2992 * smack_ipc_permission - Smack access for ipc_permission()
2993 * @ipp: the object permissions
2994 * @flag: access requested
2995 *
2996 * Returns 0 if current has read and write access, error code otherwise
2997 */
2998static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2999{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003000 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003001 int may = smack_flags_to_may(flag);
3002 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003003 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003004
Etienne Bassetecfcc532009-04-08 20:40:06 +02003005#ifdef CONFIG_AUDIT
3006 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3007 ad.a.u.ipc_id = ipp->id;
3008#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003009 rc = smk_curacc(iskp, may, &ad);
3010 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003011 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003012}
3013
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003014/**
3015 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003016 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003017 * @secid: where result will be saved
3018 */
3019static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3020{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003021 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003022
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003023 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003024}
3025
Casey Schauflere114e472008-02-04 22:29:50 -08003026/**
3027 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003028 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003029 * @inode: the object
3030 *
3031 * Set the inode's security blob if it hasn't been done already.
3032 */
3033static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3034{
3035 struct super_block *sbp;
3036 struct superblock_smack *sbsp;
3037 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003038 struct smack_known *skp;
3039 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003040 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003041 char trattr[TRANS_TRUE_SIZE];
3042 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003043 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003044 struct dentry *dp;
3045
3046 if (inode == NULL)
3047 return;
3048
3049 isp = inode->i_security;
3050
3051 mutex_lock(&isp->smk_lock);
3052 /*
3053 * If the inode is already instantiated
3054 * take the quick way out
3055 */
3056 if (isp->smk_flags & SMK_INODE_INSTANT)
3057 goto unlockandout;
3058
3059 sbp = inode->i_sb;
3060 sbsp = sbp->s_security;
3061 /*
3062 * We're going to use the superblock default label
3063 * if there's no label on the file.
3064 */
3065 final = sbsp->smk_default;
3066
3067 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003068 * If this is the root inode the superblock
3069 * may be in the process of initialization.
3070 * If that is the case use the root value out
3071 * of the superblock.
3072 */
3073 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003074 switch (sbp->s_magic) {
3075 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003076 /*
3077 * The cgroup filesystem is never mounted,
3078 * so there's no opportunity to set the mount
3079 * options.
3080 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003081 sbsp->smk_root = &smack_known_star;
3082 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003083 isp->smk_inode = sbsp->smk_root;
3084 break;
3085 case TMPFS_MAGIC:
3086 /*
3087 * What about shmem/tmpfs anonymous files with dentry
3088 * obtained from d_alloc_pseudo()?
3089 */
3090 isp->smk_inode = smk_of_current();
3091 break;
3092 default:
3093 isp->smk_inode = sbsp->smk_root;
3094 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003095 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003096 isp->smk_flags |= SMK_INODE_INSTANT;
3097 goto unlockandout;
3098 }
3099
3100 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003101 * This is pretty hackish.
3102 * Casey says that we shouldn't have to do
3103 * file system specific code, but it does help
3104 * with keeping it simple.
3105 */
3106 switch (sbp->s_magic) {
3107 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003108 case PIPEFS_MAGIC:
3109 case SOCKFS_MAGIC:
3110 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003111 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003112 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003113 * that the smack file system doesn't do
3114 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003115 *
Casey Schauflere114e472008-02-04 22:29:50 -08003116 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003117 *
3118 * Socket access is controlled by the socket
3119 * structures associated with the task involved.
3120 *
3121 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003122 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003123 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003124 break;
3125 case DEVPTS_SUPER_MAGIC:
3126 /*
3127 * devpts seems content with the label of the task.
3128 * Programs that change smack have to treat the
3129 * pty with respect.
3130 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003131 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003132 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003133 case PROC_SUPER_MAGIC:
3134 /*
3135 * Casey says procfs appears not to care.
3136 * The superblock default suffices.
3137 */
3138 break;
3139 case TMPFS_MAGIC:
3140 /*
3141 * Device labels should come from the filesystem,
3142 * but watch out, because they're volitile,
3143 * getting recreated on every reboot.
3144 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003145 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003146 /*
3147 * No break.
3148 *
3149 * If a smack value has been set we want to use it,
3150 * but since tmpfs isn't giving us the opportunity
3151 * to set mount options simulate setting the
3152 * superblock default.
3153 */
3154 default:
3155 /*
3156 * This isn't an understood special case.
3157 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003158 */
3159
3160 /*
3161 * UNIX domain sockets use lower level socket data.
3162 */
3163 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003164 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003165 break;
3166 }
3167 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003168 * No xattr support means, alas, no SMACK label.
3169 * Use the aforeapplied default.
3170 * It would be curious if the label of the task
3171 * does not match that assigned.
3172 */
3173 if (inode->i_op->getxattr == NULL)
3174 break;
3175 /*
3176 * Get the dentry for xattr.
3177 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003178 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003179 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3180 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003181 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003182
3183 /*
3184 * Transmuting directory
3185 */
3186 if (S_ISDIR(inode->i_mode)) {
3187 /*
3188 * If this is a new directory and the label was
3189 * transmuted when the inode was initialized
3190 * set the transmute attribute on the directory
3191 * and mark the inode.
3192 *
3193 * If there is a transmute attribute on the
3194 * directory mark the inode.
3195 */
3196 if (isp->smk_flags & SMK_INODE_CHANGED) {
3197 isp->smk_flags &= ~SMK_INODE_CHANGED;
3198 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003199 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003200 TRANS_TRUE, TRANS_TRUE_SIZE,
3201 0);
3202 } else {
3203 rc = inode->i_op->getxattr(dp,
3204 XATTR_NAME_SMACKTRANSMUTE, trattr,
3205 TRANS_TRUE_SIZE);
3206 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3207 TRANS_TRUE_SIZE) != 0)
3208 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003209 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003210 if (rc >= 0)
3211 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003212 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003213 /*
3214 * Don't let the exec or mmap label be "*" or "@".
3215 */
3216 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3217 if (skp == &smack_known_star || skp == &smack_known_web)
3218 skp = NULL;
3219 isp->smk_task = skp;
3220 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3221 if (skp == &smack_known_star || skp == &smack_known_web)
3222 skp = NULL;
3223 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003224
Casey Schauflere114e472008-02-04 22:29:50 -08003225 dput(dp);
3226 break;
3227 }
3228
3229 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003230 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003231 else
3232 isp->smk_inode = final;
3233
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003234 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003235
3236unlockandout:
3237 mutex_unlock(&isp->smk_lock);
3238 return;
3239}
3240
3241/**
3242 * smack_getprocattr - Smack process attribute access
3243 * @p: the object task
3244 * @name: the name of the attribute in /proc/.../attr
3245 * @value: where to put the result
3246 *
3247 * Places a copy of the task Smack into value
3248 *
3249 * Returns the length of the smack label or an error code
3250 */
3251static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3252{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003253 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003254 char *cp;
3255 int slen;
3256
3257 if (strcmp(name, "current") != 0)
3258 return -EINVAL;
3259
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003260 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003261 if (cp == NULL)
3262 return -ENOMEM;
3263
3264 slen = strlen(cp);
3265 *value = cp;
3266 return slen;
3267}
3268
3269/**
3270 * smack_setprocattr - Smack process attribute setting
3271 * @p: the object task
3272 * @name: the name of the attribute in /proc/.../attr
3273 * @value: the value to set
3274 * @size: the size of the value
3275 *
3276 * Sets the Smack value of the task. Only setting self
3277 * is permitted and only with privilege
3278 *
3279 * Returns the length of the smack label or an error code
3280 */
3281static int smack_setprocattr(struct task_struct *p, char *name,
3282 void *value, size_t size)
3283{
Casey Schaufler676dac42010-12-02 06:43:39 -08003284 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003285 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003286 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003287
Casey Schauflere114e472008-02-04 22:29:50 -08003288 /*
3289 * Changing another process' Smack value is too dangerous
3290 * and supports no sane use case.
3291 */
3292 if (p != current)
3293 return -EPERM;
3294
Casey Schaufler1880eff2012-06-05 15:28:30 -07003295 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003296 return -EPERM;
3297
Casey Schauflerf7112e62012-05-06 15:22:02 -07003298 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003299 return -EINVAL;
3300
3301 if (strcmp(name, "current") != 0)
3302 return -EINVAL;
3303
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003304 skp = smk_import_entry(value, size);
3305 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003306 return -EINVAL;
3307
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003308 /*
3309 * No process is ever allowed the web ("@") label.
3310 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003311 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003312 return -EPERM;
3313
David Howellsd84f4f92008-11-14 10:39:23 +11003314 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003315 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003316 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003317
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003318 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003319 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003320
David Howellsd84f4f92008-11-14 10:39:23 +11003321 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003322 return size;
3323}
3324
3325/**
3326 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003327 * @sock: one sock
3328 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003329 * @newsk: unused
3330 *
3331 * Return 0 if a subject with the smack of sock could access
3332 * an object with the smack of other, otherwise an error code
3333 */
David S. Miller3610cda2011-01-05 15:38:53 -08003334static int smack_unix_stream_connect(struct sock *sock,
3335 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003336{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003337 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003338 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003339 struct socket_smack *ssp = sock->sk_security;
3340 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003341 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003342 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003343 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003344#ifdef CONFIG_AUDIT
3345 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003346#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003347
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003348 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3349 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003350 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003351#ifdef CONFIG_AUDIT
3352 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3353 smk_ad_setfield_u_net_sk(&ad, other);
3354#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003355 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3356 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003357 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003358 okp = osp->smk_out;
3359 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003360 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003361 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003362 MAY_WRITE, rc);
3363 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003364 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003365
Casey Schaufler975d5e52011-09-26 14:43:39 -07003366 /*
3367 * Cross reference the peer labels for SO_PEERSEC.
3368 */
3369 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003370 nsp->smk_packet = ssp->smk_out;
3371 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003372 }
3373
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003374 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003375}
3376
3377/**
3378 * smack_unix_may_send - Smack access on UDS
3379 * @sock: one socket
3380 * @other: the other socket
3381 *
3382 * Return 0 if a subject with the smack of sock could access
3383 * an object with the smack of other, otherwise an error code
3384 */
3385static int smack_unix_may_send(struct socket *sock, struct socket *other)
3386{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003387 struct socket_smack *ssp = sock->sk->sk_security;
3388 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003389 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003390 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003391
Kees Cook923e9a12012-04-10 13:26:44 -07003392#ifdef CONFIG_AUDIT
3393 struct lsm_network_audit net;
3394
Eric Paris48c62af2012-04-02 13:15:44 -04003395 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003396 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003397#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003398
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003399 if (smack_privileged(CAP_MAC_OVERRIDE))
3400 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003401
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003402 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3403 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003404 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003405}
3406
3407/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003408 * smack_socket_sendmsg - Smack check based on destination host
3409 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003410 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003411 * @size: the size of the message
3412 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003413 * Return 0 if the current subject can write to the destination host.
3414 * For IPv4 this is only a question if the destination is a single label host.
3415 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003416 */
3417static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3418 int size)
3419{
3420 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003421#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003422 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003423#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003424 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003425
3426 /*
3427 * Perfectly reasonable for this to be NULL
3428 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003429 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003430 return 0;
3431
Casey Schauflerc6739442013-05-22 18:42:56 -07003432 switch (sip->sin_family) {
3433 case AF_INET:
3434 rc = smack_netlabel_send(sock->sk, sip);
3435 break;
3436 case AF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003437#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07003438 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003439#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003440 break;
3441 }
3442 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003443}
3444
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003445/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003446 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003447 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003448 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003449 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003450 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003451 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003452static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3453 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003454{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003455 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003456 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003457 int acat;
3458 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003459
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003460 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003461 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003462 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003463 * If there are flags but no level netlabel isn't
3464 * behaving the way we expect it to.
3465 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003466 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003467 * Without guidance regarding the smack value
3468 * for the packet fall back on the network
3469 * ambient value.
3470 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003471 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003472 list_for_each_entry(skp, &smack_known_list, list) {
3473 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003474 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003475 /*
3476 * Compare the catsets. Use the netlbl APIs.
3477 */
3478 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3479 if ((skp->smk_netlabel.flags &
3480 NETLBL_SECATTR_MLS_CAT) == 0)
3481 found = 1;
3482 break;
3483 }
3484 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003485 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3486 acat + 1);
3487 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003488 skp->smk_netlabel.attr.mls.cat,
3489 kcat + 1);
3490 if (acat < 0 || kcat < 0)
3491 break;
3492 }
3493 if (acat == kcat) {
3494 found = 1;
3495 break;
3496 }
Casey Schauflere114e472008-02-04 22:29:50 -08003497 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003498 rcu_read_unlock();
3499
3500 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003501 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003502
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003503 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003504 return &smack_known_web;
3505 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003506 }
3507 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3508 /*
3509 * Looks like a fallback, which gives us a secid.
3510 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003511 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003512 /*
3513 * This has got to be a bug because it is
3514 * impossible to specify a fallback without
3515 * specifying the label, which will ensure
3516 * it has a secid, and the only way to get a
3517 * secid is from a fallback.
3518 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003519 BUG_ON(skp == NULL);
3520 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003521 }
3522 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003523 * Without guidance regarding the smack value
3524 * for the packet fall back on the network
3525 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003526 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003527 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003528}
3529
Casey Schaufler69f287a2014-12-12 17:08:40 -08003530#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003531static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003532{
Casey Schauflerc6739442013-05-22 18:42:56 -07003533 u8 nexthdr;
3534 int offset;
3535 int proto = -EINVAL;
3536 struct ipv6hdr _ipv6h;
3537 struct ipv6hdr *ip6;
3538 __be16 frag_off;
3539 struct tcphdr _tcph, *th;
3540 struct udphdr _udph, *uh;
3541 struct dccp_hdr _dccph, *dh;
3542
3543 sip->sin6_port = 0;
3544
3545 offset = skb_network_offset(skb);
3546 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3547 if (ip6 == NULL)
3548 return -EINVAL;
3549 sip->sin6_addr = ip6->saddr;
3550
3551 nexthdr = ip6->nexthdr;
3552 offset += sizeof(_ipv6h);
3553 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3554 if (offset < 0)
3555 return -EINVAL;
3556
3557 proto = nexthdr;
3558 switch (proto) {
3559 case IPPROTO_TCP:
3560 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3561 if (th != NULL)
3562 sip->sin6_port = th->source;
3563 break;
3564 case IPPROTO_UDP:
3565 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3566 if (uh != NULL)
3567 sip->sin6_port = uh->source;
3568 break;
3569 case IPPROTO_DCCP:
3570 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3571 if (dh != NULL)
3572 sip->sin6_port = dh->dccph_sport;
3573 break;
3574 }
3575 return proto;
3576}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003577#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003578
Casey Schauflere114e472008-02-04 22:29:50 -08003579/**
3580 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3581 * @sk: socket
3582 * @skb: packet
3583 *
3584 * Returns 0 if the packet should be delivered, an error code otherwise
3585 */
3586static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3587{
3588 struct netlbl_lsm_secattr secattr;
3589 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003590 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003591 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003592 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003593#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003594 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003595#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003596#if IS_ENABLED(CONFIG_IPV6)
3597 struct sockaddr_in6 sadd;
3598 int proto;
3599#endif /* CONFIG_IPV6 */
3600
Casey Schauflerc6739442013-05-22 18:42:56 -07003601 switch (sk->sk_family) {
3602 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003603#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3604 /*
3605 * If there is a secmark use it rather than the CIPSO label.
3606 * If there is no secmark fall back to CIPSO.
3607 * The secmark is assumed to reflect policy better.
3608 */
3609 if (skb && skb->secmark != 0) {
3610 skp = smack_from_secid(skb->secmark);
3611 goto access_check;
3612 }
3613#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003614 /*
3615 * Translate what netlabel gave us.
3616 */
3617 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003618
Casey Schauflerc6739442013-05-22 18:42:56 -07003619 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3620 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003621 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003622 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003623 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003624
Casey Schauflerc6739442013-05-22 18:42:56 -07003625 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003626
Casey Schaufler69f287a2014-12-12 17:08:40 -08003627#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3628access_check:
3629#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003630#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003631 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3632 ad.a.u.net->family = sk->sk_family;
3633 ad.a.u.net->netif = skb->skb_iif;
3634 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003635#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003636 /*
3637 * Receiving a packet requires that the other end
3638 * be able to write here. Read access is not required.
3639 * This is the simplist possible security model
3640 * for networking.
3641 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003642 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3643 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003644 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003645 if (rc != 0)
3646 netlbl_skbuff_err(skb, rc, 0);
3647 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003648#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003649 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003650 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3651 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3652 break;
3653#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3654 if (skb && skb->secmark != 0)
3655 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003656 else
Casey Schaufler69f287a2014-12-12 17:08:40 -08003657 skp = smack_net_ambient;
3658#ifdef CONFIG_AUDIT
3659 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3660 ad.a.u.net->family = sk->sk_family;
3661 ad.a.u.net->netif = skb->skb_iif;
3662 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3663#endif /* CONFIG_AUDIT */
3664 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3665 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3666 MAY_WRITE, rc);
3667#else /* CONFIG_SECURITY_SMACK_NETFILTER */
3668 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3669#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003670 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003671#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003672 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003673
Paul Moorea8134292008-10-10 10:16:31 -04003674 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003675}
3676
3677/**
3678 * smack_socket_getpeersec_stream - pull in packet label
3679 * @sock: the socket
3680 * @optval: user's destination
3681 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003682 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003683 *
3684 * returns zero on success, an error code otherwise
3685 */
3686static int smack_socket_getpeersec_stream(struct socket *sock,
3687 char __user *optval,
3688 int __user *optlen, unsigned len)
3689{
3690 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003691 char *rcp = "";
3692 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003693 int rc = 0;
3694
3695 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003696 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003697 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003698 slen = strlen(rcp) + 1;
3699 }
Casey Schauflere114e472008-02-04 22:29:50 -08003700
3701 if (slen > len)
3702 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003703 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003704 rc = -EFAULT;
3705
3706 if (put_user(slen, optlen) != 0)
3707 rc = -EFAULT;
3708
3709 return rc;
3710}
3711
3712
3713/**
3714 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003715 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003716 * @skb: packet data
3717 * @secid: pointer to where to put the secid of the packet
3718 *
3719 * Sets the netlabel socket state on sk from parent
3720 */
3721static int smack_socket_getpeersec_dgram(struct socket *sock,
3722 struct sk_buff *skb, u32 *secid)
3723
3724{
3725 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003726 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003727 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003728 int family = PF_UNSPEC;
3729 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003730 int rc;
3731
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003732 if (skb != NULL) {
3733 if (skb->protocol == htons(ETH_P_IP))
3734 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003735#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003736 else if (skb->protocol == htons(ETH_P_IPV6))
3737 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003738#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003739 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003740 if (family == PF_UNSPEC && sock != NULL)
3741 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003742
Casey Schaufler69f287a2014-12-12 17:08:40 -08003743 switch (family) {
3744 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003745 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003746 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003747 break;
3748 case PF_INET:
3749#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3750 s = skb->secmark;
3751 if (s != 0)
3752 break;
3753#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003754 /*
3755 * Translate what netlabel gave us.
3756 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003757 if (sock != NULL && sock->sk != NULL)
3758 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003759 netlbl_secattr_init(&secattr);
3760 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3761 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003762 skp = smack_from_secattr(&secattr, ssp);
3763 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003764 }
3765 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003766 break;
3767#if IS_ENABLED(CONFIG_IPV6)
3768 case PF_INET6:
3769#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3770 s = skb->secmark;
3771#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3772 break;
3773#endif /* CONFIG_IPV6 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003774 }
3775 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003776 if (s == 0)
3777 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003778 return 0;
3779}
3780
3781/**
Paul Moore07feee82009-03-27 17:10:54 -04003782 * smack_sock_graft - Initialize a newly created socket with an existing sock
3783 * @sk: child sock
3784 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003785 *
Paul Moore07feee82009-03-27 17:10:54 -04003786 * Set the smk_{in,out} state of an existing sock based on the process that
3787 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003788 */
3789static void smack_sock_graft(struct sock *sk, struct socket *parent)
3790{
3791 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003792 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003793
Paul Moore07feee82009-03-27 17:10:54 -04003794 if (sk == NULL ||
3795 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003796 return;
3797
3798 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003799 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003800 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003801 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003802}
3803
3804/**
3805 * smack_inet_conn_request - Smack access check on connect
3806 * @sk: socket involved
3807 * @skb: packet
3808 * @req: unused
3809 *
3810 * Returns 0 if a task with the packet label could write to
3811 * the socket, otherwise an error code
3812 */
3813static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3814 struct request_sock *req)
3815{
Paul Moore07feee82009-03-27 17:10:54 -04003816 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003817 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003818 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003819 struct netlbl_lsm_secattr secattr;
3820 struct sockaddr_in addr;
3821 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003822 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003823 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003824 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003825#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003826 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003827#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003828
Casey Schaufler69f287a2014-12-12 17:08:40 -08003829#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003830 if (family == PF_INET6) {
3831 /*
3832 * Handle mapped IPv4 packets arriving
3833 * via IPv6 sockets. Don't set up netlabel
3834 * processing on IPv6.
3835 */
3836 if (skb->protocol == htons(ETH_P_IP))
3837 family = PF_INET;
3838 else
3839 return 0;
3840 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003841#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003842
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003843#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3844 /*
3845 * If there is a secmark use it rather than the CIPSO label.
3846 * If there is no secmark fall back to CIPSO.
3847 * The secmark is assumed to reflect policy better.
3848 */
3849 if (skb && skb->secmark != 0) {
3850 skp = smack_from_secid(skb->secmark);
3851 goto access_check;
3852 }
3853#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3854
Paul Moore07feee82009-03-27 17:10:54 -04003855 netlbl_secattr_init(&secattr);
3856 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003857 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003858 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003859 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003860 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003861 netlbl_secattr_destroy(&secattr);
3862
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003863#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3864access_check:
3865#endif
3866
Etienne Bassetecfcc532009-04-08 20:40:06 +02003867#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003868 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3869 ad.a.u.net->family = family;
3870 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003871 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3872#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003873 /*
Paul Moore07feee82009-03-27 17:10:54 -04003874 * Receiving a packet requires that the other end be able to write
3875 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003876 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003877 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3878 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003879 if (rc != 0)
3880 return rc;
3881
3882 /*
3883 * Save the peer's label in the request_sock so we can later setup
3884 * smk_packet in the child socket so that SO_PEERCRED can report it.
3885 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003886 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003887
3888 /*
3889 * We need to decide if we want to label the incoming connection here
3890 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003891 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003892 */
3893 hdr = ip_hdr(skb);
3894 addr.sin_addr.s_addr = hdr->saddr;
3895 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003896 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003897 rcu_read_unlock();
3898
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003899 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003900 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003901 else
Paul Moore07feee82009-03-27 17:10:54 -04003902 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003903
3904 return rc;
3905}
3906
Paul Moore07feee82009-03-27 17:10:54 -04003907/**
3908 * smack_inet_csk_clone - Copy the connection information to the new socket
3909 * @sk: the new socket
3910 * @req: the connection's request_sock
3911 *
3912 * Transfer the connection's peer label to the newly created socket.
3913 */
3914static void smack_inet_csk_clone(struct sock *sk,
3915 const struct request_sock *req)
3916{
3917 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003918 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003919
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003920 if (req->peer_secid != 0) {
3921 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003922 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003923 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003924 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003925}
3926
Casey Schauflere114e472008-02-04 22:29:50 -08003927/*
3928 * Key management security hooks
3929 *
3930 * Casey has not tested key support very heavily.
3931 * The permission check is most likely too restrictive.
3932 * If you care about keys please have a look.
3933 */
3934#ifdef CONFIG_KEYS
3935
3936/**
3937 * smack_key_alloc - Set the key security blob
3938 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003939 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003940 * @flags: unused
3941 *
3942 * No allocation required
3943 *
3944 * Returns 0
3945 */
David Howellsd84f4f92008-11-14 10:39:23 +11003946static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003947 unsigned long flags)
3948{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003949 struct smack_known *skp = smk_of_task(cred->security);
3950
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003951 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003952 return 0;
3953}
3954
3955/**
3956 * smack_key_free - Clear the key security blob
3957 * @key: the object
3958 *
3959 * Clear the blob pointer
3960 */
3961static void smack_key_free(struct key *key)
3962{
3963 key->security = NULL;
3964}
3965
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003966/**
Casey Schauflere114e472008-02-04 22:29:50 -08003967 * smack_key_permission - Smack access on a key
3968 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003969 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003970 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08003971 *
3972 * Return 0 if the task has read and write to the object,
3973 * an error code otherwise
3974 */
3975static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00003976 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003977{
3978 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003979 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003980 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003981 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07003982 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003983
3984 keyp = key_ref_to_ptr(key_ref);
3985 if (keyp == NULL)
3986 return -EINVAL;
3987 /*
3988 * If the key hasn't been initialized give it access so that
3989 * it may do so.
3990 */
3991 if (keyp->security == NULL)
3992 return 0;
3993 /*
3994 * This should not occur
3995 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003996 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003997 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003998#ifdef CONFIG_AUDIT
3999 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4000 ad.a.u.key_struct.key = keyp->serial;
4001 ad.a.u.key_struct.key_desc = keyp->description;
4002#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004003 if (perm & KEY_NEED_READ)
4004 request = MAY_READ;
4005 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4006 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004007 rc = smk_access(tkp, keyp->security, request, &ad);
4008 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4009 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004010}
José Bollo7fc5f362015-02-17 15:41:22 +01004011
4012/*
4013 * smack_key_getsecurity - Smack label tagging the key
4014 * @key points to the key to be queried
4015 * @_buffer points to a pointer that should be set to point to the
4016 * resulting string (if no label or an error occurs).
4017 * Return the length of the string (including terminating NUL) or -ve if
4018 * an error.
4019 * May also return 0 (and a NULL buffer pointer) if there is no label.
4020 */
4021static int smack_key_getsecurity(struct key *key, char **_buffer)
4022{
4023 struct smack_known *skp = key->security;
4024 size_t length;
4025 char *copy;
4026
4027 if (key->security == NULL) {
4028 *_buffer = NULL;
4029 return 0;
4030 }
4031
4032 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4033 if (copy == NULL)
4034 return -ENOMEM;
4035 length = strlen(copy) + 1;
4036
4037 *_buffer = copy;
4038 return length;
4039}
4040
Casey Schauflere114e472008-02-04 22:29:50 -08004041#endif /* CONFIG_KEYS */
4042
4043/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004044 * Smack Audit hooks
4045 *
4046 * Audit requires a unique representation of each Smack specific
4047 * rule. This unique representation is used to distinguish the
4048 * object to be audited from remaining kernel objects and also
4049 * works as a glue between the audit hooks.
4050 *
4051 * Since repository entries are added but never deleted, we'll use
4052 * the smack_known label address related to the given audit rule as
4053 * the needed unique representation. This also better fits the smack
4054 * model where nearly everything is a label.
4055 */
4056#ifdef CONFIG_AUDIT
4057
4058/**
4059 * smack_audit_rule_init - Initialize a smack audit rule
4060 * @field: audit rule fields given from user-space (audit.h)
4061 * @op: required testing operator (=, !=, >, <, ...)
4062 * @rulestr: smack label to be audited
4063 * @vrule: pointer to save our own audit rule representation
4064 *
4065 * Prepare to audit cases where (@field @op @rulestr) is true.
4066 * The label to be audited is created if necessay.
4067 */
4068static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4069{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004070 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004071 char **rule = (char **)vrule;
4072 *rule = NULL;
4073
4074 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4075 return -EINVAL;
4076
Al Viro5af75d82008-12-16 05:59:26 -05004077 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004078 return -EINVAL;
4079
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004080 skp = smk_import_entry(rulestr, 0);
4081 if (skp)
4082 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004083
4084 return 0;
4085}
4086
4087/**
4088 * smack_audit_rule_known - Distinguish Smack audit rules
4089 * @krule: rule of interest, in Audit kernel representation format
4090 *
4091 * This is used to filter Smack rules from remaining Audit ones.
4092 * If it's proved that this rule belongs to us, the
4093 * audit_rule_match hook will be called to do the final judgement.
4094 */
4095static int smack_audit_rule_known(struct audit_krule *krule)
4096{
4097 struct audit_field *f;
4098 int i;
4099
4100 for (i = 0; i < krule->field_count; i++) {
4101 f = &krule->fields[i];
4102
4103 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4104 return 1;
4105 }
4106
4107 return 0;
4108}
4109
4110/**
4111 * smack_audit_rule_match - Audit given object ?
4112 * @secid: security id for identifying the object to test
4113 * @field: audit rule flags given from user-space
4114 * @op: required testing operator
4115 * @vrule: smack internal rule presentation
4116 * @actx: audit context associated with the check
4117 *
4118 * The core Audit hook. It's used to take the decision of
4119 * whether to audit or not to audit a given object.
4120 */
4121static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4122 struct audit_context *actx)
4123{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004124 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004125 char *rule = vrule;
4126
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004127 if (unlikely(!rule)) {
4128 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004129 return -ENOENT;
4130 }
4131
4132 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4133 return 0;
4134
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004135 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004136
4137 /*
4138 * No need to do string comparisons. If a match occurs,
4139 * both pointers will point to the same smack_known
4140 * label.
4141 */
Al Viro5af75d82008-12-16 05:59:26 -05004142 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004143 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004144 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004145 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004146
4147 return 0;
4148}
4149
4150/**
4151 * smack_audit_rule_free - free smack rule representation
4152 * @vrule: rule to be freed.
4153 *
4154 * No memory was allocated.
4155 */
4156static void smack_audit_rule_free(void *vrule)
4157{
4158 /* No-op */
4159}
4160
4161#endif /* CONFIG_AUDIT */
4162
Randy Dunlap251a2a92009-02-18 11:42:33 -08004163/**
David Quigley746df9b2013-05-22 12:50:35 -04004164 * smack_ismaclabel - check if xattr @name references a smack MAC label
4165 * @name: Full xattr name to check.
4166 */
4167static int smack_ismaclabel(const char *name)
4168{
4169 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4170}
4171
4172
4173/**
Casey Schauflere114e472008-02-04 22:29:50 -08004174 * smack_secid_to_secctx - return the smack label for a secid
4175 * @secid: incoming integer
4176 * @secdata: destination
4177 * @seclen: how long it is
4178 *
4179 * Exists for networking code.
4180 */
4181static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4182{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004183 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004184
Eric Parisd5630b92010-10-13 16:24:48 -04004185 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004186 *secdata = skp->smk_known;
4187 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004188 return 0;
4189}
4190
Randy Dunlap251a2a92009-02-18 11:42:33 -08004191/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004192 * smack_secctx_to_secid - return the secid for a smack label
4193 * @secdata: smack label
4194 * @seclen: how long result is
4195 * @secid: outgoing integer
4196 *
4197 * Exists for audit and networking code.
4198 */
David Howellse52c17642008-04-29 20:52:51 +01004199static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004200{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004201 struct smack_known *skp = smk_find_entry(secdata);
4202
4203 if (skp)
4204 *secid = skp->smk_secid;
4205 else
4206 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004207 return 0;
4208}
4209
Randy Dunlap251a2a92009-02-18 11:42:33 -08004210/**
Casey Schauflere114e472008-02-04 22:29:50 -08004211 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004212 * @secdata: unused
4213 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004214 *
4215 * Exists to make sure nothing gets done, and properly
4216 */
4217static void smack_release_secctx(char *secdata, u32 seclen)
4218{
4219}
4220
David P. Quigley1ee65e32009-09-03 14:25:57 -04004221static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4222{
4223 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4224}
4225
4226static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4227{
4228 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4229}
4230
4231static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4232{
4233 int len = 0;
4234 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4235
4236 if (len < 0)
4237 return len;
4238 *ctxlen = len;
4239 return 0;
4240}
4241
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004242struct security_hook_list smack_hooks[] = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004243 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4244 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4245 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004246
Casey Schauflere20b0432015-05-02 15:11:36 -07004247 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4248 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4249 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4250 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4251 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Casey Schauflere114e472008-02-04 22:29:50 -08004252
Casey Schauflere20b0432015-05-02 15:11:36 -07004253 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4254 LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
4255 LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004256
Casey Schauflere20b0432015-05-02 15:11:36 -07004257 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4258 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4259 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4260 LSM_HOOK_INIT(inode_link, smack_inode_link),
4261 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4262 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4263 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4264 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4265 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4266 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4267 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4268 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4269 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4270 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4271 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4272 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4273 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4274 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004275
Casey Schauflere20b0432015-05-02 15:11:36 -07004276 LSM_HOOK_INIT(file_permission, smack_file_permission),
4277 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4278 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4279 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4280 LSM_HOOK_INIT(file_lock, smack_file_lock),
4281 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4282 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4283 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4284 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4285 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4286 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004287
Casey Schauflere20b0432015-05-02 15:11:36 -07004288 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004289
Casey Schauflere20b0432015-05-02 15:11:36 -07004290 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4291 LSM_HOOK_INIT(cred_free, smack_cred_free),
4292 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4293 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4294 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4295 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4296 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4297 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4298 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4299 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4300 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4301 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4302 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4303 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4304 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4305 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4306 LSM_HOOK_INIT(task_kill, smack_task_kill),
4307 LSM_HOOK_INIT(task_wait, smack_task_wait),
4308 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004309
Casey Schauflere20b0432015-05-02 15:11:36 -07004310 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4311 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004312
Casey Schauflere20b0432015-05-02 15:11:36 -07004313 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4314 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004315
Casey Schauflere20b0432015-05-02 15:11:36 -07004316 LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
4317 LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
4318 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4319 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4320 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4321 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004322
Casey Schauflere20b0432015-05-02 15:11:36 -07004323 LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
4324 LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
4325 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4326 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4327 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004328
Casey Schauflere20b0432015-05-02 15:11:36 -07004329 LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
4330 LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
4331 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4332 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4333 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004334
Casey Schauflere20b0432015-05-02 15:11:36 -07004335 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004336
Casey Schauflere20b0432015-05-02 15:11:36 -07004337 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4338 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004339
Casey Schauflere20b0432015-05-02 15:11:36 -07004340 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4341 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004342
Casey Schauflere20b0432015-05-02 15:11:36 -07004343 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Casey Schaufler69f287a2014-12-12 17:08:40 -08004344#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schauflere20b0432015-05-02 15:11:36 -07004345 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler69f287a2014-12-12 17:08:40 -08004346#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflere20b0432015-05-02 15:11:36 -07004347 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4348 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4349 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4350 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4351 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4352 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4353 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4354 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4355 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4356 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004357
Casey Schauflere114e472008-02-04 22:29:50 -08004358 /* key management security hooks */
4359#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004360 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4361 LSM_HOOK_INIT(key_free, smack_key_free),
4362 LSM_HOOK_INIT(key_permission, smack_key_permission),
4363 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004364#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004365
4366 /* Audit hooks */
4367#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004368 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4369 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4370 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4371 LSM_HOOK_INIT(audit_rule_free, smack_audit_rule_free),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004372#endif /* CONFIG_AUDIT */
4373
Casey Schauflere20b0432015-05-02 15:11:36 -07004374 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4375 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4376 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4377 LSM_HOOK_INIT(release_secctx, smack_release_secctx),
4378 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4379 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4380 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflere114e472008-02-04 22:29:50 -08004381};
4382
Etienne Basset7198e2e2009-03-24 20:53:24 +01004383
Casey Schaufler86812bb2012-04-17 18:55:46 -07004384static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004385{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004386 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004387 * Initialize rule list locks
4388 */
4389 mutex_init(&smack_known_huh.smk_rules_lock);
4390 mutex_init(&smack_known_hat.smk_rules_lock);
4391 mutex_init(&smack_known_floor.smk_rules_lock);
4392 mutex_init(&smack_known_star.smk_rules_lock);
4393 mutex_init(&smack_known_invalid.smk_rules_lock);
4394 mutex_init(&smack_known_web.smk_rules_lock);
4395 /*
4396 * Initialize rule lists
4397 */
4398 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4399 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4400 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4401 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4402 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4403 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4404 /*
4405 * Create the known labels list
4406 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004407 smk_insert_entry(&smack_known_huh);
4408 smk_insert_entry(&smack_known_hat);
4409 smk_insert_entry(&smack_known_star);
4410 smk_insert_entry(&smack_known_floor);
4411 smk_insert_entry(&smack_known_invalid);
4412 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004413}
4414
Casey Schauflere114e472008-02-04 22:29:50 -08004415/**
4416 * smack_init - initialize the smack system
4417 *
4418 * Returns 0
4419 */
4420static __init int smack_init(void)
4421{
David Howellsd84f4f92008-11-14 10:39:23 +11004422 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004423 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004424
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004425 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004426 return 0;
4427
Casey Schaufler69f287a2014-12-12 17:08:40 -08004428 smack_enabled = 1;
4429
Rohit1a5b4722014-10-15 17:40:41 +05304430 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4431 if (!smack_inode_cache)
4432 return -ENOMEM;
4433
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004434 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4435 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304436 if (tsp == NULL) {
4437 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004438 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304439 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004440
Casey Schauflere114e472008-02-04 22:29:50 -08004441 printk(KERN_INFO "Smack: Initializing.\n");
4442
4443 /*
4444 * Set the security state for the initial task.
4445 */
David Howellsd84f4f92008-11-14 10:39:23 +11004446 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004447 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004448
Casey Schaufler86812bb2012-04-17 18:55:46 -07004449 /* initialize the smack_known_list */
4450 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004451
4452 /*
4453 * Register with LSM
4454 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004455 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
Casey Schauflere114e472008-02-04 22:29:50 -08004456
4457 return 0;
4458}
4459
4460/*
4461 * Smack requires early initialization in order to label
4462 * all processes and objects when they are created.
4463 */
4464security_initcall(smack_init);