blob: ed6885bccec4dcb8de652851433799109ecd0516 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
19 */
20
21#include <linux/xattr.h>
22#include <linux/pagemap.h>
23#include <linux/mount.h>
24#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070030#include <linux/dccp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080032#include <linux/mutex.h>
33#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080034#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070035#include <net/ip.h>
36#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100037#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070038#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050039#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030040#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000041#include <linux/msg.h>
42#include <linux/shm.h>
43#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053044#include <linux/parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080045#include "smack.h"
46
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020047#define TRANS_TRUE "TRUE"
48#define TRANS_TRUE_SIZE 4
49
Casey Schauflerc6739442013-05-22 18:42:56 -070050#define SMK_CONNECTING 0
51#define SMK_RECEIVING 1
52#define SMK_SENDING 2
53
Casey Schaufler21abb1e2015-07-22 14:25:31 -070054#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053055DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080056static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070057#endif
Rohit1a5b4722014-10-15 17:40:41 +053058static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080059int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070060
Casey Schaufler3d04c922015-08-12 11:56:02 -070061static const match_table_t smk_mount_tokens = {
Vivek Trivedi3bf27892015-06-22 15:36:06 +053062 {Opt_fsdefault, SMK_FSDEFAULT "%s"},
63 {Opt_fsfloor, SMK_FSFLOOR "%s"},
64 {Opt_fshat, SMK_FSHAT "%s"},
65 {Opt_fsroot, SMK_FSROOT "%s"},
66 {Opt_fstransmute, SMK_FSTRANS "%s"},
67 {Opt_error, NULL},
68};
69
Casey Schaufler3d04c922015-08-12 11:56:02 -070070#ifdef CONFIG_SECURITY_SMACK_BRINGUP
71static char *smk_bu_mess[] = {
72 "Bringup Error", /* Unused */
73 "Bringup", /* SMACK_BRINGUP_ALLOW */
74 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
75 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
76};
77
Casey Schauflerd166c802014-08-27 14:51:27 -070078static void smk_bu_mode(int mode, char *s)
79{
80 int i = 0;
81
82 if (mode & MAY_READ)
83 s[i++] = 'r';
84 if (mode & MAY_WRITE)
85 s[i++] = 'w';
86 if (mode & MAY_EXEC)
87 s[i++] = 'x';
88 if (mode & MAY_APPEND)
89 s[i++] = 'a';
90 if (mode & MAY_TRANSMUTE)
91 s[i++] = 't';
92 if (mode & MAY_LOCK)
93 s[i++] = 'l';
94 if (i == 0)
95 s[i++] = '-';
96 s[i] = '\0';
97}
98#endif
99
100#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200101static int smk_bu_note(char *note, struct smack_known *sskp,
102 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700103{
104 char acc[SMK_NUM_ACCESS_TYPE + 1];
105
106 if (rc <= 0)
107 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700108 if (rc > SMACK_UNCONFINED_OBJECT)
109 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700110
111 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700112 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200113 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700114 return 0;
115}
116#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200117#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700118#endif
119
120#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200121static int smk_bu_current(char *note, struct smack_known *oskp,
122 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700123{
124 struct task_smack *tsp = current_security();
125 char acc[SMK_NUM_ACCESS_TYPE + 1];
126
127 if (rc <= 0)
128 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700129 if (rc > SMACK_UNCONFINED_OBJECT)
130 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700131
132 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700133 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200134 tsp->smk_task->smk_known, oskp->smk_known,
135 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700136 return 0;
137}
138#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200139#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700140#endif
141
142#ifdef CONFIG_SECURITY_SMACK_BRINGUP
143static int smk_bu_task(struct task_struct *otp, int mode, int rc)
144{
145 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300146 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700147 char acc[SMK_NUM_ACCESS_TYPE + 1];
148
149 if (rc <= 0)
150 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700151 if (rc > SMACK_UNCONFINED_OBJECT)
152 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700153
154 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700155 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300156 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700157 current->comm, otp->comm);
158 return 0;
159}
160#else
161#define smk_bu_task(otp, mode, RC) (RC)
162#endif
163
164#ifdef CONFIG_SECURITY_SMACK_BRINGUP
165static int smk_bu_inode(struct inode *inode, int mode, int rc)
166{
167 struct task_smack *tsp = current_security();
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700168 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700169 char acc[SMK_NUM_ACCESS_TYPE + 1];
170
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700171 if (isp->smk_flags & SMK_INODE_IMPURE)
172 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
173 inode->i_sb->s_id, inode->i_ino, current->comm);
174
Casey Schauflerd166c802014-08-27 14:51:27 -0700175 if (rc <= 0)
176 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700177 if (rc > SMACK_UNCONFINED_OBJECT)
178 rc = 0;
179 if (rc == SMACK_UNCONFINED_SUBJECT &&
180 (mode & (MAY_WRITE | MAY_APPEND)))
181 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700182
183 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700184
185 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
186 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700187 inode->i_sb->s_id, inode->i_ino, current->comm);
188 return 0;
189}
190#else
191#define smk_bu_inode(inode, mode, RC) (RC)
192#endif
193
194#ifdef CONFIG_SECURITY_SMACK_BRINGUP
195static int smk_bu_file(struct file *file, int mode, int rc)
196{
197 struct task_smack *tsp = current_security();
198 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800199 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700200 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700201 char acc[SMK_NUM_ACCESS_TYPE + 1];
202
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700203 if (isp->smk_flags & SMK_INODE_IMPURE)
204 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
205 inode->i_sb->s_id, inode->i_ino, current->comm);
206
Casey Schauflerd166c802014-08-27 14:51:27 -0700207 if (rc <= 0)
208 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700209 if (rc > SMACK_UNCONFINED_OBJECT)
210 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700211
212 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700213 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800214 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400215 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700216 current->comm);
217 return 0;
218}
219#else
220#define smk_bu_file(file, mode, RC) (RC)
221#endif
222
223#ifdef CONFIG_SECURITY_SMACK_BRINGUP
224static int smk_bu_credfile(const struct cred *cred, struct file *file,
225 int mode, int rc)
226{
227 struct task_smack *tsp = cred->security;
228 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500229 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700230 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700231 char acc[SMK_NUM_ACCESS_TYPE + 1];
232
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700233 if (isp->smk_flags & SMK_INODE_IMPURE)
234 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
235 inode->i_sb->s_id, inode->i_ino, current->comm);
236
Casey Schauflerd166c802014-08-27 14:51:27 -0700237 if (rc <= 0)
238 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700239 if (rc > SMACK_UNCONFINED_OBJECT)
240 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700241
242 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700243 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200244 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400245 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700246 current->comm);
247 return 0;
248}
249#else
250#define smk_bu_credfile(cred, file, mode, RC) (RC)
251#endif
252
Casey Schauflere114e472008-02-04 22:29:50 -0800253/**
254 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100255 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800256 * @ip: a pointer to the inode
257 * @dp: a pointer to the dentry
258 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200259 * Returns a pointer to the master list entry for the Smack label,
260 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800261 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700262static struct smack_known *smk_fetch(const char *name, struct inode *ip,
263 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800264{
265 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700266 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700267 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800268
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200269 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200270 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800271
Casey Schauflerf7112e62012-05-06 15:22:02 -0700272 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
273 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200274 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800275
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200276 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200277 if (rc < 0)
278 skp = ERR_PTR(rc);
279 else if (rc == 0)
280 skp = NULL;
281 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700282 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700283
284 kfree(buffer);
285
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700286 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800287}
288
289/**
290 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200291 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800292 *
293 * Returns the new blob or NULL if there's no memory available
294 */
Casey Schaufler1eddfe82015-07-30 14:35:14 -0700295static struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800296{
297 struct inode_smack *isp;
298
Rohit1a5b4722014-10-15 17:40:41 +0530299 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800300 if (isp == NULL)
301 return NULL;
302
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200303 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800304 isp->smk_flags = 0;
305 mutex_init(&isp->smk_lock);
306
307 return isp;
308}
309
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800310/**
311 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100312 * @task: a pointer to the Smack label for the running task
313 * @forked: a pointer to the Smack label for the forked task
314 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800315 *
316 * Returns the new blob or NULL if there's no memory available
317 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700318static struct task_smack *new_task_smack(struct smack_known *task,
319 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800320{
321 struct task_smack *tsp;
322
323 tsp = kzalloc(sizeof(struct task_smack), gfp);
324 if (tsp == NULL)
325 return NULL;
326
327 tsp->smk_task = task;
328 tsp->smk_forked = forked;
329 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200330 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800331 mutex_init(&tsp->smk_rules_lock);
332
333 return tsp;
334}
335
336/**
337 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100338 * @nhead: new rules header pointer
339 * @ohead: old rules header pointer
340 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800341 *
342 * Returns 0 on success, -ENOMEM on error
343 */
344static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
345 gfp_t gfp)
346{
347 struct smack_rule *nrp;
348 struct smack_rule *orp;
349 int rc = 0;
350
351 INIT_LIST_HEAD(nhead);
352
353 list_for_each_entry_rcu(orp, ohead, list) {
354 nrp = kzalloc(sizeof(struct smack_rule), gfp);
355 if (nrp == NULL) {
356 rc = -ENOMEM;
357 break;
358 }
359 *nrp = *orp;
360 list_add_rcu(&nrp->list, nhead);
361 }
362 return rc;
363}
364
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100365/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200366 * smk_copy_relabel - copy smk_relabel labels list
367 * @nhead: new rules header pointer
368 * @ohead: old rules header pointer
369 * @gfp: type of the memory for the allocation
370 *
371 * Returns 0 on success, -ENOMEM on error
372 */
373static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
374 gfp_t gfp)
375{
376 struct smack_known_list_elem *nklep;
377 struct smack_known_list_elem *oklep;
378
379 INIT_LIST_HEAD(nhead);
380
381 list_for_each_entry(oklep, ohead, list) {
382 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
383 if (nklep == NULL) {
384 smk_destroy_label_list(nhead);
385 return -ENOMEM;
386 }
387 nklep->smk_label = oklep->smk_label;
388 list_add(&nklep->list, nhead);
389 }
390
391 return 0;
392}
393
394/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100395 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
396 * @mode - input mode in form of PTRACE_MODE_*
397 *
398 * Returns a converted MAY_* mode usable by smack rules
399 */
400static inline unsigned int smk_ptrace_mode(unsigned int mode)
401{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800402 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100403 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800404 if (mode & PTRACE_MODE_READ)
405 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100406
407 return 0;
408}
409
410/**
411 * smk_ptrace_rule_check - helper for ptrace access
412 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200413 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100414 * @mode: ptrace attachment mode (PTRACE_MODE_*)
415 * @func: name of the function that called us, used for audit
416 *
417 * Returns 0 on access granted, -error on error
418 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200419static int smk_ptrace_rule_check(struct task_struct *tracer,
420 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100421 unsigned int mode, const char *func)
422{
423 int rc;
424 struct smk_audit_info ad, *saip = NULL;
425 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200426 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100427
428 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
429 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
430 smk_ad_setfield_u_tsk(&ad, tracer);
431 saip = &ad;
432 }
433
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300434 rcu_read_lock();
435 tsp = __task_cred(tracer)->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200436 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100437
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100438 if ((mode & PTRACE_MODE_ATTACH) &&
439 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
440 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200441 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100442 rc = 0;
443 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
444 rc = -EACCES;
445 else if (capable(CAP_SYS_PTRACE))
446 rc = 0;
447 else
448 rc = -EACCES;
449
450 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200451 smack_log(tracer_known->smk_known,
452 tracee_known->smk_known,
453 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100454
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300455 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100456 return rc;
457 }
458
459 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200460 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300461
462 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100463 return rc;
464}
465
Casey Schauflere114e472008-02-04 22:29:50 -0800466/*
467 * LSM hooks.
468 * We he, that is fun!
469 */
470
471/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000472 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800473 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100474 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800475 *
476 * Returns 0 if access is OK, an error code otherwise
477 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100478 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800479 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000480static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800481{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700482 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800483
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300484 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200485
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700486 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100487}
Casey Schauflere114e472008-02-04 22:29:50 -0800488
David Howells5cd9c582008-08-14 11:37:28 +0100489/**
490 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
491 * @ptp: parent task pointer
492 *
493 * Returns 0 if access is OK, an error code otherwise
494 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100495 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100496 */
497static int smack_ptrace_traceme(struct task_struct *ptp)
498{
499 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700500 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100501
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100502 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200503
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200504 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800505 return rc;
506}
507
508/**
509 * smack_syslog - Smack approval on syslog
510 * @type: message type
511 *
Casey Schauflere114e472008-02-04 22:29:50 -0800512 * Returns 0 on success, error code otherwise.
513 */
Eric Paris12b30522010-11-15 18:36:29 -0500514static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800515{
Eric Paris12b30522010-11-15 18:36:29 -0500516 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700517 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800518
Casey Schaufler1880eff2012-06-05 15:28:30 -0700519 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800520 return 0;
521
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800522 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800523 rc = -EACCES;
524
525 return rc;
526}
527
528
529/*
530 * Superblock Hooks.
531 */
532
533/**
534 * smack_sb_alloc_security - allocate a superblock blob
535 * @sb: the superblock getting the blob
536 *
537 * Returns 0 on success or -ENOMEM on error.
538 */
539static int smack_sb_alloc_security(struct super_block *sb)
540{
541 struct superblock_smack *sbsp;
542
543 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
544
545 if (sbsp == NULL)
546 return -ENOMEM;
547
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200548 sbsp->smk_root = &smack_known_floor;
549 sbsp->smk_default = &smack_known_floor;
550 sbsp->smk_floor = &smack_known_floor;
551 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700552 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500553 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700554 */
Casey Schauflere114e472008-02-04 22:29:50 -0800555 sb->s_security = sbsp;
556
557 return 0;
558}
559
560/**
561 * smack_sb_free_security - free a superblock blob
562 * @sb: the superblock getting the blob
563 *
564 */
565static void smack_sb_free_security(struct super_block *sb)
566{
567 kfree(sb->s_security);
568 sb->s_security = NULL;
569}
570
571/**
572 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800573 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800574 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800575 *
576 * Returns 0 on success or -ENOMEM on error.
577 *
578 * Copy the Smack specific mount options out of the mount
579 * options list.
580 */
Eric Parise0007522008-03-05 10:31:54 -0500581static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800582{
583 char *cp, *commap, *otheropts, *dp;
584
Casey Schauflere114e472008-02-04 22:29:50 -0800585 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
586 if (otheropts == NULL)
587 return -ENOMEM;
588
589 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
590 if (strstr(cp, SMK_FSDEFAULT) == cp)
591 dp = smackopts;
592 else if (strstr(cp, SMK_FSFLOOR) == cp)
593 dp = smackopts;
594 else if (strstr(cp, SMK_FSHAT) == cp)
595 dp = smackopts;
596 else if (strstr(cp, SMK_FSROOT) == cp)
597 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700598 else if (strstr(cp, SMK_FSTRANS) == cp)
599 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800600 else
601 dp = otheropts;
602
603 commap = strchr(cp, ',');
604 if (commap != NULL)
605 *commap = '\0';
606
607 if (*dp != '\0')
608 strcat(dp, ",");
609 strcat(dp, cp);
610 }
611
612 strcpy(orig, otheropts);
613 free_page((unsigned long)otheropts);
614
615 return 0;
616}
617
618/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530619 * smack_parse_opts_str - parse Smack specific mount options
620 * @options: mount options string
621 * @opts: where to store converted mount opts
622 *
623 * Returns 0 on success or -ENOMEM on error.
624 *
625 * converts Smack specific mount options to generic security option format
626 */
627static int smack_parse_opts_str(char *options,
628 struct security_mnt_opts *opts)
629{
630 char *p;
Casey Schaufler3d04c922015-08-12 11:56:02 -0700631 char *fsdefault = NULL;
632 char *fsfloor = NULL;
633 char *fshat = NULL;
634 char *fsroot = NULL;
635 char *fstransmute = NULL;
636 int rc = -ENOMEM;
637 int num_mnt_opts = 0;
638 int token;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530639
640 opts->num_mnt_opts = 0;
641
642 if (!options)
643 return 0;
644
645 while ((p = strsep(&options, ",")) != NULL) {
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530646 substring_t args[MAX_OPT_ARGS];
647
648 if (!*p)
649 continue;
650
Casey Schaufler3d04c922015-08-12 11:56:02 -0700651 token = match_token(p, smk_mount_tokens, args);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530652
653 switch (token) {
654 case Opt_fsdefault:
655 if (fsdefault)
656 goto out_opt_err;
657 fsdefault = match_strdup(&args[0]);
658 if (!fsdefault)
659 goto out_err;
660 break;
661 case Opt_fsfloor:
662 if (fsfloor)
663 goto out_opt_err;
664 fsfloor = match_strdup(&args[0]);
665 if (!fsfloor)
666 goto out_err;
667 break;
668 case Opt_fshat:
669 if (fshat)
670 goto out_opt_err;
671 fshat = match_strdup(&args[0]);
672 if (!fshat)
673 goto out_err;
674 break;
675 case Opt_fsroot:
676 if (fsroot)
677 goto out_opt_err;
678 fsroot = match_strdup(&args[0]);
679 if (!fsroot)
680 goto out_err;
681 break;
682 case Opt_fstransmute:
683 if (fstransmute)
684 goto out_opt_err;
685 fstransmute = match_strdup(&args[0]);
686 if (!fstransmute)
687 goto out_err;
688 break;
689 default:
690 rc = -EINVAL;
691 pr_warn("Smack: unknown mount option\n");
692 goto out_err;
693 }
694 }
695
Tetsuo Handa8c15d662016-11-14 20:12:55 +0900696 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_KERNEL);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530697 if (!opts->mnt_opts)
698 goto out_err;
699
700 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
Tetsuo Handa8c15d662016-11-14 20:12:55 +0900701 GFP_KERNEL);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530702 if (!opts->mnt_opts_flags) {
703 kfree(opts->mnt_opts);
704 goto out_err;
705 }
706
707 if (fsdefault) {
708 opts->mnt_opts[num_mnt_opts] = fsdefault;
709 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
710 }
711 if (fsfloor) {
712 opts->mnt_opts[num_mnt_opts] = fsfloor;
713 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
714 }
715 if (fshat) {
716 opts->mnt_opts[num_mnt_opts] = fshat;
717 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
718 }
719 if (fsroot) {
720 opts->mnt_opts[num_mnt_opts] = fsroot;
721 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
722 }
723 if (fstransmute) {
724 opts->mnt_opts[num_mnt_opts] = fstransmute;
725 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
726 }
727
728 opts->num_mnt_opts = num_mnt_opts;
729 return 0;
730
731out_opt_err:
732 rc = -EINVAL;
733 pr_warn("Smack: duplicate mount options\n");
734
735out_err:
736 kfree(fsdefault);
737 kfree(fsfloor);
738 kfree(fshat);
739 kfree(fsroot);
740 kfree(fstransmute);
741 return rc;
742}
743
744/**
745 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800746 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530747 * @opts: Smack mount options
748 * @kern_flags: mount option from kernel space or user space
749 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800750 *
751 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530752 *
753 * Allow filesystems with binary mount data to explicitly set Smack mount
754 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800755 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530756static int smack_set_mnt_opts(struct super_block *sb,
757 struct security_mnt_opts *opts,
758 unsigned long kern_flags,
759 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800760{
761 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000762 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800763 struct superblock_smack *sp = sb->s_security;
764 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800765 struct smack_known *skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530766 int i;
767 int num_opts = opts->num_mnt_opts;
Casey Schauflere830b392013-05-22 18:43:07 -0700768 int transmute = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800769
Seth Forshee9f50eda2015-09-23 15:16:06 -0500770 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800771 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700772
Himanshu Shukla2097f592016-11-10 16:19:52 +0530773 if (!smack_privileged(CAP_MAC_ADMIN)) {
774 /*
775 * Unprivileged mounts don't get to specify Smack values.
776 */
777 if (num_opts)
778 return -EPERM;
779 /*
780 * Unprivileged mounts get root and default from the caller.
781 */
782 skp = smk_of_current();
783 sp->smk_root = skp;
784 sp->smk_default = skp;
785 /*
786 * For a handful of fs types with no user-controlled
787 * backing store it's okay to trust security labels
788 * in the filesystem. The rest are untrusted.
789 */
790 if (sb->s_user_ns != &init_user_ns &&
791 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
792 sb->s_magic != RAMFS_MAGIC) {
793 transmute = 1;
794 sp->smk_flags |= SMK_SB_UNTRUSTED;
795 }
796 }
797
Seth Forshee9f50eda2015-09-23 15:16:06 -0500798 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800799
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530800 for (i = 0; i < num_opts; i++) {
801 switch (opts->mnt_opts_flags[i]) {
802 case FSDEFAULT_MNT:
803 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200804 if (IS_ERR(skp))
805 return PTR_ERR(skp);
806 sp->smk_default = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530807 break;
808 case FSFLOOR_MNT:
809 skp = smk_import_entry(opts->mnt_opts[i], 0);
810 if (IS_ERR(skp))
811 return PTR_ERR(skp);
812 sp->smk_floor = skp;
813 break;
814 case FSHAT_MNT:
815 skp = smk_import_entry(opts->mnt_opts[i], 0);
816 if (IS_ERR(skp))
817 return PTR_ERR(skp);
818 sp->smk_hat = skp;
819 break;
820 case FSROOT_MNT:
821 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200822 if (IS_ERR(skp))
823 return PTR_ERR(skp);
824 sp->smk_root = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530825 break;
826 case FSTRANS_MNT:
827 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200828 if (IS_ERR(skp))
829 return PTR_ERR(skp);
830 sp->smk_root = skp;
831 transmute = 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530832 break;
833 default:
834 break;
Casey Schauflere114e472008-02-04 22:29:50 -0800835 }
836 }
837
838 /*
839 * Initialize the root inode.
840 */
841 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100842 if (isp == NULL) {
843 isp = new_inode_smack(sp->smk_root);
844 if (isp == NULL)
845 return -ENOMEM;
846 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700847 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800848 isp->smk_inode = sp->smk_root;
849
Casey Schauflere830b392013-05-22 18:43:07 -0700850 if (transmute)
851 isp->smk_flags |= SMK_INODE_TRANSMUTE;
852
Casey Schauflere114e472008-02-04 22:29:50 -0800853 return 0;
854}
855
856/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530857 * smack_sb_kern_mount - Smack specific mount processing
858 * @sb: the file system superblock
859 * @flags: the mount flags
860 * @data: the smack mount options
861 *
862 * Returns 0 on success, an error code on failure
863 */
864static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
865{
866 int rc = 0;
867 char *options = data;
868 struct security_mnt_opts opts;
869
870 security_init_mnt_opts(&opts);
871
872 if (!options)
873 goto out;
874
875 rc = smack_parse_opts_str(options, &opts);
876 if (rc)
877 goto out_err;
878
879out:
880 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
881
882out_err:
883 security_free_mnt_opts(&opts);
884 return rc;
885}
886
887/**
Casey Schauflere114e472008-02-04 22:29:50 -0800888 * smack_sb_statfs - Smack check on statfs
889 * @dentry: identifies the file system in question
890 *
891 * Returns 0 if current can read the floor of the filesystem,
892 * and error code otherwise
893 */
894static int smack_sb_statfs(struct dentry *dentry)
895{
896 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200897 int rc;
898 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800899
Eric Parisa2694342011-04-25 13:10:27 -0400900 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200901 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
902
903 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700904 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200905 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800906}
907
Casey Schauflere114e472008-02-04 22:29:50 -0800908/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800909 * BPRM hooks
910 */
911
Casey Schauflerce8a4322011-09-29 18:21:01 -0700912/**
913 * smack_bprm_set_creds - set creds for exec
914 * @bprm: the exec information
915 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100916 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700917 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800918static int smack_bprm_set_creds(struct linux_binprm *bprm)
919{
Al Viro496ad9a2013-01-23 17:07:38 -0500920 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300921 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800922 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500923 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800924 int rc;
925
Casey Schaufler676dac42010-12-02 06:43:39 -0800926 if (bprm->cred_prepared)
927 return 0;
928
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300929 isp = inode->i_security;
930 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800931 return 0;
932
Seth Forshee809c02e2016-04-26 14:36:22 -0500933 sbsp = inode->i_sb->s_security;
934 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
935 isp->smk_task != sbsp->smk_root)
936 return 0;
937
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100938 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
939 struct task_struct *tracer;
940 rc = 0;
941
942 rcu_read_lock();
943 tracer = ptrace_parent(current);
944 if (likely(tracer != NULL))
945 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200946 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100947 PTRACE_MODE_ATTACH,
948 __func__);
949 rcu_read_unlock();
950
951 if (rc != 0)
952 return rc;
953 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300954 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800955
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300956 bsp->smk_task = isp->smk_task;
957 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800958
959 return 0;
960}
961
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300962/**
963 * smack_bprm_committing_creds - Prepare to install the new credentials
964 * from bprm.
965 *
966 * @bprm: binprm for exec
967 */
968static void smack_bprm_committing_creds(struct linux_binprm *bprm)
969{
970 struct task_smack *bsp = bprm->cred->security;
971
972 if (bsp->smk_task != bsp->smk_forked)
973 current->pdeath_signal = 0;
974}
975
976/**
977 * smack_bprm_secureexec - Return the decision to use secureexec.
978 * @bprm: binprm for exec
979 *
980 * Returns 0 on success.
981 */
982static int smack_bprm_secureexec(struct linux_binprm *bprm)
983{
984 struct task_smack *tsp = current_security();
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300985
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700986 if (tsp->smk_task != tsp->smk_forked)
987 return 1;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300988
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700989 return 0;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300990}
991
Casey Schaufler676dac42010-12-02 06:43:39 -0800992/*
Casey Schauflere114e472008-02-04 22:29:50 -0800993 * Inode hooks
994 */
995
996/**
997 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800998 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800999 *
1000 * Returns 0 if it gets a blob, -ENOMEM otherwise
1001 */
1002static int smack_inode_alloc_security(struct inode *inode)
1003{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001004 struct smack_known *skp = smk_of_current();
1005
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001006 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08001007 if (inode->i_security == NULL)
1008 return -ENOMEM;
1009 return 0;
1010}
1011
1012/**
1013 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -08001014 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -08001015 *
1016 * Clears the blob pointer in inode
1017 */
1018static void smack_inode_free_security(struct inode *inode)
1019{
Rohit1a5b4722014-10-15 17:40:41 +05301020 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -08001021 inode->i_security = NULL;
1022}
1023
1024/**
1025 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001026 * @inode: the newly created inode
1027 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -05001028 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001029 * @name: where to put the attribute name
1030 * @value: where to put the attribute value
1031 * @len: where to put the length of the attribute
1032 *
1033 * Returns 0 if it all works out, -ENOMEM if there's no memory
1034 */
1035static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09001036 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05001037 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -08001038{
Casey Schaufler2267b132012-03-13 19:14:19 -07001039 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001040 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001041 struct smack_known *isp = smk_of_inode(inode);
1042 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001043 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08001044
Tetsuo Handa95489062013-07-25 05:44:02 +09001045 if (name)
1046 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -08001047
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001048 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001049 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001050 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1051 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001052 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001053
1054 /*
1055 * If the access rule allows transmutation and
1056 * the directory requests transmutation then
1057 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001058 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001059 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001060 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001061 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001062 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001063 issp->smk_flags |= SMK_INODE_CHANGED;
1064 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001065
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001066 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001067 if (*value == NULL)
1068 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001069
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001070 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001071 }
Casey Schauflere114e472008-02-04 22:29:50 -08001072
1073 return 0;
1074}
1075
1076/**
1077 * smack_inode_link - Smack check on link
1078 * @old_dentry: the existing object
1079 * @dir: unused
1080 * @new_dentry: the new object
1081 *
1082 * Returns 0 if access is permitted, an error code otherwise
1083 */
1084static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1085 struct dentry *new_dentry)
1086{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001087 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001088 struct smk_audit_info ad;
1089 int rc;
1090
Eric Parisa2694342011-04-25 13:10:27 -04001091 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001092 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001093
David Howellsc6f493d2015-03-17 22:26:22 +00001094 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001095 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001096 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001097
David Howells88025652015-01-29 12:02:32 +00001098 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001099 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001100 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1101 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001102 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001103 }
1104
1105 return rc;
1106}
1107
1108/**
1109 * smack_inode_unlink - Smack check on inode deletion
1110 * @dir: containing directory object
1111 * @dentry: file to unlink
1112 *
1113 * Returns 0 if current can write the containing directory
1114 * and the object, error code otherwise
1115 */
1116static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1117{
David Howellsc6f493d2015-03-17 22:26:22 +00001118 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001119 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001120 int rc;
1121
Eric Parisa2694342011-04-25 13:10:27 -04001122 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001123 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1124
Casey Schauflere114e472008-02-04 22:29:50 -08001125 /*
1126 * You need write access to the thing you're unlinking
1127 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001128 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001129 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001130 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001131 /*
1132 * You also need write access to the containing directory
1133 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001134 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001135 smk_ad_setfield_u_fs_inode(&ad, dir);
1136 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001137 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001138 }
Casey Schauflere114e472008-02-04 22:29:50 -08001139 return rc;
1140}
1141
1142/**
1143 * smack_inode_rmdir - Smack check on directory deletion
1144 * @dir: containing directory object
1145 * @dentry: directory to unlink
1146 *
1147 * Returns 0 if current can write the containing directory
1148 * and the directory, error code otherwise
1149 */
1150static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1151{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001152 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001153 int rc;
1154
Eric Parisa2694342011-04-25 13:10:27 -04001155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001156 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1157
Casey Schauflere114e472008-02-04 22:29:50 -08001158 /*
1159 * You need write access to the thing you're removing
1160 */
David Howellsc6f493d2015-03-17 22:26:22 +00001161 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1162 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001163 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001164 /*
1165 * You also need write access to the containing directory
1166 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001167 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001168 smk_ad_setfield_u_fs_inode(&ad, dir);
1169 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001170 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171 }
Casey Schauflere114e472008-02-04 22:29:50 -08001172
1173 return rc;
1174}
1175
1176/**
1177 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001178 * @old_inode: unused
1179 * @old_dentry: the old object
1180 * @new_inode: unused
1181 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001182 *
1183 * Read and write access is required on both the old and
1184 * new directories.
1185 *
1186 * Returns 0 if access is permitted, an error code otherwise
1187 */
1188static int smack_inode_rename(struct inode *old_inode,
1189 struct dentry *old_dentry,
1190 struct inode *new_inode,
1191 struct dentry *new_dentry)
1192{
1193 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001194 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001195 struct smk_audit_info ad;
1196
Eric Parisa2694342011-04-25 13:10:27 -04001197 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001198 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001199
David Howellsc6f493d2015-03-17 22:26:22 +00001200 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001201 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001202 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001203
David Howells88025652015-01-29 12:02:32 +00001204 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001205 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001206 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1207 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001208 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001209 }
Casey Schauflere114e472008-02-04 22:29:50 -08001210 return rc;
1211}
1212
1213/**
1214 * smack_inode_permission - Smack version of permission()
1215 * @inode: the inode in question
1216 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001217 *
1218 * This is the important Smack hook.
1219 *
1220 * Returns 0 if access is permitted, -EACCES otherwise
1221 */
Al Viroe74f71e2011-06-20 19:38:15 -04001222static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001223{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001224 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001225 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001226 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001227 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001228
1229 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001230 /*
1231 * No permission to check. Existence test. Yup, it's there.
1232 */
1233 if (mask == 0)
1234 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001235
Seth Forshee9f50eda2015-09-23 15:16:06 -05001236 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1237 if (smk_of_inode(inode) != sbsp->smk_root)
1238 return -EACCES;
1239 }
1240
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001241 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001242 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001243 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001244 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001245 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001246 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1247 rc = smk_bu_inode(inode, mask, rc);
1248 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001249}
1250
1251/**
1252 * smack_inode_setattr - Smack check for setting attributes
1253 * @dentry: the object
1254 * @iattr: for the force flag
1255 *
1256 * Returns 0 if access is permitted, an error code otherwise
1257 */
1258static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1259{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001260 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001261 int rc;
1262
Casey Schauflere114e472008-02-04 22:29:50 -08001263 /*
1264 * Need to allow for clearing the setuid bit.
1265 */
1266 if (iattr->ia_valid & ATTR_FORCE)
1267 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001268 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001269 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001270
David Howellsc6f493d2015-03-17 22:26:22 +00001271 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1272 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001273 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001274}
1275
1276/**
1277 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001278 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001279 * @dentry: the object
1280 *
1281 * Returns 0 if access is permitted, an error code otherwise
1282 */
Al Viro3f7036a2015-03-08 19:28:30 -04001283static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001284{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001285 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001286 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001287 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001288
Eric Parisf48b7392011-04-25 12:54:27 -04001289 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001290 smk_ad_setfield_u_fs_path(&ad, *path);
1291 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1292 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001293 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001294}
1295
1296/**
1297 * smack_inode_setxattr - Smack check for setting xattrs
1298 * @dentry: the object
1299 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001300 * @value: value of the attribute
1301 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001302 * @flags: unused
1303 *
1304 * This protects the Smack attribute explicitly.
1305 *
1306 * Returns 0 if access is permitted, an error code otherwise
1307 */
David Howells8f0cfa52008-04-29 00:59:41 -07001308static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1309 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001310{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001311 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001312 struct smack_known *skp;
1313 int check_priv = 0;
1314 int check_import = 0;
1315 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001316 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001317
Casey Schaufler19760ad2013-12-16 16:27:26 -08001318 /*
1319 * Check label validity here so import won't fail in post_setxattr
1320 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001321 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1322 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001323 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1324 check_priv = 1;
1325 check_import = 1;
1326 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1327 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1328 check_priv = 1;
1329 check_import = 1;
1330 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001331 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001332 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001333 if (size != TRANS_TRUE_SIZE ||
1334 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1335 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001336 } else
1337 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1338
Casey Schaufler19760ad2013-12-16 16:27:26 -08001339 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1340 rc = -EPERM;
1341
1342 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001343 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001344 if (IS_ERR(skp))
1345 rc = PTR_ERR(skp);
1346 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001347 (skp == &smack_known_star || skp == &smack_known_web)))
1348 rc = -EINVAL;
1349 }
1350
Eric Parisa2694342011-04-25 13:10:27 -04001351 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001352 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1353
Casey Schauflerd166c802014-08-27 14:51:27 -07001354 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001355 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1356 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001357 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001358
1359 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001360}
1361
1362/**
1363 * smack_inode_post_setxattr - Apply the Smack update approved above
1364 * @dentry: object
1365 * @name: attribute name
1366 * @value: attribute value
1367 * @size: attribute size
1368 * @flags: unused
1369 *
1370 * Set the pointer in the inode blob to the entry found
1371 * in the master label list.
1372 */
David Howells8f0cfa52008-04-29 00:59:41 -07001373static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1374 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001375{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001376 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001377 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001378
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001379 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1380 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1381 return;
1382 }
1383
Casey Schaufler676dac42010-12-02 06:43:39 -08001384 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001385 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001386 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001387 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001388 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001389 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001390 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001391 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001392 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001393 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001394 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001395 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001396 }
Casey Schauflere114e472008-02-04 22:29:50 -08001397
1398 return;
1399}
1400
Casey Schauflerce8a4322011-09-29 18:21:01 -07001401/**
Casey Schauflere114e472008-02-04 22:29:50 -08001402 * smack_inode_getxattr - Smack check on getxattr
1403 * @dentry: the object
1404 * @name: unused
1405 *
1406 * Returns 0 if access is permitted, an error code otherwise
1407 */
David Howells8f0cfa52008-04-29 00:59:41 -07001408static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001409{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001410 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001411 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001412
Eric Parisa2694342011-04-25 13:10:27 -04001413 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001414 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1415
David Howellsc6f493d2015-03-17 22:26:22 +00001416 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1417 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001418 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001419}
1420
Casey Schauflerce8a4322011-09-29 18:21:01 -07001421/**
Casey Schauflere114e472008-02-04 22:29:50 -08001422 * smack_inode_removexattr - Smack check on removexattr
1423 * @dentry: the object
1424 * @name: name of the attribute
1425 *
1426 * Removing the Smack attribute requires CAP_MAC_ADMIN
1427 *
1428 * Returns 0 if access is permitted, an error code otherwise
1429 */
David Howells8f0cfa52008-04-29 00:59:41 -07001430static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001431{
Casey Schaufler676dac42010-12-02 06:43:39 -08001432 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001433 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001434 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001435
Casey Schauflerbcdca222008-02-23 15:24:04 -08001436 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1437 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001438 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001439 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001440 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301441 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001442 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001443 rc = -EPERM;
1444 } else
1445 rc = cap_inode_removexattr(dentry, name);
1446
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001447 if (rc != 0)
1448 return rc;
1449
Eric Parisa2694342011-04-25 13:10:27 -04001450 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001451 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001452
David Howellsc6f493d2015-03-17 22:26:22 +00001453 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1454 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001455 if (rc != 0)
1456 return rc;
1457
David Howellsc6f493d2015-03-17 22:26:22 +00001458 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001459 /*
1460 * Don't do anything special for these.
1461 * XATTR_NAME_SMACKIPIN
1462 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001463 */
José Bollo80124952016-01-12 21:23:40 +01001464 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001465 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001466 struct superblock_smack *sbsp = sbp->s_security;
1467
1468 isp->smk_inode = sbsp->smk_default;
1469 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001470 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001471 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001472 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001473 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1474 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001475
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001476 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001477}
1478
1479/**
1480 * smack_inode_getsecurity - get smack xattrs
1481 * @inode: the object
1482 * @name: attribute name
1483 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001484 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001485 *
1486 * Returns the size of the attribute or an error code
1487 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001488static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001489 const char *name, void **buffer,
1490 bool alloc)
1491{
1492 struct socket_smack *ssp;
1493 struct socket *sock;
1494 struct super_block *sbp;
1495 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001496 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001497 int ilen;
1498 int rc = 0;
1499
1500 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1501 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001502 ilen = strlen(isp->smk_known);
1503 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001504 return ilen;
1505 }
1506
1507 /*
1508 * The rest of the Smack xattrs are only on sockets.
1509 */
1510 sbp = ip->i_sb;
1511 if (sbp->s_magic != SOCKFS_MAGIC)
1512 return -EOPNOTSUPP;
1513
1514 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001515 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001516 return -EOPNOTSUPP;
1517
1518 ssp = sock->sk->sk_security;
1519
1520 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001521 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001522 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001523 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001524 else
1525 return -EOPNOTSUPP;
1526
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001527 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001528 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001529 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001530 rc = ilen;
1531 }
1532
1533 return rc;
1534}
1535
1536
1537/**
1538 * smack_inode_listsecurity - list the Smack attributes
1539 * @inode: the object
1540 * @buffer: where they go
1541 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001542 */
1543static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1544 size_t buffer_size)
1545{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001546 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001547
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001548 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001549 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001550
1551 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001552}
1553
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001554/**
1555 * smack_inode_getsecid - Extract inode's security id
1556 * @inode: inode to extract the info from
1557 * @secid: where result will be saved
1558 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001559static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001560{
1561 struct inode_smack *isp = inode->i_security;
1562
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001563 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001564}
1565
Casey Schauflere114e472008-02-04 22:29:50 -08001566/*
1567 * File Hooks
1568 */
1569
Casey Schaufler491a0b02016-01-26 15:08:35 -08001570/*
1571 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001572 *
1573 * Should access checks be done on each read or write?
1574 * UNICOS and SELinux say yes.
1575 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1576 *
1577 * I'll say no for now. Smack does not do the frequent
1578 * label changing that SELinux does.
1579 */
Casey Schauflere114e472008-02-04 22:29:50 -08001580
1581/**
1582 * smack_file_alloc_security - assign a file security blob
1583 * @file: the object
1584 *
1585 * The security blob for a file is a pointer to the master
1586 * label list, so no allocation is done.
1587 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001588 * f_security is the owner security information. It
1589 * isn't used on file access checks, it's for send_sigio.
1590 *
Casey Schauflere114e472008-02-04 22:29:50 -08001591 * Returns 0
1592 */
1593static int smack_file_alloc_security(struct file *file)
1594{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001595 struct smack_known *skp = smk_of_current();
1596
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001597 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001598 return 0;
1599}
1600
1601/**
1602 * smack_file_free_security - clear a file security blob
1603 * @file: the object
1604 *
1605 * The security blob for a file is a pointer to the master
1606 * label list, so no memory is freed.
1607 */
1608static void smack_file_free_security(struct file *file)
1609{
1610 file->f_security = NULL;
1611}
1612
1613/**
1614 * smack_file_ioctl - Smack check on ioctls
1615 * @file: the object
1616 * @cmd: what to do
1617 * @arg: unused
1618 *
1619 * Relies heavily on the correct use of the ioctl command conventions.
1620 *
1621 * Returns 0 if allowed, error code otherwise
1622 */
1623static int smack_file_ioctl(struct file *file, unsigned int cmd,
1624 unsigned long arg)
1625{
1626 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001627 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001628 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001629
Eric Parisf48b7392011-04-25 12:54:27 -04001630 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001631 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001632
Casey Schauflerd166c802014-08-27 14:51:27 -07001633 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001634 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001635 rc = smk_bu_file(file, MAY_WRITE, rc);
1636 }
Casey Schauflere114e472008-02-04 22:29:50 -08001637
Casey Schauflerd166c802014-08-27 14:51:27 -07001638 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001639 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001640 rc = smk_bu_file(file, MAY_READ, rc);
1641 }
Casey Schauflere114e472008-02-04 22:29:50 -08001642
1643 return rc;
1644}
1645
1646/**
1647 * smack_file_lock - Smack check on file locking
1648 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001649 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001650 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001651 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001652 */
1653static int smack_file_lock(struct file *file, unsigned int cmd)
1654{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001655 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001656 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001657 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001658
Eric Paris92f42502011-04-25 13:15:55 -04001659 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1660 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001661 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001662 rc = smk_bu_file(file, MAY_LOCK, rc);
1663 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001664}
1665
1666/**
1667 * smack_file_fcntl - Smack check on fcntl
1668 * @file: the object
1669 * @cmd: what action to check
1670 * @arg: unused
1671 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001672 * Generally these operations are harmless.
1673 * File locking operations present an obvious mechanism
1674 * for passing information, so they require write access.
1675 *
Casey Schauflere114e472008-02-04 22:29:50 -08001676 * Returns 0 if current has access, error code otherwise
1677 */
1678static int smack_file_fcntl(struct file *file, unsigned int cmd,
1679 unsigned long arg)
1680{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001681 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001682 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001683 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001684
Casey Schauflere114e472008-02-04 22:29:50 -08001685 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001686 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001687 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001688 case F_SETLK:
1689 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001690 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1691 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001692 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001693 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001694 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001695 case F_SETOWN:
1696 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001697 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1698 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001699 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001700 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001701 break;
1702 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001703 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001704 }
1705
1706 return rc;
1707}
1708
1709/**
Al Viroe5467852012-05-30 13:30:51 -04001710 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001711 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1712 * if mapping anonymous memory.
1713 * @file contains the file structure for file to map (may be NULL).
1714 * @reqprot contains the protection requested by the application.
1715 * @prot contains the protection that will be applied by the kernel.
1716 * @flags contains the operational flags.
1717 * Return 0 if permission is granted.
1718 */
Al Viroe5467852012-05-30 13:30:51 -04001719static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001720 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001721 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001722{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001723 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001724 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001725 struct smack_rule *srp;
1726 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001727 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001728 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001729 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001730 int may;
1731 int mmay;
1732 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001733 int rc;
1734
Al Viro496ad9a2013-01-23 17:07:38 -05001735 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001736 return 0;
1737
Al Viro496ad9a2013-01-23 17:07:38 -05001738 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001739 if (isp->smk_mmap == NULL)
1740 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001741 sbsp = file_inode(file)->i_sb->s_security;
1742 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1743 isp->smk_mmap != sbsp->smk_root)
1744 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001745 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001746
1747 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001748 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001749 rc = 0;
1750
1751 rcu_read_lock();
1752 /*
1753 * For each Smack rule associated with the subject
1754 * label verify that the SMACK64MMAP also has access
1755 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001756 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001757 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001758 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001759 /*
1760 * Matching labels always allows access.
1761 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001762 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001763 continue;
1764 /*
1765 * If there is a matching local rule take
1766 * that into account as well.
1767 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001768 may = smk_access_entry(srp->smk_subject->smk_known,
1769 okp->smk_known,
1770 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001771 if (may == -ENOENT)
1772 may = srp->smk_access;
1773 else
1774 may &= srp->smk_access;
1775 /*
1776 * If may is zero the SMACK64MMAP subject can't
1777 * possibly have less access.
1778 */
1779 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001780 continue;
1781
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001782 /*
1783 * Fetch the global list entry.
1784 * If there isn't one a SMACK64MMAP subject
1785 * can't have as much access as current.
1786 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001787 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1788 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001789 if (mmay == -ENOENT) {
1790 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001791 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001792 }
1793 /*
1794 * If there is a local entry it modifies the
1795 * potential access, too.
1796 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001797 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1798 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001799 if (tmay != -ENOENT)
1800 mmay &= tmay;
1801
1802 /*
1803 * If there is any access available to current that is
1804 * not available to a SMACK64MMAP subject
1805 * deny access.
1806 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001807 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001808 rc = -EACCES;
1809 break;
1810 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001811 }
1812
1813 rcu_read_unlock();
1814
1815 return rc;
1816}
1817
1818/**
Casey Schauflere114e472008-02-04 22:29:50 -08001819 * smack_file_set_fowner - set the file security blob value
1820 * @file: object in question
1821 *
Casey Schauflere114e472008-02-04 22:29:50 -08001822 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001823static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001824{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001825 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001826}
1827
1828/**
1829 * smack_file_send_sigiotask - Smack on sigio
1830 * @tsk: The target task
1831 * @fown: the object the signal come from
1832 * @signum: unused
1833 *
1834 * Allow a privileged task to get signals even if it shouldn't
1835 *
1836 * Returns 0 if a subject with the object's smack could
1837 * write to the task, an error code otherwise.
1838 */
1839static int smack_file_send_sigiotask(struct task_struct *tsk,
1840 struct fown_struct *fown, int signum)
1841{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001842 struct smack_known *skp;
1843 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001844 struct file *file;
1845 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001846 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001847
1848 /*
1849 * struct fown_struct is never outside the context of a struct file
1850 */
1851 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001852
Etienne Bassetecfcc532009-04-08 20:40:06 +02001853 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001854 skp = file->f_security;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001855 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1856 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001857 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001858 rc = 0;
1859
1860 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1861 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001862 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001863 return rc;
1864}
1865
1866/**
1867 * smack_file_receive - Smack file receive check
1868 * @file: the object
1869 *
1870 * Returns 0 if current has access, error code otherwise
1871 */
1872static int smack_file_receive(struct file *file)
1873{
Casey Schauflerd166c802014-08-27 14:51:27 -07001874 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001875 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001876 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001877 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001878 struct socket *sock;
1879 struct task_smack *tsp;
1880 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001881
Seung-Woo Kim97775822015-04-17 15:25:04 +09001882 if (unlikely(IS_PRIVATE(inode)))
1883 return 0;
1884
Casey Schaufler4482a442013-12-30 17:37:45 -08001885 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001886 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001887
1888 if (S_ISSOCK(inode->i_mode)) {
1889 sock = SOCKET_I(inode);
1890 ssp = sock->sk->sk_security;
1891 tsp = current_security();
1892 /*
1893 * If the receiving process can't write to the
1894 * passed socket or if the passed socket can't
1895 * write to the receiving process don't accept
1896 * the passed socket.
1897 */
1898 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1899 rc = smk_bu_file(file, may, rc);
1900 if (rc < 0)
1901 return rc;
1902 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1903 rc = smk_bu_file(file, may, rc);
1904 return rc;
1905 }
Casey Schauflere114e472008-02-04 22:29:50 -08001906 /*
1907 * This code relies on bitmasks.
1908 */
1909 if (file->f_mode & FMODE_READ)
1910 may = MAY_READ;
1911 if (file->f_mode & FMODE_WRITE)
1912 may |= MAY_WRITE;
1913
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001914 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001915 rc = smk_bu_file(file, may, rc);
1916 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001917}
1918
Casey Schaufler531f1d42011-09-19 12:41:42 -07001919/**
Eric Paris83d49852012-04-04 13:45:40 -04001920 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001921 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001922 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001923 *
1924 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001925 * Allow the open only if the task has read access. There are
1926 * many read operations (e.g. fstat) that you can do with an
1927 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001928 *
1929 * Returns 0
1930 */
Eric Paris83d49852012-04-04 13:45:40 -04001931static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001932{
Casey Schauflera6834c02014-04-21 11:10:26 -07001933 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001934 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001935 struct smk_audit_info ad;
1936 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001937
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001938 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001939 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001940
Casey Schauflera6834c02014-04-21 11:10:26 -07001941 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1942 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001943 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001944 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001945
1946 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001947}
1948
Casey Schauflere114e472008-02-04 22:29:50 -08001949/*
1950 * Task hooks
1951 */
1952
1953/**
David Howellsee18d642009-09-02 09:14:21 +01001954 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1955 * @new: the new credentials
1956 * @gfp: the atomicity of any memory allocations
1957 *
1958 * Prepare a blank set of credentials for modification. This must allocate all
1959 * the memory the LSM module might require such that cred_transfer() can
1960 * complete without error.
1961 */
1962static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1963{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001964 struct task_smack *tsp;
1965
1966 tsp = new_task_smack(NULL, NULL, gfp);
1967 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001968 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001969
1970 cred->security = tsp;
1971
David Howellsee18d642009-09-02 09:14:21 +01001972 return 0;
1973}
1974
1975
1976/**
David Howellsf1752ee2008-11-14 10:39:17 +11001977 * smack_cred_free - "free" task-level security credentials
1978 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001979 *
Casey Schauflere114e472008-02-04 22:29:50 -08001980 */
David Howellsf1752ee2008-11-14 10:39:17 +11001981static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001982{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001983 struct task_smack *tsp = cred->security;
1984 struct smack_rule *rp;
1985 struct list_head *l;
1986 struct list_head *n;
1987
1988 if (tsp == NULL)
1989 return;
1990 cred->security = NULL;
1991
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001992 smk_destroy_label_list(&tsp->smk_relabel);
1993
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001994 list_for_each_safe(l, n, &tsp->smk_rules) {
1995 rp = list_entry(l, struct smack_rule, list);
1996 list_del(&rp->list);
1997 kfree(rp);
1998 }
1999 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08002000}
2001
2002/**
David Howellsd84f4f92008-11-14 10:39:23 +11002003 * smack_cred_prepare - prepare new set of credentials for modification
2004 * @new: the new credentials
2005 * @old: the original credentials
2006 * @gfp: the atomicity of any memory allocations
2007 *
2008 * Prepare a new set of credentials for modification.
2009 */
2010static int smack_cred_prepare(struct cred *new, const struct cred *old,
2011 gfp_t gfp)
2012{
Casey Schaufler676dac42010-12-02 06:43:39 -08002013 struct task_smack *old_tsp = old->security;
2014 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002015 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002016
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002017 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002018 if (new_tsp == NULL)
2019 return -ENOMEM;
2020
Himanshu Shuklab437aba2016-11-10 16:17:02 +05302021 new->security = new_tsp;
2022
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002023 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2024 if (rc != 0)
2025 return rc;
2026
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002027 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2028 gfp);
2029 if (rc != 0)
2030 return rc;
2031
David Howellsd84f4f92008-11-14 10:39:23 +11002032 return 0;
2033}
2034
Randy Dunlap251a2a92009-02-18 11:42:33 -08002035/**
David Howellsee18d642009-09-02 09:14:21 +01002036 * smack_cred_transfer - Transfer the old credentials to the new credentials
2037 * @new: the new credentials
2038 * @old: the original credentials
2039 *
2040 * Fill in a set of blank credentials from another set of credentials.
2041 */
2042static void smack_cred_transfer(struct cred *new, const struct cred *old)
2043{
Casey Schaufler676dac42010-12-02 06:43:39 -08002044 struct task_smack *old_tsp = old->security;
2045 struct task_smack *new_tsp = new->security;
2046
2047 new_tsp->smk_task = old_tsp->smk_task;
2048 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002049 mutex_init(&new_tsp->smk_rules_lock);
2050 INIT_LIST_HEAD(&new_tsp->smk_rules);
2051
2052
2053 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01002054}
2055
2056/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002057 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002058 * @new: points to the set of credentials to be modified.
2059 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002060 *
2061 * Set the security data for a kernel service.
2062 */
2063static int smack_kernel_act_as(struct cred *new, u32 secid)
2064{
Casey Schaufler676dac42010-12-02 06:43:39 -08002065 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002066
Casey Schaufler152f91d2016-11-14 09:38:15 -08002067 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002068 return 0;
2069}
2070
2071/**
2072 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002073 * @new: points to the set of credentials to be modified
2074 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002075 *
2076 * Set the file creation context in a set of credentials to the same
2077 * as the objective context of the specified inode
2078 */
2079static int smack_kernel_create_files_as(struct cred *new,
2080 struct inode *inode)
2081{
2082 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002083 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002084
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002085 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002086 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002087 return 0;
2088}
2089
2090/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002091 * smk_curacc_on_task - helper to log task related access
2092 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002093 * @access: the access requested
2094 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002095 *
2096 * Return 0 if access is permitted
2097 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002098static int smk_curacc_on_task(struct task_struct *p, int access,
2099 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002100{
2101 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002102 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002103 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002104
Casey Schaufler531f1d42011-09-19 12:41:42 -07002105 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002106 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002107 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002108 rc = smk_bu_task(p, access, rc);
2109 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002110}
2111
2112/**
Casey Schauflere114e472008-02-04 22:29:50 -08002113 * smack_task_setpgid - Smack check on setting pgid
2114 * @p: the task object
2115 * @pgid: unused
2116 *
2117 * Return 0 if write access is permitted
2118 */
2119static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2120{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002121 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002122}
2123
2124/**
2125 * smack_task_getpgid - Smack access check for getpgid
2126 * @p: the object task
2127 *
2128 * Returns 0 if current can read the object task, error code otherwise
2129 */
2130static int smack_task_getpgid(struct task_struct *p)
2131{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002132 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002133}
2134
2135/**
2136 * smack_task_getsid - Smack access check for getsid
2137 * @p: the object task
2138 *
2139 * Returns 0 if current can read the object task, error code otherwise
2140 */
2141static int smack_task_getsid(struct task_struct *p)
2142{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002143 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002144}
2145
2146/**
2147 * smack_task_getsecid - get the secid of the task
2148 * @p: the object task
2149 * @secid: where to put the result
2150 *
2151 * Sets the secid to contain a u32 version of the smack label.
2152 */
2153static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2154{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002155 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002156
2157 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002158}
2159
2160/**
2161 * smack_task_setnice - Smack check on setting nice
2162 * @p: the task object
2163 * @nice: unused
2164 *
2165 * Return 0 if write access is permitted
2166 */
2167static int smack_task_setnice(struct task_struct *p, int nice)
2168{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002169 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002170}
2171
2172/**
2173 * smack_task_setioprio - Smack check on setting ioprio
2174 * @p: the task object
2175 * @ioprio: unused
2176 *
2177 * Return 0 if write access is permitted
2178 */
2179static int smack_task_setioprio(struct task_struct *p, int ioprio)
2180{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002181 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002182}
2183
2184/**
2185 * smack_task_getioprio - Smack check on reading ioprio
2186 * @p: the task object
2187 *
2188 * Return 0 if read access is permitted
2189 */
2190static int smack_task_getioprio(struct task_struct *p)
2191{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002192 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002193}
2194
2195/**
2196 * smack_task_setscheduler - Smack check on setting scheduler
2197 * @p: the task object
2198 * @policy: unused
2199 * @lp: unused
2200 *
2201 * Return 0 if read access is permitted
2202 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002203static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002204{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002205 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002206}
2207
2208/**
2209 * smack_task_getscheduler - Smack check on reading scheduler
2210 * @p: the task object
2211 *
2212 * Return 0 if read access is permitted
2213 */
2214static int smack_task_getscheduler(struct task_struct *p)
2215{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002216 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002217}
2218
2219/**
2220 * smack_task_movememory - Smack check on moving memory
2221 * @p: the task object
2222 *
2223 * Return 0 if write access is permitted
2224 */
2225static int smack_task_movememory(struct task_struct *p)
2226{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002227 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002228}
2229
2230/**
2231 * smack_task_kill - Smack check on signal delivery
2232 * @p: the task object
2233 * @info: unused
2234 * @sig: unused
2235 * @secid: identifies the smack to use in lieu of current's
2236 *
2237 * Return 0 if write access is permitted
2238 *
2239 * The secid behavior is an artifact of an SELinux hack
2240 * in the USB code. Someday it may go away.
2241 */
2242static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2243 int sig, u32 secid)
2244{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002245 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002246 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002247 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002248 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002249
Rafal Krypa18d872f2016-04-04 11:14:53 +02002250 if (!sig)
2251 return 0; /* null signal; existence test */
2252
Etienne Bassetecfcc532009-04-08 20:40:06 +02002253 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2254 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002255 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002256 * Sending a signal requires that the sender
2257 * can write the receiver.
2258 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002259 if (secid == 0) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002260 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2261 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002262 return rc;
2263 }
Casey Schauflere114e472008-02-04 22:29:50 -08002264 /*
2265 * If the secid isn't 0 we're dealing with some USB IO
2266 * specific behavior. This is not clean. For one thing
2267 * we can't take privilege into account.
2268 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002269 skp = smack_from_secid(secid);
Casey Schauflerc60b9062016-08-30 10:31:39 -07002270 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2271 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002272 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002273}
2274
2275/**
2276 * smack_task_wait - Smack access check for waiting
2277 * @p: task to wait for
2278 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002279 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002280 */
2281static int smack_task_wait(struct task_struct *p)
2282{
Casey Schauflere114e472008-02-04 22:29:50 -08002283 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002284 * Allow the operation to succeed.
2285 * Zombies are bad.
2286 * In userless environments (e.g. phones) programs
2287 * get marked with SMACK64EXEC and even if the parent
2288 * and child shouldn't be talking the parent still
2289 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002290 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002291 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002292}
2293
2294/**
2295 * smack_task_to_inode - copy task smack into the inode blob
2296 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002297 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002298 *
2299 * Sets the smack pointer in the inode security blob
2300 */
2301static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2302{
2303 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002304 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002305
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002306 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002307}
2308
2309/*
2310 * Socket hooks.
2311 */
2312
2313/**
2314 * smack_sk_alloc_security - Allocate a socket blob
2315 * @sk: the socket
2316 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002317 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002318 *
2319 * Assign Smack pointers to current
2320 *
2321 * Returns 0 on success, -ENOMEM is there's no memory
2322 */
2323static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2324{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002325 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002326 struct socket_smack *ssp;
2327
2328 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2329 if (ssp == NULL)
2330 return -ENOMEM;
2331
jooseong lee08382c92016-11-03 11:54:39 +01002332 /*
2333 * Sockets created by kernel threads receive web label.
2334 */
2335 if (unlikely(current->flags & PF_KTHREAD)) {
2336 ssp->smk_in = &smack_known_web;
2337 ssp->smk_out = &smack_known_web;
2338 } else {
2339 ssp->smk_in = skp;
2340 ssp->smk_out = skp;
2341 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002342 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002343
2344 sk->sk_security = ssp;
2345
2346 return 0;
2347}
2348
2349/**
2350 * smack_sk_free_security - Free a socket blob
2351 * @sk: the socket
2352 *
2353 * Clears the blob pointer
2354 */
2355static void smack_sk_free_security(struct sock *sk)
2356{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302357#ifdef SMACK_IPV6_PORT_LABELING
2358 struct smk_port_label *spp;
2359
2360 if (sk->sk_family == PF_INET6) {
2361 rcu_read_lock();
2362 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2363 if (spp->smk_sock != sk)
2364 continue;
2365 spp->smk_can_reuse = 1;
2366 break;
2367 }
2368 rcu_read_unlock();
2369 }
2370#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002371 kfree(sk->sk_security);
2372}
2373
2374/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002375* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002376* @sip: the object end
2377*
2378* looks for host based access restrictions
2379*
2380* This version will only be appropriate for really small sets of single label
2381* hosts. The caller is responsible for ensuring that the RCU read lock is
2382* taken before calling this function.
2383*
2384* Returns the label of the far end or NULL if it's not special.
2385*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002386static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002387{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002388 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002389 struct in_addr *siap = &sip->sin_addr;
2390
2391 if (siap->s_addr == 0)
2392 return NULL;
2393
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002394 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2395 /*
2396 * we break after finding the first match because
2397 * the list is sorted from longest to shortest mask
2398 * so we have found the most specific match
2399 */
2400 if (snp->smk_host.s_addr ==
2401 (siap->s_addr & snp->smk_mask.s_addr))
2402 return snp->smk_label;
2403
2404 return NULL;
2405}
2406
2407#if IS_ENABLED(CONFIG_IPV6)
2408/*
2409 * smk_ipv6_localhost - Check for local ipv6 host address
2410 * @sip: the address
2411 *
2412 * Returns boolean true if this is the localhost address
2413 */
2414static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2415{
2416 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2417 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2418
2419 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2420 ntohs(be16p[7]) == 1)
2421 return true;
2422 return false;
2423}
2424
2425/**
2426* smack_ipv6host_label - check host based restrictions
2427* @sip: the object end
2428*
2429* looks for host based access restrictions
2430*
2431* This version will only be appropriate for really small sets of single label
2432* hosts. The caller is responsible for ensuring that the RCU read lock is
2433* taken before calling this function.
2434*
2435* Returns the label of the far end or NULL if it's not special.
2436*/
2437static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2438{
2439 struct smk_net6addr *snp;
2440 struct in6_addr *sap = &sip->sin6_addr;
2441 int i;
2442 int found = 0;
2443
2444 /*
2445 * It's local. Don't look for a host label.
2446 */
2447 if (smk_ipv6_localhost(sip))
2448 return NULL;
2449
2450 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002451 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002452 * If the label is NULL the entry has
2453 * been renounced. Ignore it.
2454 */
2455 if (snp->smk_label == NULL)
2456 continue;
2457 /*
Paul Moore07feee82009-03-27 17:10:54 -04002458 * we break after finding the first match because
2459 * the list is sorted from longest to shortest mask
2460 * so we have found the most specific match
2461 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002462 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002463 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2464 snp->smk_host.s6_addr16[i]) {
2465 found = 0;
2466 break;
2467 }
Etienne Basset43031542009-03-27 17:11:01 -04002468 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002469 if (found)
2470 return snp->smk_label;
2471 }
Paul Moore07feee82009-03-27 17:10:54 -04002472
2473 return NULL;
2474}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002475#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002476
2477/**
Casey Schauflere114e472008-02-04 22:29:50 -08002478 * smack_netlabel - Set the secattr on a socket
2479 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002480 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002481 *
2482 * Convert the outbound smack value (smk_out) to a
2483 * secattr and attach it to the socket.
2484 *
2485 * Returns 0 on success or an error code
2486 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002487static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002488{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002489 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002490 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002491 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002492
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002493 /*
2494 * Usually the netlabel code will handle changing the
2495 * packet labeling based on the label.
2496 * The case of a single label host is different, because
2497 * a single label host should never get a labeled packet
2498 * even though the label is usually associated with a packet
2499 * label.
2500 */
2501 local_bh_disable();
2502 bh_lock_sock_nested(sk);
2503
2504 if (ssp->smk_out == smack_net_ambient ||
2505 labeled == SMACK_UNLABELED_SOCKET)
2506 netlbl_sock_delattr(sk);
2507 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002508 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002509 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002510 }
2511
2512 bh_unlock_sock(sk);
2513 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002514
Casey Schauflere114e472008-02-04 22:29:50 -08002515 return rc;
2516}
2517
2518/**
Paul Moore07feee82009-03-27 17:10:54 -04002519 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2520 * @sk: the socket
2521 * @sap: the destination address
2522 *
2523 * Set the correct secattr for the given socket based on the destination
2524 * address and perform any outbound access checks needed.
2525 *
2526 * Returns 0 on success or an error code.
2527 *
2528 */
2529static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2530{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002531 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002532 int rc;
2533 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002534 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002535 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002536 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002537
2538 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002539 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002540 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002541#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002542 struct lsm_network_audit net;
2543
Eric Paris48c62af2012-04-02 13:15:44 -04002544 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2545 ad.a.u.net->family = sap->sin_family;
2546 ad.a.u.net->dport = sap->sin_port;
2547 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002548#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002549 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002550 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002551 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2552 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002553 } else {
2554 sk_lbl = SMACK_CIPSO_SOCKET;
2555 rc = 0;
2556 }
2557 rcu_read_unlock();
2558 if (rc != 0)
2559 return rc;
2560
2561 return smack_netlabel(sk, sk_lbl);
2562}
2563
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002564#if IS_ENABLED(CONFIG_IPV6)
2565/**
2566 * smk_ipv6_check - check Smack access
2567 * @subject: subject Smack label
2568 * @object: object Smack label
2569 * @address: address
2570 * @act: the action being taken
2571 *
2572 * Check an IPv6 access
2573 */
2574static int smk_ipv6_check(struct smack_known *subject,
2575 struct smack_known *object,
2576 struct sockaddr_in6 *address, int act)
2577{
2578#ifdef CONFIG_AUDIT
2579 struct lsm_network_audit net;
2580#endif
2581 struct smk_audit_info ad;
2582 int rc;
2583
2584#ifdef CONFIG_AUDIT
2585 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2586 ad.a.u.net->family = PF_INET6;
2587 ad.a.u.net->dport = ntohs(address->sin6_port);
2588 if (act == SMK_RECEIVING)
2589 ad.a.u.net->v6info.saddr = address->sin6_addr;
2590 else
2591 ad.a.u.net->v6info.daddr = address->sin6_addr;
2592#endif
2593 rc = smk_access(subject, object, MAY_WRITE, &ad);
2594 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2595 return rc;
2596}
2597#endif /* CONFIG_IPV6 */
2598
2599#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002600/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002601 * smk_ipv6_port_label - Smack port access table management
2602 * @sock: socket
2603 * @address: address
2604 *
2605 * Create or update the port list entry
2606 */
2607static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2608{
2609 struct sock *sk = sock->sk;
2610 struct sockaddr_in6 *addr6;
2611 struct socket_smack *ssp = sock->sk->sk_security;
2612 struct smk_port_label *spp;
2613 unsigned short port = 0;
2614
2615 if (address == NULL) {
2616 /*
2617 * This operation is changing the Smack information
2618 * on the bound socket. Take the changes to the port
2619 * as well.
2620 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302621 rcu_read_lock();
2622 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 if (sk != spp->smk_sock)
2624 continue;
2625 spp->smk_in = ssp->smk_in;
2626 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302627 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002628 return;
2629 }
2630 /*
2631 * A NULL address is only used for updating existing
2632 * bound entries. If there isn't one, it's OK.
2633 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302634 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002635 return;
2636 }
2637
2638 addr6 = (struct sockaddr_in6 *)address;
2639 port = ntohs(addr6->sin6_port);
2640 /*
2641 * This is a special case that is safely ignored.
2642 */
2643 if (port == 0)
2644 return;
2645
2646 /*
2647 * Look for an existing port list entry.
2648 * This is an indication that a port is getting reused.
2649 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302650 rcu_read_lock();
2651 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302652 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002653 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302654 if (spp->smk_can_reuse != 1) {
2655 rcu_read_unlock();
2656 return;
2657 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002658 spp->smk_port = port;
2659 spp->smk_sock = sk;
2660 spp->smk_in = ssp->smk_in;
2661 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302662 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302663 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002664 return;
2665 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302666 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002667 /*
2668 * A new port entry is required.
2669 */
2670 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2671 if (spp == NULL)
2672 return;
2673
2674 spp->smk_port = port;
2675 spp->smk_sock = sk;
2676 spp->smk_in = ssp->smk_in;
2677 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302678 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302679 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002680
Vishal Goel3c7ce342016-11-23 10:31:08 +05302681 mutex_lock(&smack_ipv6_lock);
2682 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2683 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002684 return;
2685}
2686
2687/**
2688 * smk_ipv6_port_check - check Smack port access
2689 * @sock: socket
2690 * @address: address
2691 *
2692 * Create or update the port list entry
2693 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002694static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002695 int act)
2696{
Casey Schauflerc6739442013-05-22 18:42:56 -07002697 struct smk_port_label *spp;
2698 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002699 struct smack_known *skp = NULL;
2700 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002701 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002702
2703 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002704 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002705 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002706 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002707 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002708 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002709 }
2710
2711 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002712 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002713 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002714 if (skp != NULL && object != NULL)
2715 return smk_ipv6_check(skp, object, address, act);
2716 if (skp == NULL)
2717 skp = smack_net_ambient;
2718 if (object == NULL)
2719 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002720
2721 /*
2722 * It's remote, so port lookup does no good.
2723 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002724 if (!smk_ipv6_localhost(address))
2725 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002726
2727 /*
2728 * It's local so the send check has to have passed.
2729 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002730 if (act == SMK_RECEIVING)
2731 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002732
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002733 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302734 rcu_read_lock();
2735 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302736 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002737 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002738 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002739 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002740 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002741 break;
2742 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302743 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002744
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002745 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002746}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002747#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002748
2749/**
Casey Schauflere114e472008-02-04 22:29:50 -08002750 * smack_inode_setsecurity - set smack xattrs
2751 * @inode: the object
2752 * @name: attribute name
2753 * @value: attribute value
2754 * @size: size of the attribute
2755 * @flags: unused
2756 *
2757 * Sets the named attribute in the appropriate blob
2758 *
2759 * Returns 0 on success, or an error code
2760 */
2761static int smack_inode_setsecurity(struct inode *inode, const char *name,
2762 const void *value, size_t size, int flags)
2763{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002764 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002765 struct inode_smack *nsp = inode->i_security;
2766 struct socket_smack *ssp;
2767 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002768 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002769
Casey Schauflerf7112e62012-05-06 15:22:02 -07002770 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302771 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002772
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002773 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002774 if (IS_ERR(skp))
2775 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002776
2777 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002778 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002779 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002780 return 0;
2781 }
2782 /*
2783 * The rest of the Smack xattrs are only on sockets.
2784 */
2785 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2786 return -EOPNOTSUPP;
2787
2788 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002789 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002790 return -EOPNOTSUPP;
2791
2792 ssp = sock->sk->sk_security;
2793
2794 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002795 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002796 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002797 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002798 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002799 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2800 if (rc != 0)
2801 printk(KERN_WARNING
2802 "Smack: \"%s\" netlbl error %d.\n",
2803 __func__, -rc);
2804 }
Casey Schauflere114e472008-02-04 22:29:50 -08002805 } else
2806 return -EOPNOTSUPP;
2807
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002808#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002809 if (sock->sk->sk_family == PF_INET6)
2810 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002811#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002812
Casey Schauflere114e472008-02-04 22:29:50 -08002813 return 0;
2814}
2815
2816/**
2817 * smack_socket_post_create - finish socket setup
2818 * @sock: the socket
2819 * @family: protocol family
2820 * @type: unused
2821 * @protocol: unused
2822 * @kern: unused
2823 *
2824 * Sets the netlabel information on the socket
2825 *
2826 * Returns 0 on success, and error code otherwise
2827 */
2828static int smack_socket_post_create(struct socket *sock, int family,
2829 int type, int protocol, int kern)
2830{
Marcin Lis74123012015-01-22 15:40:33 +01002831 struct socket_smack *ssp;
2832
2833 if (sock->sk == NULL)
2834 return 0;
2835
2836 /*
2837 * Sockets created by kernel threads receive web label.
2838 */
2839 if (unlikely(current->flags & PF_KTHREAD)) {
2840 ssp = sock->sk->sk_security;
2841 ssp->smk_in = &smack_known_web;
2842 ssp->smk_out = &smack_known_web;
2843 }
2844
2845 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002846 return 0;
2847 /*
2848 * Set the outbound netlbl.
2849 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002850 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2851}
2852
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002853#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002854/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002855 * smack_socket_bind - record port binding information.
2856 * @sock: the socket
2857 * @address: the port address
2858 * @addrlen: size of the address
2859 *
2860 * Records the label bound to a port.
2861 *
2862 * Returns 0
2863 */
2864static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2865 int addrlen)
2866{
2867 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2868 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002869 return 0;
2870}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002871#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002872
2873/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002874 * smack_socket_connect - connect access check
2875 * @sock: the socket
2876 * @sap: the other end
2877 * @addrlen: size of sap
2878 *
2879 * Verifies that a connection may be possible
2880 *
2881 * Returns 0 on success, and error code otherwise
2882 */
2883static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2884 int addrlen)
2885{
Casey Schauflerc6739442013-05-22 18:42:56 -07002886 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002887#if IS_ENABLED(CONFIG_IPV6)
2888 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2889#endif
2890#ifdef SMACK_IPV6_SECMARK_LABELING
2891 struct smack_known *rsp;
2892 struct socket_smack *ssp = sock->sk->sk_security;
2893#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002894
Casey Schauflerc6739442013-05-22 18:42:56 -07002895 if (sock->sk == NULL)
2896 return 0;
2897
2898 switch (sock->sk->sk_family) {
2899 case PF_INET:
2900 if (addrlen < sizeof(struct sockaddr_in))
2901 return -EINVAL;
2902 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2903 break;
2904 case PF_INET6:
2905 if (addrlen < sizeof(struct sockaddr_in6))
2906 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002907#ifdef SMACK_IPV6_SECMARK_LABELING
2908 rsp = smack_ipv6host_label(sip);
2909 if (rsp != NULL)
2910 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002911 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002912#endif
2913#ifdef SMACK_IPV6_PORT_LABELING
2914 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2915#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002916 break;
2917 }
2918 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002919}
2920
2921/**
2922 * smack_flags_to_may - convert S_ to MAY_ values
2923 * @flags: the S_ value
2924 *
2925 * Returns the equivalent MAY_ value
2926 */
2927static int smack_flags_to_may(int flags)
2928{
2929 int may = 0;
2930
2931 if (flags & S_IRUGO)
2932 may |= MAY_READ;
2933 if (flags & S_IWUGO)
2934 may |= MAY_WRITE;
2935 if (flags & S_IXUGO)
2936 may |= MAY_EXEC;
2937
2938 return may;
2939}
2940
2941/**
2942 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2943 * @msg: the object
2944 *
2945 * Returns 0
2946 */
2947static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2948{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002949 struct smack_known *skp = smk_of_current();
2950
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002951 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002952 return 0;
2953}
2954
2955/**
2956 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2957 * @msg: the object
2958 *
2959 * Clears the blob pointer
2960 */
2961static void smack_msg_msg_free_security(struct msg_msg *msg)
2962{
2963 msg->security = NULL;
2964}
2965
2966/**
2967 * smack_of_shm - the smack pointer for the shm
2968 * @shp: the object
2969 *
2970 * Returns a pointer to the smack value
2971 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002972static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002973{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002974 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002975}
2976
2977/**
2978 * smack_shm_alloc_security - Set the security blob for shm
2979 * @shp: the object
2980 *
2981 * Returns 0
2982 */
2983static int smack_shm_alloc_security(struct shmid_kernel *shp)
2984{
2985 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002986 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002987
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002988 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002989 return 0;
2990}
2991
2992/**
2993 * smack_shm_free_security - Clear the security blob for shm
2994 * @shp: the object
2995 *
2996 * Clears the blob pointer
2997 */
2998static void smack_shm_free_security(struct shmid_kernel *shp)
2999{
3000 struct kern_ipc_perm *isp = &shp->shm_perm;
3001
3002 isp->security = NULL;
3003}
3004
3005/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003006 * smk_curacc_shm : check if current has access on shm
3007 * @shp : the object
3008 * @access : access requested
3009 *
3010 * Returns 0 if current has the requested access, error code otherwise
3011 */
3012static int smk_curacc_shm(struct shmid_kernel *shp, int access)
3013{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003014 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003015 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003016 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003017
3018#ifdef CONFIG_AUDIT
3019 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3020 ad.a.u.ipc_id = shp->shm_perm.id;
3021#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003022 rc = smk_curacc(ssp, access, &ad);
3023 rc = smk_bu_current("shm", ssp, access, rc);
3024 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003025}
3026
3027/**
Casey Schauflere114e472008-02-04 22:29:50 -08003028 * smack_shm_associate - Smack access check for shm
3029 * @shp: the object
3030 * @shmflg: access requested
3031 *
3032 * Returns 0 if current has the requested access, error code otherwise
3033 */
3034static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
3035{
Casey Schauflere114e472008-02-04 22:29:50 -08003036 int may;
3037
3038 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003039 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003040}
3041
3042/**
3043 * smack_shm_shmctl - Smack access check for shm
3044 * @shp: the object
3045 * @cmd: what it wants to do
3046 *
3047 * Returns 0 if current has the requested access, error code otherwise
3048 */
3049static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
3050{
Casey Schauflere114e472008-02-04 22:29:50 -08003051 int may;
3052
3053 switch (cmd) {
3054 case IPC_STAT:
3055 case SHM_STAT:
3056 may = MAY_READ;
3057 break;
3058 case IPC_SET:
3059 case SHM_LOCK:
3060 case SHM_UNLOCK:
3061 case IPC_RMID:
3062 may = MAY_READWRITE;
3063 break;
3064 case IPC_INFO:
3065 case SHM_INFO:
3066 /*
3067 * System level information.
3068 */
3069 return 0;
3070 default:
3071 return -EINVAL;
3072 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02003073 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003074}
3075
3076/**
3077 * smack_shm_shmat - Smack access for shmat
3078 * @shp: the object
3079 * @shmaddr: unused
3080 * @shmflg: access requested
3081 *
3082 * Returns 0 if current has the requested access, error code otherwise
3083 */
3084static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
3085 int shmflg)
3086{
Casey Schauflere114e472008-02-04 22:29:50 -08003087 int may;
3088
3089 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003090 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003091}
3092
3093/**
3094 * smack_of_sem - the smack pointer for the sem
3095 * @sma: the object
3096 *
3097 * Returns a pointer to the smack value
3098 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003099static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08003100{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003101 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08003102}
3103
3104/**
3105 * smack_sem_alloc_security - Set the security blob for sem
3106 * @sma: the object
3107 *
3108 * Returns 0
3109 */
3110static int smack_sem_alloc_security(struct sem_array *sma)
3111{
3112 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003113 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003114
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003115 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003116 return 0;
3117}
3118
3119/**
3120 * smack_sem_free_security - Clear the security blob for sem
3121 * @sma: the object
3122 *
3123 * Clears the blob pointer
3124 */
3125static void smack_sem_free_security(struct sem_array *sma)
3126{
3127 struct kern_ipc_perm *isp = &sma->sem_perm;
3128
3129 isp->security = NULL;
3130}
3131
3132/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003133 * smk_curacc_sem : check if current has access on sem
3134 * @sma : the object
3135 * @access : access requested
3136 *
3137 * Returns 0 if current has the requested access, error code otherwise
3138 */
3139static int smk_curacc_sem(struct sem_array *sma, int access)
3140{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003141 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003142 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003143 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003144
3145#ifdef CONFIG_AUDIT
3146 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3147 ad.a.u.ipc_id = sma->sem_perm.id;
3148#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003149 rc = smk_curacc(ssp, access, &ad);
3150 rc = smk_bu_current("sem", ssp, access, rc);
3151 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003152}
3153
3154/**
Casey Schauflere114e472008-02-04 22:29:50 -08003155 * smack_sem_associate - Smack access check for sem
3156 * @sma: the object
3157 * @semflg: access requested
3158 *
3159 * Returns 0 if current has the requested access, error code otherwise
3160 */
3161static int smack_sem_associate(struct sem_array *sma, int semflg)
3162{
Casey Schauflere114e472008-02-04 22:29:50 -08003163 int may;
3164
3165 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003166 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003167}
3168
3169/**
3170 * smack_sem_shmctl - Smack access check for sem
3171 * @sma: the object
3172 * @cmd: what it wants to do
3173 *
3174 * Returns 0 if current has the requested access, error code otherwise
3175 */
3176static int smack_sem_semctl(struct sem_array *sma, int cmd)
3177{
Casey Schauflere114e472008-02-04 22:29:50 -08003178 int may;
3179
3180 switch (cmd) {
3181 case GETPID:
3182 case GETNCNT:
3183 case GETZCNT:
3184 case GETVAL:
3185 case GETALL:
3186 case IPC_STAT:
3187 case SEM_STAT:
3188 may = MAY_READ;
3189 break;
3190 case SETVAL:
3191 case SETALL:
3192 case IPC_RMID:
3193 case IPC_SET:
3194 may = MAY_READWRITE;
3195 break;
3196 case IPC_INFO:
3197 case SEM_INFO:
3198 /*
3199 * System level information
3200 */
3201 return 0;
3202 default:
3203 return -EINVAL;
3204 }
3205
Etienne Bassetecfcc532009-04-08 20:40:06 +02003206 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003207}
3208
3209/**
3210 * smack_sem_semop - Smack checks of semaphore operations
3211 * @sma: the object
3212 * @sops: unused
3213 * @nsops: unused
3214 * @alter: unused
3215 *
3216 * Treated as read and write in all cases.
3217 *
3218 * Returns 0 if access is allowed, error code otherwise
3219 */
3220static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
3221 unsigned nsops, int alter)
3222{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003223 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003224}
3225
3226/**
3227 * smack_msg_alloc_security - Set the security blob for msg
3228 * @msq: the object
3229 *
3230 * Returns 0
3231 */
3232static int smack_msg_queue_alloc_security(struct msg_queue *msq)
3233{
3234 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003235 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003236
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003237 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003238 return 0;
3239}
3240
3241/**
3242 * smack_msg_free_security - Clear the security blob for msg
3243 * @msq: the object
3244 *
3245 * Clears the blob pointer
3246 */
3247static void smack_msg_queue_free_security(struct msg_queue *msq)
3248{
3249 struct kern_ipc_perm *kisp = &msq->q_perm;
3250
3251 kisp->security = NULL;
3252}
3253
3254/**
3255 * smack_of_msq - the smack pointer for the msq
3256 * @msq: the object
3257 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003258 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08003259 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003260static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08003261{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003262 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08003263}
3264
3265/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003266 * smk_curacc_msq : helper to check if current has access on msq
3267 * @msq : the msq
3268 * @access : access requested
3269 *
3270 * return 0 if current has access, error otherwise
3271 */
3272static int smk_curacc_msq(struct msg_queue *msq, int access)
3273{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003274 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003275 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003276 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003277
3278#ifdef CONFIG_AUDIT
3279 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3280 ad.a.u.ipc_id = msq->q_perm.id;
3281#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003282 rc = smk_curacc(msp, access, &ad);
3283 rc = smk_bu_current("msq", msp, access, rc);
3284 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003285}
3286
3287/**
Casey Schauflere114e472008-02-04 22:29:50 -08003288 * smack_msg_queue_associate - Smack access check for msg_queue
3289 * @msq: the object
3290 * @msqflg: access requested
3291 *
3292 * Returns 0 if current has the requested access, error code otherwise
3293 */
3294static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
3295{
Casey Schauflere114e472008-02-04 22:29:50 -08003296 int may;
3297
3298 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003299 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003300}
3301
3302/**
3303 * smack_msg_queue_msgctl - Smack access check for msg_queue
3304 * @msq: the object
3305 * @cmd: what it wants to do
3306 *
3307 * Returns 0 if current has the requested access, error code otherwise
3308 */
3309static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
3310{
Casey Schauflere114e472008-02-04 22:29:50 -08003311 int may;
3312
3313 switch (cmd) {
3314 case IPC_STAT:
3315 case MSG_STAT:
3316 may = MAY_READ;
3317 break;
3318 case IPC_SET:
3319 case IPC_RMID:
3320 may = MAY_READWRITE;
3321 break;
3322 case IPC_INFO:
3323 case MSG_INFO:
3324 /*
3325 * System level information
3326 */
3327 return 0;
3328 default:
3329 return -EINVAL;
3330 }
3331
Etienne Bassetecfcc532009-04-08 20:40:06 +02003332 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003333}
3334
3335/**
3336 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3337 * @msq: the object
3338 * @msg: unused
3339 * @msqflg: access requested
3340 *
3341 * Returns 0 if current has the requested access, error code otherwise
3342 */
3343static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3344 int msqflg)
3345{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003346 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003347
Etienne Bassetecfcc532009-04-08 20:40:06 +02003348 may = smack_flags_to_may(msqflg);
3349 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003350}
3351
3352/**
3353 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3354 * @msq: the object
3355 * @msg: unused
3356 * @target: unused
3357 * @type: unused
3358 * @mode: unused
3359 *
3360 * Returns 0 if current has read and write access, error code otherwise
3361 */
3362static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3363 struct task_struct *target, long type, int mode)
3364{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003365 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003366}
3367
3368/**
3369 * smack_ipc_permission - Smack access for ipc_permission()
3370 * @ipp: the object permissions
3371 * @flag: access requested
3372 *
3373 * Returns 0 if current has read and write access, error code otherwise
3374 */
3375static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3376{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003377 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003378 int may = smack_flags_to_may(flag);
3379 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003380 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003381
Etienne Bassetecfcc532009-04-08 20:40:06 +02003382#ifdef CONFIG_AUDIT
3383 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3384 ad.a.u.ipc_id = ipp->id;
3385#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003386 rc = smk_curacc(iskp, may, &ad);
3387 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003388 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003389}
3390
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003391/**
3392 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003393 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003394 * @secid: where result will be saved
3395 */
3396static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3397{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003398 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003399
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003400 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003401}
3402
Casey Schauflere114e472008-02-04 22:29:50 -08003403/**
3404 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003405 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003406 * @inode: the object
3407 *
3408 * Set the inode's security blob if it hasn't been done already.
3409 */
3410static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3411{
3412 struct super_block *sbp;
3413 struct superblock_smack *sbsp;
3414 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003415 struct smack_known *skp;
3416 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003417 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003418 char trattr[TRANS_TRUE_SIZE];
3419 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003420 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003421 struct dentry *dp;
3422
3423 if (inode == NULL)
3424 return;
3425
3426 isp = inode->i_security;
3427
3428 mutex_lock(&isp->smk_lock);
3429 /*
3430 * If the inode is already instantiated
3431 * take the quick way out
3432 */
3433 if (isp->smk_flags & SMK_INODE_INSTANT)
3434 goto unlockandout;
3435
3436 sbp = inode->i_sb;
3437 sbsp = sbp->s_security;
3438 /*
3439 * We're going to use the superblock default label
3440 * if there's no label on the file.
3441 */
3442 final = sbsp->smk_default;
3443
3444 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003445 * If this is the root inode the superblock
3446 * may be in the process of initialization.
3447 * If that is the case use the root value out
3448 * of the superblock.
3449 */
3450 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003451 switch (sbp->s_magic) {
3452 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003453 /*
3454 * The cgroup filesystem is never mounted,
3455 * so there's no opportunity to set the mount
3456 * options.
3457 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003458 sbsp->smk_root = &smack_known_star;
3459 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003460 isp->smk_inode = sbsp->smk_root;
3461 break;
3462 case TMPFS_MAGIC:
3463 /*
3464 * What about shmem/tmpfs anonymous files with dentry
3465 * obtained from d_alloc_pseudo()?
3466 */
3467 isp->smk_inode = smk_of_current();
3468 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003469 case PIPEFS_MAGIC:
3470 isp->smk_inode = smk_of_current();
3471 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003472 default:
3473 isp->smk_inode = sbsp->smk_root;
3474 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003475 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003476 isp->smk_flags |= SMK_INODE_INSTANT;
3477 goto unlockandout;
3478 }
3479
3480 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003481 * This is pretty hackish.
3482 * Casey says that we shouldn't have to do
3483 * file system specific code, but it does help
3484 * with keeping it simple.
3485 */
3486 switch (sbp->s_magic) {
3487 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003488 case PIPEFS_MAGIC:
3489 case SOCKFS_MAGIC:
3490 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003491 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003492 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003493 * that the smack file system doesn't do
3494 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003495 *
Casey Schauflere114e472008-02-04 22:29:50 -08003496 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003497 *
3498 * Socket access is controlled by the socket
3499 * structures associated with the task involved.
3500 *
3501 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003502 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003503 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003504 break;
3505 case DEVPTS_SUPER_MAGIC:
3506 /*
3507 * devpts seems content with the label of the task.
3508 * Programs that change smack have to treat the
3509 * pty with respect.
3510 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003511 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003512 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003513 case PROC_SUPER_MAGIC:
3514 /*
3515 * Casey says procfs appears not to care.
3516 * The superblock default suffices.
3517 */
3518 break;
3519 case TMPFS_MAGIC:
3520 /*
3521 * Device labels should come from the filesystem,
3522 * but watch out, because they're volitile,
3523 * getting recreated on every reboot.
3524 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003525 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003526 /*
3527 * No break.
3528 *
3529 * If a smack value has been set we want to use it,
3530 * but since tmpfs isn't giving us the opportunity
3531 * to set mount options simulate setting the
3532 * superblock default.
3533 */
3534 default:
3535 /*
3536 * This isn't an understood special case.
3537 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003538 */
3539
3540 /*
3541 * UNIX domain sockets use lower level socket data.
3542 */
3543 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003544 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003545 break;
3546 }
3547 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003548 * No xattr support means, alas, no SMACK label.
3549 * Use the aforeapplied default.
3550 * It would be curious if the label of the task
3551 * does not match that assigned.
3552 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003553 if (!(inode->i_opflags & IOP_XATTR))
3554 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003555 /*
3556 * Get the dentry for xattr.
3557 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003558 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003559 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003560 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003561 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003562
3563 /*
3564 * Transmuting directory
3565 */
3566 if (S_ISDIR(inode->i_mode)) {
3567 /*
3568 * If this is a new directory and the label was
3569 * transmuted when the inode was initialized
3570 * set the transmute attribute on the directory
3571 * and mark the inode.
3572 *
3573 * If there is a transmute attribute on the
3574 * directory mark the inode.
3575 */
3576 if (isp->smk_flags & SMK_INODE_CHANGED) {
3577 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003578 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003579 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003580 TRANS_TRUE, TRANS_TRUE_SIZE,
3581 0);
3582 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003583 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003584 XATTR_NAME_SMACKTRANSMUTE, trattr,
3585 TRANS_TRUE_SIZE);
3586 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3587 TRANS_TRUE_SIZE) != 0)
3588 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003589 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003590 if (rc >= 0)
3591 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003592 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003593 /*
3594 * Don't let the exec or mmap label be "*" or "@".
3595 */
3596 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3597 if (IS_ERR(skp) || skp == &smack_known_star ||
3598 skp == &smack_known_web)
3599 skp = NULL;
3600 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003601
Casey Schaufler19760ad2013-12-16 16:27:26 -08003602 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003603 if (IS_ERR(skp) || skp == &smack_known_star ||
3604 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003605 skp = NULL;
3606 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003607
Casey Schauflere114e472008-02-04 22:29:50 -08003608 dput(dp);
3609 break;
3610 }
3611
3612 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003613 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003614 else
3615 isp->smk_inode = final;
3616
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003617 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003618
3619unlockandout:
3620 mutex_unlock(&isp->smk_lock);
3621 return;
3622}
3623
3624/**
3625 * smack_getprocattr - Smack process attribute access
3626 * @p: the object task
3627 * @name: the name of the attribute in /proc/.../attr
3628 * @value: where to put the result
3629 *
3630 * Places a copy of the task Smack into value
3631 *
3632 * Returns the length of the smack label or an error code
3633 */
3634static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3635{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003636 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003637 char *cp;
3638 int slen;
3639
3640 if (strcmp(name, "current") != 0)
3641 return -EINVAL;
3642
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003643 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003644 if (cp == NULL)
3645 return -ENOMEM;
3646
3647 slen = strlen(cp);
3648 *value = cp;
3649 return slen;
3650}
3651
3652/**
3653 * smack_setprocattr - Smack process attribute setting
3654 * @p: the object task
3655 * @name: the name of the attribute in /proc/.../attr
3656 * @value: the value to set
3657 * @size: the size of the value
3658 *
3659 * Sets the Smack value of the task. Only setting self
3660 * is permitted and only with privilege
3661 *
3662 * Returns the length of the smack label or an error code
3663 */
3664static int smack_setprocattr(struct task_struct *p, char *name,
3665 void *value, size_t size)
3666{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003667 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003668 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003669 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003670 struct smack_known_list_elem *sklep;
3671 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003672
Casey Schauflere114e472008-02-04 22:29:50 -08003673 /*
3674 * Changing another process' Smack value is too dangerous
3675 * and supports no sane use case.
3676 */
3677 if (p != current)
3678 return -EPERM;
3679
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003680 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003681 return -EPERM;
3682
Casey Schauflerf7112e62012-05-06 15:22:02 -07003683 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003684 return -EINVAL;
3685
3686 if (strcmp(name, "current") != 0)
3687 return -EINVAL;
3688
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003689 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003690 if (IS_ERR(skp))
3691 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003692
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003693 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303694 * No process is ever allowed the web ("@") label
3695 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003696 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303697 if (skp == &smack_known_web || skp == &smack_known_star)
3698 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003699
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003700 if (!smack_privileged(CAP_MAC_ADMIN)) {
3701 rc = -EPERM;
3702 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3703 if (sklep->smk_label == skp) {
3704 rc = 0;
3705 break;
3706 }
3707 if (rc)
3708 return rc;
3709 }
3710
David Howellsd84f4f92008-11-14 10:39:23 +11003711 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003712 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003713 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003714
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003715 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003716 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003717 /*
3718 * process can change its label only once
3719 */
3720 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003721
David Howellsd84f4f92008-11-14 10:39:23 +11003722 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003723 return size;
3724}
3725
3726/**
3727 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003728 * @sock: one sock
3729 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003730 * @newsk: unused
3731 *
3732 * Return 0 if a subject with the smack of sock could access
3733 * an object with the smack of other, otherwise an error code
3734 */
David S. Miller3610cda2011-01-05 15:38:53 -08003735static int smack_unix_stream_connect(struct sock *sock,
3736 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003737{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003738 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003739 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003740 struct socket_smack *ssp = sock->sk_security;
3741 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003742 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003743 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003744 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003745#ifdef CONFIG_AUDIT
3746 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003747#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003748
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003749 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3750 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003751 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003752#ifdef CONFIG_AUDIT
3753 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3754 smk_ad_setfield_u_net_sk(&ad, other);
3755#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003756 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3757 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003758 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003759 okp = osp->smk_out;
3760 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003761 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003762 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003763 MAY_WRITE, rc);
3764 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003765 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003766
Casey Schaufler975d5e52011-09-26 14:43:39 -07003767 /*
3768 * Cross reference the peer labels for SO_PEERSEC.
3769 */
3770 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003771 nsp->smk_packet = ssp->smk_out;
3772 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003773 }
3774
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003775 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003776}
3777
3778/**
3779 * smack_unix_may_send - Smack access on UDS
3780 * @sock: one socket
3781 * @other: the other socket
3782 *
3783 * Return 0 if a subject with the smack of sock could access
3784 * an object with the smack of other, otherwise an error code
3785 */
3786static int smack_unix_may_send(struct socket *sock, struct socket *other)
3787{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003788 struct socket_smack *ssp = sock->sk->sk_security;
3789 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003790 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003791 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003792
Kees Cook923e9a12012-04-10 13:26:44 -07003793#ifdef CONFIG_AUDIT
3794 struct lsm_network_audit net;
3795
Eric Paris48c62af2012-04-02 13:15:44 -04003796 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003797 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003798#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003799
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003800 if (smack_privileged(CAP_MAC_OVERRIDE))
3801 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003802
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003803 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3804 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003805 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003806}
3807
3808/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003809 * smack_socket_sendmsg - Smack check based on destination host
3810 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003811 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003812 * @size: the size of the message
3813 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003814 * Return 0 if the current subject can write to the destination host.
3815 * For IPv4 this is only a question if the destination is a single label host.
3816 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003817 */
3818static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3819 int size)
3820{
3821 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003822#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003823 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003824#endif
3825#ifdef SMACK_IPV6_SECMARK_LABELING
3826 struct socket_smack *ssp = sock->sk->sk_security;
3827 struct smack_known *rsp;
3828#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003829 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003830
3831 /*
3832 * Perfectly reasonable for this to be NULL
3833 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003834 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003835 return 0;
3836
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003837 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003838 case AF_INET:
3839 rc = smack_netlabel_send(sock->sk, sip);
3840 break;
3841 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003842#ifdef SMACK_IPV6_SECMARK_LABELING
3843 rsp = smack_ipv6host_label(sap);
3844 if (rsp != NULL)
3845 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3846 SMK_CONNECTING);
3847#endif
3848#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003849 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003850#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003851 break;
3852 }
3853 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003854}
3855
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003856/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003857 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003858 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003859 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003860 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003861 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003862 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003863static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3864 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003865{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003866 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003867 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003868 int acat;
3869 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003870
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003871 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003872 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003873 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003874 * If there are flags but no level netlabel isn't
3875 * behaving the way we expect it to.
3876 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003877 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003878 * Without guidance regarding the smack value
3879 * for the packet fall back on the network
3880 * ambient value.
3881 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003882 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003883 list_for_each_entry(skp, &smack_known_list, list) {
3884 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003885 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003886 /*
3887 * Compare the catsets. Use the netlbl APIs.
3888 */
3889 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3890 if ((skp->smk_netlabel.flags &
3891 NETLBL_SECATTR_MLS_CAT) == 0)
3892 found = 1;
3893 break;
3894 }
3895 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003896 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3897 acat + 1);
3898 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003899 skp->smk_netlabel.attr.mls.cat,
3900 kcat + 1);
3901 if (acat < 0 || kcat < 0)
3902 break;
3903 }
3904 if (acat == kcat) {
3905 found = 1;
3906 break;
3907 }
Casey Schauflere114e472008-02-04 22:29:50 -08003908 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003909 rcu_read_unlock();
3910
3911 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003912 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003913
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003914 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003915 return &smack_known_web;
3916 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003917 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003918 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003919 /*
3920 * Looks like a fallback, which gives us a secid.
3921 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003922 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003923 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003924 * Without guidance regarding the smack value
3925 * for the packet fall back on the network
3926 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003927 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003928 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003929}
3930
Casey Schaufler69f287a2014-12-12 17:08:40 -08003931#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003932static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003933{
Casey Schauflerc6739442013-05-22 18:42:56 -07003934 u8 nexthdr;
3935 int offset;
3936 int proto = -EINVAL;
3937 struct ipv6hdr _ipv6h;
3938 struct ipv6hdr *ip6;
3939 __be16 frag_off;
3940 struct tcphdr _tcph, *th;
3941 struct udphdr _udph, *uh;
3942 struct dccp_hdr _dccph, *dh;
3943
3944 sip->sin6_port = 0;
3945
3946 offset = skb_network_offset(skb);
3947 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3948 if (ip6 == NULL)
3949 return -EINVAL;
3950 sip->sin6_addr = ip6->saddr;
3951
3952 nexthdr = ip6->nexthdr;
3953 offset += sizeof(_ipv6h);
3954 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3955 if (offset < 0)
3956 return -EINVAL;
3957
3958 proto = nexthdr;
3959 switch (proto) {
3960 case IPPROTO_TCP:
3961 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3962 if (th != NULL)
3963 sip->sin6_port = th->source;
3964 break;
3965 case IPPROTO_UDP:
3966 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3967 if (uh != NULL)
3968 sip->sin6_port = uh->source;
3969 break;
3970 case IPPROTO_DCCP:
3971 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3972 if (dh != NULL)
3973 sip->sin6_port = dh->dccph_sport;
3974 break;
3975 }
3976 return proto;
3977}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003978#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003979
Casey Schauflere114e472008-02-04 22:29:50 -08003980/**
3981 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3982 * @sk: socket
3983 * @skb: packet
3984 *
3985 * Returns 0 if the packet should be delivered, an error code otherwise
3986 */
3987static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3988{
3989 struct netlbl_lsm_secattr secattr;
3990 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003991 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003992 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003993 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003994#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003995 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003996#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003997#if IS_ENABLED(CONFIG_IPV6)
3998 struct sockaddr_in6 sadd;
3999 int proto;
4000#endif /* CONFIG_IPV6 */
4001
Casey Schauflerc6739442013-05-22 18:42:56 -07004002 switch (sk->sk_family) {
4003 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08004004#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4005 /*
4006 * If there is a secmark use it rather than the CIPSO label.
4007 * If there is no secmark fall back to CIPSO.
4008 * The secmark is assumed to reflect policy better.
4009 */
4010 if (skb && skb->secmark != 0) {
4011 skp = smack_from_secid(skb->secmark);
4012 goto access_check;
4013 }
4014#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07004015 /*
4016 * Translate what netlabel gave us.
4017 */
4018 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004019
Casey Schauflerc6739442013-05-22 18:42:56 -07004020 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
4021 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004022 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07004023 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004024 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004025
Casey Schauflerc6739442013-05-22 18:42:56 -07004026 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004027
Casey Schaufler69f287a2014-12-12 17:08:40 -08004028#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4029access_check:
4030#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02004031#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07004032 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4033 ad.a.u.net->family = sk->sk_family;
4034 ad.a.u.net->netif = skb->skb_iif;
4035 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02004036#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07004037 /*
4038 * Receiving a packet requires that the other end
4039 * be able to write here. Read access is not required.
4040 * This is the simplist possible security model
4041 * for networking.
4042 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004043 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4044 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07004045 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07004046 if (rc != 0)
Huw Daviesa04e71f2016-06-27 15:06:16 -04004047 netlbl_skbuff_err(skb, sk->sk_family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07004048 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004049#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004050 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08004051 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4052 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
4053 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004054#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004055 if (skb && skb->secmark != 0)
4056 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07004057 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004058 skp = smack_ipv6host_label(&sadd);
4059 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08004060 skp = smack_net_ambient;
4061#ifdef CONFIG_AUDIT
4062 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4063 ad.a.u.net->family = sk->sk_family;
4064 ad.a.u.net->netif = skb->skb_iif;
4065 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4066#endif /* CONFIG_AUDIT */
4067 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4068 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4069 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004070#endif /* SMACK_IPV6_SECMARK_LABELING */
4071#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004072 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004073#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07004074 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004075#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07004076 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004077
Paul Moorea8134292008-10-10 10:16:31 -04004078 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004079}
4080
4081/**
4082 * smack_socket_getpeersec_stream - pull in packet label
4083 * @sock: the socket
4084 * @optval: user's destination
4085 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004086 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004087 *
4088 * returns zero on success, an error code otherwise
4089 */
4090static int smack_socket_getpeersec_stream(struct socket *sock,
4091 char __user *optval,
4092 int __user *optlen, unsigned len)
4093{
4094 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004095 char *rcp = "";
4096 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004097 int rc = 0;
4098
4099 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004100 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004101 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004102 slen = strlen(rcp) + 1;
4103 }
Casey Schauflere114e472008-02-04 22:29:50 -08004104
4105 if (slen > len)
4106 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004107 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004108 rc = -EFAULT;
4109
4110 if (put_user(slen, optlen) != 0)
4111 rc = -EFAULT;
4112
4113 return rc;
4114}
4115
4116
4117/**
4118 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004119 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004120 * @skb: packet data
4121 * @secid: pointer to where to put the secid of the packet
4122 *
4123 * Sets the netlabel socket state on sk from parent
4124 */
4125static int smack_socket_getpeersec_dgram(struct socket *sock,
4126 struct sk_buff *skb, u32 *secid)
4127
4128{
4129 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004130 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004131 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004132 int family = PF_UNSPEC;
4133 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004134 int rc;
4135
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004136 if (skb != NULL) {
4137 if (skb->protocol == htons(ETH_P_IP))
4138 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004139#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004140 else if (skb->protocol == htons(ETH_P_IPV6))
4141 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004142#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004143 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004144 if (family == PF_UNSPEC && sock != NULL)
4145 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004146
Casey Schaufler69f287a2014-12-12 17:08:40 -08004147 switch (family) {
4148 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004149 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004150 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004151 break;
4152 case PF_INET:
4153#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4154 s = skb->secmark;
4155 if (s != 0)
4156 break;
4157#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004158 /*
4159 * Translate what netlabel gave us.
4160 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004161 if (sock != NULL && sock->sk != NULL)
4162 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004163 netlbl_secattr_init(&secattr);
4164 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4165 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004166 skp = smack_from_secattr(&secattr, ssp);
4167 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004168 }
4169 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004170 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004171 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004172#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004173 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004174#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004175 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004176 }
4177 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004178 if (s == 0)
4179 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004180 return 0;
4181}
4182
4183/**
Paul Moore07feee82009-03-27 17:10:54 -04004184 * smack_sock_graft - Initialize a newly created socket with an existing sock
4185 * @sk: child sock
4186 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004187 *
Paul Moore07feee82009-03-27 17:10:54 -04004188 * Set the smk_{in,out} state of an existing sock based on the process that
4189 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004190 */
4191static void smack_sock_graft(struct sock *sk, struct socket *parent)
4192{
4193 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004194 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004195
Paul Moore07feee82009-03-27 17:10:54 -04004196 if (sk == NULL ||
4197 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004198 return;
4199
4200 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004201 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004202 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004203 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004204}
4205
4206/**
4207 * smack_inet_conn_request - Smack access check on connect
4208 * @sk: socket involved
4209 * @skb: packet
4210 * @req: unused
4211 *
4212 * Returns 0 if a task with the packet label could write to
4213 * the socket, otherwise an error code
4214 */
4215static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4216 struct request_sock *req)
4217{
Paul Moore07feee82009-03-27 17:10:54 -04004218 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004219 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004220 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004221 struct netlbl_lsm_secattr secattr;
4222 struct sockaddr_in addr;
4223 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004224 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004225 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004226 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004227#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004228 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004229#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004230
Casey Schaufler69f287a2014-12-12 17:08:40 -08004231#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004232 if (family == PF_INET6) {
4233 /*
4234 * Handle mapped IPv4 packets arriving
4235 * via IPv6 sockets. Don't set up netlabel
4236 * processing on IPv6.
4237 */
4238 if (skb->protocol == htons(ETH_P_IP))
4239 family = PF_INET;
4240 else
4241 return 0;
4242 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004243#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004244
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004245#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4246 /*
4247 * If there is a secmark use it rather than the CIPSO label.
4248 * If there is no secmark fall back to CIPSO.
4249 * The secmark is assumed to reflect policy better.
4250 */
4251 if (skb && skb->secmark != 0) {
4252 skp = smack_from_secid(skb->secmark);
4253 goto access_check;
4254 }
4255#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4256
Paul Moore07feee82009-03-27 17:10:54 -04004257 netlbl_secattr_init(&secattr);
4258 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004259 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004260 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004261 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004262 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004263 netlbl_secattr_destroy(&secattr);
4264
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004265#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4266access_check:
4267#endif
4268
Etienne Bassetecfcc532009-04-08 20:40:06 +02004269#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004270 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4271 ad.a.u.net->family = family;
4272 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004273 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4274#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004275 /*
Paul Moore07feee82009-03-27 17:10:54 -04004276 * Receiving a packet requires that the other end be able to write
4277 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004278 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004279 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4280 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004281 if (rc != 0)
4282 return rc;
4283
4284 /*
4285 * Save the peer's label in the request_sock so we can later setup
4286 * smk_packet in the child socket so that SO_PEERCRED can report it.
4287 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004288 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004289
4290 /*
4291 * We need to decide if we want to label the incoming connection here
4292 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004293 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004294 */
4295 hdr = ip_hdr(skb);
4296 addr.sin_addr.s_addr = hdr->saddr;
4297 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004298 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004299 rcu_read_unlock();
4300
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004301 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004302 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004303 else
Paul Moore07feee82009-03-27 17:10:54 -04004304 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004305
4306 return rc;
4307}
4308
Paul Moore07feee82009-03-27 17:10:54 -04004309/**
4310 * smack_inet_csk_clone - Copy the connection information to the new socket
4311 * @sk: the new socket
4312 * @req: the connection's request_sock
4313 *
4314 * Transfer the connection's peer label to the newly created socket.
4315 */
4316static void smack_inet_csk_clone(struct sock *sk,
4317 const struct request_sock *req)
4318{
4319 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004320 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004321
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004322 if (req->peer_secid != 0) {
4323 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004324 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004325 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004326 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004327}
4328
Casey Schauflere114e472008-02-04 22:29:50 -08004329/*
4330 * Key management security hooks
4331 *
4332 * Casey has not tested key support very heavily.
4333 * The permission check is most likely too restrictive.
4334 * If you care about keys please have a look.
4335 */
4336#ifdef CONFIG_KEYS
4337
4338/**
4339 * smack_key_alloc - Set the key security blob
4340 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004341 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004342 * @flags: unused
4343 *
4344 * No allocation required
4345 *
4346 * Returns 0
4347 */
David Howellsd84f4f92008-11-14 10:39:23 +11004348static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004349 unsigned long flags)
4350{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004351 struct smack_known *skp = smk_of_task(cred->security);
4352
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004353 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004354 return 0;
4355}
4356
4357/**
4358 * smack_key_free - Clear the key security blob
4359 * @key: the object
4360 *
4361 * Clear the blob pointer
4362 */
4363static void smack_key_free(struct key *key)
4364{
4365 key->security = NULL;
4366}
4367
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004368/**
Casey Schauflere114e472008-02-04 22:29:50 -08004369 * smack_key_permission - Smack access on a key
4370 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004371 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004372 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004373 *
4374 * Return 0 if the task has read and write to the object,
4375 * an error code otherwise
4376 */
4377static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004378 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004379{
4380 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004381 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004382 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004383 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004384 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004385
4386 keyp = key_ref_to_ptr(key_ref);
4387 if (keyp == NULL)
4388 return -EINVAL;
4389 /*
4390 * If the key hasn't been initialized give it access so that
4391 * it may do so.
4392 */
4393 if (keyp->security == NULL)
4394 return 0;
4395 /*
4396 * This should not occur
4397 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004398 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004399 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004400#ifdef CONFIG_AUDIT
4401 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4402 ad.a.u.key_struct.key = keyp->serial;
4403 ad.a.u.key_struct.key_desc = keyp->description;
4404#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004405 if (perm & KEY_NEED_READ)
4406 request = MAY_READ;
4407 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4408 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004409 rc = smk_access(tkp, keyp->security, request, &ad);
4410 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4411 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004412}
José Bollo7fc5f362015-02-17 15:41:22 +01004413
4414/*
4415 * smack_key_getsecurity - Smack label tagging the key
4416 * @key points to the key to be queried
4417 * @_buffer points to a pointer that should be set to point to the
4418 * resulting string (if no label or an error occurs).
4419 * Return the length of the string (including terminating NUL) or -ve if
4420 * an error.
4421 * May also return 0 (and a NULL buffer pointer) if there is no label.
4422 */
4423static int smack_key_getsecurity(struct key *key, char **_buffer)
4424{
4425 struct smack_known *skp = key->security;
4426 size_t length;
4427 char *copy;
4428
4429 if (key->security == NULL) {
4430 *_buffer = NULL;
4431 return 0;
4432 }
4433
4434 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4435 if (copy == NULL)
4436 return -ENOMEM;
4437 length = strlen(copy) + 1;
4438
4439 *_buffer = copy;
4440 return length;
4441}
4442
Casey Schauflere114e472008-02-04 22:29:50 -08004443#endif /* CONFIG_KEYS */
4444
4445/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004446 * Smack Audit hooks
4447 *
4448 * Audit requires a unique representation of each Smack specific
4449 * rule. This unique representation is used to distinguish the
4450 * object to be audited from remaining kernel objects and also
4451 * works as a glue between the audit hooks.
4452 *
4453 * Since repository entries are added but never deleted, we'll use
4454 * the smack_known label address related to the given audit rule as
4455 * the needed unique representation. This also better fits the smack
4456 * model where nearly everything is a label.
4457 */
4458#ifdef CONFIG_AUDIT
4459
4460/**
4461 * smack_audit_rule_init - Initialize a smack audit rule
4462 * @field: audit rule fields given from user-space (audit.h)
4463 * @op: required testing operator (=, !=, >, <, ...)
4464 * @rulestr: smack label to be audited
4465 * @vrule: pointer to save our own audit rule representation
4466 *
4467 * Prepare to audit cases where (@field @op @rulestr) is true.
4468 * The label to be audited is created if necessay.
4469 */
4470static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4471{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004472 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004473 char **rule = (char **)vrule;
4474 *rule = NULL;
4475
4476 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4477 return -EINVAL;
4478
Al Viro5af75d82008-12-16 05:59:26 -05004479 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004480 return -EINVAL;
4481
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004482 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004483 if (IS_ERR(skp))
4484 return PTR_ERR(skp);
4485
4486 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004487
4488 return 0;
4489}
4490
4491/**
4492 * smack_audit_rule_known - Distinguish Smack audit rules
4493 * @krule: rule of interest, in Audit kernel representation format
4494 *
4495 * This is used to filter Smack rules from remaining Audit ones.
4496 * If it's proved that this rule belongs to us, the
4497 * audit_rule_match hook will be called to do the final judgement.
4498 */
4499static int smack_audit_rule_known(struct audit_krule *krule)
4500{
4501 struct audit_field *f;
4502 int i;
4503
4504 for (i = 0; i < krule->field_count; i++) {
4505 f = &krule->fields[i];
4506
4507 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4508 return 1;
4509 }
4510
4511 return 0;
4512}
4513
4514/**
4515 * smack_audit_rule_match - Audit given object ?
4516 * @secid: security id for identifying the object to test
4517 * @field: audit rule flags given from user-space
4518 * @op: required testing operator
4519 * @vrule: smack internal rule presentation
4520 * @actx: audit context associated with the check
4521 *
4522 * The core Audit hook. It's used to take the decision of
4523 * whether to audit or not to audit a given object.
4524 */
4525static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4526 struct audit_context *actx)
4527{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004528 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004529 char *rule = vrule;
4530
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004531 if (unlikely(!rule)) {
4532 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004533 return -ENOENT;
4534 }
4535
4536 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4537 return 0;
4538
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004539 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004540
4541 /*
4542 * No need to do string comparisons. If a match occurs,
4543 * both pointers will point to the same smack_known
4544 * label.
4545 */
Al Viro5af75d82008-12-16 05:59:26 -05004546 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004547 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004548 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004549 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004550
4551 return 0;
4552}
4553
Casey Schaufler491a0b02016-01-26 15:08:35 -08004554/*
4555 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004556 * No memory was allocated.
4557 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004558
4559#endif /* CONFIG_AUDIT */
4560
Randy Dunlap251a2a92009-02-18 11:42:33 -08004561/**
David Quigley746df9b2013-05-22 12:50:35 -04004562 * smack_ismaclabel - check if xattr @name references a smack MAC label
4563 * @name: Full xattr name to check.
4564 */
4565static int smack_ismaclabel(const char *name)
4566{
4567 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4568}
4569
4570
4571/**
Casey Schauflere114e472008-02-04 22:29:50 -08004572 * smack_secid_to_secctx - return the smack label for a secid
4573 * @secid: incoming integer
4574 * @secdata: destination
4575 * @seclen: how long it is
4576 *
4577 * Exists for networking code.
4578 */
4579static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4580{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004581 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004582
Eric Parisd5630b92010-10-13 16:24:48 -04004583 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004584 *secdata = skp->smk_known;
4585 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004586 return 0;
4587}
4588
Randy Dunlap251a2a92009-02-18 11:42:33 -08004589/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004590 * smack_secctx_to_secid - return the secid for a smack label
4591 * @secdata: smack label
4592 * @seclen: how long result is
4593 * @secid: outgoing integer
4594 *
4595 * Exists for audit and networking code.
4596 */
David Howellse52c17642008-04-29 20:52:51 +01004597static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004598{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004599 struct smack_known *skp = smk_find_entry(secdata);
4600
4601 if (skp)
4602 *secid = skp->smk_secid;
4603 else
4604 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004605 return 0;
4606}
4607
Casey Schaufler491a0b02016-01-26 15:08:35 -08004608/*
4609 * There used to be a smack_release_secctx hook
4610 * that did nothing back when hooks were in a vector.
4611 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004612 */
Casey Schauflere114e472008-02-04 22:29:50 -08004613
David P. Quigley1ee65e32009-09-03 14:25:57 -04004614static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4615{
4616 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4617}
4618
4619static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4620{
4621 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4622}
4623
4624static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4625{
4626 int len = 0;
4627 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4628
4629 if (len < 0)
4630 return len;
4631 *ctxlen = len;
4632 return 0;
4633}
4634
Casey Schaufler1eddfe82015-07-30 14:35:14 -07004635static struct security_hook_list smack_hooks[] = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004636 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4637 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4638 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004639
Casey Schauflere20b0432015-05-02 15:11:36 -07004640 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4641 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4642 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4643 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4644 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304645 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4646 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
Casey Schauflere114e472008-02-04 22:29:50 -08004647
Casey Schauflere20b0432015-05-02 15:11:36 -07004648 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4649 LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
4650 LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004651
Casey Schauflere20b0432015-05-02 15:11:36 -07004652 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4653 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4654 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4655 LSM_HOOK_INIT(inode_link, smack_inode_link),
4656 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4657 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4658 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4659 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4660 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4661 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4662 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4663 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4664 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4665 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4666 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4667 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4668 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4669 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004670
Casey Schauflere20b0432015-05-02 15:11:36 -07004671 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4672 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4673 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4674 LSM_HOOK_INIT(file_lock, smack_file_lock),
4675 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4676 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4677 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4678 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4679 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4680 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004681
Casey Schauflere20b0432015-05-02 15:11:36 -07004682 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004683
Casey Schauflere20b0432015-05-02 15:11:36 -07004684 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4685 LSM_HOOK_INIT(cred_free, smack_cred_free),
4686 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4687 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4688 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4689 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4690 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4691 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4692 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4693 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4694 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4695 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4696 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4697 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4698 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4699 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4700 LSM_HOOK_INIT(task_kill, smack_task_kill),
4701 LSM_HOOK_INIT(task_wait, smack_task_wait),
4702 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004703
Casey Schauflere20b0432015-05-02 15:11:36 -07004704 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4705 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004706
Casey Schauflere20b0432015-05-02 15:11:36 -07004707 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4708 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004709
Casey Schauflere20b0432015-05-02 15:11:36 -07004710 LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
4711 LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
4712 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4713 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4714 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4715 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004716
Casey Schauflere20b0432015-05-02 15:11:36 -07004717 LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
4718 LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
4719 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4720 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4721 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004722
Casey Schauflere20b0432015-05-02 15:11:36 -07004723 LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
4724 LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
4725 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4726 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4727 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004728
Casey Schauflere20b0432015-05-02 15:11:36 -07004729 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004730
Casey Schauflere20b0432015-05-02 15:11:36 -07004731 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4732 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004733
Casey Schauflere20b0432015-05-02 15:11:36 -07004734 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4735 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004736
Casey Schauflere20b0432015-05-02 15:11:36 -07004737 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004738#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004739 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004740#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004741 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4742 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4743 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4744 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4745 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4746 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4747 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4748 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4749 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4750 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004751
Casey Schauflere114e472008-02-04 22:29:50 -08004752 /* key management security hooks */
4753#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004754 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4755 LSM_HOOK_INIT(key_free, smack_key_free),
4756 LSM_HOOK_INIT(key_permission, smack_key_permission),
4757 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004758#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004759
4760 /* Audit hooks */
4761#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004762 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4763 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4764 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004765#endif /* CONFIG_AUDIT */
4766
Casey Schauflere20b0432015-05-02 15:11:36 -07004767 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4768 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4769 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004770 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4771 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4772 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflere114e472008-02-04 22:29:50 -08004773};
4774
Etienne Basset7198e2e2009-03-24 20:53:24 +01004775
Casey Schaufler86812bb2012-04-17 18:55:46 -07004776static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004777{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004778 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004779 * Initialize rule list locks
4780 */
4781 mutex_init(&smack_known_huh.smk_rules_lock);
4782 mutex_init(&smack_known_hat.smk_rules_lock);
4783 mutex_init(&smack_known_floor.smk_rules_lock);
4784 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004785 mutex_init(&smack_known_web.smk_rules_lock);
4786 /*
4787 * Initialize rule lists
4788 */
4789 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4790 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4791 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4792 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004793 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4794 /*
4795 * Create the known labels list
4796 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004797 smk_insert_entry(&smack_known_huh);
4798 smk_insert_entry(&smack_known_hat);
4799 smk_insert_entry(&smack_known_star);
4800 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004801 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004802}
4803
Casey Schauflere114e472008-02-04 22:29:50 -08004804/**
4805 * smack_init - initialize the smack system
4806 *
4807 * Returns 0
4808 */
4809static __init int smack_init(void)
4810{
David Howellsd84f4f92008-11-14 10:39:23 +11004811 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004812 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004813
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004814 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004815 return 0;
4816
Rohit1a5b4722014-10-15 17:40:41 +05304817 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4818 if (!smack_inode_cache)
4819 return -ENOMEM;
4820
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004821 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4822 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304823 if (tsp == NULL) {
4824 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004825 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304826 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004827
José Bollod21b7b02015-10-02 15:15:56 +02004828 smack_enabled = 1;
4829
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004830 pr_info("Smack: Initializing.\n");
4831#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4832 pr_info("Smack: Netfilter enabled.\n");
4833#endif
4834#ifdef SMACK_IPV6_PORT_LABELING
4835 pr_info("Smack: IPv6 port labeling enabled.\n");
4836#endif
4837#ifdef SMACK_IPV6_SECMARK_LABELING
4838 pr_info("Smack: IPv6 Netfilter enabled.\n");
4839#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004840
4841 /*
4842 * Set the security state for the initial task.
4843 */
David Howellsd84f4f92008-11-14 10:39:23 +11004844 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004845 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004846
Casey Schaufler86812bb2012-04-17 18:55:46 -07004847 /* initialize the smack_known_list */
4848 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004849
4850 /*
4851 * Register with LSM
4852 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004853 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
Casey Schauflere114e472008-02-04 22:29:50 -08004854
4855 return 0;
4856}
4857
4858/*
4859 * Smack requires early initialization in order to label
4860 * all processes and objects when they are created.
4861 */
4862security_initcall(smack_init);