blob: 4c5e5a438f8bdd77f88d4cdfdf8424c910c444ed [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Casey Schauflere114e472008-02-04 22:29:50 -08002/*
3 * Simplified MAC Kernel (smack) security module
4 *
5 * This file contains the smack hook function implementations.
6 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02007 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08008 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03009 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -080010 *
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040012 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000013 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020014 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030015 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080016 */
17
18#include <linux/xattr.h>
19#include <linux/pagemap.h>
20#include <linux/mount.h>
21#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080022#include <linux/kd.h>
23#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040024#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/tcp.h>
26#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070027#include <linux/dccp.h>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020028#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030#include <linux/mutex.h>
31#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080032#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070033#include <net/ip.h>
34#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100035#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070036#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050037#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030038#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000039#include <linux/msg.h>
40#include <linux/shm.h>
41#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053042#include <linux/parser.h>
David Howells2febd252018-11-01 23:07:24 +000043#include <linux/fs_context.h>
44#include <linux/fs_parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080045#include "smack.h"
46
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020047#define TRANS_TRUE "TRUE"
48#define TRANS_TRUE_SIZE 4
49
Casey Schauflerc6739442013-05-22 18:42:56 -070050#define SMK_CONNECTING 0
51#define SMK_RECEIVING 1
52#define SMK_SENDING 2
53
Casey Schaufler21abb1e2015-07-22 14:25:31 -070054#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053055DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080056static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070057#endif
Rohit1a5b4722014-10-15 17:40:41 +053058static struct kmem_cache *smack_inode_cache;
Casey Schaufler4e328b02019-04-02 11:37:12 -070059struct kmem_cache *smack_rule_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080060int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070061
Al Viroc3300aa2018-12-16 01:52:24 -050062#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
63static struct {
64 const char *name;
65 int len;
66 int opt;
67} smk_mount_opts[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +010068 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
Al Viroc3300aa2018-12-16 01:52:24 -050069 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
Vivek Trivedi3bf27892015-06-22 15:36:06 +053070};
Al Viroc3300aa2018-12-16 01:52:24 -050071#undef A
72
73static int match_opt_prefix(char *s, int l, char **arg)
74{
75 int i;
76
77 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
78 size_t len = smk_mount_opts[i].len;
79 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
80 continue;
81 if (len == l || s[len] != '=')
82 continue;
83 *arg = s + len + 1;
84 return smk_mount_opts[i].opt;
85 }
86 return Opt_error;
87}
Vivek Trivedi3bf27892015-06-22 15:36:06 +053088
Casey Schaufler3d04c922015-08-12 11:56:02 -070089#ifdef CONFIG_SECURITY_SMACK_BRINGUP
90static char *smk_bu_mess[] = {
91 "Bringup Error", /* Unused */
92 "Bringup", /* SMACK_BRINGUP_ALLOW */
93 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
94 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
95};
96
Casey Schauflerd166c802014-08-27 14:51:27 -070097static void smk_bu_mode(int mode, char *s)
98{
99 int i = 0;
100
101 if (mode & MAY_READ)
102 s[i++] = 'r';
103 if (mode & MAY_WRITE)
104 s[i++] = 'w';
105 if (mode & MAY_EXEC)
106 s[i++] = 'x';
107 if (mode & MAY_APPEND)
108 s[i++] = 'a';
109 if (mode & MAY_TRANSMUTE)
110 s[i++] = 't';
111 if (mode & MAY_LOCK)
112 s[i++] = 'l';
113 if (i == 0)
114 s[i++] = '-';
115 s[i] = '\0';
116}
117#endif
118
119#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200120static int smk_bu_note(char *note, struct smack_known *sskp,
121 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700122{
123 char acc[SMK_NUM_ACCESS_TYPE + 1];
124
125 if (rc <= 0)
126 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700127 if (rc > SMACK_UNCONFINED_OBJECT)
128 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700129
130 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700131 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200132 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700133 return 0;
134}
135#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200136#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700137#endif
138
139#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200140static int smk_bu_current(char *note, struct smack_known *oskp,
141 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700142{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800143 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700144 char acc[SMK_NUM_ACCESS_TYPE + 1];
145
146 if (rc <= 0)
147 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700148 if (rc > SMACK_UNCONFINED_OBJECT)
149 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700150
151 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700152 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200153 tsp->smk_task->smk_known, oskp->smk_known,
154 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700155 return 0;
156}
157#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200158#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700159#endif
160
161#ifdef CONFIG_SECURITY_SMACK_BRINGUP
162static int smk_bu_task(struct task_struct *otp, int mode, int rc)
163{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800164 struct task_smack *tsp = smack_cred(current_cred());
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300165 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700166 char acc[SMK_NUM_ACCESS_TYPE + 1];
167
168 if (rc <= 0)
169 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700170 if (rc > SMACK_UNCONFINED_OBJECT)
171 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700172
173 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700174 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300175 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700176 current->comm, otp->comm);
177 return 0;
178}
179#else
180#define smk_bu_task(otp, mode, RC) (RC)
181#endif
182
183#ifdef CONFIG_SECURITY_SMACK_BRINGUP
184static int smk_bu_inode(struct inode *inode, int mode, int rc)
185{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800186 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800187 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700188 char acc[SMK_NUM_ACCESS_TYPE + 1];
189
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700190 if (isp->smk_flags & SMK_INODE_IMPURE)
191 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
192 inode->i_sb->s_id, inode->i_ino, current->comm);
193
Casey Schauflerd166c802014-08-27 14:51:27 -0700194 if (rc <= 0)
195 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700196 if (rc > SMACK_UNCONFINED_OBJECT)
197 rc = 0;
198 if (rc == SMACK_UNCONFINED_SUBJECT &&
199 (mode & (MAY_WRITE | MAY_APPEND)))
200 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700201
202 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700203
204 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
205 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700206 inode->i_sb->s_id, inode->i_ino, current->comm);
207 return 0;
208}
209#else
210#define smk_bu_inode(inode, mode, RC) (RC)
211#endif
212
213#ifdef CONFIG_SECURITY_SMACK_BRINGUP
214static int smk_bu_file(struct file *file, int mode, int rc)
215{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800216 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700217 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800218 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800219 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700220 char acc[SMK_NUM_ACCESS_TYPE + 1];
221
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700222 if (isp->smk_flags & SMK_INODE_IMPURE)
223 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
224 inode->i_sb->s_id, inode->i_ino, current->comm);
225
Casey Schauflerd166c802014-08-27 14:51:27 -0700226 if (rc <= 0)
227 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700228 if (rc > SMACK_UNCONFINED_OBJECT)
229 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700230
231 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700232 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800233 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400234 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700235 current->comm);
236 return 0;
237}
238#else
239#define smk_bu_file(file, mode, RC) (RC)
240#endif
241
242#ifdef CONFIG_SECURITY_SMACK_BRINGUP
243static int smk_bu_credfile(const struct cred *cred, struct file *file,
244 int mode, int rc)
245{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800246 struct task_smack *tsp = smack_cred(cred);
Casey Schauflerd166c802014-08-27 14:51:27 -0700247 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500248 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800249 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700250 char acc[SMK_NUM_ACCESS_TYPE + 1];
251
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700252 if (isp->smk_flags & SMK_INODE_IMPURE)
253 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
254 inode->i_sb->s_id, inode->i_ino, current->comm);
255
Casey Schauflerd166c802014-08-27 14:51:27 -0700256 if (rc <= 0)
257 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700258 if (rc > SMACK_UNCONFINED_OBJECT)
259 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700260
261 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700262 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200263 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400264 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700265 current->comm);
266 return 0;
267}
268#else
269#define smk_bu_credfile(cred, file, mode, RC) (RC)
270#endif
271
Casey Schauflere114e472008-02-04 22:29:50 -0800272/**
273 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100274 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800275 * @ip: a pointer to the inode
276 * @dp: a pointer to the dentry
277 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200278 * Returns a pointer to the master list entry for the Smack label,
279 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800280 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700281static struct smack_known *smk_fetch(const char *name, struct inode *ip,
282 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800283{
284 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700285 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700286 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800287
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200288 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200289 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800290
Casey Schauflerf7112e62012-05-06 15:22:02 -0700291 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
292 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200293 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800294
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200295 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200296 if (rc < 0)
297 skp = ERR_PTR(rc);
298 else if (rc == 0)
299 skp = NULL;
300 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700301 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700302
303 kfree(buffer);
304
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700305 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800306}
307
308/**
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700309 * init_inode_smack - initialize an inode security blob
310 * @isp: the blob to initialize
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200311 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800312 *
Casey Schauflere114e472008-02-04 22:29:50 -0800313 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700314static void init_inode_smack(struct inode *inode, struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800315{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700316 struct inode_smack *isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -0800317
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200318 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800319 isp->smk_flags = 0;
320 mutex_init(&isp->smk_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800321}
322
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800323/**
Casey Schauflerbbd36622018-11-12 09:30:56 -0800324 * init_task_smack - initialize a task security blob
325 * @tsp: blob to initialize
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100326 * @task: a pointer to the Smack label for the running task
327 * @forked: a pointer to the Smack label for the forked task
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800328 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800329 */
Casey Schauflerbbd36622018-11-12 09:30:56 -0800330static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
331 struct smack_known *forked)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800332{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800333 tsp->smk_task = task;
334 tsp->smk_forked = forked;
335 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200336 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800337 mutex_init(&tsp->smk_rules_lock);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800338}
339
340/**
341 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100342 * @nhead: new rules header pointer
343 * @ohead: old rules header pointer
344 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800345 *
346 * Returns 0 on success, -ENOMEM on error
347 */
348static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
349 gfp_t gfp)
350{
351 struct smack_rule *nrp;
352 struct smack_rule *orp;
353 int rc = 0;
354
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800355 list_for_each_entry_rcu(orp, ohead, list) {
Casey Schaufler4e328b02019-04-02 11:37:12 -0700356 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800357 if (nrp == NULL) {
358 rc = -ENOMEM;
359 break;
360 }
361 *nrp = *orp;
362 list_add_rcu(&nrp->list, nhead);
363 }
364 return rc;
365}
366
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100367/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200368 * smk_copy_relabel - copy smk_relabel labels list
369 * @nhead: new rules header pointer
370 * @ohead: old rules header pointer
371 * @gfp: type of the memory for the allocation
372 *
373 * Returns 0 on success, -ENOMEM on error
374 */
375static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
376 gfp_t gfp)
377{
378 struct smack_known_list_elem *nklep;
379 struct smack_known_list_elem *oklep;
380
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200381 list_for_each_entry(oklep, ohead, list) {
382 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
383 if (nklep == NULL) {
384 smk_destroy_label_list(nhead);
385 return -ENOMEM;
386 }
387 nklep->smk_label = oklep->smk_label;
388 list_add(&nklep->list, nhead);
389 }
390
391 return 0;
392}
393
394/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100395 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
396 * @mode - input mode in form of PTRACE_MODE_*
397 *
398 * Returns a converted MAY_* mode usable by smack rules
399 */
400static inline unsigned int smk_ptrace_mode(unsigned int mode)
401{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800402 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100403 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800404 if (mode & PTRACE_MODE_READ)
405 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100406
407 return 0;
408}
409
410/**
411 * smk_ptrace_rule_check - helper for ptrace access
412 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200413 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100414 * @mode: ptrace attachment mode (PTRACE_MODE_*)
415 * @func: name of the function that called us, used for audit
416 *
417 * Returns 0 on access granted, -error on error
418 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200419static int smk_ptrace_rule_check(struct task_struct *tracer,
420 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100421 unsigned int mode, const char *func)
422{
423 int rc;
424 struct smk_audit_info ad, *saip = NULL;
425 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200426 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700427 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100428
429 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
430 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
431 smk_ad_setfield_u_tsk(&ad, tracer);
432 saip = &ad;
433 }
434
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300435 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700436 tracercred = __task_cred(tracer);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800437 tsp = smack_cred(tracercred);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200438 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100439
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100440 if ((mode & PTRACE_MODE_ATTACH) &&
441 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
442 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200443 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100444 rc = 0;
445 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
446 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700447 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100448 rc = 0;
449 else
450 rc = -EACCES;
451
452 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200453 smack_log(tracer_known->smk_known,
454 tracee_known->smk_known,
455 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100456
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300457 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100458 return rc;
459 }
460
461 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200462 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300463
464 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100465 return rc;
466}
467
Casey Schauflere114e472008-02-04 22:29:50 -0800468/*
469 * LSM hooks.
470 * We he, that is fun!
471 */
472
473/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000474 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800475 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100476 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800477 *
478 * Returns 0 if access is OK, an error code otherwise
479 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100480 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800481 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000482static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800483{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700484 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800485
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300486 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200487
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700488 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100489}
Casey Schauflere114e472008-02-04 22:29:50 -0800490
David Howells5cd9c582008-08-14 11:37:28 +0100491/**
492 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
493 * @ptp: parent task pointer
494 *
495 * Returns 0 if access is OK, an error code otherwise
496 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100497 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100498 */
499static int smack_ptrace_traceme(struct task_struct *ptp)
500{
501 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700502 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100503
Casey Schauflerb17103a2018-11-09 16:12:56 -0800504 skp = smk_of_task(smack_cred(current_cred()));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200505
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200506 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800507 return rc;
508}
509
510/**
511 * smack_syslog - Smack approval on syslog
512 * @type: message type
513 *
Casey Schauflere114e472008-02-04 22:29:50 -0800514 * Returns 0 on success, error code otherwise.
515 */
Eric Paris12b30522010-11-15 18:36:29 -0500516static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800517{
Eric Paris12b30522010-11-15 18:36:29 -0500518 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700519 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800520
Casey Schaufler1880eff2012-06-05 15:28:30 -0700521 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800522 return 0;
523
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800524 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800525 rc = -EACCES;
526
527 return rc;
528}
529
Casey Schauflere114e472008-02-04 22:29:50 -0800530/*
531 * Superblock Hooks.
532 */
533
534/**
535 * smack_sb_alloc_security - allocate a superblock blob
536 * @sb: the superblock getting the blob
537 *
538 * Returns 0 on success or -ENOMEM on error.
539 */
540static int smack_sb_alloc_security(struct super_block *sb)
541{
542 struct superblock_smack *sbsp;
543
544 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
545
546 if (sbsp == NULL)
547 return -ENOMEM;
548
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200549 sbsp->smk_root = &smack_known_floor;
550 sbsp->smk_default = &smack_known_floor;
551 sbsp->smk_floor = &smack_known_floor;
552 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700553 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500554 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700555 */
Casey Schauflere114e472008-02-04 22:29:50 -0800556 sb->s_security = sbsp;
557
558 return 0;
559}
560
561/**
562 * smack_sb_free_security - free a superblock blob
563 * @sb: the superblock getting the blob
564 *
565 */
566static void smack_sb_free_security(struct super_block *sb)
567{
568 kfree(sb->s_security);
569 sb->s_security = NULL;
570}
571
Al Viro12085b12018-12-13 15:18:05 -0500572struct smack_mnt_opts {
573 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
574};
575
Al Viro204cc0c2018-12-13 13:41:47 -0500576static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800577{
Al Viro12085b12018-12-13 15:18:05 -0500578 struct smack_mnt_opts *opts = mnt_opts;
579 kfree(opts->fsdefault);
580 kfree(opts->fsfloor);
581 kfree(opts->fshat);
582 kfree(opts->fsroot);
583 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500584 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800585}
586
Al Viro55c0e5b2018-12-16 01:09:45 -0500587static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530588{
Al Viro55c0e5b2018-12-16 01:09:45 -0500589 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530590
Al Viro55c0e5b2018-12-16 01:09:45 -0500591 if (!opts) {
592 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
593 if (!opts)
594 return -ENOMEM;
595 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530596 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500597 if (!s)
598 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530599
Al Viro55c0e5b2018-12-16 01:09:45 -0500600 switch (token) {
601 case Opt_fsdefault:
602 if (opts->fsdefault)
603 goto out_opt_err;
604 opts->fsdefault = s;
605 break;
606 case Opt_fsfloor:
607 if (opts->fsfloor)
608 goto out_opt_err;
609 opts->fsfloor = s;
610 break;
611 case Opt_fshat:
612 if (opts->fshat)
613 goto out_opt_err;
614 opts->fshat = s;
615 break;
616 case Opt_fsroot:
617 if (opts->fsroot)
618 goto out_opt_err;
619 opts->fsroot = s;
620 break;
621 case Opt_fstransmute:
622 if (opts->fstransmute)
623 goto out_opt_err;
624 opts->fstransmute = s;
625 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530626 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530627 return 0;
628
629out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530630 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500631 return -EINVAL;
632}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530633
Al Viro0b520752018-12-23 16:02:47 -0500634/**
635 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
636 * @fc: The new filesystem context.
637 * @src_fc: The source filesystem context being duplicated.
638 *
639 * Returns 0 on success or -ENOMEM on error.
640 */
641static int smack_fs_context_dup(struct fs_context *fc,
642 struct fs_context *src_fc)
643{
644 struct smack_mnt_opts *dst, *src = src_fc->security;
645
646 if (!src)
647 return 0;
648
649 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
650 if (!fc->security)
651 return -ENOMEM;
652 dst = fc->security;
653
654 if (src->fsdefault) {
655 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
656 if (!dst->fsdefault)
657 return -ENOMEM;
658 }
659 if (src->fsfloor) {
660 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
661 if (!dst->fsfloor)
662 return -ENOMEM;
663 }
664 if (src->fshat) {
665 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
666 if (!dst->fshat)
667 return -ENOMEM;
668 }
669 if (src->fsroot) {
670 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
671 if (!dst->fsroot)
672 return -ENOMEM;
673 }
674 if (src->fstransmute) {
675 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
676 if (!dst->fstransmute)
677 return -ENOMEM;
678 }
679 return 0;
680}
681
David Howells2febd252018-11-01 23:07:24 +0000682static const struct fs_parameter_spec smack_param_specs[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +0100683 fsparam_string("smackfsdef", Opt_fsdefault),
684 fsparam_string("smackfsdefault", Opt_fsdefault),
685 fsparam_string("smackfsfloor", Opt_fsfloor),
686 fsparam_string("smackfshat", Opt_fshat),
687 fsparam_string("smackfsroot", Opt_fsroot),
688 fsparam_string("smackfstransmute", Opt_fstransmute),
David Howells2febd252018-11-01 23:07:24 +0000689 {}
690};
691
692static const struct fs_parameter_description smack_fs_parameters = {
693 .name = "smack",
694 .specs = smack_param_specs,
695};
696
697/**
698 * smack_fs_context_parse_param - Parse a single mount parameter
699 * @fc: The new filesystem context being constructed.
700 * @param: The parameter.
701 *
702 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
703 * error.
704 */
705static int smack_fs_context_parse_param(struct fs_context *fc,
706 struct fs_parameter *param)
707{
708 struct fs_parse_result result;
709 int opt, rc;
710
711 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
712 if (opt < 0)
713 return opt;
714
715 rc = smack_add_opt(opt, param->string, &fc->security);
716 if (!rc)
717 param->string = NULL;
718 return rc;
719}
720
Al Virod2497e12018-12-16 01:37:06 -0500721static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530722{
Al Virod2497e12018-12-16 01:37:06 -0500723 char *from = options, *to = options;
724 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530725
Al Viroc3300aa2018-12-16 01:52:24 -0500726 while (1) {
727 char *next = strchr(from, ',');
728 int token, len, rc;
729 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530730
Al Viroc3300aa2018-12-16 01:52:24 -0500731 if (next)
732 len = next - from;
733 else
734 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530735
Al Viroc3300aa2018-12-16 01:52:24 -0500736 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500737 if (token != Opt_error) {
738 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
739 rc = smack_add_opt(token, arg, mnt_opts);
740 if (unlikely(rc)) {
741 kfree(arg);
742 if (*mnt_opts)
743 smack_free_mnt_opts(*mnt_opts);
744 *mnt_opts = NULL;
745 return rc;
746 }
747 } else {
748 if (!first) { // copy with preceding comma
749 from--;
750 len++;
751 }
752 if (to != from)
753 memmove(to, from, len);
754 to += len;
755 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530756 }
Al Viroc3300aa2018-12-16 01:52:24 -0500757 if (!from[len])
758 break;
759 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530760 }
Al Virod2497e12018-12-16 01:37:06 -0500761 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530762 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530763}
764
765/**
766 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800767 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530768 * @opts: Smack mount options
769 * @kern_flags: mount option from kernel space or user space
770 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800771 *
772 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530773 *
774 * Allow filesystems with binary mount data to explicitly set Smack mount
775 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800776 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530777static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500778 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530779 unsigned long kern_flags,
780 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800781{
782 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000783 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800784 struct superblock_smack *sp = sb->s_security;
785 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800786 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500787 struct smack_mnt_opts *opts = mnt_opts;
788 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800789
Seth Forshee9f50eda2015-09-23 15:16:06 -0500790 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800791 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700792
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700793 if (inode->i_security == NULL) {
794 int rc = lsm_inode_alloc(inode);
795
796 if (rc)
797 return rc;
798 }
799
Himanshu Shukla2097f592016-11-10 16:19:52 +0530800 if (!smack_privileged(CAP_MAC_ADMIN)) {
801 /*
802 * Unprivileged mounts don't get to specify Smack values.
803 */
Al Viro12085b12018-12-13 15:18:05 -0500804 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530805 return -EPERM;
806 /*
807 * Unprivileged mounts get root and default from the caller.
808 */
809 skp = smk_of_current();
810 sp->smk_root = skp;
811 sp->smk_default = skp;
812 /*
813 * For a handful of fs types with no user-controlled
814 * backing store it's okay to trust security labels
815 * in the filesystem. The rest are untrusted.
816 */
817 if (sb->s_user_ns != &init_user_ns &&
818 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
819 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500820 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530821 sp->smk_flags |= SMK_SB_UNTRUSTED;
822 }
823 }
824
Seth Forshee9f50eda2015-09-23 15:16:06 -0500825 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800826
Al Viro12085b12018-12-13 15:18:05 -0500827 if (opts) {
828 if (opts->fsdefault) {
829 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200830 if (IS_ERR(skp))
831 return PTR_ERR(skp);
832 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500833 }
834 if (opts->fsfloor) {
835 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530836 if (IS_ERR(skp))
837 return PTR_ERR(skp);
838 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500839 }
840 if (opts->fshat) {
841 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530842 if (IS_ERR(skp))
843 return PTR_ERR(skp);
844 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500845 }
846 if (opts->fsroot) {
847 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200848 if (IS_ERR(skp))
849 return PTR_ERR(skp);
850 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500851 }
852 if (opts->fstransmute) {
853 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200854 if (IS_ERR(skp))
855 return PTR_ERR(skp);
856 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500857 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800858 }
859 }
860
861 /*
862 * Initialize the root inode.
863 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700864 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800865
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700866 if (transmute) {
867 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700868 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700869 }
Casey Schauflere830b392013-05-22 18:43:07 -0700870
Casey Schauflere114e472008-02-04 22:29:50 -0800871 return 0;
872}
873
874/**
875 * smack_sb_statfs - Smack check on statfs
876 * @dentry: identifies the file system in question
877 *
878 * Returns 0 if current can read the floor of the filesystem,
879 * and error code otherwise
880 */
881static int smack_sb_statfs(struct dentry *dentry)
882{
883 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200884 int rc;
885 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800886
Eric Parisa2694342011-04-25 13:10:27 -0400887 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200888 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
889
890 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700891 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200892 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800893}
894
Casey Schauflere114e472008-02-04 22:29:50 -0800895/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800896 * BPRM hooks
897 */
898
Casey Schauflerce8a4322011-09-29 18:21:01 -0700899/**
900 * smack_bprm_set_creds - set creds for exec
901 * @bprm: the exec information
902 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100903 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700904 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800905static int smack_bprm_set_creds(struct linux_binprm *bprm)
906{
Al Viro496ad9a2013-01-23 17:07:38 -0500907 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800908 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800909 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500910 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800911 int rc;
912
Kees Cookddb4a142017-07-18 15:25:23 -0700913 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800914 return 0;
915
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800916 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300917 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800918 return 0;
919
Seth Forshee809c02e2016-04-26 14:36:22 -0500920 sbsp = inode->i_sb->s_security;
921 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
922 isp->smk_task != sbsp->smk_root)
923 return 0;
924
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300925 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100926 struct task_struct *tracer;
927 rc = 0;
928
929 rcu_read_lock();
930 tracer = ptrace_parent(current);
931 if (likely(tracer != NULL))
932 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200933 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100934 PTRACE_MODE_ATTACH,
935 __func__);
936 rcu_read_unlock();
937
938 if (rc != 0)
939 return rc;
940 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300941 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800942
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300943 bsp->smk_task = isp->smk_task;
944 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800945
Kees Cookccbb6e12017-07-18 15:25:26 -0700946 /* Decide if this is a secure exec. */
947 if (bsp->smk_task != bsp->smk_forked)
948 bprm->secureexec = 1;
949
Casey Schaufler676dac42010-12-02 06:43:39 -0800950 return 0;
951}
952
953/*
Casey Schauflere114e472008-02-04 22:29:50 -0800954 * Inode hooks
955 */
956
957/**
958 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800959 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800960 *
961 * Returns 0 if it gets a blob, -ENOMEM otherwise
962 */
963static int smack_inode_alloc_security(struct inode *inode)
964{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700965 struct smack_known *skp = smk_of_current();
966
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700967 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800968 return 0;
969}
970
971/**
Casey Schauflere114e472008-02-04 22:29:50 -0800972 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200973 * @inode: the newly created inode
974 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500975 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800976 * @name: where to put the attribute name
977 * @value: where to put the attribute value
978 * @len: where to put the length of the attribute
979 *
980 * Returns 0 if it all works out, -ENOMEM if there's no memory
981 */
982static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900983 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500984 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800985{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800986 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700987 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200988 struct smack_known *isp = smk_of_inode(inode);
989 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800990 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800991
Tetsuo Handa95489062013-07-25 05:44:02 +0900992 if (name)
993 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800994
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100995 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800996 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200997 may = smk_access_entry(skp->smk_known, dsp->smk_known,
998 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800999 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001000
1001 /*
1002 * If the access rule allows transmutation and
1003 * the directory requests transmutation then
1004 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001005 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001006 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001007 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001008 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001009 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001010 issp->smk_flags |= SMK_INODE_CHANGED;
1011 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001012
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001013 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001014 if (*value == NULL)
1015 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001016
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001017 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001018 }
Casey Schauflere114e472008-02-04 22:29:50 -08001019
1020 return 0;
1021}
1022
1023/**
1024 * smack_inode_link - Smack check on link
1025 * @old_dentry: the existing object
1026 * @dir: unused
1027 * @new_dentry: the new object
1028 *
1029 * Returns 0 if access is permitted, an error code otherwise
1030 */
1031static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1032 struct dentry *new_dentry)
1033{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001034 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001035 struct smk_audit_info ad;
1036 int rc;
1037
Eric Parisa2694342011-04-25 13:10:27 -04001038 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001039 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001040
David Howellsc6f493d2015-03-17 22:26:22 +00001041 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001043 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001044
David Howells88025652015-01-29 12:02:32 +00001045 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001046 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001047 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1048 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001049 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001050 }
1051
1052 return rc;
1053}
1054
1055/**
1056 * smack_inode_unlink - Smack check on inode deletion
1057 * @dir: containing directory object
1058 * @dentry: file to unlink
1059 *
1060 * Returns 0 if current can write the containing directory
1061 * and the object, error code otherwise
1062 */
1063static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1064{
David Howellsc6f493d2015-03-17 22:26:22 +00001065 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001066 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001067 int rc;
1068
Eric Parisa2694342011-04-25 13:10:27 -04001069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001070 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1071
Casey Schauflere114e472008-02-04 22:29:50 -08001072 /*
1073 * You need write access to the thing you're unlinking
1074 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001075 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001076 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001077 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001078 /*
1079 * You also need write access to the containing directory
1080 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001081 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001082 smk_ad_setfield_u_fs_inode(&ad, dir);
1083 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001084 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 }
Casey Schauflere114e472008-02-04 22:29:50 -08001086 return rc;
1087}
1088
1089/**
1090 * smack_inode_rmdir - Smack check on directory deletion
1091 * @dir: containing directory object
1092 * @dentry: directory to unlink
1093 *
1094 * Returns 0 if current can write the containing directory
1095 * and the directory, error code otherwise
1096 */
1097static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1098{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001099 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001100 int rc;
1101
Eric Parisa2694342011-04-25 13:10:27 -04001102 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1104
Casey Schauflere114e472008-02-04 22:29:50 -08001105 /*
1106 * You need write access to the thing you're removing
1107 */
David Howellsc6f493d2015-03-17 22:26:22 +00001108 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1109 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001110 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001111 /*
1112 * You also need write access to the containing directory
1113 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001114 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001115 smk_ad_setfield_u_fs_inode(&ad, dir);
1116 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001117 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 }
Casey Schauflere114e472008-02-04 22:29:50 -08001119
1120 return rc;
1121}
1122
1123/**
1124 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001125 * @old_inode: unused
1126 * @old_dentry: the old object
1127 * @new_inode: unused
1128 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001129 *
1130 * Read and write access is required on both the old and
1131 * new directories.
1132 *
1133 * Returns 0 if access is permitted, an error code otherwise
1134 */
1135static int smack_inode_rename(struct inode *old_inode,
1136 struct dentry *old_dentry,
1137 struct inode *new_inode,
1138 struct dentry *new_dentry)
1139{
1140 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001141 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001142 struct smk_audit_info ad;
1143
Eric Parisa2694342011-04-25 13:10:27 -04001144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001146
David Howellsc6f493d2015-03-17 22:26:22 +00001147 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001149 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001150
David Howells88025652015-01-29 12:02:32 +00001151 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001152 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001153 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1154 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001155 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001156 }
Casey Schauflere114e472008-02-04 22:29:50 -08001157 return rc;
1158}
1159
1160/**
1161 * smack_inode_permission - Smack version of permission()
1162 * @inode: the inode in question
1163 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001164 *
1165 * This is the important Smack hook.
1166 *
1167 * Returns 0 if access is permitted, -EACCES otherwise
1168 */
Al Viroe74f71e2011-06-20 19:38:15 -04001169static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001170{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001171 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001172 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001173 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001174 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001175
1176 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001177 /*
1178 * No permission to check. Existence test. Yup, it's there.
1179 */
1180 if (mask == 0)
1181 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001182
Seth Forshee9f50eda2015-09-23 15:16:06 -05001183 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1184 if (smk_of_inode(inode) != sbsp->smk_root)
1185 return -EACCES;
1186 }
1187
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001188 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001189 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001190 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001192 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001193 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1194 rc = smk_bu_inode(inode, mask, rc);
1195 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001196}
1197
1198/**
1199 * smack_inode_setattr - Smack check for setting attributes
1200 * @dentry: the object
1201 * @iattr: for the force flag
1202 *
1203 * Returns 0 if access is permitted, an error code otherwise
1204 */
1205static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1206{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001207 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001208 int rc;
1209
Casey Schauflere114e472008-02-04 22:29:50 -08001210 /*
1211 * Need to allow for clearing the setuid bit.
1212 */
1213 if (iattr->ia_valid & ATTR_FORCE)
1214 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001215 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001216 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001217
David Howellsc6f493d2015-03-17 22:26:22 +00001218 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1219 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001220 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001221}
1222
1223/**
1224 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001225 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001226 * @dentry: the object
1227 *
1228 * Returns 0 if access is permitted, an error code otherwise
1229 */
Al Viro3f7036a2015-03-08 19:28:30 -04001230static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001231{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001232 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001233 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001234 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001235
Eric Parisf48b7392011-04-25 12:54:27 -04001236 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001237 smk_ad_setfield_u_fs_path(&ad, *path);
1238 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1239 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001240 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001241}
1242
1243/**
1244 * smack_inode_setxattr - Smack check for setting xattrs
1245 * @dentry: the object
1246 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001247 * @value: value of the attribute
1248 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001249 * @flags: unused
1250 *
1251 * This protects the Smack attribute explicitly.
1252 *
1253 * Returns 0 if access is permitted, an error code otherwise
1254 */
David Howells8f0cfa52008-04-29 00:59:41 -07001255static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1256 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001257{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001258 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001259 struct smack_known *skp;
1260 int check_priv = 0;
1261 int check_import = 0;
1262 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001263 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001264
Casey Schaufler19760ad2013-12-16 16:27:26 -08001265 /*
1266 * Check label validity here so import won't fail in post_setxattr
1267 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001268 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1269 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001270 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1271 check_priv = 1;
1272 check_import = 1;
1273 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1274 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1275 check_priv = 1;
1276 check_import = 1;
1277 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001278 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001279 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001280 if (size != TRANS_TRUE_SIZE ||
1281 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1282 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001283 } else
1284 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1285
Casey Schaufler19760ad2013-12-16 16:27:26 -08001286 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1287 rc = -EPERM;
1288
1289 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001290 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001291 if (IS_ERR(skp))
1292 rc = PTR_ERR(skp);
1293 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001294 (skp == &smack_known_star || skp == &smack_known_web)))
1295 rc = -EINVAL;
1296 }
1297
Eric Parisa2694342011-04-25 13:10:27 -04001298 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001299 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1300
Casey Schauflerd166c802014-08-27 14:51:27 -07001301 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001302 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1303 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001304 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001305
1306 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001307}
1308
1309/**
1310 * smack_inode_post_setxattr - Apply the Smack update approved above
1311 * @dentry: object
1312 * @name: attribute name
1313 * @value: attribute value
1314 * @size: attribute size
1315 * @flags: unused
1316 *
1317 * Set the pointer in the inode blob to the entry found
1318 * in the master label list.
1319 */
David Howells8f0cfa52008-04-29 00:59:41 -07001320static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1321 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001322{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001323 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001324 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001325
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001326 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1327 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1328 return;
1329 }
1330
Casey Schaufler676dac42010-12-02 06:43:39 -08001331 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001332 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001333 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001334 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001335 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001336 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001337 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001338 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001339 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001340 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001341 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001342 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001343 }
Casey Schauflere114e472008-02-04 22:29:50 -08001344
1345 return;
1346}
1347
Casey Schauflerce8a4322011-09-29 18:21:01 -07001348/**
Casey Schauflere114e472008-02-04 22:29:50 -08001349 * smack_inode_getxattr - Smack check on getxattr
1350 * @dentry: the object
1351 * @name: unused
1352 *
1353 * Returns 0 if access is permitted, an error code otherwise
1354 */
David Howells8f0cfa52008-04-29 00:59:41 -07001355static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001356{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001357 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001358 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001359
Eric Parisa2694342011-04-25 13:10:27 -04001360 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001361 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1362
David Howellsc6f493d2015-03-17 22:26:22 +00001363 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1364 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001365 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001366}
1367
Casey Schauflerce8a4322011-09-29 18:21:01 -07001368/**
Casey Schauflere114e472008-02-04 22:29:50 -08001369 * smack_inode_removexattr - Smack check on removexattr
1370 * @dentry: the object
1371 * @name: name of the attribute
1372 *
1373 * Removing the Smack attribute requires CAP_MAC_ADMIN
1374 *
1375 * Returns 0 if access is permitted, an error code otherwise
1376 */
David Howells8f0cfa52008-04-29 00:59:41 -07001377static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001378{
Casey Schaufler676dac42010-12-02 06:43:39 -08001379 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001380 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001381 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001382
Casey Schauflerbcdca222008-02-23 15:24:04 -08001383 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1384 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001385 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001386 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001387 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301388 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001389 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001390 rc = -EPERM;
1391 } else
1392 rc = cap_inode_removexattr(dentry, name);
1393
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001394 if (rc != 0)
1395 return rc;
1396
Eric Parisa2694342011-04-25 13:10:27 -04001397 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001398 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001399
David Howellsc6f493d2015-03-17 22:26:22 +00001400 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1401 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001402 if (rc != 0)
1403 return rc;
1404
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001405 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001406 /*
1407 * Don't do anything special for these.
1408 * XATTR_NAME_SMACKIPIN
1409 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001410 */
José Bollo80124952016-01-12 21:23:40 +01001411 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001412 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001413 struct superblock_smack *sbsp = sbp->s_security;
1414
1415 isp->smk_inode = sbsp->smk_default;
1416 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001417 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001418 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001419 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001420 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1421 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001422
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001423 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001424}
1425
1426/**
1427 * smack_inode_getsecurity - get smack xattrs
1428 * @inode: the object
1429 * @name: attribute name
1430 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001431 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001432 *
1433 * Returns the size of the attribute or an error code
1434 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001435static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001436 const char *name, void **buffer,
1437 bool alloc)
1438{
1439 struct socket_smack *ssp;
1440 struct socket *sock;
1441 struct super_block *sbp;
1442 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001443 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001444
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001445 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001446 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001447 else {
1448 /*
1449 * The rest of the Smack xattrs are only on sockets.
1450 */
1451 sbp = ip->i_sb;
1452 if (sbp->s_magic != SOCKFS_MAGIC)
1453 return -EOPNOTSUPP;
1454
1455 sock = SOCKET_I(ip);
1456 if (sock == NULL || sock->sk == NULL)
1457 return -EOPNOTSUPP;
1458
1459 ssp = sock->sk->sk_security;
1460
1461 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1462 isp = ssp->smk_in;
1463 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1464 isp = ssp->smk_out;
1465 else
1466 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001467 }
1468
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001469 if (alloc) {
1470 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1471 if (*buffer == NULL)
1472 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001473 }
1474
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001475 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001476}
1477
1478
1479/**
1480 * smack_inode_listsecurity - list the Smack attributes
1481 * @inode: the object
1482 * @buffer: where they go
1483 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001484 */
1485static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1486 size_t buffer_size)
1487{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001488 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001489
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001490 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001491 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001492
1493 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001494}
1495
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001496/**
1497 * smack_inode_getsecid - Extract inode's security id
1498 * @inode: inode to extract the info from
1499 * @secid: where result will be saved
1500 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001501static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001502{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001503 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001504
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001505 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001506}
1507
Casey Schauflere114e472008-02-04 22:29:50 -08001508/*
1509 * File Hooks
1510 */
1511
Casey Schaufler491a0b02016-01-26 15:08:35 -08001512/*
1513 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001514 *
1515 * Should access checks be done on each read or write?
1516 * UNICOS and SELinux say yes.
1517 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1518 *
1519 * I'll say no for now. Smack does not do the frequent
1520 * label changing that SELinux does.
1521 */
Casey Schauflere114e472008-02-04 22:29:50 -08001522
1523/**
1524 * smack_file_alloc_security - assign a file security blob
1525 * @file: the object
1526 *
1527 * The security blob for a file is a pointer to the master
1528 * label list, so no allocation is done.
1529 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001530 * f_security is the owner security information. It
1531 * isn't used on file access checks, it's for send_sigio.
1532 *
Casey Schauflere114e472008-02-04 22:29:50 -08001533 * Returns 0
1534 */
1535static int smack_file_alloc_security(struct file *file)
1536{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001537 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001538
Casey Schauflerf28952a2018-11-12 09:38:53 -08001539 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001540 return 0;
1541}
1542
1543/**
Casey Schauflere114e472008-02-04 22:29:50 -08001544 * smack_file_ioctl - Smack check on ioctls
1545 * @file: the object
1546 * @cmd: what to do
1547 * @arg: unused
1548 *
1549 * Relies heavily on the correct use of the ioctl command conventions.
1550 *
1551 * Returns 0 if allowed, error code otherwise
1552 */
1553static int smack_file_ioctl(struct file *file, unsigned int cmd,
1554 unsigned long arg)
1555{
1556 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001557 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001558 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001559
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001560 if (unlikely(IS_PRIVATE(inode)))
1561 return 0;
1562
Eric Parisf48b7392011-04-25 12:54:27 -04001563 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001564 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001565
Casey Schauflerd166c802014-08-27 14:51:27 -07001566 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001567 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001568 rc = smk_bu_file(file, MAY_WRITE, rc);
1569 }
Casey Schauflere114e472008-02-04 22:29:50 -08001570
Casey Schauflerd166c802014-08-27 14:51:27 -07001571 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001572 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001573 rc = smk_bu_file(file, MAY_READ, rc);
1574 }
Casey Schauflere114e472008-02-04 22:29:50 -08001575
1576 return rc;
1577}
1578
1579/**
1580 * smack_file_lock - Smack check on file locking
1581 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001582 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001583 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001584 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001585 */
1586static int smack_file_lock(struct file *file, unsigned int cmd)
1587{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001588 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001589 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001590 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001591
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001592 if (unlikely(IS_PRIVATE(inode)))
1593 return 0;
1594
Eric Paris92f42502011-04-25 13:15:55 -04001595 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1596 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001597 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001598 rc = smk_bu_file(file, MAY_LOCK, rc);
1599 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001600}
1601
1602/**
1603 * smack_file_fcntl - Smack check on fcntl
1604 * @file: the object
1605 * @cmd: what action to check
1606 * @arg: unused
1607 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001608 * Generally these operations are harmless.
1609 * File locking operations present an obvious mechanism
1610 * for passing information, so they require write access.
1611 *
Casey Schauflere114e472008-02-04 22:29:50 -08001612 * Returns 0 if current has access, error code otherwise
1613 */
1614static int smack_file_fcntl(struct file *file, unsigned int cmd,
1615 unsigned long arg)
1616{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001617 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001618 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001619 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001620
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001621 if (unlikely(IS_PRIVATE(inode)))
1622 return 0;
1623
Casey Schauflere114e472008-02-04 22:29:50 -08001624 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001625 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001626 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001627 case F_SETLK:
1628 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001629 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1630 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001631 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001632 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001633 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001634 case F_SETOWN:
1635 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001636 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1637 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001638 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001639 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001640 break;
1641 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001642 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001643 }
1644
1645 return rc;
1646}
1647
1648/**
Al Viroe5467852012-05-30 13:30:51 -04001649 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001650 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1651 * if mapping anonymous memory.
1652 * @file contains the file structure for file to map (may be NULL).
1653 * @reqprot contains the protection requested by the application.
1654 * @prot contains the protection that will be applied by the kernel.
1655 * @flags contains the operational flags.
1656 * Return 0 if permission is granted.
1657 */
Al Viroe5467852012-05-30 13:30:51 -04001658static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001659 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001660 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001661{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001662 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001663 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001664 struct smack_rule *srp;
1665 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001666 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001667 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001668 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001669 int may;
1670 int mmay;
1671 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001672 int rc;
1673
Al Viro496ad9a2013-01-23 17:07:38 -05001674 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001675 return 0;
1676
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001677 if (unlikely(IS_PRIVATE(file_inode(file))))
1678 return 0;
1679
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001680 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001681 if (isp->smk_mmap == NULL)
1682 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001683 sbsp = file_inode(file)->i_sb->s_security;
1684 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1685 isp->smk_mmap != sbsp->smk_root)
1686 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001687 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001688
Casey Schauflerb17103a2018-11-09 16:12:56 -08001689 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001690 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001691 rc = 0;
1692
1693 rcu_read_lock();
1694 /*
1695 * For each Smack rule associated with the subject
1696 * label verify that the SMACK64MMAP also has access
1697 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001698 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001699 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001700 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001701 /*
1702 * Matching labels always allows access.
1703 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001704 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001705 continue;
1706 /*
1707 * If there is a matching local rule take
1708 * that into account as well.
1709 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001710 may = smk_access_entry(srp->smk_subject->smk_known,
1711 okp->smk_known,
1712 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001713 if (may == -ENOENT)
1714 may = srp->smk_access;
1715 else
1716 may &= srp->smk_access;
1717 /*
1718 * If may is zero the SMACK64MMAP subject can't
1719 * possibly have less access.
1720 */
1721 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001722 continue;
1723
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001724 /*
1725 * Fetch the global list entry.
1726 * If there isn't one a SMACK64MMAP subject
1727 * can't have as much access as current.
1728 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001729 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1730 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001731 if (mmay == -ENOENT) {
1732 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001733 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001734 }
1735 /*
1736 * If there is a local entry it modifies the
1737 * potential access, too.
1738 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001739 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1740 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001741 if (tmay != -ENOENT)
1742 mmay &= tmay;
1743
1744 /*
1745 * If there is any access available to current that is
1746 * not available to a SMACK64MMAP subject
1747 * deny access.
1748 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001749 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001750 rc = -EACCES;
1751 break;
1752 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001753 }
1754
1755 rcu_read_unlock();
1756
1757 return rc;
1758}
1759
1760/**
Casey Schauflere114e472008-02-04 22:29:50 -08001761 * smack_file_set_fowner - set the file security blob value
1762 * @file: object in question
1763 *
Casey Schauflere114e472008-02-04 22:29:50 -08001764 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001765static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001766{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001767 struct smack_known **blob = smack_file(file);
1768
1769 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001770}
1771
1772/**
1773 * smack_file_send_sigiotask - Smack on sigio
1774 * @tsk: The target task
1775 * @fown: the object the signal come from
1776 * @signum: unused
1777 *
1778 * Allow a privileged task to get signals even if it shouldn't
1779 *
1780 * Returns 0 if a subject with the object's smack could
1781 * write to the task, an error code otherwise.
1782 */
1783static int smack_file_send_sigiotask(struct task_struct *tsk,
1784 struct fown_struct *fown, int signum)
1785{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001786 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001787 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001788 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001789 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001790 struct file *file;
1791 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001792 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001793
1794 /*
1795 * struct fown_struct is never outside the context of a struct file
1796 */
1797 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001798
Etienne Bassetecfcc532009-04-08 20:40:06 +02001799 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001800 blob = smack_file(file);
1801 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001802 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1803 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001804
1805 rcu_read_lock();
1806 tcred = __task_cred(tsk);
1807 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001808 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001809 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001810
1811 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1812 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001813 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001814 return rc;
1815}
1816
1817/**
1818 * smack_file_receive - Smack file receive check
1819 * @file: the object
1820 *
1821 * Returns 0 if current has access, error code otherwise
1822 */
1823static int smack_file_receive(struct file *file)
1824{
Casey Schauflerd166c802014-08-27 14:51:27 -07001825 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001826 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001827 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001828 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001829 struct socket *sock;
1830 struct task_smack *tsp;
1831 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001832
Seung-Woo Kim97775822015-04-17 15:25:04 +09001833 if (unlikely(IS_PRIVATE(inode)))
1834 return 0;
1835
Casey Schaufler4482a442013-12-30 17:37:45 -08001836 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001837 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001838
Casey Schaufler51d59af2017-05-31 08:53:42 -07001839 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001840 sock = SOCKET_I(inode);
1841 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001842 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001843 /*
1844 * If the receiving process can't write to the
1845 * passed socket or if the passed socket can't
1846 * write to the receiving process don't accept
1847 * the passed socket.
1848 */
1849 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1850 rc = smk_bu_file(file, may, rc);
1851 if (rc < 0)
1852 return rc;
1853 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1854 rc = smk_bu_file(file, may, rc);
1855 return rc;
1856 }
Casey Schauflere114e472008-02-04 22:29:50 -08001857 /*
1858 * This code relies on bitmasks.
1859 */
1860 if (file->f_mode & FMODE_READ)
1861 may = MAY_READ;
1862 if (file->f_mode & FMODE_WRITE)
1863 may |= MAY_WRITE;
1864
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001865 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001866 rc = smk_bu_file(file, may, rc);
1867 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001868}
1869
Casey Schaufler531f1d42011-09-19 12:41:42 -07001870/**
Eric Paris83d49852012-04-04 13:45:40 -04001871 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001872 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001873 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001874 *
1875 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001876 * Allow the open only if the task has read access. There are
1877 * many read operations (e.g. fstat) that you can do with an
1878 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001879 *
1880 * Returns 0
1881 */
Al Viro94817692018-07-10 14:13:18 -04001882static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001883{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001884 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001885 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001886 struct smk_audit_info ad;
1887 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001888
Casey Schauflera6834c02014-04-21 11:10:26 -07001889 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1890 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301891 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001892 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001893
1894 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001895}
1896
Casey Schauflere114e472008-02-04 22:29:50 -08001897/*
1898 * Task hooks
1899 */
1900
1901/**
David Howellsee18d642009-09-02 09:14:21 +01001902 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1903 * @new: the new credentials
1904 * @gfp: the atomicity of any memory allocations
1905 *
1906 * Prepare a blank set of credentials for modification. This must allocate all
1907 * the memory the LSM module might require such that cred_transfer() can
1908 * complete without error.
1909 */
1910static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1911{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001912 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001913 return 0;
1914}
1915
1916
1917/**
David Howellsf1752ee2008-11-14 10:39:17 +11001918 * smack_cred_free - "free" task-level security credentials
1919 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001920 *
Casey Schauflere114e472008-02-04 22:29:50 -08001921 */
David Howellsf1752ee2008-11-14 10:39:17 +11001922static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001923{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001924 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001925 struct smack_rule *rp;
1926 struct list_head *l;
1927 struct list_head *n;
1928
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001929 smk_destroy_label_list(&tsp->smk_relabel);
1930
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001931 list_for_each_safe(l, n, &tsp->smk_rules) {
1932 rp = list_entry(l, struct smack_rule, list);
1933 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001934 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001935 }
Casey Schauflere114e472008-02-04 22:29:50 -08001936}
1937
1938/**
David Howellsd84f4f92008-11-14 10:39:23 +11001939 * smack_cred_prepare - prepare new set of credentials for modification
1940 * @new: the new credentials
1941 * @old: the original credentials
1942 * @gfp: the atomicity of any memory allocations
1943 *
1944 * Prepare a new set of credentials for modification.
1945 */
1946static int smack_cred_prepare(struct cred *new, const struct cred *old,
1947 gfp_t gfp)
1948{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001949 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001950 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001951 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001952
Casey Schauflerbbd36622018-11-12 09:30:56 -08001953 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301954
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001955 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1956 if (rc != 0)
1957 return rc;
1958
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001959 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1960 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001961 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001962}
1963
Randy Dunlap251a2a92009-02-18 11:42:33 -08001964/**
David Howellsee18d642009-09-02 09:14:21 +01001965 * smack_cred_transfer - Transfer the old credentials to the new credentials
1966 * @new: the new credentials
1967 * @old: the original credentials
1968 *
1969 * Fill in a set of blank credentials from another set of credentials.
1970 */
1971static void smack_cred_transfer(struct cred *new, const struct cred *old)
1972{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001973 struct task_smack *old_tsp = smack_cred(old);
1974 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001975
1976 new_tsp->smk_task = old_tsp->smk_task;
1977 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001978 mutex_init(&new_tsp->smk_rules_lock);
1979 INIT_LIST_HEAD(&new_tsp->smk_rules);
1980
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001981 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001982}
1983
1984/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001985 * smack_cred_getsecid - get the secid corresponding to a creds structure
1986 * @c: the object creds
1987 * @secid: where to put the result
1988 *
1989 * Sets the secid to contain a u32 version of the smack label.
1990 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001991static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001992{
1993 struct smack_known *skp;
1994
1995 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001996 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001997 *secid = skp->smk_secid;
1998 rcu_read_unlock();
1999}
2000
2001/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002002 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002003 * @new: points to the set of credentials to be modified.
2004 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002005 *
2006 * Set the security data for a kernel service.
2007 */
2008static int smack_kernel_act_as(struct cred *new, u32 secid)
2009{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002010 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002011
Casey Schaufler152f91d2016-11-14 09:38:15 -08002012 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002013 return 0;
2014}
2015
2016/**
2017 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002018 * @new: points to the set of credentials to be modified
2019 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002020 *
2021 * Set the file creation context in a set of credentials to the same
2022 * as the objective context of the specified inode
2023 */
2024static int smack_kernel_create_files_as(struct cred *new,
2025 struct inode *inode)
2026{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002027 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002028 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002029
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002030 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002031 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002032 return 0;
2033}
2034
2035/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002036 * smk_curacc_on_task - helper to log task related access
2037 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002038 * @access: the access requested
2039 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002040 *
2041 * Return 0 if access is permitted
2042 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002043static int smk_curacc_on_task(struct task_struct *p, int access,
2044 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002045{
2046 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002047 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002048 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002049
Casey Schaufler531f1d42011-09-19 12:41:42 -07002050 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002051 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002052 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002053 rc = smk_bu_task(p, access, rc);
2054 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002055}
2056
2057/**
Casey Schauflere114e472008-02-04 22:29:50 -08002058 * smack_task_setpgid - Smack check on setting pgid
2059 * @p: the task object
2060 * @pgid: unused
2061 *
2062 * Return 0 if write access is permitted
2063 */
2064static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2065{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002066 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002067}
2068
2069/**
2070 * smack_task_getpgid - Smack access check for getpgid
2071 * @p: the object task
2072 *
2073 * Returns 0 if current can read the object task, error code otherwise
2074 */
2075static int smack_task_getpgid(struct task_struct *p)
2076{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002077 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002078}
2079
2080/**
2081 * smack_task_getsid - Smack access check for getsid
2082 * @p: the object task
2083 *
2084 * Returns 0 if current can read the object task, error code otherwise
2085 */
2086static int smack_task_getsid(struct task_struct *p)
2087{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002088 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002089}
2090
2091/**
2092 * smack_task_getsecid - get the secid of the task
2093 * @p: the object task
2094 * @secid: where to put the result
2095 *
2096 * Sets the secid to contain a u32 version of the smack label.
2097 */
2098static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2099{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002100 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002101
2102 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002103}
2104
2105/**
2106 * smack_task_setnice - Smack check on setting nice
2107 * @p: the task object
2108 * @nice: unused
2109 *
2110 * Return 0 if write access is permitted
2111 */
2112static int smack_task_setnice(struct task_struct *p, int nice)
2113{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002114 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002115}
2116
2117/**
2118 * smack_task_setioprio - Smack check on setting ioprio
2119 * @p: the task object
2120 * @ioprio: unused
2121 *
2122 * Return 0 if write access is permitted
2123 */
2124static int smack_task_setioprio(struct task_struct *p, int ioprio)
2125{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002126 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002127}
2128
2129/**
2130 * smack_task_getioprio - Smack check on reading ioprio
2131 * @p: the task object
2132 *
2133 * Return 0 if read access is permitted
2134 */
2135static int smack_task_getioprio(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_setscheduler - Smack check on setting scheduler
2142 * @p: the task object
2143 * @policy: unused
2144 * @lp: unused
2145 *
2146 * Return 0 if read access is permitted
2147 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002148static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002149{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002150 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002151}
2152
2153/**
2154 * smack_task_getscheduler - Smack check on reading scheduler
2155 * @p: the task object
2156 *
2157 * Return 0 if read access is permitted
2158 */
2159static int smack_task_getscheduler(struct task_struct *p)
2160{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002161 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002162}
2163
2164/**
2165 * smack_task_movememory - Smack check on moving memory
2166 * @p: the task object
2167 *
2168 * Return 0 if write access is permitted
2169 */
2170static int smack_task_movememory(struct task_struct *p)
2171{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002172 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002173}
2174
2175/**
2176 * smack_task_kill - Smack check on signal delivery
2177 * @p: the task object
2178 * @info: unused
2179 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002180 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002181 *
2182 * Return 0 if write access is permitted
2183 *
Casey Schauflere114e472008-02-04 22:29:50 -08002184 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002185static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002186 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002187{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002188 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002189 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002190 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002191 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002192
Rafal Krypa18d872f2016-04-04 11:14:53 +02002193 if (!sig)
2194 return 0; /* null signal; existence test */
2195
Etienne Bassetecfcc532009-04-08 20:40:06 +02002196 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2197 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002198 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002199 * Sending a signal requires that the sender
2200 * can write the receiver.
2201 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002202 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002203 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2204 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002205 return rc;
2206 }
Casey Schauflere114e472008-02-04 22:29:50 -08002207 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002208 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002209 * specific behavior. This is not clean. For one thing
2210 * we can't take privilege into account.
2211 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002212 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002213 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2214 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002215 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002216}
2217
2218/**
Casey Schauflere114e472008-02-04 22:29:50 -08002219 * smack_task_to_inode - copy task smack into the inode blob
2220 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002221 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002222 *
2223 * Sets the smack pointer in the inode security blob
2224 */
2225static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2226{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002227 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002228 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002229
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002230 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002231 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002232}
2233
2234/*
2235 * Socket hooks.
2236 */
2237
2238/**
2239 * smack_sk_alloc_security - Allocate a socket blob
2240 * @sk: the socket
2241 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002242 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002243 *
2244 * Assign Smack pointers to current
2245 *
2246 * Returns 0 on success, -ENOMEM is there's no memory
2247 */
2248static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2249{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002250 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002251 struct socket_smack *ssp;
2252
2253 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2254 if (ssp == NULL)
2255 return -ENOMEM;
2256
jooseong lee08382c92016-11-03 11:54:39 +01002257 /*
2258 * Sockets created by kernel threads receive web label.
2259 */
2260 if (unlikely(current->flags & PF_KTHREAD)) {
2261 ssp->smk_in = &smack_known_web;
2262 ssp->smk_out = &smack_known_web;
2263 } else {
2264 ssp->smk_in = skp;
2265 ssp->smk_out = skp;
2266 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002267 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002268
2269 sk->sk_security = ssp;
2270
2271 return 0;
2272}
2273
2274/**
2275 * smack_sk_free_security - Free a socket blob
2276 * @sk: the socket
2277 *
2278 * Clears the blob pointer
2279 */
2280static void smack_sk_free_security(struct sock *sk)
2281{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302282#ifdef SMACK_IPV6_PORT_LABELING
2283 struct smk_port_label *spp;
2284
2285 if (sk->sk_family == PF_INET6) {
2286 rcu_read_lock();
2287 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2288 if (spp->smk_sock != sk)
2289 continue;
2290 spp->smk_can_reuse = 1;
2291 break;
2292 }
2293 rcu_read_unlock();
2294 }
2295#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002296 kfree(sk->sk_security);
2297}
2298
2299/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002300* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002301* @sip: the object end
2302*
2303* looks for host based access restrictions
2304*
2305* This version will only be appropriate for really small sets of single label
2306* hosts. The caller is responsible for ensuring that the RCU read lock is
2307* taken before calling this function.
2308*
2309* Returns the label of the far end or NULL if it's not special.
2310*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002311static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002312{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002313 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002314 struct in_addr *siap = &sip->sin_addr;
2315
2316 if (siap->s_addr == 0)
2317 return NULL;
2318
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002319 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2320 /*
2321 * we break after finding the first match because
2322 * the list is sorted from longest to shortest mask
2323 * so we have found the most specific match
2324 */
2325 if (snp->smk_host.s_addr ==
2326 (siap->s_addr & snp->smk_mask.s_addr))
2327 return snp->smk_label;
2328
2329 return NULL;
2330}
2331
2332#if IS_ENABLED(CONFIG_IPV6)
2333/*
2334 * smk_ipv6_localhost - Check for local ipv6 host address
2335 * @sip: the address
2336 *
2337 * Returns boolean true if this is the localhost address
2338 */
2339static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2340{
2341 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2342 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2343
2344 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2345 ntohs(be16p[7]) == 1)
2346 return true;
2347 return false;
2348}
2349
2350/**
2351* smack_ipv6host_label - check host based restrictions
2352* @sip: the object end
2353*
2354* looks for host based access restrictions
2355*
2356* This version will only be appropriate for really small sets of single label
2357* hosts. The caller is responsible for ensuring that the RCU read lock is
2358* taken before calling this function.
2359*
2360* Returns the label of the far end or NULL if it's not special.
2361*/
2362static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2363{
2364 struct smk_net6addr *snp;
2365 struct in6_addr *sap = &sip->sin6_addr;
2366 int i;
2367 int found = 0;
2368
2369 /*
2370 * It's local. Don't look for a host label.
2371 */
2372 if (smk_ipv6_localhost(sip))
2373 return NULL;
2374
2375 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002376 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002377 * If the label is NULL the entry has
2378 * been renounced. Ignore it.
2379 */
2380 if (snp->smk_label == NULL)
2381 continue;
2382 /*
Paul Moore07feee82009-03-27 17:10:54 -04002383 * we break after finding the first match because
2384 * the list is sorted from longest to shortest mask
2385 * so we have found the most specific match
2386 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002387 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002388 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2389 snp->smk_host.s6_addr16[i]) {
2390 found = 0;
2391 break;
2392 }
Etienne Basset43031542009-03-27 17:11:01 -04002393 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002394 if (found)
2395 return snp->smk_label;
2396 }
Paul Moore07feee82009-03-27 17:10:54 -04002397
2398 return NULL;
2399}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002400#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002401
2402/**
Casey Schauflere114e472008-02-04 22:29:50 -08002403 * smack_netlabel - Set the secattr on a socket
2404 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002405 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002406 *
2407 * Convert the outbound smack value (smk_out) to a
2408 * secattr and attach it to the socket.
2409 *
2410 * Returns 0 on success or an error code
2411 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002412static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002413{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002414 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002415 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002416 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002417
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002418 /*
2419 * Usually the netlabel code will handle changing the
2420 * packet labeling based on the label.
2421 * The case of a single label host is different, because
2422 * a single label host should never get a labeled packet
2423 * even though the label is usually associated with a packet
2424 * label.
2425 */
2426 local_bh_disable();
2427 bh_lock_sock_nested(sk);
2428
2429 if (ssp->smk_out == smack_net_ambient ||
2430 labeled == SMACK_UNLABELED_SOCKET)
2431 netlbl_sock_delattr(sk);
2432 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002433 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002434 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002435 }
2436
2437 bh_unlock_sock(sk);
2438 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002439
Casey Schauflere114e472008-02-04 22:29:50 -08002440 return rc;
2441}
2442
2443/**
Paul Moore07feee82009-03-27 17:10:54 -04002444 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2445 * @sk: the socket
2446 * @sap: the destination address
2447 *
2448 * Set the correct secattr for the given socket based on the destination
2449 * address and perform any outbound access checks needed.
2450 *
2451 * Returns 0 on success or an error code.
2452 *
2453 */
2454static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2455{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002456 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002457 int rc;
2458 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002459 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002460 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002461 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002462
2463 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002464 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002465 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002466#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002467 struct lsm_network_audit net;
2468
Eric Paris48c62af2012-04-02 13:15:44 -04002469 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2470 ad.a.u.net->family = sap->sin_family;
2471 ad.a.u.net->dport = sap->sin_port;
2472 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002473#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002474 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002475 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002476 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2477 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002478 } else {
2479 sk_lbl = SMACK_CIPSO_SOCKET;
2480 rc = 0;
2481 }
2482 rcu_read_unlock();
2483 if (rc != 0)
2484 return rc;
2485
2486 return smack_netlabel(sk, sk_lbl);
2487}
2488
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002489#if IS_ENABLED(CONFIG_IPV6)
2490/**
2491 * smk_ipv6_check - check Smack access
2492 * @subject: subject Smack label
2493 * @object: object Smack label
2494 * @address: address
2495 * @act: the action being taken
2496 *
2497 * Check an IPv6 access
2498 */
2499static int smk_ipv6_check(struct smack_known *subject,
2500 struct smack_known *object,
2501 struct sockaddr_in6 *address, int act)
2502{
2503#ifdef CONFIG_AUDIT
2504 struct lsm_network_audit net;
2505#endif
2506 struct smk_audit_info ad;
2507 int rc;
2508
2509#ifdef CONFIG_AUDIT
2510 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2511 ad.a.u.net->family = PF_INET6;
2512 ad.a.u.net->dport = ntohs(address->sin6_port);
2513 if (act == SMK_RECEIVING)
2514 ad.a.u.net->v6info.saddr = address->sin6_addr;
2515 else
2516 ad.a.u.net->v6info.daddr = address->sin6_addr;
2517#endif
2518 rc = smk_access(subject, object, MAY_WRITE, &ad);
2519 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2520 return rc;
2521}
2522#endif /* CONFIG_IPV6 */
2523
2524#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002525/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002526 * smk_ipv6_port_label - Smack port access table management
2527 * @sock: socket
2528 * @address: address
2529 *
2530 * Create or update the port list entry
2531 */
2532static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2533{
2534 struct sock *sk = sock->sk;
2535 struct sockaddr_in6 *addr6;
2536 struct socket_smack *ssp = sock->sk->sk_security;
2537 struct smk_port_label *spp;
2538 unsigned short port = 0;
2539
2540 if (address == NULL) {
2541 /*
2542 * This operation is changing the Smack information
2543 * on the bound socket. Take the changes to the port
2544 * as well.
2545 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302546 rcu_read_lock();
2547 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002548 if (sk != spp->smk_sock)
2549 continue;
2550 spp->smk_in = ssp->smk_in;
2551 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302552 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002553 return;
2554 }
2555 /*
2556 * A NULL address is only used for updating existing
2557 * bound entries. If there isn't one, it's OK.
2558 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302559 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002560 return;
2561 }
2562
2563 addr6 = (struct sockaddr_in6 *)address;
2564 port = ntohs(addr6->sin6_port);
2565 /*
2566 * This is a special case that is safely ignored.
2567 */
2568 if (port == 0)
2569 return;
2570
2571 /*
2572 * Look for an existing port list entry.
2573 * This is an indication that a port is getting reused.
2574 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302575 rcu_read_lock();
2576 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302577 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002578 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302579 if (spp->smk_can_reuse != 1) {
2580 rcu_read_unlock();
2581 return;
2582 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002583 spp->smk_port = port;
2584 spp->smk_sock = sk;
2585 spp->smk_in = ssp->smk_in;
2586 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302587 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302588 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002589 return;
2590 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302591 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002592 /*
2593 * A new port entry is required.
2594 */
2595 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2596 if (spp == NULL)
2597 return;
2598
2599 spp->smk_port = port;
2600 spp->smk_sock = sk;
2601 spp->smk_in = ssp->smk_in;
2602 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302603 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302604 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002605
Vishal Goel3c7ce342016-11-23 10:31:08 +05302606 mutex_lock(&smack_ipv6_lock);
2607 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2608 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002609 return;
2610}
2611
2612/**
2613 * smk_ipv6_port_check - check Smack port access
2614 * @sock: socket
2615 * @address: address
2616 *
2617 * Create or update the port list entry
2618 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002619static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002620 int act)
2621{
Casey Schauflerc6739442013-05-22 18:42:56 -07002622 struct smk_port_label *spp;
2623 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002624 struct smack_known *skp = NULL;
2625 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002626 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002627
2628 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002629 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002630 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002631 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002632 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002633 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002634 }
2635
2636 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002637 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002638 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002639 if (skp != NULL && object != NULL)
2640 return smk_ipv6_check(skp, object, address, act);
2641 if (skp == NULL)
2642 skp = smack_net_ambient;
2643 if (object == NULL)
2644 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002645
2646 /*
2647 * It's remote, so port lookup does no good.
2648 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002649 if (!smk_ipv6_localhost(address))
2650 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002651
2652 /*
2653 * It's local so the send check has to have passed.
2654 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002655 if (act == SMK_RECEIVING)
2656 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002657
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002658 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302659 rcu_read_lock();
2660 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302661 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002662 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002663 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002664 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002665 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002666 break;
2667 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302668 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002669
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002670 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002671}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002672#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002673
2674/**
Casey Schauflere114e472008-02-04 22:29:50 -08002675 * smack_inode_setsecurity - set smack xattrs
2676 * @inode: the object
2677 * @name: attribute name
2678 * @value: attribute value
2679 * @size: size of the attribute
2680 * @flags: unused
2681 *
2682 * Sets the named attribute in the appropriate blob
2683 *
2684 * Returns 0 on success, or an error code
2685 */
2686static int smack_inode_setsecurity(struct inode *inode, const char *name,
2687 const void *value, size_t size, int flags)
2688{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002689 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002690 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002691 struct socket_smack *ssp;
2692 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002693 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002694
Casey Schauflerf7112e62012-05-06 15:22:02 -07002695 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302696 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002697
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002698 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002699 if (IS_ERR(skp))
2700 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002701
2702 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002703 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002704 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002705 return 0;
2706 }
2707 /*
2708 * The rest of the Smack xattrs are only on sockets.
2709 */
2710 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2711 return -EOPNOTSUPP;
2712
2713 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002714 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002715 return -EOPNOTSUPP;
2716
2717 ssp = sock->sk->sk_security;
2718
2719 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002720 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002721 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002722 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002723 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002724 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2725 if (rc != 0)
2726 printk(KERN_WARNING
2727 "Smack: \"%s\" netlbl error %d.\n",
2728 __func__, -rc);
2729 }
Casey Schauflere114e472008-02-04 22:29:50 -08002730 } else
2731 return -EOPNOTSUPP;
2732
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002733#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002734 if (sock->sk->sk_family == PF_INET6)
2735 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002736#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002737
Casey Schauflere114e472008-02-04 22:29:50 -08002738 return 0;
2739}
2740
2741/**
2742 * smack_socket_post_create - finish socket setup
2743 * @sock: the socket
2744 * @family: protocol family
2745 * @type: unused
2746 * @protocol: unused
2747 * @kern: unused
2748 *
2749 * Sets the netlabel information on the socket
2750 *
2751 * Returns 0 on success, and error code otherwise
2752 */
2753static int smack_socket_post_create(struct socket *sock, int family,
2754 int type, int protocol, int kern)
2755{
Marcin Lis74123012015-01-22 15:40:33 +01002756 struct socket_smack *ssp;
2757
2758 if (sock->sk == NULL)
2759 return 0;
2760
2761 /*
2762 * Sockets created by kernel threads receive web label.
2763 */
2764 if (unlikely(current->flags & PF_KTHREAD)) {
2765 ssp = sock->sk->sk_security;
2766 ssp->smk_in = &smack_known_web;
2767 ssp->smk_out = &smack_known_web;
2768 }
2769
2770 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002771 return 0;
2772 /*
2773 * Set the outbound netlbl.
2774 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002775 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2776}
2777
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002778/**
2779 * smack_socket_socketpair - create socket pair
2780 * @socka: one socket
2781 * @sockb: another socket
2782 *
2783 * Cross reference the peer labels for SO_PEERSEC
2784 *
2785 * Returns 0 on success, and error code otherwise
2786 */
2787static int smack_socket_socketpair(struct socket *socka,
2788 struct socket *sockb)
2789{
2790 struct socket_smack *asp = socka->sk->sk_security;
2791 struct socket_smack *bsp = sockb->sk->sk_security;
2792
2793 asp->smk_packet = bsp->smk_out;
2794 bsp->smk_packet = asp->smk_out;
2795
2796 return 0;
2797}
2798
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002799#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002800/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002801 * smack_socket_bind - record port binding information.
2802 * @sock: the socket
2803 * @address: the port address
2804 * @addrlen: size of the address
2805 *
2806 * Records the label bound to a port.
2807 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002808 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002809 */
2810static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2811 int addrlen)
2812{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002813 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2814 if (addrlen < SIN6_LEN_RFC2133 ||
2815 address->sa_family != AF_INET6)
2816 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002817 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002818 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002819 return 0;
2820}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002821#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002822
2823/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002824 * smack_socket_connect - connect access check
2825 * @sock: the socket
2826 * @sap: the other end
2827 * @addrlen: size of sap
2828 *
2829 * Verifies that a connection may be possible
2830 *
2831 * Returns 0 on success, and error code otherwise
2832 */
2833static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2834 int addrlen)
2835{
Casey Schauflerc6739442013-05-22 18:42:56 -07002836 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002837#if IS_ENABLED(CONFIG_IPV6)
2838 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2839#endif
2840#ifdef SMACK_IPV6_SECMARK_LABELING
2841 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002842 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002843#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002844
Casey Schauflerc6739442013-05-22 18:42:56 -07002845 if (sock->sk == NULL)
2846 return 0;
2847
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002848#ifdef SMACK_IPV6_SECMARK_LABELING
2849 ssp = sock->sk->sk_security;
2850#endif
2851
Casey Schauflerc6739442013-05-22 18:42:56 -07002852 switch (sock->sk->sk_family) {
2853 case PF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002854 if (addrlen < sizeof(struct sockaddr_in) ||
2855 sap->sa_family != AF_INET)
Casey Schauflerc6739442013-05-22 18:42:56 -07002856 return -EINVAL;
2857 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2858 break;
2859 case PF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002860 if (addrlen < SIN6_LEN_RFC2133 || sap->sa_family != AF_INET6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002861 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002862#ifdef SMACK_IPV6_SECMARK_LABELING
2863 rsp = smack_ipv6host_label(sip);
2864 if (rsp != NULL)
2865 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002866 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002867#endif
2868#ifdef SMACK_IPV6_PORT_LABELING
2869 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2870#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002871 break;
2872 }
2873 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002874}
2875
2876/**
2877 * smack_flags_to_may - convert S_ to MAY_ values
2878 * @flags: the S_ value
2879 *
2880 * Returns the equivalent MAY_ value
2881 */
2882static int smack_flags_to_may(int flags)
2883{
2884 int may = 0;
2885
2886 if (flags & S_IRUGO)
2887 may |= MAY_READ;
2888 if (flags & S_IWUGO)
2889 may |= MAY_WRITE;
2890 if (flags & S_IXUGO)
2891 may |= MAY_EXEC;
2892
2893 return may;
2894}
2895
2896/**
2897 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2898 * @msg: the object
2899 *
2900 * Returns 0
2901 */
2902static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2903{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002904 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002905
Casey Schauflerecd5f822018-11-20 11:55:02 -08002906 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002907 return 0;
2908}
2909
2910/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002911 * smack_of_ipc - the smack pointer for the ipc
2912 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002913 *
2914 * Returns a pointer to the smack value
2915 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002916static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002917{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002918 struct smack_known **blob = smack_ipc(isp);
2919
2920 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002921}
2922
2923/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002924 * smack_ipc_alloc_security - Set the security blob for ipc
2925 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002926 *
2927 * Returns 0
2928 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002929static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002930{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002931 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002932
Casey Schaufler019bcca2018-09-21 17:19:54 -07002933 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002934 return 0;
2935}
2936
2937/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002938 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002939 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002940 * @access : access requested
2941 *
2942 * Returns 0 if current has the requested access, error code otherwise
2943 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002944static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002945{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002946 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002947 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002948 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002949
2950#ifdef CONFIG_AUDIT
2951 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002952 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002953#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002954 rc = smk_curacc(ssp, access, &ad);
2955 rc = smk_bu_current("shm", ssp, access, rc);
2956 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002957}
2958
2959/**
Casey Schauflere114e472008-02-04 22:29:50 -08002960 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002961 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002962 * @shmflg: access requested
2963 *
2964 * Returns 0 if current has the requested access, error code otherwise
2965 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002966static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002967{
Casey Schauflere114e472008-02-04 22:29:50 -08002968 int may;
2969
2970 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002971 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002972}
2973
2974/**
2975 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002976 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002977 * @cmd: what it wants to do
2978 *
2979 * Returns 0 if current has the requested access, error code otherwise
2980 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002981static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002982{
Casey Schauflere114e472008-02-04 22:29:50 -08002983 int may;
2984
2985 switch (cmd) {
2986 case IPC_STAT:
2987 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002988 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002989 may = MAY_READ;
2990 break;
2991 case IPC_SET:
2992 case SHM_LOCK:
2993 case SHM_UNLOCK:
2994 case IPC_RMID:
2995 may = MAY_READWRITE;
2996 break;
2997 case IPC_INFO:
2998 case SHM_INFO:
2999 /*
3000 * System level information.
3001 */
3002 return 0;
3003 default:
3004 return -EINVAL;
3005 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003006 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003007}
3008
3009/**
3010 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003011 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003012 * @shmaddr: unused
3013 * @shmflg: access requested
3014 *
3015 * Returns 0 if current has the requested access, error code otherwise
3016 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003017static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003018 int shmflg)
3019{
Casey Schauflere114e472008-02-04 22:29:50 -08003020 int may;
3021
3022 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003023 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003024}
3025
3026/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003027 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003028 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003029 * @access : access requested
3030 *
3031 * Returns 0 if current has the requested access, error code otherwise
3032 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003033static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003034{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003035 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003036 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003037 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003038
3039#ifdef CONFIG_AUDIT
3040 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003041 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003042#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003043 rc = smk_curacc(ssp, access, &ad);
3044 rc = smk_bu_current("sem", ssp, access, rc);
3045 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003046}
3047
3048/**
Casey Schauflere114e472008-02-04 22:29:50 -08003049 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003050 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003051 * @semflg: access requested
3052 *
3053 * Returns 0 if current has the requested access, error code otherwise
3054 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003055static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003056{
Casey Schauflere114e472008-02-04 22:29:50 -08003057 int may;
3058
3059 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003060 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003061}
3062
3063/**
3064 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003065 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003066 * @cmd: what it wants to do
3067 *
3068 * Returns 0 if current has the requested access, error code otherwise
3069 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003070static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003071{
Casey Schauflere114e472008-02-04 22:29:50 -08003072 int may;
3073
3074 switch (cmd) {
3075 case GETPID:
3076 case GETNCNT:
3077 case GETZCNT:
3078 case GETVAL:
3079 case GETALL:
3080 case IPC_STAT:
3081 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003082 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003083 may = MAY_READ;
3084 break;
3085 case SETVAL:
3086 case SETALL:
3087 case IPC_RMID:
3088 case IPC_SET:
3089 may = MAY_READWRITE;
3090 break;
3091 case IPC_INFO:
3092 case SEM_INFO:
3093 /*
3094 * System level information
3095 */
3096 return 0;
3097 default:
3098 return -EINVAL;
3099 }
3100
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003101 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003102}
3103
3104/**
3105 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003106 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003107 * @sops: unused
3108 * @nsops: unused
3109 * @alter: unused
3110 *
3111 * Treated as read and write in all cases.
3112 *
3113 * Returns 0 if access is allowed, error code otherwise
3114 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003115static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003116 unsigned nsops, int alter)
3117{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003118 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003119}
3120
3121/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003122 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003123 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003124 * @access : access requested
3125 *
3126 * return 0 if current has access, error otherwise
3127 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003128static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003129{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003130 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003131 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003132 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003133
3134#ifdef CONFIG_AUDIT
3135 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003136 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003137#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003138 rc = smk_curacc(msp, access, &ad);
3139 rc = smk_bu_current("msq", msp, access, rc);
3140 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003141}
3142
3143/**
Casey Schauflere114e472008-02-04 22:29:50 -08003144 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003145 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003146 * @msqflg: access requested
3147 *
3148 * Returns 0 if current has the requested access, error code otherwise
3149 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003150static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003151{
Casey Schauflere114e472008-02-04 22:29:50 -08003152 int may;
3153
3154 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003155 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003156}
3157
3158/**
3159 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003160 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003161 * @cmd: what it wants to do
3162 *
3163 * Returns 0 if current has the requested access, error code otherwise
3164 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003165static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003166{
Casey Schauflere114e472008-02-04 22:29:50 -08003167 int may;
3168
3169 switch (cmd) {
3170 case IPC_STAT:
3171 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003172 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003173 may = MAY_READ;
3174 break;
3175 case IPC_SET:
3176 case IPC_RMID:
3177 may = MAY_READWRITE;
3178 break;
3179 case IPC_INFO:
3180 case MSG_INFO:
3181 /*
3182 * System level information
3183 */
3184 return 0;
3185 default:
3186 return -EINVAL;
3187 }
3188
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003189 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003190}
3191
3192/**
3193 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003194 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003195 * @msg: unused
3196 * @msqflg: access requested
3197 *
3198 * Returns 0 if current has the requested access, error code otherwise
3199 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003200static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003201 int msqflg)
3202{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003203 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003204
Etienne Bassetecfcc532009-04-08 20:40:06 +02003205 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003206 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003207}
3208
3209/**
3210 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003211 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003212 * @msg: unused
3213 * @target: unused
3214 * @type: unused
3215 * @mode: unused
3216 *
3217 * Returns 0 if current has read and write access, error code otherwise
3218 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003219static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003220 struct task_struct *target, long type, int mode)
3221{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003222 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003223}
3224
3225/**
3226 * smack_ipc_permission - Smack access for ipc_permission()
3227 * @ipp: the object permissions
3228 * @flag: access requested
3229 *
3230 * Returns 0 if current has read and write access, error code otherwise
3231 */
3232static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3233{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003234 struct smack_known **blob = smack_ipc(ipp);
3235 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003236 int may = smack_flags_to_may(flag);
3237 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003238 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003239
Etienne Bassetecfcc532009-04-08 20:40:06 +02003240#ifdef CONFIG_AUDIT
3241 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3242 ad.a.u.ipc_id = ipp->id;
3243#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003244 rc = smk_curacc(iskp, may, &ad);
3245 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003246 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003247}
3248
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003249/**
3250 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003251 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003252 * @secid: where result will be saved
3253 */
3254static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3255{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003256 struct smack_known **blob = smack_ipc(ipp);
3257 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003258
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003259 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003260}
3261
Casey Schauflere114e472008-02-04 22:29:50 -08003262/**
3263 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003264 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003265 * @inode: the object
3266 *
3267 * Set the inode's security blob if it hasn't been done already.
3268 */
3269static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3270{
3271 struct super_block *sbp;
3272 struct superblock_smack *sbsp;
3273 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003274 struct smack_known *skp;
3275 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003276 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003277 char trattr[TRANS_TRUE_SIZE];
3278 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003279 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003280 struct dentry *dp;
3281
3282 if (inode == NULL)
3283 return;
3284
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003285 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003286
3287 mutex_lock(&isp->smk_lock);
3288 /*
3289 * If the inode is already instantiated
3290 * take the quick way out
3291 */
3292 if (isp->smk_flags & SMK_INODE_INSTANT)
3293 goto unlockandout;
3294
3295 sbp = inode->i_sb;
3296 sbsp = sbp->s_security;
3297 /*
3298 * We're going to use the superblock default label
3299 * if there's no label on the file.
3300 */
3301 final = sbsp->smk_default;
3302
3303 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003304 * If this is the root inode the superblock
3305 * may be in the process of initialization.
3306 * If that is the case use the root value out
3307 * of the superblock.
3308 */
3309 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003310 switch (sbp->s_magic) {
3311 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003312 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003313 /*
3314 * The cgroup filesystem is never mounted,
3315 * so there's no opportunity to set the mount
3316 * options.
3317 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003318 sbsp->smk_root = &smack_known_star;
3319 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003320 isp->smk_inode = sbsp->smk_root;
3321 break;
3322 case TMPFS_MAGIC:
3323 /*
3324 * What about shmem/tmpfs anonymous files with dentry
3325 * obtained from d_alloc_pseudo()?
3326 */
3327 isp->smk_inode = smk_of_current();
3328 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003329 case PIPEFS_MAGIC:
3330 isp->smk_inode = smk_of_current();
3331 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003332 case SOCKFS_MAGIC:
3333 /*
3334 * Socket access is controlled by the socket
3335 * structures associated with the task involved.
3336 */
3337 isp->smk_inode = &smack_known_star;
3338 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003339 default:
3340 isp->smk_inode = sbsp->smk_root;
3341 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003342 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003343 isp->smk_flags |= SMK_INODE_INSTANT;
3344 goto unlockandout;
3345 }
3346
3347 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003348 * This is pretty hackish.
3349 * Casey says that we shouldn't have to do
3350 * file system specific code, but it does help
3351 * with keeping it simple.
3352 */
3353 switch (sbp->s_magic) {
3354 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003355 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003356 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003357 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003358 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003359 * that the smack file system doesn't do
3360 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003361 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003362 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003363 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003364 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003365 break;
3366 case DEVPTS_SUPER_MAGIC:
3367 /*
3368 * devpts seems content with the label of the task.
3369 * Programs that change smack have to treat the
3370 * pty with respect.
3371 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003372 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003373 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003374 case PROC_SUPER_MAGIC:
3375 /*
3376 * Casey says procfs appears not to care.
3377 * The superblock default suffices.
3378 */
3379 break;
3380 case TMPFS_MAGIC:
3381 /*
3382 * Device labels should come from the filesystem,
3383 * but watch out, because they're volitile,
3384 * getting recreated on every reboot.
3385 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003386 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003387 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003388 * If a smack value has been set we want to use it,
3389 * but since tmpfs isn't giving us the opportunity
3390 * to set mount options simulate setting the
3391 * superblock default.
3392 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003393 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003394 default:
3395 /*
3396 * This isn't an understood special case.
3397 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003398 */
3399
3400 /*
3401 * UNIX domain sockets use lower level socket data.
3402 */
3403 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003404 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003405 break;
3406 }
3407 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003408 * No xattr support means, alas, no SMACK label.
3409 * Use the aforeapplied default.
3410 * It would be curious if the label of the task
3411 * does not match that assigned.
3412 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003413 if (!(inode->i_opflags & IOP_XATTR))
3414 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003415 /*
3416 * Get the dentry for xattr.
3417 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003418 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003419 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003420 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003421 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003422
3423 /*
3424 * Transmuting directory
3425 */
3426 if (S_ISDIR(inode->i_mode)) {
3427 /*
3428 * If this is a new directory and the label was
3429 * transmuted when the inode was initialized
3430 * set the transmute attribute on the directory
3431 * and mark the inode.
3432 *
3433 * If there is a transmute attribute on the
3434 * directory mark the inode.
3435 */
3436 if (isp->smk_flags & SMK_INODE_CHANGED) {
3437 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003438 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003439 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003440 TRANS_TRUE, TRANS_TRUE_SIZE,
3441 0);
3442 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003443 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003444 XATTR_NAME_SMACKTRANSMUTE, trattr,
3445 TRANS_TRUE_SIZE);
3446 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3447 TRANS_TRUE_SIZE) != 0)
3448 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003449 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003450 if (rc >= 0)
3451 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003452 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003453 /*
3454 * Don't let the exec or mmap label be "*" or "@".
3455 */
3456 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3457 if (IS_ERR(skp) || skp == &smack_known_star ||
3458 skp == &smack_known_web)
3459 skp = NULL;
3460 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003461
Casey Schaufler19760ad2013-12-16 16:27:26 -08003462 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003463 if (IS_ERR(skp) || skp == &smack_known_star ||
3464 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003465 skp = NULL;
3466 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003467
Casey Schauflere114e472008-02-04 22:29:50 -08003468 dput(dp);
3469 break;
3470 }
3471
3472 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003473 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003474 else
3475 isp->smk_inode = final;
3476
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003477 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003478
3479unlockandout:
3480 mutex_unlock(&isp->smk_lock);
3481 return;
3482}
3483
3484/**
3485 * smack_getprocattr - Smack process attribute access
3486 * @p: the object task
3487 * @name: the name of the attribute in /proc/.../attr
3488 * @value: where to put the result
3489 *
3490 * Places a copy of the task Smack into value
3491 *
3492 * Returns the length of the smack label or an error code
3493 */
3494static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3495{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003496 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003497 char *cp;
3498 int slen;
3499
3500 if (strcmp(name, "current") != 0)
3501 return -EINVAL;
3502
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003503 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003504 if (cp == NULL)
3505 return -ENOMEM;
3506
3507 slen = strlen(cp);
3508 *value = cp;
3509 return slen;
3510}
3511
3512/**
3513 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003514 * @name: the name of the attribute in /proc/.../attr
3515 * @value: the value to set
3516 * @size: the size of the value
3517 *
3518 * Sets the Smack value of the task. Only setting self
3519 * is permitted and only with privilege
3520 *
3521 * Returns the length of the smack label or an error code
3522 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003523static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003524{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003525 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003526 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003527 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003528 struct smack_known_list_elem *sklep;
3529 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003530
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003531 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003532 return -EPERM;
3533
Casey Schauflerf7112e62012-05-06 15:22:02 -07003534 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003535 return -EINVAL;
3536
3537 if (strcmp(name, "current") != 0)
3538 return -EINVAL;
3539
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003540 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003541 if (IS_ERR(skp))
3542 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003543
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003544 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303545 * No process is ever allowed the web ("@") label
3546 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003547 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303548 if (skp == &smack_known_web || skp == &smack_known_star)
3549 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003550
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003551 if (!smack_privileged(CAP_MAC_ADMIN)) {
3552 rc = -EPERM;
3553 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3554 if (sklep->smk_label == skp) {
3555 rc = 0;
3556 break;
3557 }
3558 if (rc)
3559 return rc;
3560 }
3561
David Howellsd84f4f92008-11-14 10:39:23 +11003562 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003563 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003564 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003565
Casey Schauflerb17103a2018-11-09 16:12:56 -08003566 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003567 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003568 /*
3569 * process can change its label only once
3570 */
3571 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003572
David Howellsd84f4f92008-11-14 10:39:23 +11003573 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003574 return size;
3575}
3576
3577/**
3578 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003579 * @sock: one sock
3580 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003581 * @newsk: unused
3582 *
3583 * Return 0 if a subject with the smack of sock could access
3584 * an object with the smack of other, otherwise an error code
3585 */
David S. Miller3610cda2011-01-05 15:38:53 -08003586static int smack_unix_stream_connect(struct sock *sock,
3587 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003588{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003589 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003590 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003591 struct socket_smack *ssp = sock->sk_security;
3592 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003593 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003594 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003595 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003596#ifdef CONFIG_AUDIT
3597 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003598#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003599
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003600 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3601 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003602 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003603#ifdef CONFIG_AUDIT
3604 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3605 smk_ad_setfield_u_net_sk(&ad, other);
3606#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003607 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3608 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003609 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003610 okp = osp->smk_out;
3611 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003612 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003613 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003614 MAY_WRITE, rc);
3615 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003616 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003617
Casey Schaufler975d5e52011-09-26 14:43:39 -07003618 /*
3619 * Cross reference the peer labels for SO_PEERSEC.
3620 */
3621 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003622 nsp->smk_packet = ssp->smk_out;
3623 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003624 }
3625
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003626 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003627}
3628
3629/**
3630 * smack_unix_may_send - Smack access on UDS
3631 * @sock: one socket
3632 * @other: the other socket
3633 *
3634 * Return 0 if a subject with the smack of sock could access
3635 * an object with the smack of other, otherwise an error code
3636 */
3637static int smack_unix_may_send(struct socket *sock, struct socket *other)
3638{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003639 struct socket_smack *ssp = sock->sk->sk_security;
3640 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003641 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003642 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003643
Kees Cook923e9a12012-04-10 13:26:44 -07003644#ifdef CONFIG_AUDIT
3645 struct lsm_network_audit net;
3646
Eric Paris48c62af2012-04-02 13:15:44 -04003647 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003648 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003649#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003650
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003651 if (smack_privileged(CAP_MAC_OVERRIDE))
3652 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003653
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003654 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3655 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003656 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003657}
3658
3659/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003660 * smack_socket_sendmsg - Smack check based on destination host
3661 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003662 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003663 * @size: the size of the message
3664 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003665 * Return 0 if the current subject can write to the destination host.
3666 * For IPv4 this is only a question if the destination is a single label host.
3667 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003668 */
3669static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3670 int size)
3671{
3672 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003673#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003674 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003675#endif
3676#ifdef SMACK_IPV6_SECMARK_LABELING
3677 struct socket_smack *ssp = sock->sk->sk_security;
3678 struct smack_known *rsp;
3679#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003680 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003681
3682 /*
3683 * Perfectly reasonable for this to be NULL
3684 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003685 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003686 return 0;
3687
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003688 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003689 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003690 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3691 sip->sin_family != AF_INET)
3692 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003693 rc = smack_netlabel_send(sock->sk, sip);
3694 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003695#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003696 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003697 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3698 sap->sin6_family != AF_INET6)
3699 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003700#ifdef SMACK_IPV6_SECMARK_LABELING
3701 rsp = smack_ipv6host_label(sap);
3702 if (rsp != NULL)
3703 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3704 SMK_CONNECTING);
3705#endif
3706#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003707 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003708#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003709#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003710 break;
3711 }
3712 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003713}
3714
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003715/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003716 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003717 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003718 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003719 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003720 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003721 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003722static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3723 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003724{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003725 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003726 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003727 int acat;
3728 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003729
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003730 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003731 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003732 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003733 * If there are flags but no level netlabel isn't
3734 * behaving the way we expect it to.
3735 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003736 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003737 * Without guidance regarding the smack value
3738 * for the packet fall back on the network
3739 * ambient value.
3740 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003741 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303742 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003743 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003744 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003745 /*
3746 * Compare the catsets. Use the netlbl APIs.
3747 */
3748 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3749 if ((skp->smk_netlabel.flags &
3750 NETLBL_SECATTR_MLS_CAT) == 0)
3751 found = 1;
3752 break;
3753 }
3754 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003755 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3756 acat + 1);
3757 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003758 skp->smk_netlabel.attr.mls.cat,
3759 kcat + 1);
3760 if (acat < 0 || kcat < 0)
3761 break;
3762 }
3763 if (acat == kcat) {
3764 found = 1;
3765 break;
3766 }
Casey Schauflere114e472008-02-04 22:29:50 -08003767 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003768 rcu_read_unlock();
3769
3770 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003771 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003772
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003773 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003774 return &smack_known_web;
3775 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003776 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003777 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003778 /*
3779 * Looks like a fallback, which gives us a secid.
3780 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003781 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003782 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003783 * Without guidance regarding the smack value
3784 * for the packet fall back on the network
3785 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003786 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003787 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003788}
3789
Casey Schaufler69f287a2014-12-12 17:08:40 -08003790#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003791static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003792{
Casey Schauflerc6739442013-05-22 18:42:56 -07003793 u8 nexthdr;
3794 int offset;
3795 int proto = -EINVAL;
3796 struct ipv6hdr _ipv6h;
3797 struct ipv6hdr *ip6;
3798 __be16 frag_off;
3799 struct tcphdr _tcph, *th;
3800 struct udphdr _udph, *uh;
3801 struct dccp_hdr _dccph, *dh;
3802
3803 sip->sin6_port = 0;
3804
3805 offset = skb_network_offset(skb);
3806 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3807 if (ip6 == NULL)
3808 return -EINVAL;
3809 sip->sin6_addr = ip6->saddr;
3810
3811 nexthdr = ip6->nexthdr;
3812 offset += sizeof(_ipv6h);
3813 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3814 if (offset < 0)
3815 return -EINVAL;
3816
3817 proto = nexthdr;
3818 switch (proto) {
3819 case IPPROTO_TCP:
3820 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3821 if (th != NULL)
3822 sip->sin6_port = th->source;
3823 break;
3824 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003825 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003826 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3827 if (uh != NULL)
3828 sip->sin6_port = uh->source;
3829 break;
3830 case IPPROTO_DCCP:
3831 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3832 if (dh != NULL)
3833 sip->sin6_port = dh->dccph_sport;
3834 break;
3835 }
3836 return proto;
3837}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003838#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003839
Casey Schauflere114e472008-02-04 22:29:50 -08003840/**
3841 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3842 * @sk: socket
3843 * @skb: packet
3844 *
3845 * Returns 0 if the packet should be delivered, an error code otherwise
3846 */
3847static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3848{
3849 struct netlbl_lsm_secattr secattr;
3850 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003851 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003852 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003853 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003854 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003855#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003856 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003857#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003858#if IS_ENABLED(CONFIG_IPV6)
3859 struct sockaddr_in6 sadd;
3860 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003861
3862 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3863 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003864#endif /* CONFIG_IPV6 */
3865
Piotr Sawicki129a9982018-07-19 11:42:58 +02003866 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003867 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003868#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3869 /*
3870 * If there is a secmark use it rather than the CIPSO label.
3871 * If there is no secmark fall back to CIPSO.
3872 * The secmark is assumed to reflect policy better.
3873 */
3874 if (skb && skb->secmark != 0) {
3875 skp = smack_from_secid(skb->secmark);
3876 goto access_check;
3877 }
3878#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003879 /*
3880 * Translate what netlabel gave us.
3881 */
3882 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003883
Piotr Sawicki129a9982018-07-19 11:42:58 +02003884 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003885 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003886 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003887 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003888 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003889
Casey Schauflerc6739442013-05-22 18:42:56 -07003890 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003891
Casey Schaufler69f287a2014-12-12 17:08:40 -08003892#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3893access_check:
3894#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003895#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003896 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003897 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003898 ad.a.u.net->netif = skb->skb_iif;
3899 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003900#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003901 /*
3902 * Receiving a packet requires that the other end
3903 * be able to write here. Read access is not required.
3904 * This is the simplist possible security model
3905 * for networking.
3906 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003907 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3908 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003909 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003910 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003911 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003912 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003913#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003914 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003915 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003916 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3917 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003918 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003919#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003920 if (skb && skb->secmark != 0)
3921 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003922 else if (smk_ipv6_localhost(&sadd))
3923 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003924 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003925 skp = smack_ipv6host_label(&sadd);
3926 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003927 skp = smack_net_ambient;
3928#ifdef CONFIG_AUDIT
3929 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003930 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003931 ad.a.u.net->netif = skb->skb_iif;
3932 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3933#endif /* CONFIG_AUDIT */
3934 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3935 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3936 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003937#endif /* SMACK_IPV6_SECMARK_LABELING */
3938#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003939 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003940#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003941 if (rc != 0)
3942 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3943 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003944 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003945#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003946 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003947
Paul Moorea8134292008-10-10 10:16:31 -04003948 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003949}
3950
3951/**
3952 * smack_socket_getpeersec_stream - pull in packet label
3953 * @sock: the socket
3954 * @optval: user's destination
3955 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003956 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003957 *
3958 * returns zero on success, an error code otherwise
3959 */
3960static int smack_socket_getpeersec_stream(struct socket *sock,
3961 char __user *optval,
3962 int __user *optlen, unsigned len)
3963{
3964 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003965 char *rcp = "";
3966 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003967 int rc = 0;
3968
3969 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003970 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003971 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003972 slen = strlen(rcp) + 1;
3973 }
Casey Schauflere114e472008-02-04 22:29:50 -08003974
3975 if (slen > len)
3976 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003977 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003978 rc = -EFAULT;
3979
3980 if (put_user(slen, optlen) != 0)
3981 rc = -EFAULT;
3982
3983 return rc;
3984}
3985
3986
3987/**
3988 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003989 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003990 * @skb: packet data
3991 * @secid: pointer to where to put the secid of the packet
3992 *
3993 * Sets the netlabel socket state on sk from parent
3994 */
3995static int smack_socket_getpeersec_dgram(struct socket *sock,
3996 struct sk_buff *skb, u32 *secid)
3997
3998{
3999 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004000 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004001 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004002 int family = PF_UNSPEC;
4003 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004004 int rc;
4005
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004006 if (skb != NULL) {
4007 if (skb->protocol == htons(ETH_P_IP))
4008 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004009#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004010 else if (skb->protocol == htons(ETH_P_IPV6))
4011 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004012#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004013 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004014 if (family == PF_UNSPEC && sock != NULL)
4015 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004016
Casey Schaufler69f287a2014-12-12 17:08:40 -08004017 switch (family) {
4018 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004019 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004020 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004021 break;
4022 case PF_INET:
4023#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4024 s = skb->secmark;
4025 if (s != 0)
4026 break;
4027#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004028 /*
4029 * Translate what netlabel gave us.
4030 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004031 if (sock != NULL && sock->sk != NULL)
4032 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004033 netlbl_secattr_init(&secattr);
4034 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4035 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004036 skp = smack_from_secattr(&secattr, ssp);
4037 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004038 }
4039 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004040 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004041 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004042#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004043 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004044#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004045 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004046 }
4047 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004048 if (s == 0)
4049 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004050 return 0;
4051}
4052
4053/**
Paul Moore07feee82009-03-27 17:10:54 -04004054 * smack_sock_graft - Initialize a newly created socket with an existing sock
4055 * @sk: child sock
4056 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004057 *
Paul Moore07feee82009-03-27 17:10:54 -04004058 * Set the smk_{in,out} state of an existing sock based on the process that
4059 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004060 */
4061static void smack_sock_graft(struct sock *sk, struct socket *parent)
4062{
4063 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004064 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004065
Paul Moore07feee82009-03-27 17:10:54 -04004066 if (sk == NULL ||
4067 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004068 return;
4069
4070 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004071 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004072 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004073 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004074}
4075
4076/**
4077 * smack_inet_conn_request - Smack access check on connect
4078 * @sk: socket involved
4079 * @skb: packet
4080 * @req: unused
4081 *
4082 * Returns 0 if a task with the packet label could write to
4083 * the socket, otherwise an error code
4084 */
4085static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4086 struct request_sock *req)
4087{
Paul Moore07feee82009-03-27 17:10:54 -04004088 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004089 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004090 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004091 struct netlbl_lsm_secattr secattr;
4092 struct sockaddr_in addr;
4093 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004094 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004095 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004096 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004097#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004098 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004099#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004100
Casey Schaufler69f287a2014-12-12 17:08:40 -08004101#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004102 if (family == PF_INET6) {
4103 /*
4104 * Handle mapped IPv4 packets arriving
4105 * via IPv6 sockets. Don't set up netlabel
4106 * processing on IPv6.
4107 */
4108 if (skb->protocol == htons(ETH_P_IP))
4109 family = PF_INET;
4110 else
4111 return 0;
4112 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004113#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004114
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004115#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4116 /*
4117 * If there is a secmark use it rather than the CIPSO label.
4118 * If there is no secmark fall back to CIPSO.
4119 * The secmark is assumed to reflect policy better.
4120 */
4121 if (skb && skb->secmark != 0) {
4122 skp = smack_from_secid(skb->secmark);
4123 goto access_check;
4124 }
4125#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4126
Paul Moore07feee82009-03-27 17:10:54 -04004127 netlbl_secattr_init(&secattr);
4128 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004129 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004130 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004131 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004132 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004133 netlbl_secattr_destroy(&secattr);
4134
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004135#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4136access_check:
4137#endif
4138
Etienne Bassetecfcc532009-04-08 20:40:06 +02004139#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004140 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4141 ad.a.u.net->family = family;
4142 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004143 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4144#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004145 /*
Paul Moore07feee82009-03-27 17:10:54 -04004146 * Receiving a packet requires that the other end be able to write
4147 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004148 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004149 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4150 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004151 if (rc != 0)
4152 return rc;
4153
4154 /*
4155 * Save the peer's label in the request_sock so we can later setup
4156 * smk_packet in the child socket so that SO_PEERCRED can report it.
4157 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004158 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004159
4160 /*
4161 * We need to decide if we want to label the incoming connection here
4162 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004163 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004164 */
4165 hdr = ip_hdr(skb);
4166 addr.sin_addr.s_addr = hdr->saddr;
4167 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004168 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004169 rcu_read_unlock();
4170
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004171 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004172 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004173 else
Paul Moore07feee82009-03-27 17:10:54 -04004174 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004175
4176 return rc;
4177}
4178
Paul Moore07feee82009-03-27 17:10:54 -04004179/**
4180 * smack_inet_csk_clone - Copy the connection information to the new socket
4181 * @sk: the new socket
4182 * @req: the connection's request_sock
4183 *
4184 * Transfer the connection's peer label to the newly created socket.
4185 */
4186static void smack_inet_csk_clone(struct sock *sk,
4187 const struct request_sock *req)
4188{
4189 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004190 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004191
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004192 if (req->peer_secid != 0) {
4193 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004194 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004195 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004196 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004197}
4198
Casey Schauflere114e472008-02-04 22:29:50 -08004199/*
4200 * Key management security hooks
4201 *
4202 * Casey has not tested key support very heavily.
4203 * The permission check is most likely too restrictive.
4204 * If you care about keys please have a look.
4205 */
4206#ifdef CONFIG_KEYS
4207
4208/**
4209 * smack_key_alloc - Set the key security blob
4210 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004211 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004212 * @flags: unused
4213 *
4214 * No allocation required
4215 *
4216 * Returns 0
4217 */
David Howellsd84f4f92008-11-14 10:39:23 +11004218static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004219 unsigned long flags)
4220{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004221 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004222
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004223 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004224 return 0;
4225}
4226
4227/**
4228 * smack_key_free - Clear the key security blob
4229 * @key: the object
4230 *
4231 * Clear the blob pointer
4232 */
4233static void smack_key_free(struct key *key)
4234{
4235 key->security = NULL;
4236}
4237
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004238/**
Casey Schauflere114e472008-02-04 22:29:50 -08004239 * smack_key_permission - Smack access on a key
4240 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004241 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004242 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004243 *
4244 * Return 0 if the task has read and write to the object,
4245 * an error code otherwise
4246 */
4247static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004248 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004249{
4250 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004251 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004252 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004253 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004254 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004255
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004256 /*
4257 * Validate requested permissions
4258 */
4259 if (perm & ~KEY_NEED_ALL)
4260 return -EINVAL;
4261
Casey Schauflere114e472008-02-04 22:29:50 -08004262 keyp = key_ref_to_ptr(key_ref);
4263 if (keyp == NULL)
4264 return -EINVAL;
4265 /*
4266 * If the key hasn't been initialized give it access so that
4267 * it may do so.
4268 */
4269 if (keyp->security == NULL)
4270 return 0;
4271 /*
4272 * This should not occur
4273 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004274 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004275 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004276
4277 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4278 return 0;
4279
Etienne Bassetecfcc532009-04-08 20:40:06 +02004280#ifdef CONFIG_AUDIT
4281 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4282 ad.a.u.key_struct.key = keyp->serial;
4283 ad.a.u.key_struct.key_desc = keyp->description;
4284#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004285 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4286 request |= MAY_READ;
Linus Torvalds028db3e2019-07-10 18:43:43 -07004287 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004288 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004289 rc = smk_access(tkp, keyp->security, request, &ad);
4290 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4291 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004292}
José Bollo7fc5f362015-02-17 15:41:22 +01004293
4294/*
4295 * smack_key_getsecurity - Smack label tagging the key
4296 * @key points to the key to be queried
4297 * @_buffer points to a pointer that should be set to point to the
4298 * resulting string (if no label or an error occurs).
4299 * Return the length of the string (including terminating NUL) or -ve if
4300 * an error.
4301 * May also return 0 (and a NULL buffer pointer) if there is no label.
4302 */
4303static int smack_key_getsecurity(struct key *key, char **_buffer)
4304{
4305 struct smack_known *skp = key->security;
4306 size_t length;
4307 char *copy;
4308
4309 if (key->security == NULL) {
4310 *_buffer = NULL;
4311 return 0;
4312 }
4313
4314 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4315 if (copy == NULL)
4316 return -ENOMEM;
4317 length = strlen(copy) + 1;
4318
4319 *_buffer = copy;
4320 return length;
4321}
4322
Casey Schauflere114e472008-02-04 22:29:50 -08004323#endif /* CONFIG_KEYS */
4324
4325/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004326 * Smack Audit hooks
4327 *
4328 * Audit requires a unique representation of each Smack specific
4329 * rule. This unique representation is used to distinguish the
4330 * object to be audited from remaining kernel objects and also
4331 * works as a glue between the audit hooks.
4332 *
4333 * Since repository entries are added but never deleted, we'll use
4334 * the smack_known label address related to the given audit rule as
4335 * the needed unique representation. This also better fits the smack
4336 * model where nearly everything is a label.
4337 */
4338#ifdef CONFIG_AUDIT
4339
4340/**
4341 * smack_audit_rule_init - Initialize a smack audit rule
4342 * @field: audit rule fields given from user-space (audit.h)
4343 * @op: required testing operator (=, !=, >, <, ...)
4344 * @rulestr: smack label to be audited
4345 * @vrule: pointer to save our own audit rule representation
4346 *
4347 * Prepare to audit cases where (@field @op @rulestr) is true.
4348 * The label to be audited is created if necessay.
4349 */
4350static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4351{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004352 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004353 char **rule = (char **)vrule;
4354 *rule = NULL;
4355
4356 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4357 return -EINVAL;
4358
Al Viro5af75d82008-12-16 05:59:26 -05004359 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004360 return -EINVAL;
4361
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004362 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004363 if (IS_ERR(skp))
4364 return PTR_ERR(skp);
4365
4366 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004367
4368 return 0;
4369}
4370
4371/**
4372 * smack_audit_rule_known - Distinguish Smack audit rules
4373 * @krule: rule of interest, in Audit kernel representation format
4374 *
4375 * This is used to filter Smack rules from remaining Audit ones.
4376 * If it's proved that this rule belongs to us, the
4377 * audit_rule_match hook will be called to do the final judgement.
4378 */
4379static int smack_audit_rule_known(struct audit_krule *krule)
4380{
4381 struct audit_field *f;
4382 int i;
4383
4384 for (i = 0; i < krule->field_count; i++) {
4385 f = &krule->fields[i];
4386
4387 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4388 return 1;
4389 }
4390
4391 return 0;
4392}
4393
4394/**
4395 * smack_audit_rule_match - Audit given object ?
4396 * @secid: security id for identifying the object to test
4397 * @field: audit rule flags given from user-space
4398 * @op: required testing operator
4399 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004400 *
4401 * The core Audit hook. It's used to take the decision of
4402 * whether to audit or not to audit a given object.
4403 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004404static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004405{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004406 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004407 char *rule = vrule;
4408
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004409 if (unlikely(!rule)) {
4410 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004411 return -ENOENT;
4412 }
4413
4414 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4415 return 0;
4416
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004417 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004418
4419 /*
4420 * No need to do string comparisons. If a match occurs,
4421 * both pointers will point to the same smack_known
4422 * label.
4423 */
Al Viro5af75d82008-12-16 05:59:26 -05004424 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004425 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004426 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004427 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004428
4429 return 0;
4430}
4431
Casey Schaufler491a0b02016-01-26 15:08:35 -08004432/*
4433 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004434 * No memory was allocated.
4435 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004436
4437#endif /* CONFIG_AUDIT */
4438
Randy Dunlap251a2a92009-02-18 11:42:33 -08004439/**
David Quigley746df9b2013-05-22 12:50:35 -04004440 * smack_ismaclabel - check if xattr @name references a smack MAC label
4441 * @name: Full xattr name to check.
4442 */
4443static int smack_ismaclabel(const char *name)
4444{
4445 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4446}
4447
4448
4449/**
Casey Schauflere114e472008-02-04 22:29:50 -08004450 * smack_secid_to_secctx - return the smack label for a secid
4451 * @secid: incoming integer
4452 * @secdata: destination
4453 * @seclen: how long it is
4454 *
4455 * Exists for networking code.
4456 */
4457static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4458{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004459 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004460
Eric Parisd5630b92010-10-13 16:24:48 -04004461 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004462 *secdata = skp->smk_known;
4463 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004464 return 0;
4465}
4466
Randy Dunlap251a2a92009-02-18 11:42:33 -08004467/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004468 * smack_secctx_to_secid - return the secid for a smack label
4469 * @secdata: smack label
4470 * @seclen: how long result is
4471 * @secid: outgoing integer
4472 *
4473 * Exists for audit and networking code.
4474 */
David Howellse52c17642008-04-29 20:52:51 +01004475static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004476{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004477 struct smack_known *skp = smk_find_entry(secdata);
4478
4479 if (skp)
4480 *secid = skp->smk_secid;
4481 else
4482 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004483 return 0;
4484}
4485
Casey Schaufler491a0b02016-01-26 15:08:35 -08004486/*
4487 * There used to be a smack_release_secctx hook
4488 * that did nothing back when hooks were in a vector.
4489 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004490 */
Casey Schauflere114e472008-02-04 22:29:50 -08004491
David P. Quigley1ee65e32009-09-03 14:25:57 -04004492static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4493{
4494 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4495}
4496
4497static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4498{
4499 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4500}
4501
4502static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4503{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004504 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004505
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004506 *ctx = skp->smk_known;
4507 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004508 return 0;
4509}
4510
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004511static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4512{
4513
4514 struct task_smack *tsp;
4515 struct smack_known *skp;
4516 struct inode_smack *isp;
4517 struct cred *new_creds = *new;
4518
4519 if (new_creds == NULL) {
4520 new_creds = prepare_creds();
4521 if (new_creds == NULL)
4522 return -ENOMEM;
4523 }
4524
Casey Schauflerb17103a2018-11-09 16:12:56 -08004525 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004526
4527 /*
4528 * Get label from overlay inode and set it in create_sid
4529 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004530 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004531 skp = isp->smk_inode;
4532 tsp->smk_task = skp;
4533 *new = new_creds;
4534 return 0;
4535}
4536
4537static int smack_inode_copy_up_xattr(const char *name)
4538{
4539 /*
4540 * Return 1 if this is the smack access Smack attribute.
4541 */
4542 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4543 return 1;
4544
4545 return -EOPNOTSUPP;
4546}
4547
4548static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4549 struct qstr *name,
4550 const struct cred *old,
4551 struct cred *new)
4552{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004553 struct task_smack *otsp = smack_cred(old);
4554 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004555 struct inode_smack *isp;
4556 int may;
4557
4558 /*
4559 * Use the process credential unless all of
4560 * the transmuting criteria are met
4561 */
4562 ntsp->smk_task = otsp->smk_task;
4563
4564 /*
4565 * the attribute of the containing directory
4566 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004567 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004568
4569 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4570 rcu_read_lock();
4571 may = smk_access_entry(otsp->smk_task->smk_known,
4572 isp->smk_inode->smk_known,
4573 &otsp->smk_task->smk_rules);
4574 rcu_read_unlock();
4575
4576 /*
4577 * If the directory is transmuting and the rule
4578 * providing access is transmuting use the containing
4579 * directory label instead of the process label.
4580 */
4581 if (may > 0 && (may & MAY_TRANSMUTE))
4582 ntsp->smk_task = isp->smk_inode;
4583 }
4584 return 0;
4585}
4586
Casey Schauflerbbd36622018-11-12 09:30:56 -08004587struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4588 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004589 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004590 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004591 .lbs_ipc = sizeof(struct smack_known *),
4592 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004593};
4594
James Morrisca97d932017-02-15 00:18:51 +11004595static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004596 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4597 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4598 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004599
Al Viro0b520752018-12-23 16:02:47 -05004600 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004601 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4602
Casey Schauflere20b0432015-05-02 15:11:36 -07004603 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4604 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004605 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004606 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004607 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304608 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004609
Casey Schauflere20b0432015-05-02 15:11:36 -07004610 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004611
Casey Schauflere20b0432015-05-02 15:11:36 -07004612 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004613 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4614 LSM_HOOK_INIT(inode_link, smack_inode_link),
4615 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4616 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4617 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4618 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4619 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4620 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4621 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4622 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4623 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4624 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4625 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4626 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4627 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4628 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004629
Casey Schauflere20b0432015-05-02 15:11:36 -07004630 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004631 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4632 LSM_HOOK_INIT(file_lock, smack_file_lock),
4633 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4634 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4635 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4636 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4637 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4638 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004639
Casey Schauflere20b0432015-05-02 15:11:36 -07004640 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004641
Casey Schauflere20b0432015-05-02 15:11:36 -07004642 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4643 LSM_HOOK_INIT(cred_free, smack_cred_free),
4644 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4645 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004646 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004647 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4648 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4649 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4650 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4651 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4652 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4653 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4654 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4655 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4656 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4657 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4658 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4659 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004660 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004661
Casey Schauflere20b0432015-05-02 15:11:36 -07004662 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4663 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004664
Casey Schauflere20b0432015-05-02 15:11:36 -07004665 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004666
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004667 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004668 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4669 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4670 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4671 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004672
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004673 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004674 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4675 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4676 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004677
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004678 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004679 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4680 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4681 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004682
Casey Schauflere20b0432015-05-02 15:11:36 -07004683 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004684
Casey Schauflere20b0432015-05-02 15:11:36 -07004685 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4686 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004687
Casey Schauflere20b0432015-05-02 15:11:36 -07004688 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4689 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004690
Casey Schauflere20b0432015-05-02 15:11:36 -07004691 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004692 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004693#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004694 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004695#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004696 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4697 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4698 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4699 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4700 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4701 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4702 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4703 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4704 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4705 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004706
Casey Schauflere114e472008-02-04 22:29:50 -08004707 /* key management security hooks */
4708#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004709 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4710 LSM_HOOK_INIT(key_free, smack_key_free),
4711 LSM_HOOK_INIT(key_permission, smack_key_permission),
4712 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004713#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004714
4715 /* Audit hooks */
4716#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004717 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4718 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4719 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004720#endif /* CONFIG_AUDIT */
4721
Casey Schauflere20b0432015-05-02 15:11:36 -07004722 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4723 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4724 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004725 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4726 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4727 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004728 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4729 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4730 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004731};
4732
Etienne Basset7198e2e2009-03-24 20:53:24 +01004733
Casey Schaufler86812bb2012-04-17 18:55:46 -07004734static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004735{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004736 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004737 * Initialize rule list locks
4738 */
4739 mutex_init(&smack_known_huh.smk_rules_lock);
4740 mutex_init(&smack_known_hat.smk_rules_lock);
4741 mutex_init(&smack_known_floor.smk_rules_lock);
4742 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004743 mutex_init(&smack_known_web.smk_rules_lock);
4744 /*
4745 * Initialize rule lists
4746 */
4747 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4748 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4749 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4750 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004751 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4752 /*
4753 * Create the known labels list
4754 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004755 smk_insert_entry(&smack_known_huh);
4756 smk_insert_entry(&smack_known_hat);
4757 smk_insert_entry(&smack_known_star);
4758 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004759 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004760}
4761
Casey Schauflere114e472008-02-04 22:29:50 -08004762/**
4763 * smack_init - initialize the smack system
4764 *
4765 * Returns 0
4766 */
4767static __init int smack_init(void)
4768{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004769 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004770 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004771
Rohit1a5b4722014-10-15 17:40:41 +05304772 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4773 if (!smack_inode_cache)
4774 return -ENOMEM;
4775
Casey Schaufler4e328b02019-04-02 11:37:12 -07004776 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4777 if (!smack_rule_cache) {
4778 kmem_cache_destroy(smack_inode_cache);
4779 return -ENOMEM;
4780 }
4781
Casey Schauflerbbd36622018-11-12 09:30:56 -08004782 /*
4783 * Set the security state for the initial task.
4784 */
4785 tsp = smack_cred(cred);
4786 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4787
4788 /*
4789 * Register with LSM
4790 */
4791 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004792 smack_enabled = 1;
4793
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004794 pr_info("Smack: Initializing.\n");
4795#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4796 pr_info("Smack: Netfilter enabled.\n");
4797#endif
4798#ifdef SMACK_IPV6_PORT_LABELING
4799 pr_info("Smack: IPv6 port labeling enabled.\n");
4800#endif
4801#ifdef SMACK_IPV6_SECMARK_LABELING
4802 pr_info("Smack: IPv6 Netfilter enabled.\n");
4803#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004804
Casey Schaufler86812bb2012-04-17 18:55:46 -07004805 /* initialize the smack_known_list */
4806 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004807
Casey Schauflere114e472008-02-04 22:29:50 -08004808 return 0;
4809}
4810
4811/*
4812 * Smack requires early initialization in order to label
4813 * all processes and objects when they are created.
4814 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004815DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004816 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004817 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004818 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004819 .init = smack_init,
4820};