blob: dba7bc53d86a5d9e32663a56e46390511ad4fbec [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;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700424 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100425
426 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
427 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
428 smk_ad_setfield_u_tsk(&ad, tracer);
429 saip = &ad;
430 }
431
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300432 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700433 tracercred = __task_cred(tracer);
434 tsp = tracercred->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200435 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100436
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100437 if ((mode & PTRACE_MODE_ATTACH) &&
438 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
439 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200440 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100441 rc = 0;
442 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
443 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700444 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100445 rc = 0;
446 else
447 rc = -EACCES;
448
449 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200450 smack_log(tracer_known->smk_known,
451 tracee_known->smk_known,
452 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100453
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300454 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100455 return rc;
456 }
457
458 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200459 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300460
461 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100462 return rc;
463}
464
Casey Schauflere114e472008-02-04 22:29:50 -0800465/*
466 * LSM hooks.
467 * We he, that is fun!
468 */
469
470/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000471 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800472 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100473 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800474 *
475 * Returns 0 if access is OK, an error code otherwise
476 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100477 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800478 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000479static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800480{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700481 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800482
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300483 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200484
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700485 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100486}
Casey Schauflere114e472008-02-04 22:29:50 -0800487
David Howells5cd9c582008-08-14 11:37:28 +0100488/**
489 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
490 * @ptp: parent task pointer
491 *
492 * Returns 0 if access is OK, an error code otherwise
493 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100494 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100495 */
496static int smack_ptrace_traceme(struct task_struct *ptp)
497{
498 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700499 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100500
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100501 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200502
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200503 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800504 return rc;
505}
506
507/**
508 * smack_syslog - Smack approval on syslog
509 * @type: message type
510 *
Casey Schauflere114e472008-02-04 22:29:50 -0800511 * Returns 0 on success, error code otherwise.
512 */
Eric Paris12b30522010-11-15 18:36:29 -0500513static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800514{
Eric Paris12b30522010-11-15 18:36:29 -0500515 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700516 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800517
Casey Schaufler1880eff2012-06-05 15:28:30 -0700518 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800519 return 0;
520
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800521 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800522 rc = -EACCES;
523
524 return rc;
525}
526
527
528/*
529 * Superblock Hooks.
530 */
531
532/**
533 * smack_sb_alloc_security - allocate a superblock blob
534 * @sb: the superblock getting the blob
535 *
536 * Returns 0 on success or -ENOMEM on error.
537 */
538static int smack_sb_alloc_security(struct super_block *sb)
539{
540 struct superblock_smack *sbsp;
541
542 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
543
544 if (sbsp == NULL)
545 return -ENOMEM;
546
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200547 sbsp->smk_root = &smack_known_floor;
548 sbsp->smk_default = &smack_known_floor;
549 sbsp->smk_floor = &smack_known_floor;
550 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700551 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500552 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700553 */
Casey Schauflere114e472008-02-04 22:29:50 -0800554 sb->s_security = sbsp;
555
556 return 0;
557}
558
559/**
560 * smack_sb_free_security - free a superblock blob
561 * @sb: the superblock getting the blob
562 *
563 */
564static void smack_sb_free_security(struct super_block *sb)
565{
566 kfree(sb->s_security);
567 sb->s_security = NULL;
568}
569
Al Viro12085b12018-12-13 15:18:05 -0500570struct smack_mnt_opts {
571 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
572};
573
Al Viro204cc0c2018-12-13 13:41:47 -0500574static void smack_free_mnt_opts(void *mnt_opts)
575{
Al Viro12085b12018-12-13 15:18:05 -0500576 struct smack_mnt_opts *opts = mnt_opts;
577 kfree(opts->fsdefault);
578 kfree(opts->fsfloor);
579 kfree(opts->fshat);
580 kfree(opts->fsroot);
581 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500582 kfree(opts);
583}
584
Casey Schauflere114e472008-02-04 22:29:50 -0800585/**
586 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800587 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800588 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800589 *
590 * Returns 0 on success or -ENOMEM on error.
591 *
592 * Copy the Smack specific mount options out of the mount
593 * options list.
594 */
Eric Parise0007522008-03-05 10:31:54 -0500595static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800596{
597 char *cp, *commap, *otheropts, *dp;
598
Casey Schauflere114e472008-02-04 22:29:50 -0800599 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
600 if (otheropts == NULL)
601 return -ENOMEM;
602
603 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
604 if (strstr(cp, SMK_FSDEFAULT) == cp)
605 dp = smackopts;
606 else if (strstr(cp, SMK_FSFLOOR) == cp)
607 dp = smackopts;
608 else if (strstr(cp, SMK_FSHAT) == cp)
609 dp = smackopts;
610 else if (strstr(cp, SMK_FSROOT) == cp)
611 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700612 else if (strstr(cp, SMK_FSTRANS) == cp)
613 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800614 else
615 dp = otheropts;
616
617 commap = strchr(cp, ',');
618 if (commap != NULL)
619 *commap = '\0';
620
621 if (*dp != '\0')
622 strcat(dp, ",");
623 strcat(dp, cp);
624 }
625
626 strcpy(orig, otheropts);
627 free_page((unsigned long)otheropts);
628
629 return 0;
630}
631
Al Viro55c0e5b2018-12-16 01:09:45 -0500632static int smack_add_opt(int token, const char *s, void **mnt_opts)
633{
634 struct smack_mnt_opts *opts = *mnt_opts;
635
636 if (!opts) {
637 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
638 if (!opts)
639 return -ENOMEM;
640 *mnt_opts = opts;
641 }
642
643 if (!s)
644 return -ENOMEM;
645
646 switch (token) {
647 case Opt_fsdefault:
648 if (opts->fsdefault)
649 goto out_opt_err;
650 opts->fsdefault = s;
651 break;
652 case Opt_fsfloor:
653 if (opts->fsfloor)
654 goto out_opt_err;
655 opts->fsfloor = s;
656 break;
657 case Opt_fshat:
658 if (opts->fshat)
659 goto out_opt_err;
660 opts->fshat = s;
661 break;
662 case Opt_fsroot:
663 if (opts->fsroot)
664 goto out_opt_err;
665 opts->fsroot = s;
666 break;
667 case Opt_fstransmute:
668 if (opts->fstransmute)
669 goto out_opt_err;
670 opts->fstransmute = s;
671 break;
672 }
673 return 0;
674
675out_opt_err:
676 pr_warn("Smack: duplicate mount options\n");
677 return -EINVAL;
678}
679
Casey Schauflere114e472008-02-04 22:29:50 -0800680/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530681 * smack_parse_opts_str - parse Smack specific mount options
682 * @options: mount options string
683 * @opts: where to store converted mount opts
684 *
685 * Returns 0 on success or -ENOMEM on error.
686 *
687 * converts Smack specific mount options to generic security option format
688 */
689static int smack_parse_opts_str(char *options,
Al Viro204cc0c2018-12-13 13:41:47 -0500690 void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530691{
692 char *p;
Casey Schaufler3d04c922015-08-12 11:56:02 -0700693 int rc = -ENOMEM;
Casey Schaufler3d04c922015-08-12 11:56:02 -0700694 int token;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530695
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530696 if (!options)
697 return 0;
698
699 while ((p = strsep(&options, ",")) != NULL) {
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530700 substring_t args[MAX_OPT_ARGS];
Al Viro55c0e5b2018-12-16 01:09:45 -0500701 const char *arg;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530702
703 if (!*p)
704 continue;
705
Casey Schaufler3d04c922015-08-12 11:56:02 -0700706 token = match_token(p, smk_mount_tokens, args);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530707
Al Viro55c0e5b2018-12-16 01:09:45 -0500708 arg = match_strdup(&args[0]);
709 rc = smack_add_opt(token, arg, mnt_opts);
710 if (unlikely(rc)) {
711 kfree(arg);
712 if (*mnt_opts)
713 smack_free_mnt_opts(*mnt_opts);
714 *mnt_opts = NULL;
715 return rc;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530716 }
717 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530718 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530719}
720
Al Viro204cc0c2018-12-13 13:41:47 -0500721static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Al Viro5b400232018-12-12 20:13:29 -0500722{
723 char *s = (char *)get_zeroed_page(GFP_KERNEL);
724 int err;
725
726 if (!s)
727 return -ENOMEM;
728 err = smack_sb_copy_data(options, s);
729 if (!err)
Al Viro204cc0c2018-12-13 13:41:47 -0500730 err = smack_parse_opts_str(s, mnt_opts);
Al Viro5b400232018-12-12 20:13:29 -0500731 free_page((unsigned long)s);
732 return err;
733}
734
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530735/**
736 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800737 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530738 * @opts: Smack mount options
739 * @kern_flags: mount option from kernel space or user space
740 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800741 *
742 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530743 *
744 * Allow filesystems with binary mount data to explicitly set Smack mount
745 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800746 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530747static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500748 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530749 unsigned long kern_flags,
750 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800751{
752 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000753 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800754 struct superblock_smack *sp = sb->s_security;
755 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800756 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500757 struct smack_mnt_opts *opts = mnt_opts;
758 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800759
Seth Forshee9f50eda2015-09-23 15:16:06 -0500760 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800761 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700762
Himanshu Shukla2097f592016-11-10 16:19:52 +0530763 if (!smack_privileged(CAP_MAC_ADMIN)) {
764 /*
765 * Unprivileged mounts don't get to specify Smack values.
766 */
Al Viro12085b12018-12-13 15:18:05 -0500767 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530768 return -EPERM;
769 /*
770 * Unprivileged mounts get root and default from the caller.
771 */
772 skp = smk_of_current();
773 sp->smk_root = skp;
774 sp->smk_default = skp;
775 /*
776 * For a handful of fs types with no user-controlled
777 * backing store it's okay to trust security labels
778 * in the filesystem. The rest are untrusted.
779 */
780 if (sb->s_user_ns != &init_user_ns &&
781 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
782 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500783 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530784 sp->smk_flags |= SMK_SB_UNTRUSTED;
785 }
786 }
787
Seth Forshee9f50eda2015-09-23 15:16:06 -0500788 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800789
Al Viro12085b12018-12-13 15:18:05 -0500790 if (opts) {
791 if (opts->fsdefault) {
792 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200793 if (IS_ERR(skp))
794 return PTR_ERR(skp);
795 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500796 }
797 if (opts->fsfloor) {
798 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530799 if (IS_ERR(skp))
800 return PTR_ERR(skp);
801 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500802 }
803 if (opts->fshat) {
804 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530805 if (IS_ERR(skp))
806 return PTR_ERR(skp);
807 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500808 }
809 if (opts->fsroot) {
810 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200811 if (IS_ERR(skp))
812 return PTR_ERR(skp);
813 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500814 }
815 if (opts->fstransmute) {
816 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200817 if (IS_ERR(skp))
818 return PTR_ERR(skp);
819 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500820 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800821 }
822 }
823
824 /*
825 * Initialize the root inode.
826 */
827 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100828 if (isp == NULL) {
829 isp = new_inode_smack(sp->smk_root);
830 if (isp == NULL)
831 return -ENOMEM;
832 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700833 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800834 isp->smk_inode = sp->smk_root;
835
Casey Schauflere830b392013-05-22 18:43:07 -0700836 if (transmute)
837 isp->smk_flags |= SMK_INODE_TRANSMUTE;
838
Casey Schauflere114e472008-02-04 22:29:50 -0800839 return 0;
840}
841
842/**
843 * smack_sb_statfs - Smack check on statfs
844 * @dentry: identifies the file system in question
845 *
846 * Returns 0 if current can read the floor of the filesystem,
847 * and error code otherwise
848 */
849static int smack_sb_statfs(struct dentry *dentry)
850{
851 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200852 int rc;
853 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800854
Eric Parisa2694342011-04-25 13:10:27 -0400855 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200856 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
857
858 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700859 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200860 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800861}
862
Casey Schauflere114e472008-02-04 22:29:50 -0800863/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800864 * BPRM hooks
865 */
866
Casey Schauflerce8a4322011-09-29 18:21:01 -0700867/**
868 * smack_bprm_set_creds - set creds for exec
869 * @bprm: the exec information
870 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100871 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700872 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800873static int smack_bprm_set_creds(struct linux_binprm *bprm)
874{
Al Viro496ad9a2013-01-23 17:07:38 -0500875 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300876 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800877 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500878 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800879 int rc;
880
Kees Cookddb4a142017-07-18 15:25:23 -0700881 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800882 return 0;
883
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300884 isp = inode->i_security;
885 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800886 return 0;
887
Seth Forshee809c02e2016-04-26 14:36:22 -0500888 sbsp = inode->i_sb->s_security;
889 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
890 isp->smk_task != sbsp->smk_root)
891 return 0;
892
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300893 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100894 struct task_struct *tracer;
895 rc = 0;
896
897 rcu_read_lock();
898 tracer = ptrace_parent(current);
899 if (likely(tracer != NULL))
900 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200901 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100902 PTRACE_MODE_ATTACH,
903 __func__);
904 rcu_read_unlock();
905
906 if (rc != 0)
907 return rc;
908 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300909 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800910
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300911 bsp->smk_task = isp->smk_task;
912 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800913
Kees Cookccbb6e12017-07-18 15:25:26 -0700914 /* Decide if this is a secure exec. */
915 if (bsp->smk_task != bsp->smk_forked)
916 bprm->secureexec = 1;
917
Casey Schaufler676dac42010-12-02 06:43:39 -0800918 return 0;
919}
920
921/*
Casey Schauflere114e472008-02-04 22:29:50 -0800922 * Inode hooks
923 */
924
925/**
926 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800927 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800928 *
929 * Returns 0 if it gets a blob, -ENOMEM otherwise
930 */
931static int smack_inode_alloc_security(struct inode *inode)
932{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700933 struct smack_known *skp = smk_of_current();
934
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200935 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800936 if (inode->i_security == NULL)
937 return -ENOMEM;
938 return 0;
939}
940
941/**
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530942 * smack_inode_free_rcu - Free inode_smack blob from cache
943 * @head: the rcu_head for getting inode_smack pointer
944 *
945 * Call back function called from call_rcu() to free
946 * the i_security blob pointer in inode
947 */
948static void smack_inode_free_rcu(struct rcu_head *head)
949{
950 struct inode_smack *issp;
951
952 issp = container_of(head, struct inode_smack, smk_rcu);
953 kmem_cache_free(smack_inode_cache, issp);
954}
955
956/**
957 * smack_inode_free_security - free an inode blob using call_rcu()
Randy Dunlap251a2a92009-02-18 11:42:33 -0800958 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800959 *
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530960 * Clears the blob pointer in inode using RCU
Casey Schauflere114e472008-02-04 22:29:50 -0800961 */
962static void smack_inode_free_security(struct inode *inode)
963{
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530964 struct inode_smack *issp = inode->i_security;
965
966 /*
967 * The inode may still be referenced in a path walk and
968 * a call to smack_inode_permission() can be made
969 * after smack_inode_free_security() is called.
970 * To avoid race condition free the i_security via RCU
971 * and leave the current inode->i_security pointer intact.
972 * The inode will be freed after the RCU grace period too.
973 */
974 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
Casey Schauflere114e472008-02-04 22:29:50 -0800975}
976
977/**
978 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200979 * @inode: the newly created inode
980 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500981 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800982 * @name: where to put the attribute name
983 * @value: where to put the attribute value
984 * @len: where to put the length of the attribute
985 *
986 * Returns 0 if it all works out, -ENOMEM if there's no memory
987 */
988static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900989 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500990 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800991{
Casey Schaufler2267b132012-03-13 19:14:19 -0700992 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700993 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200994 struct smack_known *isp = smk_of_inode(inode);
995 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800996 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800997
Tetsuo Handa95489062013-07-25 05:44:02 +0900998 if (name)
999 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -08001000
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001001 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001002 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001003 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1004 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001005 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001006
1007 /*
1008 * If the access rule allows transmutation and
1009 * the directory requests transmutation then
1010 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001011 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001012 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001013 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001014 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001015 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001016 issp->smk_flags |= SMK_INODE_CHANGED;
1017 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001018
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001019 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001020 if (*value == NULL)
1021 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001022
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001023 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001024 }
Casey Schauflere114e472008-02-04 22:29:50 -08001025
1026 return 0;
1027}
1028
1029/**
1030 * smack_inode_link - Smack check on link
1031 * @old_dentry: the existing object
1032 * @dir: unused
1033 * @new_dentry: the new object
1034 *
1035 * Returns 0 if access is permitted, an error code otherwise
1036 */
1037static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1038 struct dentry *new_dentry)
1039{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001040 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001041 struct smk_audit_info ad;
1042 int rc;
1043
Eric Parisa2694342011-04-25 13:10:27 -04001044 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001045 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001046
David Howellsc6f493d2015-03-17 22:26:22 +00001047 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001048 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001049 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001050
David Howells88025652015-01-29 12:02:32 +00001051 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001052 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001053 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1054 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001055 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001056 }
1057
1058 return rc;
1059}
1060
1061/**
1062 * smack_inode_unlink - Smack check on inode deletion
1063 * @dir: containing directory object
1064 * @dentry: file to unlink
1065 *
1066 * Returns 0 if current can write the containing directory
1067 * and the object, error code otherwise
1068 */
1069static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1070{
David Howellsc6f493d2015-03-17 22:26:22 +00001071 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001072 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001073 int rc;
1074
Eric Parisa2694342011-04-25 13:10:27 -04001075 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001076 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1077
Casey Schauflere114e472008-02-04 22:29:50 -08001078 /*
1079 * You need write access to the thing you're unlinking
1080 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001081 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001082 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001083 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001084 /*
1085 * You also need write access to the containing directory
1086 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001087 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001088 smk_ad_setfield_u_fs_inode(&ad, dir);
1089 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001090 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001091 }
Casey Schauflere114e472008-02-04 22:29:50 -08001092 return rc;
1093}
1094
1095/**
1096 * smack_inode_rmdir - Smack check on directory deletion
1097 * @dir: containing directory object
1098 * @dentry: directory to unlink
1099 *
1100 * Returns 0 if current can write the containing directory
1101 * and the directory, error code otherwise
1102 */
1103static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1104{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001105 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001106 int rc;
1107
Eric Parisa2694342011-04-25 13:10:27 -04001108 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001109 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1110
Casey Schauflere114e472008-02-04 22:29:50 -08001111 /*
1112 * You need write access to the thing you're removing
1113 */
David Howellsc6f493d2015-03-17 22:26:22 +00001114 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1115 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001116 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001117 /*
1118 * You also need write access to the containing directory
1119 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001120 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001121 smk_ad_setfield_u_fs_inode(&ad, dir);
1122 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001123 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001124 }
Casey Schauflere114e472008-02-04 22:29:50 -08001125
1126 return rc;
1127}
1128
1129/**
1130 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001131 * @old_inode: unused
1132 * @old_dentry: the old object
1133 * @new_inode: unused
1134 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001135 *
1136 * Read and write access is required on both the old and
1137 * new directories.
1138 *
1139 * Returns 0 if access is permitted, an error code otherwise
1140 */
1141static int smack_inode_rename(struct inode *old_inode,
1142 struct dentry *old_dentry,
1143 struct inode *new_inode,
1144 struct dentry *new_dentry)
1145{
1146 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001147 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 struct smk_audit_info ad;
1149
Eric Parisa2694342011-04-25 13:10:27 -04001150 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001151 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001152
David Howellsc6f493d2015-03-17 22:26:22 +00001153 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001154 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001155 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001156
David Howells88025652015-01-29 12:02:32 +00001157 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001158 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001159 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1160 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001161 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001162 }
Casey Schauflere114e472008-02-04 22:29:50 -08001163 return rc;
1164}
1165
1166/**
1167 * smack_inode_permission - Smack version of permission()
1168 * @inode: the inode in question
1169 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001170 *
1171 * This is the important Smack hook.
1172 *
1173 * Returns 0 if access is permitted, -EACCES otherwise
1174 */
Al Viroe74f71e2011-06-20 19:38:15 -04001175static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001176{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001177 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001178 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001179 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001180 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001181
1182 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001183 /*
1184 * No permission to check. Existence test. Yup, it's there.
1185 */
1186 if (mask == 0)
1187 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001188
Seth Forshee9f50eda2015-09-23 15:16:06 -05001189 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1190 if (smk_of_inode(inode) != sbsp->smk_root)
1191 return -EACCES;
1192 }
1193
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001194 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001195 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001196 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001197 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001198 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001199 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1200 rc = smk_bu_inode(inode, mask, rc);
1201 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001202}
1203
1204/**
1205 * smack_inode_setattr - Smack check for setting attributes
1206 * @dentry: the object
1207 * @iattr: for the force flag
1208 *
1209 * Returns 0 if access is permitted, an error code otherwise
1210 */
1211static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1212{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001213 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001214 int rc;
1215
Casey Schauflere114e472008-02-04 22:29:50 -08001216 /*
1217 * Need to allow for clearing the setuid bit.
1218 */
1219 if (iattr->ia_valid & ATTR_FORCE)
1220 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001221 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001222 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001223
David Howellsc6f493d2015-03-17 22:26:22 +00001224 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1225 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001226 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001227}
1228
1229/**
1230 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001231 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001232 * @dentry: the object
1233 *
1234 * Returns 0 if access is permitted, an error code otherwise
1235 */
Al Viro3f7036a2015-03-08 19:28:30 -04001236static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001237{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001238 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001239 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001240 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001241
Eric Parisf48b7392011-04-25 12:54:27 -04001242 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001243 smk_ad_setfield_u_fs_path(&ad, *path);
1244 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1245 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001246 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001247}
1248
1249/**
1250 * smack_inode_setxattr - Smack check for setting xattrs
1251 * @dentry: the object
1252 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001253 * @value: value of the attribute
1254 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001255 * @flags: unused
1256 *
1257 * This protects the Smack attribute explicitly.
1258 *
1259 * Returns 0 if access is permitted, an error code otherwise
1260 */
David Howells8f0cfa52008-04-29 00:59:41 -07001261static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1262 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001263{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001264 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001265 struct smack_known *skp;
1266 int check_priv = 0;
1267 int check_import = 0;
1268 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001269 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001270
Casey Schaufler19760ad2013-12-16 16:27:26 -08001271 /*
1272 * Check label validity here so import won't fail in post_setxattr
1273 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001274 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1275 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001276 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1277 check_priv = 1;
1278 check_import = 1;
1279 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1280 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1281 check_priv = 1;
1282 check_import = 1;
1283 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001284 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001285 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001286 if (size != TRANS_TRUE_SIZE ||
1287 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1288 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001289 } else
1290 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1291
Casey Schaufler19760ad2013-12-16 16:27:26 -08001292 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1293 rc = -EPERM;
1294
1295 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001296 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001297 if (IS_ERR(skp))
1298 rc = PTR_ERR(skp);
1299 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001300 (skp == &smack_known_star || skp == &smack_known_web)))
1301 rc = -EINVAL;
1302 }
1303
Eric Parisa2694342011-04-25 13:10:27 -04001304 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001305 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1306
Casey Schauflerd166c802014-08-27 14:51:27 -07001307 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001308 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1309 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001310 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001311
1312 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001313}
1314
1315/**
1316 * smack_inode_post_setxattr - Apply the Smack update approved above
1317 * @dentry: object
1318 * @name: attribute name
1319 * @value: attribute value
1320 * @size: attribute size
1321 * @flags: unused
1322 *
1323 * Set the pointer in the inode blob to the entry found
1324 * in the master label list.
1325 */
David Howells8f0cfa52008-04-29 00:59:41 -07001326static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1327 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001328{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001329 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001330 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001331
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001332 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1333 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1334 return;
1335 }
1336
Casey Schaufler676dac42010-12-02 06:43:39 -08001337 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001338 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001339 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001340 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001341 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001342 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001343 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001344 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001345 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001346 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001347 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001348 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001349 }
Casey Schauflere114e472008-02-04 22:29:50 -08001350
1351 return;
1352}
1353
Casey Schauflerce8a4322011-09-29 18:21:01 -07001354/**
Casey Schauflere114e472008-02-04 22:29:50 -08001355 * smack_inode_getxattr - Smack check on getxattr
1356 * @dentry: the object
1357 * @name: unused
1358 *
1359 * Returns 0 if access is permitted, an error code otherwise
1360 */
David Howells8f0cfa52008-04-29 00:59:41 -07001361static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001362{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001363 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001364 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001365
Eric Parisa2694342011-04-25 13:10:27 -04001366 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001367 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1368
David Howellsc6f493d2015-03-17 22:26:22 +00001369 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1370 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001371 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001372}
1373
Casey Schauflerce8a4322011-09-29 18:21:01 -07001374/**
Casey Schauflere114e472008-02-04 22:29:50 -08001375 * smack_inode_removexattr - Smack check on removexattr
1376 * @dentry: the object
1377 * @name: name of the attribute
1378 *
1379 * Removing the Smack attribute requires CAP_MAC_ADMIN
1380 *
1381 * Returns 0 if access is permitted, an error code otherwise
1382 */
David Howells8f0cfa52008-04-29 00:59:41 -07001383static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001384{
Casey Schaufler676dac42010-12-02 06:43:39 -08001385 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001386 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001387 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001388
Casey Schauflerbcdca222008-02-23 15:24:04 -08001389 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1390 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001391 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001392 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001393 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301394 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001395 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001396 rc = -EPERM;
1397 } else
1398 rc = cap_inode_removexattr(dentry, name);
1399
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001400 if (rc != 0)
1401 return rc;
1402
Eric Parisa2694342011-04-25 13:10:27 -04001403 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001404 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001405
David Howellsc6f493d2015-03-17 22:26:22 +00001406 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1407 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001408 if (rc != 0)
1409 return rc;
1410
David Howellsc6f493d2015-03-17 22:26:22 +00001411 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001412 /*
1413 * Don't do anything special for these.
1414 * XATTR_NAME_SMACKIPIN
1415 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001416 */
José Bollo80124952016-01-12 21:23:40 +01001417 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001418 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001419 struct superblock_smack *sbsp = sbp->s_security;
1420
1421 isp->smk_inode = sbsp->smk_default;
1422 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001423 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001424 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001425 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001426 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1427 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001428
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001429 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001430}
1431
1432/**
1433 * smack_inode_getsecurity - get smack xattrs
1434 * @inode: the object
1435 * @name: attribute name
1436 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001437 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001438 *
1439 * Returns the size of the attribute or an error code
1440 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001441static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001442 const char *name, void **buffer,
1443 bool alloc)
1444{
1445 struct socket_smack *ssp;
1446 struct socket *sock;
1447 struct super_block *sbp;
1448 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001449 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001450
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001451 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001452 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001453 else {
1454 /*
1455 * The rest of the Smack xattrs are only on sockets.
1456 */
1457 sbp = ip->i_sb;
1458 if (sbp->s_magic != SOCKFS_MAGIC)
1459 return -EOPNOTSUPP;
1460
1461 sock = SOCKET_I(ip);
1462 if (sock == NULL || sock->sk == NULL)
1463 return -EOPNOTSUPP;
1464
1465 ssp = sock->sk->sk_security;
1466
1467 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1468 isp = ssp->smk_in;
1469 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1470 isp = ssp->smk_out;
1471 else
1472 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001473 }
1474
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001475 if (alloc) {
1476 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1477 if (*buffer == NULL)
1478 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001479 }
1480
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001481 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001482}
1483
1484
1485/**
1486 * smack_inode_listsecurity - list the Smack attributes
1487 * @inode: the object
1488 * @buffer: where they go
1489 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001490 */
1491static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1492 size_t buffer_size)
1493{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001494 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001495
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001496 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001497 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001498
1499 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001500}
1501
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001502/**
1503 * smack_inode_getsecid - Extract inode's security id
1504 * @inode: inode to extract the info from
1505 * @secid: where result will be saved
1506 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001507static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001508{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001509 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001510
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001511 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001512}
1513
Casey Schauflere114e472008-02-04 22:29:50 -08001514/*
1515 * File Hooks
1516 */
1517
Casey Schaufler491a0b02016-01-26 15:08:35 -08001518/*
1519 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001520 *
1521 * Should access checks be done on each read or write?
1522 * UNICOS and SELinux say yes.
1523 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1524 *
1525 * I'll say no for now. Smack does not do the frequent
1526 * label changing that SELinux does.
1527 */
Casey Schauflere114e472008-02-04 22:29:50 -08001528
1529/**
1530 * smack_file_alloc_security - assign a file security blob
1531 * @file: the object
1532 *
1533 * The security blob for a file is a pointer to the master
1534 * label list, so no allocation is done.
1535 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001536 * f_security is the owner security information. It
1537 * isn't used on file access checks, it's for send_sigio.
1538 *
Casey Schauflere114e472008-02-04 22:29:50 -08001539 * Returns 0
1540 */
1541static int smack_file_alloc_security(struct file *file)
1542{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001543 struct smack_known *skp = smk_of_current();
1544
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001545 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001546 return 0;
1547}
1548
1549/**
1550 * smack_file_free_security - clear a file security blob
1551 * @file: the object
1552 *
1553 * The security blob for a file is a pointer to the master
1554 * label list, so no memory is freed.
1555 */
1556static void smack_file_free_security(struct file *file)
1557{
1558 file->f_security = NULL;
1559}
1560
1561/**
1562 * smack_file_ioctl - Smack check on ioctls
1563 * @file: the object
1564 * @cmd: what to do
1565 * @arg: unused
1566 *
1567 * Relies heavily on the correct use of the ioctl command conventions.
1568 *
1569 * Returns 0 if allowed, error code otherwise
1570 */
1571static int smack_file_ioctl(struct file *file, unsigned int cmd,
1572 unsigned long arg)
1573{
1574 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001575 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001576 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001577
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001578 if (unlikely(IS_PRIVATE(inode)))
1579 return 0;
1580
Eric Parisf48b7392011-04-25 12:54:27 -04001581 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001582 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001583
Casey Schauflerd166c802014-08-27 14:51:27 -07001584 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001585 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001586 rc = smk_bu_file(file, MAY_WRITE, rc);
1587 }
Casey Schauflere114e472008-02-04 22:29:50 -08001588
Casey Schauflerd166c802014-08-27 14:51:27 -07001589 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001590 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001591 rc = smk_bu_file(file, MAY_READ, rc);
1592 }
Casey Schauflere114e472008-02-04 22:29:50 -08001593
1594 return rc;
1595}
1596
1597/**
1598 * smack_file_lock - Smack check on file locking
1599 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001600 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001601 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001602 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001603 */
1604static int smack_file_lock(struct file *file, unsigned int cmd)
1605{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001606 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001607 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001608 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001609
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001610 if (unlikely(IS_PRIVATE(inode)))
1611 return 0;
1612
Eric Paris92f42502011-04-25 13:15:55 -04001613 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1614 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001615 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001616 rc = smk_bu_file(file, MAY_LOCK, rc);
1617 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001618}
1619
1620/**
1621 * smack_file_fcntl - Smack check on fcntl
1622 * @file: the object
1623 * @cmd: what action to check
1624 * @arg: unused
1625 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001626 * Generally these operations are harmless.
1627 * File locking operations present an obvious mechanism
1628 * for passing information, so they require write access.
1629 *
Casey Schauflere114e472008-02-04 22:29:50 -08001630 * Returns 0 if current has access, error code otherwise
1631 */
1632static int smack_file_fcntl(struct file *file, unsigned int cmd,
1633 unsigned long arg)
1634{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001635 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001636 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001637 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001638
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001639 if (unlikely(IS_PRIVATE(inode)))
1640 return 0;
1641
Casey Schauflere114e472008-02-04 22:29:50 -08001642 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001643 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001644 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001645 case F_SETLK:
1646 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001647 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1648 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001649 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001650 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001651 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001652 case F_SETOWN:
1653 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001654 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1655 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001656 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001657 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001658 break;
1659 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001660 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001661 }
1662
1663 return rc;
1664}
1665
1666/**
Al Viroe5467852012-05-30 13:30:51 -04001667 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001668 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1669 * if mapping anonymous memory.
1670 * @file contains the file structure for file to map (may be NULL).
1671 * @reqprot contains the protection requested by the application.
1672 * @prot contains the protection that will be applied by the kernel.
1673 * @flags contains the operational flags.
1674 * Return 0 if permission is granted.
1675 */
Al Viroe5467852012-05-30 13:30:51 -04001676static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001677 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001678 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001679{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001680 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001681 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001682 struct smack_rule *srp;
1683 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001684 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001685 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001686 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001687 int may;
1688 int mmay;
1689 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001690 int rc;
1691
Al Viro496ad9a2013-01-23 17:07:38 -05001692 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001693 return 0;
1694
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001695 if (unlikely(IS_PRIVATE(file_inode(file))))
1696 return 0;
1697
Al Viro496ad9a2013-01-23 17:07:38 -05001698 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001699 if (isp->smk_mmap == NULL)
1700 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001701 sbsp = file_inode(file)->i_sb->s_security;
1702 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1703 isp->smk_mmap != sbsp->smk_root)
1704 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001705 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001706
1707 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001708 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001709 rc = 0;
1710
1711 rcu_read_lock();
1712 /*
1713 * For each Smack rule associated with the subject
1714 * label verify that the SMACK64MMAP also has access
1715 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001716 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001717 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001718 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001719 /*
1720 * Matching labels always allows access.
1721 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001722 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001723 continue;
1724 /*
1725 * If there is a matching local rule take
1726 * that into account as well.
1727 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001728 may = smk_access_entry(srp->smk_subject->smk_known,
1729 okp->smk_known,
1730 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001731 if (may == -ENOENT)
1732 may = srp->smk_access;
1733 else
1734 may &= srp->smk_access;
1735 /*
1736 * If may is zero the SMACK64MMAP subject can't
1737 * possibly have less access.
1738 */
1739 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001740 continue;
1741
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001742 /*
1743 * Fetch the global list entry.
1744 * If there isn't one a SMACK64MMAP subject
1745 * can't have as much access as current.
1746 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001747 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1748 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001749 if (mmay == -ENOENT) {
1750 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001751 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001752 }
1753 /*
1754 * If there is a local entry it modifies the
1755 * potential access, too.
1756 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001757 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1758 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001759 if (tmay != -ENOENT)
1760 mmay &= tmay;
1761
1762 /*
1763 * If there is any access available to current that is
1764 * not available to a SMACK64MMAP subject
1765 * deny access.
1766 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001767 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001768 rc = -EACCES;
1769 break;
1770 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001771 }
1772
1773 rcu_read_unlock();
1774
1775 return rc;
1776}
1777
1778/**
Casey Schauflere114e472008-02-04 22:29:50 -08001779 * smack_file_set_fowner - set the file security blob value
1780 * @file: object in question
1781 *
Casey Schauflere114e472008-02-04 22:29:50 -08001782 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001783static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001784{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001785 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001786}
1787
1788/**
1789 * smack_file_send_sigiotask - Smack on sigio
1790 * @tsk: The target task
1791 * @fown: the object the signal come from
1792 * @signum: unused
1793 *
1794 * Allow a privileged task to get signals even if it shouldn't
1795 *
1796 * Returns 0 if a subject with the object's smack could
1797 * write to the task, an error code otherwise.
1798 */
1799static int smack_file_send_sigiotask(struct task_struct *tsk,
1800 struct fown_struct *fown, int signum)
1801{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001802 struct smack_known *skp;
1803 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001804 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001805 struct file *file;
1806 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001807 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001808
1809 /*
1810 * struct fown_struct is never outside the context of a struct file
1811 */
1812 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001813
Etienne Bassetecfcc532009-04-08 20:40:06 +02001814 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001815 skp = file->f_security;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001816 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1817 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001818
1819 rcu_read_lock();
1820 tcred = __task_cred(tsk);
1821 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001822 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001823 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001824
1825 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1826 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001827 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001828 return rc;
1829}
1830
1831/**
1832 * smack_file_receive - Smack file receive check
1833 * @file: the object
1834 *
1835 * Returns 0 if current has access, error code otherwise
1836 */
1837static int smack_file_receive(struct file *file)
1838{
Casey Schauflerd166c802014-08-27 14:51:27 -07001839 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001840 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001841 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001842 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001843 struct socket *sock;
1844 struct task_smack *tsp;
1845 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001846
Seung-Woo Kim97775822015-04-17 15:25:04 +09001847 if (unlikely(IS_PRIVATE(inode)))
1848 return 0;
1849
Casey Schaufler4482a442013-12-30 17:37:45 -08001850 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001851 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001852
Casey Schaufler51d59af2017-05-31 08:53:42 -07001853 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001854 sock = SOCKET_I(inode);
1855 ssp = sock->sk->sk_security;
1856 tsp = current_security();
1857 /*
1858 * If the receiving process can't write to the
1859 * passed socket or if the passed socket can't
1860 * write to the receiving process don't accept
1861 * the passed socket.
1862 */
1863 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1864 rc = smk_bu_file(file, may, rc);
1865 if (rc < 0)
1866 return rc;
1867 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1868 rc = smk_bu_file(file, may, rc);
1869 return rc;
1870 }
Casey Schauflere114e472008-02-04 22:29:50 -08001871 /*
1872 * This code relies on bitmasks.
1873 */
1874 if (file->f_mode & FMODE_READ)
1875 may = MAY_READ;
1876 if (file->f_mode & FMODE_WRITE)
1877 may |= MAY_WRITE;
1878
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001879 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001880 rc = smk_bu_file(file, may, rc);
1881 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001882}
1883
Casey Schaufler531f1d42011-09-19 12:41:42 -07001884/**
Eric Paris83d49852012-04-04 13:45:40 -04001885 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001886 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001887 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001888 *
1889 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001890 * Allow the open only if the task has read access. There are
1891 * many read operations (e.g. fstat) that you can do with an
1892 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001893 *
1894 * Returns 0
1895 */
Al Viro94817692018-07-10 14:13:18 -04001896static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001897{
Al Viro94817692018-07-10 14:13:18 -04001898 struct task_smack *tsp = file->f_cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001899 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001900 struct smk_audit_info ad;
1901 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001902
Casey Schauflera6834c02014-04-21 11:10:26 -07001903 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1904 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301905 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001906 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001907
1908 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001909}
1910
Casey Schauflere114e472008-02-04 22:29:50 -08001911/*
1912 * Task hooks
1913 */
1914
1915/**
David Howellsee18d642009-09-02 09:14:21 +01001916 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1917 * @new: the new credentials
1918 * @gfp: the atomicity of any memory allocations
1919 *
1920 * Prepare a blank set of credentials for modification. This must allocate all
1921 * the memory the LSM module might require such that cred_transfer() can
1922 * complete without error.
1923 */
1924static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1925{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001926 struct task_smack *tsp;
1927
1928 tsp = new_task_smack(NULL, NULL, gfp);
1929 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001930 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001931
1932 cred->security = tsp;
1933
David Howellsee18d642009-09-02 09:14:21 +01001934 return 0;
1935}
1936
1937
1938/**
David Howellsf1752ee2008-11-14 10:39:17 +11001939 * smack_cred_free - "free" task-level security credentials
1940 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001941 *
Casey Schauflere114e472008-02-04 22:29:50 -08001942 */
David Howellsf1752ee2008-11-14 10:39:17 +11001943static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001944{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001945 struct task_smack *tsp = cred->security;
1946 struct smack_rule *rp;
1947 struct list_head *l;
1948 struct list_head *n;
1949
1950 if (tsp == NULL)
1951 return;
1952 cred->security = NULL;
1953
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001954 smk_destroy_label_list(&tsp->smk_relabel);
1955
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001956 list_for_each_safe(l, n, &tsp->smk_rules) {
1957 rp = list_entry(l, struct smack_rule, list);
1958 list_del(&rp->list);
1959 kfree(rp);
1960 }
1961 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001962}
1963
1964/**
David Howellsd84f4f92008-11-14 10:39:23 +11001965 * smack_cred_prepare - prepare new set of credentials for modification
1966 * @new: the new credentials
1967 * @old: the original credentials
1968 * @gfp: the atomicity of any memory allocations
1969 *
1970 * Prepare a new set of credentials for modification.
1971 */
1972static int smack_cred_prepare(struct cred *new, const struct cred *old,
1973 gfp_t gfp)
1974{
Casey Schaufler676dac42010-12-02 06:43:39 -08001975 struct task_smack *old_tsp = old->security;
1976 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001977 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001978
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001979 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001980 if (new_tsp == NULL)
1981 return -ENOMEM;
1982
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301983 new->security = new_tsp;
1984
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001985 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1986 if (rc != 0)
1987 return rc;
1988
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001989 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1990 gfp);
1991 if (rc != 0)
1992 return rc;
1993
David Howellsd84f4f92008-11-14 10:39:23 +11001994 return 0;
1995}
1996
Randy Dunlap251a2a92009-02-18 11:42:33 -08001997/**
David Howellsee18d642009-09-02 09:14:21 +01001998 * smack_cred_transfer - Transfer the old credentials to the new credentials
1999 * @new: the new credentials
2000 * @old: the original credentials
2001 *
2002 * Fill in a set of blank credentials from another set of credentials.
2003 */
2004static void smack_cred_transfer(struct cred *new, const struct cred *old)
2005{
Casey Schaufler676dac42010-12-02 06:43:39 -08002006 struct task_smack *old_tsp = old->security;
2007 struct task_smack *new_tsp = new->security;
2008
2009 new_tsp->smk_task = old_tsp->smk_task;
2010 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002011 mutex_init(&new_tsp->smk_rules_lock);
2012 INIT_LIST_HEAD(&new_tsp->smk_rules);
2013
2014
2015 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01002016}
2017
2018/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08002019 * smack_cred_getsecid - get the secid corresponding to a creds structure
2020 * @c: the object creds
2021 * @secid: where to put the result
2022 *
2023 * Sets the secid to contain a u32 version of the smack label.
2024 */
2025static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2026{
2027 struct smack_known *skp;
2028
2029 rcu_read_lock();
2030 skp = smk_of_task(c->security);
2031 *secid = skp->smk_secid;
2032 rcu_read_unlock();
2033}
2034
2035/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002036 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002037 * @new: points to the set of credentials to be modified.
2038 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002039 *
2040 * Set the security data for a kernel service.
2041 */
2042static int smack_kernel_act_as(struct cred *new, u32 secid)
2043{
Casey Schaufler676dac42010-12-02 06:43:39 -08002044 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002045
Casey Schaufler152f91d2016-11-14 09:38:15 -08002046 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002047 return 0;
2048}
2049
2050/**
2051 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002052 * @new: points to the set of credentials to be modified
2053 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002054 *
2055 * Set the file creation context in a set of credentials to the same
2056 * as the objective context of the specified inode
2057 */
2058static int smack_kernel_create_files_as(struct cred *new,
2059 struct inode *inode)
2060{
2061 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002062 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002063
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002064 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002065 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002066 return 0;
2067}
2068
2069/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002070 * smk_curacc_on_task - helper to log task related access
2071 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002072 * @access: the access requested
2073 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002074 *
2075 * Return 0 if access is permitted
2076 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002077static int smk_curacc_on_task(struct task_struct *p, int access,
2078 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002079{
2080 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002081 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002082 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002083
Casey Schaufler531f1d42011-09-19 12:41:42 -07002084 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002085 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002086 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002087 rc = smk_bu_task(p, access, rc);
2088 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002089}
2090
2091/**
Casey Schauflere114e472008-02-04 22:29:50 -08002092 * smack_task_setpgid - Smack check on setting pgid
2093 * @p: the task object
2094 * @pgid: unused
2095 *
2096 * Return 0 if write access is permitted
2097 */
2098static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2099{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002100 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002101}
2102
2103/**
2104 * smack_task_getpgid - Smack access check for getpgid
2105 * @p: the object task
2106 *
2107 * Returns 0 if current can read the object task, error code otherwise
2108 */
2109static int smack_task_getpgid(struct task_struct *p)
2110{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002111 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002112}
2113
2114/**
2115 * smack_task_getsid - Smack access check for getsid
2116 * @p: the object task
2117 *
2118 * Returns 0 if current can read the object task, error code otherwise
2119 */
2120static int smack_task_getsid(struct task_struct *p)
2121{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002122 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002123}
2124
2125/**
2126 * smack_task_getsecid - get the secid of the task
2127 * @p: the object task
2128 * @secid: where to put the result
2129 *
2130 * Sets the secid to contain a u32 version of the smack label.
2131 */
2132static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2133{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002134 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002135
2136 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002137}
2138
2139/**
2140 * smack_task_setnice - Smack check on setting nice
2141 * @p: the task object
2142 * @nice: unused
2143 *
2144 * Return 0 if write access is permitted
2145 */
2146static int smack_task_setnice(struct task_struct *p, int nice)
2147{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002148 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002149}
2150
2151/**
2152 * smack_task_setioprio - Smack check on setting ioprio
2153 * @p: the task object
2154 * @ioprio: unused
2155 *
2156 * Return 0 if write access is permitted
2157 */
2158static int smack_task_setioprio(struct task_struct *p, int ioprio)
2159{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002160 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002161}
2162
2163/**
2164 * smack_task_getioprio - Smack check on reading ioprio
2165 * @p: the task object
2166 *
2167 * Return 0 if read access is permitted
2168 */
2169static int smack_task_getioprio(struct task_struct *p)
2170{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002171 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002172}
2173
2174/**
2175 * smack_task_setscheduler - Smack check on setting scheduler
2176 * @p: the task object
2177 * @policy: unused
2178 * @lp: unused
2179 *
2180 * Return 0 if read access is permitted
2181 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002182static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002183{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002184 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002185}
2186
2187/**
2188 * smack_task_getscheduler - Smack check on reading scheduler
2189 * @p: the task object
2190 *
2191 * Return 0 if read access is permitted
2192 */
2193static int smack_task_getscheduler(struct task_struct *p)
2194{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002195 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002196}
2197
2198/**
2199 * smack_task_movememory - Smack check on moving memory
2200 * @p: the task object
2201 *
2202 * Return 0 if write access is permitted
2203 */
2204static int smack_task_movememory(struct task_struct *p)
2205{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002206 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002207}
2208
2209/**
2210 * smack_task_kill - Smack check on signal delivery
2211 * @p: the task object
2212 * @info: unused
2213 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002214 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002215 *
2216 * Return 0 if write access is permitted
2217 *
Casey Schauflere114e472008-02-04 22:29:50 -08002218 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002219static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002220 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002221{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002222 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002223 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002224 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002225 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002226
Rafal Krypa18d872f2016-04-04 11:14:53 +02002227 if (!sig)
2228 return 0; /* null signal; existence test */
2229
Etienne Bassetecfcc532009-04-08 20:40:06 +02002230 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2231 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002232 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002233 * Sending a signal requires that the sender
2234 * can write the receiver.
2235 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002236 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002237 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2238 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002239 return rc;
2240 }
Casey Schauflere114e472008-02-04 22:29:50 -08002241 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002242 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002243 * specific behavior. This is not clean. For one thing
2244 * we can't take privilege into account.
2245 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002246 skp = smk_of_task(cred->security);
Casey Schauflerc60b9062016-08-30 10:31:39 -07002247 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2248 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002249 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002250}
2251
2252/**
Casey Schauflere114e472008-02-04 22:29:50 -08002253 * smack_task_to_inode - copy task smack into the inode blob
2254 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002255 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002256 *
2257 * Sets the smack pointer in the inode security blob
2258 */
2259static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2260{
2261 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002262 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002263
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002264 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002265 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002266}
2267
2268/*
2269 * Socket hooks.
2270 */
2271
2272/**
2273 * smack_sk_alloc_security - Allocate a socket blob
2274 * @sk: the socket
2275 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002276 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002277 *
2278 * Assign Smack pointers to current
2279 *
2280 * Returns 0 on success, -ENOMEM is there's no memory
2281 */
2282static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2283{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002284 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002285 struct socket_smack *ssp;
2286
2287 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2288 if (ssp == NULL)
2289 return -ENOMEM;
2290
jooseong lee08382c92016-11-03 11:54:39 +01002291 /*
2292 * Sockets created by kernel threads receive web label.
2293 */
2294 if (unlikely(current->flags & PF_KTHREAD)) {
2295 ssp->smk_in = &smack_known_web;
2296 ssp->smk_out = &smack_known_web;
2297 } else {
2298 ssp->smk_in = skp;
2299 ssp->smk_out = skp;
2300 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002301 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002302
2303 sk->sk_security = ssp;
2304
2305 return 0;
2306}
2307
2308/**
2309 * smack_sk_free_security - Free a socket blob
2310 * @sk: the socket
2311 *
2312 * Clears the blob pointer
2313 */
2314static void smack_sk_free_security(struct sock *sk)
2315{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302316#ifdef SMACK_IPV6_PORT_LABELING
2317 struct smk_port_label *spp;
2318
2319 if (sk->sk_family == PF_INET6) {
2320 rcu_read_lock();
2321 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2322 if (spp->smk_sock != sk)
2323 continue;
2324 spp->smk_can_reuse = 1;
2325 break;
2326 }
2327 rcu_read_unlock();
2328 }
2329#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002330 kfree(sk->sk_security);
2331}
2332
2333/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002334* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002335* @sip: the object end
2336*
2337* looks for host based access restrictions
2338*
2339* This version will only be appropriate for really small sets of single label
2340* hosts. The caller is responsible for ensuring that the RCU read lock is
2341* taken before calling this function.
2342*
2343* Returns the label of the far end or NULL if it's not special.
2344*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002345static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002346{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002347 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002348 struct in_addr *siap = &sip->sin_addr;
2349
2350 if (siap->s_addr == 0)
2351 return NULL;
2352
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002353 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2354 /*
2355 * we break after finding the first match because
2356 * the list is sorted from longest to shortest mask
2357 * so we have found the most specific match
2358 */
2359 if (snp->smk_host.s_addr ==
2360 (siap->s_addr & snp->smk_mask.s_addr))
2361 return snp->smk_label;
2362
2363 return NULL;
2364}
2365
2366#if IS_ENABLED(CONFIG_IPV6)
2367/*
2368 * smk_ipv6_localhost - Check for local ipv6 host address
2369 * @sip: the address
2370 *
2371 * Returns boolean true if this is the localhost address
2372 */
2373static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2374{
2375 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2376 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2377
2378 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2379 ntohs(be16p[7]) == 1)
2380 return true;
2381 return false;
2382}
2383
2384/**
2385* smack_ipv6host_label - check host based restrictions
2386* @sip: the object end
2387*
2388* looks for host based access restrictions
2389*
2390* This version will only be appropriate for really small sets of single label
2391* hosts. The caller is responsible for ensuring that the RCU read lock is
2392* taken before calling this function.
2393*
2394* Returns the label of the far end or NULL if it's not special.
2395*/
2396static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2397{
2398 struct smk_net6addr *snp;
2399 struct in6_addr *sap = &sip->sin6_addr;
2400 int i;
2401 int found = 0;
2402
2403 /*
2404 * It's local. Don't look for a host label.
2405 */
2406 if (smk_ipv6_localhost(sip))
2407 return NULL;
2408
2409 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002410 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002411 * If the label is NULL the entry has
2412 * been renounced. Ignore it.
2413 */
2414 if (snp->smk_label == NULL)
2415 continue;
2416 /*
Paul Moore07feee82009-03-27 17:10:54 -04002417 * we break after finding the first match because
2418 * the list is sorted from longest to shortest mask
2419 * so we have found the most specific match
2420 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002421 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002422 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2423 snp->smk_host.s6_addr16[i]) {
2424 found = 0;
2425 break;
2426 }
Etienne Basset43031542009-03-27 17:11:01 -04002427 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002428 if (found)
2429 return snp->smk_label;
2430 }
Paul Moore07feee82009-03-27 17:10:54 -04002431
2432 return NULL;
2433}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002434#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002435
2436/**
Casey Schauflere114e472008-02-04 22:29:50 -08002437 * smack_netlabel - Set the secattr on a socket
2438 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002439 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002440 *
2441 * Convert the outbound smack value (smk_out) to a
2442 * secattr and attach it to the socket.
2443 *
2444 * Returns 0 on success or an error code
2445 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002446static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002447{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002448 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002449 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002450 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002451
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002452 /*
2453 * Usually the netlabel code will handle changing the
2454 * packet labeling based on the label.
2455 * The case of a single label host is different, because
2456 * a single label host should never get a labeled packet
2457 * even though the label is usually associated with a packet
2458 * label.
2459 */
2460 local_bh_disable();
2461 bh_lock_sock_nested(sk);
2462
2463 if (ssp->smk_out == smack_net_ambient ||
2464 labeled == SMACK_UNLABELED_SOCKET)
2465 netlbl_sock_delattr(sk);
2466 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002467 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002468 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002469 }
2470
2471 bh_unlock_sock(sk);
2472 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002473
Casey Schauflere114e472008-02-04 22:29:50 -08002474 return rc;
2475}
2476
2477/**
Paul Moore07feee82009-03-27 17:10:54 -04002478 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2479 * @sk: the socket
2480 * @sap: the destination address
2481 *
2482 * Set the correct secattr for the given socket based on the destination
2483 * address and perform any outbound access checks needed.
2484 *
2485 * Returns 0 on success or an error code.
2486 *
2487 */
2488static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2489{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002490 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002491 int rc;
2492 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002493 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002494 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002495 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002496
2497 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002498 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002499 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002500#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002501 struct lsm_network_audit net;
2502
Eric Paris48c62af2012-04-02 13:15:44 -04002503 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2504 ad.a.u.net->family = sap->sin_family;
2505 ad.a.u.net->dport = sap->sin_port;
2506 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002507#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002508 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002509 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002510 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2511 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002512 } else {
2513 sk_lbl = SMACK_CIPSO_SOCKET;
2514 rc = 0;
2515 }
2516 rcu_read_unlock();
2517 if (rc != 0)
2518 return rc;
2519
2520 return smack_netlabel(sk, sk_lbl);
2521}
2522
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002523#if IS_ENABLED(CONFIG_IPV6)
2524/**
2525 * smk_ipv6_check - check Smack access
2526 * @subject: subject Smack label
2527 * @object: object Smack label
2528 * @address: address
2529 * @act: the action being taken
2530 *
2531 * Check an IPv6 access
2532 */
2533static int smk_ipv6_check(struct smack_known *subject,
2534 struct smack_known *object,
2535 struct sockaddr_in6 *address, int act)
2536{
2537#ifdef CONFIG_AUDIT
2538 struct lsm_network_audit net;
2539#endif
2540 struct smk_audit_info ad;
2541 int rc;
2542
2543#ifdef CONFIG_AUDIT
2544 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2545 ad.a.u.net->family = PF_INET6;
2546 ad.a.u.net->dport = ntohs(address->sin6_port);
2547 if (act == SMK_RECEIVING)
2548 ad.a.u.net->v6info.saddr = address->sin6_addr;
2549 else
2550 ad.a.u.net->v6info.daddr = address->sin6_addr;
2551#endif
2552 rc = smk_access(subject, object, MAY_WRITE, &ad);
2553 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2554 return rc;
2555}
2556#endif /* CONFIG_IPV6 */
2557
2558#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002559/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002560 * smk_ipv6_port_label - Smack port access table management
2561 * @sock: socket
2562 * @address: address
2563 *
2564 * Create or update the port list entry
2565 */
2566static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2567{
2568 struct sock *sk = sock->sk;
2569 struct sockaddr_in6 *addr6;
2570 struct socket_smack *ssp = sock->sk->sk_security;
2571 struct smk_port_label *spp;
2572 unsigned short port = 0;
2573
2574 if (address == NULL) {
2575 /*
2576 * This operation is changing the Smack information
2577 * on the bound socket. Take the changes to the port
2578 * as well.
2579 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302580 rcu_read_lock();
2581 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002582 if (sk != spp->smk_sock)
2583 continue;
2584 spp->smk_in = ssp->smk_in;
2585 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302586 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002587 return;
2588 }
2589 /*
2590 * A NULL address is only used for updating existing
2591 * bound entries. If there isn't one, it's OK.
2592 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302593 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002594 return;
2595 }
2596
2597 addr6 = (struct sockaddr_in6 *)address;
2598 port = ntohs(addr6->sin6_port);
2599 /*
2600 * This is a special case that is safely ignored.
2601 */
2602 if (port == 0)
2603 return;
2604
2605 /*
2606 * Look for an existing port list entry.
2607 * This is an indication that a port is getting reused.
2608 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302609 rcu_read_lock();
2610 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302611 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002612 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302613 if (spp->smk_can_reuse != 1) {
2614 rcu_read_unlock();
2615 return;
2616 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002617 spp->smk_port = port;
2618 spp->smk_sock = sk;
2619 spp->smk_in = ssp->smk_in;
2620 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302621 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302622 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 return;
2624 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302625 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002626 /*
2627 * A new port entry is required.
2628 */
2629 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2630 if (spp == NULL)
2631 return;
2632
2633 spp->smk_port = port;
2634 spp->smk_sock = sk;
2635 spp->smk_in = ssp->smk_in;
2636 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302637 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302638 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002639
Vishal Goel3c7ce342016-11-23 10:31:08 +05302640 mutex_lock(&smack_ipv6_lock);
2641 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2642 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002643 return;
2644}
2645
2646/**
2647 * smk_ipv6_port_check - check Smack port access
2648 * @sock: socket
2649 * @address: address
2650 *
2651 * Create or update the port list entry
2652 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002653static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002654 int act)
2655{
Casey Schauflerc6739442013-05-22 18:42:56 -07002656 struct smk_port_label *spp;
2657 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002658 struct smack_known *skp = NULL;
2659 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002660 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002661
2662 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002663 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002664 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002665 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002666 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002667 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002668 }
2669
2670 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002671 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002672 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002673 if (skp != NULL && object != NULL)
2674 return smk_ipv6_check(skp, object, address, act);
2675 if (skp == NULL)
2676 skp = smack_net_ambient;
2677 if (object == NULL)
2678 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002679
2680 /*
2681 * It's remote, so port lookup does no good.
2682 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002683 if (!smk_ipv6_localhost(address))
2684 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002685
2686 /*
2687 * It's local so the send check has to have passed.
2688 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002689 if (act == SMK_RECEIVING)
2690 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002691
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002692 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302693 rcu_read_lock();
2694 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302695 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002696 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002697 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002698 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002699 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002700 break;
2701 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302702 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002703
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002704 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002705}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002706#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002707
2708/**
Casey Schauflere114e472008-02-04 22:29:50 -08002709 * smack_inode_setsecurity - set smack xattrs
2710 * @inode: the object
2711 * @name: attribute name
2712 * @value: attribute value
2713 * @size: size of the attribute
2714 * @flags: unused
2715 *
2716 * Sets the named attribute in the appropriate blob
2717 *
2718 * Returns 0 on success, or an error code
2719 */
2720static int smack_inode_setsecurity(struct inode *inode, const char *name,
2721 const void *value, size_t size, int flags)
2722{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002723 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002724 struct inode_smack *nsp = inode->i_security;
2725 struct socket_smack *ssp;
2726 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002727 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002728
Casey Schauflerf7112e62012-05-06 15:22:02 -07002729 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302730 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002731
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002732 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002733 if (IS_ERR(skp))
2734 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002735
2736 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002737 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002738 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002739 return 0;
2740 }
2741 /*
2742 * The rest of the Smack xattrs are only on sockets.
2743 */
2744 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2745 return -EOPNOTSUPP;
2746
2747 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002748 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002749 return -EOPNOTSUPP;
2750
2751 ssp = sock->sk->sk_security;
2752
2753 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002754 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002755 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002756 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002757 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002758 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2759 if (rc != 0)
2760 printk(KERN_WARNING
2761 "Smack: \"%s\" netlbl error %d.\n",
2762 __func__, -rc);
2763 }
Casey Schauflere114e472008-02-04 22:29:50 -08002764 } else
2765 return -EOPNOTSUPP;
2766
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002767#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002768 if (sock->sk->sk_family == PF_INET6)
2769 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002770#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002771
Casey Schauflere114e472008-02-04 22:29:50 -08002772 return 0;
2773}
2774
2775/**
2776 * smack_socket_post_create - finish socket setup
2777 * @sock: the socket
2778 * @family: protocol family
2779 * @type: unused
2780 * @protocol: unused
2781 * @kern: unused
2782 *
2783 * Sets the netlabel information on the socket
2784 *
2785 * Returns 0 on success, and error code otherwise
2786 */
2787static int smack_socket_post_create(struct socket *sock, int family,
2788 int type, int protocol, int kern)
2789{
Marcin Lis74123012015-01-22 15:40:33 +01002790 struct socket_smack *ssp;
2791
2792 if (sock->sk == NULL)
2793 return 0;
2794
2795 /*
2796 * Sockets created by kernel threads receive web label.
2797 */
2798 if (unlikely(current->flags & PF_KTHREAD)) {
2799 ssp = sock->sk->sk_security;
2800 ssp->smk_in = &smack_known_web;
2801 ssp->smk_out = &smack_known_web;
2802 }
2803
2804 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002805 return 0;
2806 /*
2807 * Set the outbound netlbl.
2808 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002809 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2810}
2811
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002812/**
2813 * smack_socket_socketpair - create socket pair
2814 * @socka: one socket
2815 * @sockb: another socket
2816 *
2817 * Cross reference the peer labels for SO_PEERSEC
2818 *
2819 * Returns 0 on success, and error code otherwise
2820 */
2821static int smack_socket_socketpair(struct socket *socka,
2822 struct socket *sockb)
2823{
2824 struct socket_smack *asp = socka->sk->sk_security;
2825 struct socket_smack *bsp = sockb->sk->sk_security;
2826
2827 asp->smk_packet = bsp->smk_out;
2828 bsp->smk_packet = asp->smk_out;
2829
2830 return 0;
2831}
2832
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002833#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002834/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002835 * smack_socket_bind - record port binding information.
2836 * @sock: the socket
2837 * @address: the port address
2838 * @addrlen: size of the address
2839 *
2840 * Records the label bound to a port.
2841 *
2842 * Returns 0
2843 */
2844static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2845 int addrlen)
2846{
2847 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2848 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002849 return 0;
2850}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002851#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002852
2853/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002854 * smack_socket_connect - connect access check
2855 * @sock: the socket
2856 * @sap: the other end
2857 * @addrlen: size of sap
2858 *
2859 * Verifies that a connection may be possible
2860 *
2861 * Returns 0 on success, and error code otherwise
2862 */
2863static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2864 int addrlen)
2865{
Casey Schauflerc6739442013-05-22 18:42:56 -07002866 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002867#if IS_ENABLED(CONFIG_IPV6)
2868 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2869#endif
2870#ifdef SMACK_IPV6_SECMARK_LABELING
2871 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002872 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002873#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002874
Casey Schauflerc6739442013-05-22 18:42:56 -07002875 if (sock->sk == NULL)
2876 return 0;
2877
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002878#ifdef SMACK_IPV6_SECMARK_LABELING
2879 ssp = sock->sk->sk_security;
2880#endif
2881
Casey Schauflerc6739442013-05-22 18:42:56 -07002882 switch (sock->sk->sk_family) {
2883 case PF_INET:
2884 if (addrlen < sizeof(struct sockaddr_in))
2885 return -EINVAL;
2886 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2887 break;
2888 case PF_INET6:
2889 if (addrlen < sizeof(struct sockaddr_in6))
2890 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002891#ifdef SMACK_IPV6_SECMARK_LABELING
2892 rsp = smack_ipv6host_label(sip);
2893 if (rsp != NULL)
2894 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002895 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002896#endif
2897#ifdef SMACK_IPV6_PORT_LABELING
2898 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2899#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002900 break;
2901 }
2902 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002903}
2904
2905/**
2906 * smack_flags_to_may - convert S_ to MAY_ values
2907 * @flags: the S_ value
2908 *
2909 * Returns the equivalent MAY_ value
2910 */
2911static int smack_flags_to_may(int flags)
2912{
2913 int may = 0;
2914
2915 if (flags & S_IRUGO)
2916 may |= MAY_READ;
2917 if (flags & S_IWUGO)
2918 may |= MAY_WRITE;
2919 if (flags & S_IXUGO)
2920 may |= MAY_EXEC;
2921
2922 return may;
2923}
2924
2925/**
2926 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2927 * @msg: the object
2928 *
2929 * Returns 0
2930 */
2931static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2932{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002933 struct smack_known *skp = smk_of_current();
2934
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002935 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002936 return 0;
2937}
2938
2939/**
2940 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2941 * @msg: the object
2942 *
2943 * Clears the blob pointer
2944 */
2945static void smack_msg_msg_free_security(struct msg_msg *msg)
2946{
2947 msg->security = NULL;
2948}
2949
2950/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002951 * smack_of_ipc - the smack pointer for the ipc
2952 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002953 *
2954 * Returns a pointer to the smack value
2955 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002956static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002957{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002958 return (struct smack_known *)isp->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002959}
2960
2961/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002962 * smack_ipc_alloc_security - Set the security blob for ipc
2963 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002964 *
2965 * Returns 0
2966 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002967static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002968{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002969 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002970
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002971 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002972 return 0;
2973}
2974
2975/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002976 * smack_ipc_free_security - Clear the security blob for ipc
2977 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002978 *
2979 * Clears the blob pointer
2980 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002981static void smack_ipc_free_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002982{
Casey Schauflere114e472008-02-04 22:29:50 -08002983 isp->security = NULL;
2984}
2985
2986/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002987 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002988 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002989 * @access : access requested
2990 *
2991 * Returns 0 if current has the requested access, error code otherwise
2992 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002993static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002994{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002995 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002996 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002997 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002998
2999#ifdef CONFIG_AUDIT
3000 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003001 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003002#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003003 rc = smk_curacc(ssp, access, &ad);
3004 rc = smk_bu_current("shm", ssp, access, rc);
3005 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003006}
3007
3008/**
Casey Schauflere114e472008-02-04 22:29:50 -08003009 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003010 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003011 * @shmflg: access requested
3012 *
3013 * Returns 0 if current has the requested access, error code otherwise
3014 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003015static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003016{
Casey Schauflere114e472008-02-04 22:29:50 -08003017 int may;
3018
3019 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003020 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003021}
3022
3023/**
3024 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003025 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003026 * @cmd: what it wants to do
3027 *
3028 * Returns 0 if current has the requested access, error code otherwise
3029 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003030static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003031{
Casey Schauflere114e472008-02-04 22:29:50 -08003032 int may;
3033
3034 switch (cmd) {
3035 case IPC_STAT:
3036 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07003037 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003038 may = MAY_READ;
3039 break;
3040 case IPC_SET:
3041 case SHM_LOCK:
3042 case SHM_UNLOCK:
3043 case IPC_RMID:
3044 may = MAY_READWRITE;
3045 break;
3046 case IPC_INFO:
3047 case SHM_INFO:
3048 /*
3049 * System level information.
3050 */
3051 return 0;
3052 default:
3053 return -EINVAL;
3054 }
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_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003060 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003061 * @shmaddr: unused
3062 * @shmflg: access requested
3063 *
3064 * Returns 0 if current has the requested access, error code otherwise
3065 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003066static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003067 int shmflg)
3068{
Casey Schauflere114e472008-02-04 22:29:50 -08003069 int may;
3070
3071 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003072 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003073}
3074
3075/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003076 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003077 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003078 * @access : access requested
3079 *
3080 * Returns 0 if current has the requested access, error code otherwise
3081 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003082static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003083{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003084 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003085 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003086 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003087
3088#ifdef CONFIG_AUDIT
3089 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003090 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003091#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003092 rc = smk_curacc(ssp, access, &ad);
3093 rc = smk_bu_current("sem", ssp, access, rc);
3094 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003095}
3096
3097/**
Casey Schauflere114e472008-02-04 22:29:50 -08003098 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003099 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003100 * @semflg: access requested
3101 *
3102 * Returns 0 if current has the requested access, error code otherwise
3103 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003104static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003105{
Casey Schauflere114e472008-02-04 22:29:50 -08003106 int may;
3107
3108 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003109 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003110}
3111
3112/**
3113 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003114 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003115 * @cmd: what it wants to do
3116 *
3117 * Returns 0 if current has the requested access, error code otherwise
3118 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003120{
Casey Schauflere114e472008-02-04 22:29:50 -08003121 int may;
3122
3123 switch (cmd) {
3124 case GETPID:
3125 case GETNCNT:
3126 case GETZCNT:
3127 case GETVAL:
3128 case GETALL:
3129 case IPC_STAT:
3130 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003131 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003132 may = MAY_READ;
3133 break;
3134 case SETVAL:
3135 case SETALL:
3136 case IPC_RMID:
3137 case IPC_SET:
3138 may = MAY_READWRITE;
3139 break;
3140 case IPC_INFO:
3141 case SEM_INFO:
3142 /*
3143 * System level information
3144 */
3145 return 0;
3146 default:
3147 return -EINVAL;
3148 }
3149
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003150 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003151}
3152
3153/**
3154 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003155 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003156 * @sops: unused
3157 * @nsops: unused
3158 * @alter: unused
3159 *
3160 * Treated as read and write in all cases.
3161 *
3162 * Returns 0 if access is allowed, error code otherwise
3163 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003164static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003165 unsigned nsops, int alter)
3166{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003167 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003168}
3169
3170/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003171 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003172 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003173 * @access : access requested
3174 *
3175 * return 0 if current has access, error otherwise
3176 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003177static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003178{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003179 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003180 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003181 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003182
3183#ifdef CONFIG_AUDIT
3184 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003185 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003186#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003187 rc = smk_curacc(msp, access, &ad);
3188 rc = smk_bu_current("msq", msp, access, rc);
3189 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003190}
3191
3192/**
Casey Schauflere114e472008-02-04 22:29:50 -08003193 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003194 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003195 * @msqflg: access requested
3196 *
3197 * Returns 0 if current has the requested access, error code otherwise
3198 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003199static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003200{
Casey Schauflere114e472008-02-04 22:29:50 -08003201 int may;
3202
3203 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003204 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003205}
3206
3207/**
3208 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003209 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003210 * @cmd: what it wants to do
3211 *
3212 * Returns 0 if current has the requested access, error code otherwise
3213 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003214static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003215{
Casey Schauflere114e472008-02-04 22:29:50 -08003216 int may;
3217
3218 switch (cmd) {
3219 case IPC_STAT:
3220 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003221 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003222 may = MAY_READ;
3223 break;
3224 case IPC_SET:
3225 case IPC_RMID:
3226 may = MAY_READWRITE;
3227 break;
3228 case IPC_INFO:
3229 case MSG_INFO:
3230 /*
3231 * System level information
3232 */
3233 return 0;
3234 default:
3235 return -EINVAL;
3236 }
3237
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003238 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003239}
3240
3241/**
3242 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003243 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003244 * @msg: unused
3245 * @msqflg: access requested
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_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003250 int msqflg)
3251{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003252 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003253
Etienne Bassetecfcc532009-04-08 20:40:06 +02003254 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003255 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003256}
3257
3258/**
3259 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003260 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003261 * @msg: unused
3262 * @target: unused
3263 * @type: unused
3264 * @mode: unused
3265 *
3266 * Returns 0 if current has read and write access, error code otherwise
3267 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003268static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003269 struct task_struct *target, long type, int mode)
3270{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003271 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003272}
3273
3274/**
3275 * smack_ipc_permission - Smack access for ipc_permission()
3276 * @ipp: the object permissions
3277 * @flag: access requested
3278 *
3279 * Returns 0 if current has read and write access, error code otherwise
3280 */
3281static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3282{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003283 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003284 int may = smack_flags_to_may(flag);
3285 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003286 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003287
Etienne Bassetecfcc532009-04-08 20:40:06 +02003288#ifdef CONFIG_AUDIT
3289 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3290 ad.a.u.ipc_id = ipp->id;
3291#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003292 rc = smk_curacc(iskp, may, &ad);
3293 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003294 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003295}
3296
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003297/**
3298 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003299 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003300 * @secid: where result will be saved
3301 */
3302static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3303{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003304 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003305
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003306 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003307}
3308
Casey Schauflere114e472008-02-04 22:29:50 -08003309/**
3310 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003311 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003312 * @inode: the object
3313 *
3314 * Set the inode's security blob if it hasn't been done already.
3315 */
3316static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3317{
3318 struct super_block *sbp;
3319 struct superblock_smack *sbsp;
3320 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003321 struct smack_known *skp;
3322 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003323 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003324 char trattr[TRANS_TRUE_SIZE];
3325 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003326 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003327 struct dentry *dp;
3328
3329 if (inode == NULL)
3330 return;
3331
3332 isp = inode->i_security;
3333
3334 mutex_lock(&isp->smk_lock);
3335 /*
3336 * If the inode is already instantiated
3337 * take the quick way out
3338 */
3339 if (isp->smk_flags & SMK_INODE_INSTANT)
3340 goto unlockandout;
3341
3342 sbp = inode->i_sb;
3343 sbsp = sbp->s_security;
3344 /*
3345 * We're going to use the superblock default label
3346 * if there's no label on the file.
3347 */
3348 final = sbsp->smk_default;
3349
3350 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003351 * If this is the root inode the superblock
3352 * may be in the process of initialization.
3353 * If that is the case use the root value out
3354 * of the superblock.
3355 */
3356 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003357 switch (sbp->s_magic) {
3358 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003359 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003360 /*
3361 * The cgroup filesystem is never mounted,
3362 * so there's no opportunity to set the mount
3363 * options.
3364 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003365 sbsp->smk_root = &smack_known_star;
3366 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003367 isp->smk_inode = sbsp->smk_root;
3368 break;
3369 case TMPFS_MAGIC:
3370 /*
3371 * What about shmem/tmpfs anonymous files with dentry
3372 * obtained from d_alloc_pseudo()?
3373 */
3374 isp->smk_inode = smk_of_current();
3375 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003376 case PIPEFS_MAGIC:
3377 isp->smk_inode = smk_of_current();
3378 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003379 case SOCKFS_MAGIC:
3380 /*
3381 * Socket access is controlled by the socket
3382 * structures associated with the task involved.
3383 */
3384 isp->smk_inode = &smack_known_star;
3385 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003386 default:
3387 isp->smk_inode = sbsp->smk_root;
3388 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003389 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003390 isp->smk_flags |= SMK_INODE_INSTANT;
3391 goto unlockandout;
3392 }
3393
3394 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003395 * This is pretty hackish.
3396 * Casey says that we shouldn't have to do
3397 * file system specific code, but it does help
3398 * with keeping it simple.
3399 */
3400 switch (sbp->s_magic) {
3401 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003402 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003403 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003404 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003405 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003406 * that the smack file system doesn't do
3407 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003408 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003409 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003410 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003411 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003412 break;
3413 case DEVPTS_SUPER_MAGIC:
3414 /*
3415 * devpts seems content with the label of the task.
3416 * Programs that change smack have to treat the
3417 * pty with respect.
3418 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003419 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003420 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003421 case PROC_SUPER_MAGIC:
3422 /*
3423 * Casey says procfs appears not to care.
3424 * The superblock default suffices.
3425 */
3426 break;
3427 case TMPFS_MAGIC:
3428 /*
3429 * Device labels should come from the filesystem,
3430 * but watch out, because they're volitile,
3431 * getting recreated on every reboot.
3432 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003433 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003434 /*
Gustavo A. R. Silvab1fed3e2018-08-01 17:38:54 -05003435 * Fall through.
Casey Schauflere114e472008-02-04 22:29:50 -08003436 *
3437 * If a smack value has been set we want to use it,
3438 * but since tmpfs isn't giving us the opportunity
3439 * to set mount options simulate setting the
3440 * superblock default.
3441 */
3442 default:
3443 /*
3444 * This isn't an understood special case.
3445 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003446 */
3447
3448 /*
3449 * UNIX domain sockets use lower level socket data.
3450 */
3451 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003452 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003453 break;
3454 }
3455 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003456 * No xattr support means, alas, no SMACK label.
3457 * Use the aforeapplied default.
3458 * It would be curious if the label of the task
3459 * does not match that assigned.
3460 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003461 if (!(inode->i_opflags & IOP_XATTR))
3462 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003463 /*
3464 * Get the dentry for xattr.
3465 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003466 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003467 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003468 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003469 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003470
3471 /*
3472 * Transmuting directory
3473 */
3474 if (S_ISDIR(inode->i_mode)) {
3475 /*
3476 * If this is a new directory and the label was
3477 * transmuted when the inode was initialized
3478 * set the transmute attribute on the directory
3479 * and mark the inode.
3480 *
3481 * If there is a transmute attribute on the
3482 * directory mark the inode.
3483 */
3484 if (isp->smk_flags & SMK_INODE_CHANGED) {
3485 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003486 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003487 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003488 TRANS_TRUE, TRANS_TRUE_SIZE,
3489 0);
3490 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003491 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003492 XATTR_NAME_SMACKTRANSMUTE, trattr,
3493 TRANS_TRUE_SIZE);
3494 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3495 TRANS_TRUE_SIZE) != 0)
3496 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003497 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003498 if (rc >= 0)
3499 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003500 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003501 /*
3502 * Don't let the exec or mmap label be "*" or "@".
3503 */
3504 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3505 if (IS_ERR(skp) || skp == &smack_known_star ||
3506 skp == &smack_known_web)
3507 skp = NULL;
3508 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003509
Casey Schaufler19760ad2013-12-16 16:27:26 -08003510 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003511 if (IS_ERR(skp) || skp == &smack_known_star ||
3512 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003513 skp = NULL;
3514 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003515
Casey Schauflere114e472008-02-04 22:29:50 -08003516 dput(dp);
3517 break;
3518 }
3519
3520 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003521 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003522 else
3523 isp->smk_inode = final;
3524
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003525 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003526
3527unlockandout:
3528 mutex_unlock(&isp->smk_lock);
3529 return;
3530}
3531
3532/**
3533 * smack_getprocattr - Smack process attribute access
3534 * @p: the object task
3535 * @name: the name of the attribute in /proc/.../attr
3536 * @value: where to put the result
3537 *
3538 * Places a copy of the task Smack into value
3539 *
3540 * Returns the length of the smack label or an error code
3541 */
3542static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3543{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003544 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003545 char *cp;
3546 int slen;
3547
3548 if (strcmp(name, "current") != 0)
3549 return -EINVAL;
3550
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003551 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003552 if (cp == NULL)
3553 return -ENOMEM;
3554
3555 slen = strlen(cp);
3556 *value = cp;
3557 return slen;
3558}
3559
3560/**
3561 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003562 * @name: the name of the attribute in /proc/.../attr
3563 * @value: the value to set
3564 * @size: the size of the value
3565 *
3566 * Sets the Smack value of the task. Only setting self
3567 * is permitted and only with privilege
3568 *
3569 * Returns the length of the smack label or an error code
3570 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003571static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003572{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003573 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003574 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003575 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003576 struct smack_known_list_elem *sklep;
3577 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003578
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003579 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003580 return -EPERM;
3581
Casey Schauflerf7112e62012-05-06 15:22:02 -07003582 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003583 return -EINVAL;
3584
3585 if (strcmp(name, "current") != 0)
3586 return -EINVAL;
3587
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003588 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003589 if (IS_ERR(skp))
3590 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003591
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003592 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303593 * No process is ever allowed the web ("@") label
3594 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003595 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303596 if (skp == &smack_known_web || skp == &smack_known_star)
3597 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003598
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003599 if (!smack_privileged(CAP_MAC_ADMIN)) {
3600 rc = -EPERM;
3601 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3602 if (sklep->smk_label == skp) {
3603 rc = 0;
3604 break;
3605 }
3606 if (rc)
3607 return rc;
3608 }
3609
David Howellsd84f4f92008-11-14 10:39:23 +11003610 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003611 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003612 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003613
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003614 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003615 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003616 /*
3617 * process can change its label only once
3618 */
3619 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003620
David Howellsd84f4f92008-11-14 10:39:23 +11003621 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003622 return size;
3623}
3624
3625/**
3626 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003627 * @sock: one sock
3628 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003629 * @newsk: unused
3630 *
3631 * Return 0 if a subject with the smack of sock could access
3632 * an object with the smack of other, otherwise an error code
3633 */
David S. Miller3610cda2011-01-05 15:38:53 -08003634static int smack_unix_stream_connect(struct sock *sock,
3635 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003636{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003637 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003638 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003639 struct socket_smack *ssp = sock->sk_security;
3640 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003641 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003642 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003643 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003644#ifdef CONFIG_AUDIT
3645 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003646#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003647
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003648 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3649 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003650 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003651#ifdef CONFIG_AUDIT
3652 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3653 smk_ad_setfield_u_net_sk(&ad, other);
3654#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003655 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3656 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003657 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003658 okp = osp->smk_out;
3659 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003660 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003661 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003662 MAY_WRITE, rc);
3663 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003664 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003665
Casey Schaufler975d5e52011-09-26 14:43:39 -07003666 /*
3667 * Cross reference the peer labels for SO_PEERSEC.
3668 */
3669 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003670 nsp->smk_packet = ssp->smk_out;
3671 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003672 }
3673
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003674 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003675}
3676
3677/**
3678 * smack_unix_may_send - Smack access on UDS
3679 * @sock: one socket
3680 * @other: the other socket
3681 *
3682 * Return 0 if a subject with the smack of sock could access
3683 * an object with the smack of other, otherwise an error code
3684 */
3685static int smack_unix_may_send(struct socket *sock, struct socket *other)
3686{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003687 struct socket_smack *ssp = sock->sk->sk_security;
3688 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003689 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003690 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003691
Kees Cook923e9a12012-04-10 13:26:44 -07003692#ifdef CONFIG_AUDIT
3693 struct lsm_network_audit net;
3694
Eric Paris48c62af2012-04-02 13:15:44 -04003695 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003696 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003697#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003698
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003699 if (smack_privileged(CAP_MAC_OVERRIDE))
3700 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003701
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003702 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3703 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003704 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003705}
3706
3707/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003708 * smack_socket_sendmsg - Smack check based on destination host
3709 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003710 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003711 * @size: the size of the message
3712 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003713 * Return 0 if the current subject can write to the destination host.
3714 * For IPv4 this is only a question if the destination is a single label host.
3715 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003716 */
3717static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3718 int size)
3719{
3720 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003721#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003722 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003723#endif
3724#ifdef SMACK_IPV6_SECMARK_LABELING
3725 struct socket_smack *ssp = sock->sk->sk_security;
3726 struct smack_known *rsp;
3727#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003728 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003729
3730 /*
3731 * Perfectly reasonable for this to be NULL
3732 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003733 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003734 return 0;
3735
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003736 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003737 case AF_INET:
3738 rc = smack_netlabel_send(sock->sk, sip);
3739 break;
3740 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003741#ifdef SMACK_IPV6_SECMARK_LABELING
3742 rsp = smack_ipv6host_label(sap);
3743 if (rsp != NULL)
3744 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3745 SMK_CONNECTING);
3746#endif
3747#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003748 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003749#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003750 break;
3751 }
3752 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003753}
3754
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003755/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003756 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003757 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003758 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003759 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003760 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003761 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003762static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3763 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003764{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003765 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003766 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003767 int acat;
3768 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003769
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003770 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003771 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003772 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003773 * If there are flags but no level netlabel isn't
3774 * behaving the way we expect it to.
3775 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003776 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003777 * Without guidance regarding the smack value
3778 * for the packet fall back on the network
3779 * ambient value.
3780 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003781 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303782 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003783 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003784 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003785 /*
3786 * Compare the catsets. Use the netlbl APIs.
3787 */
3788 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3789 if ((skp->smk_netlabel.flags &
3790 NETLBL_SECATTR_MLS_CAT) == 0)
3791 found = 1;
3792 break;
3793 }
3794 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003795 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3796 acat + 1);
3797 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003798 skp->smk_netlabel.attr.mls.cat,
3799 kcat + 1);
3800 if (acat < 0 || kcat < 0)
3801 break;
3802 }
3803 if (acat == kcat) {
3804 found = 1;
3805 break;
3806 }
Casey Schauflere114e472008-02-04 22:29:50 -08003807 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003808 rcu_read_unlock();
3809
3810 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003811 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003812
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003813 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003814 return &smack_known_web;
3815 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003816 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003817 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003818 /*
3819 * Looks like a fallback, which gives us a secid.
3820 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003821 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003822 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003823 * Without guidance regarding the smack value
3824 * for the packet fall back on the network
3825 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003826 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003827 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003828}
3829
Casey Schaufler69f287a2014-12-12 17:08:40 -08003830#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003831static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003832{
Casey Schauflerc6739442013-05-22 18:42:56 -07003833 u8 nexthdr;
3834 int offset;
3835 int proto = -EINVAL;
3836 struct ipv6hdr _ipv6h;
3837 struct ipv6hdr *ip6;
3838 __be16 frag_off;
3839 struct tcphdr _tcph, *th;
3840 struct udphdr _udph, *uh;
3841 struct dccp_hdr _dccph, *dh;
3842
3843 sip->sin6_port = 0;
3844
3845 offset = skb_network_offset(skb);
3846 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3847 if (ip6 == NULL)
3848 return -EINVAL;
3849 sip->sin6_addr = ip6->saddr;
3850
3851 nexthdr = ip6->nexthdr;
3852 offset += sizeof(_ipv6h);
3853 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3854 if (offset < 0)
3855 return -EINVAL;
3856
3857 proto = nexthdr;
3858 switch (proto) {
3859 case IPPROTO_TCP:
3860 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3861 if (th != NULL)
3862 sip->sin6_port = th->source;
3863 break;
3864 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003865 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003866 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3867 if (uh != NULL)
3868 sip->sin6_port = uh->source;
3869 break;
3870 case IPPROTO_DCCP:
3871 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3872 if (dh != NULL)
3873 sip->sin6_port = dh->dccph_sport;
3874 break;
3875 }
3876 return proto;
3877}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003878#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003879
Casey Schauflere114e472008-02-04 22:29:50 -08003880/**
3881 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3882 * @sk: socket
3883 * @skb: packet
3884 *
3885 * Returns 0 if the packet should be delivered, an error code otherwise
3886 */
3887static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3888{
3889 struct netlbl_lsm_secattr secattr;
3890 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003891 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003892 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003893 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003894 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003895#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003896 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003897#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003898#if IS_ENABLED(CONFIG_IPV6)
3899 struct sockaddr_in6 sadd;
3900 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003901
3902 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3903 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003904#endif /* CONFIG_IPV6 */
3905
Piotr Sawicki129a9982018-07-19 11:42:58 +02003906 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003907 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003908#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3909 /*
3910 * If there is a secmark use it rather than the CIPSO label.
3911 * If there is no secmark fall back to CIPSO.
3912 * The secmark is assumed to reflect policy better.
3913 */
3914 if (skb && skb->secmark != 0) {
3915 skp = smack_from_secid(skb->secmark);
3916 goto access_check;
3917 }
3918#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003919 /*
3920 * Translate what netlabel gave us.
3921 */
3922 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003923
Piotr Sawicki129a9982018-07-19 11:42:58 +02003924 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003925 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003926 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003927 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003928 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003929
Casey Schauflerc6739442013-05-22 18:42:56 -07003930 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003931
Casey Schaufler69f287a2014-12-12 17:08:40 -08003932#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3933access_check:
3934#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003935#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003936 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003937 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003938 ad.a.u.net->netif = skb->skb_iif;
3939 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003940#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003941 /*
3942 * Receiving a packet requires that the other end
3943 * be able to write here. Read access is not required.
3944 * This is the simplist possible security model
3945 * for networking.
3946 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003947 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3948 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003949 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003950 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003951 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003952 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003953#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003954 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003955 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003956 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3957 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003958 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003959#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003960 if (skb && skb->secmark != 0)
3961 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003962 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003963 skp = smack_ipv6host_label(&sadd);
3964 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003965 skp = smack_net_ambient;
3966#ifdef CONFIG_AUDIT
3967 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003968 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003969 ad.a.u.net->netif = skb->skb_iif;
3970 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3971#endif /* CONFIG_AUDIT */
3972 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3973 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3974 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003975#endif /* SMACK_IPV6_SECMARK_LABELING */
3976#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003977 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003978#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003979 if (rc != 0)
3980 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3981 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003982 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003983#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003984 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003985
Paul Moorea8134292008-10-10 10:16:31 -04003986 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003987}
3988
3989/**
3990 * smack_socket_getpeersec_stream - pull in packet label
3991 * @sock: the socket
3992 * @optval: user's destination
3993 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003994 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003995 *
3996 * returns zero on success, an error code otherwise
3997 */
3998static int smack_socket_getpeersec_stream(struct socket *sock,
3999 char __user *optval,
4000 int __user *optlen, unsigned len)
4001{
4002 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004003 char *rcp = "";
4004 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004005 int rc = 0;
4006
4007 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004008 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004009 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004010 slen = strlen(rcp) + 1;
4011 }
Casey Schauflere114e472008-02-04 22:29:50 -08004012
4013 if (slen > len)
4014 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004015 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004016 rc = -EFAULT;
4017
4018 if (put_user(slen, optlen) != 0)
4019 rc = -EFAULT;
4020
4021 return rc;
4022}
4023
4024
4025/**
4026 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004027 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004028 * @skb: packet data
4029 * @secid: pointer to where to put the secid of the packet
4030 *
4031 * Sets the netlabel socket state on sk from parent
4032 */
4033static int smack_socket_getpeersec_dgram(struct socket *sock,
4034 struct sk_buff *skb, u32 *secid)
4035
4036{
4037 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004038 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004039 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004040 int family = PF_UNSPEC;
4041 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004042 int rc;
4043
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004044 if (skb != NULL) {
4045 if (skb->protocol == htons(ETH_P_IP))
4046 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004047#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004048 else if (skb->protocol == htons(ETH_P_IPV6))
4049 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004050#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004051 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004052 if (family == PF_UNSPEC && sock != NULL)
4053 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004054
Casey Schaufler69f287a2014-12-12 17:08:40 -08004055 switch (family) {
4056 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004057 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004058 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004059 break;
4060 case PF_INET:
4061#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4062 s = skb->secmark;
4063 if (s != 0)
4064 break;
4065#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004066 /*
4067 * Translate what netlabel gave us.
4068 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004069 if (sock != NULL && sock->sk != NULL)
4070 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004071 netlbl_secattr_init(&secattr);
4072 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4073 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004074 skp = smack_from_secattr(&secattr, ssp);
4075 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004076 }
4077 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004078 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004079 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004080#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004081 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004082#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004083 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004084 }
4085 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004086 if (s == 0)
4087 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004088 return 0;
4089}
4090
4091/**
Paul Moore07feee82009-03-27 17:10:54 -04004092 * smack_sock_graft - Initialize a newly created socket with an existing sock
4093 * @sk: child sock
4094 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004095 *
Paul Moore07feee82009-03-27 17:10:54 -04004096 * Set the smk_{in,out} state of an existing sock based on the process that
4097 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004098 */
4099static void smack_sock_graft(struct sock *sk, struct socket *parent)
4100{
4101 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004102 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004103
Paul Moore07feee82009-03-27 17:10:54 -04004104 if (sk == NULL ||
4105 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004106 return;
4107
4108 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004109 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004110 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004111 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004112}
4113
4114/**
4115 * smack_inet_conn_request - Smack access check on connect
4116 * @sk: socket involved
4117 * @skb: packet
4118 * @req: unused
4119 *
4120 * Returns 0 if a task with the packet label could write to
4121 * the socket, otherwise an error code
4122 */
4123static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4124 struct request_sock *req)
4125{
Paul Moore07feee82009-03-27 17:10:54 -04004126 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004127 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004128 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004129 struct netlbl_lsm_secattr secattr;
4130 struct sockaddr_in addr;
4131 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004132 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004133 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004134 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004135#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004136 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004137#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004138
Casey Schaufler69f287a2014-12-12 17:08:40 -08004139#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004140 if (family == PF_INET6) {
4141 /*
4142 * Handle mapped IPv4 packets arriving
4143 * via IPv6 sockets. Don't set up netlabel
4144 * processing on IPv6.
4145 */
4146 if (skb->protocol == htons(ETH_P_IP))
4147 family = PF_INET;
4148 else
4149 return 0;
4150 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004151#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004152
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004153#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4154 /*
4155 * If there is a secmark use it rather than the CIPSO label.
4156 * If there is no secmark fall back to CIPSO.
4157 * The secmark is assumed to reflect policy better.
4158 */
4159 if (skb && skb->secmark != 0) {
4160 skp = smack_from_secid(skb->secmark);
4161 goto access_check;
4162 }
4163#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4164
Paul Moore07feee82009-03-27 17:10:54 -04004165 netlbl_secattr_init(&secattr);
4166 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004167 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004168 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004169 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004170 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004171 netlbl_secattr_destroy(&secattr);
4172
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004173#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4174access_check:
4175#endif
4176
Etienne Bassetecfcc532009-04-08 20:40:06 +02004177#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004178 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4179 ad.a.u.net->family = family;
4180 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004181 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4182#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004183 /*
Paul Moore07feee82009-03-27 17:10:54 -04004184 * Receiving a packet requires that the other end be able to write
4185 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004186 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004187 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4188 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004189 if (rc != 0)
4190 return rc;
4191
4192 /*
4193 * Save the peer's label in the request_sock so we can later setup
4194 * smk_packet in the child socket so that SO_PEERCRED can report it.
4195 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004196 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004197
4198 /*
4199 * We need to decide if we want to label the incoming connection here
4200 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004201 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004202 */
4203 hdr = ip_hdr(skb);
4204 addr.sin_addr.s_addr = hdr->saddr;
4205 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004206 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004207 rcu_read_unlock();
4208
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004209 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004210 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004211 else
Paul Moore07feee82009-03-27 17:10:54 -04004212 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004213
4214 return rc;
4215}
4216
Paul Moore07feee82009-03-27 17:10:54 -04004217/**
4218 * smack_inet_csk_clone - Copy the connection information to the new socket
4219 * @sk: the new socket
4220 * @req: the connection's request_sock
4221 *
4222 * Transfer the connection's peer label to the newly created socket.
4223 */
4224static void smack_inet_csk_clone(struct sock *sk,
4225 const struct request_sock *req)
4226{
4227 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004228 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004229
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004230 if (req->peer_secid != 0) {
4231 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004232 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004233 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004234 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004235}
4236
Casey Schauflere114e472008-02-04 22:29:50 -08004237/*
4238 * Key management security hooks
4239 *
4240 * Casey has not tested key support very heavily.
4241 * The permission check is most likely too restrictive.
4242 * If you care about keys please have a look.
4243 */
4244#ifdef CONFIG_KEYS
4245
4246/**
4247 * smack_key_alloc - Set the key security blob
4248 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004249 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004250 * @flags: unused
4251 *
4252 * No allocation required
4253 *
4254 * Returns 0
4255 */
David Howellsd84f4f92008-11-14 10:39:23 +11004256static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004257 unsigned long flags)
4258{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004259 struct smack_known *skp = smk_of_task(cred->security);
4260
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004261 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004262 return 0;
4263}
4264
4265/**
4266 * smack_key_free - Clear the key security blob
4267 * @key: the object
4268 *
4269 * Clear the blob pointer
4270 */
4271static void smack_key_free(struct key *key)
4272{
4273 key->security = NULL;
4274}
4275
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004276/**
Casey Schauflere114e472008-02-04 22:29:50 -08004277 * smack_key_permission - Smack access on a key
4278 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004279 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004280 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004281 *
4282 * Return 0 if the task has read and write to the object,
4283 * an error code otherwise
4284 */
4285static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004286 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004287{
4288 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004289 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004290 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004291 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004292 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004293
4294 keyp = key_ref_to_ptr(key_ref);
4295 if (keyp == NULL)
4296 return -EINVAL;
4297 /*
4298 * If the key hasn't been initialized give it access so that
4299 * it may do so.
4300 */
4301 if (keyp->security == NULL)
4302 return 0;
4303 /*
4304 * This should not occur
4305 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004306 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004307 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004308
4309 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4310 return 0;
4311
Etienne Bassetecfcc532009-04-08 20:40:06 +02004312#ifdef CONFIG_AUDIT
4313 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4314 ad.a.u.key_struct.key = keyp->serial;
4315 ad.a.u.key_struct.key_desc = keyp->description;
4316#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004317 if (perm & KEY_NEED_READ)
4318 request = MAY_READ;
4319 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4320 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004321 rc = smk_access(tkp, keyp->security, request, &ad);
4322 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4323 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004324}
José Bollo7fc5f362015-02-17 15:41:22 +01004325
4326/*
4327 * smack_key_getsecurity - Smack label tagging the key
4328 * @key points to the key to be queried
4329 * @_buffer points to a pointer that should be set to point to the
4330 * resulting string (if no label or an error occurs).
4331 * Return the length of the string (including terminating NUL) or -ve if
4332 * an error.
4333 * May also return 0 (and a NULL buffer pointer) if there is no label.
4334 */
4335static int smack_key_getsecurity(struct key *key, char **_buffer)
4336{
4337 struct smack_known *skp = key->security;
4338 size_t length;
4339 char *copy;
4340
4341 if (key->security == NULL) {
4342 *_buffer = NULL;
4343 return 0;
4344 }
4345
4346 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4347 if (copy == NULL)
4348 return -ENOMEM;
4349 length = strlen(copy) + 1;
4350
4351 *_buffer = copy;
4352 return length;
4353}
4354
Casey Schauflere114e472008-02-04 22:29:50 -08004355#endif /* CONFIG_KEYS */
4356
4357/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004358 * Smack Audit hooks
4359 *
4360 * Audit requires a unique representation of each Smack specific
4361 * rule. This unique representation is used to distinguish the
4362 * object to be audited from remaining kernel objects and also
4363 * works as a glue between the audit hooks.
4364 *
4365 * Since repository entries are added but never deleted, we'll use
4366 * the smack_known label address related to the given audit rule as
4367 * the needed unique representation. This also better fits the smack
4368 * model where nearly everything is a label.
4369 */
4370#ifdef CONFIG_AUDIT
4371
4372/**
4373 * smack_audit_rule_init - Initialize a smack audit rule
4374 * @field: audit rule fields given from user-space (audit.h)
4375 * @op: required testing operator (=, !=, >, <, ...)
4376 * @rulestr: smack label to be audited
4377 * @vrule: pointer to save our own audit rule representation
4378 *
4379 * Prepare to audit cases where (@field @op @rulestr) is true.
4380 * The label to be audited is created if necessay.
4381 */
4382static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4383{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004384 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004385 char **rule = (char **)vrule;
4386 *rule = NULL;
4387
4388 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4389 return -EINVAL;
4390
Al Viro5af75d82008-12-16 05:59:26 -05004391 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004392 return -EINVAL;
4393
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004394 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004395 if (IS_ERR(skp))
4396 return PTR_ERR(skp);
4397
4398 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004399
4400 return 0;
4401}
4402
4403/**
4404 * smack_audit_rule_known - Distinguish Smack audit rules
4405 * @krule: rule of interest, in Audit kernel representation format
4406 *
4407 * This is used to filter Smack rules from remaining Audit ones.
4408 * If it's proved that this rule belongs to us, the
4409 * audit_rule_match hook will be called to do the final judgement.
4410 */
4411static int smack_audit_rule_known(struct audit_krule *krule)
4412{
4413 struct audit_field *f;
4414 int i;
4415
4416 for (i = 0; i < krule->field_count; i++) {
4417 f = &krule->fields[i];
4418
4419 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4420 return 1;
4421 }
4422
4423 return 0;
4424}
4425
4426/**
4427 * smack_audit_rule_match - Audit given object ?
4428 * @secid: security id for identifying the object to test
4429 * @field: audit rule flags given from user-space
4430 * @op: required testing operator
4431 * @vrule: smack internal rule presentation
4432 * @actx: audit context associated with the check
4433 *
4434 * The core Audit hook. It's used to take the decision of
4435 * whether to audit or not to audit a given object.
4436 */
4437static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4438 struct audit_context *actx)
4439{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004440 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004441 char *rule = vrule;
4442
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004443 if (unlikely(!rule)) {
4444 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004445 return -ENOENT;
4446 }
4447
4448 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4449 return 0;
4450
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004451 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004452
4453 /*
4454 * No need to do string comparisons. If a match occurs,
4455 * both pointers will point to the same smack_known
4456 * label.
4457 */
Al Viro5af75d82008-12-16 05:59:26 -05004458 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004459 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004460 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004461 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004462
4463 return 0;
4464}
4465
Casey Schaufler491a0b02016-01-26 15:08:35 -08004466/*
4467 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004468 * No memory was allocated.
4469 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004470
4471#endif /* CONFIG_AUDIT */
4472
Randy Dunlap251a2a92009-02-18 11:42:33 -08004473/**
David Quigley746df9b2013-05-22 12:50:35 -04004474 * smack_ismaclabel - check if xattr @name references a smack MAC label
4475 * @name: Full xattr name to check.
4476 */
4477static int smack_ismaclabel(const char *name)
4478{
4479 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4480}
4481
4482
4483/**
Casey Schauflere114e472008-02-04 22:29:50 -08004484 * smack_secid_to_secctx - return the smack label for a secid
4485 * @secid: incoming integer
4486 * @secdata: destination
4487 * @seclen: how long it is
4488 *
4489 * Exists for networking code.
4490 */
4491static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4492{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004493 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004494
Eric Parisd5630b92010-10-13 16:24:48 -04004495 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004496 *secdata = skp->smk_known;
4497 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004498 return 0;
4499}
4500
Randy Dunlap251a2a92009-02-18 11:42:33 -08004501/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004502 * smack_secctx_to_secid - return the secid for a smack label
4503 * @secdata: smack label
4504 * @seclen: how long result is
4505 * @secid: outgoing integer
4506 *
4507 * Exists for audit and networking code.
4508 */
David Howellse52c17642008-04-29 20:52:51 +01004509static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004510{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004511 struct smack_known *skp = smk_find_entry(secdata);
4512
4513 if (skp)
4514 *secid = skp->smk_secid;
4515 else
4516 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004517 return 0;
4518}
4519
Casey Schaufler491a0b02016-01-26 15:08:35 -08004520/*
4521 * There used to be a smack_release_secctx hook
4522 * that did nothing back when hooks were in a vector.
4523 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004524 */
Casey Schauflere114e472008-02-04 22:29:50 -08004525
David P. Quigley1ee65e32009-09-03 14:25:57 -04004526static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4527{
4528 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4529}
4530
4531static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4532{
4533 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4534}
4535
4536static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4537{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004538 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004539
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004540 *ctx = skp->smk_known;
4541 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004542 return 0;
4543}
4544
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004545static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4546{
4547
4548 struct task_smack *tsp;
4549 struct smack_known *skp;
4550 struct inode_smack *isp;
4551 struct cred *new_creds = *new;
4552
4553 if (new_creds == NULL) {
4554 new_creds = prepare_creds();
4555 if (new_creds == NULL)
4556 return -ENOMEM;
4557 }
4558
4559 tsp = new_creds->security;
4560
4561 /*
4562 * Get label from overlay inode and set it in create_sid
4563 */
4564 isp = d_inode(dentry->d_parent)->i_security;
4565 skp = isp->smk_inode;
4566 tsp->smk_task = skp;
4567 *new = new_creds;
4568 return 0;
4569}
4570
4571static int smack_inode_copy_up_xattr(const char *name)
4572{
4573 /*
4574 * Return 1 if this is the smack access Smack attribute.
4575 */
4576 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4577 return 1;
4578
4579 return -EOPNOTSUPP;
4580}
4581
4582static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4583 struct qstr *name,
4584 const struct cred *old,
4585 struct cred *new)
4586{
4587 struct task_smack *otsp = old->security;
4588 struct task_smack *ntsp = new->security;
4589 struct inode_smack *isp;
4590 int may;
4591
4592 /*
4593 * Use the process credential unless all of
4594 * the transmuting criteria are met
4595 */
4596 ntsp->smk_task = otsp->smk_task;
4597
4598 /*
4599 * the attribute of the containing directory
4600 */
4601 isp = d_inode(dentry->d_parent)->i_security;
4602
4603 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4604 rcu_read_lock();
4605 may = smk_access_entry(otsp->smk_task->smk_known,
4606 isp->smk_inode->smk_known,
4607 &otsp->smk_task->smk_rules);
4608 rcu_read_unlock();
4609
4610 /*
4611 * If the directory is transmuting and the rule
4612 * providing access is transmuting use the containing
4613 * directory label instead of the process label.
4614 */
4615 if (may > 0 && (may & MAY_TRANSMUTE))
4616 ntsp->smk_task = isp->smk_inode;
4617 }
4618 return 0;
4619}
4620
James Morrisca97d932017-02-15 00:18:51 +11004621static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004622 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4623 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4624 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004625
Casey Schauflere20b0432015-05-02 15:11:36 -07004626 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4627 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004628 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004629 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004630 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304631 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004632
Casey Schauflere20b0432015-05-02 15:11:36 -07004633 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004634
Casey Schauflere20b0432015-05-02 15:11:36 -07004635 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4636 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4637 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4638 LSM_HOOK_INIT(inode_link, smack_inode_link),
4639 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4640 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4641 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4642 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4643 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4644 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4645 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4646 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4647 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4648 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4649 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4650 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4651 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4652 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004653
Casey Schauflere20b0432015-05-02 15:11:36 -07004654 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4655 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4656 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4657 LSM_HOOK_INIT(file_lock, smack_file_lock),
4658 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4659 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4660 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4661 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4662 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4663 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004664
Casey Schauflere20b0432015-05-02 15:11:36 -07004665 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004666
Casey Schauflere20b0432015-05-02 15:11:36 -07004667 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4668 LSM_HOOK_INIT(cred_free, smack_cred_free),
4669 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4670 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004671 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004672 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4673 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4674 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4675 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4676 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4677 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4678 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4679 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4680 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4681 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4682 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4683 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4684 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004685 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004686
Casey Schauflere20b0432015-05-02 15:11:36 -07004687 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4688 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004689
Casey Schauflere20b0432015-05-02 15:11:36 -07004690 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4691 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004692
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004693 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4694 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004695 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4696 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4697 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4698 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004699
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004700 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4701 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004702 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4703 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4704 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004705
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004706 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4707 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004708 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4709 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4710 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004711
Casey Schauflere20b0432015-05-02 15:11:36 -07004712 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004713
Casey Schauflere20b0432015-05-02 15:11:36 -07004714 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4715 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004716
Casey Schauflere20b0432015-05-02 15:11:36 -07004717 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4718 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004719
Casey Schauflere20b0432015-05-02 15:11:36 -07004720 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004721 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004722#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004723 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004724#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004725 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4726 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4727 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4728 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4729 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4730 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4731 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4732 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4733 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4734 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004735
Casey Schauflere114e472008-02-04 22:29:50 -08004736 /* key management security hooks */
4737#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004738 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4739 LSM_HOOK_INIT(key_free, smack_key_free),
4740 LSM_HOOK_INIT(key_permission, smack_key_permission),
4741 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004742#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004743
4744 /* Audit hooks */
4745#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004746 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4747 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4748 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004749#endif /* CONFIG_AUDIT */
4750
Casey Schauflere20b0432015-05-02 15:11:36 -07004751 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4752 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4753 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004754 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4755 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4756 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004757 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4758 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4759 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004760};
4761
Etienne Basset7198e2e2009-03-24 20:53:24 +01004762
Casey Schaufler86812bb2012-04-17 18:55:46 -07004763static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004764{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004765 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004766 * Initialize rule list locks
4767 */
4768 mutex_init(&smack_known_huh.smk_rules_lock);
4769 mutex_init(&smack_known_hat.smk_rules_lock);
4770 mutex_init(&smack_known_floor.smk_rules_lock);
4771 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004772 mutex_init(&smack_known_web.smk_rules_lock);
4773 /*
4774 * Initialize rule lists
4775 */
4776 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4777 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4778 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4779 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004780 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4781 /*
4782 * Create the known labels list
4783 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004784 smk_insert_entry(&smack_known_huh);
4785 smk_insert_entry(&smack_known_hat);
4786 smk_insert_entry(&smack_known_star);
4787 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004788 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004789}
4790
Casey Schauflere114e472008-02-04 22:29:50 -08004791/**
4792 * smack_init - initialize the smack system
4793 *
4794 * Returns 0
4795 */
4796static __init int smack_init(void)
4797{
David Howellsd84f4f92008-11-14 10:39:23 +11004798 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004799 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004800
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004801 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004802 return 0;
4803
Rohit1a5b4722014-10-15 17:40:41 +05304804 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4805 if (!smack_inode_cache)
4806 return -ENOMEM;
4807
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004808 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4809 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304810 if (tsp == NULL) {
4811 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004812 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304813 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004814
José Bollod21b7b02015-10-02 15:15:56 +02004815 smack_enabled = 1;
4816
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004817 pr_info("Smack: Initializing.\n");
4818#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4819 pr_info("Smack: Netfilter enabled.\n");
4820#endif
4821#ifdef SMACK_IPV6_PORT_LABELING
4822 pr_info("Smack: IPv6 port labeling enabled.\n");
4823#endif
4824#ifdef SMACK_IPV6_SECMARK_LABELING
4825 pr_info("Smack: IPv6 Netfilter enabled.\n");
4826#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004827
4828 /*
4829 * Set the security state for the initial task.
4830 */
David Howellsd84f4f92008-11-14 10:39:23 +11004831 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004832 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004833
Casey Schaufler86812bb2012-04-17 18:55:46 -07004834 /* initialize the smack_known_list */
4835 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004836
4837 /*
4838 * Register with LSM
4839 */
Casey Schauflerd69dece52017-01-18 17:09:05 -08004840 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
Casey Schauflere114e472008-02-04 22:29:50 -08004841
4842 return 0;
4843}
4844
4845/*
4846 * Smack requires early initialization in order to label
4847 * all processes and objects when they are created.
4848 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004849DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004850 .name = "smack",
Kees Cook3d6e5f62018-10-10 17:18:23 -07004851 .init = smack_init,
4852};