blob: 6f3c7d866d0414e0e384df718d87e45306e9eba7 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
19 */
20
21#include <linux/xattr.h>
22#include <linux/pagemap.h>
23#include <linux/mount.h>
24#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070030#include <linux/dccp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080032#include <linux/mutex.h>
33#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080034#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070035#include <net/ip.h>
36#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100037#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070038#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050039#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030040#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000041#include <linux/msg.h>
42#include <linux/shm.h>
43#include <linux/binfmts.h>
Casey Schauflere114e472008-02-04 22:29:50 -080044#include "smack.h"
45
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020046#define TRANS_TRUE "TRUE"
47#define TRANS_TRUE_SIZE 4
48
Casey Schauflerc6739442013-05-22 18:42:56 -070049#define SMK_CONNECTING 0
50#define SMK_RECEIVING 1
51#define SMK_SENDING 2
52
Casey Schaufler69f287a2014-12-12 17:08:40 -080053#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -070054LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler69f287a2014-12-12 17:08:40 -080055#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Rohit1a5b4722014-10-15 17:40:41 +053056static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080057int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070058
Casey Schauflerd166c802014-08-27 14:51:27 -070059#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070060static char *smk_bu_mess[] = {
61 "Bringup Error", /* Unused */
62 "Bringup", /* SMACK_BRINGUP_ALLOW */
63 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
64 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
65};
66
Casey Schauflerd166c802014-08-27 14:51:27 -070067static void smk_bu_mode(int mode, char *s)
68{
69 int i = 0;
70
71 if (mode & MAY_READ)
72 s[i++] = 'r';
73 if (mode & MAY_WRITE)
74 s[i++] = 'w';
75 if (mode & MAY_EXEC)
76 s[i++] = 'x';
77 if (mode & MAY_APPEND)
78 s[i++] = 'a';
79 if (mode & MAY_TRANSMUTE)
80 s[i++] = 't';
81 if (mode & MAY_LOCK)
82 s[i++] = 'l';
83 if (i == 0)
84 s[i++] = '-';
85 s[i] = '\0';
86}
87#endif
88
89#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020090static int smk_bu_note(char *note, struct smack_known *sskp,
91 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -070092{
93 char acc[SMK_NUM_ACCESS_TYPE + 1];
94
95 if (rc <= 0)
96 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -070097 if (rc > SMACK_UNCONFINED_OBJECT)
98 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -070099
100 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700101 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200102 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700103 return 0;
104}
105#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200106#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700107#endif
108
109#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200110static int smk_bu_current(char *note, struct smack_known *oskp,
111 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700112{
113 struct task_smack *tsp = current_security();
114 char acc[SMK_NUM_ACCESS_TYPE + 1];
115
116 if (rc <= 0)
117 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700118 if (rc > SMACK_UNCONFINED_OBJECT)
119 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700120
121 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700122 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200123 tsp->smk_task->smk_known, oskp->smk_known,
124 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700125 return 0;
126}
127#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200128#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700129#endif
130
131#ifdef CONFIG_SECURITY_SMACK_BRINGUP
132static int smk_bu_task(struct task_struct *otp, int mode, int rc)
133{
134 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300135 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700136 char acc[SMK_NUM_ACCESS_TYPE + 1];
137
138 if (rc <= 0)
139 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700140 if (rc > SMACK_UNCONFINED_OBJECT)
141 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700142
143 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700144 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300145 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700146 current->comm, otp->comm);
147 return 0;
148}
149#else
150#define smk_bu_task(otp, mode, RC) (RC)
151#endif
152
153#ifdef CONFIG_SECURITY_SMACK_BRINGUP
154static int smk_bu_inode(struct inode *inode, int mode, int rc)
155{
156 struct task_smack *tsp = current_security();
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700157 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700158 char acc[SMK_NUM_ACCESS_TYPE + 1];
159
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700160 if (isp->smk_flags & SMK_INODE_IMPURE)
161 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
162 inode->i_sb->s_id, inode->i_ino, current->comm);
163
Casey Schauflerd166c802014-08-27 14:51:27 -0700164 if (rc <= 0)
165 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700166 if (rc > SMACK_UNCONFINED_OBJECT)
167 rc = 0;
168 if (rc == SMACK_UNCONFINED_SUBJECT &&
169 (mode & (MAY_WRITE | MAY_APPEND)))
170 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700171
172 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700173
174 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
175 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700176 inode->i_sb->s_id, inode->i_ino, current->comm);
177 return 0;
178}
179#else
180#define smk_bu_inode(inode, mode, RC) (RC)
181#endif
182
183#ifdef CONFIG_SECURITY_SMACK_BRINGUP
184static int smk_bu_file(struct file *file, int mode, int rc)
185{
186 struct task_smack *tsp = current_security();
187 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800188 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700189 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700190 char acc[SMK_NUM_ACCESS_TYPE + 1];
191
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700192 if (isp->smk_flags & SMK_INODE_IMPURE)
193 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
194 inode->i_sb->s_id, inode->i_ino, current->comm);
195
Casey Schauflerd166c802014-08-27 14:51:27 -0700196 if (rc <= 0)
197 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700198 if (rc > SMACK_UNCONFINED_OBJECT)
199 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700200
201 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700202 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800203 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400204 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700205 current->comm);
206 return 0;
207}
208#else
209#define smk_bu_file(file, mode, RC) (RC)
210#endif
211
212#ifdef CONFIG_SECURITY_SMACK_BRINGUP
213static int smk_bu_credfile(const struct cred *cred, struct file *file,
214 int mode, int rc)
215{
216 struct task_smack *tsp = cred->security;
217 struct smack_known *sskp = tsp->smk_task;
218 struct inode *inode = file->f_inode;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700219 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700220 char acc[SMK_NUM_ACCESS_TYPE + 1];
221
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700222 if (isp->smk_flags & SMK_INODE_IMPURE)
223 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
224 inode->i_sb->s_id, inode->i_ino, current->comm);
225
Casey Schauflerd166c802014-08-27 14:51:27 -0700226 if (rc <= 0)
227 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700228 if (rc > SMACK_UNCONFINED_OBJECT)
229 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700230
231 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700232 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200233 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400234 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700235 current->comm);
236 return 0;
237}
238#else
239#define smk_bu_credfile(cred, file, mode, RC) (RC)
240#endif
241
Casey Schauflere114e472008-02-04 22:29:50 -0800242/**
243 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100244 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800245 * @ip: a pointer to the inode
246 * @dp: a pointer to the dentry
247 *
248 * Returns a pointer to the master list entry for the Smack label
249 * or NULL if there was no label to fetch.
250 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700251static struct smack_known *smk_fetch(const char *name, struct inode *ip,
252 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800253{
254 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700255 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700256 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800257
258 if (ip->i_op->getxattr == NULL)
259 return NULL;
260
Casey Schauflerf7112e62012-05-06 15:22:02 -0700261 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
262 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800263 return NULL;
264
Casey Schauflerf7112e62012-05-06 15:22:02 -0700265 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
266 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700267 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700268
269 kfree(buffer);
270
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700271 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800272}
273
274/**
275 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200276 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800277 *
278 * Returns the new blob or NULL if there's no memory available
279 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200280struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800281{
282 struct inode_smack *isp;
283
Rohit1a5b4722014-10-15 17:40:41 +0530284 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800285 if (isp == NULL)
286 return NULL;
287
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200288 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800289 isp->smk_flags = 0;
290 mutex_init(&isp->smk_lock);
291
292 return isp;
293}
294
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800295/**
296 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100297 * @task: a pointer to the Smack label for the running task
298 * @forked: a pointer to the Smack label for the forked task
299 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800300 *
301 * Returns the new blob or NULL if there's no memory available
302 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700303static struct task_smack *new_task_smack(struct smack_known *task,
304 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800305{
306 struct task_smack *tsp;
307
308 tsp = kzalloc(sizeof(struct task_smack), gfp);
309 if (tsp == NULL)
310 return NULL;
311
312 tsp->smk_task = task;
313 tsp->smk_forked = forked;
314 INIT_LIST_HEAD(&tsp->smk_rules);
315 mutex_init(&tsp->smk_rules_lock);
316
317 return tsp;
318}
319
320/**
321 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100322 * @nhead: new rules header pointer
323 * @ohead: old rules header pointer
324 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800325 *
326 * Returns 0 on success, -ENOMEM on error
327 */
328static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
329 gfp_t gfp)
330{
331 struct smack_rule *nrp;
332 struct smack_rule *orp;
333 int rc = 0;
334
335 INIT_LIST_HEAD(nhead);
336
337 list_for_each_entry_rcu(orp, ohead, list) {
338 nrp = kzalloc(sizeof(struct smack_rule), gfp);
339 if (nrp == NULL) {
340 rc = -ENOMEM;
341 break;
342 }
343 *nrp = *orp;
344 list_add_rcu(&nrp->list, nhead);
345 }
346 return rc;
347}
348
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100349/**
350 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
351 * @mode - input mode in form of PTRACE_MODE_*
352 *
353 * Returns a converted MAY_* mode usable by smack rules
354 */
355static inline unsigned int smk_ptrace_mode(unsigned int mode)
356{
357 switch (mode) {
358 case PTRACE_MODE_READ:
359 return MAY_READ;
360 case PTRACE_MODE_ATTACH:
361 return MAY_READWRITE;
362 }
363
364 return 0;
365}
366
367/**
368 * smk_ptrace_rule_check - helper for ptrace access
369 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200370 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100371 * @mode: ptrace attachment mode (PTRACE_MODE_*)
372 * @func: name of the function that called us, used for audit
373 *
374 * Returns 0 on access granted, -error on error
375 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200376static int smk_ptrace_rule_check(struct task_struct *tracer,
377 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100378 unsigned int mode, const char *func)
379{
380 int rc;
381 struct smk_audit_info ad, *saip = NULL;
382 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200383 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100384
385 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
386 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
387 smk_ad_setfield_u_tsk(&ad, tracer);
388 saip = &ad;
389 }
390
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300391 rcu_read_lock();
392 tsp = __task_cred(tracer)->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200393 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100394
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100395 if ((mode & PTRACE_MODE_ATTACH) &&
396 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
397 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200398 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100399 rc = 0;
400 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
401 rc = -EACCES;
402 else if (capable(CAP_SYS_PTRACE))
403 rc = 0;
404 else
405 rc = -EACCES;
406
407 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200408 smack_log(tracer_known->smk_known,
409 tracee_known->smk_known,
410 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100411
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300412 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100413 return rc;
414 }
415
416 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200417 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300418
419 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100420 return rc;
421}
422
Casey Schauflere114e472008-02-04 22:29:50 -0800423/*
424 * LSM hooks.
425 * We he, that is fun!
426 */
427
428/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000429 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800430 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100431 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800432 *
433 * Returns 0 if access is OK, an error code otherwise
434 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100435 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800436 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000437static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800438{
439 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700440 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800441
Ingo Molnar9e488582009-05-07 19:26:19 +1000442 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800443 if (rc != 0)
444 return rc;
445
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300446 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200447
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200448 rc = smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100449 return rc;
450}
Casey Schauflere114e472008-02-04 22:29:50 -0800451
David Howells5cd9c582008-08-14 11:37:28 +0100452/**
453 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
454 * @ptp: parent task pointer
455 *
456 * Returns 0 if access is OK, an error code otherwise
457 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100458 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100459 */
460static int smack_ptrace_traceme(struct task_struct *ptp)
461{
462 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700463 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100464
465 rc = cap_ptrace_traceme(ptp);
466 if (rc != 0)
467 return rc;
468
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100469 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200470
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200471 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800472 return rc;
473}
474
475/**
476 * smack_syslog - Smack approval on syslog
477 * @type: message type
478 *
Casey Schauflere114e472008-02-04 22:29:50 -0800479 * Returns 0 on success, error code otherwise.
480 */
Eric Paris12b30522010-11-15 18:36:29 -0500481static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800482{
Eric Paris12b30522010-11-15 18:36:29 -0500483 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700484 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800485
Casey Schaufler1880eff2012-06-05 15:28:30 -0700486 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800487 return 0;
488
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800489 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800490 rc = -EACCES;
491
492 return rc;
493}
494
495
496/*
497 * Superblock Hooks.
498 */
499
500/**
501 * smack_sb_alloc_security - allocate a superblock blob
502 * @sb: the superblock getting the blob
503 *
504 * Returns 0 on success or -ENOMEM on error.
505 */
506static int smack_sb_alloc_security(struct super_block *sb)
507{
508 struct superblock_smack *sbsp;
509
510 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
511
512 if (sbsp == NULL)
513 return -ENOMEM;
514
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200515 sbsp->smk_root = &smack_known_floor;
516 sbsp->smk_default = &smack_known_floor;
517 sbsp->smk_floor = &smack_known_floor;
518 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700519 /*
520 * smk_initialized will be zero from kzalloc.
521 */
Casey Schauflere114e472008-02-04 22:29:50 -0800522 sb->s_security = sbsp;
523
524 return 0;
525}
526
527/**
528 * smack_sb_free_security - free a superblock blob
529 * @sb: the superblock getting the blob
530 *
531 */
532static void smack_sb_free_security(struct super_block *sb)
533{
534 kfree(sb->s_security);
535 sb->s_security = NULL;
536}
537
538/**
539 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800540 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800541 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800542 *
543 * Returns 0 on success or -ENOMEM on error.
544 *
545 * Copy the Smack specific mount options out of the mount
546 * options list.
547 */
Eric Parise0007522008-03-05 10:31:54 -0500548static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800549{
550 char *cp, *commap, *otheropts, *dp;
551
Casey Schauflere114e472008-02-04 22:29:50 -0800552 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
553 if (otheropts == NULL)
554 return -ENOMEM;
555
556 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
557 if (strstr(cp, SMK_FSDEFAULT) == cp)
558 dp = smackopts;
559 else if (strstr(cp, SMK_FSFLOOR) == cp)
560 dp = smackopts;
561 else if (strstr(cp, SMK_FSHAT) == cp)
562 dp = smackopts;
563 else if (strstr(cp, SMK_FSROOT) == cp)
564 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700565 else if (strstr(cp, SMK_FSTRANS) == cp)
566 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800567 else
568 dp = otheropts;
569
570 commap = strchr(cp, ',');
571 if (commap != NULL)
572 *commap = '\0';
573
574 if (*dp != '\0')
575 strcat(dp, ",");
576 strcat(dp, cp);
577 }
578
579 strcpy(orig, otheropts);
580 free_page((unsigned long)otheropts);
581
582 return 0;
583}
584
585/**
586 * smack_sb_kern_mount - Smack specific mount processing
587 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100588 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800589 * @data: the smack mount options
590 *
591 * Returns 0 on success, an error code on failure
592 */
James Morris12204e22008-12-19 10:44:42 +1100593static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800594{
595 struct dentry *root = sb->s_root;
596 struct inode *inode = root->d_inode;
597 struct superblock_smack *sp = sb->s_security;
598 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800599 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800600 char *op;
601 char *commap;
Casey Schauflere830b392013-05-22 18:43:07 -0700602 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800603 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800604
Casey Schauflere830b392013-05-22 18:43:07 -0700605 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800606 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700607
Casey Schauflere114e472008-02-04 22:29:50 -0800608 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800609
610 for (op = data; op != NULL; op = commap) {
611 commap = strchr(op, ',');
612 if (commap != NULL)
613 *commap++ = '\0';
614
615 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
616 op += strlen(SMK_FSHAT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200617 skp = smk_import_entry(op, 0);
618 if (skp != NULL) {
619 sp->smk_hat = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800620 specified = 1;
621 }
Casey Schauflere114e472008-02-04 22:29:50 -0800622 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
623 op += strlen(SMK_FSFLOOR);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200624 skp = smk_import_entry(op, 0);
625 if (skp != NULL) {
626 sp->smk_floor = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800627 specified = 1;
628 }
Casey Schauflere114e472008-02-04 22:29:50 -0800629 } else if (strncmp(op, SMK_FSDEFAULT,
630 strlen(SMK_FSDEFAULT)) == 0) {
631 op += strlen(SMK_FSDEFAULT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200632 skp = smk_import_entry(op, 0);
633 if (skp != NULL) {
634 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800635 specified = 1;
636 }
Casey Schauflere114e472008-02-04 22:29:50 -0800637 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
638 op += strlen(SMK_FSROOT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200639 skp = smk_import_entry(op, 0);
640 if (skp != NULL) {
641 sp->smk_root = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800642 specified = 1;
643 }
Casey Schauflere830b392013-05-22 18:43:07 -0700644 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
645 op += strlen(SMK_FSTRANS);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200646 skp = smk_import_entry(op, 0);
647 if (skp != NULL) {
648 sp->smk_root = skp;
Casey Schauflere830b392013-05-22 18:43:07 -0700649 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800650 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700651 }
Casey Schauflere114e472008-02-04 22:29:50 -0800652 }
653 }
654
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800655 if (!smack_privileged(CAP_MAC_ADMIN)) {
656 /*
657 * Unprivileged mounts don't get to specify Smack values.
658 */
659 if (specified)
660 return -EPERM;
661 /*
662 * Unprivileged mounts get root and default from the caller.
663 */
664 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200665 sp->smk_root = skp;
666 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800667 }
Casey Schauflere114e472008-02-04 22:29:50 -0800668 /*
669 * Initialize the root inode.
670 */
671 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100672 if (isp == NULL) {
673 isp = new_inode_smack(sp->smk_root);
674 if (isp == NULL)
675 return -ENOMEM;
676 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700677 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800678 isp->smk_inode = sp->smk_root;
679
Casey Schauflere830b392013-05-22 18:43:07 -0700680 if (transmute)
681 isp->smk_flags |= SMK_INODE_TRANSMUTE;
682
Casey Schauflere114e472008-02-04 22:29:50 -0800683 return 0;
684}
685
686/**
687 * smack_sb_statfs - Smack check on statfs
688 * @dentry: identifies the file system in question
689 *
690 * Returns 0 if current can read the floor of the filesystem,
691 * and error code otherwise
692 */
693static int smack_sb_statfs(struct dentry *dentry)
694{
695 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200696 int rc;
697 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800698
Eric Parisa2694342011-04-25 13:10:27 -0400699 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200700 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
701
702 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700703 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200704 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800705}
706
Casey Schauflere114e472008-02-04 22:29:50 -0800707/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800708 * BPRM hooks
709 */
710
Casey Schauflerce8a4322011-09-29 18:21:01 -0700711/**
712 * smack_bprm_set_creds - set creds for exec
713 * @bprm: the exec information
714 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100715 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700716 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800717static int smack_bprm_set_creds(struct linux_binprm *bprm)
718{
Al Viro496ad9a2013-01-23 17:07:38 -0500719 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300720 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800721 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800722 int rc;
723
724 rc = cap_bprm_set_creds(bprm);
725 if (rc != 0)
726 return rc;
727
728 if (bprm->cred_prepared)
729 return 0;
730
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300731 isp = inode->i_security;
732 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800733 return 0;
734
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100735 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
736 struct task_struct *tracer;
737 rc = 0;
738
739 rcu_read_lock();
740 tracer = ptrace_parent(current);
741 if (likely(tracer != NULL))
742 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200743 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100744 PTRACE_MODE_ATTACH,
745 __func__);
746 rcu_read_unlock();
747
748 if (rc != 0)
749 return rc;
750 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300751 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800752
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300753 bsp->smk_task = isp->smk_task;
754 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800755
756 return 0;
757}
758
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300759/**
760 * smack_bprm_committing_creds - Prepare to install the new credentials
761 * from bprm.
762 *
763 * @bprm: binprm for exec
764 */
765static void smack_bprm_committing_creds(struct linux_binprm *bprm)
766{
767 struct task_smack *bsp = bprm->cred->security;
768
769 if (bsp->smk_task != bsp->smk_forked)
770 current->pdeath_signal = 0;
771}
772
773/**
774 * smack_bprm_secureexec - Return the decision to use secureexec.
775 * @bprm: binprm for exec
776 *
777 * Returns 0 on success.
778 */
779static int smack_bprm_secureexec(struct linux_binprm *bprm)
780{
781 struct task_smack *tsp = current_security();
782 int ret = cap_bprm_secureexec(bprm);
783
784 if (!ret && (tsp->smk_task != tsp->smk_forked))
785 ret = 1;
786
787 return ret;
788}
789
Casey Schaufler676dac42010-12-02 06:43:39 -0800790/*
Casey Schauflere114e472008-02-04 22:29:50 -0800791 * Inode hooks
792 */
793
794/**
795 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800796 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800797 *
798 * Returns 0 if it gets a blob, -ENOMEM otherwise
799 */
800static int smack_inode_alloc_security(struct inode *inode)
801{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700802 struct smack_known *skp = smk_of_current();
803
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200804 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800805 if (inode->i_security == NULL)
806 return -ENOMEM;
807 return 0;
808}
809
810/**
811 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800812 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800813 *
814 * Clears the blob pointer in inode
815 */
816static void smack_inode_free_security(struct inode *inode)
817{
Rohit1a5b4722014-10-15 17:40:41 +0530818 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800819 inode->i_security = NULL;
820}
821
822/**
823 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200824 * @inode: the newly created inode
825 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500826 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800827 * @name: where to put the attribute name
828 * @value: where to put the attribute value
829 * @len: where to put the length of the attribute
830 *
831 * Returns 0 if it all works out, -ENOMEM if there's no memory
832 */
833static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900834 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500835 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800836{
Casey Schaufler2267b132012-03-13 19:14:19 -0700837 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700838 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200839 struct smack_known *isp = smk_of_inode(inode);
840 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800841 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800842
Tetsuo Handa95489062013-07-25 05:44:02 +0900843 if (name)
844 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800845
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100846 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800847 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200848 may = smk_access_entry(skp->smk_known, dsp->smk_known,
849 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800850 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200851
852 /*
853 * If the access rule allows transmutation and
854 * the directory requests transmutation then
855 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700856 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200857 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800858 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700859 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200860 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700861 issp->smk_flags |= SMK_INODE_CHANGED;
862 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200863
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200864 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800865 if (*value == NULL)
866 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800867
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200868 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100869 }
Casey Schauflere114e472008-02-04 22:29:50 -0800870
871 return 0;
872}
873
874/**
875 * smack_inode_link - Smack check on link
876 * @old_dentry: the existing object
877 * @dir: unused
878 * @new_dentry: the new object
879 *
880 * Returns 0 if access is permitted, an error code otherwise
881 */
882static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
883 struct dentry *new_dentry)
884{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200885 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200886 struct smk_audit_info ad;
887 int rc;
888
Eric Parisa2694342011-04-25 13:10:27 -0400889 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200890 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800891
892 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200893 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700894 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800895
David Howells88025652015-01-29 12:02:32 +0000896 if (rc == 0 && d_is_positive(new_dentry)) {
Casey Schauflere114e472008-02-04 22:29:50 -0800897 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200898 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
899 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700900 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800901 }
902
903 return rc;
904}
905
906/**
907 * smack_inode_unlink - Smack check on inode deletion
908 * @dir: containing directory object
909 * @dentry: file to unlink
910 *
911 * Returns 0 if current can write the containing directory
912 * and the object, error code otherwise
913 */
914static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
915{
916 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200917 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800918 int rc;
919
Eric Parisa2694342011-04-25 13:10:27 -0400920 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200921 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
922
Casey Schauflere114e472008-02-04 22:29:50 -0800923 /*
924 * You need write access to the thing you're unlinking
925 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200926 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700927 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200928 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800929 /*
930 * You also need write access to the containing directory
931 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400932 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200933 smk_ad_setfield_u_fs_inode(&ad, dir);
934 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700935 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200936 }
Casey Schauflere114e472008-02-04 22:29:50 -0800937 return rc;
938}
939
940/**
941 * smack_inode_rmdir - Smack check on directory deletion
942 * @dir: containing directory object
943 * @dentry: directory to unlink
944 *
945 * Returns 0 if current can write the containing directory
946 * and the directory, error code otherwise
947 */
948static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
949{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200950 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800951 int rc;
952
Eric Parisa2694342011-04-25 13:10:27 -0400953 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200954 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
955
Casey Schauflere114e472008-02-04 22:29:50 -0800956 /*
957 * You need write access to the thing you're removing
958 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200959 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700960 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200961 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800962 /*
963 * You also need write access to the containing directory
964 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400965 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200966 smk_ad_setfield_u_fs_inode(&ad, dir);
967 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700968 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200969 }
Casey Schauflere114e472008-02-04 22:29:50 -0800970
971 return rc;
972}
973
974/**
975 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200976 * @old_inode: unused
977 * @old_dentry: the old object
978 * @new_inode: unused
979 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -0800980 *
981 * Read and write access is required on both the old and
982 * new directories.
983 *
984 * Returns 0 if access is permitted, an error code otherwise
985 */
986static int smack_inode_rename(struct inode *old_inode,
987 struct dentry *old_dentry,
988 struct inode *new_inode,
989 struct dentry *new_dentry)
990{
991 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200992 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200993 struct smk_audit_info ad;
994
Eric Parisa2694342011-04-25 13:10:27 -0400995 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200996 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800997
998 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200999 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001000 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001001
David Howells88025652015-01-29 12:02:32 +00001002 if (rc == 0 && d_is_positive(new_dentry)) {
Casey Schauflere114e472008-02-04 22:29:50 -08001003 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001004 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1005 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001006 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001007 }
Casey Schauflere114e472008-02-04 22:29:50 -08001008 return rc;
1009}
1010
1011/**
1012 * smack_inode_permission - Smack version of permission()
1013 * @inode: the inode in question
1014 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001015 *
1016 * This is the important Smack hook.
1017 *
1018 * Returns 0 if access is permitted, -EACCES otherwise
1019 */
Al Viroe74f71e2011-06-20 19:38:15 -04001020static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001021{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001022 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001023 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001024 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001025
1026 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001027 /*
1028 * No permission to check. Existence test. Yup, it's there.
1029 */
1030 if (mask == 0)
1031 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001032
1033 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001034 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001035 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001037 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001038 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1039 rc = smk_bu_inode(inode, mask, rc);
1040 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001041}
1042
1043/**
1044 * smack_inode_setattr - Smack check for setting attributes
1045 * @dentry: the object
1046 * @iattr: for the force flag
1047 *
1048 * Returns 0 if access is permitted, an error code otherwise
1049 */
1050static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1051{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001052 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001053 int rc;
1054
Casey Schauflere114e472008-02-04 22:29:50 -08001055 /*
1056 * Need to allow for clearing the setuid bit.
1057 */
1058 if (iattr->ia_valid & ATTR_FORCE)
1059 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001060 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001061 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001062
Casey Schauflerd166c802014-08-27 14:51:27 -07001063 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1064 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1065 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001066}
1067
1068/**
1069 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001070 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001071 * @dentry: the object
1072 *
1073 * Returns 0 if access is permitted, an error code otherwise
1074 */
1075static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1076{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001077 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -04001078 struct path path;
Casey Schauflerd166c802014-08-27 14:51:27 -07001079 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001080
Eric Parisa2694342011-04-25 13:10:27 -04001081 path.dentry = dentry;
1082 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001083
Eric Parisf48b7392011-04-25 12:54:27 -04001084 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -04001085 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001086 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1087 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1088 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001089}
1090
1091/**
1092 * smack_inode_setxattr - Smack check for setting xattrs
1093 * @dentry: the object
1094 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001095 * @value: value of the attribute
1096 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001097 * @flags: unused
1098 *
1099 * This protects the Smack attribute explicitly.
1100 *
1101 * Returns 0 if access is permitted, an error code otherwise
1102 */
David Howells8f0cfa52008-04-29 00:59:41 -07001103static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1104 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001105{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001107 struct smack_known *skp;
1108 int check_priv = 0;
1109 int check_import = 0;
1110 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001111 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001112
Casey Schaufler19760ad2013-12-16 16:27:26 -08001113 /*
1114 * Check label validity here so import won't fail in post_setxattr
1115 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001116 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1117 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001118 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1119 check_priv = 1;
1120 check_import = 1;
1121 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1122 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1123 check_priv = 1;
1124 check_import = 1;
1125 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001126 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001127 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001128 if (size != TRANS_TRUE_SIZE ||
1129 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1130 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001131 } else
1132 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1133
Casey Schaufler19760ad2013-12-16 16:27:26 -08001134 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1135 rc = -EPERM;
1136
1137 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001138 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001139 if (skp == NULL || (check_star &&
1140 (skp == &smack_known_star || skp == &smack_known_web)))
1141 rc = -EINVAL;
1142 }
1143
Eric Parisa2694342011-04-25 13:10:27 -04001144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1146
Casey Schauflerd166c802014-08-27 14:51:27 -07001147 if (rc == 0) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001149 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1150 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001151
1152 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001153}
1154
1155/**
1156 * smack_inode_post_setxattr - Apply the Smack update approved above
1157 * @dentry: object
1158 * @name: attribute name
1159 * @value: attribute value
1160 * @size: attribute size
1161 * @flags: unused
1162 *
1163 * Set the pointer in the inode blob to the entry found
1164 * in the master label list.
1165 */
David Howells8f0cfa52008-04-29 00:59:41 -07001166static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1167 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001168{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001169 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001170 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001171
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001172 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1173 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1174 return;
1175 }
1176
Casey Schaufler676dac42010-12-02 06:43:39 -08001177 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001178 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001179 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001180 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001181 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001182 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001183 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001184 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001185 if (skp != NULL)
1186 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001187 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001188 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001189 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001190 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001191 if (skp != NULL)
1192 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001193 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001194 isp->smk_mmap = &smack_known_invalid;
1195 }
Casey Schauflere114e472008-02-04 22:29:50 -08001196
1197 return;
1198}
1199
Casey Schauflerce8a4322011-09-29 18:21:01 -07001200/**
Casey Schauflere114e472008-02-04 22:29:50 -08001201 * smack_inode_getxattr - Smack check on getxattr
1202 * @dentry: the object
1203 * @name: unused
1204 *
1205 * Returns 0 if access is permitted, an error code otherwise
1206 */
David Howells8f0cfa52008-04-29 00:59:41 -07001207static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001208{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001209 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001210 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001211
Eric Parisa2694342011-04-25 13:10:27 -04001212 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001213 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1214
Casey Schauflerd166c802014-08-27 14:51:27 -07001215 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1216 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1217 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001218}
1219
Casey Schauflerce8a4322011-09-29 18:21:01 -07001220/**
Casey Schauflere114e472008-02-04 22:29:50 -08001221 * smack_inode_removexattr - Smack check on removexattr
1222 * @dentry: the object
1223 * @name: name of the attribute
1224 *
1225 * Removing the Smack attribute requires CAP_MAC_ADMIN
1226 *
1227 * Returns 0 if access is permitted, an error code otherwise
1228 */
David Howells8f0cfa52008-04-29 00:59:41 -07001229static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001230{
Casey Schaufler676dac42010-12-02 06:43:39 -08001231 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001232 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001233 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001234
Casey Schauflerbcdca222008-02-23 15:24:04 -08001235 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1236 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001237 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001238 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001239 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301240 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001241 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001242 rc = -EPERM;
1243 } else
1244 rc = cap_inode_removexattr(dentry, name);
1245
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001246 if (rc != 0)
1247 return rc;
1248
Eric Parisa2694342011-04-25 13:10:27 -04001249 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001250 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001251
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001252 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001253 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001254 if (rc != 0)
1255 return rc;
1256
1257 isp = dentry->d_inode->i_security;
1258 /*
1259 * Don't do anything special for these.
1260 * XATTR_NAME_SMACKIPIN
1261 * XATTR_NAME_SMACKIPOUT
1262 * XATTR_NAME_SMACKEXEC
1263 */
1264 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001265 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001266 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001267 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001268 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1269 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001270
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001271 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001272}
1273
1274/**
1275 * smack_inode_getsecurity - get smack xattrs
1276 * @inode: the object
1277 * @name: attribute name
1278 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001279 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001280 *
1281 * Returns the size of the attribute or an error code
1282 */
1283static int smack_inode_getsecurity(const struct inode *inode,
1284 const char *name, void **buffer,
1285 bool alloc)
1286{
1287 struct socket_smack *ssp;
1288 struct socket *sock;
1289 struct super_block *sbp;
1290 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001291 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001292 int ilen;
1293 int rc = 0;
1294
1295 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1296 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001297 ilen = strlen(isp->smk_known);
1298 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001299 return ilen;
1300 }
1301
1302 /*
1303 * The rest of the Smack xattrs are only on sockets.
1304 */
1305 sbp = ip->i_sb;
1306 if (sbp->s_magic != SOCKFS_MAGIC)
1307 return -EOPNOTSUPP;
1308
1309 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001310 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001311 return -EOPNOTSUPP;
1312
1313 ssp = sock->sk->sk_security;
1314
1315 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001316 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001317 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001318 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001319 else
1320 return -EOPNOTSUPP;
1321
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001322 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001323 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001324 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001325 rc = ilen;
1326 }
1327
1328 return rc;
1329}
1330
1331
1332/**
1333 * smack_inode_listsecurity - list the Smack attributes
1334 * @inode: the object
1335 * @buffer: where they go
1336 * @buffer_size: size of buffer
1337 *
1338 * Returns 0 on success, -EINVAL otherwise
1339 */
1340static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1341 size_t buffer_size)
1342{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001343 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001344
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001345 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001346 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001347
1348 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001349}
1350
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001351/**
1352 * smack_inode_getsecid - Extract inode's security id
1353 * @inode: inode to extract the info from
1354 * @secid: where result will be saved
1355 */
1356static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1357{
1358 struct inode_smack *isp = inode->i_security;
1359
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001360 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001361}
1362
Casey Schauflere114e472008-02-04 22:29:50 -08001363/*
1364 * File Hooks
1365 */
1366
1367/**
1368 * smack_file_permission - Smack check on file operations
1369 * @file: unused
1370 * @mask: unused
1371 *
1372 * Returns 0
1373 *
1374 * Should access checks be done on each read or write?
1375 * UNICOS and SELinux say yes.
1376 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1377 *
1378 * I'll say no for now. Smack does not do the frequent
1379 * label changing that SELinux does.
1380 */
1381static int smack_file_permission(struct file *file, int mask)
1382{
1383 return 0;
1384}
1385
1386/**
1387 * smack_file_alloc_security - assign a file security blob
1388 * @file: the object
1389 *
1390 * The security blob for a file is a pointer to the master
1391 * label list, so no allocation is done.
1392 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001393 * f_security is the owner security information. It
1394 * isn't used on file access checks, it's for send_sigio.
1395 *
Casey Schauflere114e472008-02-04 22:29:50 -08001396 * Returns 0
1397 */
1398static int smack_file_alloc_security(struct file *file)
1399{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001400 struct smack_known *skp = smk_of_current();
1401
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001402 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001403 return 0;
1404}
1405
1406/**
1407 * smack_file_free_security - clear a file security blob
1408 * @file: the object
1409 *
1410 * The security blob for a file is a pointer to the master
1411 * label list, so no memory is freed.
1412 */
1413static void smack_file_free_security(struct file *file)
1414{
1415 file->f_security = NULL;
1416}
1417
1418/**
1419 * smack_file_ioctl - Smack check on ioctls
1420 * @file: the object
1421 * @cmd: what to do
1422 * @arg: unused
1423 *
1424 * Relies heavily on the correct use of the ioctl command conventions.
1425 *
1426 * Returns 0 if allowed, error code otherwise
1427 */
1428static int smack_file_ioctl(struct file *file, unsigned int cmd,
1429 unsigned long arg)
1430{
1431 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001432 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001433 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001434
Eric Parisf48b7392011-04-25 12:54:27 -04001435 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001436 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001437
Casey Schauflerd166c802014-08-27 14:51:27 -07001438 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001439 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001440 rc = smk_bu_file(file, MAY_WRITE, rc);
1441 }
Casey Schauflere114e472008-02-04 22:29:50 -08001442
Casey Schauflerd166c802014-08-27 14:51:27 -07001443 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001444 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001445 rc = smk_bu_file(file, MAY_READ, rc);
1446 }
Casey Schauflere114e472008-02-04 22:29:50 -08001447
1448 return rc;
1449}
1450
1451/**
1452 * smack_file_lock - Smack check on file locking
1453 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001454 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001455 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001456 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001457 */
1458static int smack_file_lock(struct file *file, unsigned int cmd)
1459{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001460 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001461 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001462 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001463
Eric Paris92f42502011-04-25 13:15:55 -04001464 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1465 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001466 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001467 rc = smk_bu_file(file, MAY_LOCK, rc);
1468 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001469}
1470
1471/**
1472 * smack_file_fcntl - Smack check on fcntl
1473 * @file: the object
1474 * @cmd: what action to check
1475 * @arg: unused
1476 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001477 * Generally these operations are harmless.
1478 * File locking operations present an obvious mechanism
1479 * for passing information, so they require write access.
1480 *
Casey Schauflere114e472008-02-04 22:29:50 -08001481 * Returns 0 if current has access, error code otherwise
1482 */
1483static int smack_file_fcntl(struct file *file, unsigned int cmd,
1484 unsigned long arg)
1485{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001486 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001487 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001488 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001489
Casey Schauflere114e472008-02-04 22:29:50 -08001490 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001491 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001492 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001493 case F_SETLK:
1494 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001495 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1496 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001497 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001498 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001499 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001500 case F_SETOWN:
1501 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001502 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1503 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001504 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001505 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001506 break;
1507 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001508 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001509 }
1510
1511 return rc;
1512}
1513
1514/**
Al Viroe5467852012-05-30 13:30:51 -04001515 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001516 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1517 * if mapping anonymous memory.
1518 * @file contains the file structure for file to map (may be NULL).
1519 * @reqprot contains the protection requested by the application.
1520 * @prot contains the protection that will be applied by the kernel.
1521 * @flags contains the operational flags.
1522 * Return 0 if permission is granted.
1523 */
Al Viroe5467852012-05-30 13:30:51 -04001524static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001525 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001526 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001527{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001528 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001529 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001530 struct smack_rule *srp;
1531 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001532 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001533 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001534 int may;
1535 int mmay;
1536 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001537 int rc;
1538
Al Viro496ad9a2013-01-23 17:07:38 -05001539 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001540 return 0;
1541
Al Viro496ad9a2013-01-23 17:07:38 -05001542 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001543 if (isp->smk_mmap == NULL)
1544 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001545 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001546
1547 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001548 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001549 rc = 0;
1550
1551 rcu_read_lock();
1552 /*
1553 * For each Smack rule associated with the subject
1554 * label verify that the SMACK64MMAP also has access
1555 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001556 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001557 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001558 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001559 /*
1560 * Matching labels always allows access.
1561 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001562 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001563 continue;
1564 /*
1565 * If there is a matching local rule take
1566 * that into account as well.
1567 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001568 may = smk_access_entry(srp->smk_subject->smk_known,
1569 okp->smk_known,
1570 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001571 if (may == -ENOENT)
1572 may = srp->smk_access;
1573 else
1574 may &= srp->smk_access;
1575 /*
1576 * If may is zero the SMACK64MMAP subject can't
1577 * possibly have less access.
1578 */
1579 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001580 continue;
1581
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001582 /*
1583 * Fetch the global list entry.
1584 * If there isn't one a SMACK64MMAP subject
1585 * can't have as much access as current.
1586 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001587 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1588 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001589 if (mmay == -ENOENT) {
1590 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001591 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001592 }
1593 /*
1594 * If there is a local entry it modifies the
1595 * potential access, too.
1596 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001597 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1598 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001599 if (tmay != -ENOENT)
1600 mmay &= tmay;
1601
1602 /*
1603 * If there is any access available to current that is
1604 * not available to a SMACK64MMAP subject
1605 * deny access.
1606 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001607 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001608 rc = -EACCES;
1609 break;
1610 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001611 }
1612
1613 rcu_read_unlock();
1614
1615 return rc;
1616}
1617
1618/**
Casey Schauflere114e472008-02-04 22:29:50 -08001619 * smack_file_set_fowner - set the file security blob value
1620 * @file: object in question
1621 *
Casey Schauflere114e472008-02-04 22:29:50 -08001622 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001623static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001624{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001625 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001626}
1627
1628/**
1629 * smack_file_send_sigiotask - Smack on sigio
1630 * @tsk: The target task
1631 * @fown: the object the signal come from
1632 * @signum: unused
1633 *
1634 * Allow a privileged task to get signals even if it shouldn't
1635 *
1636 * Returns 0 if a subject with the object's smack could
1637 * write to the task, an error code otherwise.
1638 */
1639static int smack_file_send_sigiotask(struct task_struct *tsk,
1640 struct fown_struct *fown, int signum)
1641{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001642 struct smack_known *skp;
1643 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001644 struct file *file;
1645 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001646 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001647
1648 /*
1649 * struct fown_struct is never outside the context of a struct file
1650 */
1651 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001652
Etienne Bassetecfcc532009-04-08 20:40:06 +02001653 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001654 skp = file->f_security;
1655 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1656 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001657 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001658 rc = 0;
1659
1660 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1661 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001662 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001663 return rc;
1664}
1665
1666/**
1667 * smack_file_receive - Smack file receive check
1668 * @file: the object
1669 *
1670 * Returns 0 if current has access, error code otherwise
1671 */
1672static int smack_file_receive(struct file *file)
1673{
Casey Schauflerd166c802014-08-27 14:51:27 -07001674 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001675 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001676 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001677 struct inode *inode = file_inode(file);
Casey Schauflere114e472008-02-04 22:29:50 -08001678
Casey Schaufler4482a442013-12-30 17:37:45 -08001679 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001680 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001681 /*
1682 * This code relies on bitmasks.
1683 */
1684 if (file->f_mode & FMODE_READ)
1685 may = MAY_READ;
1686 if (file->f_mode & FMODE_WRITE)
1687 may |= MAY_WRITE;
1688
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001689 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001690 rc = smk_bu_file(file, may, rc);
1691 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001692}
1693
Casey Schaufler531f1d42011-09-19 12:41:42 -07001694/**
Eric Paris83d49852012-04-04 13:45:40 -04001695 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001696 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001697 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001698 *
1699 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001700 * Allow the open only if the task has read access. There are
1701 * many read operations (e.g. fstat) that you can do with an
1702 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001703 *
1704 * Returns 0
1705 */
Eric Paris83d49852012-04-04 13:45:40 -04001706static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001707{
Casey Schauflera6834c02014-04-21 11:10:26 -07001708 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001709 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001710 struct smk_audit_info ad;
1711 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001712
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001713 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001714 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001715
Casey Schauflera6834c02014-04-21 11:10:26 -07001716 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1717 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001718 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001719 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001720
1721 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001722}
1723
Casey Schauflere114e472008-02-04 22:29:50 -08001724/*
1725 * Task hooks
1726 */
1727
1728/**
David Howellsee18d642009-09-02 09:14:21 +01001729 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1730 * @new: the new credentials
1731 * @gfp: the atomicity of any memory allocations
1732 *
1733 * Prepare a blank set of credentials for modification. This must allocate all
1734 * the memory the LSM module might require such that cred_transfer() can
1735 * complete without error.
1736 */
1737static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1738{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001739 struct task_smack *tsp;
1740
1741 tsp = new_task_smack(NULL, NULL, gfp);
1742 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001743 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001744
1745 cred->security = tsp;
1746
David Howellsee18d642009-09-02 09:14:21 +01001747 return 0;
1748}
1749
1750
1751/**
David Howellsf1752ee2008-11-14 10:39:17 +11001752 * smack_cred_free - "free" task-level security credentials
1753 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001754 *
Casey Schauflere114e472008-02-04 22:29:50 -08001755 */
David Howellsf1752ee2008-11-14 10:39:17 +11001756static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001757{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001758 struct task_smack *tsp = cred->security;
1759 struct smack_rule *rp;
1760 struct list_head *l;
1761 struct list_head *n;
1762
1763 if (tsp == NULL)
1764 return;
1765 cred->security = NULL;
1766
1767 list_for_each_safe(l, n, &tsp->smk_rules) {
1768 rp = list_entry(l, struct smack_rule, list);
1769 list_del(&rp->list);
1770 kfree(rp);
1771 }
1772 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001773}
1774
1775/**
David Howellsd84f4f92008-11-14 10:39:23 +11001776 * smack_cred_prepare - prepare new set of credentials for modification
1777 * @new: the new credentials
1778 * @old: the original credentials
1779 * @gfp: the atomicity of any memory allocations
1780 *
1781 * Prepare a new set of credentials for modification.
1782 */
1783static int smack_cred_prepare(struct cred *new, const struct cred *old,
1784 gfp_t gfp)
1785{
Casey Schaufler676dac42010-12-02 06:43:39 -08001786 struct task_smack *old_tsp = old->security;
1787 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001788 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001789
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001790 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001791 if (new_tsp == NULL)
1792 return -ENOMEM;
1793
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001794 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1795 if (rc != 0)
1796 return rc;
1797
Casey Schaufler676dac42010-12-02 06:43:39 -08001798 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001799 return 0;
1800}
1801
Randy Dunlap251a2a92009-02-18 11:42:33 -08001802/**
David Howellsee18d642009-09-02 09:14:21 +01001803 * smack_cred_transfer - Transfer the old credentials to the new credentials
1804 * @new: the new credentials
1805 * @old: the original credentials
1806 *
1807 * Fill in a set of blank credentials from another set of credentials.
1808 */
1809static void smack_cred_transfer(struct cred *new, const struct cred *old)
1810{
Casey Schaufler676dac42010-12-02 06:43:39 -08001811 struct task_smack *old_tsp = old->security;
1812 struct task_smack *new_tsp = new->security;
1813
1814 new_tsp->smk_task = old_tsp->smk_task;
1815 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001816 mutex_init(&new_tsp->smk_rules_lock);
1817 INIT_LIST_HEAD(&new_tsp->smk_rules);
1818
1819
1820 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001821}
1822
1823/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001824 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001825 * @new: points to the set of credentials to be modified.
1826 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001827 *
1828 * Set the security data for a kernel service.
1829 */
1830static int smack_kernel_act_as(struct cred *new, u32 secid)
1831{
Casey Schaufler676dac42010-12-02 06:43:39 -08001832 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001833 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001834
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001835 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001836 return -EINVAL;
1837
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001838 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001839 return 0;
1840}
1841
1842/**
1843 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001844 * @new: points to the set of credentials to be modified
1845 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001846 *
1847 * Set the file creation context in a set of credentials to the same
1848 * as the objective context of the specified inode
1849 */
1850static int smack_kernel_create_files_as(struct cred *new,
1851 struct inode *inode)
1852{
1853 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001854 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001855
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001856 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001857 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001858 return 0;
1859}
1860
1861/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001862 * smk_curacc_on_task - helper to log task related access
1863 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001864 * @access: the access requested
1865 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001866 *
1867 * Return 0 if access is permitted
1868 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001869static int smk_curacc_on_task(struct task_struct *p, int access,
1870 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001871{
1872 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001873 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001874 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001875
Casey Schaufler531f1d42011-09-19 12:41:42 -07001876 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001877 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001878 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001879 rc = smk_bu_task(p, access, rc);
1880 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001881}
1882
1883/**
Casey Schauflere114e472008-02-04 22:29:50 -08001884 * smack_task_setpgid - Smack check on setting pgid
1885 * @p: the task object
1886 * @pgid: unused
1887 *
1888 * Return 0 if write access is permitted
1889 */
1890static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1891{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001892 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001893}
1894
1895/**
1896 * smack_task_getpgid - Smack access check for getpgid
1897 * @p: the object task
1898 *
1899 * Returns 0 if current can read the object task, error code otherwise
1900 */
1901static int smack_task_getpgid(struct task_struct *p)
1902{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001903 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001904}
1905
1906/**
1907 * smack_task_getsid - Smack access check for getsid
1908 * @p: the object task
1909 *
1910 * Returns 0 if current can read the object task, error code otherwise
1911 */
1912static int smack_task_getsid(struct task_struct *p)
1913{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001914 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001915}
1916
1917/**
1918 * smack_task_getsecid - get the secid of the task
1919 * @p: the object task
1920 * @secid: where to put the result
1921 *
1922 * Sets the secid to contain a u32 version of the smack label.
1923 */
1924static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1925{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001926 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001927
1928 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001929}
1930
1931/**
1932 * smack_task_setnice - Smack check on setting nice
1933 * @p: the task object
1934 * @nice: unused
1935 *
1936 * Return 0 if write access is permitted
1937 */
1938static int smack_task_setnice(struct task_struct *p, int nice)
1939{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001940 int rc;
1941
1942 rc = cap_task_setnice(p, nice);
1943 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001944 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001945 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001946}
1947
1948/**
1949 * smack_task_setioprio - Smack check on setting ioprio
1950 * @p: the task object
1951 * @ioprio: unused
1952 *
1953 * Return 0 if write access is permitted
1954 */
1955static int smack_task_setioprio(struct task_struct *p, int ioprio)
1956{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001957 int rc;
1958
1959 rc = cap_task_setioprio(p, ioprio);
1960 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001961 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001962 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001963}
1964
1965/**
1966 * smack_task_getioprio - Smack check on reading ioprio
1967 * @p: the task object
1968 *
1969 * Return 0 if read access is permitted
1970 */
1971static int smack_task_getioprio(struct task_struct *p)
1972{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001973 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001974}
1975
1976/**
1977 * smack_task_setscheduler - Smack check on setting scheduler
1978 * @p: the task object
1979 * @policy: unused
1980 * @lp: unused
1981 *
1982 * Return 0 if read access is permitted
1983 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001984static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001985{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001986 int rc;
1987
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001988 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001989 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001990 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001991 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001992}
1993
1994/**
1995 * smack_task_getscheduler - Smack check on reading scheduler
1996 * @p: the task object
1997 *
1998 * Return 0 if read access is permitted
1999 */
2000static int smack_task_getscheduler(struct task_struct *p)
2001{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002002 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002003}
2004
2005/**
2006 * smack_task_movememory - Smack check on moving memory
2007 * @p: the task object
2008 *
2009 * Return 0 if write access is permitted
2010 */
2011static int smack_task_movememory(struct task_struct *p)
2012{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002013 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002014}
2015
2016/**
2017 * smack_task_kill - Smack check on signal delivery
2018 * @p: the task object
2019 * @info: unused
2020 * @sig: unused
2021 * @secid: identifies the smack to use in lieu of current's
2022 *
2023 * Return 0 if write access is permitted
2024 *
2025 * The secid behavior is an artifact of an SELinux hack
2026 * in the USB code. Someday it may go away.
2027 */
2028static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2029 int sig, u32 secid)
2030{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002031 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002032 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002033 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002034 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002035
2036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2037 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002038 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002039 * Sending a signal requires that the sender
2040 * can write the receiver.
2041 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002042 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002043 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002044 rc = smk_bu_task(p, MAY_WRITE, rc);
2045 return rc;
2046 }
Casey Schauflere114e472008-02-04 22:29:50 -08002047 /*
2048 * If the secid isn't 0 we're dealing with some USB IO
2049 * specific behavior. This is not clean. For one thing
2050 * we can't take privilege into account.
2051 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002052 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002053 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2054 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002055 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002056}
2057
2058/**
2059 * smack_task_wait - Smack access check for waiting
2060 * @p: task to wait for
2061 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002062 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002063 */
2064static int smack_task_wait(struct task_struct *p)
2065{
Casey Schauflere114e472008-02-04 22:29:50 -08002066 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002067 * Allow the operation to succeed.
2068 * Zombies are bad.
2069 * In userless environments (e.g. phones) programs
2070 * get marked with SMACK64EXEC and even if the parent
2071 * and child shouldn't be talking the parent still
2072 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002073 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002074 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002075}
2076
2077/**
2078 * smack_task_to_inode - copy task smack into the inode blob
2079 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002080 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002081 *
2082 * Sets the smack pointer in the inode security blob
2083 */
2084static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2085{
2086 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002087 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002088
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002089 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002090}
2091
2092/*
2093 * Socket hooks.
2094 */
2095
2096/**
2097 * smack_sk_alloc_security - Allocate a socket blob
2098 * @sk: the socket
2099 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002100 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002101 *
2102 * Assign Smack pointers to current
2103 *
2104 * Returns 0 on success, -ENOMEM is there's no memory
2105 */
2106static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2107{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002108 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002109 struct socket_smack *ssp;
2110
2111 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2112 if (ssp == NULL)
2113 return -ENOMEM;
2114
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002115 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002116 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002117 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002118
2119 sk->sk_security = ssp;
2120
2121 return 0;
2122}
2123
2124/**
2125 * smack_sk_free_security - Free a socket blob
2126 * @sk: the socket
2127 *
2128 * Clears the blob pointer
2129 */
2130static void smack_sk_free_security(struct sock *sk)
2131{
2132 kfree(sk->sk_security);
2133}
2134
2135/**
Paul Moore07feee82009-03-27 17:10:54 -04002136* smack_host_label - check host based restrictions
2137* @sip: the object end
2138*
2139* looks for host based access restrictions
2140*
2141* This version will only be appropriate for really small sets of single label
2142* hosts. The caller is responsible for ensuring that the RCU read lock is
2143* taken before calling this function.
2144*
2145* Returns the label of the far end or NULL if it's not special.
2146*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002147static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002148{
2149 struct smk_netlbladdr *snp;
2150 struct in_addr *siap = &sip->sin_addr;
2151
2152 if (siap->s_addr == 0)
2153 return NULL;
2154
2155 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2156 /*
2157 * we break after finding the first match because
2158 * the list is sorted from longest to shortest mask
2159 * so we have found the most specific match
2160 */
2161 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002162 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2163 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002164 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002165 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002166 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002167 }
Paul Moore07feee82009-03-27 17:10:54 -04002168
2169 return NULL;
2170}
2171
2172/**
Casey Schauflere114e472008-02-04 22:29:50 -08002173 * smack_netlabel - Set the secattr on a socket
2174 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002175 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002176 *
2177 * Convert the outbound smack value (smk_out) to a
2178 * secattr and attach it to the socket.
2179 *
2180 * Returns 0 on success or an error code
2181 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002182static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002183{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002184 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002185 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002186 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002187
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002188 /*
2189 * Usually the netlabel code will handle changing the
2190 * packet labeling based on the label.
2191 * The case of a single label host is different, because
2192 * a single label host should never get a labeled packet
2193 * even though the label is usually associated with a packet
2194 * label.
2195 */
2196 local_bh_disable();
2197 bh_lock_sock_nested(sk);
2198
2199 if (ssp->smk_out == smack_net_ambient ||
2200 labeled == SMACK_UNLABELED_SOCKET)
2201 netlbl_sock_delattr(sk);
2202 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002203 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002204 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002205 }
2206
2207 bh_unlock_sock(sk);
2208 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002209
Casey Schauflere114e472008-02-04 22:29:50 -08002210 return rc;
2211}
2212
2213/**
Paul Moore07feee82009-03-27 17:10:54 -04002214 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2215 * @sk: the socket
2216 * @sap: the destination address
2217 *
2218 * Set the correct secattr for the given socket based on the destination
2219 * address and perform any outbound access checks needed.
2220 *
2221 * Returns 0 on success or an error code.
2222 *
2223 */
2224static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2225{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002226 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002227 int rc;
2228 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002229 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002230 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002231 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002232
2233 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002234 hkp = smack_host_label(sap);
2235 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002236#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002237 struct lsm_network_audit net;
2238
Eric Paris48c62af2012-04-02 13:15:44 -04002239 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2240 ad.a.u.net->family = sap->sin_family;
2241 ad.a.u.net->dport = sap->sin_port;
2242 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002243#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002244 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002245 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002246 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2247 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002248 } else {
2249 sk_lbl = SMACK_CIPSO_SOCKET;
2250 rc = 0;
2251 }
2252 rcu_read_unlock();
2253 if (rc != 0)
2254 return rc;
2255
2256 return smack_netlabel(sk, sk_lbl);
2257}
2258
Casey Schaufler69f287a2014-12-12 17:08:40 -08002259#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Paul Moore07feee82009-03-27 17:10:54 -04002260/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002261 * smk_ipv6_port_label - Smack port access table management
2262 * @sock: socket
2263 * @address: address
2264 *
2265 * Create or update the port list entry
2266 */
2267static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2268{
2269 struct sock *sk = sock->sk;
2270 struct sockaddr_in6 *addr6;
2271 struct socket_smack *ssp = sock->sk->sk_security;
2272 struct smk_port_label *spp;
2273 unsigned short port = 0;
2274
2275 if (address == NULL) {
2276 /*
2277 * This operation is changing the Smack information
2278 * on the bound socket. Take the changes to the port
2279 * as well.
2280 */
2281 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2282 if (sk != spp->smk_sock)
2283 continue;
2284 spp->smk_in = ssp->smk_in;
2285 spp->smk_out = ssp->smk_out;
2286 return;
2287 }
2288 /*
2289 * A NULL address is only used for updating existing
2290 * bound entries. If there isn't one, it's OK.
2291 */
2292 return;
2293 }
2294
2295 addr6 = (struct sockaddr_in6 *)address;
2296 port = ntohs(addr6->sin6_port);
2297 /*
2298 * This is a special case that is safely ignored.
2299 */
2300 if (port == 0)
2301 return;
2302
2303 /*
2304 * Look for an existing port list entry.
2305 * This is an indication that a port is getting reused.
2306 */
2307 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2308 if (spp->smk_port != port)
2309 continue;
2310 spp->smk_port = port;
2311 spp->smk_sock = sk;
2312 spp->smk_in = ssp->smk_in;
2313 spp->smk_out = ssp->smk_out;
2314 return;
2315 }
2316
2317 /*
2318 * A new port entry is required.
2319 */
2320 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2321 if (spp == NULL)
2322 return;
2323
2324 spp->smk_port = port;
2325 spp->smk_sock = sk;
2326 spp->smk_in = ssp->smk_in;
2327 spp->smk_out = ssp->smk_out;
2328
2329 list_add(&spp->list, &smk_ipv6_port_list);
2330 return;
2331}
2332
2333/**
2334 * smk_ipv6_port_check - check Smack port access
2335 * @sock: socket
2336 * @address: address
2337 *
2338 * Create or update the port list entry
2339 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002340static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002341 int act)
2342{
2343 __be16 *bep;
2344 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002345 struct smk_port_label *spp;
2346 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002347 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002348 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002349 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002350 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002351 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002352#ifdef CONFIG_AUDIT
2353 struct lsm_network_audit net;
2354#endif
2355
2356 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002357 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002358 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002359 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002360 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002361 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002362 }
2363
2364 /*
2365 * Get the IP address and port from the address.
2366 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002367 port = ntohs(address->sin6_port);
2368 bep = (__be16 *)(&address->sin6_addr);
2369 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002370
2371 /*
2372 * It's remote, so port lookup does no good.
2373 */
2374 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2375 goto auditout;
2376
2377 /*
2378 * It's local so the send check has to have passed.
2379 */
2380 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002381 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002382 goto auditout;
2383 }
2384
2385 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2386 if (spp->smk_port != port)
2387 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002388 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002389 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002390 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002391 break;
2392 }
2393
2394auditout:
2395
2396#ifdef CONFIG_AUDIT
2397 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2398 ad.a.u.net->family = sk->sk_family;
2399 ad.a.u.net->dport = port;
2400 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002401 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002402 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002403 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002404#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002405 rc = smk_access(skp, object, MAY_WRITE, &ad);
2406 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2407 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002408}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002409#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002410
2411/**
Casey Schauflere114e472008-02-04 22:29:50 -08002412 * smack_inode_setsecurity - set smack xattrs
2413 * @inode: the object
2414 * @name: attribute name
2415 * @value: attribute value
2416 * @size: size of the attribute
2417 * @flags: unused
2418 *
2419 * Sets the named attribute in the appropriate blob
2420 *
2421 * Returns 0 on success, or an error code
2422 */
2423static int smack_inode_setsecurity(struct inode *inode, const char *name,
2424 const void *value, size_t size, int flags)
2425{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002426 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002427 struct inode_smack *nsp = inode->i_security;
2428 struct socket_smack *ssp;
2429 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002430 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002431
Casey Schauflerf7112e62012-05-06 15:22:02 -07002432 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302433 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002434
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002435 skp = smk_import_entry(value, size);
2436 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002437 return -EINVAL;
2438
2439 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002440 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002441 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002442 return 0;
2443 }
2444 /*
2445 * The rest of the Smack xattrs are only on sockets.
2446 */
2447 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2448 return -EOPNOTSUPP;
2449
2450 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002451 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002452 return -EOPNOTSUPP;
2453
2454 ssp = sock->sk->sk_security;
2455
2456 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002457 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002458 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002459 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002460 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002461 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2462 if (rc != 0)
2463 printk(KERN_WARNING
2464 "Smack: \"%s\" netlbl error %d.\n",
2465 __func__, -rc);
2466 }
Casey Schauflere114e472008-02-04 22:29:50 -08002467 } else
2468 return -EOPNOTSUPP;
2469
Casey Schaufler69f287a2014-12-12 17:08:40 -08002470#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07002471 if (sock->sk->sk_family == PF_INET6)
2472 smk_ipv6_port_label(sock, NULL);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002473#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002474
Casey Schauflere114e472008-02-04 22:29:50 -08002475 return 0;
2476}
2477
2478/**
2479 * smack_socket_post_create - finish socket setup
2480 * @sock: the socket
2481 * @family: protocol family
2482 * @type: unused
2483 * @protocol: unused
2484 * @kern: unused
2485 *
2486 * Sets the netlabel information on the socket
2487 *
2488 * Returns 0 on success, and error code otherwise
2489 */
2490static int smack_socket_post_create(struct socket *sock, int family,
2491 int type, int protocol, int kern)
2492{
Marcin Lis74123012015-01-22 15:40:33 +01002493 struct socket_smack *ssp;
2494
2495 if (sock->sk == NULL)
2496 return 0;
2497
2498 /*
2499 * Sockets created by kernel threads receive web label.
2500 */
2501 if (unlikely(current->flags & PF_KTHREAD)) {
2502 ssp = sock->sk->sk_security;
2503 ssp->smk_in = &smack_known_web;
2504 ssp->smk_out = &smack_known_web;
2505 }
2506
2507 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002508 return 0;
2509 /*
2510 * Set the outbound netlbl.
2511 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002512 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2513}
2514
Casey Schaufler69f287a2014-12-12 17:08:40 -08002515#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002516/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002517 * smack_socket_bind - record port binding information.
2518 * @sock: the socket
2519 * @address: the port address
2520 * @addrlen: size of the address
2521 *
2522 * Records the label bound to a port.
2523 *
2524 * Returns 0
2525 */
2526static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2527 int addrlen)
2528{
Casey Schaufler69f287a2014-12-12 17:08:40 -08002529#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002530 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2531 smk_ipv6_port_label(sock, address);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002532#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002533
2534 return 0;
2535}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002536#endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002537
2538/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002539 * smack_socket_connect - connect access check
2540 * @sock: the socket
2541 * @sap: the other end
2542 * @addrlen: size of sap
2543 *
2544 * Verifies that a connection may be possible
2545 *
2546 * Returns 0 on success, and error code otherwise
2547 */
2548static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2549 int addrlen)
2550{
Casey Schauflerc6739442013-05-22 18:42:56 -07002551 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002552
Casey Schauflerc6739442013-05-22 18:42:56 -07002553 if (sock->sk == NULL)
2554 return 0;
2555
2556 switch (sock->sk->sk_family) {
2557 case PF_INET:
2558 if (addrlen < sizeof(struct sockaddr_in))
2559 return -EINVAL;
2560 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2561 break;
2562 case PF_INET6:
2563 if (addrlen < sizeof(struct sockaddr_in6))
2564 return -EINVAL;
Casey Schaufler69f287a2014-12-12 17:08:40 -08002565#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002566 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2567 SMK_CONNECTING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002568#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002569 break;
2570 }
2571 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002572}
2573
2574/**
2575 * smack_flags_to_may - convert S_ to MAY_ values
2576 * @flags: the S_ value
2577 *
2578 * Returns the equivalent MAY_ value
2579 */
2580static int smack_flags_to_may(int flags)
2581{
2582 int may = 0;
2583
2584 if (flags & S_IRUGO)
2585 may |= MAY_READ;
2586 if (flags & S_IWUGO)
2587 may |= MAY_WRITE;
2588 if (flags & S_IXUGO)
2589 may |= MAY_EXEC;
2590
2591 return may;
2592}
2593
2594/**
2595 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2596 * @msg: the object
2597 *
2598 * Returns 0
2599 */
2600static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2601{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002602 struct smack_known *skp = smk_of_current();
2603
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002604 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002605 return 0;
2606}
2607
2608/**
2609 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2610 * @msg: the object
2611 *
2612 * Clears the blob pointer
2613 */
2614static void smack_msg_msg_free_security(struct msg_msg *msg)
2615{
2616 msg->security = NULL;
2617}
2618
2619/**
2620 * smack_of_shm - the smack pointer for the shm
2621 * @shp: the object
2622 *
2623 * Returns a pointer to the smack value
2624 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002625static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002626{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002627 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002628}
2629
2630/**
2631 * smack_shm_alloc_security - Set the security blob for shm
2632 * @shp: the object
2633 *
2634 * Returns 0
2635 */
2636static int smack_shm_alloc_security(struct shmid_kernel *shp)
2637{
2638 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002639 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002640
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002641 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002642 return 0;
2643}
2644
2645/**
2646 * smack_shm_free_security - Clear the security blob for shm
2647 * @shp: the object
2648 *
2649 * Clears the blob pointer
2650 */
2651static void smack_shm_free_security(struct shmid_kernel *shp)
2652{
2653 struct kern_ipc_perm *isp = &shp->shm_perm;
2654
2655 isp->security = NULL;
2656}
2657
2658/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002659 * smk_curacc_shm : check if current has access on shm
2660 * @shp : the object
2661 * @access : access requested
2662 *
2663 * Returns 0 if current has the requested access, error code otherwise
2664 */
2665static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2666{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002667 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002668 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002669 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002670
2671#ifdef CONFIG_AUDIT
2672 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2673 ad.a.u.ipc_id = shp->shm_perm.id;
2674#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002675 rc = smk_curacc(ssp, access, &ad);
2676 rc = smk_bu_current("shm", ssp, access, rc);
2677 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002678}
2679
2680/**
Casey Schauflere114e472008-02-04 22:29:50 -08002681 * smack_shm_associate - Smack access check for shm
2682 * @shp: the object
2683 * @shmflg: access requested
2684 *
2685 * Returns 0 if current has the requested access, error code otherwise
2686 */
2687static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2688{
Casey Schauflere114e472008-02-04 22:29:50 -08002689 int may;
2690
2691 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002692 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002693}
2694
2695/**
2696 * smack_shm_shmctl - Smack access check for shm
2697 * @shp: the object
2698 * @cmd: what it wants to do
2699 *
2700 * Returns 0 if current has the requested access, error code otherwise
2701 */
2702static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2703{
Casey Schauflere114e472008-02-04 22:29:50 -08002704 int may;
2705
2706 switch (cmd) {
2707 case IPC_STAT:
2708 case SHM_STAT:
2709 may = MAY_READ;
2710 break;
2711 case IPC_SET:
2712 case SHM_LOCK:
2713 case SHM_UNLOCK:
2714 case IPC_RMID:
2715 may = MAY_READWRITE;
2716 break;
2717 case IPC_INFO:
2718 case SHM_INFO:
2719 /*
2720 * System level information.
2721 */
2722 return 0;
2723 default:
2724 return -EINVAL;
2725 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002726 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002727}
2728
2729/**
2730 * smack_shm_shmat - Smack access for shmat
2731 * @shp: the object
2732 * @shmaddr: unused
2733 * @shmflg: access requested
2734 *
2735 * Returns 0 if current has the requested access, error code otherwise
2736 */
2737static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2738 int shmflg)
2739{
Casey Schauflere114e472008-02-04 22:29:50 -08002740 int may;
2741
2742 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002743 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002744}
2745
2746/**
2747 * smack_of_sem - the smack pointer for the sem
2748 * @sma: the object
2749 *
2750 * Returns a pointer to the smack value
2751 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002752static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002753{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002754 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002755}
2756
2757/**
2758 * smack_sem_alloc_security - Set the security blob for sem
2759 * @sma: the object
2760 *
2761 * Returns 0
2762 */
2763static int smack_sem_alloc_security(struct sem_array *sma)
2764{
2765 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002766 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002767
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002768 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002769 return 0;
2770}
2771
2772/**
2773 * smack_sem_free_security - Clear the security blob for sem
2774 * @sma: the object
2775 *
2776 * Clears the blob pointer
2777 */
2778static void smack_sem_free_security(struct sem_array *sma)
2779{
2780 struct kern_ipc_perm *isp = &sma->sem_perm;
2781
2782 isp->security = NULL;
2783}
2784
2785/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002786 * smk_curacc_sem : check if current has access on sem
2787 * @sma : the object
2788 * @access : access requested
2789 *
2790 * Returns 0 if current has the requested access, error code otherwise
2791 */
2792static int smk_curacc_sem(struct sem_array *sma, int access)
2793{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002794 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002795 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002796 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002797
2798#ifdef CONFIG_AUDIT
2799 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2800 ad.a.u.ipc_id = sma->sem_perm.id;
2801#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002802 rc = smk_curacc(ssp, access, &ad);
2803 rc = smk_bu_current("sem", ssp, access, rc);
2804 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002805}
2806
2807/**
Casey Schauflere114e472008-02-04 22:29:50 -08002808 * smack_sem_associate - Smack access check for sem
2809 * @sma: the object
2810 * @semflg: access requested
2811 *
2812 * Returns 0 if current has the requested access, error code otherwise
2813 */
2814static int smack_sem_associate(struct sem_array *sma, int semflg)
2815{
Casey Schauflere114e472008-02-04 22:29:50 -08002816 int may;
2817
2818 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002819 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002820}
2821
2822/**
2823 * smack_sem_shmctl - Smack access check for sem
2824 * @sma: the object
2825 * @cmd: what it wants to do
2826 *
2827 * Returns 0 if current has the requested access, error code otherwise
2828 */
2829static int smack_sem_semctl(struct sem_array *sma, int cmd)
2830{
Casey Schauflere114e472008-02-04 22:29:50 -08002831 int may;
2832
2833 switch (cmd) {
2834 case GETPID:
2835 case GETNCNT:
2836 case GETZCNT:
2837 case GETVAL:
2838 case GETALL:
2839 case IPC_STAT:
2840 case SEM_STAT:
2841 may = MAY_READ;
2842 break;
2843 case SETVAL:
2844 case SETALL:
2845 case IPC_RMID:
2846 case IPC_SET:
2847 may = MAY_READWRITE;
2848 break;
2849 case IPC_INFO:
2850 case SEM_INFO:
2851 /*
2852 * System level information
2853 */
2854 return 0;
2855 default:
2856 return -EINVAL;
2857 }
2858
Etienne Bassetecfcc532009-04-08 20:40:06 +02002859 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002860}
2861
2862/**
2863 * smack_sem_semop - Smack checks of semaphore operations
2864 * @sma: the object
2865 * @sops: unused
2866 * @nsops: unused
2867 * @alter: unused
2868 *
2869 * Treated as read and write in all cases.
2870 *
2871 * Returns 0 if access is allowed, error code otherwise
2872 */
2873static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2874 unsigned nsops, int alter)
2875{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002876 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002877}
2878
2879/**
2880 * smack_msg_alloc_security - Set the security blob for msg
2881 * @msq: the object
2882 *
2883 * Returns 0
2884 */
2885static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2886{
2887 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002888 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002889
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002890 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002891 return 0;
2892}
2893
2894/**
2895 * smack_msg_free_security - Clear the security blob for msg
2896 * @msq: the object
2897 *
2898 * Clears the blob pointer
2899 */
2900static void smack_msg_queue_free_security(struct msg_queue *msq)
2901{
2902 struct kern_ipc_perm *kisp = &msq->q_perm;
2903
2904 kisp->security = NULL;
2905}
2906
2907/**
2908 * smack_of_msq - the smack pointer for the msq
2909 * @msq: the object
2910 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002911 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002912 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002913static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002914{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002915 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002916}
2917
2918/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002919 * smk_curacc_msq : helper to check if current has access on msq
2920 * @msq : the msq
2921 * @access : access requested
2922 *
2923 * return 0 if current has access, error otherwise
2924 */
2925static int smk_curacc_msq(struct msg_queue *msq, int access)
2926{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002927 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002928 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002929 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002930
2931#ifdef CONFIG_AUDIT
2932 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2933 ad.a.u.ipc_id = msq->q_perm.id;
2934#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002935 rc = smk_curacc(msp, access, &ad);
2936 rc = smk_bu_current("msq", msp, access, rc);
2937 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002938}
2939
2940/**
Casey Schauflere114e472008-02-04 22:29:50 -08002941 * smack_msg_queue_associate - Smack access check for msg_queue
2942 * @msq: the object
2943 * @msqflg: access requested
2944 *
2945 * Returns 0 if current has the requested access, error code otherwise
2946 */
2947static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2948{
Casey Schauflere114e472008-02-04 22:29:50 -08002949 int may;
2950
2951 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002952 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002953}
2954
2955/**
2956 * smack_msg_queue_msgctl - Smack access check for msg_queue
2957 * @msq: the object
2958 * @cmd: what it wants to do
2959 *
2960 * Returns 0 if current has the requested access, error code otherwise
2961 */
2962static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2963{
Casey Schauflere114e472008-02-04 22:29:50 -08002964 int may;
2965
2966 switch (cmd) {
2967 case IPC_STAT:
2968 case MSG_STAT:
2969 may = MAY_READ;
2970 break;
2971 case IPC_SET:
2972 case IPC_RMID:
2973 may = MAY_READWRITE;
2974 break;
2975 case IPC_INFO:
2976 case MSG_INFO:
2977 /*
2978 * System level information
2979 */
2980 return 0;
2981 default:
2982 return -EINVAL;
2983 }
2984
Etienne Bassetecfcc532009-04-08 20:40:06 +02002985 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002986}
2987
2988/**
2989 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2990 * @msq: the object
2991 * @msg: unused
2992 * @msqflg: access requested
2993 *
2994 * Returns 0 if current has the requested access, error code otherwise
2995 */
2996static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2997 int msqflg)
2998{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002999 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003000
Etienne Bassetecfcc532009-04-08 20:40:06 +02003001 may = smack_flags_to_may(msqflg);
3002 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003003}
3004
3005/**
3006 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3007 * @msq: the object
3008 * @msg: unused
3009 * @target: unused
3010 * @type: unused
3011 * @mode: unused
3012 *
3013 * Returns 0 if current has read and write access, error code otherwise
3014 */
3015static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3016 struct task_struct *target, long type, int mode)
3017{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003018 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003019}
3020
3021/**
3022 * smack_ipc_permission - Smack access for ipc_permission()
3023 * @ipp: the object permissions
3024 * @flag: access requested
3025 *
3026 * Returns 0 if current has read and write access, error code otherwise
3027 */
3028static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3029{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003030 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003031 int may = smack_flags_to_may(flag);
3032 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003033 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003034
Etienne Bassetecfcc532009-04-08 20:40:06 +02003035#ifdef CONFIG_AUDIT
3036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3037 ad.a.u.ipc_id = ipp->id;
3038#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003039 rc = smk_curacc(iskp, may, &ad);
3040 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003041 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003042}
3043
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003044/**
3045 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003046 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003047 * @secid: where result will be saved
3048 */
3049static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3050{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003051 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003052
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003053 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003054}
3055
Casey Schauflere114e472008-02-04 22:29:50 -08003056/**
3057 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003058 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003059 * @inode: the object
3060 *
3061 * Set the inode's security blob if it hasn't been done already.
3062 */
3063static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3064{
3065 struct super_block *sbp;
3066 struct superblock_smack *sbsp;
3067 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003068 struct smack_known *skp;
3069 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003070 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003071 char trattr[TRANS_TRUE_SIZE];
3072 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003073 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003074 struct dentry *dp;
3075
3076 if (inode == NULL)
3077 return;
3078
3079 isp = inode->i_security;
3080
3081 mutex_lock(&isp->smk_lock);
3082 /*
3083 * If the inode is already instantiated
3084 * take the quick way out
3085 */
3086 if (isp->smk_flags & SMK_INODE_INSTANT)
3087 goto unlockandout;
3088
3089 sbp = inode->i_sb;
3090 sbsp = sbp->s_security;
3091 /*
3092 * We're going to use the superblock default label
3093 * if there's no label on the file.
3094 */
3095 final = sbsp->smk_default;
3096
3097 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003098 * If this is the root inode the superblock
3099 * may be in the process of initialization.
3100 * If that is the case use the root value out
3101 * of the superblock.
3102 */
3103 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003104 switch (sbp->s_magic) {
3105 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003106 /*
3107 * The cgroup filesystem is never mounted,
3108 * so there's no opportunity to set the mount
3109 * options.
3110 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003111 sbsp->smk_root = &smack_known_star;
3112 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003113 isp->smk_inode = sbsp->smk_root;
3114 break;
3115 case TMPFS_MAGIC:
3116 /*
3117 * What about shmem/tmpfs anonymous files with dentry
3118 * obtained from d_alloc_pseudo()?
3119 */
3120 isp->smk_inode = smk_of_current();
3121 break;
3122 default:
3123 isp->smk_inode = sbsp->smk_root;
3124 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003125 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003126 isp->smk_flags |= SMK_INODE_INSTANT;
3127 goto unlockandout;
3128 }
3129
3130 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003131 * This is pretty hackish.
3132 * Casey says that we shouldn't have to do
3133 * file system specific code, but it does help
3134 * with keeping it simple.
3135 */
3136 switch (sbp->s_magic) {
3137 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003138 case PIPEFS_MAGIC:
3139 case SOCKFS_MAGIC:
3140 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003141 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003142 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003143 * that the smack file system doesn't do
3144 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003145 *
Casey Schauflere114e472008-02-04 22:29:50 -08003146 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003147 *
3148 * Socket access is controlled by the socket
3149 * structures associated with the task involved.
3150 *
3151 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003152 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003153 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003154 break;
3155 case DEVPTS_SUPER_MAGIC:
3156 /*
3157 * devpts seems content with the label of the task.
3158 * Programs that change smack have to treat the
3159 * pty with respect.
3160 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003161 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003162 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003163 case PROC_SUPER_MAGIC:
3164 /*
3165 * Casey says procfs appears not to care.
3166 * The superblock default suffices.
3167 */
3168 break;
3169 case TMPFS_MAGIC:
3170 /*
3171 * Device labels should come from the filesystem,
3172 * but watch out, because they're volitile,
3173 * getting recreated on every reboot.
3174 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003175 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003176 /*
3177 * No break.
3178 *
3179 * If a smack value has been set we want to use it,
3180 * but since tmpfs isn't giving us the opportunity
3181 * to set mount options simulate setting the
3182 * superblock default.
3183 */
3184 default:
3185 /*
3186 * This isn't an understood special case.
3187 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003188 */
3189
3190 /*
3191 * UNIX domain sockets use lower level socket data.
3192 */
3193 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003194 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003195 break;
3196 }
3197 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003198 * No xattr support means, alas, no SMACK label.
3199 * Use the aforeapplied default.
3200 * It would be curious if the label of the task
3201 * does not match that assigned.
3202 */
3203 if (inode->i_op->getxattr == NULL)
3204 break;
3205 /*
3206 * Get the dentry for xattr.
3207 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003208 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003209 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3210 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003211 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003212
3213 /*
3214 * Transmuting directory
3215 */
3216 if (S_ISDIR(inode->i_mode)) {
3217 /*
3218 * If this is a new directory and the label was
3219 * transmuted when the inode was initialized
3220 * set the transmute attribute on the directory
3221 * and mark the inode.
3222 *
3223 * If there is a transmute attribute on the
3224 * directory mark the inode.
3225 */
3226 if (isp->smk_flags & SMK_INODE_CHANGED) {
3227 isp->smk_flags &= ~SMK_INODE_CHANGED;
3228 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003229 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003230 TRANS_TRUE, TRANS_TRUE_SIZE,
3231 0);
3232 } else {
3233 rc = inode->i_op->getxattr(dp,
3234 XATTR_NAME_SMACKTRANSMUTE, trattr,
3235 TRANS_TRUE_SIZE);
3236 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3237 TRANS_TRUE_SIZE) != 0)
3238 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003239 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003240 if (rc >= 0)
3241 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003242 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003243 /*
3244 * Don't let the exec or mmap label be "*" or "@".
3245 */
3246 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3247 if (skp == &smack_known_star || skp == &smack_known_web)
3248 skp = NULL;
3249 isp->smk_task = skp;
3250 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3251 if (skp == &smack_known_star || skp == &smack_known_web)
3252 skp = NULL;
3253 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003254
Casey Schauflere114e472008-02-04 22:29:50 -08003255 dput(dp);
3256 break;
3257 }
3258
3259 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003260 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003261 else
3262 isp->smk_inode = final;
3263
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003264 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003265
3266unlockandout:
3267 mutex_unlock(&isp->smk_lock);
3268 return;
3269}
3270
3271/**
3272 * smack_getprocattr - Smack process attribute access
3273 * @p: the object task
3274 * @name: the name of the attribute in /proc/.../attr
3275 * @value: where to put the result
3276 *
3277 * Places a copy of the task Smack into value
3278 *
3279 * Returns the length of the smack label or an error code
3280 */
3281static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3282{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003283 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003284 char *cp;
3285 int slen;
3286
3287 if (strcmp(name, "current") != 0)
3288 return -EINVAL;
3289
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003290 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003291 if (cp == NULL)
3292 return -ENOMEM;
3293
3294 slen = strlen(cp);
3295 *value = cp;
3296 return slen;
3297}
3298
3299/**
3300 * smack_setprocattr - Smack process attribute setting
3301 * @p: the object task
3302 * @name: the name of the attribute in /proc/.../attr
3303 * @value: the value to set
3304 * @size: the size of the value
3305 *
3306 * Sets the Smack value of the task. Only setting self
3307 * is permitted and only with privilege
3308 *
3309 * Returns the length of the smack label or an error code
3310 */
3311static int smack_setprocattr(struct task_struct *p, char *name,
3312 void *value, size_t size)
3313{
Casey Schaufler676dac42010-12-02 06:43:39 -08003314 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003315 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003316 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003317
Casey Schauflere114e472008-02-04 22:29:50 -08003318 /*
3319 * Changing another process' Smack value is too dangerous
3320 * and supports no sane use case.
3321 */
3322 if (p != current)
3323 return -EPERM;
3324
Casey Schaufler1880eff2012-06-05 15:28:30 -07003325 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003326 return -EPERM;
3327
Casey Schauflerf7112e62012-05-06 15:22:02 -07003328 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003329 return -EINVAL;
3330
3331 if (strcmp(name, "current") != 0)
3332 return -EINVAL;
3333
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003334 skp = smk_import_entry(value, size);
3335 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003336 return -EINVAL;
3337
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003338 /*
3339 * No process is ever allowed the web ("@") label.
3340 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003341 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003342 return -EPERM;
3343
David Howellsd84f4f92008-11-14 10:39:23 +11003344 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003345 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003346 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003347
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003348 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003349 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003350
David Howellsd84f4f92008-11-14 10:39:23 +11003351 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003352 return size;
3353}
3354
3355/**
3356 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003357 * @sock: one sock
3358 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003359 * @newsk: unused
3360 *
3361 * Return 0 if a subject with the smack of sock could access
3362 * an object with the smack of other, otherwise an error code
3363 */
David S. Miller3610cda2011-01-05 15:38:53 -08003364static int smack_unix_stream_connect(struct sock *sock,
3365 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003366{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003367 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003368 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003369 struct socket_smack *ssp = sock->sk_security;
3370 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003371 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003372 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003373 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003374#ifdef CONFIG_AUDIT
3375 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003376#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003377
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003378 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3379 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003380 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003381#ifdef CONFIG_AUDIT
3382 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3383 smk_ad_setfield_u_net_sk(&ad, other);
3384#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003385 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3386 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003387 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003388 okp = osp->smk_out;
3389 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003390 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003391 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003392 MAY_WRITE, rc);
3393 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003394 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003395
Casey Schaufler975d5e52011-09-26 14:43:39 -07003396 /*
3397 * Cross reference the peer labels for SO_PEERSEC.
3398 */
3399 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003400 nsp->smk_packet = ssp->smk_out;
3401 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003402 }
3403
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003404 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003405}
3406
3407/**
3408 * smack_unix_may_send - Smack access on UDS
3409 * @sock: one socket
3410 * @other: the other socket
3411 *
3412 * Return 0 if a subject with the smack of sock could access
3413 * an object with the smack of other, otherwise an error code
3414 */
3415static int smack_unix_may_send(struct socket *sock, struct socket *other)
3416{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003417 struct socket_smack *ssp = sock->sk->sk_security;
3418 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003419 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003420 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003421
Kees Cook923e9a12012-04-10 13:26:44 -07003422#ifdef CONFIG_AUDIT
3423 struct lsm_network_audit net;
3424
Eric Paris48c62af2012-04-02 13:15:44 -04003425 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003426 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003427#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003428
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003429 if (smack_privileged(CAP_MAC_OVERRIDE))
3430 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003431
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003432 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3433 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003434 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003435}
3436
3437/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003438 * smack_socket_sendmsg - Smack check based on destination host
3439 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003440 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003441 * @size: the size of the message
3442 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003443 * Return 0 if the current subject can write to the destination host.
3444 * For IPv4 this is only a question if the destination is a single label host.
3445 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003446 */
3447static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3448 int size)
3449{
3450 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003451#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003452 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003453#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003454 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003455
3456 /*
3457 * Perfectly reasonable for this to be NULL
3458 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003459 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003460 return 0;
3461
Casey Schauflerc6739442013-05-22 18:42:56 -07003462 switch (sip->sin_family) {
3463 case AF_INET:
3464 rc = smack_netlabel_send(sock->sk, sip);
3465 break;
3466 case AF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003467#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07003468 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003469#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003470 break;
3471 }
3472 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003473}
3474
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003475/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003476 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003477 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003478 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003479 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003480 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003481 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003482static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3483 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003484{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003485 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003486 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003487 int acat;
3488 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003489
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003490 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003491 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003492 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003493 * If there are flags but no level netlabel isn't
3494 * behaving the way we expect it to.
3495 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003496 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003497 * Without guidance regarding the smack value
3498 * for the packet fall back on the network
3499 * ambient value.
3500 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003501 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003502 list_for_each_entry(skp, &smack_known_list, list) {
3503 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003504 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003505 /*
3506 * Compare the catsets. Use the netlbl APIs.
3507 */
3508 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3509 if ((skp->smk_netlabel.flags &
3510 NETLBL_SECATTR_MLS_CAT) == 0)
3511 found = 1;
3512 break;
3513 }
3514 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003515 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3516 acat + 1);
3517 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003518 skp->smk_netlabel.attr.mls.cat,
3519 kcat + 1);
3520 if (acat < 0 || kcat < 0)
3521 break;
3522 }
3523 if (acat == kcat) {
3524 found = 1;
3525 break;
3526 }
Casey Schauflere114e472008-02-04 22:29:50 -08003527 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003528 rcu_read_unlock();
3529
3530 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003531 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003532
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003533 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003534 return &smack_known_web;
3535 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003536 }
3537 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3538 /*
3539 * Looks like a fallback, which gives us a secid.
3540 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003541 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003542 /*
3543 * This has got to be a bug because it is
3544 * impossible to specify a fallback without
3545 * specifying the label, which will ensure
3546 * it has a secid, and the only way to get a
3547 * secid is from a fallback.
3548 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003549 BUG_ON(skp == NULL);
3550 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003551 }
3552 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003553 * Without guidance regarding the smack value
3554 * for the packet fall back on the network
3555 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003556 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003557 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003558}
3559
Casey Schaufler69f287a2014-12-12 17:08:40 -08003560#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003561static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003562{
Casey Schauflerc6739442013-05-22 18:42:56 -07003563 u8 nexthdr;
3564 int offset;
3565 int proto = -EINVAL;
3566 struct ipv6hdr _ipv6h;
3567 struct ipv6hdr *ip6;
3568 __be16 frag_off;
3569 struct tcphdr _tcph, *th;
3570 struct udphdr _udph, *uh;
3571 struct dccp_hdr _dccph, *dh;
3572
3573 sip->sin6_port = 0;
3574
3575 offset = skb_network_offset(skb);
3576 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3577 if (ip6 == NULL)
3578 return -EINVAL;
3579 sip->sin6_addr = ip6->saddr;
3580
3581 nexthdr = ip6->nexthdr;
3582 offset += sizeof(_ipv6h);
3583 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3584 if (offset < 0)
3585 return -EINVAL;
3586
3587 proto = nexthdr;
3588 switch (proto) {
3589 case IPPROTO_TCP:
3590 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3591 if (th != NULL)
3592 sip->sin6_port = th->source;
3593 break;
3594 case IPPROTO_UDP:
3595 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3596 if (uh != NULL)
3597 sip->sin6_port = uh->source;
3598 break;
3599 case IPPROTO_DCCP:
3600 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3601 if (dh != NULL)
3602 sip->sin6_port = dh->dccph_sport;
3603 break;
3604 }
3605 return proto;
3606}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003607#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003608
Casey Schauflere114e472008-02-04 22:29:50 -08003609/**
3610 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3611 * @sk: socket
3612 * @skb: packet
3613 *
3614 * Returns 0 if the packet should be delivered, an error code otherwise
3615 */
3616static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3617{
3618 struct netlbl_lsm_secattr secattr;
3619 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003620 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003621 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003622 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003623#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003624 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003625#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003626#if IS_ENABLED(CONFIG_IPV6)
3627 struct sockaddr_in6 sadd;
3628 int proto;
3629#endif /* CONFIG_IPV6 */
3630
Casey Schauflerc6739442013-05-22 18:42:56 -07003631 switch (sk->sk_family) {
3632 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003633#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3634 /*
3635 * If there is a secmark use it rather than the CIPSO label.
3636 * If there is no secmark fall back to CIPSO.
3637 * The secmark is assumed to reflect policy better.
3638 */
3639 if (skb && skb->secmark != 0) {
3640 skp = smack_from_secid(skb->secmark);
3641 goto access_check;
3642 }
3643#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003644 /*
3645 * Translate what netlabel gave us.
3646 */
3647 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003648
Casey Schauflerc6739442013-05-22 18:42:56 -07003649 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3650 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003651 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003652 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003653 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003654
Casey Schauflerc6739442013-05-22 18:42:56 -07003655 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003656
Casey Schaufler69f287a2014-12-12 17:08:40 -08003657#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3658access_check:
3659#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003660#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003661 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3662 ad.a.u.net->family = sk->sk_family;
3663 ad.a.u.net->netif = skb->skb_iif;
3664 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003665#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003666 /*
3667 * Receiving a packet requires that the other end
3668 * be able to write here. Read access is not required.
3669 * This is the simplist possible security model
3670 * for networking.
3671 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003672 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3673 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003674 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003675 if (rc != 0)
3676 netlbl_skbuff_err(skb, rc, 0);
3677 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003678#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003679 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003680 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3681 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3682 break;
3683#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3684 if (skb && skb->secmark != 0)
3685 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003686 else
Casey Schaufler69f287a2014-12-12 17:08:40 -08003687 skp = smack_net_ambient;
3688#ifdef CONFIG_AUDIT
3689 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3690 ad.a.u.net->family = sk->sk_family;
3691 ad.a.u.net->netif = skb->skb_iif;
3692 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3693#endif /* CONFIG_AUDIT */
3694 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3695 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3696 MAY_WRITE, rc);
3697#else /* CONFIG_SECURITY_SMACK_NETFILTER */
3698 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3699#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003700 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003701#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003702 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003703
Paul Moorea8134292008-10-10 10:16:31 -04003704 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003705}
3706
3707/**
3708 * smack_socket_getpeersec_stream - pull in packet label
3709 * @sock: the socket
3710 * @optval: user's destination
3711 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003712 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003713 *
3714 * returns zero on success, an error code otherwise
3715 */
3716static int smack_socket_getpeersec_stream(struct socket *sock,
3717 char __user *optval,
3718 int __user *optlen, unsigned len)
3719{
3720 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003721 char *rcp = "";
3722 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003723 int rc = 0;
3724
3725 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003726 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003727 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003728 slen = strlen(rcp) + 1;
3729 }
Casey Schauflere114e472008-02-04 22:29:50 -08003730
3731 if (slen > len)
3732 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003733 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003734 rc = -EFAULT;
3735
3736 if (put_user(slen, optlen) != 0)
3737 rc = -EFAULT;
3738
3739 return rc;
3740}
3741
3742
3743/**
3744 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003745 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003746 * @skb: packet data
3747 * @secid: pointer to where to put the secid of the packet
3748 *
3749 * Sets the netlabel socket state on sk from parent
3750 */
3751static int smack_socket_getpeersec_dgram(struct socket *sock,
3752 struct sk_buff *skb, u32 *secid)
3753
3754{
3755 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003756 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003757 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003758 int family = PF_UNSPEC;
3759 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003760 int rc;
3761
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003762 if (skb != NULL) {
3763 if (skb->protocol == htons(ETH_P_IP))
3764 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003765#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003766 else if (skb->protocol == htons(ETH_P_IPV6))
3767 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003768#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003769 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003770 if (family == PF_UNSPEC && sock != NULL)
3771 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003772
Casey Schaufler69f287a2014-12-12 17:08:40 -08003773 switch (family) {
3774 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003775 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003776 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003777 break;
3778 case PF_INET:
3779#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3780 s = skb->secmark;
3781 if (s != 0)
3782 break;
3783#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003784 /*
3785 * Translate what netlabel gave us.
3786 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003787 if (sock != NULL && sock->sk != NULL)
3788 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003789 netlbl_secattr_init(&secattr);
3790 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3791 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003792 skp = smack_from_secattr(&secattr, ssp);
3793 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003794 }
3795 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003796 break;
3797#if IS_ENABLED(CONFIG_IPV6)
3798 case PF_INET6:
3799#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3800 s = skb->secmark;
3801#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3802 break;
3803#endif /* CONFIG_IPV6 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003804 }
3805 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003806 if (s == 0)
3807 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003808 return 0;
3809}
3810
3811/**
Paul Moore07feee82009-03-27 17:10:54 -04003812 * smack_sock_graft - Initialize a newly created socket with an existing sock
3813 * @sk: child sock
3814 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003815 *
Paul Moore07feee82009-03-27 17:10:54 -04003816 * Set the smk_{in,out} state of an existing sock based on the process that
3817 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003818 */
3819static void smack_sock_graft(struct sock *sk, struct socket *parent)
3820{
3821 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003822 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003823
Paul Moore07feee82009-03-27 17:10:54 -04003824 if (sk == NULL ||
3825 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003826 return;
3827
3828 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003829 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003830 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003831 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003832}
3833
3834/**
3835 * smack_inet_conn_request - Smack access check on connect
3836 * @sk: socket involved
3837 * @skb: packet
3838 * @req: unused
3839 *
3840 * Returns 0 if a task with the packet label could write to
3841 * the socket, otherwise an error code
3842 */
3843static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3844 struct request_sock *req)
3845{
Paul Moore07feee82009-03-27 17:10:54 -04003846 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003847 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003848 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003849 struct netlbl_lsm_secattr secattr;
3850 struct sockaddr_in addr;
3851 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003852 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003853 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003854 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003855#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003856 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003857#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003858
Casey Schaufler69f287a2014-12-12 17:08:40 -08003859#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003860 if (family == PF_INET6) {
3861 /*
3862 * Handle mapped IPv4 packets arriving
3863 * via IPv6 sockets. Don't set up netlabel
3864 * processing on IPv6.
3865 */
3866 if (skb->protocol == htons(ETH_P_IP))
3867 family = PF_INET;
3868 else
3869 return 0;
3870 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003871#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003872
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003873#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3874 /*
3875 * If there is a secmark use it rather than the CIPSO label.
3876 * If there is no secmark fall back to CIPSO.
3877 * The secmark is assumed to reflect policy better.
3878 */
3879 if (skb && skb->secmark != 0) {
3880 skp = smack_from_secid(skb->secmark);
3881 goto access_check;
3882 }
3883#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3884
Paul Moore07feee82009-03-27 17:10:54 -04003885 netlbl_secattr_init(&secattr);
3886 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003887 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003888 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003889 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003890 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003891 netlbl_secattr_destroy(&secattr);
3892
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003893#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3894access_check:
3895#endif
3896
Etienne Bassetecfcc532009-04-08 20:40:06 +02003897#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003898 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3899 ad.a.u.net->family = family;
3900 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003901 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3902#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003903 /*
Paul Moore07feee82009-03-27 17:10:54 -04003904 * Receiving a packet requires that the other end be able to write
3905 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003906 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003907 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3908 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003909 if (rc != 0)
3910 return rc;
3911
3912 /*
3913 * Save the peer's label in the request_sock so we can later setup
3914 * smk_packet in the child socket so that SO_PEERCRED can report it.
3915 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003916 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003917
3918 /*
3919 * We need to decide if we want to label the incoming connection here
3920 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003921 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003922 */
3923 hdr = ip_hdr(skb);
3924 addr.sin_addr.s_addr = hdr->saddr;
3925 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003926 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003927 rcu_read_unlock();
3928
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003929 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003930 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003931 else
Paul Moore07feee82009-03-27 17:10:54 -04003932 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003933
3934 return rc;
3935}
3936
Paul Moore07feee82009-03-27 17:10:54 -04003937/**
3938 * smack_inet_csk_clone - Copy the connection information to the new socket
3939 * @sk: the new socket
3940 * @req: the connection's request_sock
3941 *
3942 * Transfer the connection's peer label to the newly created socket.
3943 */
3944static void smack_inet_csk_clone(struct sock *sk,
3945 const struct request_sock *req)
3946{
3947 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003948 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003949
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003950 if (req->peer_secid != 0) {
3951 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003952 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003953 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003954 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003955}
3956
Casey Schauflere114e472008-02-04 22:29:50 -08003957/*
3958 * Key management security hooks
3959 *
3960 * Casey has not tested key support very heavily.
3961 * The permission check is most likely too restrictive.
3962 * If you care about keys please have a look.
3963 */
3964#ifdef CONFIG_KEYS
3965
3966/**
3967 * smack_key_alloc - Set the key security blob
3968 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003969 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003970 * @flags: unused
3971 *
3972 * No allocation required
3973 *
3974 * Returns 0
3975 */
David Howellsd84f4f92008-11-14 10:39:23 +11003976static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003977 unsigned long flags)
3978{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003979 struct smack_known *skp = smk_of_task(cred->security);
3980
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003981 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003982 return 0;
3983}
3984
3985/**
3986 * smack_key_free - Clear the key security blob
3987 * @key: the object
3988 *
3989 * Clear the blob pointer
3990 */
3991static void smack_key_free(struct key *key)
3992{
3993 key->security = NULL;
3994}
3995
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003996/**
Casey Schauflere114e472008-02-04 22:29:50 -08003997 * smack_key_permission - Smack access on a key
3998 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003999 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004000 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004001 *
4002 * Return 0 if the task has read and write to the object,
4003 * an error code otherwise
4004 */
4005static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004006 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004007{
4008 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004009 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004010 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004011 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004012 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004013
4014 keyp = key_ref_to_ptr(key_ref);
4015 if (keyp == NULL)
4016 return -EINVAL;
4017 /*
4018 * If the key hasn't been initialized give it access so that
4019 * it may do so.
4020 */
4021 if (keyp->security == NULL)
4022 return 0;
4023 /*
4024 * This should not occur
4025 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004026 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004027 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004028#ifdef CONFIG_AUDIT
4029 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4030 ad.a.u.key_struct.key = keyp->serial;
4031 ad.a.u.key_struct.key_desc = keyp->description;
4032#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004033 if (perm & KEY_NEED_READ)
4034 request = MAY_READ;
4035 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4036 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004037 rc = smk_access(tkp, keyp->security, request, &ad);
4038 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4039 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004040}
José Bollo7fc5f362015-02-17 15:41:22 +01004041
4042/*
4043 * smack_key_getsecurity - Smack label tagging the key
4044 * @key points to the key to be queried
4045 * @_buffer points to a pointer that should be set to point to the
4046 * resulting string (if no label or an error occurs).
4047 * Return the length of the string (including terminating NUL) or -ve if
4048 * an error.
4049 * May also return 0 (and a NULL buffer pointer) if there is no label.
4050 */
4051static int smack_key_getsecurity(struct key *key, char **_buffer)
4052{
4053 struct smack_known *skp = key->security;
4054 size_t length;
4055 char *copy;
4056
4057 if (key->security == NULL) {
4058 *_buffer = NULL;
4059 return 0;
4060 }
4061
4062 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4063 if (copy == NULL)
4064 return -ENOMEM;
4065 length = strlen(copy) + 1;
4066
4067 *_buffer = copy;
4068 return length;
4069}
4070
Casey Schauflere114e472008-02-04 22:29:50 -08004071#endif /* CONFIG_KEYS */
4072
4073/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004074 * Smack Audit hooks
4075 *
4076 * Audit requires a unique representation of each Smack specific
4077 * rule. This unique representation is used to distinguish the
4078 * object to be audited from remaining kernel objects and also
4079 * works as a glue between the audit hooks.
4080 *
4081 * Since repository entries are added but never deleted, we'll use
4082 * the smack_known label address related to the given audit rule as
4083 * the needed unique representation. This also better fits the smack
4084 * model where nearly everything is a label.
4085 */
4086#ifdef CONFIG_AUDIT
4087
4088/**
4089 * smack_audit_rule_init - Initialize a smack audit rule
4090 * @field: audit rule fields given from user-space (audit.h)
4091 * @op: required testing operator (=, !=, >, <, ...)
4092 * @rulestr: smack label to be audited
4093 * @vrule: pointer to save our own audit rule representation
4094 *
4095 * Prepare to audit cases where (@field @op @rulestr) is true.
4096 * The label to be audited is created if necessay.
4097 */
4098static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4099{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004100 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004101 char **rule = (char **)vrule;
4102 *rule = NULL;
4103
4104 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4105 return -EINVAL;
4106
Al Viro5af75d82008-12-16 05:59:26 -05004107 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004108 return -EINVAL;
4109
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004110 skp = smk_import_entry(rulestr, 0);
4111 if (skp)
4112 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004113
4114 return 0;
4115}
4116
4117/**
4118 * smack_audit_rule_known - Distinguish Smack audit rules
4119 * @krule: rule of interest, in Audit kernel representation format
4120 *
4121 * This is used to filter Smack rules from remaining Audit ones.
4122 * If it's proved that this rule belongs to us, the
4123 * audit_rule_match hook will be called to do the final judgement.
4124 */
4125static int smack_audit_rule_known(struct audit_krule *krule)
4126{
4127 struct audit_field *f;
4128 int i;
4129
4130 for (i = 0; i < krule->field_count; i++) {
4131 f = &krule->fields[i];
4132
4133 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4134 return 1;
4135 }
4136
4137 return 0;
4138}
4139
4140/**
4141 * smack_audit_rule_match - Audit given object ?
4142 * @secid: security id for identifying the object to test
4143 * @field: audit rule flags given from user-space
4144 * @op: required testing operator
4145 * @vrule: smack internal rule presentation
4146 * @actx: audit context associated with the check
4147 *
4148 * The core Audit hook. It's used to take the decision of
4149 * whether to audit or not to audit a given object.
4150 */
4151static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4152 struct audit_context *actx)
4153{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004154 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004155 char *rule = vrule;
4156
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004157 if (unlikely(!rule)) {
4158 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004159 return -ENOENT;
4160 }
4161
4162 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4163 return 0;
4164
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004165 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004166
4167 /*
4168 * No need to do string comparisons. If a match occurs,
4169 * both pointers will point to the same smack_known
4170 * label.
4171 */
Al Viro5af75d82008-12-16 05:59:26 -05004172 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004173 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004174 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004175 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004176
4177 return 0;
4178}
4179
4180/**
4181 * smack_audit_rule_free - free smack rule representation
4182 * @vrule: rule to be freed.
4183 *
4184 * No memory was allocated.
4185 */
4186static void smack_audit_rule_free(void *vrule)
4187{
4188 /* No-op */
4189}
4190
4191#endif /* CONFIG_AUDIT */
4192
Randy Dunlap251a2a92009-02-18 11:42:33 -08004193/**
David Quigley746df9b2013-05-22 12:50:35 -04004194 * smack_ismaclabel - check if xattr @name references a smack MAC label
4195 * @name: Full xattr name to check.
4196 */
4197static int smack_ismaclabel(const char *name)
4198{
4199 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4200}
4201
4202
4203/**
Casey Schauflere114e472008-02-04 22:29:50 -08004204 * smack_secid_to_secctx - return the smack label for a secid
4205 * @secid: incoming integer
4206 * @secdata: destination
4207 * @seclen: how long it is
4208 *
4209 * Exists for networking code.
4210 */
4211static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4212{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004213 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004214
Eric Parisd5630b92010-10-13 16:24:48 -04004215 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004216 *secdata = skp->smk_known;
4217 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004218 return 0;
4219}
4220
Randy Dunlap251a2a92009-02-18 11:42:33 -08004221/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004222 * smack_secctx_to_secid - return the secid for a smack label
4223 * @secdata: smack label
4224 * @seclen: how long result is
4225 * @secid: outgoing integer
4226 *
4227 * Exists for audit and networking code.
4228 */
David Howellse52c17642008-04-29 20:52:51 +01004229static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004230{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004231 struct smack_known *skp = smk_find_entry(secdata);
4232
4233 if (skp)
4234 *secid = skp->smk_secid;
4235 else
4236 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004237 return 0;
4238}
4239
Randy Dunlap251a2a92009-02-18 11:42:33 -08004240/**
Casey Schauflere114e472008-02-04 22:29:50 -08004241 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004242 * @secdata: unused
4243 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004244 *
4245 * Exists to make sure nothing gets done, and properly
4246 */
4247static void smack_release_secctx(char *secdata, u32 seclen)
4248{
4249}
4250
David P. Quigley1ee65e32009-09-03 14:25:57 -04004251static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4252{
4253 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4254}
4255
4256static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4257{
4258 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4259}
4260
4261static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4262{
4263 int len = 0;
4264 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4265
4266 if (len < 0)
4267 return len;
4268 *ctxlen = len;
4269 return 0;
4270}
4271
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02004272struct security_operations smack_ops = {
4273 .name = "smack",
4274
Ingo Molnar9e488582009-05-07 19:26:19 +10004275 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01004276 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08004277 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08004278
4279 .sb_alloc_security = smack_sb_alloc_security,
4280 .sb_free_security = smack_sb_free_security,
4281 .sb_copy_data = smack_sb_copy_data,
4282 .sb_kern_mount = smack_sb_kern_mount,
4283 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08004284
Casey Schaufler676dac42010-12-02 06:43:39 -08004285 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03004286 .bprm_committing_creds = smack_bprm_committing_creds,
4287 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08004288
Casey Schauflere114e472008-02-04 22:29:50 -08004289 .inode_alloc_security = smack_inode_alloc_security,
4290 .inode_free_security = smack_inode_free_security,
4291 .inode_init_security = smack_inode_init_security,
4292 .inode_link = smack_inode_link,
4293 .inode_unlink = smack_inode_unlink,
4294 .inode_rmdir = smack_inode_rmdir,
4295 .inode_rename = smack_inode_rename,
4296 .inode_permission = smack_inode_permission,
4297 .inode_setattr = smack_inode_setattr,
4298 .inode_getattr = smack_inode_getattr,
4299 .inode_setxattr = smack_inode_setxattr,
4300 .inode_post_setxattr = smack_inode_post_setxattr,
4301 .inode_getxattr = smack_inode_getxattr,
4302 .inode_removexattr = smack_inode_removexattr,
4303 .inode_getsecurity = smack_inode_getsecurity,
4304 .inode_setsecurity = smack_inode_setsecurity,
4305 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004306 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004307
4308 .file_permission = smack_file_permission,
4309 .file_alloc_security = smack_file_alloc_security,
4310 .file_free_security = smack_file_free_security,
4311 .file_ioctl = smack_file_ioctl,
4312 .file_lock = smack_file_lock,
4313 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04004314 .mmap_file = smack_mmap_file,
4315 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08004316 .file_set_fowner = smack_file_set_fowner,
4317 .file_send_sigiotask = smack_file_send_sigiotask,
4318 .file_receive = smack_file_receive,
4319
Eric Paris83d49852012-04-04 13:45:40 -04004320 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07004321
David Howellsee18d642009-09-02 09:14:21 +01004322 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11004323 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11004324 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01004325 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11004326 .kernel_act_as = smack_kernel_act_as,
4327 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08004328 .task_setpgid = smack_task_setpgid,
4329 .task_getpgid = smack_task_getpgid,
4330 .task_getsid = smack_task_getsid,
4331 .task_getsecid = smack_task_getsecid,
4332 .task_setnice = smack_task_setnice,
4333 .task_setioprio = smack_task_setioprio,
4334 .task_getioprio = smack_task_getioprio,
4335 .task_setscheduler = smack_task_setscheduler,
4336 .task_getscheduler = smack_task_getscheduler,
4337 .task_movememory = smack_task_movememory,
4338 .task_kill = smack_task_kill,
4339 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08004340 .task_to_inode = smack_task_to_inode,
4341
4342 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004343 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004344
4345 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4346 .msg_msg_free_security = smack_msg_msg_free_security,
4347
4348 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4349 .msg_queue_free_security = smack_msg_queue_free_security,
4350 .msg_queue_associate = smack_msg_queue_associate,
4351 .msg_queue_msgctl = smack_msg_queue_msgctl,
4352 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4353 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4354
4355 .shm_alloc_security = smack_shm_alloc_security,
4356 .shm_free_security = smack_shm_free_security,
4357 .shm_associate = smack_shm_associate,
4358 .shm_shmctl = smack_shm_shmctl,
4359 .shm_shmat = smack_shm_shmat,
4360
4361 .sem_alloc_security = smack_sem_alloc_security,
4362 .sem_free_security = smack_sem_free_security,
4363 .sem_associate = smack_sem_associate,
4364 .sem_semctl = smack_sem_semctl,
4365 .sem_semop = smack_sem_semop,
4366
Casey Schauflere114e472008-02-04 22:29:50 -08004367 .d_instantiate = smack_d_instantiate,
4368
4369 .getprocattr = smack_getprocattr,
4370 .setprocattr = smack_setprocattr,
4371
4372 .unix_stream_connect = smack_unix_stream_connect,
4373 .unix_may_send = smack_unix_may_send,
4374
4375 .socket_post_create = smack_socket_post_create,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004376#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schauflerc6739442013-05-22 18:42:56 -07004377 .socket_bind = smack_socket_bind,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004378#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004379 .socket_connect = smack_socket_connect,
4380 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08004381 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4382 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4383 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4384 .sk_alloc_security = smack_sk_alloc_security,
4385 .sk_free_security = smack_sk_free_security,
4386 .sock_graft = smack_sock_graft,
4387 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04004388 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004389
Casey Schauflere114e472008-02-04 22:29:50 -08004390 /* key management security hooks */
4391#ifdef CONFIG_KEYS
4392 .key_alloc = smack_key_alloc,
4393 .key_free = smack_key_free,
4394 .key_permission = smack_key_permission,
José Bollo7fc5f362015-02-17 15:41:22 +01004395 .key_getsecurity = smack_key_getsecurity,
Casey Schauflere114e472008-02-04 22:29:50 -08004396#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004397
4398 /* Audit hooks */
4399#ifdef CONFIG_AUDIT
4400 .audit_rule_init = smack_audit_rule_init,
4401 .audit_rule_known = smack_audit_rule_known,
4402 .audit_rule_match = smack_audit_rule_match,
4403 .audit_rule_free = smack_audit_rule_free,
4404#endif /* CONFIG_AUDIT */
4405
David Quigley746df9b2013-05-22 12:50:35 -04004406 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08004407 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004408 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08004409 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04004410 .inode_notifysecctx = smack_inode_notifysecctx,
4411 .inode_setsecctx = smack_inode_setsecctx,
4412 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08004413};
4414
Etienne Basset7198e2e2009-03-24 20:53:24 +01004415
Casey Schaufler86812bb2012-04-17 18:55:46 -07004416static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004417{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004418 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004419 * Initialize rule list locks
4420 */
4421 mutex_init(&smack_known_huh.smk_rules_lock);
4422 mutex_init(&smack_known_hat.smk_rules_lock);
4423 mutex_init(&smack_known_floor.smk_rules_lock);
4424 mutex_init(&smack_known_star.smk_rules_lock);
4425 mutex_init(&smack_known_invalid.smk_rules_lock);
4426 mutex_init(&smack_known_web.smk_rules_lock);
4427 /*
4428 * Initialize rule lists
4429 */
4430 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4431 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4432 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4433 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4434 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4435 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4436 /*
4437 * Create the known labels list
4438 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004439 smk_insert_entry(&smack_known_huh);
4440 smk_insert_entry(&smack_known_hat);
4441 smk_insert_entry(&smack_known_star);
4442 smk_insert_entry(&smack_known_floor);
4443 smk_insert_entry(&smack_known_invalid);
4444 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004445}
4446
Casey Schauflere114e472008-02-04 22:29:50 -08004447/**
4448 * smack_init - initialize the smack system
4449 *
4450 * Returns 0
4451 */
4452static __init int smack_init(void)
4453{
David Howellsd84f4f92008-11-14 10:39:23 +11004454 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004455 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004456
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004457 if (!security_module_enable(&smack_ops))
4458 return 0;
4459
Casey Schaufler69f287a2014-12-12 17:08:40 -08004460 smack_enabled = 1;
4461
Rohit1a5b4722014-10-15 17:40:41 +05304462 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4463 if (!smack_inode_cache)
4464 return -ENOMEM;
4465
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004466 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4467 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304468 if (tsp == NULL) {
4469 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004470 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304471 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004472
Casey Schauflere114e472008-02-04 22:29:50 -08004473 printk(KERN_INFO "Smack: Initializing.\n");
4474
4475 /*
4476 * Set the security state for the initial task.
4477 */
David Howellsd84f4f92008-11-14 10:39:23 +11004478 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004479 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004480
Casey Schaufler86812bb2012-04-17 18:55:46 -07004481 /* initialize the smack_known_list */
4482 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004483
4484 /*
4485 * Register with LSM
4486 */
4487 if (register_security(&smack_ops))
4488 panic("smack: Unable to register with kernel.\n");
4489
4490 return 0;
4491}
4492
4493/*
4494 * Smack requires early initialization in order to label
4495 * all processes and objects when they are created.
4496 */
4497security_initcall(smack_init);