blob: 025de76af1dbf87aa17db67e12a2262e0c4961bb [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>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020031#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080033#include <linux/mutex.h>
34#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080035#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070036#include <net/ip.h>
37#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100038#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070039#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050040#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030041#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000042#include <linux/msg.h>
43#include <linux/shm.h>
44#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053045#include <linux/parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080046#include "smack.h"
47
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020048#define TRANS_TRUE "TRUE"
49#define TRANS_TRUE_SIZE 4
50
Casey Schauflerc6739442013-05-22 18:42:56 -070051#define SMK_CONNECTING 0
52#define SMK_RECEIVING 1
53#define SMK_SENDING 2
54
Casey Schaufler21abb1e2015-07-22 14:25:31 -070055#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053056DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080057static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070058#endif
Rohit1a5b4722014-10-15 17:40:41 +053059static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080060int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070061
Casey Schaufler3d04c922015-08-12 11:56:02 -070062static const match_table_t smk_mount_tokens = {
Vivek Trivedi3bf27892015-06-22 15:36:06 +053063 {Opt_fsdefault, SMK_FSDEFAULT "%s"},
64 {Opt_fsfloor, SMK_FSFLOOR "%s"},
65 {Opt_fshat, SMK_FSHAT "%s"},
66 {Opt_fsroot, SMK_FSROOT "%s"},
67 {Opt_fstransmute, SMK_FSTRANS "%s"},
68 {Opt_error, NULL},
69};
70
Casey Schaufler3d04c922015-08-12 11:56:02 -070071#ifdef CONFIG_SECURITY_SMACK_BRINGUP
72static char *smk_bu_mess[] = {
73 "Bringup Error", /* Unused */
74 "Bringup", /* SMACK_BRINGUP_ALLOW */
75 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
76 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
77};
78
Casey Schauflerd166c802014-08-27 14:51:27 -070079static void smk_bu_mode(int mode, char *s)
80{
81 int i = 0;
82
83 if (mode & MAY_READ)
84 s[i++] = 'r';
85 if (mode & MAY_WRITE)
86 s[i++] = 'w';
87 if (mode & MAY_EXEC)
88 s[i++] = 'x';
89 if (mode & MAY_APPEND)
90 s[i++] = 'a';
91 if (mode & MAY_TRANSMUTE)
92 s[i++] = 't';
93 if (mode & MAY_LOCK)
94 s[i++] = 'l';
95 if (i == 0)
96 s[i++] = '-';
97 s[i] = '\0';
98}
99#endif
100
101#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200102static int smk_bu_note(char *note, struct smack_known *sskp,
103 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700104{
105 char acc[SMK_NUM_ACCESS_TYPE + 1];
106
107 if (rc <= 0)
108 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700109 if (rc > SMACK_UNCONFINED_OBJECT)
110 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700111
112 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700113 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200114 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700115 return 0;
116}
117#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200118#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700119#endif
120
121#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200122static int smk_bu_current(char *note, struct smack_known *oskp,
123 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700124{
125 struct task_smack *tsp = current_security();
126 char acc[SMK_NUM_ACCESS_TYPE + 1];
127
128 if (rc <= 0)
129 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700130 if (rc > SMACK_UNCONFINED_OBJECT)
131 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700132
133 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700134 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200135 tsp->smk_task->smk_known, oskp->smk_known,
136 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700137 return 0;
138}
139#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200140#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700141#endif
142
143#ifdef CONFIG_SECURITY_SMACK_BRINGUP
144static int smk_bu_task(struct task_struct *otp, int mode, int rc)
145{
146 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300147 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700148 char acc[SMK_NUM_ACCESS_TYPE + 1];
149
150 if (rc <= 0)
151 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700152 if (rc > SMACK_UNCONFINED_OBJECT)
153 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700154
155 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700156 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300157 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700158 current->comm, otp->comm);
159 return 0;
160}
161#else
162#define smk_bu_task(otp, mode, RC) (RC)
163#endif
164
165#ifdef CONFIG_SECURITY_SMACK_BRINGUP
166static int smk_bu_inode(struct inode *inode, int mode, int rc)
167{
168 struct task_smack *tsp = current_security();
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700169 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700170 char acc[SMK_NUM_ACCESS_TYPE + 1];
171
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700172 if (isp->smk_flags & SMK_INODE_IMPURE)
173 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
174 inode->i_sb->s_id, inode->i_ino, current->comm);
175
Casey Schauflerd166c802014-08-27 14:51:27 -0700176 if (rc <= 0)
177 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700178 if (rc > SMACK_UNCONFINED_OBJECT)
179 rc = 0;
180 if (rc == SMACK_UNCONFINED_SUBJECT &&
181 (mode & (MAY_WRITE | MAY_APPEND)))
182 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700183
184 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700185
186 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
187 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700188 inode->i_sb->s_id, inode->i_ino, current->comm);
189 return 0;
190}
191#else
192#define smk_bu_inode(inode, mode, RC) (RC)
193#endif
194
195#ifdef CONFIG_SECURITY_SMACK_BRINGUP
196static int smk_bu_file(struct file *file, int mode, int rc)
197{
198 struct task_smack *tsp = current_security();
199 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800200 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700201 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700202 char acc[SMK_NUM_ACCESS_TYPE + 1];
203
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700204 if (isp->smk_flags & SMK_INODE_IMPURE)
205 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
206 inode->i_sb->s_id, inode->i_ino, current->comm);
207
Casey Schauflerd166c802014-08-27 14:51:27 -0700208 if (rc <= 0)
209 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700210 if (rc > SMACK_UNCONFINED_OBJECT)
211 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700212
213 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700214 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800215 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400216 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700217 current->comm);
218 return 0;
219}
220#else
221#define smk_bu_file(file, mode, RC) (RC)
222#endif
223
224#ifdef CONFIG_SECURITY_SMACK_BRINGUP
225static int smk_bu_credfile(const struct cred *cred, struct file *file,
226 int mode, int rc)
227{
228 struct task_smack *tsp = cred->security;
229 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500230 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700231 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700232 char acc[SMK_NUM_ACCESS_TYPE + 1];
233
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700234 if (isp->smk_flags & SMK_INODE_IMPURE)
235 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
236 inode->i_sb->s_id, inode->i_ino, current->comm);
237
Casey Schauflerd166c802014-08-27 14:51:27 -0700238 if (rc <= 0)
239 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700240 if (rc > SMACK_UNCONFINED_OBJECT)
241 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700242
243 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700244 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200245 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400246 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700247 current->comm);
248 return 0;
249}
250#else
251#define smk_bu_credfile(cred, file, mode, RC) (RC)
252#endif
253
Casey Schauflere114e472008-02-04 22:29:50 -0800254/**
255 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100256 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800257 * @ip: a pointer to the inode
258 * @dp: a pointer to the dentry
259 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200260 * Returns a pointer to the master list entry for the Smack label,
261 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800262 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700263static struct smack_known *smk_fetch(const char *name, struct inode *ip,
264 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800265{
266 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700267 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700268 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800269
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200270 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200271 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800272
Casey Schauflerf7112e62012-05-06 15:22:02 -0700273 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
274 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200275 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800276
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200277 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200278 if (rc < 0)
279 skp = ERR_PTR(rc);
280 else if (rc == 0)
281 skp = NULL;
282 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700283 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700284
285 kfree(buffer);
286
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700287 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800288}
289
290/**
291 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200292 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800293 *
294 * Returns the new blob or NULL if there's no memory available
295 */
Casey Schaufler1eddfe82015-07-30 14:35:14 -0700296static struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800297{
298 struct inode_smack *isp;
299
Rohit1a5b4722014-10-15 17:40:41 +0530300 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800301 if (isp == NULL)
302 return NULL;
303
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200304 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800305 isp->smk_flags = 0;
306 mutex_init(&isp->smk_lock);
307
308 return isp;
309}
310
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800311/**
312 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100313 * @task: a pointer to the Smack label for the running task
314 * @forked: a pointer to the Smack label for the forked task
315 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800316 *
317 * Returns the new blob or NULL if there's no memory available
318 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700319static struct task_smack *new_task_smack(struct smack_known *task,
320 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800321{
322 struct task_smack *tsp;
323
324 tsp = kzalloc(sizeof(struct task_smack), gfp);
325 if (tsp == NULL)
326 return NULL;
327
328 tsp->smk_task = task;
329 tsp->smk_forked = forked;
330 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200331 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800332 mutex_init(&tsp->smk_rules_lock);
333
334 return tsp;
335}
336
337/**
338 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100339 * @nhead: new rules header pointer
340 * @ohead: old rules header pointer
341 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800342 *
343 * Returns 0 on success, -ENOMEM on error
344 */
345static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
346 gfp_t gfp)
347{
348 struct smack_rule *nrp;
349 struct smack_rule *orp;
350 int rc = 0;
351
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800352 list_for_each_entry_rcu(orp, ohead, list) {
353 nrp = kzalloc(sizeof(struct smack_rule), gfp);
354 if (nrp == NULL) {
355 rc = -ENOMEM;
356 break;
357 }
358 *nrp = *orp;
359 list_add_rcu(&nrp->list, nhead);
360 }
361 return rc;
362}
363
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100364/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200365 * smk_copy_relabel - copy smk_relabel labels list
366 * @nhead: new rules header pointer
367 * @ohead: old rules header pointer
368 * @gfp: type of the memory for the allocation
369 *
370 * Returns 0 on success, -ENOMEM on error
371 */
372static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
373 gfp_t gfp)
374{
375 struct smack_known_list_elem *nklep;
376 struct smack_known_list_elem *oklep;
377
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200378 list_for_each_entry(oklep, ohead, list) {
379 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
380 if (nklep == NULL) {
381 smk_destroy_label_list(nhead);
382 return -ENOMEM;
383 }
384 nklep->smk_label = oklep->smk_label;
385 list_add(&nklep->list, nhead);
386 }
387
388 return 0;
389}
390
391/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100392 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
393 * @mode - input mode in form of PTRACE_MODE_*
394 *
395 * Returns a converted MAY_* mode usable by smack rules
396 */
397static inline unsigned int smk_ptrace_mode(unsigned int mode)
398{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800399 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100400 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800401 if (mode & PTRACE_MODE_READ)
402 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100403
404 return 0;
405}
406
407/**
408 * smk_ptrace_rule_check - helper for ptrace access
409 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200410 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100411 * @mode: ptrace attachment mode (PTRACE_MODE_*)
412 * @func: name of the function that called us, used for audit
413 *
414 * Returns 0 on access granted, -error on error
415 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200416static int smk_ptrace_rule_check(struct task_struct *tracer,
417 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100418 unsigned int mode, const char *func)
419{
420 int rc;
421 struct smk_audit_info ad, *saip = NULL;
422 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200423 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100424
425 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
426 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
427 smk_ad_setfield_u_tsk(&ad, tracer);
428 saip = &ad;
429 }
430
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300431 rcu_read_lock();
432 tsp = __task_cred(tracer)->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200433 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100434
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100435 if ((mode & PTRACE_MODE_ATTACH) &&
436 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
437 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200438 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100439 rc = 0;
440 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
441 rc = -EACCES;
442 else if (capable(CAP_SYS_PTRACE))
443 rc = 0;
444 else
445 rc = -EACCES;
446
447 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200448 smack_log(tracer_known->smk_known,
449 tracee_known->smk_known,
450 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100451
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300452 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100453 return rc;
454 }
455
456 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200457 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300458
459 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100460 return rc;
461}
462
Casey Schauflere114e472008-02-04 22:29:50 -0800463/*
464 * LSM hooks.
465 * We he, that is fun!
466 */
467
468/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000469 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800470 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100471 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800472 *
473 * Returns 0 if access is OK, an error code otherwise
474 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100475 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800476 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000477static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800478{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700479 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800480
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300481 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200482
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700483 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100484}
Casey Schauflere114e472008-02-04 22:29:50 -0800485
David Howells5cd9c582008-08-14 11:37:28 +0100486/**
487 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
488 * @ptp: parent task pointer
489 *
490 * Returns 0 if access is OK, an error code otherwise
491 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100492 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100493 */
494static int smack_ptrace_traceme(struct task_struct *ptp)
495{
496 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700497 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100498
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100499 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200500
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200501 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800502 return rc;
503}
504
505/**
506 * smack_syslog - Smack approval on syslog
507 * @type: message type
508 *
Casey Schauflere114e472008-02-04 22:29:50 -0800509 * Returns 0 on success, error code otherwise.
510 */
Eric Paris12b30522010-11-15 18:36:29 -0500511static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800512{
Eric Paris12b30522010-11-15 18:36:29 -0500513 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700514 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800515
Casey Schaufler1880eff2012-06-05 15:28:30 -0700516 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800517 return 0;
518
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800519 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800520 rc = -EACCES;
521
522 return rc;
523}
524
525
526/*
527 * Superblock Hooks.
528 */
529
530/**
531 * smack_sb_alloc_security - allocate a superblock blob
532 * @sb: the superblock getting the blob
533 *
534 * Returns 0 on success or -ENOMEM on error.
535 */
536static int smack_sb_alloc_security(struct super_block *sb)
537{
538 struct superblock_smack *sbsp;
539
540 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
541
542 if (sbsp == NULL)
543 return -ENOMEM;
544
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200545 sbsp->smk_root = &smack_known_floor;
546 sbsp->smk_default = &smack_known_floor;
547 sbsp->smk_floor = &smack_known_floor;
548 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700549 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500550 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700551 */
Casey Schauflere114e472008-02-04 22:29:50 -0800552 sb->s_security = sbsp;
553
554 return 0;
555}
556
557/**
558 * smack_sb_free_security - free a superblock blob
559 * @sb: the superblock getting the blob
560 *
561 */
562static void smack_sb_free_security(struct super_block *sb)
563{
564 kfree(sb->s_security);
565 sb->s_security = NULL;
566}
567
568/**
569 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800570 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800571 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800572 *
573 * Returns 0 on success or -ENOMEM on error.
574 *
575 * Copy the Smack specific mount options out of the mount
576 * options list.
577 */
Eric Parise0007522008-03-05 10:31:54 -0500578static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800579{
580 char *cp, *commap, *otheropts, *dp;
581
Casey Schauflere114e472008-02-04 22:29:50 -0800582 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
583 if (otheropts == NULL)
584 return -ENOMEM;
585
586 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
587 if (strstr(cp, SMK_FSDEFAULT) == cp)
588 dp = smackopts;
589 else if (strstr(cp, SMK_FSFLOOR) == cp)
590 dp = smackopts;
591 else if (strstr(cp, SMK_FSHAT) == cp)
592 dp = smackopts;
593 else if (strstr(cp, SMK_FSROOT) == cp)
594 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700595 else if (strstr(cp, SMK_FSTRANS) == cp)
596 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800597 else
598 dp = otheropts;
599
600 commap = strchr(cp, ',');
601 if (commap != NULL)
602 *commap = '\0';
603
604 if (*dp != '\0')
605 strcat(dp, ",");
606 strcat(dp, cp);
607 }
608
609 strcpy(orig, otheropts);
610 free_page((unsigned long)otheropts);
611
612 return 0;
613}
614
615/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530616 * smack_parse_opts_str - parse Smack specific mount options
617 * @options: mount options string
618 * @opts: where to store converted mount opts
619 *
620 * Returns 0 on success or -ENOMEM on error.
621 *
622 * converts Smack specific mount options to generic security option format
623 */
624static int smack_parse_opts_str(char *options,
625 struct security_mnt_opts *opts)
626{
627 char *p;
Casey Schaufler3d04c922015-08-12 11:56:02 -0700628 char *fsdefault = NULL;
629 char *fsfloor = NULL;
630 char *fshat = NULL;
631 char *fsroot = NULL;
632 char *fstransmute = NULL;
633 int rc = -ENOMEM;
634 int num_mnt_opts = 0;
635 int token;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530636
637 opts->num_mnt_opts = 0;
638
639 if (!options)
640 return 0;
641
642 while ((p = strsep(&options, ",")) != NULL) {
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530643 substring_t args[MAX_OPT_ARGS];
644
645 if (!*p)
646 continue;
647
Casey Schaufler3d04c922015-08-12 11:56:02 -0700648 token = match_token(p, smk_mount_tokens, args);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530649
650 switch (token) {
651 case Opt_fsdefault:
652 if (fsdefault)
653 goto out_opt_err;
654 fsdefault = match_strdup(&args[0]);
655 if (!fsdefault)
656 goto out_err;
657 break;
658 case Opt_fsfloor:
659 if (fsfloor)
660 goto out_opt_err;
661 fsfloor = match_strdup(&args[0]);
662 if (!fsfloor)
663 goto out_err;
664 break;
665 case Opt_fshat:
666 if (fshat)
667 goto out_opt_err;
668 fshat = match_strdup(&args[0]);
669 if (!fshat)
670 goto out_err;
671 break;
672 case Opt_fsroot:
673 if (fsroot)
674 goto out_opt_err;
675 fsroot = match_strdup(&args[0]);
676 if (!fsroot)
677 goto out_err;
678 break;
679 case Opt_fstransmute:
680 if (fstransmute)
681 goto out_opt_err;
682 fstransmute = match_strdup(&args[0]);
683 if (!fstransmute)
684 goto out_err;
685 break;
686 default:
687 rc = -EINVAL;
688 pr_warn("Smack: unknown mount option\n");
689 goto out_err;
690 }
691 }
692
Tetsuo Handa8c15d662016-11-14 20:12:55 +0900693 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_KERNEL);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530694 if (!opts->mnt_opts)
695 goto out_err;
696
697 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
Tetsuo Handa8c15d662016-11-14 20:12:55 +0900698 GFP_KERNEL);
Tetsuo Handac3c8dc92017-03-24 20:42:05 +0900699 if (!opts->mnt_opts_flags)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530700 goto out_err;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530701
702 if (fsdefault) {
703 opts->mnt_opts[num_mnt_opts] = fsdefault;
704 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
705 }
706 if (fsfloor) {
707 opts->mnt_opts[num_mnt_opts] = fsfloor;
708 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
709 }
710 if (fshat) {
711 opts->mnt_opts[num_mnt_opts] = fshat;
712 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
713 }
714 if (fsroot) {
715 opts->mnt_opts[num_mnt_opts] = fsroot;
716 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
717 }
718 if (fstransmute) {
719 opts->mnt_opts[num_mnt_opts] = fstransmute;
720 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
721 }
722
723 opts->num_mnt_opts = num_mnt_opts;
724 return 0;
725
726out_opt_err:
727 rc = -EINVAL;
728 pr_warn("Smack: duplicate mount options\n");
729
730out_err:
731 kfree(fsdefault);
732 kfree(fsfloor);
733 kfree(fshat);
734 kfree(fsroot);
735 kfree(fstransmute);
736 return rc;
737}
738
739/**
740 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800741 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530742 * @opts: Smack mount options
743 * @kern_flags: mount option from kernel space or user space
744 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800745 *
746 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530747 *
748 * Allow filesystems with binary mount data to explicitly set Smack mount
749 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800750 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530751static int smack_set_mnt_opts(struct super_block *sb,
752 struct security_mnt_opts *opts,
753 unsigned long kern_flags,
754 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800755{
756 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000757 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800758 struct superblock_smack *sp = sb->s_security;
759 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800760 struct smack_known *skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530761 int i;
762 int num_opts = opts->num_mnt_opts;
Casey Schauflere830b392013-05-22 18:43:07 -0700763 int transmute = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800764
Seth Forshee9f50eda2015-09-23 15:16:06 -0500765 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800766 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700767
Himanshu Shukla2097f592016-11-10 16:19:52 +0530768 if (!smack_privileged(CAP_MAC_ADMIN)) {
769 /*
770 * Unprivileged mounts don't get to specify Smack values.
771 */
772 if (num_opts)
773 return -EPERM;
774 /*
775 * Unprivileged mounts get root and default from the caller.
776 */
777 skp = smk_of_current();
778 sp->smk_root = skp;
779 sp->smk_default = skp;
780 /*
781 * For a handful of fs types with no user-controlled
782 * backing store it's okay to trust security labels
783 * in the filesystem. The rest are untrusted.
784 */
785 if (sb->s_user_ns != &init_user_ns &&
786 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
787 sb->s_magic != RAMFS_MAGIC) {
788 transmute = 1;
789 sp->smk_flags |= SMK_SB_UNTRUSTED;
790 }
791 }
792
Seth Forshee9f50eda2015-09-23 15:16:06 -0500793 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800794
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530795 for (i = 0; i < num_opts; i++) {
796 switch (opts->mnt_opts_flags[i]) {
797 case FSDEFAULT_MNT:
798 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200799 if (IS_ERR(skp))
800 return PTR_ERR(skp);
801 sp->smk_default = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530802 break;
803 case FSFLOOR_MNT:
804 skp = smk_import_entry(opts->mnt_opts[i], 0);
805 if (IS_ERR(skp))
806 return PTR_ERR(skp);
807 sp->smk_floor = skp;
808 break;
809 case FSHAT_MNT:
810 skp = smk_import_entry(opts->mnt_opts[i], 0);
811 if (IS_ERR(skp))
812 return PTR_ERR(skp);
813 sp->smk_hat = skp;
814 break;
815 case FSROOT_MNT:
816 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200817 if (IS_ERR(skp))
818 return PTR_ERR(skp);
819 sp->smk_root = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530820 break;
821 case FSTRANS_MNT:
822 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200823 if (IS_ERR(skp))
824 return PTR_ERR(skp);
825 sp->smk_root = skp;
826 transmute = 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530827 break;
828 default:
829 break;
Casey Schauflere114e472008-02-04 22:29:50 -0800830 }
831 }
832
833 /*
834 * Initialize the root inode.
835 */
836 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100837 if (isp == NULL) {
838 isp = new_inode_smack(sp->smk_root);
839 if (isp == NULL)
840 return -ENOMEM;
841 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700842 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800843 isp->smk_inode = sp->smk_root;
844
Casey Schauflere830b392013-05-22 18:43:07 -0700845 if (transmute)
846 isp->smk_flags |= SMK_INODE_TRANSMUTE;
847
Casey Schauflere114e472008-02-04 22:29:50 -0800848 return 0;
849}
850
851/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530852 * smack_sb_kern_mount - Smack specific mount processing
853 * @sb: the file system superblock
854 * @flags: the mount flags
855 * @data: the smack mount options
856 *
857 * Returns 0 on success, an error code on failure
858 */
859static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
860{
861 int rc = 0;
862 char *options = data;
863 struct security_mnt_opts opts;
864
865 security_init_mnt_opts(&opts);
866
867 if (!options)
868 goto out;
869
870 rc = smack_parse_opts_str(options, &opts);
871 if (rc)
872 goto out_err;
873
874out:
875 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
876
877out_err:
878 security_free_mnt_opts(&opts);
879 return rc;
880}
881
882/**
Casey Schauflere114e472008-02-04 22:29:50 -0800883 * smack_sb_statfs - Smack check on statfs
884 * @dentry: identifies the file system in question
885 *
886 * Returns 0 if current can read the floor of the filesystem,
887 * and error code otherwise
888 */
889static int smack_sb_statfs(struct dentry *dentry)
890{
891 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200892 int rc;
893 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800894
Eric Parisa2694342011-04-25 13:10:27 -0400895 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200896 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
897
898 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700899 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200900 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800901}
902
Casey Schauflere114e472008-02-04 22:29:50 -0800903/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800904 * BPRM hooks
905 */
906
Casey Schauflerce8a4322011-09-29 18:21:01 -0700907/**
908 * smack_bprm_set_creds - set creds for exec
909 * @bprm: the exec information
910 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100911 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700912 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800913static int smack_bprm_set_creds(struct linux_binprm *bprm)
914{
Al Viro496ad9a2013-01-23 17:07:38 -0500915 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300916 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800917 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500918 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800919 int rc;
920
Kees Cookddb4a142017-07-18 15:25:23 -0700921 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800922 return 0;
923
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300924 isp = inode->i_security;
925 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800926 return 0;
927
Seth Forshee809c02e2016-04-26 14:36:22 -0500928 sbsp = inode->i_sb->s_security;
929 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
930 isp->smk_task != sbsp->smk_root)
931 return 0;
932
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300933 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100934 struct task_struct *tracer;
935 rc = 0;
936
937 rcu_read_lock();
938 tracer = ptrace_parent(current);
939 if (likely(tracer != NULL))
940 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200941 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100942 PTRACE_MODE_ATTACH,
943 __func__);
944 rcu_read_unlock();
945
946 if (rc != 0)
947 return rc;
948 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300949 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800950
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300951 bsp->smk_task = isp->smk_task;
952 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800953
Kees Cookccbb6e12017-07-18 15:25:26 -0700954 /* Decide if this is a secure exec. */
955 if (bsp->smk_task != bsp->smk_forked)
956 bprm->secureexec = 1;
957
Casey Schaufler676dac42010-12-02 06:43:39 -0800958 return 0;
959}
960
961/*
Casey Schauflere114e472008-02-04 22:29:50 -0800962 * Inode hooks
963 */
964
965/**
966 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800967 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800968 *
969 * Returns 0 if it gets a blob, -ENOMEM otherwise
970 */
971static int smack_inode_alloc_security(struct inode *inode)
972{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700973 struct smack_known *skp = smk_of_current();
974
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200975 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800976 if (inode->i_security == NULL)
977 return -ENOMEM;
978 return 0;
979}
980
981/**
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530982 * smack_inode_free_rcu - Free inode_smack blob from cache
983 * @head: the rcu_head for getting inode_smack pointer
984 *
985 * Call back function called from call_rcu() to free
986 * the i_security blob pointer in inode
987 */
988static void smack_inode_free_rcu(struct rcu_head *head)
989{
990 struct inode_smack *issp;
991
992 issp = container_of(head, struct inode_smack, smk_rcu);
993 kmem_cache_free(smack_inode_cache, issp);
994}
995
996/**
997 * smack_inode_free_security - free an inode blob using call_rcu()
Randy Dunlap251a2a92009-02-18 11:42:33 -0800998 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800999 *
Himanshu Shukla3d4f6732016-11-23 11:59:19 +05301000 * Clears the blob pointer in inode using RCU
Casey Schauflere114e472008-02-04 22:29:50 -08001001 */
1002static void smack_inode_free_security(struct inode *inode)
1003{
Himanshu Shukla3d4f6732016-11-23 11:59:19 +05301004 struct inode_smack *issp = inode->i_security;
1005
1006 /*
1007 * The inode may still be referenced in a path walk and
1008 * a call to smack_inode_permission() can be made
1009 * after smack_inode_free_security() is called.
1010 * To avoid race condition free the i_security via RCU
1011 * and leave the current inode->i_security pointer intact.
1012 * The inode will be freed after the RCU grace period too.
1013 */
1014 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
Casey Schauflere114e472008-02-04 22:29:50 -08001015}
1016
1017/**
1018 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001019 * @inode: the newly created inode
1020 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -05001021 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001022 * @name: where to put the attribute name
1023 * @value: where to put the attribute value
1024 * @len: where to put the length of the attribute
1025 *
1026 * Returns 0 if it all works out, -ENOMEM if there's no memory
1027 */
1028static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09001029 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05001030 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -08001031{
Casey Schaufler2267b132012-03-13 19:14:19 -07001032 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001033 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001034 struct smack_known *isp = smk_of_inode(inode);
1035 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001036 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08001037
Tetsuo Handa95489062013-07-25 05:44:02 +09001038 if (name)
1039 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -08001040
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001041 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001042 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001043 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1044 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001045 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001046
1047 /*
1048 * If the access rule allows transmutation and
1049 * the directory requests transmutation then
1050 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001051 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001052 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001053 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001054 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001055 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001056 issp->smk_flags |= SMK_INODE_CHANGED;
1057 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001058
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001059 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001060 if (*value == NULL)
1061 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001062
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001063 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001064 }
Casey Schauflere114e472008-02-04 22:29:50 -08001065
1066 return 0;
1067}
1068
1069/**
1070 * smack_inode_link - Smack check on link
1071 * @old_dentry: the existing object
1072 * @dir: unused
1073 * @new_dentry: the new object
1074 *
1075 * Returns 0 if access is permitted, an error code otherwise
1076 */
1077static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1078 struct dentry *new_dentry)
1079{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001080 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001081 struct smk_audit_info ad;
1082 int rc;
1083
Eric Parisa2694342011-04-25 13:10:27 -04001084 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001086
David Howellsc6f493d2015-03-17 22:26:22 +00001087 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001088 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001089 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001090
David Howells88025652015-01-29 12:02:32 +00001091 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001092 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001093 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1094 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001095 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001096 }
1097
1098 return rc;
1099}
1100
1101/**
1102 * smack_inode_unlink - Smack check on inode deletion
1103 * @dir: containing directory object
1104 * @dentry: file to unlink
1105 *
1106 * Returns 0 if current can write the containing directory
1107 * and the object, error code otherwise
1108 */
1109static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1110{
David Howellsc6f493d2015-03-17 22:26:22 +00001111 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001112 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001113 int rc;
1114
Eric Parisa2694342011-04-25 13:10:27 -04001115 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001116 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1117
Casey Schauflere114e472008-02-04 22:29:50 -08001118 /*
1119 * You need write access to the thing you're unlinking
1120 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001121 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001122 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001123 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001124 /*
1125 * You also need write access to the containing directory
1126 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001127 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001128 smk_ad_setfield_u_fs_inode(&ad, dir);
1129 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001130 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001131 }
Casey Schauflere114e472008-02-04 22:29:50 -08001132 return rc;
1133}
1134
1135/**
1136 * smack_inode_rmdir - Smack check on directory deletion
1137 * @dir: containing directory object
1138 * @dentry: directory to unlink
1139 *
1140 * Returns 0 if current can write the containing directory
1141 * and the directory, error code otherwise
1142 */
1143static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1144{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001146 int rc;
1147
Eric Parisa2694342011-04-25 13:10:27 -04001148 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001149 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1150
Casey Schauflere114e472008-02-04 22:29:50 -08001151 /*
1152 * You need write access to the thing you're removing
1153 */
David Howellsc6f493d2015-03-17 22:26:22 +00001154 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1155 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001156 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001157 /*
1158 * You also need write access to the containing directory
1159 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001160 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001161 smk_ad_setfield_u_fs_inode(&ad, dir);
1162 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001163 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001164 }
Casey Schauflere114e472008-02-04 22:29:50 -08001165
1166 return rc;
1167}
1168
1169/**
1170 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001171 * @old_inode: unused
1172 * @old_dentry: the old object
1173 * @new_inode: unused
1174 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001175 *
1176 * Read and write access is required on both the old and
1177 * new directories.
1178 *
1179 * Returns 0 if access is permitted, an error code otherwise
1180 */
1181static int smack_inode_rename(struct inode *old_inode,
1182 struct dentry *old_dentry,
1183 struct inode *new_inode,
1184 struct dentry *new_dentry)
1185{
1186 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001187 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001188 struct smk_audit_info ad;
1189
Eric Parisa2694342011-04-25 13:10:27 -04001190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001191 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001192
David Howellsc6f493d2015-03-17 22:26:22 +00001193 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001194 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001195 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001196
David Howells88025652015-01-29 12:02:32 +00001197 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001198 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001199 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1200 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001201 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001202 }
Casey Schauflere114e472008-02-04 22:29:50 -08001203 return rc;
1204}
1205
1206/**
1207 * smack_inode_permission - Smack version of permission()
1208 * @inode: the inode in question
1209 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001210 *
1211 * This is the important Smack hook.
1212 *
1213 * Returns 0 if access is permitted, -EACCES otherwise
1214 */
Al Viroe74f71e2011-06-20 19:38:15 -04001215static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001216{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001217 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001218 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001219 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001220 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001221
1222 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001223 /*
1224 * No permission to check. Existence test. Yup, it's there.
1225 */
1226 if (mask == 0)
1227 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001228
Seth Forshee9f50eda2015-09-23 15:16:06 -05001229 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1230 if (smk_of_inode(inode) != sbsp->smk_root)
1231 return -EACCES;
1232 }
1233
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001234 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001235 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001236 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001237 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001238 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001239 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1240 rc = smk_bu_inode(inode, mask, rc);
1241 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001242}
1243
1244/**
1245 * smack_inode_setattr - Smack check for setting attributes
1246 * @dentry: the object
1247 * @iattr: for the force flag
1248 *
1249 * Returns 0 if access is permitted, an error code otherwise
1250 */
1251static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1252{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001253 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001254 int rc;
1255
Casey Schauflere114e472008-02-04 22:29:50 -08001256 /*
1257 * Need to allow for clearing the setuid bit.
1258 */
1259 if (iattr->ia_valid & ATTR_FORCE)
1260 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001261 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001262 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001263
David Howellsc6f493d2015-03-17 22:26:22 +00001264 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1265 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001266 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001267}
1268
1269/**
1270 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001271 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001272 * @dentry: the object
1273 *
1274 * Returns 0 if access is permitted, an error code otherwise
1275 */
Al Viro3f7036a2015-03-08 19:28:30 -04001276static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001277{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001278 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001279 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001280 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001281
Eric Parisf48b7392011-04-25 12:54:27 -04001282 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001283 smk_ad_setfield_u_fs_path(&ad, *path);
1284 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1285 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001286 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001287}
1288
1289/**
1290 * smack_inode_setxattr - Smack check for setting xattrs
1291 * @dentry: the object
1292 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001293 * @value: value of the attribute
1294 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001295 * @flags: unused
1296 *
1297 * This protects the Smack attribute explicitly.
1298 *
1299 * Returns 0 if access is permitted, an error code otherwise
1300 */
David Howells8f0cfa52008-04-29 00:59:41 -07001301static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1302 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001303{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001304 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001305 struct smack_known *skp;
1306 int check_priv = 0;
1307 int check_import = 0;
1308 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001309 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001310
Casey Schaufler19760ad2013-12-16 16:27:26 -08001311 /*
1312 * Check label validity here so import won't fail in post_setxattr
1313 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001314 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1315 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001316 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1317 check_priv = 1;
1318 check_import = 1;
1319 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1320 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1321 check_priv = 1;
1322 check_import = 1;
1323 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001324 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001325 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001326 if (size != TRANS_TRUE_SIZE ||
1327 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1328 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001329 } else
1330 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1331
Casey Schaufler19760ad2013-12-16 16:27:26 -08001332 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1333 rc = -EPERM;
1334
1335 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001336 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001337 if (IS_ERR(skp))
1338 rc = PTR_ERR(skp);
1339 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001340 (skp == &smack_known_star || skp == &smack_known_web)))
1341 rc = -EINVAL;
1342 }
1343
Eric Parisa2694342011-04-25 13:10:27 -04001344 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001345 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1346
Casey Schauflerd166c802014-08-27 14:51:27 -07001347 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001348 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1349 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001350 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001351
1352 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001353}
1354
1355/**
1356 * smack_inode_post_setxattr - Apply the Smack update approved above
1357 * @dentry: object
1358 * @name: attribute name
1359 * @value: attribute value
1360 * @size: attribute size
1361 * @flags: unused
1362 *
1363 * Set the pointer in the inode blob to the entry found
1364 * in the master label list.
1365 */
David Howells8f0cfa52008-04-29 00:59:41 -07001366static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1367 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001368{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001369 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001370 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001371
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001372 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1373 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1374 return;
1375 }
1376
Casey Schaufler676dac42010-12-02 06:43:39 -08001377 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001378 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001379 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001380 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001381 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001382 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001383 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001384 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001385 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001386 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001387 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001388 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001389 }
Casey Schauflere114e472008-02-04 22:29:50 -08001390
1391 return;
1392}
1393
Casey Schauflerce8a4322011-09-29 18:21:01 -07001394/**
Casey Schauflere114e472008-02-04 22:29:50 -08001395 * smack_inode_getxattr - Smack check on getxattr
1396 * @dentry: the object
1397 * @name: unused
1398 *
1399 * Returns 0 if access is permitted, an error code otherwise
1400 */
David Howells8f0cfa52008-04-29 00:59:41 -07001401static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001402{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001403 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001404 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001405
Eric Parisa2694342011-04-25 13:10:27 -04001406 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001407 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1408
David Howellsc6f493d2015-03-17 22:26:22 +00001409 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1410 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001411 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001412}
1413
Casey Schauflerce8a4322011-09-29 18:21:01 -07001414/**
Casey Schauflere114e472008-02-04 22:29:50 -08001415 * smack_inode_removexattr - Smack check on removexattr
1416 * @dentry: the object
1417 * @name: name of the attribute
1418 *
1419 * Removing the Smack attribute requires CAP_MAC_ADMIN
1420 *
1421 * Returns 0 if access is permitted, an error code otherwise
1422 */
David Howells8f0cfa52008-04-29 00:59:41 -07001423static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001424{
Casey Schaufler676dac42010-12-02 06:43:39 -08001425 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001426 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001427 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001428
Casey Schauflerbcdca222008-02-23 15:24:04 -08001429 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1430 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001431 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001432 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001433 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301434 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001435 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001436 rc = -EPERM;
1437 } else
1438 rc = cap_inode_removexattr(dentry, name);
1439
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001440 if (rc != 0)
1441 return rc;
1442
Eric Parisa2694342011-04-25 13:10:27 -04001443 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001444 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001445
David Howellsc6f493d2015-03-17 22:26:22 +00001446 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1447 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001448 if (rc != 0)
1449 return rc;
1450
David Howellsc6f493d2015-03-17 22:26:22 +00001451 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001452 /*
1453 * Don't do anything special for these.
1454 * XATTR_NAME_SMACKIPIN
1455 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001456 */
José Bollo80124952016-01-12 21:23:40 +01001457 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001458 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001459 struct superblock_smack *sbsp = sbp->s_security;
1460
1461 isp->smk_inode = sbsp->smk_default;
1462 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001463 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001464 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001465 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001466 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1467 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001468
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001469 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001470}
1471
1472/**
1473 * smack_inode_getsecurity - get smack xattrs
1474 * @inode: the object
1475 * @name: attribute name
1476 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001477 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001478 *
1479 * Returns the size of the attribute or an error code
1480 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001481static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001482 const char *name, void **buffer,
1483 bool alloc)
1484{
1485 struct socket_smack *ssp;
1486 struct socket *sock;
1487 struct super_block *sbp;
1488 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001489 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001490
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001491 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001492 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001493 else {
1494 /*
1495 * The rest of the Smack xattrs are only on sockets.
1496 */
1497 sbp = ip->i_sb;
1498 if (sbp->s_magic != SOCKFS_MAGIC)
1499 return -EOPNOTSUPP;
1500
1501 sock = SOCKET_I(ip);
1502 if (sock == NULL || sock->sk == NULL)
1503 return -EOPNOTSUPP;
1504
1505 ssp = sock->sk->sk_security;
1506
1507 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1508 isp = ssp->smk_in;
1509 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1510 isp = ssp->smk_out;
1511 else
1512 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001513 }
1514
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001515 if (alloc) {
1516 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1517 if (*buffer == NULL)
1518 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001519 }
1520
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001521 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001522}
1523
1524
1525/**
1526 * smack_inode_listsecurity - list the Smack attributes
1527 * @inode: the object
1528 * @buffer: where they go
1529 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001530 */
1531static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1532 size_t buffer_size)
1533{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001534 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001535
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001536 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001537 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001538
1539 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001540}
1541
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001542/**
1543 * smack_inode_getsecid - Extract inode's security id
1544 * @inode: inode to extract the info from
1545 * @secid: where result will be saved
1546 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001547static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001548{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001549 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001550
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001551 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001552}
1553
Casey Schauflere114e472008-02-04 22:29:50 -08001554/*
1555 * File Hooks
1556 */
1557
Casey Schaufler491a0b02016-01-26 15:08:35 -08001558/*
1559 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001560 *
1561 * Should access checks be done on each read or write?
1562 * UNICOS and SELinux say yes.
1563 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1564 *
1565 * I'll say no for now. Smack does not do the frequent
1566 * label changing that SELinux does.
1567 */
Casey Schauflere114e472008-02-04 22:29:50 -08001568
1569/**
1570 * smack_file_alloc_security - assign a file security blob
1571 * @file: the object
1572 *
1573 * The security blob for a file is a pointer to the master
1574 * label list, so no allocation is done.
1575 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001576 * f_security is the owner security information. It
1577 * isn't used on file access checks, it's for send_sigio.
1578 *
Casey Schauflere114e472008-02-04 22:29:50 -08001579 * Returns 0
1580 */
1581static int smack_file_alloc_security(struct file *file)
1582{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001583 struct smack_known *skp = smk_of_current();
1584
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001585 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001586 return 0;
1587}
1588
1589/**
1590 * smack_file_free_security - clear a file security blob
1591 * @file: the object
1592 *
1593 * The security blob for a file is a pointer to the master
1594 * label list, so no memory is freed.
1595 */
1596static void smack_file_free_security(struct file *file)
1597{
1598 file->f_security = NULL;
1599}
1600
1601/**
1602 * smack_file_ioctl - Smack check on ioctls
1603 * @file: the object
1604 * @cmd: what to do
1605 * @arg: unused
1606 *
1607 * Relies heavily on the correct use of the ioctl command conventions.
1608 *
1609 * Returns 0 if allowed, error code otherwise
1610 */
1611static int smack_file_ioctl(struct file *file, unsigned int cmd,
1612 unsigned long arg)
1613{
1614 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001615 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001616 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001617
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001618 if (unlikely(IS_PRIVATE(inode)))
1619 return 0;
1620
Eric Parisf48b7392011-04-25 12:54:27 -04001621 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001622 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001623
Casey Schauflerd166c802014-08-27 14:51:27 -07001624 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001625 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001626 rc = smk_bu_file(file, MAY_WRITE, rc);
1627 }
Casey Schauflere114e472008-02-04 22:29:50 -08001628
Casey Schauflerd166c802014-08-27 14:51:27 -07001629 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001630 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001631 rc = smk_bu_file(file, MAY_READ, rc);
1632 }
Casey Schauflere114e472008-02-04 22:29:50 -08001633
1634 return rc;
1635}
1636
1637/**
1638 * smack_file_lock - Smack check on file locking
1639 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001640 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001641 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001642 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001643 */
1644static int smack_file_lock(struct file *file, unsigned int cmd)
1645{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001646 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001647 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001648 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001649
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001650 if (unlikely(IS_PRIVATE(inode)))
1651 return 0;
1652
Eric Paris92f42502011-04-25 13:15:55 -04001653 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1654 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001655 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001656 rc = smk_bu_file(file, MAY_LOCK, rc);
1657 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001658}
1659
1660/**
1661 * smack_file_fcntl - Smack check on fcntl
1662 * @file: the object
1663 * @cmd: what action to check
1664 * @arg: unused
1665 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001666 * Generally these operations are harmless.
1667 * File locking operations present an obvious mechanism
1668 * for passing information, so they require write access.
1669 *
Casey Schauflere114e472008-02-04 22:29:50 -08001670 * Returns 0 if current has access, error code otherwise
1671 */
1672static int smack_file_fcntl(struct file *file, unsigned int cmd,
1673 unsigned long arg)
1674{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001675 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001676 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001677 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001678
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001679 if (unlikely(IS_PRIVATE(inode)))
1680 return 0;
1681
Casey Schauflere114e472008-02-04 22:29:50 -08001682 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001683 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001684 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001685 case F_SETLK:
1686 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001687 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1688 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001689 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001690 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001691 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001692 case F_SETOWN:
1693 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001694 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1695 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001696 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001697 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001698 break;
1699 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001700 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001701 }
1702
1703 return rc;
1704}
1705
1706/**
Al Viroe5467852012-05-30 13:30:51 -04001707 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001708 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1709 * if mapping anonymous memory.
1710 * @file contains the file structure for file to map (may be NULL).
1711 * @reqprot contains the protection requested by the application.
1712 * @prot contains the protection that will be applied by the kernel.
1713 * @flags contains the operational flags.
1714 * Return 0 if permission is granted.
1715 */
Al Viroe5467852012-05-30 13:30:51 -04001716static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001717 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001718 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001719{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001720 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001721 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001722 struct smack_rule *srp;
1723 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001724 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001725 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001726 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001727 int may;
1728 int mmay;
1729 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001730 int rc;
1731
Al Viro496ad9a2013-01-23 17:07:38 -05001732 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001733 return 0;
1734
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001735 if (unlikely(IS_PRIVATE(file_inode(file))))
1736 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
Casey Schaufler51d59af2017-05-31 08:53:42 -07001888 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001889 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 */
Al Viro94817692018-07-10 14:13:18 -04001931static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001932{
Al Viro94817692018-07-10 14:13:18 -04001933 struct task_smack *tsp = file->f_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 Schauflera6834c02014-04-21 11:10:26 -07001938 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1939 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301940 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001941 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001942
1943 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001944}
1945
Casey Schauflere114e472008-02-04 22:29:50 -08001946/*
1947 * Task hooks
1948 */
1949
1950/**
David Howellsee18d642009-09-02 09:14:21 +01001951 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1952 * @new: the new credentials
1953 * @gfp: the atomicity of any memory allocations
1954 *
1955 * Prepare a blank set of credentials for modification. This must allocate all
1956 * the memory the LSM module might require such that cred_transfer() can
1957 * complete without error.
1958 */
1959static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1960{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001961 struct task_smack *tsp;
1962
1963 tsp = new_task_smack(NULL, NULL, gfp);
1964 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001965 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001966
1967 cred->security = tsp;
1968
David Howellsee18d642009-09-02 09:14:21 +01001969 return 0;
1970}
1971
1972
1973/**
David Howellsf1752ee2008-11-14 10:39:17 +11001974 * smack_cred_free - "free" task-level security credentials
1975 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001976 *
Casey Schauflere114e472008-02-04 22:29:50 -08001977 */
David Howellsf1752ee2008-11-14 10:39:17 +11001978static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001979{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001980 struct task_smack *tsp = cred->security;
1981 struct smack_rule *rp;
1982 struct list_head *l;
1983 struct list_head *n;
1984
1985 if (tsp == NULL)
1986 return;
1987 cred->security = NULL;
1988
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001989 smk_destroy_label_list(&tsp->smk_relabel);
1990
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001991 list_for_each_safe(l, n, &tsp->smk_rules) {
1992 rp = list_entry(l, struct smack_rule, list);
1993 list_del(&rp->list);
1994 kfree(rp);
1995 }
1996 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001997}
1998
1999/**
David Howellsd84f4f92008-11-14 10:39:23 +11002000 * smack_cred_prepare - prepare new set of credentials for modification
2001 * @new: the new credentials
2002 * @old: the original credentials
2003 * @gfp: the atomicity of any memory allocations
2004 *
2005 * Prepare a new set of credentials for modification.
2006 */
2007static int smack_cred_prepare(struct cred *new, const struct cred *old,
2008 gfp_t gfp)
2009{
Casey Schaufler676dac42010-12-02 06:43:39 -08002010 struct task_smack *old_tsp = old->security;
2011 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002012 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002013
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002014 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002015 if (new_tsp == NULL)
2016 return -ENOMEM;
2017
Himanshu Shuklab437aba2016-11-10 16:17:02 +05302018 new->security = new_tsp;
2019
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002020 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2021 if (rc != 0)
2022 return rc;
2023
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002024 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2025 gfp);
2026 if (rc != 0)
2027 return rc;
2028
David Howellsd84f4f92008-11-14 10:39:23 +11002029 return 0;
2030}
2031
Randy Dunlap251a2a92009-02-18 11:42:33 -08002032/**
David Howellsee18d642009-09-02 09:14:21 +01002033 * smack_cred_transfer - Transfer the old credentials to the new credentials
2034 * @new: the new credentials
2035 * @old: the original credentials
2036 *
2037 * Fill in a set of blank credentials from another set of credentials.
2038 */
2039static void smack_cred_transfer(struct cred *new, const struct cred *old)
2040{
Casey Schaufler676dac42010-12-02 06:43:39 -08002041 struct task_smack *old_tsp = old->security;
2042 struct task_smack *new_tsp = new->security;
2043
2044 new_tsp->smk_task = old_tsp->smk_task;
2045 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002046 mutex_init(&new_tsp->smk_rules_lock);
2047 INIT_LIST_HEAD(&new_tsp->smk_rules);
2048
2049
2050 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01002051}
2052
2053/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08002054 * smack_cred_getsecid - get the secid corresponding to a creds structure
2055 * @c: the object creds
2056 * @secid: where to put the result
2057 *
2058 * Sets the secid to contain a u32 version of the smack label.
2059 */
2060static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2061{
2062 struct smack_known *skp;
2063
2064 rcu_read_lock();
2065 skp = smk_of_task(c->security);
2066 *secid = skp->smk_secid;
2067 rcu_read_unlock();
2068}
2069
2070/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002071 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002072 * @new: points to the set of credentials to be modified.
2073 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002074 *
2075 * Set the security data for a kernel service.
2076 */
2077static int smack_kernel_act_as(struct cred *new, u32 secid)
2078{
Casey Schaufler676dac42010-12-02 06:43:39 -08002079 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002080
Casey Schaufler152f91d2016-11-14 09:38:15 -08002081 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002082 return 0;
2083}
2084
2085/**
2086 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002087 * @new: points to the set of credentials to be modified
2088 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002089 *
2090 * Set the file creation context in a set of credentials to the same
2091 * as the objective context of the specified inode
2092 */
2093static int smack_kernel_create_files_as(struct cred *new,
2094 struct inode *inode)
2095{
2096 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002097 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002098
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002099 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002100 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002101 return 0;
2102}
2103
2104/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002105 * smk_curacc_on_task - helper to log task related access
2106 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002107 * @access: the access requested
2108 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002109 *
2110 * Return 0 if access is permitted
2111 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002112static int smk_curacc_on_task(struct task_struct *p, int access,
2113 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002114{
2115 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002116 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002117 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002118
Casey Schaufler531f1d42011-09-19 12:41:42 -07002119 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002120 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002121 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002122 rc = smk_bu_task(p, access, rc);
2123 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002124}
2125
2126/**
Casey Schauflere114e472008-02-04 22:29:50 -08002127 * smack_task_setpgid - Smack check on setting pgid
2128 * @p: the task object
2129 * @pgid: unused
2130 *
2131 * Return 0 if write access is permitted
2132 */
2133static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2134{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002135 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002136}
2137
2138/**
2139 * smack_task_getpgid - Smack access check for getpgid
2140 * @p: the object task
2141 *
2142 * Returns 0 if current can read the object task, error code otherwise
2143 */
2144static int smack_task_getpgid(struct task_struct *p)
2145{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002146 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002147}
2148
2149/**
2150 * smack_task_getsid - Smack access check for getsid
2151 * @p: the object task
2152 *
2153 * Returns 0 if current can read the object task, error code otherwise
2154 */
2155static int smack_task_getsid(struct task_struct *p)
2156{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002157 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002158}
2159
2160/**
2161 * smack_task_getsecid - get the secid of the task
2162 * @p: the object task
2163 * @secid: where to put the result
2164 *
2165 * Sets the secid to contain a u32 version of the smack label.
2166 */
2167static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2168{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002169 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002170
2171 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002172}
2173
2174/**
2175 * smack_task_setnice - Smack check on setting nice
2176 * @p: the task object
2177 * @nice: unused
2178 *
2179 * Return 0 if write access is permitted
2180 */
2181static int smack_task_setnice(struct task_struct *p, int nice)
2182{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002183 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002184}
2185
2186/**
2187 * smack_task_setioprio - Smack check on setting ioprio
2188 * @p: the task object
2189 * @ioprio: unused
2190 *
2191 * Return 0 if write access is permitted
2192 */
2193static int smack_task_setioprio(struct task_struct *p, int ioprio)
2194{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002195 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002196}
2197
2198/**
2199 * smack_task_getioprio - Smack check on reading ioprio
2200 * @p: the task object
2201 *
2202 * Return 0 if read access is permitted
2203 */
2204static int smack_task_getioprio(struct task_struct *p)
2205{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002206 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002207}
2208
2209/**
2210 * smack_task_setscheduler - Smack check on setting scheduler
2211 * @p: the task object
2212 * @policy: unused
2213 * @lp: unused
2214 *
2215 * Return 0 if read access is permitted
2216 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002217static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002218{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002219 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002220}
2221
2222/**
2223 * smack_task_getscheduler - Smack check on reading scheduler
2224 * @p: the task object
2225 *
2226 * Return 0 if read access is permitted
2227 */
2228static int smack_task_getscheduler(struct task_struct *p)
2229{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002230 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002231}
2232
2233/**
2234 * smack_task_movememory - Smack check on moving memory
2235 * @p: the task object
2236 *
2237 * Return 0 if write access is permitted
2238 */
2239static int smack_task_movememory(struct task_struct *p)
2240{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002241 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002242}
2243
2244/**
2245 * smack_task_kill - Smack check on signal delivery
2246 * @p: the task object
2247 * @info: unused
2248 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002249 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002250 *
2251 * Return 0 if write access is permitted
2252 *
Casey Schauflere114e472008-02-04 22:29:50 -08002253 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002254static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002255 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002256{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002257 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002258 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002259 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002260 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002261
Rafal Krypa18d872f2016-04-04 11:14:53 +02002262 if (!sig)
2263 return 0; /* null signal; existence test */
2264
Etienne Bassetecfcc532009-04-08 20:40:06 +02002265 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2266 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002267 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002268 * Sending a signal requires that the sender
2269 * can write the receiver.
2270 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002271 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002272 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2273 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002274 return rc;
2275 }
Casey Schauflere114e472008-02-04 22:29:50 -08002276 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002277 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002278 * specific behavior. This is not clean. For one thing
2279 * we can't take privilege into account.
2280 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002281 skp = smk_of_task(cred->security);
Casey Schauflerc60b9062016-08-30 10:31:39 -07002282 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2283 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002284 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002285}
2286
2287/**
Casey Schauflere114e472008-02-04 22:29:50 -08002288 * smack_task_to_inode - copy task smack into the inode blob
2289 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002290 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002291 *
2292 * Sets the smack pointer in the inode security blob
2293 */
2294static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2295{
2296 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002297 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002298
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002299 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002300 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002301}
2302
2303/*
2304 * Socket hooks.
2305 */
2306
2307/**
2308 * smack_sk_alloc_security - Allocate a socket blob
2309 * @sk: the socket
2310 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002311 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002312 *
2313 * Assign Smack pointers to current
2314 *
2315 * Returns 0 on success, -ENOMEM is there's no memory
2316 */
2317static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2318{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002319 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002320 struct socket_smack *ssp;
2321
2322 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2323 if (ssp == NULL)
2324 return -ENOMEM;
2325
jooseong lee08382c92016-11-03 11:54:39 +01002326 /*
2327 * Sockets created by kernel threads receive web label.
2328 */
2329 if (unlikely(current->flags & PF_KTHREAD)) {
2330 ssp->smk_in = &smack_known_web;
2331 ssp->smk_out = &smack_known_web;
2332 } else {
2333 ssp->smk_in = skp;
2334 ssp->smk_out = skp;
2335 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002336 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002337
2338 sk->sk_security = ssp;
2339
2340 return 0;
2341}
2342
2343/**
2344 * smack_sk_free_security - Free a socket blob
2345 * @sk: the socket
2346 *
2347 * Clears the blob pointer
2348 */
2349static void smack_sk_free_security(struct sock *sk)
2350{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302351#ifdef SMACK_IPV6_PORT_LABELING
2352 struct smk_port_label *spp;
2353
2354 if (sk->sk_family == PF_INET6) {
2355 rcu_read_lock();
2356 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2357 if (spp->smk_sock != sk)
2358 continue;
2359 spp->smk_can_reuse = 1;
2360 break;
2361 }
2362 rcu_read_unlock();
2363 }
2364#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002365 kfree(sk->sk_security);
2366}
2367
2368/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002369* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002370* @sip: the object end
2371*
2372* looks for host based access restrictions
2373*
2374* This version will only be appropriate for really small sets of single label
2375* hosts. The caller is responsible for ensuring that the RCU read lock is
2376* taken before calling this function.
2377*
2378* Returns the label of the far end or NULL if it's not special.
2379*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002380static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002381{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002382 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002383 struct in_addr *siap = &sip->sin_addr;
2384
2385 if (siap->s_addr == 0)
2386 return NULL;
2387
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002388 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2389 /*
2390 * we break after finding the first match because
2391 * the list is sorted from longest to shortest mask
2392 * so we have found the most specific match
2393 */
2394 if (snp->smk_host.s_addr ==
2395 (siap->s_addr & snp->smk_mask.s_addr))
2396 return snp->smk_label;
2397
2398 return NULL;
2399}
2400
2401#if IS_ENABLED(CONFIG_IPV6)
2402/*
2403 * smk_ipv6_localhost - Check for local ipv6 host address
2404 * @sip: the address
2405 *
2406 * Returns boolean true if this is the localhost address
2407 */
2408static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2409{
2410 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2411 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2412
2413 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2414 ntohs(be16p[7]) == 1)
2415 return true;
2416 return false;
2417}
2418
2419/**
2420* smack_ipv6host_label - check host based restrictions
2421* @sip: the object end
2422*
2423* looks for host based access restrictions
2424*
2425* This version will only be appropriate for really small sets of single label
2426* hosts. The caller is responsible for ensuring that the RCU read lock is
2427* taken before calling this function.
2428*
2429* Returns the label of the far end or NULL if it's not special.
2430*/
2431static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2432{
2433 struct smk_net6addr *snp;
2434 struct in6_addr *sap = &sip->sin6_addr;
2435 int i;
2436 int found = 0;
2437
2438 /*
2439 * It's local. Don't look for a host label.
2440 */
2441 if (smk_ipv6_localhost(sip))
2442 return NULL;
2443
2444 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002445 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002446 * If the label is NULL the entry has
2447 * been renounced. Ignore it.
2448 */
2449 if (snp->smk_label == NULL)
2450 continue;
2451 /*
Paul Moore07feee82009-03-27 17:10:54 -04002452 * we break after finding the first match because
2453 * the list is sorted from longest to shortest mask
2454 * so we have found the most specific match
2455 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002456 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002457 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2458 snp->smk_host.s6_addr16[i]) {
2459 found = 0;
2460 break;
2461 }
Etienne Basset43031542009-03-27 17:11:01 -04002462 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002463 if (found)
2464 return snp->smk_label;
2465 }
Paul Moore07feee82009-03-27 17:10:54 -04002466
2467 return NULL;
2468}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002469#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002470
2471/**
Casey Schauflere114e472008-02-04 22:29:50 -08002472 * smack_netlabel - Set the secattr on a socket
2473 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002474 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002475 *
2476 * Convert the outbound smack value (smk_out) to a
2477 * secattr and attach it to the socket.
2478 *
2479 * Returns 0 on success or an error code
2480 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002481static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002482{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002483 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002484 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002485 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002486
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002487 /*
2488 * Usually the netlabel code will handle changing the
2489 * packet labeling based on the label.
2490 * The case of a single label host is different, because
2491 * a single label host should never get a labeled packet
2492 * even though the label is usually associated with a packet
2493 * label.
2494 */
2495 local_bh_disable();
2496 bh_lock_sock_nested(sk);
2497
2498 if (ssp->smk_out == smack_net_ambient ||
2499 labeled == SMACK_UNLABELED_SOCKET)
2500 netlbl_sock_delattr(sk);
2501 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002502 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002503 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002504 }
2505
2506 bh_unlock_sock(sk);
2507 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002508
Casey Schauflere114e472008-02-04 22:29:50 -08002509 return rc;
2510}
2511
2512/**
Paul Moore07feee82009-03-27 17:10:54 -04002513 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2514 * @sk: the socket
2515 * @sap: the destination address
2516 *
2517 * Set the correct secattr for the given socket based on the destination
2518 * address and perform any outbound access checks needed.
2519 *
2520 * Returns 0 on success or an error code.
2521 *
2522 */
2523static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2524{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002525 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002526 int rc;
2527 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002528 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002529 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002530 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002531
2532 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002533 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002534 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002535#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002536 struct lsm_network_audit net;
2537
Eric Paris48c62af2012-04-02 13:15:44 -04002538 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2539 ad.a.u.net->family = sap->sin_family;
2540 ad.a.u.net->dport = sap->sin_port;
2541 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002542#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002543 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002544 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002545 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2546 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002547 } else {
2548 sk_lbl = SMACK_CIPSO_SOCKET;
2549 rc = 0;
2550 }
2551 rcu_read_unlock();
2552 if (rc != 0)
2553 return rc;
2554
2555 return smack_netlabel(sk, sk_lbl);
2556}
2557
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002558#if IS_ENABLED(CONFIG_IPV6)
2559/**
2560 * smk_ipv6_check - check Smack access
2561 * @subject: subject Smack label
2562 * @object: object Smack label
2563 * @address: address
2564 * @act: the action being taken
2565 *
2566 * Check an IPv6 access
2567 */
2568static int smk_ipv6_check(struct smack_known *subject,
2569 struct smack_known *object,
2570 struct sockaddr_in6 *address, int act)
2571{
2572#ifdef CONFIG_AUDIT
2573 struct lsm_network_audit net;
2574#endif
2575 struct smk_audit_info ad;
2576 int rc;
2577
2578#ifdef CONFIG_AUDIT
2579 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2580 ad.a.u.net->family = PF_INET6;
2581 ad.a.u.net->dport = ntohs(address->sin6_port);
2582 if (act == SMK_RECEIVING)
2583 ad.a.u.net->v6info.saddr = address->sin6_addr;
2584 else
2585 ad.a.u.net->v6info.daddr = address->sin6_addr;
2586#endif
2587 rc = smk_access(subject, object, MAY_WRITE, &ad);
2588 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2589 return rc;
2590}
2591#endif /* CONFIG_IPV6 */
2592
2593#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002594/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002595 * smk_ipv6_port_label - Smack port access table management
2596 * @sock: socket
2597 * @address: address
2598 *
2599 * Create or update the port list entry
2600 */
2601static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2602{
2603 struct sock *sk = sock->sk;
2604 struct sockaddr_in6 *addr6;
2605 struct socket_smack *ssp = sock->sk->sk_security;
2606 struct smk_port_label *spp;
2607 unsigned short port = 0;
2608
2609 if (address == NULL) {
2610 /*
2611 * This operation is changing the Smack information
2612 * on the bound socket. Take the changes to the port
2613 * as well.
2614 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302615 rcu_read_lock();
2616 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002617 if (sk != spp->smk_sock)
2618 continue;
2619 spp->smk_in = ssp->smk_in;
2620 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302621 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002622 return;
2623 }
2624 /*
2625 * A NULL address is only used for updating existing
2626 * bound entries. If there isn't one, it's OK.
2627 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302628 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002629 return;
2630 }
2631
2632 addr6 = (struct sockaddr_in6 *)address;
2633 port = ntohs(addr6->sin6_port);
2634 /*
2635 * This is a special case that is safely ignored.
2636 */
2637 if (port == 0)
2638 return;
2639
2640 /*
2641 * Look for an existing port list entry.
2642 * This is an indication that a port is getting reused.
2643 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302644 rcu_read_lock();
2645 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302646 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002647 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302648 if (spp->smk_can_reuse != 1) {
2649 rcu_read_unlock();
2650 return;
2651 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002652 spp->smk_port = port;
2653 spp->smk_sock = sk;
2654 spp->smk_in = ssp->smk_in;
2655 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302656 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302657 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002658 return;
2659 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302660 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002661 /*
2662 * A new port entry is required.
2663 */
2664 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2665 if (spp == NULL)
2666 return;
2667
2668 spp->smk_port = port;
2669 spp->smk_sock = sk;
2670 spp->smk_in = ssp->smk_in;
2671 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302672 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302673 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002674
Vishal Goel3c7ce342016-11-23 10:31:08 +05302675 mutex_lock(&smack_ipv6_lock);
2676 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2677 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002678 return;
2679}
2680
2681/**
2682 * smk_ipv6_port_check - check Smack port access
2683 * @sock: socket
2684 * @address: address
2685 *
2686 * Create or update the port list entry
2687 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002688static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002689 int act)
2690{
Casey Schauflerc6739442013-05-22 18:42:56 -07002691 struct smk_port_label *spp;
2692 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002693 struct smack_known *skp = NULL;
2694 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002695 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002696
2697 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002698 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002699 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002700 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002701 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002702 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002703 }
2704
2705 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002706 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002707 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002708 if (skp != NULL && object != NULL)
2709 return smk_ipv6_check(skp, object, address, act);
2710 if (skp == NULL)
2711 skp = smack_net_ambient;
2712 if (object == NULL)
2713 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002714
2715 /*
2716 * It's remote, so port lookup does no good.
2717 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002718 if (!smk_ipv6_localhost(address))
2719 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002720
2721 /*
2722 * It's local so the send check has to have passed.
2723 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002724 if (act == SMK_RECEIVING)
2725 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002726
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002727 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302728 rcu_read_lock();
2729 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302730 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002731 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002732 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002733 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002734 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002735 break;
2736 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302737 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002738
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002739 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002740}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002741#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002742
2743/**
Casey Schauflere114e472008-02-04 22:29:50 -08002744 * smack_inode_setsecurity - set smack xattrs
2745 * @inode: the object
2746 * @name: attribute name
2747 * @value: attribute value
2748 * @size: size of the attribute
2749 * @flags: unused
2750 *
2751 * Sets the named attribute in the appropriate blob
2752 *
2753 * Returns 0 on success, or an error code
2754 */
2755static int smack_inode_setsecurity(struct inode *inode, const char *name,
2756 const void *value, size_t size, int flags)
2757{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002758 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002759 struct inode_smack *nsp = inode->i_security;
2760 struct socket_smack *ssp;
2761 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002762 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002763
Casey Schauflerf7112e62012-05-06 15:22:02 -07002764 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302765 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002766
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002767 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002768 if (IS_ERR(skp))
2769 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002770
2771 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002772 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002773 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002774 return 0;
2775 }
2776 /*
2777 * The rest of the Smack xattrs are only on sockets.
2778 */
2779 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2780 return -EOPNOTSUPP;
2781
2782 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002783 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002784 return -EOPNOTSUPP;
2785
2786 ssp = sock->sk->sk_security;
2787
2788 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002789 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002790 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002791 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002792 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002793 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2794 if (rc != 0)
2795 printk(KERN_WARNING
2796 "Smack: \"%s\" netlbl error %d.\n",
2797 __func__, -rc);
2798 }
Casey Schauflere114e472008-02-04 22:29:50 -08002799 } else
2800 return -EOPNOTSUPP;
2801
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002802#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002803 if (sock->sk->sk_family == PF_INET6)
2804 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002805#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002806
Casey Schauflere114e472008-02-04 22:29:50 -08002807 return 0;
2808}
2809
2810/**
2811 * smack_socket_post_create - finish socket setup
2812 * @sock: the socket
2813 * @family: protocol family
2814 * @type: unused
2815 * @protocol: unused
2816 * @kern: unused
2817 *
2818 * Sets the netlabel information on the socket
2819 *
2820 * Returns 0 on success, and error code otherwise
2821 */
2822static int smack_socket_post_create(struct socket *sock, int family,
2823 int type, int protocol, int kern)
2824{
Marcin Lis74123012015-01-22 15:40:33 +01002825 struct socket_smack *ssp;
2826
2827 if (sock->sk == NULL)
2828 return 0;
2829
2830 /*
2831 * Sockets created by kernel threads receive web label.
2832 */
2833 if (unlikely(current->flags & PF_KTHREAD)) {
2834 ssp = sock->sk->sk_security;
2835 ssp->smk_in = &smack_known_web;
2836 ssp->smk_out = &smack_known_web;
2837 }
2838
2839 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002840 return 0;
2841 /*
2842 * Set the outbound netlbl.
2843 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002844 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2845}
2846
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002847/**
2848 * smack_socket_socketpair - create socket pair
2849 * @socka: one socket
2850 * @sockb: another socket
2851 *
2852 * Cross reference the peer labels for SO_PEERSEC
2853 *
2854 * Returns 0 on success, and error code otherwise
2855 */
2856static int smack_socket_socketpair(struct socket *socka,
2857 struct socket *sockb)
2858{
2859 struct socket_smack *asp = socka->sk->sk_security;
2860 struct socket_smack *bsp = sockb->sk->sk_security;
2861
2862 asp->smk_packet = bsp->smk_out;
2863 bsp->smk_packet = asp->smk_out;
2864
2865 return 0;
2866}
2867
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002868#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002869/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002870 * smack_socket_bind - record port binding information.
2871 * @sock: the socket
2872 * @address: the port address
2873 * @addrlen: size of the address
2874 *
2875 * Records the label bound to a port.
2876 *
2877 * Returns 0
2878 */
2879static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2880 int addrlen)
2881{
2882 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2883 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002884 return 0;
2885}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002886#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002887
2888/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002889 * smack_socket_connect - connect access check
2890 * @sock: the socket
2891 * @sap: the other end
2892 * @addrlen: size of sap
2893 *
2894 * Verifies that a connection may be possible
2895 *
2896 * Returns 0 on success, and error code otherwise
2897 */
2898static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2899 int addrlen)
2900{
Casey Schauflerc6739442013-05-22 18:42:56 -07002901 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002902#if IS_ENABLED(CONFIG_IPV6)
2903 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2904#endif
2905#ifdef SMACK_IPV6_SECMARK_LABELING
2906 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002907 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002908#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002909
Casey Schauflerc6739442013-05-22 18:42:56 -07002910 if (sock->sk == NULL)
2911 return 0;
2912
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002913#ifdef SMACK_IPV6_SECMARK_LABELING
2914 ssp = sock->sk->sk_security;
2915#endif
2916
Casey Schauflerc6739442013-05-22 18:42:56 -07002917 switch (sock->sk->sk_family) {
2918 case PF_INET:
2919 if (addrlen < sizeof(struct sockaddr_in))
2920 return -EINVAL;
2921 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2922 break;
2923 case PF_INET6:
2924 if (addrlen < sizeof(struct sockaddr_in6))
2925 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002926#ifdef SMACK_IPV6_SECMARK_LABELING
2927 rsp = smack_ipv6host_label(sip);
2928 if (rsp != NULL)
2929 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002930 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002931#endif
2932#ifdef SMACK_IPV6_PORT_LABELING
2933 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2934#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002935 break;
2936 }
2937 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002938}
2939
2940/**
2941 * smack_flags_to_may - convert S_ to MAY_ values
2942 * @flags: the S_ value
2943 *
2944 * Returns the equivalent MAY_ value
2945 */
2946static int smack_flags_to_may(int flags)
2947{
2948 int may = 0;
2949
2950 if (flags & S_IRUGO)
2951 may |= MAY_READ;
2952 if (flags & S_IWUGO)
2953 may |= MAY_WRITE;
2954 if (flags & S_IXUGO)
2955 may |= MAY_EXEC;
2956
2957 return may;
2958}
2959
2960/**
2961 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2962 * @msg: the object
2963 *
2964 * Returns 0
2965 */
2966static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2967{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002968 struct smack_known *skp = smk_of_current();
2969
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002970 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002971 return 0;
2972}
2973
2974/**
2975 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2976 * @msg: the object
2977 *
2978 * Clears the blob pointer
2979 */
2980static void smack_msg_msg_free_security(struct msg_msg *msg)
2981{
2982 msg->security = NULL;
2983}
2984
2985/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002986 * smack_of_ipc - the smack pointer for the ipc
2987 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002988 *
2989 * Returns a pointer to the smack value
2990 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002991static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002992{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002993 return (struct smack_known *)isp->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002994}
2995
2996/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002997 * smack_ipc_alloc_security - Set the security blob for ipc
2998 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002999 *
3000 * Returns 0
3001 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003002static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08003003{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003004 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003005
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003006 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003007 return 0;
3008}
3009
3010/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003011 * smack_ipc_free_security - Clear the security blob for ipc
3012 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003013 *
3014 * Clears the blob pointer
3015 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003016static void smack_ipc_free_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08003017{
Casey Schauflere114e472008-02-04 22:29:50 -08003018 isp->security = NULL;
3019}
3020
3021/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003022 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003023 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003024 * @access : access requested
3025 *
3026 * Returns 0 if current has the requested access, error code otherwise
3027 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003028static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003029{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003030 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003031 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003032 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003033
3034#ifdef CONFIG_AUDIT
3035 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003036 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003037#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003038 rc = smk_curacc(ssp, access, &ad);
3039 rc = smk_bu_current("shm", ssp, access, rc);
3040 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003041}
3042
3043/**
Casey Schauflere114e472008-02-04 22:29:50 -08003044 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003045 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003046 * @shmflg: access requested
3047 *
3048 * Returns 0 if current has the requested access, error code otherwise
3049 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003050static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003051{
Casey Schauflere114e472008-02-04 22:29:50 -08003052 int may;
3053
3054 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003055 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003056}
3057
3058/**
3059 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003060 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003061 * @cmd: what it wants to do
3062 *
3063 * Returns 0 if current has the requested access, error code otherwise
3064 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003065static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003066{
Casey Schauflere114e472008-02-04 22:29:50 -08003067 int may;
3068
3069 switch (cmd) {
3070 case IPC_STAT:
3071 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07003072 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003073 may = MAY_READ;
3074 break;
3075 case IPC_SET:
3076 case SHM_LOCK:
3077 case SHM_UNLOCK:
3078 case IPC_RMID:
3079 may = MAY_READWRITE;
3080 break;
3081 case IPC_INFO:
3082 case SHM_INFO:
3083 /*
3084 * System level information.
3085 */
3086 return 0;
3087 default:
3088 return -EINVAL;
3089 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003090 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003091}
3092
3093/**
3094 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003095 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003096 * @shmaddr: unused
3097 * @shmflg: access requested
3098 *
3099 * Returns 0 if current has the requested access, error code otherwise
3100 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003101static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003102 int shmflg)
3103{
Casey Schauflere114e472008-02-04 22:29:50 -08003104 int may;
3105
3106 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003107 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003108}
3109
3110/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003111 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003112 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003113 * @access : access requested
3114 *
3115 * Returns 0 if current has the requested access, error code otherwise
3116 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003117static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003118{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003120 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003121 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003122
3123#ifdef CONFIG_AUDIT
3124 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003125 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003126#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003127 rc = smk_curacc(ssp, access, &ad);
3128 rc = smk_bu_current("sem", ssp, access, rc);
3129 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003130}
3131
3132/**
Casey Schauflere114e472008-02-04 22:29:50 -08003133 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003134 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003135 * @semflg: access requested
3136 *
3137 * Returns 0 if current has the requested access, error code otherwise
3138 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003139static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003140{
Casey Schauflere114e472008-02-04 22:29:50 -08003141 int may;
3142
3143 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003144 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003145}
3146
3147/**
3148 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003149 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003150 * @cmd: what it wants to do
3151 *
3152 * Returns 0 if current has the requested access, error code otherwise
3153 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003154static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003155{
Casey Schauflere114e472008-02-04 22:29:50 -08003156 int may;
3157
3158 switch (cmd) {
3159 case GETPID:
3160 case GETNCNT:
3161 case GETZCNT:
3162 case GETVAL:
3163 case GETALL:
3164 case IPC_STAT:
3165 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003166 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003167 may = MAY_READ;
3168 break;
3169 case SETVAL:
3170 case SETALL:
3171 case IPC_RMID:
3172 case IPC_SET:
3173 may = MAY_READWRITE;
3174 break;
3175 case IPC_INFO:
3176 case SEM_INFO:
3177 /*
3178 * System level information
3179 */
3180 return 0;
3181 default:
3182 return -EINVAL;
3183 }
3184
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003185 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003186}
3187
3188/**
3189 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003190 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003191 * @sops: unused
3192 * @nsops: unused
3193 * @alter: unused
3194 *
3195 * Treated as read and write in all cases.
3196 *
3197 * Returns 0 if access is allowed, error code otherwise
3198 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003199static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003200 unsigned nsops, int alter)
3201{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003202 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003203}
3204
3205/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003206 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003207 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003208 * @access : access requested
3209 *
3210 * return 0 if current has access, error otherwise
3211 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003212static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003213{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003214 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003215 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003216 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003217
3218#ifdef CONFIG_AUDIT
3219 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003220 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003221#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003222 rc = smk_curacc(msp, access, &ad);
3223 rc = smk_bu_current("msq", msp, access, rc);
3224 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003225}
3226
3227/**
Casey Schauflere114e472008-02-04 22:29:50 -08003228 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003229 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003230 * @msqflg: access requested
3231 *
3232 * Returns 0 if current has the requested access, error code otherwise
3233 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003234static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003235{
Casey Schauflere114e472008-02-04 22:29:50 -08003236 int may;
3237
3238 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003239 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003240}
3241
3242/**
3243 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003244 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003245 * @cmd: what it wants to do
3246 *
3247 * Returns 0 if current has the requested access, error code otherwise
3248 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003249static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003250{
Casey Schauflere114e472008-02-04 22:29:50 -08003251 int may;
3252
3253 switch (cmd) {
3254 case IPC_STAT:
3255 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003256 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003257 may = MAY_READ;
3258 break;
3259 case IPC_SET:
3260 case IPC_RMID:
3261 may = MAY_READWRITE;
3262 break;
3263 case IPC_INFO:
3264 case MSG_INFO:
3265 /*
3266 * System level information
3267 */
3268 return 0;
3269 default:
3270 return -EINVAL;
3271 }
3272
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003273 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003274}
3275
3276/**
3277 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003278 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003279 * @msg: unused
3280 * @msqflg: access requested
3281 *
3282 * Returns 0 if current has the requested access, error code otherwise
3283 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003284static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003285 int msqflg)
3286{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003287 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003288
Etienne Bassetecfcc532009-04-08 20:40:06 +02003289 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003290 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003291}
3292
3293/**
3294 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003295 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003296 * @msg: unused
3297 * @target: unused
3298 * @type: unused
3299 * @mode: unused
3300 *
3301 * Returns 0 if current has read and write access, error code otherwise
3302 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003303static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003304 struct task_struct *target, long type, int mode)
3305{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003306 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003307}
3308
3309/**
3310 * smack_ipc_permission - Smack access for ipc_permission()
3311 * @ipp: the object permissions
3312 * @flag: access requested
3313 *
3314 * Returns 0 if current has read and write access, error code otherwise
3315 */
3316static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3317{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003318 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003319 int may = smack_flags_to_may(flag);
3320 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003321 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003322
Etienne Bassetecfcc532009-04-08 20:40:06 +02003323#ifdef CONFIG_AUDIT
3324 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3325 ad.a.u.ipc_id = ipp->id;
3326#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003327 rc = smk_curacc(iskp, may, &ad);
3328 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003329 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003330}
3331
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003332/**
3333 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003334 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003335 * @secid: where result will be saved
3336 */
3337static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3338{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003339 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003340
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003341 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003342}
3343
Casey Schauflere114e472008-02-04 22:29:50 -08003344/**
3345 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003346 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003347 * @inode: the object
3348 *
3349 * Set the inode's security blob if it hasn't been done already.
3350 */
3351static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3352{
3353 struct super_block *sbp;
3354 struct superblock_smack *sbsp;
3355 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003356 struct smack_known *skp;
3357 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003358 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003359 char trattr[TRANS_TRUE_SIZE];
3360 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003361 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003362 struct dentry *dp;
3363
3364 if (inode == NULL)
3365 return;
3366
3367 isp = inode->i_security;
3368
3369 mutex_lock(&isp->smk_lock);
3370 /*
3371 * If the inode is already instantiated
3372 * take the quick way out
3373 */
3374 if (isp->smk_flags & SMK_INODE_INSTANT)
3375 goto unlockandout;
3376
3377 sbp = inode->i_sb;
3378 sbsp = sbp->s_security;
3379 /*
3380 * We're going to use the superblock default label
3381 * if there's no label on the file.
3382 */
3383 final = sbsp->smk_default;
3384
3385 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003386 * If this is the root inode the superblock
3387 * may be in the process of initialization.
3388 * If that is the case use the root value out
3389 * of the superblock.
3390 */
3391 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003392 switch (sbp->s_magic) {
3393 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003394 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003395 /*
3396 * The cgroup filesystem is never mounted,
3397 * so there's no opportunity to set the mount
3398 * options.
3399 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003400 sbsp->smk_root = &smack_known_star;
3401 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003402 isp->smk_inode = sbsp->smk_root;
3403 break;
3404 case TMPFS_MAGIC:
3405 /*
3406 * What about shmem/tmpfs anonymous files with dentry
3407 * obtained from d_alloc_pseudo()?
3408 */
3409 isp->smk_inode = smk_of_current();
3410 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003411 case PIPEFS_MAGIC:
3412 isp->smk_inode = smk_of_current();
3413 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003414 case SOCKFS_MAGIC:
3415 /*
3416 * Socket access is controlled by the socket
3417 * structures associated with the task involved.
3418 */
3419 isp->smk_inode = &smack_known_star;
3420 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003421 default:
3422 isp->smk_inode = sbsp->smk_root;
3423 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003424 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003425 isp->smk_flags |= SMK_INODE_INSTANT;
3426 goto unlockandout;
3427 }
3428
3429 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003430 * This is pretty hackish.
3431 * Casey says that we shouldn't have to do
3432 * file system specific code, but it does help
3433 * with keeping it simple.
3434 */
3435 switch (sbp->s_magic) {
3436 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003437 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003438 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003439 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003440 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003441 * that the smack file system doesn't do
3442 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003443 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003444 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003445 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003446 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003447 break;
3448 case DEVPTS_SUPER_MAGIC:
3449 /*
3450 * devpts seems content with the label of the task.
3451 * Programs that change smack have to treat the
3452 * pty with respect.
3453 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003454 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003455 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003456 case PROC_SUPER_MAGIC:
3457 /*
3458 * Casey says procfs appears not to care.
3459 * The superblock default suffices.
3460 */
3461 break;
3462 case TMPFS_MAGIC:
3463 /*
3464 * Device labels should come from the filesystem,
3465 * but watch out, because they're volitile,
3466 * getting recreated on every reboot.
3467 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003468 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003469 /*
3470 * No break.
3471 *
3472 * If a smack value has been set we want to use it,
3473 * but since tmpfs isn't giving us the opportunity
3474 * to set mount options simulate setting the
3475 * superblock default.
3476 */
3477 default:
3478 /*
3479 * This isn't an understood special case.
3480 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003481 */
3482
3483 /*
3484 * UNIX domain sockets use lower level socket data.
3485 */
3486 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003487 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003488 break;
3489 }
3490 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003491 * No xattr support means, alas, no SMACK label.
3492 * Use the aforeapplied default.
3493 * It would be curious if the label of the task
3494 * does not match that assigned.
3495 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003496 if (!(inode->i_opflags & IOP_XATTR))
3497 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003498 /*
3499 * Get the dentry for xattr.
3500 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003501 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003502 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003503 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003504 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003505
3506 /*
3507 * Transmuting directory
3508 */
3509 if (S_ISDIR(inode->i_mode)) {
3510 /*
3511 * If this is a new directory and the label was
3512 * transmuted when the inode was initialized
3513 * set the transmute attribute on the directory
3514 * and mark the inode.
3515 *
3516 * If there is a transmute attribute on the
3517 * directory mark the inode.
3518 */
3519 if (isp->smk_flags & SMK_INODE_CHANGED) {
3520 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003521 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003522 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003523 TRANS_TRUE, TRANS_TRUE_SIZE,
3524 0);
3525 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003526 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003527 XATTR_NAME_SMACKTRANSMUTE, trattr,
3528 TRANS_TRUE_SIZE);
3529 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3530 TRANS_TRUE_SIZE) != 0)
3531 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003532 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003533 if (rc >= 0)
3534 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003535 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003536 /*
3537 * Don't let the exec or mmap label be "*" or "@".
3538 */
3539 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3540 if (IS_ERR(skp) || skp == &smack_known_star ||
3541 skp == &smack_known_web)
3542 skp = NULL;
3543 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003544
Casey Schaufler19760ad2013-12-16 16:27:26 -08003545 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003546 if (IS_ERR(skp) || skp == &smack_known_star ||
3547 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003548 skp = NULL;
3549 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003550
Casey Schauflere114e472008-02-04 22:29:50 -08003551 dput(dp);
3552 break;
3553 }
3554
3555 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003556 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003557 else
3558 isp->smk_inode = final;
3559
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003560 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003561
3562unlockandout:
3563 mutex_unlock(&isp->smk_lock);
3564 return;
3565}
3566
3567/**
3568 * smack_getprocattr - Smack process attribute access
3569 * @p: the object task
3570 * @name: the name of the attribute in /proc/.../attr
3571 * @value: where to put the result
3572 *
3573 * Places a copy of the task Smack into value
3574 *
3575 * Returns the length of the smack label or an error code
3576 */
3577static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3578{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003579 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003580 char *cp;
3581 int slen;
3582
3583 if (strcmp(name, "current") != 0)
3584 return -EINVAL;
3585
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003586 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003587 if (cp == NULL)
3588 return -ENOMEM;
3589
3590 slen = strlen(cp);
3591 *value = cp;
3592 return slen;
3593}
3594
3595/**
3596 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003597 * @name: the name of the attribute in /proc/.../attr
3598 * @value: the value to set
3599 * @size: the size of the value
3600 *
3601 * Sets the Smack value of the task. Only setting self
3602 * is permitted and only with privilege
3603 *
3604 * Returns the length of the smack label or an error code
3605 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003606static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003607{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003608 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003609 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003610 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003611 struct smack_known_list_elem *sklep;
3612 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003613
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003614 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003615 return -EPERM;
3616
Casey Schauflerf7112e62012-05-06 15:22:02 -07003617 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003618 return -EINVAL;
3619
3620 if (strcmp(name, "current") != 0)
3621 return -EINVAL;
3622
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003623 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003624 if (IS_ERR(skp))
3625 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003626
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003627 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303628 * No process is ever allowed the web ("@") label
3629 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003630 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303631 if (skp == &smack_known_web || skp == &smack_known_star)
3632 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003633
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003634 if (!smack_privileged(CAP_MAC_ADMIN)) {
3635 rc = -EPERM;
3636 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3637 if (sklep->smk_label == skp) {
3638 rc = 0;
3639 break;
3640 }
3641 if (rc)
3642 return rc;
3643 }
3644
David Howellsd84f4f92008-11-14 10:39:23 +11003645 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003646 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003647 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003648
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003649 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003650 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003651 /*
3652 * process can change its label only once
3653 */
3654 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003655
David Howellsd84f4f92008-11-14 10:39:23 +11003656 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003657 return size;
3658}
3659
3660/**
3661 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003662 * @sock: one sock
3663 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003664 * @newsk: unused
3665 *
3666 * Return 0 if a subject with the smack of sock could access
3667 * an object with the smack of other, otherwise an error code
3668 */
David S. Miller3610cda2011-01-05 15:38:53 -08003669static int smack_unix_stream_connect(struct sock *sock,
3670 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003671{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003672 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003673 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003674 struct socket_smack *ssp = sock->sk_security;
3675 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003676 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003677 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003678 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003679#ifdef CONFIG_AUDIT
3680 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003681#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003682
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003683 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3684 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003685 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003686#ifdef CONFIG_AUDIT
3687 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3688 smk_ad_setfield_u_net_sk(&ad, other);
3689#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003690 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3691 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003692 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003693 okp = osp->smk_out;
3694 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003695 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003696 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003697 MAY_WRITE, rc);
3698 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003699 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003700
Casey Schaufler975d5e52011-09-26 14:43:39 -07003701 /*
3702 * Cross reference the peer labels for SO_PEERSEC.
3703 */
3704 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003705 nsp->smk_packet = ssp->smk_out;
3706 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003707 }
3708
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003709 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003710}
3711
3712/**
3713 * smack_unix_may_send - Smack access on UDS
3714 * @sock: one socket
3715 * @other: the other socket
3716 *
3717 * Return 0 if a subject with the smack of sock could access
3718 * an object with the smack of other, otherwise an error code
3719 */
3720static int smack_unix_may_send(struct socket *sock, struct socket *other)
3721{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003722 struct socket_smack *ssp = sock->sk->sk_security;
3723 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003724 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003725 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003726
Kees Cook923e9a12012-04-10 13:26:44 -07003727#ifdef CONFIG_AUDIT
3728 struct lsm_network_audit net;
3729
Eric Paris48c62af2012-04-02 13:15:44 -04003730 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003731 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003732#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003733
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003734 if (smack_privileged(CAP_MAC_OVERRIDE))
3735 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003736
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003737 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3738 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003739 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003740}
3741
3742/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003743 * smack_socket_sendmsg - Smack check based on destination host
3744 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003745 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003746 * @size: the size of the message
3747 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003748 * Return 0 if the current subject can write to the destination host.
3749 * For IPv4 this is only a question if the destination is a single label host.
3750 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003751 */
3752static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3753 int size)
3754{
3755 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003756#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003757 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003758#endif
3759#ifdef SMACK_IPV6_SECMARK_LABELING
3760 struct socket_smack *ssp = sock->sk->sk_security;
3761 struct smack_known *rsp;
3762#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003763 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003764
3765 /*
3766 * Perfectly reasonable for this to be NULL
3767 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003768 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003769 return 0;
3770
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003771 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003772 case AF_INET:
3773 rc = smack_netlabel_send(sock->sk, sip);
3774 break;
3775 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003776#ifdef SMACK_IPV6_SECMARK_LABELING
3777 rsp = smack_ipv6host_label(sap);
3778 if (rsp != NULL)
3779 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3780 SMK_CONNECTING);
3781#endif
3782#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003783 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003784#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003785 break;
3786 }
3787 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003788}
3789
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003790/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003791 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003792 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003793 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003794 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003795 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003796 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003797static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3798 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003799{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003800 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003801 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003802 int acat;
3803 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003804
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003805 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003806 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003807 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003808 * If there are flags but no level netlabel isn't
3809 * behaving the way we expect it to.
3810 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003811 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003812 * Without guidance regarding the smack value
3813 * for the packet fall back on the network
3814 * ambient value.
3815 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003816 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303817 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003818 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003819 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003820 /*
3821 * Compare the catsets. Use the netlbl APIs.
3822 */
3823 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3824 if ((skp->smk_netlabel.flags &
3825 NETLBL_SECATTR_MLS_CAT) == 0)
3826 found = 1;
3827 break;
3828 }
3829 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003830 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3831 acat + 1);
3832 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003833 skp->smk_netlabel.attr.mls.cat,
3834 kcat + 1);
3835 if (acat < 0 || kcat < 0)
3836 break;
3837 }
3838 if (acat == kcat) {
3839 found = 1;
3840 break;
3841 }
Casey Schauflere114e472008-02-04 22:29:50 -08003842 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003843 rcu_read_unlock();
3844
3845 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003846 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003847
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003848 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003849 return &smack_known_web;
3850 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003851 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003852 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003853 /*
3854 * Looks like a fallback, which gives us a secid.
3855 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003856 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003857 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003858 * Without guidance regarding the smack value
3859 * for the packet fall back on the network
3860 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003861 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003862 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003863}
3864
Casey Schaufler69f287a2014-12-12 17:08:40 -08003865#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003866static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003867{
Casey Schauflerc6739442013-05-22 18:42:56 -07003868 u8 nexthdr;
3869 int offset;
3870 int proto = -EINVAL;
3871 struct ipv6hdr _ipv6h;
3872 struct ipv6hdr *ip6;
3873 __be16 frag_off;
3874 struct tcphdr _tcph, *th;
3875 struct udphdr _udph, *uh;
3876 struct dccp_hdr _dccph, *dh;
3877
3878 sip->sin6_port = 0;
3879
3880 offset = skb_network_offset(skb);
3881 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3882 if (ip6 == NULL)
3883 return -EINVAL;
3884 sip->sin6_addr = ip6->saddr;
3885
3886 nexthdr = ip6->nexthdr;
3887 offset += sizeof(_ipv6h);
3888 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3889 if (offset < 0)
3890 return -EINVAL;
3891
3892 proto = nexthdr;
3893 switch (proto) {
3894 case IPPROTO_TCP:
3895 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3896 if (th != NULL)
3897 sip->sin6_port = th->source;
3898 break;
3899 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003900 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003901 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3902 if (uh != NULL)
3903 sip->sin6_port = uh->source;
3904 break;
3905 case IPPROTO_DCCP:
3906 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3907 if (dh != NULL)
3908 sip->sin6_port = dh->dccph_sport;
3909 break;
3910 }
3911 return proto;
3912}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003913#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003914
Casey Schauflere114e472008-02-04 22:29:50 -08003915/**
3916 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3917 * @sk: socket
3918 * @skb: packet
3919 *
3920 * Returns 0 if the packet should be delivered, an error code otherwise
3921 */
3922static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3923{
3924 struct netlbl_lsm_secattr secattr;
3925 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003926 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003927 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003928 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003929 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003930#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003931 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003932#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003933#if IS_ENABLED(CONFIG_IPV6)
3934 struct sockaddr_in6 sadd;
3935 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003936
3937 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3938 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003939#endif /* CONFIG_IPV6 */
3940
Piotr Sawicki129a9982018-07-19 11:42:58 +02003941 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003942 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003943#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3944 /*
3945 * If there is a secmark use it rather than the CIPSO label.
3946 * If there is no secmark fall back to CIPSO.
3947 * The secmark is assumed to reflect policy better.
3948 */
3949 if (skb && skb->secmark != 0) {
3950 skp = smack_from_secid(skb->secmark);
3951 goto access_check;
3952 }
3953#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003954 /*
3955 * Translate what netlabel gave us.
3956 */
3957 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003958
Piotr Sawicki129a9982018-07-19 11:42:58 +02003959 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003960 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003961 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003962 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003963 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003964
Casey Schauflerc6739442013-05-22 18:42:56 -07003965 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003966
Casey Schaufler69f287a2014-12-12 17:08:40 -08003967#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3968access_check:
3969#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003970#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003971 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003972 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003973 ad.a.u.net->netif = skb->skb_iif;
3974 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003975#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003976 /*
3977 * Receiving a packet requires that the other end
3978 * be able to write here. Read access is not required.
3979 * This is the simplist possible security model
3980 * for networking.
3981 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003982 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3983 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003984 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003985 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003986 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003987 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003988#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003989 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003990 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003991 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3992 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003993 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003994#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003995 if (skb && skb->secmark != 0)
3996 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003997 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003998 skp = smack_ipv6host_label(&sadd);
3999 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08004000 skp = smack_net_ambient;
4001#ifdef CONFIG_AUDIT
4002 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02004003 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004004 ad.a.u.net->netif = skb->skb_iif;
4005 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4006#endif /* CONFIG_AUDIT */
4007 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4008 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4009 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004010#endif /* SMACK_IPV6_SECMARK_LABELING */
4011#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004012 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004013#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02004014 if (rc != 0)
4015 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4016 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07004017 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004018#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07004019 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004020
Paul Moorea8134292008-10-10 10:16:31 -04004021 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004022}
4023
4024/**
4025 * smack_socket_getpeersec_stream - pull in packet label
4026 * @sock: the socket
4027 * @optval: user's destination
4028 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004029 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004030 *
4031 * returns zero on success, an error code otherwise
4032 */
4033static int smack_socket_getpeersec_stream(struct socket *sock,
4034 char __user *optval,
4035 int __user *optlen, unsigned len)
4036{
4037 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004038 char *rcp = "";
4039 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004040 int rc = 0;
4041
4042 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004043 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004044 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004045 slen = strlen(rcp) + 1;
4046 }
Casey Schauflere114e472008-02-04 22:29:50 -08004047
4048 if (slen > len)
4049 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004050 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004051 rc = -EFAULT;
4052
4053 if (put_user(slen, optlen) != 0)
4054 rc = -EFAULT;
4055
4056 return rc;
4057}
4058
4059
4060/**
4061 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004062 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004063 * @skb: packet data
4064 * @secid: pointer to where to put the secid of the packet
4065 *
4066 * Sets the netlabel socket state on sk from parent
4067 */
4068static int smack_socket_getpeersec_dgram(struct socket *sock,
4069 struct sk_buff *skb, u32 *secid)
4070
4071{
4072 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004073 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004074 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004075 int family = PF_UNSPEC;
4076 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004077 int rc;
4078
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004079 if (skb != NULL) {
4080 if (skb->protocol == htons(ETH_P_IP))
4081 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004082#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004083 else if (skb->protocol == htons(ETH_P_IPV6))
4084 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004085#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004086 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004087 if (family == PF_UNSPEC && sock != NULL)
4088 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004089
Casey Schaufler69f287a2014-12-12 17:08:40 -08004090 switch (family) {
4091 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004092 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004093 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004094 break;
4095 case PF_INET:
4096#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4097 s = skb->secmark;
4098 if (s != 0)
4099 break;
4100#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004101 /*
4102 * Translate what netlabel gave us.
4103 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004104 if (sock != NULL && sock->sk != NULL)
4105 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004106 netlbl_secattr_init(&secattr);
4107 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4108 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004109 skp = smack_from_secattr(&secattr, ssp);
4110 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004111 }
4112 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004113 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004114 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004115#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004116 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004117#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004118 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004119 }
4120 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004121 if (s == 0)
4122 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004123 return 0;
4124}
4125
4126/**
Paul Moore07feee82009-03-27 17:10:54 -04004127 * smack_sock_graft - Initialize a newly created socket with an existing sock
4128 * @sk: child sock
4129 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004130 *
Paul Moore07feee82009-03-27 17:10:54 -04004131 * Set the smk_{in,out} state of an existing sock based on the process that
4132 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004133 */
4134static void smack_sock_graft(struct sock *sk, struct socket *parent)
4135{
4136 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004137 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004138
Paul Moore07feee82009-03-27 17:10:54 -04004139 if (sk == NULL ||
4140 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004141 return;
4142
4143 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004144 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004145 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004146 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004147}
4148
4149/**
4150 * smack_inet_conn_request - Smack access check on connect
4151 * @sk: socket involved
4152 * @skb: packet
4153 * @req: unused
4154 *
4155 * Returns 0 if a task with the packet label could write to
4156 * the socket, otherwise an error code
4157 */
4158static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4159 struct request_sock *req)
4160{
Paul Moore07feee82009-03-27 17:10:54 -04004161 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004162 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004163 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004164 struct netlbl_lsm_secattr secattr;
4165 struct sockaddr_in addr;
4166 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004167 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004168 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004169 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004170#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004171 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004172#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004173
Casey Schaufler69f287a2014-12-12 17:08:40 -08004174#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004175 if (family == PF_INET6) {
4176 /*
4177 * Handle mapped IPv4 packets arriving
4178 * via IPv6 sockets. Don't set up netlabel
4179 * processing on IPv6.
4180 */
4181 if (skb->protocol == htons(ETH_P_IP))
4182 family = PF_INET;
4183 else
4184 return 0;
4185 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004186#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004187
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004188#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4189 /*
4190 * If there is a secmark use it rather than the CIPSO label.
4191 * If there is no secmark fall back to CIPSO.
4192 * The secmark is assumed to reflect policy better.
4193 */
4194 if (skb && skb->secmark != 0) {
4195 skp = smack_from_secid(skb->secmark);
4196 goto access_check;
4197 }
4198#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4199
Paul Moore07feee82009-03-27 17:10:54 -04004200 netlbl_secattr_init(&secattr);
4201 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004202 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004203 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004204 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004205 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004206 netlbl_secattr_destroy(&secattr);
4207
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004208#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4209access_check:
4210#endif
4211
Etienne Bassetecfcc532009-04-08 20:40:06 +02004212#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004213 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4214 ad.a.u.net->family = family;
4215 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004216 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4217#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004218 /*
Paul Moore07feee82009-03-27 17:10:54 -04004219 * Receiving a packet requires that the other end be able to write
4220 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004221 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004222 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4223 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004224 if (rc != 0)
4225 return rc;
4226
4227 /*
4228 * Save the peer's label in the request_sock so we can later setup
4229 * smk_packet in the child socket so that SO_PEERCRED can report it.
4230 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004231 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004232
4233 /*
4234 * We need to decide if we want to label the incoming connection here
4235 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004236 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004237 */
4238 hdr = ip_hdr(skb);
4239 addr.sin_addr.s_addr = hdr->saddr;
4240 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004241 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004242 rcu_read_unlock();
4243
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004244 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004245 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004246 else
Paul Moore07feee82009-03-27 17:10:54 -04004247 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004248
4249 return rc;
4250}
4251
Paul Moore07feee82009-03-27 17:10:54 -04004252/**
4253 * smack_inet_csk_clone - Copy the connection information to the new socket
4254 * @sk: the new socket
4255 * @req: the connection's request_sock
4256 *
4257 * Transfer the connection's peer label to the newly created socket.
4258 */
4259static void smack_inet_csk_clone(struct sock *sk,
4260 const struct request_sock *req)
4261{
4262 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004263 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004264
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004265 if (req->peer_secid != 0) {
4266 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004267 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004268 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004269 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004270}
4271
Casey Schauflere114e472008-02-04 22:29:50 -08004272/*
4273 * Key management security hooks
4274 *
4275 * Casey has not tested key support very heavily.
4276 * The permission check is most likely too restrictive.
4277 * If you care about keys please have a look.
4278 */
4279#ifdef CONFIG_KEYS
4280
4281/**
4282 * smack_key_alloc - Set the key security blob
4283 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004284 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004285 * @flags: unused
4286 *
4287 * No allocation required
4288 *
4289 * Returns 0
4290 */
David Howellsd84f4f92008-11-14 10:39:23 +11004291static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004292 unsigned long flags)
4293{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004294 struct smack_known *skp = smk_of_task(cred->security);
4295
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004296 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004297 return 0;
4298}
4299
4300/**
4301 * smack_key_free - Clear the key security blob
4302 * @key: the object
4303 *
4304 * Clear the blob pointer
4305 */
4306static void smack_key_free(struct key *key)
4307{
4308 key->security = NULL;
4309}
4310
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004311/**
Casey Schauflere114e472008-02-04 22:29:50 -08004312 * smack_key_permission - Smack access on a key
4313 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004314 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004315 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004316 *
4317 * Return 0 if the task has read and write to the object,
4318 * an error code otherwise
4319 */
4320static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004321 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004322{
4323 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004324 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004325 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004326 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004327 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004328
4329 keyp = key_ref_to_ptr(key_ref);
4330 if (keyp == NULL)
4331 return -EINVAL;
4332 /*
4333 * If the key hasn't been initialized give it access so that
4334 * it may do so.
4335 */
4336 if (keyp->security == NULL)
4337 return 0;
4338 /*
4339 * This should not occur
4340 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004341 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004342 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004343
4344 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4345 return 0;
4346
Etienne Bassetecfcc532009-04-08 20:40:06 +02004347#ifdef CONFIG_AUDIT
4348 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4349 ad.a.u.key_struct.key = keyp->serial;
4350 ad.a.u.key_struct.key_desc = keyp->description;
4351#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004352 if (perm & KEY_NEED_READ)
4353 request = MAY_READ;
4354 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4355 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004356 rc = smk_access(tkp, keyp->security, request, &ad);
4357 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4358 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004359}
José Bollo7fc5f362015-02-17 15:41:22 +01004360
4361/*
4362 * smack_key_getsecurity - Smack label tagging the key
4363 * @key points to the key to be queried
4364 * @_buffer points to a pointer that should be set to point to the
4365 * resulting string (if no label or an error occurs).
4366 * Return the length of the string (including terminating NUL) or -ve if
4367 * an error.
4368 * May also return 0 (and a NULL buffer pointer) if there is no label.
4369 */
4370static int smack_key_getsecurity(struct key *key, char **_buffer)
4371{
4372 struct smack_known *skp = key->security;
4373 size_t length;
4374 char *copy;
4375
4376 if (key->security == NULL) {
4377 *_buffer = NULL;
4378 return 0;
4379 }
4380
4381 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4382 if (copy == NULL)
4383 return -ENOMEM;
4384 length = strlen(copy) + 1;
4385
4386 *_buffer = copy;
4387 return length;
4388}
4389
Casey Schauflere114e472008-02-04 22:29:50 -08004390#endif /* CONFIG_KEYS */
4391
4392/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004393 * Smack Audit hooks
4394 *
4395 * Audit requires a unique representation of each Smack specific
4396 * rule. This unique representation is used to distinguish the
4397 * object to be audited from remaining kernel objects and also
4398 * works as a glue between the audit hooks.
4399 *
4400 * Since repository entries are added but never deleted, we'll use
4401 * the smack_known label address related to the given audit rule as
4402 * the needed unique representation. This also better fits the smack
4403 * model where nearly everything is a label.
4404 */
4405#ifdef CONFIG_AUDIT
4406
4407/**
4408 * smack_audit_rule_init - Initialize a smack audit rule
4409 * @field: audit rule fields given from user-space (audit.h)
4410 * @op: required testing operator (=, !=, >, <, ...)
4411 * @rulestr: smack label to be audited
4412 * @vrule: pointer to save our own audit rule representation
4413 *
4414 * Prepare to audit cases where (@field @op @rulestr) is true.
4415 * The label to be audited is created if necessay.
4416 */
4417static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4418{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004419 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004420 char **rule = (char **)vrule;
4421 *rule = NULL;
4422
4423 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4424 return -EINVAL;
4425
Al Viro5af75d82008-12-16 05:59:26 -05004426 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004427 return -EINVAL;
4428
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004429 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004430 if (IS_ERR(skp))
4431 return PTR_ERR(skp);
4432
4433 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004434
4435 return 0;
4436}
4437
4438/**
4439 * smack_audit_rule_known - Distinguish Smack audit rules
4440 * @krule: rule of interest, in Audit kernel representation format
4441 *
4442 * This is used to filter Smack rules from remaining Audit ones.
4443 * If it's proved that this rule belongs to us, the
4444 * audit_rule_match hook will be called to do the final judgement.
4445 */
4446static int smack_audit_rule_known(struct audit_krule *krule)
4447{
4448 struct audit_field *f;
4449 int i;
4450
4451 for (i = 0; i < krule->field_count; i++) {
4452 f = &krule->fields[i];
4453
4454 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4455 return 1;
4456 }
4457
4458 return 0;
4459}
4460
4461/**
4462 * smack_audit_rule_match - Audit given object ?
4463 * @secid: security id for identifying the object to test
4464 * @field: audit rule flags given from user-space
4465 * @op: required testing operator
4466 * @vrule: smack internal rule presentation
4467 * @actx: audit context associated with the check
4468 *
4469 * The core Audit hook. It's used to take the decision of
4470 * whether to audit or not to audit a given object.
4471 */
4472static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4473 struct audit_context *actx)
4474{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004475 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004476 char *rule = vrule;
4477
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004478 if (unlikely(!rule)) {
4479 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004480 return -ENOENT;
4481 }
4482
4483 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4484 return 0;
4485
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004486 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004487
4488 /*
4489 * No need to do string comparisons. If a match occurs,
4490 * both pointers will point to the same smack_known
4491 * label.
4492 */
Al Viro5af75d82008-12-16 05:59:26 -05004493 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004494 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004495 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004496 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004497
4498 return 0;
4499}
4500
Casey Schaufler491a0b02016-01-26 15:08:35 -08004501/*
4502 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004503 * No memory was allocated.
4504 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004505
4506#endif /* CONFIG_AUDIT */
4507
Randy Dunlap251a2a92009-02-18 11:42:33 -08004508/**
David Quigley746df9b2013-05-22 12:50:35 -04004509 * smack_ismaclabel - check if xattr @name references a smack MAC label
4510 * @name: Full xattr name to check.
4511 */
4512static int smack_ismaclabel(const char *name)
4513{
4514 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4515}
4516
4517
4518/**
Casey Schauflere114e472008-02-04 22:29:50 -08004519 * smack_secid_to_secctx - return the smack label for a secid
4520 * @secid: incoming integer
4521 * @secdata: destination
4522 * @seclen: how long it is
4523 *
4524 * Exists for networking code.
4525 */
4526static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4527{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004528 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004529
Eric Parisd5630b92010-10-13 16:24:48 -04004530 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004531 *secdata = skp->smk_known;
4532 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004533 return 0;
4534}
4535
Randy Dunlap251a2a92009-02-18 11:42:33 -08004536/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004537 * smack_secctx_to_secid - return the secid for a smack label
4538 * @secdata: smack label
4539 * @seclen: how long result is
4540 * @secid: outgoing integer
4541 *
4542 * Exists for audit and networking code.
4543 */
David Howellse52c17642008-04-29 20:52:51 +01004544static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004545{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004546 struct smack_known *skp = smk_find_entry(secdata);
4547
4548 if (skp)
4549 *secid = skp->smk_secid;
4550 else
4551 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004552 return 0;
4553}
4554
Casey Schaufler491a0b02016-01-26 15:08:35 -08004555/*
4556 * There used to be a smack_release_secctx hook
4557 * that did nothing back when hooks were in a vector.
4558 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004559 */
Casey Schauflere114e472008-02-04 22:29:50 -08004560
David P. Quigley1ee65e32009-09-03 14:25:57 -04004561static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4562{
4563 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4564}
4565
4566static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4567{
4568 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4569}
4570
4571static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4572{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004573 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004574
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004575 *ctx = skp->smk_known;
4576 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004577 return 0;
4578}
4579
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004580static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4581{
4582
4583 struct task_smack *tsp;
4584 struct smack_known *skp;
4585 struct inode_smack *isp;
4586 struct cred *new_creds = *new;
4587
4588 if (new_creds == NULL) {
4589 new_creds = prepare_creds();
4590 if (new_creds == NULL)
4591 return -ENOMEM;
4592 }
4593
4594 tsp = new_creds->security;
4595
4596 /*
4597 * Get label from overlay inode and set it in create_sid
4598 */
4599 isp = d_inode(dentry->d_parent)->i_security;
4600 skp = isp->smk_inode;
4601 tsp->smk_task = skp;
4602 *new = new_creds;
4603 return 0;
4604}
4605
4606static int smack_inode_copy_up_xattr(const char *name)
4607{
4608 /*
4609 * Return 1 if this is the smack access Smack attribute.
4610 */
4611 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4612 return 1;
4613
4614 return -EOPNOTSUPP;
4615}
4616
4617static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4618 struct qstr *name,
4619 const struct cred *old,
4620 struct cred *new)
4621{
4622 struct task_smack *otsp = old->security;
4623 struct task_smack *ntsp = new->security;
4624 struct inode_smack *isp;
4625 int may;
4626
4627 /*
4628 * Use the process credential unless all of
4629 * the transmuting criteria are met
4630 */
4631 ntsp->smk_task = otsp->smk_task;
4632
4633 /*
4634 * the attribute of the containing directory
4635 */
4636 isp = d_inode(dentry->d_parent)->i_security;
4637
4638 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4639 rcu_read_lock();
4640 may = smk_access_entry(otsp->smk_task->smk_known,
4641 isp->smk_inode->smk_known,
4642 &otsp->smk_task->smk_rules);
4643 rcu_read_unlock();
4644
4645 /*
4646 * If the directory is transmuting and the rule
4647 * providing access is transmuting use the containing
4648 * directory label instead of the process label.
4649 */
4650 if (may > 0 && (may & MAY_TRANSMUTE))
4651 ntsp->smk_task = isp->smk_inode;
4652 }
4653 return 0;
4654}
4655
James Morrisca97d932017-02-15 00:18:51 +11004656static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004657 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4658 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4659 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004660
Casey Schauflere20b0432015-05-02 15:11:36 -07004661 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4662 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4663 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4664 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4665 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304666 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4667 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
Casey Schauflere114e472008-02-04 22:29:50 -08004668
Casey Schauflere20b0432015-05-02 15:11:36 -07004669 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004670
Casey Schauflere20b0432015-05-02 15:11:36 -07004671 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4672 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4673 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4674 LSM_HOOK_INIT(inode_link, smack_inode_link),
4675 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4676 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4677 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4678 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4679 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4680 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4681 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4682 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4683 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4684 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4685 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4686 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4687 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4688 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004689
Casey Schauflere20b0432015-05-02 15:11:36 -07004690 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4691 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4692 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4693 LSM_HOOK_INIT(file_lock, smack_file_lock),
4694 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4695 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4696 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4697 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4698 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4699 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004700
Casey Schauflere20b0432015-05-02 15:11:36 -07004701 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004702
Casey Schauflere20b0432015-05-02 15:11:36 -07004703 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4704 LSM_HOOK_INIT(cred_free, smack_cred_free),
4705 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4706 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004707 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004708 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4709 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4710 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4711 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4712 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4713 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4714 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4715 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4716 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4717 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4718 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4719 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4720 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004721 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004722
Casey Schauflere20b0432015-05-02 15:11:36 -07004723 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4724 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004725
Casey Schauflere20b0432015-05-02 15:11:36 -07004726 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4727 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004728
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004729 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4730 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004731 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4732 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4733 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4734 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004735
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004736 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4737 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004738 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4739 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4740 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004741
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004742 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4743 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004744 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4745 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4746 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004747
Casey Schauflere20b0432015-05-02 15:11:36 -07004748 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004749
Casey Schauflere20b0432015-05-02 15:11:36 -07004750 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4751 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004752
Casey Schauflere20b0432015-05-02 15:11:36 -07004753 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4754 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004755
Casey Schauflere20b0432015-05-02 15:11:36 -07004756 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004757 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004758#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004759 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004760#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004761 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4762 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4763 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4764 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4765 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4766 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4767 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4768 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4769 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4770 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004771
Casey Schauflere114e472008-02-04 22:29:50 -08004772 /* key management security hooks */
4773#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004774 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4775 LSM_HOOK_INIT(key_free, smack_key_free),
4776 LSM_HOOK_INIT(key_permission, smack_key_permission),
4777 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004778#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004779
4780 /* Audit hooks */
4781#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004782 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4783 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4784 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004785#endif /* CONFIG_AUDIT */
4786
Casey Schauflere20b0432015-05-02 15:11:36 -07004787 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4788 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4789 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004790 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4791 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4792 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004793 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4794 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4795 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004796};
4797
Etienne Basset7198e2e2009-03-24 20:53:24 +01004798
Casey Schaufler86812bb2012-04-17 18:55:46 -07004799static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004800{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004801 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004802 * Initialize rule list locks
4803 */
4804 mutex_init(&smack_known_huh.smk_rules_lock);
4805 mutex_init(&smack_known_hat.smk_rules_lock);
4806 mutex_init(&smack_known_floor.smk_rules_lock);
4807 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004808 mutex_init(&smack_known_web.smk_rules_lock);
4809 /*
4810 * Initialize rule lists
4811 */
4812 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4813 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4814 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4815 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004816 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4817 /*
4818 * Create the known labels list
4819 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004820 smk_insert_entry(&smack_known_huh);
4821 smk_insert_entry(&smack_known_hat);
4822 smk_insert_entry(&smack_known_star);
4823 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004824 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004825}
4826
Casey Schauflere114e472008-02-04 22:29:50 -08004827/**
4828 * smack_init - initialize the smack system
4829 *
4830 * Returns 0
4831 */
4832static __init int smack_init(void)
4833{
David Howellsd84f4f92008-11-14 10:39:23 +11004834 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004835 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004836
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004837 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004838 return 0;
4839
Rohit1a5b4722014-10-15 17:40:41 +05304840 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4841 if (!smack_inode_cache)
4842 return -ENOMEM;
4843
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004844 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4845 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304846 if (tsp == NULL) {
4847 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004848 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304849 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004850
José Bollod21b7b02015-10-02 15:15:56 +02004851 smack_enabled = 1;
4852
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004853 pr_info("Smack: Initializing.\n");
4854#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4855 pr_info("Smack: Netfilter enabled.\n");
4856#endif
4857#ifdef SMACK_IPV6_PORT_LABELING
4858 pr_info("Smack: IPv6 port labeling enabled.\n");
4859#endif
4860#ifdef SMACK_IPV6_SECMARK_LABELING
4861 pr_info("Smack: IPv6 Netfilter enabled.\n");
4862#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004863
4864 /*
4865 * Set the security state for the initial task.
4866 */
David Howellsd84f4f92008-11-14 10:39:23 +11004867 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004868 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004869
Casey Schaufler86812bb2012-04-17 18:55:46 -07004870 /* initialize the smack_known_list */
4871 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004872
4873 /*
4874 * Register with LSM
4875 */
Casey Schauflerd69dece52017-01-18 17:09:05 -08004876 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
Casey Schauflere114e472008-02-04 22:29:50 -08004877
4878 return 0;
4879}
4880
4881/*
4882 * Smack requires early initialization in order to label
4883 * all processes and objects when they are created.
4884 */
4885security_initcall(smack_init);