blob: 835cca277c2a1c4ec521c7228aaa48ad35e8d1a2 [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);
Al Viro6466f3d2018-12-01 22:55:40 -0500738 security_free_mnt_opts(opts);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530739 return rc;
740}
741
Al Viro5b400232018-12-12 20:13:29 -0500742static int smack_sb_eat_lsm_opts(char *options, struct security_mnt_opts *opts)
743{
744 char *s = (char *)get_zeroed_page(GFP_KERNEL);
745 int err;
746
747 if (!s)
748 return -ENOMEM;
749 err = smack_sb_copy_data(options, s);
750 if (!err)
751 err = smack_parse_opts_str(s, opts);
752 free_page((unsigned long)s);
753 return err;
754}
755
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530756/**
757 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800758 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530759 * @opts: Smack mount options
760 * @kern_flags: mount option from kernel space or user space
761 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800762 *
763 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530764 *
765 * Allow filesystems with binary mount data to explicitly set Smack mount
766 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800767 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530768static int smack_set_mnt_opts(struct super_block *sb,
769 struct security_mnt_opts *opts,
770 unsigned long kern_flags,
771 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800772{
773 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000774 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800775 struct superblock_smack *sp = sb->s_security;
776 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800777 struct smack_known *skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530778 int i;
779 int num_opts = opts->num_mnt_opts;
Casey Schauflere830b392013-05-22 18:43:07 -0700780 int transmute = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800781
Seth Forshee9f50eda2015-09-23 15:16:06 -0500782 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800783 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700784
Himanshu Shukla2097f592016-11-10 16:19:52 +0530785 if (!smack_privileged(CAP_MAC_ADMIN)) {
786 /*
787 * Unprivileged mounts don't get to specify Smack values.
788 */
789 if (num_opts)
790 return -EPERM;
791 /*
792 * Unprivileged mounts get root and default from the caller.
793 */
794 skp = smk_of_current();
795 sp->smk_root = skp;
796 sp->smk_default = skp;
797 /*
798 * For a handful of fs types with no user-controlled
799 * backing store it's okay to trust security labels
800 * in the filesystem. The rest are untrusted.
801 */
802 if (sb->s_user_ns != &init_user_ns &&
803 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
804 sb->s_magic != RAMFS_MAGIC) {
805 transmute = 1;
806 sp->smk_flags |= SMK_SB_UNTRUSTED;
807 }
808 }
809
Seth Forshee9f50eda2015-09-23 15:16:06 -0500810 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800811
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530812 for (i = 0; i < num_opts; i++) {
813 switch (opts->mnt_opts_flags[i]) {
814 case FSDEFAULT_MNT:
815 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200816 if (IS_ERR(skp))
817 return PTR_ERR(skp);
818 sp->smk_default = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530819 break;
820 case FSFLOOR_MNT:
821 skp = smk_import_entry(opts->mnt_opts[i], 0);
822 if (IS_ERR(skp))
823 return PTR_ERR(skp);
824 sp->smk_floor = skp;
825 break;
826 case FSHAT_MNT:
827 skp = smk_import_entry(opts->mnt_opts[i], 0);
828 if (IS_ERR(skp))
829 return PTR_ERR(skp);
830 sp->smk_hat = skp;
831 break;
832 case FSROOT_MNT:
833 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200834 if (IS_ERR(skp))
835 return PTR_ERR(skp);
836 sp->smk_root = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530837 break;
838 case FSTRANS_MNT:
839 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200840 if (IS_ERR(skp))
841 return PTR_ERR(skp);
842 sp->smk_root = skp;
843 transmute = 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530844 break;
845 default:
846 break;
Casey Schauflere114e472008-02-04 22:29:50 -0800847 }
848 }
849
850 /*
851 * Initialize the root inode.
852 */
853 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100854 if (isp == NULL) {
855 isp = new_inode_smack(sp->smk_root);
856 if (isp == NULL)
857 return -ENOMEM;
858 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700859 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800860 isp->smk_inode = sp->smk_root;
861
Casey Schauflere830b392013-05-22 18:43:07 -0700862 if (transmute)
863 isp->smk_flags |= SMK_INODE_TRANSMUTE;
864
Casey Schauflere114e472008-02-04 22:29:50 -0800865 return 0;
866}
867
868/**
869 * smack_sb_statfs - Smack check on statfs
870 * @dentry: identifies the file system in question
871 *
872 * Returns 0 if current can read the floor of the filesystem,
873 * and error code otherwise
874 */
875static int smack_sb_statfs(struct dentry *dentry)
876{
877 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200878 int rc;
879 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800880
Eric Parisa2694342011-04-25 13:10:27 -0400881 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200882 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
883
884 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700885 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200886 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800887}
888
Casey Schauflere114e472008-02-04 22:29:50 -0800889/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800890 * BPRM hooks
891 */
892
Casey Schauflerce8a4322011-09-29 18:21:01 -0700893/**
894 * smack_bprm_set_creds - set creds for exec
895 * @bprm: the exec information
896 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100897 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700898 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800899static int smack_bprm_set_creds(struct linux_binprm *bprm)
900{
Al Viro496ad9a2013-01-23 17:07:38 -0500901 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300902 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800903 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500904 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800905 int rc;
906
Kees Cookddb4a142017-07-18 15:25:23 -0700907 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800908 return 0;
909
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300910 isp = inode->i_security;
911 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800912 return 0;
913
Seth Forshee809c02e2016-04-26 14:36:22 -0500914 sbsp = inode->i_sb->s_security;
915 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
916 isp->smk_task != sbsp->smk_root)
917 return 0;
918
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300919 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100920 struct task_struct *tracer;
921 rc = 0;
922
923 rcu_read_lock();
924 tracer = ptrace_parent(current);
925 if (likely(tracer != NULL))
926 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200927 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100928 PTRACE_MODE_ATTACH,
929 __func__);
930 rcu_read_unlock();
931
932 if (rc != 0)
933 return rc;
934 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300935 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800936
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300937 bsp->smk_task = isp->smk_task;
938 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800939
Kees Cookccbb6e12017-07-18 15:25:26 -0700940 /* Decide if this is a secure exec. */
941 if (bsp->smk_task != bsp->smk_forked)
942 bprm->secureexec = 1;
943
Casey Schaufler676dac42010-12-02 06:43:39 -0800944 return 0;
945}
946
947/*
Casey Schauflere114e472008-02-04 22:29:50 -0800948 * Inode hooks
949 */
950
951/**
952 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800953 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800954 *
955 * Returns 0 if it gets a blob, -ENOMEM otherwise
956 */
957static int smack_inode_alloc_security(struct inode *inode)
958{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700959 struct smack_known *skp = smk_of_current();
960
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200961 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800962 if (inode->i_security == NULL)
963 return -ENOMEM;
964 return 0;
965}
966
967/**
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530968 * smack_inode_free_rcu - Free inode_smack blob from cache
969 * @head: the rcu_head for getting inode_smack pointer
970 *
971 * Call back function called from call_rcu() to free
972 * the i_security blob pointer in inode
973 */
974static void smack_inode_free_rcu(struct rcu_head *head)
975{
976 struct inode_smack *issp;
977
978 issp = container_of(head, struct inode_smack, smk_rcu);
979 kmem_cache_free(smack_inode_cache, issp);
980}
981
982/**
983 * smack_inode_free_security - free an inode blob using call_rcu()
Randy Dunlap251a2a92009-02-18 11:42:33 -0800984 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800985 *
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530986 * Clears the blob pointer in inode using RCU
Casey Schauflere114e472008-02-04 22:29:50 -0800987 */
988static void smack_inode_free_security(struct inode *inode)
989{
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530990 struct inode_smack *issp = inode->i_security;
991
992 /*
993 * The inode may still be referenced in a path walk and
994 * a call to smack_inode_permission() can be made
995 * after smack_inode_free_security() is called.
996 * To avoid race condition free the i_security via RCU
997 * and leave the current inode->i_security pointer intact.
998 * The inode will be freed after the RCU grace period too.
999 */
1000 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
Casey Schauflere114e472008-02-04 22:29:50 -08001001}
1002
1003/**
1004 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001005 * @inode: the newly created inode
1006 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -05001007 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001008 * @name: where to put the attribute name
1009 * @value: where to put the attribute value
1010 * @len: where to put the length of the attribute
1011 *
1012 * Returns 0 if it all works out, -ENOMEM if there's no memory
1013 */
1014static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09001015 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05001016 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -08001017{
Casey Schaufler2267b132012-03-13 19:14:19 -07001018 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001019 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001020 struct smack_known *isp = smk_of_inode(inode);
1021 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001022 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08001023
Tetsuo Handa95489062013-07-25 05:44:02 +09001024 if (name)
1025 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -08001026
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001027 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001028 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001029 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1030 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001031 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001032
1033 /*
1034 * If the access rule allows transmutation and
1035 * the directory requests transmutation then
1036 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001037 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001038 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001039 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001040 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001041 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001042 issp->smk_flags |= SMK_INODE_CHANGED;
1043 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001044
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001045 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001046 if (*value == NULL)
1047 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001048
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001049 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001050 }
Casey Schauflere114e472008-02-04 22:29:50 -08001051
1052 return 0;
1053}
1054
1055/**
1056 * smack_inode_link - Smack check on link
1057 * @old_dentry: the existing object
1058 * @dir: unused
1059 * @new_dentry: the new object
1060 *
1061 * Returns 0 if access is permitted, an error code otherwise
1062 */
1063static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1064 struct dentry *new_dentry)
1065{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001066 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001067 struct smk_audit_info ad;
1068 int rc;
1069
Eric Parisa2694342011-04-25 13:10:27 -04001070 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001071 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001072
David Howellsc6f493d2015-03-17 22:26:22 +00001073 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001074 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001075 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001076
David Howells88025652015-01-29 12:02:32 +00001077 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001078 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001079 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1080 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001081 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001082 }
1083
1084 return rc;
1085}
1086
1087/**
1088 * smack_inode_unlink - Smack check on inode deletion
1089 * @dir: containing directory object
1090 * @dentry: file to unlink
1091 *
1092 * Returns 0 if current can write the containing directory
1093 * and the object, error code otherwise
1094 */
1095static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1096{
David Howellsc6f493d2015-03-17 22:26:22 +00001097 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001098 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001099 int rc;
1100
Eric Parisa2694342011-04-25 13:10:27 -04001101 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1103
Casey Schauflere114e472008-02-04 22:29:50 -08001104 /*
1105 * You need write access to the thing you're unlinking
1106 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001107 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001108 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001109 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001110 /*
1111 * You also need write access to the containing directory
1112 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001113 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001114 smk_ad_setfield_u_fs_inode(&ad, dir);
1115 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001116 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001117 }
Casey Schauflere114e472008-02-04 22:29:50 -08001118 return rc;
1119}
1120
1121/**
1122 * smack_inode_rmdir - Smack check on directory deletion
1123 * @dir: containing directory object
1124 * @dentry: directory to unlink
1125 *
1126 * Returns 0 if current can write the containing directory
1127 * and the directory, error code otherwise
1128 */
1129static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1130{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001131 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001132 int rc;
1133
Eric Parisa2694342011-04-25 13:10:27 -04001134 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001135 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1136
Casey Schauflere114e472008-02-04 22:29:50 -08001137 /*
1138 * You need write access to the thing you're removing
1139 */
David Howellsc6f493d2015-03-17 22:26:22 +00001140 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1141 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001142 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001143 /*
1144 * You also need write access to the containing directory
1145 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001146 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001147 smk_ad_setfield_u_fs_inode(&ad, dir);
1148 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001149 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001150 }
Casey Schauflere114e472008-02-04 22:29:50 -08001151
1152 return rc;
1153}
1154
1155/**
1156 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001157 * @old_inode: unused
1158 * @old_dentry: the old object
1159 * @new_inode: unused
1160 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001161 *
1162 * Read and write access is required on both the old and
1163 * new directories.
1164 *
1165 * Returns 0 if access is permitted, an error code otherwise
1166 */
1167static int smack_inode_rename(struct inode *old_inode,
1168 struct dentry *old_dentry,
1169 struct inode *new_inode,
1170 struct dentry *new_dentry)
1171{
1172 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001173 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001174 struct smk_audit_info ad;
1175
Eric Parisa2694342011-04-25 13:10:27 -04001176 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001177 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001178
David Howellsc6f493d2015-03-17 22:26:22 +00001179 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001180 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001181 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001182
David Howells88025652015-01-29 12:02:32 +00001183 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001184 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001185 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1186 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001187 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001188 }
Casey Schauflere114e472008-02-04 22:29:50 -08001189 return rc;
1190}
1191
1192/**
1193 * smack_inode_permission - Smack version of permission()
1194 * @inode: the inode in question
1195 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001196 *
1197 * This is the important Smack hook.
1198 *
1199 * Returns 0 if access is permitted, -EACCES otherwise
1200 */
Al Viroe74f71e2011-06-20 19:38:15 -04001201static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001202{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001203 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001204 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001205 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001206 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001207
1208 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001209 /*
1210 * No permission to check. Existence test. Yup, it's there.
1211 */
1212 if (mask == 0)
1213 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001214
Seth Forshee9f50eda2015-09-23 15:16:06 -05001215 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1216 if (smk_of_inode(inode) != sbsp->smk_root)
1217 return -EACCES;
1218 }
1219
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001220 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001221 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001222 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001223 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001224 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001225 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1226 rc = smk_bu_inode(inode, mask, rc);
1227 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001228}
1229
1230/**
1231 * smack_inode_setattr - Smack check for setting attributes
1232 * @dentry: the object
1233 * @iattr: for the force flag
1234 *
1235 * Returns 0 if access is permitted, an error code otherwise
1236 */
1237static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1238{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001239 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001240 int rc;
1241
Casey Schauflere114e472008-02-04 22:29:50 -08001242 /*
1243 * Need to allow for clearing the setuid bit.
1244 */
1245 if (iattr->ia_valid & ATTR_FORCE)
1246 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001247 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001248 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001249
David Howellsc6f493d2015-03-17 22:26:22 +00001250 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1251 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001252 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001253}
1254
1255/**
1256 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001257 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001258 * @dentry: the object
1259 *
1260 * Returns 0 if access is permitted, an error code otherwise
1261 */
Al Viro3f7036a2015-03-08 19:28:30 -04001262static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001263{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001264 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001265 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001266 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001267
Eric Parisf48b7392011-04-25 12:54:27 -04001268 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001269 smk_ad_setfield_u_fs_path(&ad, *path);
1270 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1271 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001272 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001273}
1274
1275/**
1276 * smack_inode_setxattr - Smack check for setting xattrs
1277 * @dentry: the object
1278 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001279 * @value: value of the attribute
1280 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001281 * @flags: unused
1282 *
1283 * This protects the Smack attribute explicitly.
1284 *
1285 * Returns 0 if access is permitted, an error code otherwise
1286 */
David Howells8f0cfa52008-04-29 00:59:41 -07001287static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1288 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001289{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001290 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001291 struct smack_known *skp;
1292 int check_priv = 0;
1293 int check_import = 0;
1294 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001295 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001296
Casey Schaufler19760ad2013-12-16 16:27:26 -08001297 /*
1298 * Check label validity here so import won't fail in post_setxattr
1299 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001300 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1301 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001302 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1303 check_priv = 1;
1304 check_import = 1;
1305 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1306 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1307 check_priv = 1;
1308 check_import = 1;
1309 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001310 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001311 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001312 if (size != TRANS_TRUE_SIZE ||
1313 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1314 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001315 } else
1316 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1317
Casey Schaufler19760ad2013-12-16 16:27:26 -08001318 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1319 rc = -EPERM;
1320
1321 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001322 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001323 if (IS_ERR(skp))
1324 rc = PTR_ERR(skp);
1325 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001326 (skp == &smack_known_star || skp == &smack_known_web)))
1327 rc = -EINVAL;
1328 }
1329
Eric Parisa2694342011-04-25 13:10:27 -04001330 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001331 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1332
Casey Schauflerd166c802014-08-27 14:51:27 -07001333 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001334 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1335 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001336 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001337
1338 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001339}
1340
1341/**
1342 * smack_inode_post_setxattr - Apply the Smack update approved above
1343 * @dentry: object
1344 * @name: attribute name
1345 * @value: attribute value
1346 * @size: attribute size
1347 * @flags: unused
1348 *
1349 * Set the pointer in the inode blob to the entry found
1350 * in the master label list.
1351 */
David Howells8f0cfa52008-04-29 00:59:41 -07001352static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1353 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001354{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001355 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001356 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001357
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001358 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1359 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1360 return;
1361 }
1362
Casey Schaufler676dac42010-12-02 06:43:39 -08001363 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001364 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001365 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001366 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001367 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001368 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001369 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001370 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001371 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001372 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001373 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001374 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001375 }
Casey Schauflere114e472008-02-04 22:29:50 -08001376
1377 return;
1378}
1379
Casey Schauflerce8a4322011-09-29 18:21:01 -07001380/**
Casey Schauflere114e472008-02-04 22:29:50 -08001381 * smack_inode_getxattr - Smack check on getxattr
1382 * @dentry: the object
1383 * @name: unused
1384 *
1385 * Returns 0 if access is permitted, an error code otherwise
1386 */
David Howells8f0cfa52008-04-29 00:59:41 -07001387static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001388{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001389 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001390 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001391
Eric Parisa2694342011-04-25 13:10:27 -04001392 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001393 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1394
David Howellsc6f493d2015-03-17 22:26:22 +00001395 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1396 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001397 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001398}
1399
Casey Schauflerce8a4322011-09-29 18:21:01 -07001400/**
Casey Schauflere114e472008-02-04 22:29:50 -08001401 * smack_inode_removexattr - Smack check on removexattr
1402 * @dentry: the object
1403 * @name: name of the attribute
1404 *
1405 * Removing the Smack attribute requires CAP_MAC_ADMIN
1406 *
1407 * Returns 0 if access is permitted, an error code otherwise
1408 */
David Howells8f0cfa52008-04-29 00:59:41 -07001409static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001410{
Casey Schaufler676dac42010-12-02 06:43:39 -08001411 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001412 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001413 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001414
Casey Schauflerbcdca222008-02-23 15:24:04 -08001415 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1416 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001417 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001418 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001419 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301420 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001421 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001422 rc = -EPERM;
1423 } else
1424 rc = cap_inode_removexattr(dentry, name);
1425
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001426 if (rc != 0)
1427 return rc;
1428
Eric Parisa2694342011-04-25 13:10:27 -04001429 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001430 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001431
David Howellsc6f493d2015-03-17 22:26:22 +00001432 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1433 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001434 if (rc != 0)
1435 return rc;
1436
David Howellsc6f493d2015-03-17 22:26:22 +00001437 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001438 /*
1439 * Don't do anything special for these.
1440 * XATTR_NAME_SMACKIPIN
1441 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001442 */
José Bollo80124952016-01-12 21:23:40 +01001443 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001444 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001445 struct superblock_smack *sbsp = sbp->s_security;
1446
1447 isp->smk_inode = sbsp->smk_default;
1448 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001449 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001450 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001451 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001452 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1453 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001454
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001455 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001456}
1457
1458/**
1459 * smack_inode_getsecurity - get smack xattrs
1460 * @inode: the object
1461 * @name: attribute name
1462 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001463 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001464 *
1465 * Returns the size of the attribute or an error code
1466 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001467static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001468 const char *name, void **buffer,
1469 bool alloc)
1470{
1471 struct socket_smack *ssp;
1472 struct socket *sock;
1473 struct super_block *sbp;
1474 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001475 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001476
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001477 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001478 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001479 else {
1480 /*
1481 * The rest of the Smack xattrs are only on sockets.
1482 */
1483 sbp = ip->i_sb;
1484 if (sbp->s_magic != SOCKFS_MAGIC)
1485 return -EOPNOTSUPP;
1486
1487 sock = SOCKET_I(ip);
1488 if (sock == NULL || sock->sk == NULL)
1489 return -EOPNOTSUPP;
1490
1491 ssp = sock->sk->sk_security;
1492
1493 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1494 isp = ssp->smk_in;
1495 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1496 isp = ssp->smk_out;
1497 else
1498 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001499 }
1500
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001501 if (alloc) {
1502 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1503 if (*buffer == NULL)
1504 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001505 }
1506
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001507 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001508}
1509
1510
1511/**
1512 * smack_inode_listsecurity - list the Smack attributes
1513 * @inode: the object
1514 * @buffer: where they go
1515 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001516 */
1517static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1518 size_t buffer_size)
1519{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001520 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001521
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001522 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001523 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001524
1525 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001526}
1527
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001528/**
1529 * smack_inode_getsecid - Extract inode's security id
1530 * @inode: inode to extract the info from
1531 * @secid: where result will be saved
1532 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001533static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001534{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001535 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001536
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001537 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001538}
1539
Casey Schauflere114e472008-02-04 22:29:50 -08001540/*
1541 * File Hooks
1542 */
1543
Casey Schaufler491a0b02016-01-26 15:08:35 -08001544/*
1545 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001546 *
1547 * Should access checks be done on each read or write?
1548 * UNICOS and SELinux say yes.
1549 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1550 *
1551 * I'll say no for now. Smack does not do the frequent
1552 * label changing that SELinux does.
1553 */
Casey Schauflere114e472008-02-04 22:29:50 -08001554
1555/**
1556 * smack_file_alloc_security - assign a file security blob
1557 * @file: the object
1558 *
1559 * The security blob for a file is a pointer to the master
1560 * label list, so no allocation is done.
1561 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001562 * f_security is the owner security information. It
1563 * isn't used on file access checks, it's for send_sigio.
1564 *
Casey Schauflere114e472008-02-04 22:29:50 -08001565 * Returns 0
1566 */
1567static int smack_file_alloc_security(struct file *file)
1568{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001569 struct smack_known *skp = smk_of_current();
1570
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001571 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001572 return 0;
1573}
1574
1575/**
1576 * smack_file_free_security - clear a file security blob
1577 * @file: the object
1578 *
1579 * The security blob for a file is a pointer to the master
1580 * label list, so no memory is freed.
1581 */
1582static void smack_file_free_security(struct file *file)
1583{
1584 file->f_security = NULL;
1585}
1586
1587/**
1588 * smack_file_ioctl - Smack check on ioctls
1589 * @file: the object
1590 * @cmd: what to do
1591 * @arg: unused
1592 *
1593 * Relies heavily on the correct use of the ioctl command conventions.
1594 *
1595 * Returns 0 if allowed, error code otherwise
1596 */
1597static int smack_file_ioctl(struct file *file, unsigned int cmd,
1598 unsigned long arg)
1599{
1600 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001601 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001602 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001603
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001604 if (unlikely(IS_PRIVATE(inode)))
1605 return 0;
1606
Eric Parisf48b7392011-04-25 12:54:27 -04001607 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001608 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001609
Casey Schauflerd166c802014-08-27 14:51:27 -07001610 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001611 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001612 rc = smk_bu_file(file, MAY_WRITE, rc);
1613 }
Casey Schauflere114e472008-02-04 22:29:50 -08001614
Casey Schauflerd166c802014-08-27 14:51:27 -07001615 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001616 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001617 rc = smk_bu_file(file, MAY_READ, rc);
1618 }
Casey Schauflere114e472008-02-04 22:29:50 -08001619
1620 return rc;
1621}
1622
1623/**
1624 * smack_file_lock - Smack check on file locking
1625 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001626 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001627 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001628 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001629 */
1630static int smack_file_lock(struct file *file, unsigned int cmd)
1631{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001632 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001633 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001634 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001635
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001636 if (unlikely(IS_PRIVATE(inode)))
1637 return 0;
1638
Eric Paris92f42502011-04-25 13:15:55 -04001639 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1640 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001641 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001642 rc = smk_bu_file(file, MAY_LOCK, rc);
1643 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001644}
1645
1646/**
1647 * smack_file_fcntl - Smack check on fcntl
1648 * @file: the object
1649 * @cmd: what action to check
1650 * @arg: unused
1651 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001652 * Generally these operations are harmless.
1653 * File locking operations present an obvious mechanism
1654 * for passing information, so they require write access.
1655 *
Casey Schauflere114e472008-02-04 22:29:50 -08001656 * Returns 0 if current has access, error code otherwise
1657 */
1658static int smack_file_fcntl(struct file *file, unsigned int cmd,
1659 unsigned long arg)
1660{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001661 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001662 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001663 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001664
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001665 if (unlikely(IS_PRIVATE(inode)))
1666 return 0;
1667
Casey Schauflere114e472008-02-04 22:29:50 -08001668 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001669 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001670 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001671 case F_SETLK:
1672 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001673 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1674 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001675 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001676 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001677 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001678 case F_SETOWN:
1679 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001680 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1681 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001682 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001683 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001684 break;
1685 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001686 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001687 }
1688
1689 return rc;
1690}
1691
1692/**
Al Viroe5467852012-05-30 13:30:51 -04001693 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001694 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1695 * if mapping anonymous memory.
1696 * @file contains the file structure for file to map (may be NULL).
1697 * @reqprot contains the protection requested by the application.
1698 * @prot contains the protection that will be applied by the kernel.
1699 * @flags contains the operational flags.
1700 * Return 0 if permission is granted.
1701 */
Al Viroe5467852012-05-30 13:30:51 -04001702static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001703 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001704 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001705{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001706 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001707 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001708 struct smack_rule *srp;
1709 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001710 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001711 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001712 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001713 int may;
1714 int mmay;
1715 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001716 int rc;
1717
Al Viro496ad9a2013-01-23 17:07:38 -05001718 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001719 return 0;
1720
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001721 if (unlikely(IS_PRIVATE(file_inode(file))))
1722 return 0;
1723
Al Viro496ad9a2013-01-23 17:07:38 -05001724 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001725 if (isp->smk_mmap == NULL)
1726 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001727 sbsp = file_inode(file)->i_sb->s_security;
1728 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1729 isp->smk_mmap != sbsp->smk_root)
1730 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001731 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001732
1733 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001734 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001735 rc = 0;
1736
1737 rcu_read_lock();
1738 /*
1739 * For each Smack rule associated with the subject
1740 * label verify that the SMACK64MMAP also has access
1741 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001742 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001743 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001744 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001745 /*
1746 * Matching labels always allows access.
1747 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001748 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001749 continue;
1750 /*
1751 * If there is a matching local rule take
1752 * that into account as well.
1753 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001754 may = smk_access_entry(srp->smk_subject->smk_known,
1755 okp->smk_known,
1756 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001757 if (may == -ENOENT)
1758 may = srp->smk_access;
1759 else
1760 may &= srp->smk_access;
1761 /*
1762 * If may is zero the SMACK64MMAP subject can't
1763 * possibly have less access.
1764 */
1765 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001766 continue;
1767
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001768 /*
1769 * Fetch the global list entry.
1770 * If there isn't one a SMACK64MMAP subject
1771 * can't have as much access as current.
1772 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001773 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1774 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001775 if (mmay == -ENOENT) {
1776 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001777 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001778 }
1779 /*
1780 * If there is a local entry it modifies the
1781 * potential access, too.
1782 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001783 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1784 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001785 if (tmay != -ENOENT)
1786 mmay &= tmay;
1787
1788 /*
1789 * If there is any access available to current that is
1790 * not available to a SMACK64MMAP subject
1791 * deny access.
1792 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001793 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001794 rc = -EACCES;
1795 break;
1796 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001797 }
1798
1799 rcu_read_unlock();
1800
1801 return rc;
1802}
1803
1804/**
Casey Schauflere114e472008-02-04 22:29:50 -08001805 * smack_file_set_fowner - set the file security blob value
1806 * @file: object in question
1807 *
Casey Schauflere114e472008-02-04 22:29:50 -08001808 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001809static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001810{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001811 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001812}
1813
1814/**
1815 * smack_file_send_sigiotask - Smack on sigio
1816 * @tsk: The target task
1817 * @fown: the object the signal come from
1818 * @signum: unused
1819 *
1820 * Allow a privileged task to get signals even if it shouldn't
1821 *
1822 * Returns 0 if a subject with the object's smack could
1823 * write to the task, an error code otherwise.
1824 */
1825static int smack_file_send_sigiotask(struct task_struct *tsk,
1826 struct fown_struct *fown, int signum)
1827{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001828 struct smack_known *skp;
1829 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001830 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001831 struct file *file;
1832 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001833 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001834
1835 /*
1836 * struct fown_struct is never outside the context of a struct file
1837 */
1838 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001839
Etienne Bassetecfcc532009-04-08 20:40:06 +02001840 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001841 skp = file->f_security;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001842 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1843 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001844
1845 rcu_read_lock();
1846 tcred = __task_cred(tsk);
1847 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001848 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001849 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001850
1851 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1852 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001853 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001854 return rc;
1855}
1856
1857/**
1858 * smack_file_receive - Smack file receive check
1859 * @file: the object
1860 *
1861 * Returns 0 if current has access, error code otherwise
1862 */
1863static int smack_file_receive(struct file *file)
1864{
Casey Schauflerd166c802014-08-27 14:51:27 -07001865 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001866 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001867 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001868 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001869 struct socket *sock;
1870 struct task_smack *tsp;
1871 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001872
Seung-Woo Kim97775822015-04-17 15:25:04 +09001873 if (unlikely(IS_PRIVATE(inode)))
1874 return 0;
1875
Casey Schaufler4482a442013-12-30 17:37:45 -08001876 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001877 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001878
Casey Schaufler51d59af2017-05-31 08:53:42 -07001879 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001880 sock = SOCKET_I(inode);
1881 ssp = sock->sk->sk_security;
1882 tsp = current_security();
1883 /*
1884 * If the receiving process can't write to the
1885 * passed socket or if the passed socket can't
1886 * write to the receiving process don't accept
1887 * the passed socket.
1888 */
1889 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1890 rc = smk_bu_file(file, may, rc);
1891 if (rc < 0)
1892 return rc;
1893 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1894 rc = smk_bu_file(file, may, rc);
1895 return rc;
1896 }
Casey Schauflere114e472008-02-04 22:29:50 -08001897 /*
1898 * This code relies on bitmasks.
1899 */
1900 if (file->f_mode & FMODE_READ)
1901 may = MAY_READ;
1902 if (file->f_mode & FMODE_WRITE)
1903 may |= MAY_WRITE;
1904
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001905 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001906 rc = smk_bu_file(file, may, rc);
1907 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001908}
1909
Casey Schaufler531f1d42011-09-19 12:41:42 -07001910/**
Eric Paris83d49852012-04-04 13:45:40 -04001911 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001912 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001913 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001914 *
1915 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001916 * Allow the open only if the task has read access. There are
1917 * many read operations (e.g. fstat) that you can do with an
1918 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001919 *
1920 * Returns 0
1921 */
Al Viro94817692018-07-10 14:13:18 -04001922static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001923{
Al Viro94817692018-07-10 14:13:18 -04001924 struct task_smack *tsp = file->f_cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001925 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001926 struct smk_audit_info ad;
1927 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001928
Casey Schauflera6834c02014-04-21 11:10:26 -07001929 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1930 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301931 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001932 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001933
1934 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001935}
1936
Casey Schauflere114e472008-02-04 22:29:50 -08001937/*
1938 * Task hooks
1939 */
1940
1941/**
David Howellsee18d642009-09-02 09:14:21 +01001942 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1943 * @new: the new credentials
1944 * @gfp: the atomicity of any memory allocations
1945 *
1946 * Prepare a blank set of credentials for modification. This must allocate all
1947 * the memory the LSM module might require such that cred_transfer() can
1948 * complete without error.
1949 */
1950static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1951{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001952 struct task_smack *tsp;
1953
1954 tsp = new_task_smack(NULL, NULL, gfp);
1955 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001956 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001957
1958 cred->security = tsp;
1959
David Howellsee18d642009-09-02 09:14:21 +01001960 return 0;
1961}
1962
1963
1964/**
David Howellsf1752ee2008-11-14 10:39:17 +11001965 * smack_cred_free - "free" task-level security credentials
1966 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001967 *
Casey Schauflere114e472008-02-04 22:29:50 -08001968 */
David Howellsf1752ee2008-11-14 10:39:17 +11001969static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001970{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001971 struct task_smack *tsp = cred->security;
1972 struct smack_rule *rp;
1973 struct list_head *l;
1974 struct list_head *n;
1975
1976 if (tsp == NULL)
1977 return;
1978 cred->security = NULL;
1979
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001980 smk_destroy_label_list(&tsp->smk_relabel);
1981
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001982 list_for_each_safe(l, n, &tsp->smk_rules) {
1983 rp = list_entry(l, struct smack_rule, list);
1984 list_del(&rp->list);
1985 kfree(rp);
1986 }
1987 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001988}
1989
1990/**
David Howellsd84f4f92008-11-14 10:39:23 +11001991 * smack_cred_prepare - prepare new set of credentials for modification
1992 * @new: the new credentials
1993 * @old: the original credentials
1994 * @gfp: the atomicity of any memory allocations
1995 *
1996 * Prepare a new set of credentials for modification.
1997 */
1998static int smack_cred_prepare(struct cred *new, const struct cred *old,
1999 gfp_t gfp)
2000{
Casey Schaufler676dac42010-12-02 06:43:39 -08002001 struct task_smack *old_tsp = old->security;
2002 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002003 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002004
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002005 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002006 if (new_tsp == NULL)
2007 return -ENOMEM;
2008
Himanshu Shuklab437aba2016-11-10 16:17:02 +05302009 new->security = new_tsp;
2010
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002011 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2012 if (rc != 0)
2013 return rc;
2014
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002015 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2016 gfp);
2017 if (rc != 0)
2018 return rc;
2019
David Howellsd84f4f92008-11-14 10:39:23 +11002020 return 0;
2021}
2022
Randy Dunlap251a2a92009-02-18 11:42:33 -08002023/**
David Howellsee18d642009-09-02 09:14:21 +01002024 * smack_cred_transfer - Transfer the old credentials to the new credentials
2025 * @new: the new credentials
2026 * @old: the original credentials
2027 *
2028 * Fill in a set of blank credentials from another set of credentials.
2029 */
2030static void smack_cred_transfer(struct cred *new, const struct cred *old)
2031{
Casey Schaufler676dac42010-12-02 06:43:39 -08002032 struct task_smack *old_tsp = old->security;
2033 struct task_smack *new_tsp = new->security;
2034
2035 new_tsp->smk_task = old_tsp->smk_task;
2036 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002037 mutex_init(&new_tsp->smk_rules_lock);
2038 INIT_LIST_HEAD(&new_tsp->smk_rules);
2039
2040
2041 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01002042}
2043
2044/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08002045 * smack_cred_getsecid - get the secid corresponding to a creds structure
2046 * @c: the object creds
2047 * @secid: where to put the result
2048 *
2049 * Sets the secid to contain a u32 version of the smack label.
2050 */
2051static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2052{
2053 struct smack_known *skp;
2054
2055 rcu_read_lock();
2056 skp = smk_of_task(c->security);
2057 *secid = skp->smk_secid;
2058 rcu_read_unlock();
2059}
2060
2061/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002062 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002063 * @new: points to the set of credentials to be modified.
2064 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002065 *
2066 * Set the security data for a kernel service.
2067 */
2068static int smack_kernel_act_as(struct cred *new, u32 secid)
2069{
Casey Schaufler676dac42010-12-02 06:43:39 -08002070 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002071
Casey Schaufler152f91d2016-11-14 09:38:15 -08002072 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002073 return 0;
2074}
2075
2076/**
2077 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002078 * @new: points to the set of credentials to be modified
2079 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002080 *
2081 * Set the file creation context in a set of credentials to the same
2082 * as the objective context of the specified inode
2083 */
2084static int smack_kernel_create_files_as(struct cred *new,
2085 struct inode *inode)
2086{
2087 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002088 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002089
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002090 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002091 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002092 return 0;
2093}
2094
2095/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002096 * smk_curacc_on_task - helper to log task related access
2097 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002098 * @access: the access requested
2099 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002100 *
2101 * Return 0 if access is permitted
2102 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002103static int smk_curacc_on_task(struct task_struct *p, int access,
2104 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002105{
2106 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002107 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002108 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002109
Casey Schaufler531f1d42011-09-19 12:41:42 -07002110 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002111 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002112 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002113 rc = smk_bu_task(p, access, rc);
2114 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002115}
2116
2117/**
Casey Schauflere114e472008-02-04 22:29:50 -08002118 * smack_task_setpgid - Smack check on setting pgid
2119 * @p: the task object
2120 * @pgid: unused
2121 *
2122 * Return 0 if write access is permitted
2123 */
2124static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2125{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002126 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002127}
2128
2129/**
2130 * smack_task_getpgid - Smack access check for getpgid
2131 * @p: the object task
2132 *
2133 * Returns 0 if current can read the object task, error code otherwise
2134 */
2135static int smack_task_getpgid(struct task_struct *p)
2136{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002137 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002138}
2139
2140/**
2141 * smack_task_getsid - Smack access check for getsid
2142 * @p: the object task
2143 *
2144 * Returns 0 if current can read the object task, error code otherwise
2145 */
2146static int smack_task_getsid(struct task_struct *p)
2147{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002148 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002149}
2150
2151/**
2152 * smack_task_getsecid - get the secid of the task
2153 * @p: the object task
2154 * @secid: where to put the result
2155 *
2156 * Sets the secid to contain a u32 version of the smack label.
2157 */
2158static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2159{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002160 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002161
2162 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002163}
2164
2165/**
2166 * smack_task_setnice - Smack check on setting nice
2167 * @p: the task object
2168 * @nice: unused
2169 *
2170 * Return 0 if write access is permitted
2171 */
2172static int smack_task_setnice(struct task_struct *p, int nice)
2173{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002174 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002175}
2176
2177/**
2178 * smack_task_setioprio - Smack check on setting ioprio
2179 * @p: the task object
2180 * @ioprio: unused
2181 *
2182 * Return 0 if write access is permitted
2183 */
2184static int smack_task_setioprio(struct task_struct *p, int ioprio)
2185{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002186 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002187}
2188
2189/**
2190 * smack_task_getioprio - Smack check on reading ioprio
2191 * @p: the task object
2192 *
2193 * Return 0 if read access is permitted
2194 */
2195static int smack_task_getioprio(struct task_struct *p)
2196{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002197 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002198}
2199
2200/**
2201 * smack_task_setscheduler - Smack check on setting scheduler
2202 * @p: the task object
2203 * @policy: unused
2204 * @lp: unused
2205 *
2206 * Return 0 if read access is permitted
2207 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002208static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002209{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002210 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002211}
2212
2213/**
2214 * smack_task_getscheduler - Smack check on reading scheduler
2215 * @p: the task object
2216 *
2217 * Return 0 if read access is permitted
2218 */
2219static int smack_task_getscheduler(struct task_struct *p)
2220{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002221 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002222}
2223
2224/**
2225 * smack_task_movememory - Smack check on moving memory
2226 * @p: the task object
2227 *
2228 * Return 0 if write access is permitted
2229 */
2230static int smack_task_movememory(struct task_struct *p)
2231{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002232 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002233}
2234
2235/**
2236 * smack_task_kill - Smack check on signal delivery
2237 * @p: the task object
2238 * @info: unused
2239 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002240 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002241 *
2242 * Return 0 if write access is permitted
2243 *
Casey Schauflere114e472008-02-04 22:29:50 -08002244 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002245static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002246 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002247{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002248 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002249 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002250 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002251 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002252
Rafal Krypa18d872f2016-04-04 11:14:53 +02002253 if (!sig)
2254 return 0; /* null signal; existence test */
2255
Etienne Bassetecfcc532009-04-08 20:40:06 +02002256 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2257 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002258 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002259 * Sending a signal requires that the sender
2260 * can write the receiver.
2261 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002262 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002263 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2264 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002265 return rc;
2266 }
Casey Schauflere114e472008-02-04 22:29:50 -08002267 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002268 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002269 * specific behavior. This is not clean. For one thing
2270 * we can't take privilege into account.
2271 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002272 skp = smk_of_task(cred->security);
Casey Schauflerc60b9062016-08-30 10:31:39 -07002273 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2274 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002275 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002276}
2277
2278/**
Casey Schauflere114e472008-02-04 22:29:50 -08002279 * smack_task_to_inode - copy task smack into the inode blob
2280 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002281 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002282 *
2283 * Sets the smack pointer in the inode security blob
2284 */
2285static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2286{
2287 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002288 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002289
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002290 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002291 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002292}
2293
2294/*
2295 * Socket hooks.
2296 */
2297
2298/**
2299 * smack_sk_alloc_security - Allocate a socket blob
2300 * @sk: the socket
2301 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002302 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002303 *
2304 * Assign Smack pointers to current
2305 *
2306 * Returns 0 on success, -ENOMEM is there's no memory
2307 */
2308static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2309{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002310 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002311 struct socket_smack *ssp;
2312
2313 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2314 if (ssp == NULL)
2315 return -ENOMEM;
2316
jooseong lee08382c92016-11-03 11:54:39 +01002317 /*
2318 * Sockets created by kernel threads receive web label.
2319 */
2320 if (unlikely(current->flags & PF_KTHREAD)) {
2321 ssp->smk_in = &smack_known_web;
2322 ssp->smk_out = &smack_known_web;
2323 } else {
2324 ssp->smk_in = skp;
2325 ssp->smk_out = skp;
2326 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002327 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002328
2329 sk->sk_security = ssp;
2330
2331 return 0;
2332}
2333
2334/**
2335 * smack_sk_free_security - Free a socket blob
2336 * @sk: the socket
2337 *
2338 * Clears the blob pointer
2339 */
2340static void smack_sk_free_security(struct sock *sk)
2341{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302342#ifdef SMACK_IPV6_PORT_LABELING
2343 struct smk_port_label *spp;
2344
2345 if (sk->sk_family == PF_INET6) {
2346 rcu_read_lock();
2347 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2348 if (spp->smk_sock != sk)
2349 continue;
2350 spp->smk_can_reuse = 1;
2351 break;
2352 }
2353 rcu_read_unlock();
2354 }
2355#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002356 kfree(sk->sk_security);
2357}
2358
2359/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002360* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002361* @sip: the object end
2362*
2363* looks for host based access restrictions
2364*
2365* This version will only be appropriate for really small sets of single label
2366* hosts. The caller is responsible for ensuring that the RCU read lock is
2367* taken before calling this function.
2368*
2369* Returns the label of the far end or NULL if it's not special.
2370*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002371static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002372{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002373 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002374 struct in_addr *siap = &sip->sin_addr;
2375
2376 if (siap->s_addr == 0)
2377 return NULL;
2378
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002379 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2380 /*
2381 * we break after finding the first match because
2382 * the list is sorted from longest to shortest mask
2383 * so we have found the most specific match
2384 */
2385 if (snp->smk_host.s_addr ==
2386 (siap->s_addr & snp->smk_mask.s_addr))
2387 return snp->smk_label;
2388
2389 return NULL;
2390}
2391
2392#if IS_ENABLED(CONFIG_IPV6)
2393/*
2394 * smk_ipv6_localhost - Check for local ipv6 host address
2395 * @sip: the address
2396 *
2397 * Returns boolean true if this is the localhost address
2398 */
2399static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2400{
2401 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2402 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2403
2404 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2405 ntohs(be16p[7]) == 1)
2406 return true;
2407 return false;
2408}
2409
2410/**
2411* smack_ipv6host_label - check host based restrictions
2412* @sip: the object end
2413*
2414* looks for host based access restrictions
2415*
2416* This version will only be appropriate for really small sets of single label
2417* hosts. The caller is responsible for ensuring that the RCU read lock is
2418* taken before calling this function.
2419*
2420* Returns the label of the far end or NULL if it's not special.
2421*/
2422static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2423{
2424 struct smk_net6addr *snp;
2425 struct in6_addr *sap = &sip->sin6_addr;
2426 int i;
2427 int found = 0;
2428
2429 /*
2430 * It's local. Don't look for a host label.
2431 */
2432 if (smk_ipv6_localhost(sip))
2433 return NULL;
2434
2435 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002436 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002437 * If the label is NULL the entry has
2438 * been renounced. Ignore it.
2439 */
2440 if (snp->smk_label == NULL)
2441 continue;
2442 /*
Paul Moore07feee82009-03-27 17:10:54 -04002443 * we break after finding the first match because
2444 * the list is sorted from longest to shortest mask
2445 * so we have found the most specific match
2446 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002447 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002448 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2449 snp->smk_host.s6_addr16[i]) {
2450 found = 0;
2451 break;
2452 }
Etienne Basset43031542009-03-27 17:11:01 -04002453 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002454 if (found)
2455 return snp->smk_label;
2456 }
Paul Moore07feee82009-03-27 17:10:54 -04002457
2458 return NULL;
2459}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002460#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002461
2462/**
Casey Schauflere114e472008-02-04 22:29:50 -08002463 * smack_netlabel - Set the secattr on a socket
2464 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002465 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002466 *
2467 * Convert the outbound smack value (smk_out) to a
2468 * secattr and attach it to the socket.
2469 *
2470 * Returns 0 on success or an error code
2471 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002472static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002473{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002474 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002475 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002476 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002477
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002478 /*
2479 * Usually the netlabel code will handle changing the
2480 * packet labeling based on the label.
2481 * The case of a single label host is different, because
2482 * a single label host should never get a labeled packet
2483 * even though the label is usually associated with a packet
2484 * label.
2485 */
2486 local_bh_disable();
2487 bh_lock_sock_nested(sk);
2488
2489 if (ssp->smk_out == smack_net_ambient ||
2490 labeled == SMACK_UNLABELED_SOCKET)
2491 netlbl_sock_delattr(sk);
2492 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002493 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002494 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002495 }
2496
2497 bh_unlock_sock(sk);
2498 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002499
Casey Schauflere114e472008-02-04 22:29:50 -08002500 return rc;
2501}
2502
2503/**
Paul Moore07feee82009-03-27 17:10:54 -04002504 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2505 * @sk: the socket
2506 * @sap: the destination address
2507 *
2508 * Set the correct secattr for the given socket based on the destination
2509 * address and perform any outbound access checks needed.
2510 *
2511 * Returns 0 on success or an error code.
2512 *
2513 */
2514static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2515{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002516 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002517 int rc;
2518 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002519 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002520 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002521 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002522
2523 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002524 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002525 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002526#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002527 struct lsm_network_audit net;
2528
Eric Paris48c62af2012-04-02 13:15:44 -04002529 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2530 ad.a.u.net->family = sap->sin_family;
2531 ad.a.u.net->dport = sap->sin_port;
2532 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002533#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002534 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002535 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002536 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2537 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002538 } else {
2539 sk_lbl = SMACK_CIPSO_SOCKET;
2540 rc = 0;
2541 }
2542 rcu_read_unlock();
2543 if (rc != 0)
2544 return rc;
2545
2546 return smack_netlabel(sk, sk_lbl);
2547}
2548
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002549#if IS_ENABLED(CONFIG_IPV6)
2550/**
2551 * smk_ipv6_check - check Smack access
2552 * @subject: subject Smack label
2553 * @object: object Smack label
2554 * @address: address
2555 * @act: the action being taken
2556 *
2557 * Check an IPv6 access
2558 */
2559static int smk_ipv6_check(struct smack_known *subject,
2560 struct smack_known *object,
2561 struct sockaddr_in6 *address, int act)
2562{
2563#ifdef CONFIG_AUDIT
2564 struct lsm_network_audit net;
2565#endif
2566 struct smk_audit_info ad;
2567 int rc;
2568
2569#ifdef CONFIG_AUDIT
2570 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2571 ad.a.u.net->family = PF_INET6;
2572 ad.a.u.net->dport = ntohs(address->sin6_port);
2573 if (act == SMK_RECEIVING)
2574 ad.a.u.net->v6info.saddr = address->sin6_addr;
2575 else
2576 ad.a.u.net->v6info.daddr = address->sin6_addr;
2577#endif
2578 rc = smk_access(subject, object, MAY_WRITE, &ad);
2579 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2580 return rc;
2581}
2582#endif /* CONFIG_IPV6 */
2583
2584#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002585/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002586 * smk_ipv6_port_label - Smack port access table management
2587 * @sock: socket
2588 * @address: address
2589 *
2590 * Create or update the port list entry
2591 */
2592static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2593{
2594 struct sock *sk = sock->sk;
2595 struct sockaddr_in6 *addr6;
2596 struct socket_smack *ssp = sock->sk->sk_security;
2597 struct smk_port_label *spp;
2598 unsigned short port = 0;
2599
2600 if (address == NULL) {
2601 /*
2602 * This operation is changing the Smack information
2603 * on the bound socket. Take the changes to the port
2604 * as well.
2605 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302606 rcu_read_lock();
2607 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002608 if (sk != spp->smk_sock)
2609 continue;
2610 spp->smk_in = ssp->smk_in;
2611 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302612 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002613 return;
2614 }
2615 /*
2616 * A NULL address is only used for updating existing
2617 * bound entries. If there isn't one, it's OK.
2618 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302619 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002620 return;
2621 }
2622
2623 addr6 = (struct sockaddr_in6 *)address;
2624 port = ntohs(addr6->sin6_port);
2625 /*
2626 * This is a special case that is safely ignored.
2627 */
2628 if (port == 0)
2629 return;
2630
2631 /*
2632 * Look for an existing port list entry.
2633 * This is an indication that a port is getting reused.
2634 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302635 rcu_read_lock();
2636 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302637 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002638 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302639 if (spp->smk_can_reuse != 1) {
2640 rcu_read_unlock();
2641 return;
2642 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002643 spp->smk_port = port;
2644 spp->smk_sock = sk;
2645 spp->smk_in = ssp->smk_in;
2646 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302647 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302648 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002649 return;
2650 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302651 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002652 /*
2653 * A new port entry is required.
2654 */
2655 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2656 if (spp == NULL)
2657 return;
2658
2659 spp->smk_port = port;
2660 spp->smk_sock = sk;
2661 spp->smk_in = ssp->smk_in;
2662 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302663 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302664 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002665
Vishal Goel3c7ce342016-11-23 10:31:08 +05302666 mutex_lock(&smack_ipv6_lock);
2667 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2668 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002669 return;
2670}
2671
2672/**
2673 * smk_ipv6_port_check - check Smack port access
2674 * @sock: socket
2675 * @address: address
2676 *
2677 * Create or update the port list entry
2678 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002679static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002680 int act)
2681{
Casey Schauflerc6739442013-05-22 18:42:56 -07002682 struct smk_port_label *spp;
2683 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002684 struct smack_known *skp = NULL;
2685 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002686 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002687
2688 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002689 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002690 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002691 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002692 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002693 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002694 }
2695
2696 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002697 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002698 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002699 if (skp != NULL && object != NULL)
2700 return smk_ipv6_check(skp, object, address, act);
2701 if (skp == NULL)
2702 skp = smack_net_ambient;
2703 if (object == NULL)
2704 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002705
2706 /*
2707 * It's remote, so port lookup does no good.
2708 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002709 if (!smk_ipv6_localhost(address))
2710 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002711
2712 /*
2713 * It's local so the send check has to have passed.
2714 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002715 if (act == SMK_RECEIVING)
2716 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002717
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002718 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302719 rcu_read_lock();
2720 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302721 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002722 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002723 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002724 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002725 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002726 break;
2727 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302728 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002729
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002730 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002731}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002732#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002733
2734/**
Casey Schauflere114e472008-02-04 22:29:50 -08002735 * smack_inode_setsecurity - set smack xattrs
2736 * @inode: the object
2737 * @name: attribute name
2738 * @value: attribute value
2739 * @size: size of the attribute
2740 * @flags: unused
2741 *
2742 * Sets the named attribute in the appropriate blob
2743 *
2744 * Returns 0 on success, or an error code
2745 */
2746static int smack_inode_setsecurity(struct inode *inode, const char *name,
2747 const void *value, size_t size, int flags)
2748{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002749 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002750 struct inode_smack *nsp = inode->i_security;
2751 struct socket_smack *ssp;
2752 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002753 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002754
Casey Schauflerf7112e62012-05-06 15:22:02 -07002755 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302756 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002757
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002758 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002759 if (IS_ERR(skp))
2760 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002761
2762 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002763 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002764 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002765 return 0;
2766 }
2767 /*
2768 * The rest of the Smack xattrs are only on sockets.
2769 */
2770 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2771 return -EOPNOTSUPP;
2772
2773 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002774 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002775 return -EOPNOTSUPP;
2776
2777 ssp = sock->sk->sk_security;
2778
2779 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002780 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002781 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002782 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002783 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002784 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2785 if (rc != 0)
2786 printk(KERN_WARNING
2787 "Smack: \"%s\" netlbl error %d.\n",
2788 __func__, -rc);
2789 }
Casey Schauflere114e472008-02-04 22:29:50 -08002790 } else
2791 return -EOPNOTSUPP;
2792
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002793#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002794 if (sock->sk->sk_family == PF_INET6)
2795 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002796#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002797
Casey Schauflere114e472008-02-04 22:29:50 -08002798 return 0;
2799}
2800
2801/**
2802 * smack_socket_post_create - finish socket setup
2803 * @sock: the socket
2804 * @family: protocol family
2805 * @type: unused
2806 * @protocol: unused
2807 * @kern: unused
2808 *
2809 * Sets the netlabel information on the socket
2810 *
2811 * Returns 0 on success, and error code otherwise
2812 */
2813static int smack_socket_post_create(struct socket *sock, int family,
2814 int type, int protocol, int kern)
2815{
Marcin Lis74123012015-01-22 15:40:33 +01002816 struct socket_smack *ssp;
2817
2818 if (sock->sk == NULL)
2819 return 0;
2820
2821 /*
2822 * Sockets created by kernel threads receive web label.
2823 */
2824 if (unlikely(current->flags & PF_KTHREAD)) {
2825 ssp = sock->sk->sk_security;
2826 ssp->smk_in = &smack_known_web;
2827 ssp->smk_out = &smack_known_web;
2828 }
2829
2830 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002831 return 0;
2832 /*
2833 * Set the outbound netlbl.
2834 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002835 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2836}
2837
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002838/**
2839 * smack_socket_socketpair - create socket pair
2840 * @socka: one socket
2841 * @sockb: another socket
2842 *
2843 * Cross reference the peer labels for SO_PEERSEC
2844 *
2845 * Returns 0 on success, and error code otherwise
2846 */
2847static int smack_socket_socketpair(struct socket *socka,
2848 struct socket *sockb)
2849{
2850 struct socket_smack *asp = socka->sk->sk_security;
2851 struct socket_smack *bsp = sockb->sk->sk_security;
2852
2853 asp->smk_packet = bsp->smk_out;
2854 bsp->smk_packet = asp->smk_out;
2855
2856 return 0;
2857}
2858
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002859#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002860/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002861 * smack_socket_bind - record port binding information.
2862 * @sock: the socket
2863 * @address: the port address
2864 * @addrlen: size of the address
2865 *
2866 * Records the label bound to a port.
2867 *
2868 * Returns 0
2869 */
2870static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2871 int addrlen)
2872{
2873 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2874 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002875 return 0;
2876}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002877#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002878
2879/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002880 * smack_socket_connect - connect access check
2881 * @sock: the socket
2882 * @sap: the other end
2883 * @addrlen: size of sap
2884 *
2885 * Verifies that a connection may be possible
2886 *
2887 * Returns 0 on success, and error code otherwise
2888 */
2889static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2890 int addrlen)
2891{
Casey Schauflerc6739442013-05-22 18:42:56 -07002892 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002893#if IS_ENABLED(CONFIG_IPV6)
2894 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2895#endif
2896#ifdef SMACK_IPV6_SECMARK_LABELING
2897 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002898 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002899#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002900
Casey Schauflerc6739442013-05-22 18:42:56 -07002901 if (sock->sk == NULL)
2902 return 0;
2903
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002904#ifdef SMACK_IPV6_SECMARK_LABELING
2905 ssp = sock->sk->sk_security;
2906#endif
2907
Casey Schauflerc6739442013-05-22 18:42:56 -07002908 switch (sock->sk->sk_family) {
2909 case PF_INET:
2910 if (addrlen < sizeof(struct sockaddr_in))
2911 return -EINVAL;
2912 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2913 break;
2914 case PF_INET6:
2915 if (addrlen < sizeof(struct sockaddr_in6))
2916 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002917#ifdef SMACK_IPV6_SECMARK_LABELING
2918 rsp = smack_ipv6host_label(sip);
2919 if (rsp != NULL)
2920 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002921 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002922#endif
2923#ifdef SMACK_IPV6_PORT_LABELING
2924 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2925#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002926 break;
2927 }
2928 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002929}
2930
2931/**
2932 * smack_flags_to_may - convert S_ to MAY_ values
2933 * @flags: the S_ value
2934 *
2935 * Returns the equivalent MAY_ value
2936 */
2937static int smack_flags_to_may(int flags)
2938{
2939 int may = 0;
2940
2941 if (flags & S_IRUGO)
2942 may |= MAY_READ;
2943 if (flags & S_IWUGO)
2944 may |= MAY_WRITE;
2945 if (flags & S_IXUGO)
2946 may |= MAY_EXEC;
2947
2948 return may;
2949}
2950
2951/**
2952 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2953 * @msg: the object
2954 *
2955 * Returns 0
2956 */
2957static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2958{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002959 struct smack_known *skp = smk_of_current();
2960
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002961 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002962 return 0;
2963}
2964
2965/**
2966 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2967 * @msg: the object
2968 *
2969 * Clears the blob pointer
2970 */
2971static void smack_msg_msg_free_security(struct msg_msg *msg)
2972{
2973 msg->security = NULL;
2974}
2975
2976/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002977 * smack_of_ipc - the smack pointer for the ipc
2978 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002979 *
2980 * Returns a pointer to the smack value
2981 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002982static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002983{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002984 return (struct smack_known *)isp->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002985}
2986
2987/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002988 * smack_ipc_alloc_security - Set the security blob for ipc
2989 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002990 *
2991 * Returns 0
2992 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002993static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002994{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002995 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002996
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002997 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002998 return 0;
2999}
3000
3001/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003002 * smack_ipc_free_security - Clear the security blob for ipc
3003 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003004 *
3005 * Clears the blob pointer
3006 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003007static void smack_ipc_free_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08003008{
Casey Schauflere114e472008-02-04 22:29:50 -08003009 isp->security = NULL;
3010}
3011
3012/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003013 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003014 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003015 * @access : access requested
3016 *
3017 * Returns 0 if current has the requested access, error code otherwise
3018 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003019static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003020{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003021 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003022 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003023 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003024
3025#ifdef CONFIG_AUDIT
3026 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003027 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003028#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003029 rc = smk_curacc(ssp, access, &ad);
3030 rc = smk_bu_current("shm", ssp, access, rc);
3031 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032}
3033
3034/**
Casey Schauflere114e472008-02-04 22:29:50 -08003035 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003036 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003037 * @shmflg: access requested
3038 *
3039 * Returns 0 if current has the requested access, error code otherwise
3040 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003041static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003042{
Casey Schauflere114e472008-02-04 22:29:50 -08003043 int may;
3044
3045 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003046 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003047}
3048
3049/**
3050 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003051 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003052 * @cmd: what it wants to do
3053 *
3054 * Returns 0 if current has the requested access, error code otherwise
3055 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003056static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003057{
Casey Schauflere114e472008-02-04 22:29:50 -08003058 int may;
3059
3060 switch (cmd) {
3061 case IPC_STAT:
3062 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07003063 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003064 may = MAY_READ;
3065 break;
3066 case IPC_SET:
3067 case SHM_LOCK:
3068 case SHM_UNLOCK:
3069 case IPC_RMID:
3070 may = MAY_READWRITE;
3071 break;
3072 case IPC_INFO:
3073 case SHM_INFO:
3074 /*
3075 * System level information.
3076 */
3077 return 0;
3078 default:
3079 return -EINVAL;
3080 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003081 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003082}
3083
3084/**
3085 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003086 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003087 * @shmaddr: unused
3088 * @shmflg: access requested
3089 *
3090 * Returns 0 if current has the requested access, error code otherwise
3091 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003092static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003093 int shmflg)
3094{
Casey Schauflere114e472008-02-04 22:29:50 -08003095 int may;
3096
3097 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003098 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003099}
3100
3101/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003102 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003103 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003104 * @access : 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 smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003109{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003110 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003111 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003112 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003113
3114#ifdef CONFIG_AUDIT
3115 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003116 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003117#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003118 rc = smk_curacc(ssp, access, &ad);
3119 rc = smk_bu_current("sem", ssp, access, rc);
3120 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003121}
3122
3123/**
Casey Schauflere114e472008-02-04 22:29:50 -08003124 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003125 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003126 * @semflg: access requested
3127 *
3128 * Returns 0 if current has the requested access, error code otherwise
3129 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003130static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003131{
Casey Schauflere114e472008-02-04 22:29:50 -08003132 int may;
3133
3134 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003135 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003136}
3137
3138/**
3139 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003140 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003141 * @cmd: what it wants to do
3142 *
3143 * Returns 0 if current has the requested access, error code otherwise
3144 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003145static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003146{
Casey Schauflere114e472008-02-04 22:29:50 -08003147 int may;
3148
3149 switch (cmd) {
3150 case GETPID:
3151 case GETNCNT:
3152 case GETZCNT:
3153 case GETVAL:
3154 case GETALL:
3155 case IPC_STAT:
3156 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003157 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003158 may = MAY_READ;
3159 break;
3160 case SETVAL:
3161 case SETALL:
3162 case IPC_RMID:
3163 case IPC_SET:
3164 may = MAY_READWRITE;
3165 break;
3166 case IPC_INFO:
3167 case SEM_INFO:
3168 /*
3169 * System level information
3170 */
3171 return 0;
3172 default:
3173 return -EINVAL;
3174 }
3175
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003176 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003177}
3178
3179/**
3180 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003181 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003182 * @sops: unused
3183 * @nsops: unused
3184 * @alter: unused
3185 *
3186 * Treated as read and write in all cases.
3187 *
3188 * Returns 0 if access is allowed, error code otherwise
3189 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003190static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003191 unsigned nsops, int alter)
3192{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003193 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003194}
3195
3196/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003197 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003198 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003199 * @access : access requested
3200 *
3201 * return 0 if current has access, error otherwise
3202 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003203static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003204{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003205 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003206 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003207 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003208
3209#ifdef CONFIG_AUDIT
3210 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003211 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003212#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003213 rc = smk_curacc(msp, access, &ad);
3214 rc = smk_bu_current("msq", msp, access, rc);
3215 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003216}
3217
3218/**
Casey Schauflere114e472008-02-04 22:29:50 -08003219 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003220 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003221 * @msqflg: access requested
3222 *
3223 * Returns 0 if current has the requested access, error code otherwise
3224 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003225static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003226{
Casey Schauflere114e472008-02-04 22:29:50 -08003227 int may;
3228
3229 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003230 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003231}
3232
3233/**
3234 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003235 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003236 * @cmd: what it wants to do
3237 *
3238 * Returns 0 if current has the requested access, error code otherwise
3239 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003240static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003241{
Casey Schauflere114e472008-02-04 22:29:50 -08003242 int may;
3243
3244 switch (cmd) {
3245 case IPC_STAT:
3246 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003247 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003248 may = MAY_READ;
3249 break;
3250 case IPC_SET:
3251 case IPC_RMID:
3252 may = MAY_READWRITE;
3253 break;
3254 case IPC_INFO:
3255 case MSG_INFO:
3256 /*
3257 * System level information
3258 */
3259 return 0;
3260 default:
3261 return -EINVAL;
3262 }
3263
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003264 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003265}
3266
3267/**
3268 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003269 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003270 * @msg: unused
3271 * @msqflg: access requested
3272 *
3273 * Returns 0 if current has the requested access, error code otherwise
3274 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003275static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003276 int msqflg)
3277{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003278 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003279
Etienne Bassetecfcc532009-04-08 20:40:06 +02003280 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003281 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003282}
3283
3284/**
3285 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003286 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003287 * @msg: unused
3288 * @target: unused
3289 * @type: unused
3290 * @mode: unused
3291 *
3292 * Returns 0 if current has read and write access, error code otherwise
3293 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003294static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003295 struct task_struct *target, long type, int mode)
3296{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003297 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003298}
3299
3300/**
3301 * smack_ipc_permission - Smack access for ipc_permission()
3302 * @ipp: the object permissions
3303 * @flag: access requested
3304 *
3305 * Returns 0 if current has read and write access, error code otherwise
3306 */
3307static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3308{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003309 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003310 int may = smack_flags_to_may(flag);
3311 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003312 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003313
Etienne Bassetecfcc532009-04-08 20:40:06 +02003314#ifdef CONFIG_AUDIT
3315 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3316 ad.a.u.ipc_id = ipp->id;
3317#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003318 rc = smk_curacc(iskp, may, &ad);
3319 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003320 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003321}
3322
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003323/**
3324 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003325 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003326 * @secid: where result will be saved
3327 */
3328static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3329{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003330 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003331
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003332 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003333}
3334
Casey Schauflere114e472008-02-04 22:29:50 -08003335/**
3336 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003337 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003338 * @inode: the object
3339 *
3340 * Set the inode's security blob if it hasn't been done already.
3341 */
3342static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3343{
3344 struct super_block *sbp;
3345 struct superblock_smack *sbsp;
3346 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003347 struct smack_known *skp;
3348 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003349 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003350 char trattr[TRANS_TRUE_SIZE];
3351 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003352 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003353 struct dentry *dp;
3354
3355 if (inode == NULL)
3356 return;
3357
3358 isp = inode->i_security;
3359
3360 mutex_lock(&isp->smk_lock);
3361 /*
3362 * If the inode is already instantiated
3363 * take the quick way out
3364 */
3365 if (isp->smk_flags & SMK_INODE_INSTANT)
3366 goto unlockandout;
3367
3368 sbp = inode->i_sb;
3369 sbsp = sbp->s_security;
3370 /*
3371 * We're going to use the superblock default label
3372 * if there's no label on the file.
3373 */
3374 final = sbsp->smk_default;
3375
3376 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003377 * If this is the root inode the superblock
3378 * may be in the process of initialization.
3379 * If that is the case use the root value out
3380 * of the superblock.
3381 */
3382 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003383 switch (sbp->s_magic) {
3384 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003385 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003386 /*
3387 * The cgroup filesystem is never mounted,
3388 * so there's no opportunity to set the mount
3389 * options.
3390 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003391 sbsp->smk_root = &smack_known_star;
3392 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003393 isp->smk_inode = sbsp->smk_root;
3394 break;
3395 case TMPFS_MAGIC:
3396 /*
3397 * What about shmem/tmpfs anonymous files with dentry
3398 * obtained from d_alloc_pseudo()?
3399 */
3400 isp->smk_inode = smk_of_current();
3401 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003402 case PIPEFS_MAGIC:
3403 isp->smk_inode = smk_of_current();
3404 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003405 case SOCKFS_MAGIC:
3406 /*
3407 * Socket access is controlled by the socket
3408 * structures associated with the task involved.
3409 */
3410 isp->smk_inode = &smack_known_star;
3411 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003412 default:
3413 isp->smk_inode = sbsp->smk_root;
3414 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003415 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003416 isp->smk_flags |= SMK_INODE_INSTANT;
3417 goto unlockandout;
3418 }
3419
3420 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003421 * This is pretty hackish.
3422 * Casey says that we shouldn't have to do
3423 * file system specific code, but it does help
3424 * with keeping it simple.
3425 */
3426 switch (sbp->s_magic) {
3427 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003428 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003429 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003430 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003431 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003432 * that the smack file system doesn't do
3433 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003434 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003435 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003436 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003437 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003438 break;
3439 case DEVPTS_SUPER_MAGIC:
3440 /*
3441 * devpts seems content with the label of the task.
3442 * Programs that change smack have to treat the
3443 * pty with respect.
3444 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003445 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003446 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003447 case PROC_SUPER_MAGIC:
3448 /*
3449 * Casey says procfs appears not to care.
3450 * The superblock default suffices.
3451 */
3452 break;
3453 case TMPFS_MAGIC:
3454 /*
3455 * Device labels should come from the filesystem,
3456 * but watch out, because they're volitile,
3457 * getting recreated on every reboot.
3458 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003459 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003460 /*
Gustavo A. R. Silvab1fed3e2018-08-01 17:38:54 -05003461 * Fall through.
Casey Schauflere114e472008-02-04 22:29:50 -08003462 *
3463 * If a smack value has been set we want to use it,
3464 * but since tmpfs isn't giving us the opportunity
3465 * to set mount options simulate setting the
3466 * superblock default.
3467 */
3468 default:
3469 /*
3470 * This isn't an understood special case.
3471 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003472 */
3473
3474 /*
3475 * UNIX domain sockets use lower level socket data.
3476 */
3477 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003478 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003479 break;
3480 }
3481 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003482 * No xattr support means, alas, no SMACK label.
3483 * Use the aforeapplied default.
3484 * It would be curious if the label of the task
3485 * does not match that assigned.
3486 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003487 if (!(inode->i_opflags & IOP_XATTR))
3488 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003489 /*
3490 * Get the dentry for xattr.
3491 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003492 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003493 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003494 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003495 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003496
3497 /*
3498 * Transmuting directory
3499 */
3500 if (S_ISDIR(inode->i_mode)) {
3501 /*
3502 * If this is a new directory and the label was
3503 * transmuted when the inode was initialized
3504 * set the transmute attribute on the directory
3505 * and mark the inode.
3506 *
3507 * If there is a transmute attribute on the
3508 * directory mark the inode.
3509 */
3510 if (isp->smk_flags & SMK_INODE_CHANGED) {
3511 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003512 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003513 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003514 TRANS_TRUE, TRANS_TRUE_SIZE,
3515 0);
3516 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003517 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003518 XATTR_NAME_SMACKTRANSMUTE, trattr,
3519 TRANS_TRUE_SIZE);
3520 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3521 TRANS_TRUE_SIZE) != 0)
3522 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003523 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003524 if (rc >= 0)
3525 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003526 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003527 /*
3528 * Don't let the exec or mmap label be "*" or "@".
3529 */
3530 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3531 if (IS_ERR(skp) || skp == &smack_known_star ||
3532 skp == &smack_known_web)
3533 skp = NULL;
3534 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003535
Casey Schaufler19760ad2013-12-16 16:27:26 -08003536 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003537 if (IS_ERR(skp) || skp == &smack_known_star ||
3538 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003539 skp = NULL;
3540 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003541
Casey Schauflere114e472008-02-04 22:29:50 -08003542 dput(dp);
3543 break;
3544 }
3545
3546 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003547 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003548 else
3549 isp->smk_inode = final;
3550
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003551 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003552
3553unlockandout:
3554 mutex_unlock(&isp->smk_lock);
3555 return;
3556}
3557
3558/**
3559 * smack_getprocattr - Smack process attribute access
3560 * @p: the object task
3561 * @name: the name of the attribute in /proc/.../attr
3562 * @value: where to put the result
3563 *
3564 * Places a copy of the task Smack into value
3565 *
3566 * Returns the length of the smack label or an error code
3567 */
3568static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3569{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003570 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003571 char *cp;
3572 int slen;
3573
3574 if (strcmp(name, "current") != 0)
3575 return -EINVAL;
3576
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003577 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003578 if (cp == NULL)
3579 return -ENOMEM;
3580
3581 slen = strlen(cp);
3582 *value = cp;
3583 return slen;
3584}
3585
3586/**
3587 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003588 * @name: the name of the attribute in /proc/.../attr
3589 * @value: the value to set
3590 * @size: the size of the value
3591 *
3592 * Sets the Smack value of the task. Only setting self
3593 * is permitted and only with privilege
3594 *
3595 * Returns the length of the smack label or an error code
3596 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003597static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003598{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003599 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003600 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003601 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003602 struct smack_known_list_elem *sklep;
3603 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003604
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003605 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003606 return -EPERM;
3607
Casey Schauflerf7112e62012-05-06 15:22:02 -07003608 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003609 return -EINVAL;
3610
3611 if (strcmp(name, "current") != 0)
3612 return -EINVAL;
3613
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003614 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003615 if (IS_ERR(skp))
3616 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003617
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003618 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303619 * No process is ever allowed the web ("@") label
3620 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003621 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303622 if (skp == &smack_known_web || skp == &smack_known_star)
3623 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003624
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003625 if (!smack_privileged(CAP_MAC_ADMIN)) {
3626 rc = -EPERM;
3627 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3628 if (sklep->smk_label == skp) {
3629 rc = 0;
3630 break;
3631 }
3632 if (rc)
3633 return rc;
3634 }
3635
David Howellsd84f4f92008-11-14 10:39:23 +11003636 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003637 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003638 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003639
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003640 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003641 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003642 /*
3643 * process can change its label only once
3644 */
3645 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003646
David Howellsd84f4f92008-11-14 10:39:23 +11003647 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003648 return size;
3649}
3650
3651/**
3652 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003653 * @sock: one sock
3654 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003655 * @newsk: unused
3656 *
3657 * Return 0 if a subject with the smack of sock could access
3658 * an object with the smack of other, otherwise an error code
3659 */
David S. Miller3610cda2011-01-05 15:38:53 -08003660static int smack_unix_stream_connect(struct sock *sock,
3661 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003662{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003663 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003664 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003665 struct socket_smack *ssp = sock->sk_security;
3666 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003667 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003668 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003669 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003670#ifdef CONFIG_AUDIT
3671 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003672#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003673
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003674 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3675 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003676 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003677#ifdef CONFIG_AUDIT
3678 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3679 smk_ad_setfield_u_net_sk(&ad, other);
3680#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003681 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3682 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003683 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003684 okp = osp->smk_out;
3685 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003686 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003687 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003688 MAY_WRITE, rc);
3689 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003690 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003691
Casey Schaufler975d5e52011-09-26 14:43:39 -07003692 /*
3693 * Cross reference the peer labels for SO_PEERSEC.
3694 */
3695 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003696 nsp->smk_packet = ssp->smk_out;
3697 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003698 }
3699
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003700 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003701}
3702
3703/**
3704 * smack_unix_may_send - Smack access on UDS
3705 * @sock: one socket
3706 * @other: the other socket
3707 *
3708 * Return 0 if a subject with the smack of sock could access
3709 * an object with the smack of other, otherwise an error code
3710 */
3711static int smack_unix_may_send(struct socket *sock, struct socket *other)
3712{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003713 struct socket_smack *ssp = sock->sk->sk_security;
3714 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003715 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003716 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003717
Kees Cook923e9a12012-04-10 13:26:44 -07003718#ifdef CONFIG_AUDIT
3719 struct lsm_network_audit net;
3720
Eric Paris48c62af2012-04-02 13:15:44 -04003721 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003722 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003723#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003724
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003725 if (smack_privileged(CAP_MAC_OVERRIDE))
3726 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003727
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003728 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3729 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003730 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003731}
3732
3733/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003734 * smack_socket_sendmsg - Smack check based on destination host
3735 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003736 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003737 * @size: the size of the message
3738 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003739 * Return 0 if the current subject can write to the destination host.
3740 * For IPv4 this is only a question if the destination is a single label host.
3741 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003742 */
3743static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3744 int size)
3745{
3746 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003747#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003748 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003749#endif
3750#ifdef SMACK_IPV6_SECMARK_LABELING
3751 struct socket_smack *ssp = sock->sk->sk_security;
3752 struct smack_known *rsp;
3753#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003754 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003755
3756 /*
3757 * Perfectly reasonable for this to be NULL
3758 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003759 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003760 return 0;
3761
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003762 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003763 case AF_INET:
3764 rc = smack_netlabel_send(sock->sk, sip);
3765 break;
3766 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003767#ifdef SMACK_IPV6_SECMARK_LABELING
3768 rsp = smack_ipv6host_label(sap);
3769 if (rsp != NULL)
3770 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3771 SMK_CONNECTING);
3772#endif
3773#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003774 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003775#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003776 break;
3777 }
3778 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003779}
3780
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003781/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003782 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003783 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003784 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003785 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003786 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003787 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003788static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3789 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003790{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003791 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003792 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003793 int acat;
3794 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003795
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003796 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003797 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003798 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003799 * If there are flags but no level netlabel isn't
3800 * behaving the way we expect it to.
3801 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003802 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003803 * Without guidance regarding the smack value
3804 * for the packet fall back on the network
3805 * ambient value.
3806 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003807 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303808 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003809 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003810 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003811 /*
3812 * Compare the catsets. Use the netlbl APIs.
3813 */
3814 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3815 if ((skp->smk_netlabel.flags &
3816 NETLBL_SECATTR_MLS_CAT) == 0)
3817 found = 1;
3818 break;
3819 }
3820 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003821 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3822 acat + 1);
3823 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003824 skp->smk_netlabel.attr.mls.cat,
3825 kcat + 1);
3826 if (acat < 0 || kcat < 0)
3827 break;
3828 }
3829 if (acat == kcat) {
3830 found = 1;
3831 break;
3832 }
Casey Schauflere114e472008-02-04 22:29:50 -08003833 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003834 rcu_read_unlock();
3835
3836 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003837 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003838
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003839 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003840 return &smack_known_web;
3841 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003842 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003843 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003844 /*
3845 * Looks like a fallback, which gives us a secid.
3846 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003847 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003848 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003849 * Without guidance regarding the smack value
3850 * for the packet fall back on the network
3851 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003852 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003853 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003854}
3855
Casey Schaufler69f287a2014-12-12 17:08:40 -08003856#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003857static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003858{
Casey Schauflerc6739442013-05-22 18:42:56 -07003859 u8 nexthdr;
3860 int offset;
3861 int proto = -EINVAL;
3862 struct ipv6hdr _ipv6h;
3863 struct ipv6hdr *ip6;
3864 __be16 frag_off;
3865 struct tcphdr _tcph, *th;
3866 struct udphdr _udph, *uh;
3867 struct dccp_hdr _dccph, *dh;
3868
3869 sip->sin6_port = 0;
3870
3871 offset = skb_network_offset(skb);
3872 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3873 if (ip6 == NULL)
3874 return -EINVAL;
3875 sip->sin6_addr = ip6->saddr;
3876
3877 nexthdr = ip6->nexthdr;
3878 offset += sizeof(_ipv6h);
3879 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3880 if (offset < 0)
3881 return -EINVAL;
3882
3883 proto = nexthdr;
3884 switch (proto) {
3885 case IPPROTO_TCP:
3886 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3887 if (th != NULL)
3888 sip->sin6_port = th->source;
3889 break;
3890 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003891 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003892 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3893 if (uh != NULL)
3894 sip->sin6_port = uh->source;
3895 break;
3896 case IPPROTO_DCCP:
3897 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3898 if (dh != NULL)
3899 sip->sin6_port = dh->dccph_sport;
3900 break;
3901 }
3902 return proto;
3903}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003904#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003905
Casey Schauflere114e472008-02-04 22:29:50 -08003906/**
3907 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3908 * @sk: socket
3909 * @skb: packet
3910 *
3911 * Returns 0 if the packet should be delivered, an error code otherwise
3912 */
3913static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3914{
3915 struct netlbl_lsm_secattr secattr;
3916 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003917 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003918 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003919 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003920 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003921#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003922 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003923#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003924#if IS_ENABLED(CONFIG_IPV6)
3925 struct sockaddr_in6 sadd;
3926 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003927
3928 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3929 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003930#endif /* CONFIG_IPV6 */
3931
Piotr Sawicki129a9982018-07-19 11:42:58 +02003932 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003933 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003934#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3935 /*
3936 * If there is a secmark use it rather than the CIPSO label.
3937 * If there is no secmark fall back to CIPSO.
3938 * The secmark is assumed to reflect policy better.
3939 */
3940 if (skb && skb->secmark != 0) {
3941 skp = smack_from_secid(skb->secmark);
3942 goto access_check;
3943 }
3944#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003945 /*
3946 * Translate what netlabel gave us.
3947 */
3948 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003949
Piotr Sawicki129a9982018-07-19 11:42:58 +02003950 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003951 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003952 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003953 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003954 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003955
Casey Schauflerc6739442013-05-22 18:42:56 -07003956 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003957
Casey Schaufler69f287a2014-12-12 17:08:40 -08003958#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3959access_check:
3960#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003961#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003962 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003963 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003964 ad.a.u.net->netif = skb->skb_iif;
3965 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003966#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003967 /*
3968 * Receiving a packet requires that the other end
3969 * be able to write here. Read access is not required.
3970 * This is the simplist possible security model
3971 * for networking.
3972 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003973 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3974 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003975 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003976 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003977 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003978 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003979#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003980 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003981 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003982 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3983 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003984 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003985#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003986 if (skb && skb->secmark != 0)
3987 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003988 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003989 skp = smack_ipv6host_label(&sadd);
3990 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003991 skp = smack_net_ambient;
3992#ifdef CONFIG_AUDIT
3993 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003994 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003995 ad.a.u.net->netif = skb->skb_iif;
3996 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3997#endif /* CONFIG_AUDIT */
3998 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3999 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4000 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004001#endif /* SMACK_IPV6_SECMARK_LABELING */
4002#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004003 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004004#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02004005 if (rc != 0)
4006 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4007 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07004008 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004009#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07004010 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004011
Paul Moorea8134292008-10-10 10:16:31 -04004012 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004013}
4014
4015/**
4016 * smack_socket_getpeersec_stream - pull in packet label
4017 * @sock: the socket
4018 * @optval: user's destination
4019 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004020 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004021 *
4022 * returns zero on success, an error code otherwise
4023 */
4024static int smack_socket_getpeersec_stream(struct socket *sock,
4025 char __user *optval,
4026 int __user *optlen, unsigned len)
4027{
4028 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004029 char *rcp = "";
4030 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004031 int rc = 0;
4032
4033 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004034 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004035 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004036 slen = strlen(rcp) + 1;
4037 }
Casey Schauflere114e472008-02-04 22:29:50 -08004038
4039 if (slen > len)
4040 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004041 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004042 rc = -EFAULT;
4043
4044 if (put_user(slen, optlen) != 0)
4045 rc = -EFAULT;
4046
4047 return rc;
4048}
4049
4050
4051/**
4052 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004053 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004054 * @skb: packet data
4055 * @secid: pointer to where to put the secid of the packet
4056 *
4057 * Sets the netlabel socket state on sk from parent
4058 */
4059static int smack_socket_getpeersec_dgram(struct socket *sock,
4060 struct sk_buff *skb, u32 *secid)
4061
4062{
4063 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004064 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004065 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004066 int family = PF_UNSPEC;
4067 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004068 int rc;
4069
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004070 if (skb != NULL) {
4071 if (skb->protocol == htons(ETH_P_IP))
4072 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004073#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004074 else if (skb->protocol == htons(ETH_P_IPV6))
4075 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004076#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004077 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004078 if (family == PF_UNSPEC && sock != NULL)
4079 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004080
Casey Schaufler69f287a2014-12-12 17:08:40 -08004081 switch (family) {
4082 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004083 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004084 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004085 break;
4086 case PF_INET:
4087#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4088 s = skb->secmark;
4089 if (s != 0)
4090 break;
4091#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004092 /*
4093 * Translate what netlabel gave us.
4094 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004095 if (sock != NULL && sock->sk != NULL)
4096 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004097 netlbl_secattr_init(&secattr);
4098 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4099 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004100 skp = smack_from_secattr(&secattr, ssp);
4101 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004102 }
4103 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004104 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004105 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004106#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004107 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004108#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004109 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004110 }
4111 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004112 if (s == 0)
4113 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004114 return 0;
4115}
4116
4117/**
Paul Moore07feee82009-03-27 17:10:54 -04004118 * smack_sock_graft - Initialize a newly created socket with an existing sock
4119 * @sk: child sock
4120 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004121 *
Paul Moore07feee82009-03-27 17:10:54 -04004122 * Set the smk_{in,out} state of an existing sock based on the process that
4123 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004124 */
4125static void smack_sock_graft(struct sock *sk, struct socket *parent)
4126{
4127 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004128 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004129
Paul Moore07feee82009-03-27 17:10:54 -04004130 if (sk == NULL ||
4131 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004132 return;
4133
4134 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004135 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004136 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004137 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004138}
4139
4140/**
4141 * smack_inet_conn_request - Smack access check on connect
4142 * @sk: socket involved
4143 * @skb: packet
4144 * @req: unused
4145 *
4146 * Returns 0 if a task with the packet label could write to
4147 * the socket, otherwise an error code
4148 */
4149static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4150 struct request_sock *req)
4151{
Paul Moore07feee82009-03-27 17:10:54 -04004152 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004153 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004154 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004155 struct netlbl_lsm_secattr secattr;
4156 struct sockaddr_in addr;
4157 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004158 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004159 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004160 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004161#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004162 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004163#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004164
Casey Schaufler69f287a2014-12-12 17:08:40 -08004165#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004166 if (family == PF_INET6) {
4167 /*
4168 * Handle mapped IPv4 packets arriving
4169 * via IPv6 sockets. Don't set up netlabel
4170 * processing on IPv6.
4171 */
4172 if (skb->protocol == htons(ETH_P_IP))
4173 family = PF_INET;
4174 else
4175 return 0;
4176 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004177#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004178
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004179#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4180 /*
4181 * If there is a secmark use it rather than the CIPSO label.
4182 * If there is no secmark fall back to CIPSO.
4183 * The secmark is assumed to reflect policy better.
4184 */
4185 if (skb && skb->secmark != 0) {
4186 skp = smack_from_secid(skb->secmark);
4187 goto access_check;
4188 }
4189#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4190
Paul Moore07feee82009-03-27 17:10:54 -04004191 netlbl_secattr_init(&secattr);
4192 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004193 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004194 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004195 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004196 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004197 netlbl_secattr_destroy(&secattr);
4198
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004199#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4200access_check:
4201#endif
4202
Etienne Bassetecfcc532009-04-08 20:40:06 +02004203#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004204 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4205 ad.a.u.net->family = family;
4206 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004207 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4208#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004209 /*
Paul Moore07feee82009-03-27 17:10:54 -04004210 * Receiving a packet requires that the other end be able to write
4211 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004212 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004213 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4214 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004215 if (rc != 0)
4216 return rc;
4217
4218 /*
4219 * Save the peer's label in the request_sock so we can later setup
4220 * smk_packet in the child socket so that SO_PEERCRED can report it.
4221 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004222 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004223
4224 /*
4225 * We need to decide if we want to label the incoming connection here
4226 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004227 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004228 */
4229 hdr = ip_hdr(skb);
4230 addr.sin_addr.s_addr = hdr->saddr;
4231 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004232 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004233 rcu_read_unlock();
4234
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004235 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004236 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004237 else
Paul Moore07feee82009-03-27 17:10:54 -04004238 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004239
4240 return rc;
4241}
4242
Paul Moore07feee82009-03-27 17:10:54 -04004243/**
4244 * smack_inet_csk_clone - Copy the connection information to the new socket
4245 * @sk: the new socket
4246 * @req: the connection's request_sock
4247 *
4248 * Transfer the connection's peer label to the newly created socket.
4249 */
4250static void smack_inet_csk_clone(struct sock *sk,
4251 const struct request_sock *req)
4252{
4253 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004254 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004255
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004256 if (req->peer_secid != 0) {
4257 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004258 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004259 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004260 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004261}
4262
Casey Schauflere114e472008-02-04 22:29:50 -08004263/*
4264 * Key management security hooks
4265 *
4266 * Casey has not tested key support very heavily.
4267 * The permission check is most likely too restrictive.
4268 * If you care about keys please have a look.
4269 */
4270#ifdef CONFIG_KEYS
4271
4272/**
4273 * smack_key_alloc - Set the key security blob
4274 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004275 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004276 * @flags: unused
4277 *
4278 * No allocation required
4279 *
4280 * Returns 0
4281 */
David Howellsd84f4f92008-11-14 10:39:23 +11004282static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004283 unsigned long flags)
4284{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004285 struct smack_known *skp = smk_of_task(cred->security);
4286
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004287 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004288 return 0;
4289}
4290
4291/**
4292 * smack_key_free - Clear the key security blob
4293 * @key: the object
4294 *
4295 * Clear the blob pointer
4296 */
4297static void smack_key_free(struct key *key)
4298{
4299 key->security = NULL;
4300}
4301
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004302/**
Casey Schauflere114e472008-02-04 22:29:50 -08004303 * smack_key_permission - Smack access on a key
4304 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004305 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004306 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004307 *
4308 * Return 0 if the task has read and write to the object,
4309 * an error code otherwise
4310 */
4311static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004312 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004313{
4314 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004315 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004316 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004317 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004318 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004319
4320 keyp = key_ref_to_ptr(key_ref);
4321 if (keyp == NULL)
4322 return -EINVAL;
4323 /*
4324 * If the key hasn't been initialized give it access so that
4325 * it may do so.
4326 */
4327 if (keyp->security == NULL)
4328 return 0;
4329 /*
4330 * This should not occur
4331 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004332 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004333 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004334
4335 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4336 return 0;
4337
Etienne Bassetecfcc532009-04-08 20:40:06 +02004338#ifdef CONFIG_AUDIT
4339 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4340 ad.a.u.key_struct.key = keyp->serial;
4341 ad.a.u.key_struct.key_desc = keyp->description;
4342#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004343 if (perm & KEY_NEED_READ)
4344 request = MAY_READ;
4345 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4346 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004347 rc = smk_access(tkp, keyp->security, request, &ad);
4348 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4349 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004350}
José Bollo7fc5f362015-02-17 15:41:22 +01004351
4352/*
4353 * smack_key_getsecurity - Smack label tagging the key
4354 * @key points to the key to be queried
4355 * @_buffer points to a pointer that should be set to point to the
4356 * resulting string (if no label or an error occurs).
4357 * Return the length of the string (including terminating NUL) or -ve if
4358 * an error.
4359 * May also return 0 (and a NULL buffer pointer) if there is no label.
4360 */
4361static int smack_key_getsecurity(struct key *key, char **_buffer)
4362{
4363 struct smack_known *skp = key->security;
4364 size_t length;
4365 char *copy;
4366
4367 if (key->security == NULL) {
4368 *_buffer = NULL;
4369 return 0;
4370 }
4371
4372 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4373 if (copy == NULL)
4374 return -ENOMEM;
4375 length = strlen(copy) + 1;
4376
4377 *_buffer = copy;
4378 return length;
4379}
4380
Casey Schauflere114e472008-02-04 22:29:50 -08004381#endif /* CONFIG_KEYS */
4382
4383/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004384 * Smack Audit hooks
4385 *
4386 * Audit requires a unique representation of each Smack specific
4387 * rule. This unique representation is used to distinguish the
4388 * object to be audited from remaining kernel objects and also
4389 * works as a glue between the audit hooks.
4390 *
4391 * Since repository entries are added but never deleted, we'll use
4392 * the smack_known label address related to the given audit rule as
4393 * the needed unique representation. This also better fits the smack
4394 * model where nearly everything is a label.
4395 */
4396#ifdef CONFIG_AUDIT
4397
4398/**
4399 * smack_audit_rule_init - Initialize a smack audit rule
4400 * @field: audit rule fields given from user-space (audit.h)
4401 * @op: required testing operator (=, !=, >, <, ...)
4402 * @rulestr: smack label to be audited
4403 * @vrule: pointer to save our own audit rule representation
4404 *
4405 * Prepare to audit cases where (@field @op @rulestr) is true.
4406 * The label to be audited is created if necessay.
4407 */
4408static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4409{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004410 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004411 char **rule = (char **)vrule;
4412 *rule = NULL;
4413
4414 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4415 return -EINVAL;
4416
Al Viro5af75d82008-12-16 05:59:26 -05004417 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004418 return -EINVAL;
4419
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004420 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004421 if (IS_ERR(skp))
4422 return PTR_ERR(skp);
4423
4424 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004425
4426 return 0;
4427}
4428
4429/**
4430 * smack_audit_rule_known - Distinguish Smack audit rules
4431 * @krule: rule of interest, in Audit kernel representation format
4432 *
4433 * This is used to filter Smack rules from remaining Audit ones.
4434 * If it's proved that this rule belongs to us, the
4435 * audit_rule_match hook will be called to do the final judgement.
4436 */
4437static int smack_audit_rule_known(struct audit_krule *krule)
4438{
4439 struct audit_field *f;
4440 int i;
4441
4442 for (i = 0; i < krule->field_count; i++) {
4443 f = &krule->fields[i];
4444
4445 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4446 return 1;
4447 }
4448
4449 return 0;
4450}
4451
4452/**
4453 * smack_audit_rule_match - Audit given object ?
4454 * @secid: security id for identifying the object to test
4455 * @field: audit rule flags given from user-space
4456 * @op: required testing operator
4457 * @vrule: smack internal rule presentation
4458 * @actx: audit context associated with the check
4459 *
4460 * The core Audit hook. It's used to take the decision of
4461 * whether to audit or not to audit a given object.
4462 */
4463static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4464 struct audit_context *actx)
4465{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004466 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004467 char *rule = vrule;
4468
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004469 if (unlikely(!rule)) {
4470 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004471 return -ENOENT;
4472 }
4473
4474 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4475 return 0;
4476
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004477 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004478
4479 /*
4480 * No need to do string comparisons. If a match occurs,
4481 * both pointers will point to the same smack_known
4482 * label.
4483 */
Al Viro5af75d82008-12-16 05:59:26 -05004484 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004485 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004486 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004487 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004488
4489 return 0;
4490}
4491
Casey Schaufler491a0b02016-01-26 15:08:35 -08004492/*
4493 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004494 * No memory was allocated.
4495 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004496
4497#endif /* CONFIG_AUDIT */
4498
Randy Dunlap251a2a92009-02-18 11:42:33 -08004499/**
David Quigley746df9b2013-05-22 12:50:35 -04004500 * smack_ismaclabel - check if xattr @name references a smack MAC label
4501 * @name: Full xattr name to check.
4502 */
4503static int smack_ismaclabel(const char *name)
4504{
4505 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4506}
4507
4508
4509/**
Casey Schauflere114e472008-02-04 22:29:50 -08004510 * smack_secid_to_secctx - return the smack label for a secid
4511 * @secid: incoming integer
4512 * @secdata: destination
4513 * @seclen: how long it is
4514 *
4515 * Exists for networking code.
4516 */
4517static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4518{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004519 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004520
Eric Parisd5630b92010-10-13 16:24:48 -04004521 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004522 *secdata = skp->smk_known;
4523 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004524 return 0;
4525}
4526
Randy Dunlap251a2a92009-02-18 11:42:33 -08004527/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004528 * smack_secctx_to_secid - return the secid for a smack label
4529 * @secdata: smack label
4530 * @seclen: how long result is
4531 * @secid: outgoing integer
4532 *
4533 * Exists for audit and networking code.
4534 */
David Howellse52c17642008-04-29 20:52:51 +01004535static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004536{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004537 struct smack_known *skp = smk_find_entry(secdata);
4538
4539 if (skp)
4540 *secid = skp->smk_secid;
4541 else
4542 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004543 return 0;
4544}
4545
Casey Schaufler491a0b02016-01-26 15:08:35 -08004546/*
4547 * There used to be a smack_release_secctx hook
4548 * that did nothing back when hooks were in a vector.
4549 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004550 */
Casey Schauflere114e472008-02-04 22:29:50 -08004551
David P. Quigley1ee65e32009-09-03 14:25:57 -04004552static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4553{
4554 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4555}
4556
4557static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4558{
4559 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4560}
4561
4562static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4563{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004564 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004565
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004566 *ctx = skp->smk_known;
4567 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004568 return 0;
4569}
4570
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004571static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4572{
4573
4574 struct task_smack *tsp;
4575 struct smack_known *skp;
4576 struct inode_smack *isp;
4577 struct cred *new_creds = *new;
4578
4579 if (new_creds == NULL) {
4580 new_creds = prepare_creds();
4581 if (new_creds == NULL)
4582 return -ENOMEM;
4583 }
4584
4585 tsp = new_creds->security;
4586
4587 /*
4588 * Get label from overlay inode and set it in create_sid
4589 */
4590 isp = d_inode(dentry->d_parent)->i_security;
4591 skp = isp->smk_inode;
4592 tsp->smk_task = skp;
4593 *new = new_creds;
4594 return 0;
4595}
4596
4597static int smack_inode_copy_up_xattr(const char *name)
4598{
4599 /*
4600 * Return 1 if this is the smack access Smack attribute.
4601 */
4602 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4603 return 1;
4604
4605 return -EOPNOTSUPP;
4606}
4607
4608static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4609 struct qstr *name,
4610 const struct cred *old,
4611 struct cred *new)
4612{
4613 struct task_smack *otsp = old->security;
4614 struct task_smack *ntsp = new->security;
4615 struct inode_smack *isp;
4616 int may;
4617
4618 /*
4619 * Use the process credential unless all of
4620 * the transmuting criteria are met
4621 */
4622 ntsp->smk_task = otsp->smk_task;
4623
4624 /*
4625 * the attribute of the containing directory
4626 */
4627 isp = d_inode(dentry->d_parent)->i_security;
4628
4629 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4630 rcu_read_lock();
4631 may = smk_access_entry(otsp->smk_task->smk_known,
4632 isp->smk_inode->smk_known,
4633 &otsp->smk_task->smk_rules);
4634 rcu_read_unlock();
4635
4636 /*
4637 * If the directory is transmuting and the rule
4638 * providing access is transmuting use the containing
4639 * directory label instead of the process label.
4640 */
4641 if (may > 0 && (may & MAY_TRANSMUTE))
4642 ntsp->smk_task = isp->smk_inode;
4643 }
4644 return 0;
4645}
4646
James Morrisca97d932017-02-15 00:18:51 +11004647static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004648 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4649 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4650 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004651
Casey Schauflere20b0432015-05-02 15:11:36 -07004652 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4653 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05004654 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004655 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304656 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4657 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
Casey Schauflere114e472008-02-04 22:29:50 -08004658
Casey Schauflere20b0432015-05-02 15:11:36 -07004659 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004660
Casey Schauflere20b0432015-05-02 15:11:36 -07004661 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4662 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4663 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4664 LSM_HOOK_INIT(inode_link, smack_inode_link),
4665 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4666 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4667 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4668 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4669 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4670 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4671 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4672 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4673 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4674 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4675 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4676 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4677 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4678 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004679
Casey Schauflere20b0432015-05-02 15:11:36 -07004680 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4681 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4682 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4683 LSM_HOOK_INIT(file_lock, smack_file_lock),
4684 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4685 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4686 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4687 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4688 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4689 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004690
Casey Schauflere20b0432015-05-02 15:11:36 -07004691 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004692
Casey Schauflere20b0432015-05-02 15:11:36 -07004693 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4694 LSM_HOOK_INIT(cred_free, smack_cred_free),
4695 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4696 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004697 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004698 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4699 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4700 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4701 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4702 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4703 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4704 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4705 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4706 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4707 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4708 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4709 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4710 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004711 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004712
Casey Schauflere20b0432015-05-02 15:11:36 -07004713 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4714 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004715
Casey Schauflere20b0432015-05-02 15:11:36 -07004716 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4717 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004718
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004719 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4720 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004721 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4722 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4723 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4724 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004725
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004726 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4727 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004728 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4729 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4730 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004731
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004732 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4733 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004734 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4735 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4736 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004737
Casey Schauflere20b0432015-05-02 15:11:36 -07004738 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004739
Casey Schauflere20b0432015-05-02 15:11:36 -07004740 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4741 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004742
Casey Schauflere20b0432015-05-02 15:11:36 -07004743 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4744 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004745
Casey Schauflere20b0432015-05-02 15:11:36 -07004746 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004747 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004748#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004749 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004750#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004751 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4752 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4753 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4754 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4755 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4756 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4757 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4758 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4759 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4760 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004761
Casey Schauflere114e472008-02-04 22:29:50 -08004762 /* key management security hooks */
4763#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004764 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4765 LSM_HOOK_INIT(key_free, smack_key_free),
4766 LSM_HOOK_INIT(key_permission, smack_key_permission),
4767 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004768#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004769
4770 /* Audit hooks */
4771#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004772 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4773 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4774 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004775#endif /* CONFIG_AUDIT */
4776
Casey Schauflere20b0432015-05-02 15:11:36 -07004777 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4778 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4779 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004780 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4781 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4782 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004783 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4784 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4785 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004786};
4787
Etienne Basset7198e2e2009-03-24 20:53:24 +01004788
Casey Schaufler86812bb2012-04-17 18:55:46 -07004789static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004790{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004791 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004792 * Initialize rule list locks
4793 */
4794 mutex_init(&smack_known_huh.smk_rules_lock);
4795 mutex_init(&smack_known_hat.smk_rules_lock);
4796 mutex_init(&smack_known_floor.smk_rules_lock);
4797 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004798 mutex_init(&smack_known_web.smk_rules_lock);
4799 /*
4800 * Initialize rule lists
4801 */
4802 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4803 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4804 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4805 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004806 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4807 /*
4808 * Create the known labels list
4809 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004810 smk_insert_entry(&smack_known_huh);
4811 smk_insert_entry(&smack_known_hat);
4812 smk_insert_entry(&smack_known_star);
4813 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004814 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004815}
4816
Casey Schauflere114e472008-02-04 22:29:50 -08004817/**
4818 * smack_init - initialize the smack system
4819 *
4820 * Returns 0
4821 */
4822static __init int smack_init(void)
4823{
David Howellsd84f4f92008-11-14 10:39:23 +11004824 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004825 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004826
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004827 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004828 return 0;
4829
Rohit1a5b4722014-10-15 17:40:41 +05304830 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4831 if (!smack_inode_cache)
4832 return -ENOMEM;
4833
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004834 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4835 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304836 if (tsp == NULL) {
4837 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004838 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304839 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004840
José Bollod21b7b02015-10-02 15:15:56 +02004841 smack_enabled = 1;
4842
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004843 pr_info("Smack: Initializing.\n");
4844#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4845 pr_info("Smack: Netfilter enabled.\n");
4846#endif
4847#ifdef SMACK_IPV6_PORT_LABELING
4848 pr_info("Smack: IPv6 port labeling enabled.\n");
4849#endif
4850#ifdef SMACK_IPV6_SECMARK_LABELING
4851 pr_info("Smack: IPv6 Netfilter enabled.\n");
4852#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004853
4854 /*
4855 * Set the security state for the initial task.
4856 */
David Howellsd84f4f92008-11-14 10:39:23 +11004857 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004858 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004859
Casey Schaufler86812bb2012-04-17 18:55:46 -07004860 /* initialize the smack_known_list */
4861 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004862
4863 /*
4864 * Register with LSM
4865 */
Casey Schauflerd69dece52017-01-18 17:09:05 -08004866 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
Casey Schauflere114e472008-02-04 22:29:50 -08004867
4868 return 0;
4869}
4870
4871/*
4872 * Smack requires early initialization in order to label
4873 * all processes and objects when they are created.
4874 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004875DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004876 .name = "smack",
Kees Cook3d6e5f62018-10-10 17:18:23 -07004877 .init = smack_init,
4878};