blob: 70d3066e69fedf93465a33aed3db00b94251aa7b [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
570/**
571 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800572 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800573 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800574 *
575 * Returns 0 on success or -ENOMEM on error.
576 *
577 * Copy the Smack specific mount options out of the mount
578 * options list.
579 */
Eric Parise0007522008-03-05 10:31:54 -0500580static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800581{
582 char *cp, *commap, *otheropts, *dp;
583
Casey Schauflere114e472008-02-04 22:29:50 -0800584 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
585 if (otheropts == NULL)
586 return -ENOMEM;
587
588 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
589 if (strstr(cp, SMK_FSDEFAULT) == cp)
590 dp = smackopts;
591 else if (strstr(cp, SMK_FSFLOOR) == cp)
592 dp = smackopts;
593 else if (strstr(cp, SMK_FSHAT) == cp)
594 dp = smackopts;
595 else if (strstr(cp, SMK_FSROOT) == cp)
596 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700597 else if (strstr(cp, SMK_FSTRANS) == cp)
598 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800599 else
600 dp = otheropts;
601
602 commap = strchr(cp, ',');
603 if (commap != NULL)
604 *commap = '\0';
605
606 if (*dp != '\0')
607 strcat(dp, ",");
608 strcat(dp, cp);
609 }
610
611 strcpy(orig, otheropts);
612 free_page((unsigned long)otheropts);
613
614 return 0;
615}
616
617/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530618 * smack_parse_opts_str - parse Smack specific mount options
619 * @options: mount options string
620 * @opts: where to store converted mount opts
621 *
622 * Returns 0 on success or -ENOMEM on error.
623 *
624 * converts Smack specific mount options to generic security option format
625 */
626static int smack_parse_opts_str(char *options,
627 struct security_mnt_opts *opts)
628{
629 char *p;
Casey Schaufler3d04c922015-08-12 11:56:02 -0700630 char *fsdefault = NULL;
631 char *fsfloor = NULL;
632 char *fshat = NULL;
633 char *fsroot = NULL;
634 char *fstransmute = NULL;
635 int rc = -ENOMEM;
636 int num_mnt_opts = 0;
637 int token;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530638
639 opts->num_mnt_opts = 0;
640
641 if (!options)
642 return 0;
643
644 while ((p = strsep(&options, ",")) != NULL) {
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530645 substring_t args[MAX_OPT_ARGS];
646
647 if (!*p)
648 continue;
649
Casey Schaufler3d04c922015-08-12 11:56:02 -0700650 token = match_token(p, smk_mount_tokens, args);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530651
652 switch (token) {
653 case Opt_fsdefault:
654 if (fsdefault)
655 goto out_opt_err;
656 fsdefault = match_strdup(&args[0]);
657 if (!fsdefault)
658 goto out_err;
659 break;
660 case Opt_fsfloor:
661 if (fsfloor)
662 goto out_opt_err;
663 fsfloor = match_strdup(&args[0]);
664 if (!fsfloor)
665 goto out_err;
666 break;
667 case Opt_fshat:
668 if (fshat)
669 goto out_opt_err;
670 fshat = match_strdup(&args[0]);
671 if (!fshat)
672 goto out_err;
673 break;
674 case Opt_fsroot:
675 if (fsroot)
676 goto out_opt_err;
677 fsroot = match_strdup(&args[0]);
678 if (!fsroot)
679 goto out_err;
680 break;
681 case Opt_fstransmute:
682 if (fstransmute)
683 goto out_opt_err;
684 fstransmute = match_strdup(&args[0]);
685 if (!fstransmute)
686 goto out_err;
687 break;
688 default:
689 rc = -EINVAL;
690 pr_warn("Smack: unknown mount option\n");
691 goto out_err;
692 }
693 }
694
Tetsuo Handa8c15d662016-11-14 20:12:55 +0900695 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_KERNEL);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530696 if (!opts->mnt_opts)
697 goto out_err;
698
699 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
Tetsuo Handa8c15d662016-11-14 20:12:55 +0900700 GFP_KERNEL);
Tetsuo Handac3c8dc92017-03-24 20:42:05 +0900701 if (!opts->mnt_opts_flags)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530702 goto out_err;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530703
704 if (fsdefault) {
705 opts->mnt_opts[num_mnt_opts] = fsdefault;
706 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
707 }
708 if (fsfloor) {
709 opts->mnt_opts[num_mnt_opts] = fsfloor;
710 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
711 }
712 if (fshat) {
713 opts->mnt_opts[num_mnt_opts] = fshat;
714 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
715 }
716 if (fsroot) {
717 opts->mnt_opts[num_mnt_opts] = fsroot;
718 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
719 }
720 if (fstransmute) {
721 opts->mnt_opts[num_mnt_opts] = fstransmute;
722 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
723 }
724
725 opts->num_mnt_opts = num_mnt_opts;
726 return 0;
727
728out_opt_err:
729 rc = -EINVAL;
730 pr_warn("Smack: duplicate mount options\n");
731
732out_err:
733 kfree(fsdefault);
734 kfree(fsfloor);
735 kfree(fshat);
736 kfree(fsroot);
737 kfree(fstransmute);
738 return rc;
739}
740
741/**
742 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800743 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530744 * @opts: Smack mount options
745 * @kern_flags: mount option from kernel space or user space
746 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800747 *
748 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530749 *
750 * Allow filesystems with binary mount data to explicitly set Smack mount
751 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800752 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530753static int smack_set_mnt_opts(struct super_block *sb,
754 struct security_mnt_opts *opts,
755 unsigned long kern_flags,
756 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800757{
758 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000759 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800760 struct superblock_smack *sp = sb->s_security;
761 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800762 struct smack_known *skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530763 int i;
764 int num_opts = opts->num_mnt_opts;
Casey Schauflere830b392013-05-22 18:43:07 -0700765 int transmute = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800766
Seth Forshee9f50eda2015-09-23 15:16:06 -0500767 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800768 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700769
Himanshu Shukla2097f592016-11-10 16:19:52 +0530770 if (!smack_privileged(CAP_MAC_ADMIN)) {
771 /*
772 * Unprivileged mounts don't get to specify Smack values.
773 */
774 if (num_opts)
775 return -EPERM;
776 /*
777 * Unprivileged mounts get root and default from the caller.
778 */
779 skp = smk_of_current();
780 sp->smk_root = skp;
781 sp->smk_default = skp;
782 /*
783 * For a handful of fs types with no user-controlled
784 * backing store it's okay to trust security labels
785 * in the filesystem. The rest are untrusted.
786 */
787 if (sb->s_user_ns != &init_user_ns &&
788 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
789 sb->s_magic != RAMFS_MAGIC) {
790 transmute = 1;
791 sp->smk_flags |= SMK_SB_UNTRUSTED;
792 }
793 }
794
Seth Forshee9f50eda2015-09-23 15:16:06 -0500795 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800796
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530797 for (i = 0; i < num_opts; i++) {
798 switch (opts->mnt_opts_flags[i]) {
799 case FSDEFAULT_MNT:
800 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200801 if (IS_ERR(skp))
802 return PTR_ERR(skp);
803 sp->smk_default = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530804 break;
805 case FSFLOOR_MNT:
806 skp = smk_import_entry(opts->mnt_opts[i], 0);
807 if (IS_ERR(skp))
808 return PTR_ERR(skp);
809 sp->smk_floor = skp;
810 break;
811 case FSHAT_MNT:
812 skp = smk_import_entry(opts->mnt_opts[i], 0);
813 if (IS_ERR(skp))
814 return PTR_ERR(skp);
815 sp->smk_hat = skp;
816 break;
817 case FSROOT_MNT:
818 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200819 if (IS_ERR(skp))
820 return PTR_ERR(skp);
821 sp->smk_root = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530822 break;
823 case FSTRANS_MNT:
824 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200825 if (IS_ERR(skp))
826 return PTR_ERR(skp);
827 sp->smk_root = skp;
828 transmute = 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530829 break;
830 default:
831 break;
Casey Schauflere114e472008-02-04 22:29:50 -0800832 }
833 }
834
835 /*
836 * Initialize the root inode.
837 */
838 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100839 if (isp == NULL) {
840 isp = new_inode_smack(sp->smk_root);
841 if (isp == NULL)
842 return -ENOMEM;
843 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700844 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800845 isp->smk_inode = sp->smk_root;
846
Casey Schauflere830b392013-05-22 18:43:07 -0700847 if (transmute)
848 isp->smk_flags |= SMK_INODE_TRANSMUTE;
849
Casey Schauflere114e472008-02-04 22:29:50 -0800850 return 0;
851}
852
853/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530854 * smack_sb_kern_mount - Smack specific mount processing
855 * @sb: the file system superblock
856 * @flags: the mount flags
857 * @data: the smack mount options
858 *
859 * Returns 0 on success, an error code on failure
860 */
861static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
862{
863 int rc = 0;
864 char *options = data;
865 struct security_mnt_opts opts;
866
867 security_init_mnt_opts(&opts);
868
869 if (!options)
870 goto out;
871
872 rc = smack_parse_opts_str(options, &opts);
873 if (rc)
874 goto out_err;
875
876out:
877 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
878
879out_err:
880 security_free_mnt_opts(&opts);
881 return rc;
882}
883
884/**
Casey Schauflere114e472008-02-04 22:29:50 -0800885 * smack_sb_statfs - Smack check on statfs
886 * @dentry: identifies the file system in question
887 *
888 * Returns 0 if current can read the floor of the filesystem,
889 * and error code otherwise
890 */
891static int smack_sb_statfs(struct dentry *dentry)
892{
893 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200894 int rc;
895 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800896
Eric Parisa2694342011-04-25 13:10:27 -0400897 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200898 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
899
900 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700901 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200902 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800903}
904
Casey Schauflere114e472008-02-04 22:29:50 -0800905/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800906 * BPRM hooks
907 */
908
Casey Schauflerce8a4322011-09-29 18:21:01 -0700909/**
910 * smack_bprm_set_creds - set creds for exec
911 * @bprm: the exec information
912 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100913 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700914 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800915static int smack_bprm_set_creds(struct linux_binprm *bprm)
916{
Al Viro496ad9a2013-01-23 17:07:38 -0500917 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300918 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800919 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500920 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800921 int rc;
922
Kees Cookddb4a142017-07-18 15:25:23 -0700923 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800924 return 0;
925
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300926 isp = inode->i_security;
927 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800928 return 0;
929
Seth Forshee809c02e2016-04-26 14:36:22 -0500930 sbsp = inode->i_sb->s_security;
931 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
932 isp->smk_task != sbsp->smk_root)
933 return 0;
934
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300935 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100936 struct task_struct *tracer;
937 rc = 0;
938
939 rcu_read_lock();
940 tracer = ptrace_parent(current);
941 if (likely(tracer != NULL))
942 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200943 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100944 PTRACE_MODE_ATTACH,
945 __func__);
946 rcu_read_unlock();
947
948 if (rc != 0)
949 return rc;
950 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300951 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800952
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300953 bsp->smk_task = isp->smk_task;
954 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800955
Kees Cookccbb6e12017-07-18 15:25:26 -0700956 /* Decide if this is a secure exec. */
957 if (bsp->smk_task != bsp->smk_forked)
958 bprm->secureexec = 1;
959
Casey Schaufler676dac42010-12-02 06:43:39 -0800960 return 0;
961}
962
963/*
Casey Schauflere114e472008-02-04 22:29:50 -0800964 * Inode hooks
965 */
966
967/**
968 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800969 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800970 *
971 * Returns 0 if it gets a blob, -ENOMEM otherwise
972 */
973static int smack_inode_alloc_security(struct inode *inode)
974{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700975 struct smack_known *skp = smk_of_current();
976
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200977 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800978 if (inode->i_security == NULL)
979 return -ENOMEM;
980 return 0;
981}
982
983/**
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530984 * smack_inode_free_rcu - Free inode_smack blob from cache
985 * @head: the rcu_head for getting inode_smack pointer
986 *
987 * Call back function called from call_rcu() to free
988 * the i_security blob pointer in inode
989 */
990static void smack_inode_free_rcu(struct rcu_head *head)
991{
992 struct inode_smack *issp;
993
994 issp = container_of(head, struct inode_smack, smk_rcu);
995 kmem_cache_free(smack_inode_cache, issp);
996}
997
998/**
999 * smack_inode_free_security - free an inode blob using call_rcu()
Randy Dunlap251a2a92009-02-18 11:42:33 -08001000 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -08001001 *
Himanshu Shukla3d4f6732016-11-23 11:59:19 +05301002 * Clears the blob pointer in inode using RCU
Casey Schauflere114e472008-02-04 22:29:50 -08001003 */
1004static void smack_inode_free_security(struct inode *inode)
1005{
Himanshu Shukla3d4f6732016-11-23 11:59:19 +05301006 struct inode_smack *issp = inode->i_security;
1007
1008 /*
1009 * The inode may still be referenced in a path walk and
1010 * a call to smack_inode_permission() can be made
1011 * after smack_inode_free_security() is called.
1012 * To avoid race condition free the i_security via RCU
1013 * and leave the current inode->i_security pointer intact.
1014 * The inode will be freed after the RCU grace period too.
1015 */
1016 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
Casey Schauflere114e472008-02-04 22:29:50 -08001017}
1018
1019/**
1020 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001021 * @inode: the newly created inode
1022 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -05001023 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001024 * @name: where to put the attribute name
1025 * @value: where to put the attribute value
1026 * @len: where to put the length of the attribute
1027 *
1028 * Returns 0 if it all works out, -ENOMEM if there's no memory
1029 */
1030static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09001031 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05001032 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -08001033{
Casey Schaufler2267b132012-03-13 19:14:19 -07001034 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001035 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001036 struct smack_known *isp = smk_of_inode(inode);
1037 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001038 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08001039
Tetsuo Handa95489062013-07-25 05:44:02 +09001040 if (name)
1041 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -08001042
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001043 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001044 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001045 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1046 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001047 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001048
1049 /*
1050 * If the access rule allows transmutation and
1051 * the directory requests transmutation then
1052 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001053 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001054 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001055 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001056 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001057 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001058 issp->smk_flags |= SMK_INODE_CHANGED;
1059 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001060
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001061 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001062 if (*value == NULL)
1063 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001064
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001065 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001066 }
Casey Schauflere114e472008-02-04 22:29:50 -08001067
1068 return 0;
1069}
1070
1071/**
1072 * smack_inode_link - Smack check on link
1073 * @old_dentry: the existing object
1074 * @dir: unused
1075 * @new_dentry: the new object
1076 *
1077 * Returns 0 if access is permitted, an error code otherwise
1078 */
1079static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1080 struct dentry *new_dentry)
1081{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001082 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001083 struct smk_audit_info ad;
1084 int rc;
1085
Eric Parisa2694342011-04-25 13:10:27 -04001086 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001087 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001088
David Howellsc6f493d2015-03-17 22:26:22 +00001089 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001090 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001091 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001092
David Howells88025652015-01-29 12:02:32 +00001093 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001094 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001095 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1096 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001097 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001098 }
1099
1100 return rc;
1101}
1102
1103/**
1104 * smack_inode_unlink - Smack check on inode deletion
1105 * @dir: containing directory object
1106 * @dentry: file to unlink
1107 *
1108 * Returns 0 if current can write the containing directory
1109 * and the object, error code otherwise
1110 */
1111static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1112{
David Howellsc6f493d2015-03-17 22:26:22 +00001113 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001114 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001115 int rc;
1116
Eric Parisa2694342011-04-25 13:10:27 -04001117 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1119
Casey Schauflere114e472008-02-04 22:29:50 -08001120 /*
1121 * You need write access to the thing you're unlinking
1122 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001123 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001124 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001125 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001126 /*
1127 * You also need write access to the containing directory
1128 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001129 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001130 smk_ad_setfield_u_fs_inode(&ad, dir);
1131 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001132 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001133 }
Casey Schauflere114e472008-02-04 22:29:50 -08001134 return rc;
1135}
1136
1137/**
1138 * smack_inode_rmdir - Smack check on directory deletion
1139 * @dir: containing directory object
1140 * @dentry: directory to unlink
1141 *
1142 * Returns 0 if current can write the containing directory
1143 * and the directory, error code otherwise
1144 */
1145static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1146{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001147 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001148 int rc;
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, dentry);
1152
Casey Schauflere114e472008-02-04 22:29:50 -08001153 /*
1154 * You need write access to the thing you're removing
1155 */
David Howellsc6f493d2015-03-17 22:26:22 +00001156 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1157 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001158 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001159 /*
1160 * You also need write access to the containing directory
1161 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001162 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001163 smk_ad_setfield_u_fs_inode(&ad, dir);
1164 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001165 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001166 }
Casey Schauflere114e472008-02-04 22:29:50 -08001167
1168 return rc;
1169}
1170
1171/**
1172 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001173 * @old_inode: unused
1174 * @old_dentry: the old object
1175 * @new_inode: unused
1176 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001177 *
1178 * Read and write access is required on both the old and
1179 * new directories.
1180 *
1181 * Returns 0 if access is permitted, an error code otherwise
1182 */
1183static int smack_inode_rename(struct inode *old_inode,
1184 struct dentry *old_dentry,
1185 struct inode *new_inode,
1186 struct dentry *new_dentry)
1187{
1188 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001189 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001190 struct smk_audit_info ad;
1191
Eric Parisa2694342011-04-25 13:10:27 -04001192 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001193 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001194
David Howellsc6f493d2015-03-17 22:26:22 +00001195 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001196 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001197 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001198
David Howells88025652015-01-29 12:02:32 +00001199 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001200 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001201 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1202 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001203 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001204 }
Casey Schauflere114e472008-02-04 22:29:50 -08001205 return rc;
1206}
1207
1208/**
1209 * smack_inode_permission - Smack version of permission()
1210 * @inode: the inode in question
1211 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001212 *
1213 * This is the important Smack hook.
1214 *
1215 * Returns 0 if access is permitted, -EACCES otherwise
1216 */
Al Viroe74f71e2011-06-20 19:38:15 -04001217static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001218{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001219 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001220 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001221 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001222 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001223
1224 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001225 /*
1226 * No permission to check. Existence test. Yup, it's there.
1227 */
1228 if (mask == 0)
1229 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001230
Seth Forshee9f50eda2015-09-23 15:16:06 -05001231 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1232 if (smk_of_inode(inode) != sbsp->smk_root)
1233 return -EACCES;
1234 }
1235
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001236 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001237 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001238 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001239 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001240 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001241 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1242 rc = smk_bu_inode(inode, mask, rc);
1243 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001244}
1245
1246/**
1247 * smack_inode_setattr - Smack check for setting attributes
1248 * @dentry: the object
1249 * @iattr: for the force flag
1250 *
1251 * Returns 0 if access is permitted, an error code otherwise
1252 */
1253static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1254{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001255 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001256 int rc;
1257
Casey Schauflere114e472008-02-04 22:29:50 -08001258 /*
1259 * Need to allow for clearing the setuid bit.
1260 */
1261 if (iattr->ia_valid & ATTR_FORCE)
1262 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001263 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001264 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001265
David Howellsc6f493d2015-03-17 22:26:22 +00001266 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1267 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001268 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001269}
1270
1271/**
1272 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001273 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001274 * @dentry: the object
1275 *
1276 * Returns 0 if access is permitted, an error code otherwise
1277 */
Al Viro3f7036a2015-03-08 19:28:30 -04001278static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001279{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001280 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001281 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001282 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001283
Eric Parisf48b7392011-04-25 12:54:27 -04001284 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001285 smk_ad_setfield_u_fs_path(&ad, *path);
1286 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1287 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001288 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001289}
1290
1291/**
1292 * smack_inode_setxattr - Smack check for setting xattrs
1293 * @dentry: the object
1294 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001295 * @value: value of the attribute
1296 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001297 * @flags: unused
1298 *
1299 * This protects the Smack attribute explicitly.
1300 *
1301 * Returns 0 if access is permitted, an error code otherwise
1302 */
David Howells8f0cfa52008-04-29 00:59:41 -07001303static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1304 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001305{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001306 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001307 struct smack_known *skp;
1308 int check_priv = 0;
1309 int check_import = 0;
1310 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001311 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001312
Casey Schaufler19760ad2013-12-16 16:27:26 -08001313 /*
1314 * Check label validity here so import won't fail in post_setxattr
1315 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001316 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1317 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001318 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1319 check_priv = 1;
1320 check_import = 1;
1321 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1322 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1323 check_priv = 1;
1324 check_import = 1;
1325 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001326 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001327 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001328 if (size != TRANS_TRUE_SIZE ||
1329 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1330 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001331 } else
1332 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1333
Casey Schaufler19760ad2013-12-16 16:27:26 -08001334 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1335 rc = -EPERM;
1336
1337 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001338 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001339 if (IS_ERR(skp))
1340 rc = PTR_ERR(skp);
1341 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001342 (skp == &smack_known_star || skp == &smack_known_web)))
1343 rc = -EINVAL;
1344 }
1345
Eric Parisa2694342011-04-25 13:10:27 -04001346 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001347 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1348
Casey Schauflerd166c802014-08-27 14:51:27 -07001349 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001350 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1351 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001352 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001353
1354 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001355}
1356
1357/**
1358 * smack_inode_post_setxattr - Apply the Smack update approved above
1359 * @dentry: object
1360 * @name: attribute name
1361 * @value: attribute value
1362 * @size: attribute size
1363 * @flags: unused
1364 *
1365 * Set the pointer in the inode blob to the entry found
1366 * in the master label list.
1367 */
David Howells8f0cfa52008-04-29 00:59:41 -07001368static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1369 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001370{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001371 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001372 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001373
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001374 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1375 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1376 return;
1377 }
1378
Casey Schaufler676dac42010-12-02 06:43:39 -08001379 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001380 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001381 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001382 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001383 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001384 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001385 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001386 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001387 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001388 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001389 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001390 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001391 }
Casey Schauflere114e472008-02-04 22:29:50 -08001392
1393 return;
1394}
1395
Casey Schauflerce8a4322011-09-29 18:21:01 -07001396/**
Casey Schauflere114e472008-02-04 22:29:50 -08001397 * smack_inode_getxattr - Smack check on getxattr
1398 * @dentry: the object
1399 * @name: unused
1400 *
1401 * Returns 0 if access is permitted, an error code otherwise
1402 */
David Howells8f0cfa52008-04-29 00:59:41 -07001403static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001404{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001405 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001406 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001407
Eric Parisa2694342011-04-25 13:10:27 -04001408 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001409 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1410
David Howellsc6f493d2015-03-17 22:26:22 +00001411 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1412 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001413 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001414}
1415
Casey Schauflerce8a4322011-09-29 18:21:01 -07001416/**
Casey Schauflere114e472008-02-04 22:29:50 -08001417 * smack_inode_removexattr - Smack check on removexattr
1418 * @dentry: the object
1419 * @name: name of the attribute
1420 *
1421 * Removing the Smack attribute requires CAP_MAC_ADMIN
1422 *
1423 * Returns 0 if access is permitted, an error code otherwise
1424 */
David Howells8f0cfa52008-04-29 00:59:41 -07001425static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001426{
Casey Schaufler676dac42010-12-02 06:43:39 -08001427 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001428 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001429 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001430
Casey Schauflerbcdca222008-02-23 15:24:04 -08001431 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1432 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001433 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001434 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001435 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301436 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001437 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001438 rc = -EPERM;
1439 } else
1440 rc = cap_inode_removexattr(dentry, name);
1441
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001442 if (rc != 0)
1443 return rc;
1444
Eric Parisa2694342011-04-25 13:10:27 -04001445 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001446 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001447
David Howellsc6f493d2015-03-17 22:26:22 +00001448 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1449 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001450 if (rc != 0)
1451 return rc;
1452
David Howellsc6f493d2015-03-17 22:26:22 +00001453 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001454 /*
1455 * Don't do anything special for these.
1456 * XATTR_NAME_SMACKIPIN
1457 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001458 */
José Bollo80124952016-01-12 21:23:40 +01001459 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001460 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001461 struct superblock_smack *sbsp = sbp->s_security;
1462
1463 isp->smk_inode = sbsp->smk_default;
1464 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001465 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001466 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001467 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001468 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1469 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001470
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001471 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001472}
1473
1474/**
1475 * smack_inode_getsecurity - get smack xattrs
1476 * @inode: the object
1477 * @name: attribute name
1478 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001479 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001480 *
1481 * Returns the size of the attribute or an error code
1482 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001483static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001484 const char *name, void **buffer,
1485 bool alloc)
1486{
1487 struct socket_smack *ssp;
1488 struct socket *sock;
1489 struct super_block *sbp;
1490 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001491 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001492
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001493 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001494 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001495 else {
1496 /*
1497 * The rest of the Smack xattrs are only on sockets.
1498 */
1499 sbp = ip->i_sb;
1500 if (sbp->s_magic != SOCKFS_MAGIC)
1501 return -EOPNOTSUPP;
1502
1503 sock = SOCKET_I(ip);
1504 if (sock == NULL || sock->sk == NULL)
1505 return -EOPNOTSUPP;
1506
1507 ssp = sock->sk->sk_security;
1508
1509 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1510 isp = ssp->smk_in;
1511 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1512 isp = ssp->smk_out;
1513 else
1514 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001515 }
1516
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001517 if (alloc) {
1518 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1519 if (*buffer == NULL)
1520 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001521 }
1522
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001523 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001524}
1525
1526
1527/**
1528 * smack_inode_listsecurity - list the Smack attributes
1529 * @inode: the object
1530 * @buffer: where they go
1531 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001532 */
1533static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1534 size_t buffer_size)
1535{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001536 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001537
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001538 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001539 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001540
1541 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001542}
1543
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001544/**
1545 * smack_inode_getsecid - Extract inode's security id
1546 * @inode: inode to extract the info from
1547 * @secid: where result will be saved
1548 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001549static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001550{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001551 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001552
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001553 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001554}
1555
Casey Schauflere114e472008-02-04 22:29:50 -08001556/*
1557 * File Hooks
1558 */
1559
Casey Schaufler491a0b02016-01-26 15:08:35 -08001560/*
1561 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001562 *
1563 * Should access checks be done on each read or write?
1564 * UNICOS and SELinux say yes.
1565 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1566 *
1567 * I'll say no for now. Smack does not do the frequent
1568 * label changing that SELinux does.
1569 */
Casey Schauflere114e472008-02-04 22:29:50 -08001570
1571/**
1572 * smack_file_alloc_security - assign a file security blob
1573 * @file: the object
1574 *
1575 * The security blob for a file is a pointer to the master
1576 * label list, so no allocation is done.
1577 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001578 * f_security is the owner security information. It
1579 * isn't used on file access checks, it's for send_sigio.
1580 *
Casey Schauflere114e472008-02-04 22:29:50 -08001581 * Returns 0
1582 */
1583static int smack_file_alloc_security(struct file *file)
1584{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001585 struct smack_known *skp = smk_of_current();
1586
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001587 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001588 return 0;
1589}
1590
1591/**
1592 * smack_file_free_security - clear a file security blob
1593 * @file: the object
1594 *
1595 * The security blob for a file is a pointer to the master
1596 * label list, so no memory is freed.
1597 */
1598static void smack_file_free_security(struct file *file)
1599{
1600 file->f_security = NULL;
1601}
1602
1603/**
1604 * smack_file_ioctl - Smack check on ioctls
1605 * @file: the object
1606 * @cmd: what to do
1607 * @arg: unused
1608 *
1609 * Relies heavily on the correct use of the ioctl command conventions.
1610 *
1611 * Returns 0 if allowed, error code otherwise
1612 */
1613static int smack_file_ioctl(struct file *file, unsigned int cmd,
1614 unsigned long arg)
1615{
1616 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001617 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001618 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001619
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001620 if (unlikely(IS_PRIVATE(inode)))
1621 return 0;
1622
Eric Parisf48b7392011-04-25 12:54:27 -04001623 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001624 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001625
Casey Schauflerd166c802014-08-27 14:51:27 -07001626 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001627 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001628 rc = smk_bu_file(file, MAY_WRITE, rc);
1629 }
Casey Schauflere114e472008-02-04 22:29:50 -08001630
Casey Schauflerd166c802014-08-27 14:51:27 -07001631 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001632 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001633 rc = smk_bu_file(file, MAY_READ, rc);
1634 }
Casey Schauflere114e472008-02-04 22:29:50 -08001635
1636 return rc;
1637}
1638
1639/**
1640 * smack_file_lock - Smack check on file locking
1641 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001642 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001643 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001644 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001645 */
1646static int smack_file_lock(struct file *file, unsigned int cmd)
1647{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001648 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001649 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001650 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001651
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001652 if (unlikely(IS_PRIVATE(inode)))
1653 return 0;
1654
Eric Paris92f42502011-04-25 13:15:55 -04001655 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1656 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001657 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001658 rc = smk_bu_file(file, MAY_LOCK, rc);
1659 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001660}
1661
1662/**
1663 * smack_file_fcntl - Smack check on fcntl
1664 * @file: the object
1665 * @cmd: what action to check
1666 * @arg: unused
1667 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001668 * Generally these operations are harmless.
1669 * File locking operations present an obvious mechanism
1670 * for passing information, so they require write access.
1671 *
Casey Schauflere114e472008-02-04 22:29:50 -08001672 * Returns 0 if current has access, error code otherwise
1673 */
1674static int smack_file_fcntl(struct file *file, unsigned int cmd,
1675 unsigned long arg)
1676{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001677 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001678 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001679 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001680
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001681 if (unlikely(IS_PRIVATE(inode)))
1682 return 0;
1683
Casey Schauflere114e472008-02-04 22:29:50 -08001684 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001685 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001686 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001687 case F_SETLK:
1688 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001689 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1690 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001691 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001692 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001693 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001694 case F_SETOWN:
1695 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001696 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1697 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001698 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001699 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001700 break;
1701 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001702 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001703 }
1704
1705 return rc;
1706}
1707
1708/**
Al Viroe5467852012-05-30 13:30:51 -04001709 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001710 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1711 * if mapping anonymous memory.
1712 * @file contains the file structure for file to map (may be NULL).
1713 * @reqprot contains the protection requested by the application.
1714 * @prot contains the protection that will be applied by the kernel.
1715 * @flags contains the operational flags.
1716 * Return 0 if permission is granted.
1717 */
Al Viroe5467852012-05-30 13:30:51 -04001718static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001719 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001720 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001721{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001722 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001723 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001724 struct smack_rule *srp;
1725 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001726 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001727 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001728 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001729 int may;
1730 int mmay;
1731 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001732 int rc;
1733
Al Viro496ad9a2013-01-23 17:07:38 -05001734 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001735 return 0;
1736
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001737 if (unlikely(IS_PRIVATE(file_inode(file))))
1738 return 0;
1739
Al Viro496ad9a2013-01-23 17:07:38 -05001740 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001741 if (isp->smk_mmap == NULL)
1742 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001743 sbsp = file_inode(file)->i_sb->s_security;
1744 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1745 isp->smk_mmap != sbsp->smk_root)
1746 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001747 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001748
1749 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001750 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001751 rc = 0;
1752
1753 rcu_read_lock();
1754 /*
1755 * For each Smack rule associated with the subject
1756 * label verify that the SMACK64MMAP also has access
1757 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001758 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001759 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001760 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001761 /*
1762 * Matching labels always allows access.
1763 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001764 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001765 continue;
1766 /*
1767 * If there is a matching local rule take
1768 * that into account as well.
1769 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001770 may = smk_access_entry(srp->smk_subject->smk_known,
1771 okp->smk_known,
1772 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001773 if (may == -ENOENT)
1774 may = srp->smk_access;
1775 else
1776 may &= srp->smk_access;
1777 /*
1778 * If may is zero the SMACK64MMAP subject can't
1779 * possibly have less access.
1780 */
1781 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001782 continue;
1783
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001784 /*
1785 * Fetch the global list entry.
1786 * If there isn't one a SMACK64MMAP subject
1787 * can't have as much access as current.
1788 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001789 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1790 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001791 if (mmay == -ENOENT) {
1792 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001793 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001794 }
1795 /*
1796 * If there is a local entry it modifies the
1797 * potential access, too.
1798 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001799 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1800 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001801 if (tmay != -ENOENT)
1802 mmay &= tmay;
1803
1804 /*
1805 * If there is any access available to current that is
1806 * not available to a SMACK64MMAP subject
1807 * deny access.
1808 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001809 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001810 rc = -EACCES;
1811 break;
1812 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001813 }
1814
1815 rcu_read_unlock();
1816
1817 return rc;
1818}
1819
1820/**
Casey Schauflere114e472008-02-04 22:29:50 -08001821 * smack_file_set_fowner - set the file security blob value
1822 * @file: object in question
1823 *
Casey Schauflere114e472008-02-04 22:29:50 -08001824 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001825static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001826{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001827 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001828}
1829
1830/**
1831 * smack_file_send_sigiotask - Smack on sigio
1832 * @tsk: The target task
1833 * @fown: the object the signal come from
1834 * @signum: unused
1835 *
1836 * Allow a privileged task to get signals even if it shouldn't
1837 *
1838 * Returns 0 if a subject with the object's smack could
1839 * write to the task, an error code otherwise.
1840 */
1841static int smack_file_send_sigiotask(struct task_struct *tsk,
1842 struct fown_struct *fown, int signum)
1843{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001844 struct smack_known *skp;
1845 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001846 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001847 struct file *file;
1848 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001849 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001850
1851 /*
1852 * struct fown_struct is never outside the context of a struct file
1853 */
1854 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001855
Etienne Bassetecfcc532009-04-08 20:40:06 +02001856 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001857 skp = file->f_security;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001858 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1859 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001860
1861 rcu_read_lock();
1862 tcred = __task_cred(tsk);
1863 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001864 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001865 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001866
1867 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1868 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001869 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001870 return rc;
1871}
1872
1873/**
1874 * smack_file_receive - Smack file receive check
1875 * @file: the object
1876 *
1877 * Returns 0 if current has access, error code otherwise
1878 */
1879static int smack_file_receive(struct file *file)
1880{
Casey Schauflerd166c802014-08-27 14:51:27 -07001881 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001882 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001883 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001884 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001885 struct socket *sock;
1886 struct task_smack *tsp;
1887 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001888
Seung-Woo Kim97775822015-04-17 15:25:04 +09001889 if (unlikely(IS_PRIVATE(inode)))
1890 return 0;
1891
Casey Schaufler4482a442013-12-30 17:37:45 -08001892 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001893 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001894
Casey Schaufler51d59af2017-05-31 08:53:42 -07001895 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001896 sock = SOCKET_I(inode);
1897 ssp = sock->sk->sk_security;
1898 tsp = current_security();
1899 /*
1900 * If the receiving process can't write to the
1901 * passed socket or if the passed socket can't
1902 * write to the receiving process don't accept
1903 * the passed socket.
1904 */
1905 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1906 rc = smk_bu_file(file, may, rc);
1907 if (rc < 0)
1908 return rc;
1909 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1910 rc = smk_bu_file(file, may, rc);
1911 return rc;
1912 }
Casey Schauflere114e472008-02-04 22:29:50 -08001913 /*
1914 * This code relies on bitmasks.
1915 */
1916 if (file->f_mode & FMODE_READ)
1917 may = MAY_READ;
1918 if (file->f_mode & FMODE_WRITE)
1919 may |= MAY_WRITE;
1920
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001921 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001922 rc = smk_bu_file(file, may, rc);
1923 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001924}
1925
Casey Schaufler531f1d42011-09-19 12:41:42 -07001926/**
Eric Paris83d49852012-04-04 13:45:40 -04001927 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001928 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001929 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001930 *
1931 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001932 * Allow the open only if the task has read access. There are
1933 * many read operations (e.g. fstat) that you can do with an
1934 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001935 *
1936 * Returns 0
1937 */
Al Viro94817692018-07-10 14:13:18 -04001938static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001939{
Al Viro94817692018-07-10 14:13:18 -04001940 struct task_smack *tsp = file->f_cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001941 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001942 struct smk_audit_info ad;
1943 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001944
Casey Schauflera6834c02014-04-21 11:10:26 -07001945 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1946 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301947 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001948 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001949
1950 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001951}
1952
Casey Schauflere114e472008-02-04 22:29:50 -08001953/*
1954 * Task hooks
1955 */
1956
1957/**
David Howellsee18d642009-09-02 09:14:21 +01001958 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1959 * @new: the new credentials
1960 * @gfp: the atomicity of any memory allocations
1961 *
1962 * Prepare a blank set of credentials for modification. This must allocate all
1963 * the memory the LSM module might require such that cred_transfer() can
1964 * complete without error.
1965 */
1966static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1967{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001968 struct task_smack *tsp;
1969
1970 tsp = new_task_smack(NULL, NULL, gfp);
1971 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001972 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001973
1974 cred->security = tsp;
1975
David Howellsee18d642009-09-02 09:14:21 +01001976 return 0;
1977}
1978
1979
1980/**
David Howellsf1752ee2008-11-14 10:39:17 +11001981 * smack_cred_free - "free" task-level security credentials
1982 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001983 *
Casey Schauflere114e472008-02-04 22:29:50 -08001984 */
David Howellsf1752ee2008-11-14 10:39:17 +11001985static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001986{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001987 struct task_smack *tsp = cred->security;
1988 struct smack_rule *rp;
1989 struct list_head *l;
1990 struct list_head *n;
1991
1992 if (tsp == NULL)
1993 return;
1994 cred->security = NULL;
1995
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001996 smk_destroy_label_list(&tsp->smk_relabel);
1997
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001998 list_for_each_safe(l, n, &tsp->smk_rules) {
1999 rp = list_entry(l, struct smack_rule, list);
2000 list_del(&rp->list);
2001 kfree(rp);
2002 }
2003 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08002004}
2005
2006/**
David Howellsd84f4f92008-11-14 10:39:23 +11002007 * smack_cred_prepare - prepare new set of credentials for modification
2008 * @new: the new credentials
2009 * @old: the original credentials
2010 * @gfp: the atomicity of any memory allocations
2011 *
2012 * Prepare a new set of credentials for modification.
2013 */
2014static int smack_cred_prepare(struct cred *new, const struct cred *old,
2015 gfp_t gfp)
2016{
Casey Schaufler676dac42010-12-02 06:43:39 -08002017 struct task_smack *old_tsp = old->security;
2018 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002019 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002020
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002021 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002022 if (new_tsp == NULL)
2023 return -ENOMEM;
2024
Himanshu Shuklab437aba2016-11-10 16:17:02 +05302025 new->security = new_tsp;
2026
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002027 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2028 if (rc != 0)
2029 return rc;
2030
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002031 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2032 gfp);
2033 if (rc != 0)
2034 return rc;
2035
David Howellsd84f4f92008-11-14 10:39:23 +11002036 return 0;
2037}
2038
Randy Dunlap251a2a92009-02-18 11:42:33 -08002039/**
David Howellsee18d642009-09-02 09:14:21 +01002040 * smack_cred_transfer - Transfer the old credentials to the new credentials
2041 * @new: the new credentials
2042 * @old: the original credentials
2043 *
2044 * Fill in a set of blank credentials from another set of credentials.
2045 */
2046static void smack_cred_transfer(struct cred *new, const struct cred *old)
2047{
Casey Schaufler676dac42010-12-02 06:43:39 -08002048 struct task_smack *old_tsp = old->security;
2049 struct task_smack *new_tsp = new->security;
2050
2051 new_tsp->smk_task = old_tsp->smk_task;
2052 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002053 mutex_init(&new_tsp->smk_rules_lock);
2054 INIT_LIST_HEAD(&new_tsp->smk_rules);
2055
2056
2057 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01002058}
2059
2060/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08002061 * smack_cred_getsecid - get the secid corresponding to a creds structure
2062 * @c: the object creds
2063 * @secid: where to put the result
2064 *
2065 * Sets the secid to contain a u32 version of the smack label.
2066 */
2067static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2068{
2069 struct smack_known *skp;
2070
2071 rcu_read_lock();
2072 skp = smk_of_task(c->security);
2073 *secid = skp->smk_secid;
2074 rcu_read_unlock();
2075}
2076
2077/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002078 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002079 * @new: points to the set of credentials to be modified.
2080 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002081 *
2082 * Set the security data for a kernel service.
2083 */
2084static int smack_kernel_act_as(struct cred *new, u32 secid)
2085{
Casey Schaufler676dac42010-12-02 06:43:39 -08002086 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002087
Casey Schaufler152f91d2016-11-14 09:38:15 -08002088 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002089 return 0;
2090}
2091
2092/**
2093 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002094 * @new: points to the set of credentials to be modified
2095 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002096 *
2097 * Set the file creation context in a set of credentials to the same
2098 * as the objective context of the specified inode
2099 */
2100static int smack_kernel_create_files_as(struct cred *new,
2101 struct inode *inode)
2102{
2103 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002104 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002105
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002106 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002107 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002108 return 0;
2109}
2110
2111/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002112 * smk_curacc_on_task - helper to log task related access
2113 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002114 * @access: the access requested
2115 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002116 *
2117 * Return 0 if access is permitted
2118 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002119static int smk_curacc_on_task(struct task_struct *p, int access,
2120 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002121{
2122 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002123 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002124 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002125
Casey Schaufler531f1d42011-09-19 12:41:42 -07002126 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002127 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002128 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002129 rc = smk_bu_task(p, access, rc);
2130 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002131}
2132
2133/**
Casey Schauflere114e472008-02-04 22:29:50 -08002134 * smack_task_setpgid - Smack check on setting pgid
2135 * @p: the task object
2136 * @pgid: unused
2137 *
2138 * Return 0 if write access is permitted
2139 */
2140static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2141{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002142 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002143}
2144
2145/**
2146 * smack_task_getpgid - Smack access check for getpgid
2147 * @p: the object task
2148 *
2149 * Returns 0 if current can read the object task, error code otherwise
2150 */
2151static int smack_task_getpgid(struct task_struct *p)
2152{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002153 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002154}
2155
2156/**
2157 * smack_task_getsid - Smack access check for getsid
2158 * @p: the object task
2159 *
2160 * Returns 0 if current can read the object task, error code otherwise
2161 */
2162static int smack_task_getsid(struct task_struct *p)
2163{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002164 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002165}
2166
2167/**
2168 * smack_task_getsecid - get the secid of the task
2169 * @p: the object task
2170 * @secid: where to put the result
2171 *
2172 * Sets the secid to contain a u32 version of the smack label.
2173 */
2174static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2175{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002176 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002177
2178 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002179}
2180
2181/**
2182 * smack_task_setnice - Smack check on setting nice
2183 * @p: the task object
2184 * @nice: unused
2185 *
2186 * Return 0 if write access is permitted
2187 */
2188static int smack_task_setnice(struct task_struct *p, int nice)
2189{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002190 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002191}
2192
2193/**
2194 * smack_task_setioprio - Smack check on setting ioprio
2195 * @p: the task object
2196 * @ioprio: unused
2197 *
2198 * Return 0 if write access is permitted
2199 */
2200static int smack_task_setioprio(struct task_struct *p, int ioprio)
2201{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002202 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002203}
2204
2205/**
2206 * smack_task_getioprio - Smack check on reading ioprio
2207 * @p: the task object
2208 *
2209 * Return 0 if read access is permitted
2210 */
2211static int smack_task_getioprio(struct task_struct *p)
2212{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002213 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002214}
2215
2216/**
2217 * smack_task_setscheduler - Smack check on setting scheduler
2218 * @p: the task object
2219 * @policy: unused
2220 * @lp: unused
2221 *
2222 * Return 0 if read access is permitted
2223 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002224static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002225{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002226 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002227}
2228
2229/**
2230 * smack_task_getscheduler - Smack check on reading scheduler
2231 * @p: the task object
2232 *
2233 * Return 0 if read access is permitted
2234 */
2235static int smack_task_getscheduler(struct task_struct *p)
2236{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002237 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002238}
2239
2240/**
2241 * smack_task_movememory - Smack check on moving memory
2242 * @p: the task object
2243 *
2244 * Return 0 if write access is permitted
2245 */
2246static int smack_task_movememory(struct task_struct *p)
2247{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002248 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002249}
2250
2251/**
2252 * smack_task_kill - Smack check on signal delivery
2253 * @p: the task object
2254 * @info: unused
2255 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002256 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002257 *
2258 * Return 0 if write access is permitted
2259 *
Casey Schauflere114e472008-02-04 22:29:50 -08002260 */
2261static int smack_task_kill(struct task_struct *p, struct siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002262 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002263{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002264 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002265 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002266 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002267 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002268
Rafal Krypa18d872f2016-04-04 11:14:53 +02002269 if (!sig)
2270 return 0; /* null signal; existence test */
2271
Etienne Bassetecfcc532009-04-08 20:40:06 +02002272 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2273 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002274 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002275 * Sending a signal requires that the sender
2276 * can write the receiver.
2277 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002278 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002279 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2280 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002281 return rc;
2282 }
Casey Schauflere114e472008-02-04 22:29:50 -08002283 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002284 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002285 * specific behavior. This is not clean. For one thing
2286 * we can't take privilege into account.
2287 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002288 skp = smk_of_task(cred->security);
Casey Schauflerc60b9062016-08-30 10:31:39 -07002289 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2290 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002291 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002292}
2293
2294/**
Casey Schauflere114e472008-02-04 22:29:50 -08002295 * smack_task_to_inode - copy task smack into the inode blob
2296 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002297 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002298 *
2299 * Sets the smack pointer in the inode security blob
2300 */
2301static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2302{
2303 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002304 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002305
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002306 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002307 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002308}
2309
2310/*
2311 * Socket hooks.
2312 */
2313
2314/**
2315 * smack_sk_alloc_security - Allocate a socket blob
2316 * @sk: the socket
2317 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002318 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002319 *
2320 * Assign Smack pointers to current
2321 *
2322 * Returns 0 on success, -ENOMEM is there's no memory
2323 */
2324static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2325{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002326 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002327 struct socket_smack *ssp;
2328
2329 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2330 if (ssp == NULL)
2331 return -ENOMEM;
2332
jooseong lee08382c92016-11-03 11:54:39 +01002333 /*
2334 * Sockets created by kernel threads receive web label.
2335 */
2336 if (unlikely(current->flags & PF_KTHREAD)) {
2337 ssp->smk_in = &smack_known_web;
2338 ssp->smk_out = &smack_known_web;
2339 } else {
2340 ssp->smk_in = skp;
2341 ssp->smk_out = skp;
2342 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002343 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002344
2345 sk->sk_security = ssp;
2346
2347 return 0;
2348}
2349
2350/**
2351 * smack_sk_free_security - Free a socket blob
2352 * @sk: the socket
2353 *
2354 * Clears the blob pointer
2355 */
2356static void smack_sk_free_security(struct sock *sk)
2357{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302358#ifdef SMACK_IPV6_PORT_LABELING
2359 struct smk_port_label *spp;
2360
2361 if (sk->sk_family == PF_INET6) {
2362 rcu_read_lock();
2363 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2364 if (spp->smk_sock != sk)
2365 continue;
2366 spp->smk_can_reuse = 1;
2367 break;
2368 }
2369 rcu_read_unlock();
2370 }
2371#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002372 kfree(sk->sk_security);
2373}
2374
2375/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002376* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002377* @sip: the object end
2378*
2379* looks for host based access restrictions
2380*
2381* This version will only be appropriate for really small sets of single label
2382* hosts. The caller is responsible for ensuring that the RCU read lock is
2383* taken before calling this function.
2384*
2385* Returns the label of the far end or NULL if it's not special.
2386*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002387static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002388{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002389 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002390 struct in_addr *siap = &sip->sin_addr;
2391
2392 if (siap->s_addr == 0)
2393 return NULL;
2394
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002395 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2396 /*
2397 * we break after finding the first match because
2398 * the list is sorted from longest to shortest mask
2399 * so we have found the most specific match
2400 */
2401 if (snp->smk_host.s_addr ==
2402 (siap->s_addr & snp->smk_mask.s_addr))
2403 return snp->smk_label;
2404
2405 return NULL;
2406}
2407
2408#if IS_ENABLED(CONFIG_IPV6)
2409/*
2410 * smk_ipv6_localhost - Check for local ipv6 host address
2411 * @sip: the address
2412 *
2413 * Returns boolean true if this is the localhost address
2414 */
2415static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2416{
2417 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2418 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2419
2420 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2421 ntohs(be16p[7]) == 1)
2422 return true;
2423 return false;
2424}
2425
2426/**
2427* smack_ipv6host_label - check host based restrictions
2428* @sip: the object end
2429*
2430* looks for host based access restrictions
2431*
2432* This version will only be appropriate for really small sets of single label
2433* hosts. The caller is responsible for ensuring that the RCU read lock is
2434* taken before calling this function.
2435*
2436* Returns the label of the far end or NULL if it's not special.
2437*/
2438static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2439{
2440 struct smk_net6addr *snp;
2441 struct in6_addr *sap = &sip->sin6_addr;
2442 int i;
2443 int found = 0;
2444
2445 /*
2446 * It's local. Don't look for a host label.
2447 */
2448 if (smk_ipv6_localhost(sip))
2449 return NULL;
2450
2451 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002452 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002453 * If the label is NULL the entry has
2454 * been renounced. Ignore it.
2455 */
2456 if (snp->smk_label == NULL)
2457 continue;
2458 /*
Paul Moore07feee82009-03-27 17:10:54 -04002459 * we break after finding the first match because
2460 * the list is sorted from longest to shortest mask
2461 * so we have found the most specific match
2462 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002463 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002464 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2465 snp->smk_host.s6_addr16[i]) {
2466 found = 0;
2467 break;
2468 }
Etienne Basset43031542009-03-27 17:11:01 -04002469 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002470 if (found)
2471 return snp->smk_label;
2472 }
Paul Moore07feee82009-03-27 17:10:54 -04002473
2474 return NULL;
2475}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002476#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002477
2478/**
Casey Schauflere114e472008-02-04 22:29:50 -08002479 * smack_netlabel - Set the secattr on a socket
2480 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002481 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002482 *
2483 * Convert the outbound smack value (smk_out) to a
2484 * secattr and attach it to the socket.
2485 *
2486 * Returns 0 on success or an error code
2487 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002488static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002489{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002490 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002491 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002492 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002493
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002494 /*
2495 * Usually the netlabel code will handle changing the
2496 * packet labeling based on the label.
2497 * The case of a single label host is different, because
2498 * a single label host should never get a labeled packet
2499 * even though the label is usually associated with a packet
2500 * label.
2501 */
2502 local_bh_disable();
2503 bh_lock_sock_nested(sk);
2504
2505 if (ssp->smk_out == smack_net_ambient ||
2506 labeled == SMACK_UNLABELED_SOCKET)
2507 netlbl_sock_delattr(sk);
2508 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002509 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002510 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002511 }
2512
2513 bh_unlock_sock(sk);
2514 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002515
Casey Schauflere114e472008-02-04 22:29:50 -08002516 return rc;
2517}
2518
2519/**
Paul Moore07feee82009-03-27 17:10:54 -04002520 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2521 * @sk: the socket
2522 * @sap: the destination address
2523 *
2524 * Set the correct secattr for the given socket based on the destination
2525 * address and perform any outbound access checks needed.
2526 *
2527 * Returns 0 on success or an error code.
2528 *
2529 */
2530static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2531{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002532 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002533 int rc;
2534 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002535 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002536 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002537 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002538
2539 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002540 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002541 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002542#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002543 struct lsm_network_audit net;
2544
Eric Paris48c62af2012-04-02 13:15:44 -04002545 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2546 ad.a.u.net->family = sap->sin_family;
2547 ad.a.u.net->dport = sap->sin_port;
2548 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002549#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002550 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002551 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002552 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2553 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002554 } else {
2555 sk_lbl = SMACK_CIPSO_SOCKET;
2556 rc = 0;
2557 }
2558 rcu_read_unlock();
2559 if (rc != 0)
2560 return rc;
2561
2562 return smack_netlabel(sk, sk_lbl);
2563}
2564
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002565#if IS_ENABLED(CONFIG_IPV6)
2566/**
2567 * smk_ipv6_check - check Smack access
2568 * @subject: subject Smack label
2569 * @object: object Smack label
2570 * @address: address
2571 * @act: the action being taken
2572 *
2573 * Check an IPv6 access
2574 */
2575static int smk_ipv6_check(struct smack_known *subject,
2576 struct smack_known *object,
2577 struct sockaddr_in6 *address, int act)
2578{
2579#ifdef CONFIG_AUDIT
2580 struct lsm_network_audit net;
2581#endif
2582 struct smk_audit_info ad;
2583 int rc;
2584
2585#ifdef CONFIG_AUDIT
2586 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2587 ad.a.u.net->family = PF_INET6;
2588 ad.a.u.net->dport = ntohs(address->sin6_port);
2589 if (act == SMK_RECEIVING)
2590 ad.a.u.net->v6info.saddr = address->sin6_addr;
2591 else
2592 ad.a.u.net->v6info.daddr = address->sin6_addr;
2593#endif
2594 rc = smk_access(subject, object, MAY_WRITE, &ad);
2595 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2596 return rc;
2597}
2598#endif /* CONFIG_IPV6 */
2599
2600#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002601/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002602 * smk_ipv6_port_label - Smack port access table management
2603 * @sock: socket
2604 * @address: address
2605 *
2606 * Create or update the port list entry
2607 */
2608static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2609{
2610 struct sock *sk = sock->sk;
2611 struct sockaddr_in6 *addr6;
2612 struct socket_smack *ssp = sock->sk->sk_security;
2613 struct smk_port_label *spp;
2614 unsigned short port = 0;
2615
2616 if (address == NULL) {
2617 /*
2618 * This operation is changing the Smack information
2619 * on the bound socket. Take the changes to the port
2620 * as well.
2621 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302622 rcu_read_lock();
2623 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002624 if (sk != spp->smk_sock)
2625 continue;
2626 spp->smk_in = ssp->smk_in;
2627 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302628 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002629 return;
2630 }
2631 /*
2632 * A NULL address is only used for updating existing
2633 * bound entries. If there isn't one, it's OK.
2634 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302635 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002636 return;
2637 }
2638
2639 addr6 = (struct sockaddr_in6 *)address;
2640 port = ntohs(addr6->sin6_port);
2641 /*
2642 * This is a special case that is safely ignored.
2643 */
2644 if (port == 0)
2645 return;
2646
2647 /*
2648 * Look for an existing port list entry.
2649 * This is an indication that a port is getting reused.
2650 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302651 rcu_read_lock();
2652 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302653 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002654 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302655 if (spp->smk_can_reuse != 1) {
2656 rcu_read_unlock();
2657 return;
2658 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002659 spp->smk_port = port;
2660 spp->smk_sock = sk;
2661 spp->smk_in = ssp->smk_in;
2662 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302663 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302664 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002665 return;
2666 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302667 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002668 /*
2669 * A new port entry is required.
2670 */
2671 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2672 if (spp == NULL)
2673 return;
2674
2675 spp->smk_port = port;
2676 spp->smk_sock = sk;
2677 spp->smk_in = ssp->smk_in;
2678 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302679 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302680 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002681
Vishal Goel3c7ce342016-11-23 10:31:08 +05302682 mutex_lock(&smack_ipv6_lock);
2683 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2684 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002685 return;
2686}
2687
2688/**
2689 * smk_ipv6_port_check - check Smack port access
2690 * @sock: socket
2691 * @address: address
2692 *
2693 * Create or update the port list entry
2694 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002695static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002696 int act)
2697{
Casey Schauflerc6739442013-05-22 18:42:56 -07002698 struct smk_port_label *spp;
2699 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002700 struct smack_known *skp = NULL;
2701 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002702 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002703
2704 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002705 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002706 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002707 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002708 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002709 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002710 }
2711
2712 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002713 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002714 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002715 if (skp != NULL && object != NULL)
2716 return smk_ipv6_check(skp, object, address, act);
2717 if (skp == NULL)
2718 skp = smack_net_ambient;
2719 if (object == NULL)
2720 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002721
2722 /*
2723 * It's remote, so port lookup does no good.
2724 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002725 if (!smk_ipv6_localhost(address))
2726 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002727
2728 /*
2729 * It's local so the send check has to have passed.
2730 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002731 if (act == SMK_RECEIVING)
2732 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002733
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002734 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302735 rcu_read_lock();
2736 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302737 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002738 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002739 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002740 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002741 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002742 break;
2743 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302744 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002745
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002746 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002747}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002748#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002749
2750/**
Casey Schauflere114e472008-02-04 22:29:50 -08002751 * smack_inode_setsecurity - set smack xattrs
2752 * @inode: the object
2753 * @name: attribute name
2754 * @value: attribute value
2755 * @size: size of the attribute
2756 * @flags: unused
2757 *
2758 * Sets the named attribute in the appropriate blob
2759 *
2760 * Returns 0 on success, or an error code
2761 */
2762static int smack_inode_setsecurity(struct inode *inode, const char *name,
2763 const void *value, size_t size, int flags)
2764{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002765 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002766 struct inode_smack *nsp = inode->i_security;
2767 struct socket_smack *ssp;
2768 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002769 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002770
Casey Schauflerf7112e62012-05-06 15:22:02 -07002771 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302772 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002773
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002774 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002775 if (IS_ERR(skp))
2776 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002777
2778 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002779 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002780 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002781 return 0;
2782 }
2783 /*
2784 * The rest of the Smack xattrs are only on sockets.
2785 */
2786 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2787 return -EOPNOTSUPP;
2788
2789 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002790 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002791 return -EOPNOTSUPP;
2792
2793 ssp = sock->sk->sk_security;
2794
2795 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002796 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002797 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002798 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002799 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002800 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2801 if (rc != 0)
2802 printk(KERN_WARNING
2803 "Smack: \"%s\" netlbl error %d.\n",
2804 __func__, -rc);
2805 }
Casey Schauflere114e472008-02-04 22:29:50 -08002806 } else
2807 return -EOPNOTSUPP;
2808
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002809#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002810 if (sock->sk->sk_family == PF_INET6)
2811 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002812#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002813
Casey Schauflere114e472008-02-04 22:29:50 -08002814 return 0;
2815}
2816
2817/**
2818 * smack_socket_post_create - finish socket setup
2819 * @sock: the socket
2820 * @family: protocol family
2821 * @type: unused
2822 * @protocol: unused
2823 * @kern: unused
2824 *
2825 * Sets the netlabel information on the socket
2826 *
2827 * Returns 0 on success, and error code otherwise
2828 */
2829static int smack_socket_post_create(struct socket *sock, int family,
2830 int type, int protocol, int kern)
2831{
Marcin Lis74123012015-01-22 15:40:33 +01002832 struct socket_smack *ssp;
2833
2834 if (sock->sk == NULL)
2835 return 0;
2836
2837 /*
2838 * Sockets created by kernel threads receive web label.
2839 */
2840 if (unlikely(current->flags & PF_KTHREAD)) {
2841 ssp = sock->sk->sk_security;
2842 ssp->smk_in = &smack_known_web;
2843 ssp->smk_out = &smack_known_web;
2844 }
2845
2846 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002847 return 0;
2848 /*
2849 * Set the outbound netlbl.
2850 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002851 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2852}
2853
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002854/**
2855 * smack_socket_socketpair - create socket pair
2856 * @socka: one socket
2857 * @sockb: another socket
2858 *
2859 * Cross reference the peer labels for SO_PEERSEC
2860 *
2861 * Returns 0 on success, and error code otherwise
2862 */
2863static int smack_socket_socketpair(struct socket *socka,
2864 struct socket *sockb)
2865{
2866 struct socket_smack *asp = socka->sk->sk_security;
2867 struct socket_smack *bsp = sockb->sk->sk_security;
2868
2869 asp->smk_packet = bsp->smk_out;
2870 bsp->smk_packet = asp->smk_out;
2871
2872 return 0;
2873}
2874
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002875#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002876/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002877 * smack_socket_bind - record port binding information.
2878 * @sock: the socket
2879 * @address: the port address
2880 * @addrlen: size of the address
2881 *
2882 * Records the label bound to a port.
2883 *
2884 * Returns 0
2885 */
2886static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2887 int addrlen)
2888{
2889 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2890 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002891 return 0;
2892}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002893#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002894
2895/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002896 * smack_socket_connect - connect access check
2897 * @sock: the socket
2898 * @sap: the other end
2899 * @addrlen: size of sap
2900 *
2901 * Verifies that a connection may be possible
2902 *
2903 * Returns 0 on success, and error code otherwise
2904 */
2905static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2906 int addrlen)
2907{
Casey Schauflerc6739442013-05-22 18:42:56 -07002908 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002909#if IS_ENABLED(CONFIG_IPV6)
2910 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2911#endif
2912#ifdef SMACK_IPV6_SECMARK_LABELING
2913 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002914 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002915#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002916
Casey Schauflerc6739442013-05-22 18:42:56 -07002917 if (sock->sk == NULL)
2918 return 0;
2919
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002920#ifdef SMACK_IPV6_SECMARK_LABELING
2921 ssp = sock->sk->sk_security;
2922#endif
2923
Casey Schauflerc6739442013-05-22 18:42:56 -07002924 switch (sock->sk->sk_family) {
2925 case PF_INET:
2926 if (addrlen < sizeof(struct sockaddr_in))
2927 return -EINVAL;
2928 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2929 break;
2930 case PF_INET6:
2931 if (addrlen < sizeof(struct sockaddr_in6))
2932 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002933#ifdef SMACK_IPV6_SECMARK_LABELING
2934 rsp = smack_ipv6host_label(sip);
2935 if (rsp != NULL)
2936 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002937 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002938#endif
2939#ifdef SMACK_IPV6_PORT_LABELING
2940 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2941#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002942 break;
2943 }
2944 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002945}
2946
2947/**
2948 * smack_flags_to_may - convert S_ to MAY_ values
2949 * @flags: the S_ value
2950 *
2951 * Returns the equivalent MAY_ value
2952 */
2953static int smack_flags_to_may(int flags)
2954{
2955 int may = 0;
2956
2957 if (flags & S_IRUGO)
2958 may |= MAY_READ;
2959 if (flags & S_IWUGO)
2960 may |= MAY_WRITE;
2961 if (flags & S_IXUGO)
2962 may |= MAY_EXEC;
2963
2964 return may;
2965}
2966
2967/**
2968 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2969 * @msg: the object
2970 *
2971 * Returns 0
2972 */
2973static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2974{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002975 struct smack_known *skp = smk_of_current();
2976
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002977 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002978 return 0;
2979}
2980
2981/**
2982 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2983 * @msg: the object
2984 *
2985 * Clears the blob pointer
2986 */
2987static void smack_msg_msg_free_security(struct msg_msg *msg)
2988{
2989 msg->security = NULL;
2990}
2991
2992/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002993 * smack_of_ipc - the smack pointer for the ipc
2994 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002995 *
2996 * Returns a pointer to the smack value
2997 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002998static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002999{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003000 return (struct smack_known *)isp->security;
Casey Schauflere114e472008-02-04 22:29:50 -08003001}
3002
3003/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003004 * smack_ipc_alloc_security - Set the security blob for ipc
3005 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003006 *
3007 * Returns 0
3008 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003009static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08003010{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003011 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003012
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003013 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003014 return 0;
3015}
3016
3017/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003018 * smack_ipc_free_security - Clear the security blob for ipc
3019 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003020 *
3021 * Clears the blob pointer
3022 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003023static void smack_ipc_free_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08003024{
Casey Schauflere114e472008-02-04 22:29:50 -08003025 isp->security = NULL;
3026}
3027
3028/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003029 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003030 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003031 * @access : access requested
3032 *
3033 * Returns 0 if current has the requested access, error code otherwise
3034 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003035static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003036{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003037 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003038 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003039 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003040
3041#ifdef CONFIG_AUDIT
3042 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003043 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003044#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003045 rc = smk_curacc(ssp, access, &ad);
3046 rc = smk_bu_current("shm", ssp, access, rc);
3047 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003048}
3049
3050/**
Casey Schauflere114e472008-02-04 22:29:50 -08003051 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003052 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003053 * @shmflg: access requested
3054 *
3055 * Returns 0 if current has the requested access, error code otherwise
3056 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003057static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003058{
Casey Schauflere114e472008-02-04 22:29:50 -08003059 int may;
3060
3061 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003062 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003063}
3064
3065/**
3066 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003067 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003068 * @cmd: what it wants to do
3069 *
3070 * Returns 0 if current has the requested access, error code otherwise
3071 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003072static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003073{
Casey Schauflere114e472008-02-04 22:29:50 -08003074 int may;
3075
3076 switch (cmd) {
3077 case IPC_STAT:
3078 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07003079 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003080 may = MAY_READ;
3081 break;
3082 case IPC_SET:
3083 case SHM_LOCK:
3084 case SHM_UNLOCK:
3085 case IPC_RMID:
3086 may = MAY_READWRITE;
3087 break;
3088 case IPC_INFO:
3089 case SHM_INFO:
3090 /*
3091 * System level information.
3092 */
3093 return 0;
3094 default:
3095 return -EINVAL;
3096 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003097 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003098}
3099
3100/**
3101 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003102 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003103 * @shmaddr: unused
3104 * @shmflg: access requested
3105 *
3106 * Returns 0 if current has the requested access, error code otherwise
3107 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003108static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003109 int shmflg)
3110{
Casey Schauflere114e472008-02-04 22:29:50 -08003111 int may;
3112
3113 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003114 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003115}
3116
3117/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003118 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003120 * @access : access requested
3121 *
3122 * Returns 0 if current has the requested access, error code otherwise
3123 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003124static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003125{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003126 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003127 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003128 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003129
3130#ifdef CONFIG_AUDIT
3131 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003132 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003133#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003134 rc = smk_curacc(ssp, access, &ad);
3135 rc = smk_bu_current("sem", ssp, access, rc);
3136 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003137}
3138
3139/**
Casey Schauflere114e472008-02-04 22:29:50 -08003140 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003141 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003142 * @semflg: access requested
3143 *
3144 * Returns 0 if current has the requested access, error code otherwise
3145 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003146static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003147{
Casey Schauflere114e472008-02-04 22:29:50 -08003148 int may;
3149
3150 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003151 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003152}
3153
3154/**
3155 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003156 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003157 * @cmd: what it wants to do
3158 *
3159 * Returns 0 if current has the requested access, error code otherwise
3160 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003161static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003162{
Casey Schauflere114e472008-02-04 22:29:50 -08003163 int may;
3164
3165 switch (cmd) {
3166 case GETPID:
3167 case GETNCNT:
3168 case GETZCNT:
3169 case GETVAL:
3170 case GETALL:
3171 case IPC_STAT:
3172 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003173 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003174 may = MAY_READ;
3175 break;
3176 case SETVAL:
3177 case SETALL:
3178 case IPC_RMID:
3179 case IPC_SET:
3180 may = MAY_READWRITE;
3181 break;
3182 case IPC_INFO:
3183 case SEM_INFO:
3184 /*
3185 * System level information
3186 */
3187 return 0;
3188 default:
3189 return -EINVAL;
3190 }
3191
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003192 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003193}
3194
3195/**
3196 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003197 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003198 * @sops: unused
3199 * @nsops: unused
3200 * @alter: unused
3201 *
3202 * Treated as read and write in all cases.
3203 *
3204 * Returns 0 if access is allowed, error code otherwise
3205 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003206static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003207 unsigned nsops, int alter)
3208{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003209 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003210}
3211
3212/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003213 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003214 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003215 * @access : access requested
3216 *
3217 * return 0 if current has access, error otherwise
3218 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003219static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003220{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003221 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003222 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003223 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003224
3225#ifdef CONFIG_AUDIT
3226 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003227 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003228#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003229 rc = smk_curacc(msp, access, &ad);
3230 rc = smk_bu_current("msq", msp, access, rc);
3231 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003232}
3233
3234/**
Casey Schauflere114e472008-02-04 22:29:50 -08003235 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003236 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003237 * @msqflg: access requested
3238 *
3239 * Returns 0 if current has the requested access, error code otherwise
3240 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003241static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003242{
Casey Schauflere114e472008-02-04 22:29:50 -08003243 int may;
3244
3245 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003246 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003247}
3248
3249/**
3250 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003251 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003252 * @cmd: what it wants to do
3253 *
3254 * Returns 0 if current has the requested access, error code otherwise
3255 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003256static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003257{
Casey Schauflere114e472008-02-04 22:29:50 -08003258 int may;
3259
3260 switch (cmd) {
3261 case IPC_STAT:
3262 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003263 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003264 may = MAY_READ;
3265 break;
3266 case IPC_SET:
3267 case IPC_RMID:
3268 may = MAY_READWRITE;
3269 break;
3270 case IPC_INFO:
3271 case MSG_INFO:
3272 /*
3273 * System level information
3274 */
3275 return 0;
3276 default:
3277 return -EINVAL;
3278 }
3279
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003280 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003281}
3282
3283/**
3284 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003285 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003286 * @msg: unused
3287 * @msqflg: access requested
3288 *
3289 * Returns 0 if current has the requested access, error code otherwise
3290 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003291static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003292 int msqflg)
3293{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003294 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003295
Etienne Bassetecfcc532009-04-08 20:40:06 +02003296 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003297 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003298}
3299
3300/**
3301 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003302 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003303 * @msg: unused
3304 * @target: unused
3305 * @type: unused
3306 * @mode: unused
3307 *
3308 * Returns 0 if current has read and write access, error code otherwise
3309 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003310static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003311 struct task_struct *target, long type, int mode)
3312{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003313 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003314}
3315
3316/**
3317 * smack_ipc_permission - Smack access for ipc_permission()
3318 * @ipp: the object permissions
3319 * @flag: access requested
3320 *
3321 * Returns 0 if current has read and write access, error code otherwise
3322 */
3323static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3324{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003325 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003326 int may = smack_flags_to_may(flag);
3327 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003328 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003329
Etienne Bassetecfcc532009-04-08 20:40:06 +02003330#ifdef CONFIG_AUDIT
3331 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3332 ad.a.u.ipc_id = ipp->id;
3333#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003334 rc = smk_curacc(iskp, may, &ad);
3335 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003336 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003337}
3338
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003339/**
3340 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003341 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003342 * @secid: where result will be saved
3343 */
3344static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3345{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003346 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003347
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003348 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003349}
3350
Casey Schauflere114e472008-02-04 22:29:50 -08003351/**
3352 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003353 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003354 * @inode: the object
3355 *
3356 * Set the inode's security blob if it hasn't been done already.
3357 */
3358static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3359{
3360 struct super_block *sbp;
3361 struct superblock_smack *sbsp;
3362 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003363 struct smack_known *skp;
3364 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003365 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003366 char trattr[TRANS_TRUE_SIZE];
3367 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003368 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003369 struct dentry *dp;
3370
3371 if (inode == NULL)
3372 return;
3373
3374 isp = inode->i_security;
3375
3376 mutex_lock(&isp->smk_lock);
3377 /*
3378 * If the inode is already instantiated
3379 * take the quick way out
3380 */
3381 if (isp->smk_flags & SMK_INODE_INSTANT)
3382 goto unlockandout;
3383
3384 sbp = inode->i_sb;
3385 sbsp = sbp->s_security;
3386 /*
3387 * We're going to use the superblock default label
3388 * if there's no label on the file.
3389 */
3390 final = sbsp->smk_default;
3391
3392 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003393 * If this is the root inode the superblock
3394 * may be in the process of initialization.
3395 * If that is the case use the root value out
3396 * of the superblock.
3397 */
3398 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003399 switch (sbp->s_magic) {
3400 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003401 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003402 /*
3403 * The cgroup filesystem is never mounted,
3404 * so there's no opportunity to set the mount
3405 * options.
3406 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003407 sbsp->smk_root = &smack_known_star;
3408 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003409 isp->smk_inode = sbsp->smk_root;
3410 break;
3411 case TMPFS_MAGIC:
3412 /*
3413 * What about shmem/tmpfs anonymous files with dentry
3414 * obtained from d_alloc_pseudo()?
3415 */
3416 isp->smk_inode = smk_of_current();
3417 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003418 case PIPEFS_MAGIC:
3419 isp->smk_inode = smk_of_current();
3420 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003421 case SOCKFS_MAGIC:
3422 /*
3423 * Socket access is controlled by the socket
3424 * structures associated with the task involved.
3425 */
3426 isp->smk_inode = &smack_known_star;
3427 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003428 default:
3429 isp->smk_inode = sbsp->smk_root;
3430 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003431 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003432 isp->smk_flags |= SMK_INODE_INSTANT;
3433 goto unlockandout;
3434 }
3435
3436 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003437 * This is pretty hackish.
3438 * Casey says that we shouldn't have to do
3439 * file system specific code, but it does help
3440 * with keeping it simple.
3441 */
3442 switch (sbp->s_magic) {
3443 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003444 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003445 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003446 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003447 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003448 * that the smack file system doesn't do
3449 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003450 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003451 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003452 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003453 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003454 break;
3455 case DEVPTS_SUPER_MAGIC:
3456 /*
3457 * devpts seems content with the label of the task.
3458 * Programs that change smack have to treat the
3459 * pty with respect.
3460 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003461 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003462 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003463 case PROC_SUPER_MAGIC:
3464 /*
3465 * Casey says procfs appears not to care.
3466 * The superblock default suffices.
3467 */
3468 break;
3469 case TMPFS_MAGIC:
3470 /*
3471 * Device labels should come from the filesystem,
3472 * but watch out, because they're volitile,
3473 * getting recreated on every reboot.
3474 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003475 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003476 /*
3477 * No break.
3478 *
3479 * If a smack value has been set we want to use it,
3480 * but since tmpfs isn't giving us the opportunity
3481 * to set mount options simulate setting the
3482 * superblock default.
3483 */
3484 default:
3485 /*
3486 * This isn't an understood special case.
3487 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003488 */
3489
3490 /*
3491 * UNIX domain sockets use lower level socket data.
3492 */
3493 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003494 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003495 break;
3496 }
3497 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003498 * No xattr support means, alas, no SMACK label.
3499 * Use the aforeapplied default.
3500 * It would be curious if the label of the task
3501 * does not match that assigned.
3502 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003503 if (!(inode->i_opflags & IOP_XATTR))
3504 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003505 /*
3506 * Get the dentry for xattr.
3507 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003508 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003509 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003510 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003511 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003512
3513 /*
3514 * Transmuting directory
3515 */
3516 if (S_ISDIR(inode->i_mode)) {
3517 /*
3518 * If this is a new directory and the label was
3519 * transmuted when the inode was initialized
3520 * set the transmute attribute on the directory
3521 * and mark the inode.
3522 *
3523 * If there is a transmute attribute on the
3524 * directory mark the inode.
3525 */
3526 if (isp->smk_flags & SMK_INODE_CHANGED) {
3527 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003528 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003529 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003530 TRANS_TRUE, TRANS_TRUE_SIZE,
3531 0);
3532 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003533 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003534 XATTR_NAME_SMACKTRANSMUTE, trattr,
3535 TRANS_TRUE_SIZE);
3536 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3537 TRANS_TRUE_SIZE) != 0)
3538 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003539 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003540 if (rc >= 0)
3541 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003542 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003543 /*
3544 * Don't let the exec or mmap label be "*" or "@".
3545 */
3546 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3547 if (IS_ERR(skp) || skp == &smack_known_star ||
3548 skp == &smack_known_web)
3549 skp = NULL;
3550 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003551
Casey Schaufler19760ad2013-12-16 16:27:26 -08003552 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003553 if (IS_ERR(skp) || skp == &smack_known_star ||
3554 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003555 skp = NULL;
3556 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003557
Casey Schauflere114e472008-02-04 22:29:50 -08003558 dput(dp);
3559 break;
3560 }
3561
3562 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003563 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003564 else
3565 isp->smk_inode = final;
3566
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003567 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003568
3569unlockandout:
3570 mutex_unlock(&isp->smk_lock);
3571 return;
3572}
3573
3574/**
3575 * smack_getprocattr - Smack process attribute access
3576 * @p: the object task
3577 * @name: the name of the attribute in /proc/.../attr
3578 * @value: where to put the result
3579 *
3580 * Places a copy of the task Smack into value
3581 *
3582 * Returns the length of the smack label or an error code
3583 */
3584static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3585{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003586 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003587 char *cp;
3588 int slen;
3589
3590 if (strcmp(name, "current") != 0)
3591 return -EINVAL;
3592
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003593 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003594 if (cp == NULL)
3595 return -ENOMEM;
3596
3597 slen = strlen(cp);
3598 *value = cp;
3599 return slen;
3600}
3601
3602/**
3603 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003604 * @name: the name of the attribute in /proc/.../attr
3605 * @value: the value to set
3606 * @size: the size of the value
3607 *
3608 * Sets the Smack value of the task. Only setting self
3609 * is permitted and only with privilege
3610 *
3611 * Returns the length of the smack label or an error code
3612 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003613static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003614{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003615 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003616 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003617 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003618 struct smack_known_list_elem *sklep;
3619 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003620
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003621 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003622 return -EPERM;
3623
Casey Schauflerf7112e62012-05-06 15:22:02 -07003624 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003625 return -EINVAL;
3626
3627 if (strcmp(name, "current") != 0)
3628 return -EINVAL;
3629
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003630 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003631 if (IS_ERR(skp))
3632 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003633
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003634 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303635 * No process is ever allowed the web ("@") label
3636 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003637 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303638 if (skp == &smack_known_web || skp == &smack_known_star)
3639 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003640
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003641 if (!smack_privileged(CAP_MAC_ADMIN)) {
3642 rc = -EPERM;
3643 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3644 if (sklep->smk_label == skp) {
3645 rc = 0;
3646 break;
3647 }
3648 if (rc)
3649 return rc;
3650 }
3651
David Howellsd84f4f92008-11-14 10:39:23 +11003652 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003653 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003654 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003655
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003656 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003657 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003658 /*
3659 * process can change its label only once
3660 */
3661 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003662
David Howellsd84f4f92008-11-14 10:39:23 +11003663 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003664 return size;
3665}
3666
3667/**
3668 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003669 * @sock: one sock
3670 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003671 * @newsk: unused
3672 *
3673 * Return 0 if a subject with the smack of sock could access
3674 * an object with the smack of other, otherwise an error code
3675 */
David S. Miller3610cda2011-01-05 15:38:53 -08003676static int smack_unix_stream_connect(struct sock *sock,
3677 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003678{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003679 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003680 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003681 struct socket_smack *ssp = sock->sk_security;
3682 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003683 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003684 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003685 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003686#ifdef CONFIG_AUDIT
3687 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003688#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003689
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003690 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3691 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003692 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003693#ifdef CONFIG_AUDIT
3694 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3695 smk_ad_setfield_u_net_sk(&ad, other);
3696#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003697 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3698 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003699 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003700 okp = osp->smk_out;
3701 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003702 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003703 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003704 MAY_WRITE, rc);
3705 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003706 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003707
Casey Schaufler975d5e52011-09-26 14:43:39 -07003708 /*
3709 * Cross reference the peer labels for SO_PEERSEC.
3710 */
3711 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003712 nsp->smk_packet = ssp->smk_out;
3713 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003714 }
3715
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003716 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003717}
3718
3719/**
3720 * smack_unix_may_send - Smack access on UDS
3721 * @sock: one socket
3722 * @other: the other socket
3723 *
3724 * Return 0 if a subject with the smack of sock could access
3725 * an object with the smack of other, otherwise an error code
3726 */
3727static int smack_unix_may_send(struct socket *sock, struct socket *other)
3728{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003729 struct socket_smack *ssp = sock->sk->sk_security;
3730 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003731 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003732 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003733
Kees Cook923e9a12012-04-10 13:26:44 -07003734#ifdef CONFIG_AUDIT
3735 struct lsm_network_audit net;
3736
Eric Paris48c62af2012-04-02 13:15:44 -04003737 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003738 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003739#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003740
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003741 if (smack_privileged(CAP_MAC_OVERRIDE))
3742 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003743
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003744 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3745 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003746 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003747}
3748
3749/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003750 * smack_socket_sendmsg - Smack check based on destination host
3751 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003752 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003753 * @size: the size of the message
3754 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003755 * Return 0 if the current subject can write to the destination host.
3756 * For IPv4 this is only a question if the destination is a single label host.
3757 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003758 */
3759static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3760 int size)
3761{
3762 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003763#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003764 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003765#endif
3766#ifdef SMACK_IPV6_SECMARK_LABELING
3767 struct socket_smack *ssp = sock->sk->sk_security;
3768 struct smack_known *rsp;
3769#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003770 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003771
3772 /*
3773 * Perfectly reasonable for this to be NULL
3774 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003775 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003776 return 0;
3777
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003778 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003779 case AF_INET:
3780 rc = smack_netlabel_send(sock->sk, sip);
3781 break;
3782 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003783#ifdef SMACK_IPV6_SECMARK_LABELING
3784 rsp = smack_ipv6host_label(sap);
3785 if (rsp != NULL)
3786 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3787 SMK_CONNECTING);
3788#endif
3789#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003790 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003791#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003792 break;
3793 }
3794 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003795}
3796
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003797/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003798 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003799 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003800 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003801 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003802 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003803 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003804static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3805 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003806{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003807 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003808 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003809 int acat;
3810 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003811
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003812 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003813 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003814 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003815 * If there are flags but no level netlabel isn't
3816 * behaving the way we expect it to.
3817 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003818 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003819 * Without guidance regarding the smack value
3820 * for the packet fall back on the network
3821 * ambient value.
3822 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003823 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303824 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003825 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003826 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003827 /*
3828 * Compare the catsets. Use the netlbl APIs.
3829 */
3830 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3831 if ((skp->smk_netlabel.flags &
3832 NETLBL_SECATTR_MLS_CAT) == 0)
3833 found = 1;
3834 break;
3835 }
3836 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003837 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3838 acat + 1);
3839 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003840 skp->smk_netlabel.attr.mls.cat,
3841 kcat + 1);
3842 if (acat < 0 || kcat < 0)
3843 break;
3844 }
3845 if (acat == kcat) {
3846 found = 1;
3847 break;
3848 }
Casey Schauflere114e472008-02-04 22:29:50 -08003849 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003850 rcu_read_unlock();
3851
3852 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003853 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003854
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003855 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003856 return &smack_known_web;
3857 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003858 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003859 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003860 /*
3861 * Looks like a fallback, which gives us a secid.
3862 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003863 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003864 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003865 * Without guidance regarding the smack value
3866 * for the packet fall back on the network
3867 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003868 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003869 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003870}
3871
Casey Schaufler69f287a2014-12-12 17:08:40 -08003872#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003873static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003874{
Casey Schauflerc6739442013-05-22 18:42:56 -07003875 u8 nexthdr;
3876 int offset;
3877 int proto = -EINVAL;
3878 struct ipv6hdr _ipv6h;
3879 struct ipv6hdr *ip6;
3880 __be16 frag_off;
3881 struct tcphdr _tcph, *th;
3882 struct udphdr _udph, *uh;
3883 struct dccp_hdr _dccph, *dh;
3884
3885 sip->sin6_port = 0;
3886
3887 offset = skb_network_offset(skb);
3888 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3889 if (ip6 == NULL)
3890 return -EINVAL;
3891 sip->sin6_addr = ip6->saddr;
3892
3893 nexthdr = ip6->nexthdr;
3894 offset += sizeof(_ipv6h);
3895 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3896 if (offset < 0)
3897 return -EINVAL;
3898
3899 proto = nexthdr;
3900 switch (proto) {
3901 case IPPROTO_TCP:
3902 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3903 if (th != NULL)
3904 sip->sin6_port = th->source;
3905 break;
3906 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003907 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003908 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3909 if (uh != NULL)
3910 sip->sin6_port = uh->source;
3911 break;
3912 case IPPROTO_DCCP:
3913 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3914 if (dh != NULL)
3915 sip->sin6_port = dh->dccph_sport;
3916 break;
3917 }
3918 return proto;
3919}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003920#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003921
Casey Schauflere114e472008-02-04 22:29:50 -08003922/**
3923 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3924 * @sk: socket
3925 * @skb: packet
3926 *
3927 * Returns 0 if the packet should be delivered, an error code otherwise
3928 */
3929static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3930{
3931 struct netlbl_lsm_secattr secattr;
3932 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003933 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003934 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003935 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003936 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003937#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003938 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003939#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003940#if IS_ENABLED(CONFIG_IPV6)
3941 struct sockaddr_in6 sadd;
3942 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003943
3944 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3945 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003946#endif /* CONFIG_IPV6 */
3947
Piotr Sawicki129a9982018-07-19 11:42:58 +02003948 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003949 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003950#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3951 /*
3952 * If there is a secmark use it rather than the CIPSO label.
3953 * If there is no secmark fall back to CIPSO.
3954 * The secmark is assumed to reflect policy better.
3955 */
3956 if (skb && skb->secmark != 0) {
3957 skp = smack_from_secid(skb->secmark);
3958 goto access_check;
3959 }
3960#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003961 /*
3962 * Translate what netlabel gave us.
3963 */
3964 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003965
Piotr Sawicki129a9982018-07-19 11:42:58 +02003966 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003967 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003968 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003969 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003970 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003971
Casey Schauflerc6739442013-05-22 18:42:56 -07003972 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003973
Casey Schaufler69f287a2014-12-12 17:08:40 -08003974#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3975access_check:
3976#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003977#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003978 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003979 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003980 ad.a.u.net->netif = skb->skb_iif;
3981 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003982#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003983 /*
3984 * Receiving a packet requires that the other end
3985 * be able to write here. Read access is not required.
3986 * This is the simplist possible security model
3987 * for networking.
3988 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003989 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3990 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003991 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003992 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003993 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003994 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003995#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003996 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003997 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003998 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3999 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08004000 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004001#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004002 if (skb && skb->secmark != 0)
4003 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07004004 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004005 skp = smack_ipv6host_label(&sadd);
4006 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08004007 skp = smack_net_ambient;
4008#ifdef CONFIG_AUDIT
4009 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02004010 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004011 ad.a.u.net->netif = skb->skb_iif;
4012 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4013#endif /* CONFIG_AUDIT */
4014 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4015 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4016 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004017#endif /* SMACK_IPV6_SECMARK_LABELING */
4018#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004019 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004020#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02004021 if (rc != 0)
4022 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4023 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07004024 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004025#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07004026 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004027
Paul Moorea8134292008-10-10 10:16:31 -04004028 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004029}
4030
4031/**
4032 * smack_socket_getpeersec_stream - pull in packet label
4033 * @sock: the socket
4034 * @optval: user's destination
4035 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004036 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004037 *
4038 * returns zero on success, an error code otherwise
4039 */
4040static int smack_socket_getpeersec_stream(struct socket *sock,
4041 char __user *optval,
4042 int __user *optlen, unsigned len)
4043{
4044 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004045 char *rcp = "";
4046 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004047 int rc = 0;
4048
4049 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004050 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004051 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004052 slen = strlen(rcp) + 1;
4053 }
Casey Schauflere114e472008-02-04 22:29:50 -08004054
4055 if (slen > len)
4056 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004057 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004058 rc = -EFAULT;
4059
4060 if (put_user(slen, optlen) != 0)
4061 rc = -EFAULT;
4062
4063 return rc;
4064}
4065
4066
4067/**
4068 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004069 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004070 * @skb: packet data
4071 * @secid: pointer to where to put the secid of the packet
4072 *
4073 * Sets the netlabel socket state on sk from parent
4074 */
4075static int smack_socket_getpeersec_dgram(struct socket *sock,
4076 struct sk_buff *skb, u32 *secid)
4077
4078{
4079 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004080 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004081 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004082 int family = PF_UNSPEC;
4083 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004084 int rc;
4085
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004086 if (skb != NULL) {
4087 if (skb->protocol == htons(ETH_P_IP))
4088 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004089#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004090 else if (skb->protocol == htons(ETH_P_IPV6))
4091 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004092#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004093 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004094 if (family == PF_UNSPEC && sock != NULL)
4095 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004096
Casey Schaufler69f287a2014-12-12 17:08:40 -08004097 switch (family) {
4098 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004099 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004100 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004101 break;
4102 case PF_INET:
4103#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4104 s = skb->secmark;
4105 if (s != 0)
4106 break;
4107#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004108 /*
4109 * Translate what netlabel gave us.
4110 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004111 if (sock != NULL && sock->sk != NULL)
4112 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004113 netlbl_secattr_init(&secattr);
4114 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4115 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004116 skp = smack_from_secattr(&secattr, ssp);
4117 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004118 }
4119 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004120 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004121 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004122#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004123 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004124#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004125 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004126 }
4127 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004128 if (s == 0)
4129 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004130 return 0;
4131}
4132
4133/**
Paul Moore07feee82009-03-27 17:10:54 -04004134 * smack_sock_graft - Initialize a newly created socket with an existing sock
4135 * @sk: child sock
4136 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004137 *
Paul Moore07feee82009-03-27 17:10:54 -04004138 * Set the smk_{in,out} state of an existing sock based on the process that
4139 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004140 */
4141static void smack_sock_graft(struct sock *sk, struct socket *parent)
4142{
4143 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004144 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004145
Paul Moore07feee82009-03-27 17:10:54 -04004146 if (sk == NULL ||
4147 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004148 return;
4149
4150 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004151 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004152 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004153 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004154}
4155
4156/**
4157 * smack_inet_conn_request - Smack access check on connect
4158 * @sk: socket involved
4159 * @skb: packet
4160 * @req: unused
4161 *
4162 * Returns 0 if a task with the packet label could write to
4163 * the socket, otherwise an error code
4164 */
4165static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4166 struct request_sock *req)
4167{
Paul Moore07feee82009-03-27 17:10:54 -04004168 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004169 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004170 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004171 struct netlbl_lsm_secattr secattr;
4172 struct sockaddr_in addr;
4173 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004174 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004175 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004176 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004177#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004178 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004179#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004180
Casey Schaufler69f287a2014-12-12 17:08:40 -08004181#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004182 if (family == PF_INET6) {
4183 /*
4184 * Handle mapped IPv4 packets arriving
4185 * via IPv6 sockets. Don't set up netlabel
4186 * processing on IPv6.
4187 */
4188 if (skb->protocol == htons(ETH_P_IP))
4189 family = PF_INET;
4190 else
4191 return 0;
4192 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004193#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004194
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004195#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4196 /*
4197 * If there is a secmark use it rather than the CIPSO label.
4198 * If there is no secmark fall back to CIPSO.
4199 * The secmark is assumed to reflect policy better.
4200 */
4201 if (skb && skb->secmark != 0) {
4202 skp = smack_from_secid(skb->secmark);
4203 goto access_check;
4204 }
4205#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4206
Paul Moore07feee82009-03-27 17:10:54 -04004207 netlbl_secattr_init(&secattr);
4208 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004209 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004210 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004211 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004212 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004213 netlbl_secattr_destroy(&secattr);
4214
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004215#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4216access_check:
4217#endif
4218
Etienne Bassetecfcc532009-04-08 20:40:06 +02004219#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004220 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4221 ad.a.u.net->family = family;
4222 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004223 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4224#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004225 /*
Paul Moore07feee82009-03-27 17:10:54 -04004226 * Receiving a packet requires that the other end be able to write
4227 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004228 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004229 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4230 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004231 if (rc != 0)
4232 return rc;
4233
4234 /*
4235 * Save the peer's label in the request_sock so we can later setup
4236 * smk_packet in the child socket so that SO_PEERCRED can report it.
4237 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004238 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004239
4240 /*
4241 * We need to decide if we want to label the incoming connection here
4242 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004243 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004244 */
4245 hdr = ip_hdr(skb);
4246 addr.sin_addr.s_addr = hdr->saddr;
4247 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004248 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004249 rcu_read_unlock();
4250
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004251 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004252 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004253 else
Paul Moore07feee82009-03-27 17:10:54 -04004254 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004255
4256 return rc;
4257}
4258
Paul Moore07feee82009-03-27 17:10:54 -04004259/**
4260 * smack_inet_csk_clone - Copy the connection information to the new socket
4261 * @sk: the new socket
4262 * @req: the connection's request_sock
4263 *
4264 * Transfer the connection's peer label to the newly created socket.
4265 */
4266static void smack_inet_csk_clone(struct sock *sk,
4267 const struct request_sock *req)
4268{
4269 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004270 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004271
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004272 if (req->peer_secid != 0) {
4273 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004274 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004275 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004276 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004277}
4278
Casey Schauflere114e472008-02-04 22:29:50 -08004279/*
4280 * Key management security hooks
4281 *
4282 * Casey has not tested key support very heavily.
4283 * The permission check is most likely too restrictive.
4284 * If you care about keys please have a look.
4285 */
4286#ifdef CONFIG_KEYS
4287
4288/**
4289 * smack_key_alloc - Set the key security blob
4290 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004291 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004292 * @flags: unused
4293 *
4294 * No allocation required
4295 *
4296 * Returns 0
4297 */
David Howellsd84f4f92008-11-14 10:39:23 +11004298static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004299 unsigned long flags)
4300{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004301 struct smack_known *skp = smk_of_task(cred->security);
4302
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004303 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004304 return 0;
4305}
4306
4307/**
4308 * smack_key_free - Clear the key security blob
4309 * @key: the object
4310 *
4311 * Clear the blob pointer
4312 */
4313static void smack_key_free(struct key *key)
4314{
4315 key->security = NULL;
4316}
4317
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004318/**
Casey Schauflere114e472008-02-04 22:29:50 -08004319 * smack_key_permission - Smack access on a key
4320 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004321 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004322 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004323 *
4324 * Return 0 if the task has read and write to the object,
4325 * an error code otherwise
4326 */
4327static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004328 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004329{
4330 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004331 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004332 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004333 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004334 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004335
4336 keyp = key_ref_to_ptr(key_ref);
4337 if (keyp == NULL)
4338 return -EINVAL;
4339 /*
4340 * If the key hasn't been initialized give it access so that
4341 * it may do so.
4342 */
4343 if (keyp->security == NULL)
4344 return 0;
4345 /*
4346 * This should not occur
4347 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004348 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004349 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004350
4351 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4352 return 0;
4353
Etienne Bassetecfcc532009-04-08 20:40:06 +02004354#ifdef CONFIG_AUDIT
4355 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4356 ad.a.u.key_struct.key = keyp->serial;
4357 ad.a.u.key_struct.key_desc = keyp->description;
4358#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004359 if (perm & KEY_NEED_READ)
4360 request = MAY_READ;
4361 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4362 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004363 rc = smk_access(tkp, keyp->security, request, &ad);
4364 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4365 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004366}
José Bollo7fc5f362015-02-17 15:41:22 +01004367
4368/*
4369 * smack_key_getsecurity - Smack label tagging the key
4370 * @key points to the key to be queried
4371 * @_buffer points to a pointer that should be set to point to the
4372 * resulting string (if no label or an error occurs).
4373 * Return the length of the string (including terminating NUL) or -ve if
4374 * an error.
4375 * May also return 0 (and a NULL buffer pointer) if there is no label.
4376 */
4377static int smack_key_getsecurity(struct key *key, char **_buffer)
4378{
4379 struct smack_known *skp = key->security;
4380 size_t length;
4381 char *copy;
4382
4383 if (key->security == NULL) {
4384 *_buffer = NULL;
4385 return 0;
4386 }
4387
4388 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4389 if (copy == NULL)
4390 return -ENOMEM;
4391 length = strlen(copy) + 1;
4392
4393 *_buffer = copy;
4394 return length;
4395}
4396
Casey Schauflere114e472008-02-04 22:29:50 -08004397#endif /* CONFIG_KEYS */
4398
4399/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004400 * Smack Audit hooks
4401 *
4402 * Audit requires a unique representation of each Smack specific
4403 * rule. This unique representation is used to distinguish the
4404 * object to be audited from remaining kernel objects and also
4405 * works as a glue between the audit hooks.
4406 *
4407 * Since repository entries are added but never deleted, we'll use
4408 * the smack_known label address related to the given audit rule as
4409 * the needed unique representation. This also better fits the smack
4410 * model where nearly everything is a label.
4411 */
4412#ifdef CONFIG_AUDIT
4413
4414/**
4415 * smack_audit_rule_init - Initialize a smack audit rule
4416 * @field: audit rule fields given from user-space (audit.h)
4417 * @op: required testing operator (=, !=, >, <, ...)
4418 * @rulestr: smack label to be audited
4419 * @vrule: pointer to save our own audit rule representation
4420 *
4421 * Prepare to audit cases where (@field @op @rulestr) is true.
4422 * The label to be audited is created if necessay.
4423 */
4424static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4425{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004426 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004427 char **rule = (char **)vrule;
4428 *rule = NULL;
4429
4430 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4431 return -EINVAL;
4432
Al Viro5af75d82008-12-16 05:59:26 -05004433 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004434 return -EINVAL;
4435
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004436 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004437 if (IS_ERR(skp))
4438 return PTR_ERR(skp);
4439
4440 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004441
4442 return 0;
4443}
4444
4445/**
4446 * smack_audit_rule_known - Distinguish Smack audit rules
4447 * @krule: rule of interest, in Audit kernel representation format
4448 *
4449 * This is used to filter Smack rules from remaining Audit ones.
4450 * If it's proved that this rule belongs to us, the
4451 * audit_rule_match hook will be called to do the final judgement.
4452 */
4453static int smack_audit_rule_known(struct audit_krule *krule)
4454{
4455 struct audit_field *f;
4456 int i;
4457
4458 for (i = 0; i < krule->field_count; i++) {
4459 f = &krule->fields[i];
4460
4461 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4462 return 1;
4463 }
4464
4465 return 0;
4466}
4467
4468/**
4469 * smack_audit_rule_match - Audit given object ?
4470 * @secid: security id for identifying the object to test
4471 * @field: audit rule flags given from user-space
4472 * @op: required testing operator
4473 * @vrule: smack internal rule presentation
4474 * @actx: audit context associated with the check
4475 *
4476 * The core Audit hook. It's used to take the decision of
4477 * whether to audit or not to audit a given object.
4478 */
4479static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4480 struct audit_context *actx)
4481{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004482 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004483 char *rule = vrule;
4484
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004485 if (unlikely(!rule)) {
4486 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004487 return -ENOENT;
4488 }
4489
4490 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4491 return 0;
4492
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004493 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004494
4495 /*
4496 * No need to do string comparisons. If a match occurs,
4497 * both pointers will point to the same smack_known
4498 * label.
4499 */
Al Viro5af75d82008-12-16 05:59:26 -05004500 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004501 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004502 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004503 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004504
4505 return 0;
4506}
4507
Casey Schaufler491a0b02016-01-26 15:08:35 -08004508/*
4509 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004510 * No memory was allocated.
4511 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004512
4513#endif /* CONFIG_AUDIT */
4514
Randy Dunlap251a2a92009-02-18 11:42:33 -08004515/**
David Quigley746df9b2013-05-22 12:50:35 -04004516 * smack_ismaclabel - check if xattr @name references a smack MAC label
4517 * @name: Full xattr name to check.
4518 */
4519static int smack_ismaclabel(const char *name)
4520{
4521 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4522}
4523
4524
4525/**
Casey Schauflere114e472008-02-04 22:29:50 -08004526 * smack_secid_to_secctx - return the smack label for a secid
4527 * @secid: incoming integer
4528 * @secdata: destination
4529 * @seclen: how long it is
4530 *
4531 * Exists for networking code.
4532 */
4533static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4534{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004535 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004536
Eric Parisd5630b92010-10-13 16:24:48 -04004537 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004538 *secdata = skp->smk_known;
4539 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004540 return 0;
4541}
4542
Randy Dunlap251a2a92009-02-18 11:42:33 -08004543/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004544 * smack_secctx_to_secid - return the secid for a smack label
4545 * @secdata: smack label
4546 * @seclen: how long result is
4547 * @secid: outgoing integer
4548 *
4549 * Exists for audit and networking code.
4550 */
David Howellse52c17642008-04-29 20:52:51 +01004551static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004552{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004553 struct smack_known *skp = smk_find_entry(secdata);
4554
4555 if (skp)
4556 *secid = skp->smk_secid;
4557 else
4558 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004559 return 0;
4560}
4561
Casey Schaufler491a0b02016-01-26 15:08:35 -08004562/*
4563 * There used to be a smack_release_secctx hook
4564 * that did nothing back when hooks were in a vector.
4565 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004566 */
Casey Schauflere114e472008-02-04 22:29:50 -08004567
David P. Quigley1ee65e32009-09-03 14:25:57 -04004568static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4569{
4570 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4571}
4572
4573static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4574{
4575 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4576}
4577
4578static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4579{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004580 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004581
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004582 *ctx = skp->smk_known;
4583 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004584 return 0;
4585}
4586
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004587static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4588{
4589
4590 struct task_smack *tsp;
4591 struct smack_known *skp;
4592 struct inode_smack *isp;
4593 struct cred *new_creds = *new;
4594
4595 if (new_creds == NULL) {
4596 new_creds = prepare_creds();
4597 if (new_creds == NULL)
4598 return -ENOMEM;
4599 }
4600
4601 tsp = new_creds->security;
4602
4603 /*
4604 * Get label from overlay inode and set it in create_sid
4605 */
4606 isp = d_inode(dentry->d_parent)->i_security;
4607 skp = isp->smk_inode;
4608 tsp->smk_task = skp;
4609 *new = new_creds;
4610 return 0;
4611}
4612
4613static int smack_inode_copy_up_xattr(const char *name)
4614{
4615 /*
4616 * Return 1 if this is the smack access Smack attribute.
4617 */
4618 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4619 return 1;
4620
4621 return -EOPNOTSUPP;
4622}
4623
4624static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4625 struct qstr *name,
4626 const struct cred *old,
4627 struct cred *new)
4628{
4629 struct task_smack *otsp = old->security;
4630 struct task_smack *ntsp = new->security;
4631 struct inode_smack *isp;
4632 int may;
4633
4634 /*
4635 * Use the process credential unless all of
4636 * the transmuting criteria are met
4637 */
4638 ntsp->smk_task = otsp->smk_task;
4639
4640 /*
4641 * the attribute of the containing directory
4642 */
4643 isp = d_inode(dentry->d_parent)->i_security;
4644
4645 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4646 rcu_read_lock();
4647 may = smk_access_entry(otsp->smk_task->smk_known,
4648 isp->smk_inode->smk_known,
4649 &otsp->smk_task->smk_rules);
4650 rcu_read_unlock();
4651
4652 /*
4653 * If the directory is transmuting and the rule
4654 * providing access is transmuting use the containing
4655 * directory label instead of the process label.
4656 */
4657 if (may > 0 && (may & MAY_TRANSMUTE))
4658 ntsp->smk_task = isp->smk_inode;
4659 }
4660 return 0;
4661}
4662
James Morrisca97d932017-02-15 00:18:51 +11004663static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004664 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4665 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4666 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004667
Casey Schauflere20b0432015-05-02 15:11:36 -07004668 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4669 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4670 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4671 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4672 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304673 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4674 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
Casey Schauflere114e472008-02-04 22:29:50 -08004675
Casey Schauflere20b0432015-05-02 15:11:36 -07004676 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004677
Casey Schauflere20b0432015-05-02 15:11:36 -07004678 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4679 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4680 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4681 LSM_HOOK_INIT(inode_link, smack_inode_link),
4682 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4683 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4684 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4685 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4686 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4687 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4688 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4689 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4690 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4691 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4692 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4693 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4694 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4695 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004696
Casey Schauflere20b0432015-05-02 15:11:36 -07004697 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4698 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4699 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4700 LSM_HOOK_INIT(file_lock, smack_file_lock),
4701 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4702 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4703 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4704 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4705 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4706 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004707
Casey Schauflere20b0432015-05-02 15:11:36 -07004708 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004709
Casey Schauflere20b0432015-05-02 15:11:36 -07004710 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4711 LSM_HOOK_INIT(cred_free, smack_cred_free),
4712 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4713 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004714 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004715 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4716 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4717 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4718 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4719 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4720 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4721 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4722 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4723 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4724 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4725 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4726 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4727 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004728 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004729
Casey Schauflere20b0432015-05-02 15:11:36 -07004730 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4731 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004732
Casey Schauflere20b0432015-05-02 15:11:36 -07004733 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4734 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004735
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004736 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4737 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004738 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4739 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4740 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4741 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004742
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004743 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4744 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004745 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4746 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4747 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004748
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004749 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4750 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004751 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4752 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4753 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004754
Casey Schauflere20b0432015-05-02 15:11:36 -07004755 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004756
Casey Schauflere20b0432015-05-02 15:11:36 -07004757 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4758 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004759
Casey Schauflere20b0432015-05-02 15:11:36 -07004760 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4761 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004762
Casey Schauflere20b0432015-05-02 15:11:36 -07004763 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004764 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004765#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004766 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004767#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004768 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4769 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4770 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4771 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4772 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4773 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4774 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4775 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4776 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4777 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004778
Casey Schauflere114e472008-02-04 22:29:50 -08004779 /* key management security hooks */
4780#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004781 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4782 LSM_HOOK_INIT(key_free, smack_key_free),
4783 LSM_HOOK_INIT(key_permission, smack_key_permission),
4784 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004785#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004786
4787 /* Audit hooks */
4788#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004789 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4790 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4791 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004792#endif /* CONFIG_AUDIT */
4793
Casey Schauflere20b0432015-05-02 15:11:36 -07004794 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4795 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4796 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004797 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4798 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4799 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004800 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4801 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4802 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004803};
4804
Etienne Basset7198e2e2009-03-24 20:53:24 +01004805
Casey Schaufler86812bb2012-04-17 18:55:46 -07004806static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004807{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004808 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004809 * Initialize rule list locks
4810 */
4811 mutex_init(&smack_known_huh.smk_rules_lock);
4812 mutex_init(&smack_known_hat.smk_rules_lock);
4813 mutex_init(&smack_known_floor.smk_rules_lock);
4814 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004815 mutex_init(&smack_known_web.smk_rules_lock);
4816 /*
4817 * Initialize rule lists
4818 */
4819 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4820 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4821 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4822 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004823 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4824 /*
4825 * Create the known labels list
4826 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004827 smk_insert_entry(&smack_known_huh);
4828 smk_insert_entry(&smack_known_hat);
4829 smk_insert_entry(&smack_known_star);
4830 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004831 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004832}
4833
Casey Schauflere114e472008-02-04 22:29:50 -08004834/**
4835 * smack_init - initialize the smack system
4836 *
4837 * Returns 0
4838 */
4839static __init int smack_init(void)
4840{
David Howellsd84f4f92008-11-14 10:39:23 +11004841 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004842 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004843
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004844 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004845 return 0;
4846
Rohit1a5b4722014-10-15 17:40:41 +05304847 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4848 if (!smack_inode_cache)
4849 return -ENOMEM;
4850
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004851 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4852 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304853 if (tsp == NULL) {
4854 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004855 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304856 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004857
José Bollod21b7b02015-10-02 15:15:56 +02004858 smack_enabled = 1;
4859
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004860 pr_info("Smack: Initializing.\n");
4861#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4862 pr_info("Smack: Netfilter enabled.\n");
4863#endif
4864#ifdef SMACK_IPV6_PORT_LABELING
4865 pr_info("Smack: IPv6 port labeling enabled.\n");
4866#endif
4867#ifdef SMACK_IPV6_SECMARK_LABELING
4868 pr_info("Smack: IPv6 Netfilter enabled.\n");
4869#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004870
4871 /*
4872 * Set the security state for the initial task.
4873 */
David Howellsd84f4f92008-11-14 10:39:23 +11004874 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004875 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004876
Casey Schaufler86812bb2012-04-17 18:55:46 -07004877 /* initialize the smack_known_list */
4878 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004879
4880 /*
4881 * Register with LSM
4882 */
Casey Schauflerd69dece52017-01-18 17:09:05 -08004883 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
Casey Schauflere114e472008-02-04 22:29:50 -08004884
4885 return 0;
4886}
4887
4888/*
4889 * Smack requires early initialization in order to label
4890 * all processes and objects when they are created.
4891 */
4892security_initcall(smack_init);