blob: 3c4d4836da4a0f052fc73a8c113a50d1df945002 [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>
Casey Schauflere114e472008-02-04 22:29:50 -080031#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070032#include <net/ip.h>
33#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100034#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070035#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050036#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030037#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000038#include <linux/msg.h>
39#include <linux/shm.h>
40#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053041#include <linux/parser.h>
David Howells2febd252018-11-01 23:07:24 +000042#include <linux/fs_context.h>
43#include <linux/fs_parser.h>
David Howellsa8478a62020-01-14 17:07:13 +000044#include <linux/watch_queue.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
Eric Biggerse5bfad32019-08-21 22:54:41 -0700291 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700292 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
luanshia1a07f22019-07-05 10:35:20 +0800310 * @inode: inode to extract the info from
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
luanshia1a07f22019-07-05 10:35:20 +0800512 * @typefrom_file: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800513 *
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
Al Virod7167b12019-09-07 07:23:15 -0400682static const struct fs_parameter_spec smack_fs_parameters[] = {
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
David Howells2febd252018-11-01 23:07:24 +0000692/**
693 * smack_fs_context_parse_param - Parse a single mount parameter
694 * @fc: The new filesystem context being constructed.
695 * @param: The parameter.
696 *
697 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
698 * error.
699 */
700static int smack_fs_context_parse_param(struct fs_context *fc,
701 struct fs_parameter *param)
702{
703 struct fs_parse_result result;
704 int opt, rc;
705
Al Virod7167b12019-09-07 07:23:15 -0400706 opt = fs_parse(fc, smack_fs_parameters, param, &result);
David Howells2febd252018-11-01 23:07:24 +0000707 if (opt < 0)
708 return opt;
709
710 rc = smack_add_opt(opt, param->string, &fc->security);
711 if (!rc)
712 param->string = NULL;
713 return rc;
714}
715
Al Virod2497e12018-12-16 01:37:06 -0500716static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530717{
Al Virod2497e12018-12-16 01:37:06 -0500718 char *from = options, *to = options;
719 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530720
Al Viroc3300aa2018-12-16 01:52:24 -0500721 while (1) {
722 char *next = strchr(from, ',');
723 int token, len, rc;
724 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530725
Al Viroc3300aa2018-12-16 01:52:24 -0500726 if (next)
727 len = next - from;
728 else
729 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530730
Al Viroc3300aa2018-12-16 01:52:24 -0500731 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500732 if (token != Opt_error) {
733 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
734 rc = smack_add_opt(token, arg, mnt_opts);
735 if (unlikely(rc)) {
736 kfree(arg);
737 if (*mnt_opts)
738 smack_free_mnt_opts(*mnt_opts);
739 *mnt_opts = NULL;
740 return rc;
741 }
742 } else {
743 if (!first) { // copy with preceding comma
744 from--;
745 len++;
746 }
747 if (to != from)
748 memmove(to, from, len);
749 to += len;
750 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530751 }
Al Viroc3300aa2018-12-16 01:52:24 -0500752 if (!from[len])
753 break;
754 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530755 }
Al Virod2497e12018-12-16 01:37:06 -0500756 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530757 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530758}
759
760/**
761 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800762 * @sb: the file system superblock
luanshia1a07f22019-07-05 10:35:20 +0800763 * @mnt_opts: Smack mount options
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530764 * @kern_flags: mount option from kernel space or user space
765 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800766 *
767 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530768 *
769 * Allow filesystems with binary mount data to explicitly set Smack mount
770 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800771 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530772static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500773 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530774 unsigned long kern_flags,
775 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800776{
777 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000778 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800779 struct superblock_smack *sp = sb->s_security;
780 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800781 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500782 struct smack_mnt_opts *opts = mnt_opts;
783 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800784
Seth Forshee9f50eda2015-09-23 15:16:06 -0500785 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800786 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700787
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700788 if (inode->i_security == NULL) {
789 int rc = lsm_inode_alloc(inode);
790
791 if (rc)
792 return rc;
793 }
794
Himanshu Shukla2097f592016-11-10 16:19:52 +0530795 if (!smack_privileged(CAP_MAC_ADMIN)) {
796 /*
797 * Unprivileged mounts don't get to specify Smack values.
798 */
Al Viro12085b12018-12-13 15:18:05 -0500799 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530800 return -EPERM;
801 /*
802 * Unprivileged mounts get root and default from the caller.
803 */
804 skp = smk_of_current();
805 sp->smk_root = skp;
806 sp->smk_default = skp;
807 /*
808 * For a handful of fs types with no user-controlled
809 * backing store it's okay to trust security labels
810 * in the filesystem. The rest are untrusted.
811 */
812 if (sb->s_user_ns != &init_user_ns &&
813 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
814 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500815 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530816 sp->smk_flags |= SMK_SB_UNTRUSTED;
817 }
818 }
819
Seth Forshee9f50eda2015-09-23 15:16:06 -0500820 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800821
Al Viro12085b12018-12-13 15:18:05 -0500822 if (opts) {
823 if (opts->fsdefault) {
824 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200825 if (IS_ERR(skp))
826 return PTR_ERR(skp);
827 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500828 }
829 if (opts->fsfloor) {
830 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530831 if (IS_ERR(skp))
832 return PTR_ERR(skp);
833 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500834 }
835 if (opts->fshat) {
836 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530837 if (IS_ERR(skp))
838 return PTR_ERR(skp);
839 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500840 }
841 if (opts->fsroot) {
842 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200843 if (IS_ERR(skp))
844 return PTR_ERR(skp);
845 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500846 }
847 if (opts->fstransmute) {
848 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200849 if (IS_ERR(skp))
850 return PTR_ERR(skp);
851 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500852 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800853 }
854 }
855
856 /*
857 * Initialize the root inode.
858 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700859 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800860
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700861 if (transmute) {
862 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700863 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700864 }
Casey Schauflere830b392013-05-22 18:43:07 -0700865
Casey Schauflere114e472008-02-04 22:29:50 -0800866 return 0;
867}
868
869/**
870 * smack_sb_statfs - Smack check on statfs
871 * @dentry: identifies the file system in question
872 *
873 * Returns 0 if current can read the floor of the filesystem,
874 * and error code otherwise
875 */
876static int smack_sb_statfs(struct dentry *dentry)
877{
878 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200879 int rc;
880 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800881
Eric Parisa2694342011-04-25 13:10:27 -0400882 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200883 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
884
885 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700886 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200887 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800888}
889
Casey Schauflere114e472008-02-04 22:29:50 -0800890/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800891 * BPRM hooks
892 */
893
Casey Schauflerce8a4322011-09-29 18:21:01 -0700894/**
895 * smack_bprm_set_creds - set creds for exec
896 * @bprm: the exec information
897 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100898 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700899 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800900static int smack_bprm_set_creds(struct linux_binprm *bprm)
901{
Al Viro496ad9a2013-01-23 17:07:38 -0500902 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800903 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800904 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500905 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800906 int rc;
907
Kees Cookddb4a142017-07-18 15:25:23 -0700908 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800909 return 0;
910
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800911 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300912 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800913 return 0;
914
Seth Forshee809c02e2016-04-26 14:36:22 -0500915 sbsp = inode->i_sb->s_security;
916 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
917 isp->smk_task != sbsp->smk_root)
918 return 0;
919
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300920 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100921 struct task_struct *tracer;
922 rc = 0;
923
924 rcu_read_lock();
925 tracer = ptrace_parent(current);
926 if (likely(tracer != NULL))
927 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200928 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100929 PTRACE_MODE_ATTACH,
930 __func__);
931 rcu_read_unlock();
932
933 if (rc != 0)
934 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200935 }
936 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300937 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800938
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300939 bsp->smk_task = isp->smk_task;
940 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800941
Kees Cookccbb6e12017-07-18 15:25:26 -0700942 /* Decide if this is a secure exec. */
943 if (bsp->smk_task != bsp->smk_forked)
944 bprm->secureexec = 1;
945
Casey Schaufler676dac42010-12-02 06:43:39 -0800946 return 0;
947}
948
949/*
Casey Schauflere114e472008-02-04 22:29:50 -0800950 * Inode hooks
951 */
952
953/**
954 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800955 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800956 *
luanshia1a07f22019-07-05 10:35:20 +0800957 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800958 */
959static int smack_inode_alloc_security(struct inode *inode)
960{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700961 struct smack_known *skp = smk_of_current();
962
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700963 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800964 return 0;
965}
966
967/**
Casey Schauflere114e472008-02-04 22:29:50 -0800968 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200969 * @inode: the newly created inode
970 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500971 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800972 * @name: where to put the attribute name
973 * @value: where to put the attribute value
974 * @len: where to put the length of the attribute
975 *
976 * Returns 0 if it all works out, -ENOMEM if there's no memory
977 */
978static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900979 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500980 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800981{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800982 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700983 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200984 struct smack_known *isp = smk_of_inode(inode);
985 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800986 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800987
Tetsuo Handa95489062013-07-25 05:44:02 +0900988 if (name)
989 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800990
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100991 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800992 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200993 may = smk_access_entry(skp->smk_known, dsp->smk_known,
994 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800995 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200996
997 /*
998 * If the access rule allows transmutation and
999 * the directory requests transmutation then
1000 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001001 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001002 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001003 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001004 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001005 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001006 issp->smk_flags |= SMK_INODE_CHANGED;
1007 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001008
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001009 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001010 if (*value == NULL)
1011 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001012
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001013 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001014 }
Casey Schauflere114e472008-02-04 22:29:50 -08001015
1016 return 0;
1017}
1018
1019/**
1020 * smack_inode_link - Smack check on link
1021 * @old_dentry: the existing object
1022 * @dir: unused
1023 * @new_dentry: the new object
1024 *
1025 * Returns 0 if access is permitted, an error code otherwise
1026 */
1027static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1028 struct dentry *new_dentry)
1029{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001030 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001031 struct smk_audit_info ad;
1032 int rc;
1033
Eric Parisa2694342011-04-25 13:10:27 -04001034 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001035 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001036
David Howellsc6f493d2015-03-17 22:26:22 +00001037 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001038 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001039 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001040
David Howells88025652015-01-29 12:02:32 +00001041 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001042 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001043 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1044 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001045 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001046 }
1047
1048 return rc;
1049}
1050
1051/**
1052 * smack_inode_unlink - Smack check on inode deletion
1053 * @dir: containing directory object
1054 * @dentry: file to unlink
1055 *
1056 * Returns 0 if current can write the containing directory
1057 * and the object, error code otherwise
1058 */
1059static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1060{
David Howellsc6f493d2015-03-17 22:26:22 +00001061 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001062 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001063 int rc;
1064
Eric Parisa2694342011-04-25 13:10:27 -04001065 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001066 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1067
Casey Schauflere114e472008-02-04 22:29:50 -08001068 /*
1069 * You need write access to the thing you're unlinking
1070 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001071 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001072 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001073 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001074 /*
1075 * You also need write access to the containing directory
1076 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001077 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001078 smk_ad_setfield_u_fs_inode(&ad, dir);
1079 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001080 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001081 }
Casey Schauflere114e472008-02-04 22:29:50 -08001082 return rc;
1083}
1084
1085/**
1086 * smack_inode_rmdir - Smack check on directory deletion
1087 * @dir: containing directory object
1088 * @dentry: directory to unlink
1089 *
1090 * Returns 0 if current can write the containing directory
1091 * and the directory, error code otherwise
1092 */
1093static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1094{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001095 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001096 int rc;
1097
Eric Parisa2694342011-04-25 13:10:27 -04001098 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001099 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1100
Casey Schauflere114e472008-02-04 22:29:50 -08001101 /*
1102 * You need write access to the thing you're removing
1103 */
David Howellsc6f493d2015-03-17 22:26:22 +00001104 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1105 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001107 /*
1108 * You also need write access to the containing directory
1109 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001110 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001111 smk_ad_setfield_u_fs_inode(&ad, dir);
1112 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001113 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001114 }
Casey Schauflere114e472008-02-04 22:29:50 -08001115
1116 return rc;
1117}
1118
1119/**
1120 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001121 * @old_inode: unused
1122 * @old_dentry: the old object
1123 * @new_inode: unused
1124 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001125 *
1126 * Read and write access is required on both the old and
1127 * new directories.
1128 *
1129 * Returns 0 if access is permitted, an error code otherwise
1130 */
1131static int smack_inode_rename(struct inode *old_inode,
1132 struct dentry *old_dentry,
1133 struct inode *new_inode,
1134 struct dentry *new_dentry)
1135{
1136 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001137 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001138 struct smk_audit_info ad;
1139
Eric Parisa2694342011-04-25 13:10:27 -04001140 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001141 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001142
David Howellsc6f493d2015-03-17 22:26:22 +00001143 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001144 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001145 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001146
David Howells88025652015-01-29 12:02:32 +00001147 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001148 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001149 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1150 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001151 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001152 }
Casey Schauflere114e472008-02-04 22:29:50 -08001153 return rc;
1154}
1155
1156/**
1157 * smack_inode_permission - Smack version of permission()
1158 * @inode: the inode in question
1159 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001160 *
1161 * This is the important Smack hook.
1162 *
luanshia1a07f22019-07-05 10:35:20 +08001163 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001164 */
Al Viroe74f71e2011-06-20 19:38:15 -04001165static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001166{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001167 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001168 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001169 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001170 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001171
1172 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001173 /*
1174 * No permission to check. Existence test. Yup, it's there.
1175 */
1176 if (mask == 0)
1177 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001178
Seth Forshee9f50eda2015-09-23 15:16:06 -05001179 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1180 if (smk_of_inode(inode) != sbsp->smk_root)
1181 return -EACCES;
1182 }
1183
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001184 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001185 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001186 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001187 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001188 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001189 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1190 rc = smk_bu_inode(inode, mask, rc);
1191 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001192}
1193
1194/**
1195 * smack_inode_setattr - Smack check for setting attributes
1196 * @dentry: the object
1197 * @iattr: for the force flag
1198 *
1199 * Returns 0 if access is permitted, an error code otherwise
1200 */
1201static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1202{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001203 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001204 int rc;
1205
Casey Schauflere114e472008-02-04 22:29:50 -08001206 /*
1207 * Need to allow for clearing the setuid bit.
1208 */
1209 if (iattr->ia_valid & ATTR_FORCE)
1210 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001212 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001213
David Howellsc6f493d2015-03-17 22:26:22 +00001214 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1215 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001216 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001217}
1218
1219/**
1220 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001221 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001222 *
1223 * Returns 0 if access is permitted, an error code otherwise
1224 */
Al Viro3f7036a2015-03-08 19:28:30 -04001225static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001226{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001227 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001228 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001229 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001230
Eric Parisf48b7392011-04-25 12:54:27 -04001231 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001232 smk_ad_setfield_u_fs_path(&ad, *path);
1233 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1234 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001235 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001236}
1237
1238/**
1239 * smack_inode_setxattr - Smack check for setting xattrs
1240 * @dentry: the object
1241 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001242 * @value: value of the attribute
1243 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001244 * @flags: unused
1245 *
1246 * This protects the Smack attribute explicitly.
1247 *
1248 * Returns 0 if access is permitted, an error code otherwise
1249 */
David Howells8f0cfa52008-04-29 00:59:41 -07001250static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1251 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001252{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001253 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001254 struct smack_known *skp;
1255 int check_priv = 0;
1256 int check_import = 0;
1257 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001258 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001259
Casey Schaufler19760ad2013-12-16 16:27:26 -08001260 /*
1261 * Check label validity here so import won't fail in post_setxattr
1262 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001263 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1264 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001265 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1266 check_priv = 1;
1267 check_import = 1;
1268 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1269 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1270 check_priv = 1;
1271 check_import = 1;
1272 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001273 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001274 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001275 if (size != TRANS_TRUE_SIZE ||
1276 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1277 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001278 } else
1279 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1280
Casey Schaufler19760ad2013-12-16 16:27:26 -08001281 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1282 rc = -EPERM;
1283
1284 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001285 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001286 if (IS_ERR(skp))
1287 rc = PTR_ERR(skp);
1288 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001289 (skp == &smack_known_star || skp == &smack_known_web)))
1290 rc = -EINVAL;
1291 }
1292
Eric Parisa2694342011-04-25 13:10:27 -04001293 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001294 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1295
Casey Schauflerd166c802014-08-27 14:51:27 -07001296 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001297 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1298 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001299 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001300
1301 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001302}
1303
1304/**
1305 * smack_inode_post_setxattr - Apply the Smack update approved above
1306 * @dentry: object
1307 * @name: attribute name
1308 * @value: attribute value
1309 * @size: attribute size
1310 * @flags: unused
1311 *
1312 * Set the pointer in the inode blob to the entry found
1313 * in the master label list.
1314 */
David Howells8f0cfa52008-04-29 00:59:41 -07001315static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1316 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001317{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001318 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001319 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001320
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001321 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1322 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1323 return;
1324 }
1325
Casey Schaufler676dac42010-12-02 06:43:39 -08001326 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001327 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001328 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001329 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001330 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001331 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001332 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001333 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001334 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001335 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001336 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001337 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001338 }
Casey Schauflere114e472008-02-04 22:29:50 -08001339
1340 return;
1341}
1342
Casey Schauflerce8a4322011-09-29 18:21:01 -07001343/**
Casey Schauflere114e472008-02-04 22:29:50 -08001344 * smack_inode_getxattr - Smack check on getxattr
1345 * @dentry: the object
1346 * @name: unused
1347 *
1348 * Returns 0 if access is permitted, an error code otherwise
1349 */
David Howells8f0cfa52008-04-29 00:59:41 -07001350static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001351{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001352 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001353 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001354
Eric Parisa2694342011-04-25 13:10:27 -04001355 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001356 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1357
David Howellsc6f493d2015-03-17 22:26:22 +00001358 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1359 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001360 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001361}
1362
Casey Schauflerce8a4322011-09-29 18:21:01 -07001363/**
Casey Schauflere114e472008-02-04 22:29:50 -08001364 * smack_inode_removexattr - Smack check on removexattr
1365 * @dentry: the object
1366 * @name: name of the attribute
1367 *
1368 * Removing the Smack attribute requires CAP_MAC_ADMIN
1369 *
1370 * Returns 0 if access is permitted, an error code otherwise
1371 */
David Howells8f0cfa52008-04-29 00:59:41 -07001372static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001373{
Casey Schaufler676dac42010-12-02 06:43:39 -08001374 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001375 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001376 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001377
Casey Schauflerbcdca222008-02-23 15:24:04 -08001378 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1379 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001380 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001381 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001382 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301383 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001384 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001385 rc = -EPERM;
1386 } else
1387 rc = cap_inode_removexattr(dentry, name);
1388
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001389 if (rc != 0)
1390 return rc;
1391
Eric Parisa2694342011-04-25 13:10:27 -04001392 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001393 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001394
David Howellsc6f493d2015-03-17 22:26:22 +00001395 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1396 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001397 if (rc != 0)
1398 return rc;
1399
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001400 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001401 /*
1402 * Don't do anything special for these.
1403 * XATTR_NAME_SMACKIPIN
1404 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001405 */
José Bollo80124952016-01-12 21:23:40 +01001406 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001407 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001408 struct superblock_smack *sbsp = sbp->s_security;
1409
1410 isp->smk_inode = sbsp->smk_default;
1411 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001412 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001413 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001414 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001415 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1416 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001417
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001418 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001419}
1420
1421/**
1422 * smack_inode_getsecurity - get smack xattrs
1423 * @inode: the object
1424 * @name: attribute name
1425 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001426 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001427 *
1428 * Returns the size of the attribute or an error code
1429 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001430static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001431 const char *name, void **buffer,
1432 bool alloc)
1433{
1434 struct socket_smack *ssp;
1435 struct socket *sock;
1436 struct super_block *sbp;
1437 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001438 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001439
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001440 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001441 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001442 else {
1443 /*
1444 * The rest of the Smack xattrs are only on sockets.
1445 */
1446 sbp = ip->i_sb;
1447 if (sbp->s_magic != SOCKFS_MAGIC)
1448 return -EOPNOTSUPP;
1449
1450 sock = SOCKET_I(ip);
1451 if (sock == NULL || sock->sk == NULL)
1452 return -EOPNOTSUPP;
1453
1454 ssp = sock->sk->sk_security;
1455
1456 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1457 isp = ssp->smk_in;
1458 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1459 isp = ssp->smk_out;
1460 else
1461 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001462 }
1463
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001464 if (alloc) {
1465 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1466 if (*buffer == NULL)
1467 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001468 }
1469
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001470 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001471}
1472
1473
1474/**
1475 * smack_inode_listsecurity - list the Smack attributes
1476 * @inode: the object
1477 * @buffer: where they go
1478 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001479 */
1480static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1481 size_t buffer_size)
1482{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001483 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001484
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001485 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001486 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001487
1488 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001489}
1490
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001491/**
1492 * smack_inode_getsecid - Extract inode's security id
1493 * @inode: inode to extract the info from
1494 * @secid: where result will be saved
1495 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001496static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001497{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001498 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001499
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001500 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001501}
1502
Casey Schauflere114e472008-02-04 22:29:50 -08001503/*
1504 * File Hooks
1505 */
1506
Casey Schaufler491a0b02016-01-26 15:08:35 -08001507/*
1508 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001509 *
1510 * Should access checks be done on each read or write?
1511 * UNICOS and SELinux say yes.
1512 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1513 *
1514 * I'll say no for now. Smack does not do the frequent
1515 * label changing that SELinux does.
1516 */
Casey Schauflere114e472008-02-04 22:29:50 -08001517
1518/**
1519 * smack_file_alloc_security - assign a file security blob
1520 * @file: the object
1521 *
1522 * The security blob for a file is a pointer to the master
1523 * label list, so no allocation is done.
1524 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001525 * f_security is the owner security information. It
1526 * isn't used on file access checks, it's for send_sigio.
1527 *
Casey Schauflere114e472008-02-04 22:29:50 -08001528 * Returns 0
1529 */
1530static int smack_file_alloc_security(struct file *file)
1531{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001532 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001533
Casey Schauflerf28952a2018-11-12 09:38:53 -08001534 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001535 return 0;
1536}
1537
1538/**
Casey Schauflere114e472008-02-04 22:29:50 -08001539 * smack_file_ioctl - Smack check on ioctls
1540 * @file: the object
1541 * @cmd: what to do
1542 * @arg: unused
1543 *
1544 * Relies heavily on the correct use of the ioctl command conventions.
1545 *
1546 * Returns 0 if allowed, error code otherwise
1547 */
1548static int smack_file_ioctl(struct file *file, unsigned int cmd,
1549 unsigned long arg)
1550{
1551 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001552 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001553 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001554
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001555 if (unlikely(IS_PRIVATE(inode)))
1556 return 0;
1557
Eric Parisf48b7392011-04-25 12:54:27 -04001558 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001559 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001560
Casey Schauflerd166c802014-08-27 14:51:27 -07001561 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001562 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001563 rc = smk_bu_file(file, MAY_WRITE, rc);
1564 }
Casey Schauflere114e472008-02-04 22:29:50 -08001565
Casey Schauflerd166c802014-08-27 14:51:27 -07001566 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001567 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001568 rc = smk_bu_file(file, MAY_READ, rc);
1569 }
Casey Schauflere114e472008-02-04 22:29:50 -08001570
1571 return rc;
1572}
1573
1574/**
1575 * smack_file_lock - Smack check on file locking
1576 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001577 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001578 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001579 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001580 */
1581static int smack_file_lock(struct file *file, unsigned int cmd)
1582{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001583 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001584 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001585 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001586
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001587 if (unlikely(IS_PRIVATE(inode)))
1588 return 0;
1589
Eric Paris92f42502011-04-25 13:15:55 -04001590 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1591 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001592 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001593 rc = smk_bu_file(file, MAY_LOCK, rc);
1594 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001595}
1596
1597/**
1598 * smack_file_fcntl - Smack check on fcntl
1599 * @file: the object
1600 * @cmd: what action to check
1601 * @arg: unused
1602 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001603 * Generally these operations are harmless.
1604 * File locking operations present an obvious mechanism
1605 * for passing information, so they require write access.
1606 *
Casey Schauflere114e472008-02-04 22:29:50 -08001607 * Returns 0 if current has access, error code otherwise
1608 */
1609static int smack_file_fcntl(struct file *file, unsigned int cmd,
1610 unsigned long arg)
1611{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001612 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001613 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001614 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001615
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001616 if (unlikely(IS_PRIVATE(inode)))
1617 return 0;
1618
Casey Schauflere114e472008-02-04 22:29:50 -08001619 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001620 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001621 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001622 case F_SETLK:
1623 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001624 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1625 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001626 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001627 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001628 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001629 case F_SETOWN:
1630 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001631 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1632 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001633 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001634 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001635 break;
1636 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001637 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001638 }
1639
1640 return rc;
1641}
1642
1643/**
Al Viroe5467852012-05-30 13:30:51 -04001644 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001645 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1646 * if mapping anonymous memory.
1647 * @file contains the file structure for file to map (may be NULL).
1648 * @reqprot contains the protection requested by the application.
1649 * @prot contains the protection that will be applied by the kernel.
1650 * @flags contains the operational flags.
1651 * Return 0 if permission is granted.
1652 */
Al Viroe5467852012-05-30 13:30:51 -04001653static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001654 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001655 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001656{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001657 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001658 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001659 struct smack_rule *srp;
1660 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001661 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001662 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001663 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001664 int may;
1665 int mmay;
1666 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001667 int rc;
1668
Al Viro496ad9a2013-01-23 17:07:38 -05001669 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001670 return 0;
1671
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001672 if (unlikely(IS_PRIVATE(file_inode(file))))
1673 return 0;
1674
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001675 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001676 if (isp->smk_mmap == NULL)
1677 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001678 sbsp = file_inode(file)->i_sb->s_security;
1679 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1680 isp->smk_mmap != sbsp->smk_root)
1681 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001682 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001683
Casey Schauflerb17103a2018-11-09 16:12:56 -08001684 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001685 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001686 rc = 0;
1687
1688 rcu_read_lock();
1689 /*
1690 * For each Smack rule associated with the subject
1691 * label verify that the SMACK64MMAP also has access
1692 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001693 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001694 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001695 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001696 /*
1697 * Matching labels always allows access.
1698 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001699 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001700 continue;
1701 /*
1702 * If there is a matching local rule take
1703 * that into account as well.
1704 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001705 may = smk_access_entry(srp->smk_subject->smk_known,
1706 okp->smk_known,
1707 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001708 if (may == -ENOENT)
1709 may = srp->smk_access;
1710 else
1711 may &= srp->smk_access;
1712 /*
1713 * If may is zero the SMACK64MMAP subject can't
1714 * possibly have less access.
1715 */
1716 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001717 continue;
1718
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001719 /*
1720 * Fetch the global list entry.
1721 * If there isn't one a SMACK64MMAP subject
1722 * can't have as much access as current.
1723 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001724 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1725 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001726 if (mmay == -ENOENT) {
1727 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001728 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001729 }
1730 /*
1731 * If there is a local entry it modifies the
1732 * potential access, too.
1733 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001734 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1735 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001736 if (tmay != -ENOENT)
1737 mmay &= tmay;
1738
1739 /*
1740 * If there is any access available to current that is
1741 * not available to a SMACK64MMAP subject
1742 * deny access.
1743 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001744 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001745 rc = -EACCES;
1746 break;
1747 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001748 }
1749
1750 rcu_read_unlock();
1751
1752 return rc;
1753}
1754
1755/**
Casey Schauflere114e472008-02-04 22:29:50 -08001756 * smack_file_set_fowner - set the file security blob value
1757 * @file: object in question
1758 *
Casey Schauflere114e472008-02-04 22:29:50 -08001759 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001760static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001761{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001762 struct smack_known **blob = smack_file(file);
1763
1764 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001765}
1766
1767/**
1768 * smack_file_send_sigiotask - Smack on sigio
1769 * @tsk: The target task
1770 * @fown: the object the signal come from
1771 * @signum: unused
1772 *
1773 * Allow a privileged task to get signals even if it shouldn't
1774 *
1775 * Returns 0 if a subject with the object's smack could
1776 * write to the task, an error code otherwise.
1777 */
1778static int smack_file_send_sigiotask(struct task_struct *tsk,
1779 struct fown_struct *fown, int signum)
1780{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001781 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001782 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001783 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001784 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001785 struct file *file;
1786 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001787 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001788
1789 /*
1790 * struct fown_struct is never outside the context of a struct file
1791 */
1792 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001793
Etienne Bassetecfcc532009-04-08 20:40:06 +02001794 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001795 blob = smack_file(file);
1796 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001797 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1798 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001799
1800 rcu_read_lock();
1801 tcred = __task_cred(tsk);
1802 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001803 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001804 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001805
1806 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1807 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001808 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001809 return rc;
1810}
1811
1812/**
1813 * smack_file_receive - Smack file receive check
1814 * @file: the object
1815 *
1816 * Returns 0 if current has access, error code otherwise
1817 */
1818static int smack_file_receive(struct file *file)
1819{
Casey Schauflerd166c802014-08-27 14:51:27 -07001820 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001821 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001822 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001823 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001824 struct socket *sock;
1825 struct task_smack *tsp;
1826 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001827
Seung-Woo Kim97775822015-04-17 15:25:04 +09001828 if (unlikely(IS_PRIVATE(inode)))
1829 return 0;
1830
Casey Schaufler4482a442013-12-30 17:37:45 -08001831 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001832 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001833
Casey Schaufler51d59af2017-05-31 08:53:42 -07001834 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001835 sock = SOCKET_I(inode);
1836 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001837 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001838 /*
1839 * If the receiving process can't write to the
1840 * passed socket or if the passed socket can't
1841 * write to the receiving process don't accept
1842 * the passed socket.
1843 */
1844 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1845 rc = smk_bu_file(file, may, rc);
1846 if (rc < 0)
1847 return rc;
1848 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1849 rc = smk_bu_file(file, may, rc);
1850 return rc;
1851 }
Casey Schauflere114e472008-02-04 22:29:50 -08001852 /*
1853 * This code relies on bitmasks.
1854 */
1855 if (file->f_mode & FMODE_READ)
1856 may = MAY_READ;
1857 if (file->f_mode & FMODE_WRITE)
1858 may |= MAY_WRITE;
1859
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001860 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001861 rc = smk_bu_file(file, may, rc);
1862 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001863}
1864
Casey Schaufler531f1d42011-09-19 12:41:42 -07001865/**
Eric Paris83d49852012-04-04 13:45:40 -04001866 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001867 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001868 *
1869 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001870 * Allow the open only if the task has read access. There are
1871 * many read operations (e.g. fstat) that you can do with an
1872 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001873 *
luanshia1a07f22019-07-05 10:35:20 +08001874 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001875 */
Al Viro94817692018-07-10 14:13:18 -04001876static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001877{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001878 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001879 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001880 struct smk_audit_info ad;
1881 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001882
Casey Schauflera6834c02014-04-21 11:10:26 -07001883 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1884 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301885 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001886 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001887
1888 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001889}
1890
Casey Schauflere114e472008-02-04 22:29:50 -08001891/*
1892 * Task hooks
1893 */
1894
1895/**
David Howellsee18d642009-09-02 09:14:21 +01001896 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001897 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001898 * @gfp: the atomicity of any memory allocations
1899 *
1900 * Prepare a blank set of credentials for modification. This must allocate all
1901 * the memory the LSM module might require such that cred_transfer() can
1902 * complete without error.
1903 */
1904static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1905{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001906 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001907 return 0;
1908}
1909
1910
1911/**
David Howellsf1752ee2008-11-14 10:39:17 +11001912 * smack_cred_free - "free" task-level security credentials
1913 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001914 *
Casey Schauflere114e472008-02-04 22:29:50 -08001915 */
David Howellsf1752ee2008-11-14 10:39:17 +11001916static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001917{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001918 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001919 struct smack_rule *rp;
1920 struct list_head *l;
1921 struct list_head *n;
1922
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001923 smk_destroy_label_list(&tsp->smk_relabel);
1924
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001925 list_for_each_safe(l, n, &tsp->smk_rules) {
1926 rp = list_entry(l, struct smack_rule, list);
1927 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001928 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001929 }
Casey Schauflere114e472008-02-04 22:29:50 -08001930}
1931
1932/**
David Howellsd84f4f92008-11-14 10:39:23 +11001933 * smack_cred_prepare - prepare new set of credentials for modification
1934 * @new: the new credentials
1935 * @old: the original credentials
1936 * @gfp: the atomicity of any memory allocations
1937 *
1938 * Prepare a new set of credentials for modification.
1939 */
1940static int smack_cred_prepare(struct cred *new, const struct cred *old,
1941 gfp_t gfp)
1942{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001943 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001944 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001945 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001946
Casey Schauflerbbd36622018-11-12 09:30:56 -08001947 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301948
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001949 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1950 if (rc != 0)
1951 return rc;
1952
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001953 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1954 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001955 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001956}
1957
Randy Dunlap251a2a92009-02-18 11:42:33 -08001958/**
David Howellsee18d642009-09-02 09:14:21 +01001959 * smack_cred_transfer - Transfer the old credentials to the new credentials
1960 * @new: the new credentials
1961 * @old: the original credentials
1962 *
1963 * Fill in a set of blank credentials from another set of credentials.
1964 */
1965static void smack_cred_transfer(struct cred *new, const struct cred *old)
1966{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001967 struct task_smack *old_tsp = smack_cred(old);
1968 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001969
1970 new_tsp->smk_task = old_tsp->smk_task;
1971 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001972 mutex_init(&new_tsp->smk_rules_lock);
1973 INIT_LIST_HEAD(&new_tsp->smk_rules);
1974
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001975 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001976}
1977
1978/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001979 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001980 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001981 * @secid: where to put the result
1982 *
1983 * Sets the secid to contain a u32 version of the smack label.
1984 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001985static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001986{
1987 struct smack_known *skp;
1988
1989 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001990 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001991 *secid = skp->smk_secid;
1992 rcu_read_unlock();
1993}
1994
1995/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001996 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001997 * @new: points to the set of credentials to be modified.
1998 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001999 *
2000 * Set the security data for a kernel service.
2001 */
2002static int smack_kernel_act_as(struct cred *new, u32 secid)
2003{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002004 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002005
Casey Schaufler152f91d2016-11-14 09:38:15 -08002006 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002007 return 0;
2008}
2009
2010/**
2011 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002012 * @new: points to the set of credentials to be modified
2013 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002014 *
2015 * Set the file creation context in a set of credentials to the same
2016 * as the objective context of the specified inode
2017 */
2018static int smack_kernel_create_files_as(struct cred *new,
2019 struct inode *inode)
2020{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002021 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002022 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002023
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002024 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002025 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002026 return 0;
2027}
2028
2029/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002030 * smk_curacc_on_task - helper to log task related access
2031 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002032 * @access: the access requested
2033 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002034 *
2035 * Return 0 if access is permitted
2036 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002037static int smk_curacc_on_task(struct task_struct *p, int access,
2038 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002039{
2040 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002041 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002042 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002043
Casey Schaufler531f1d42011-09-19 12:41:42 -07002044 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002045 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002046 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002047 rc = smk_bu_task(p, access, rc);
2048 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002049}
2050
2051/**
Casey Schauflere114e472008-02-04 22:29:50 -08002052 * smack_task_setpgid - Smack check on setting pgid
2053 * @p: the task object
2054 * @pgid: unused
2055 *
2056 * Return 0 if write access is permitted
2057 */
2058static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2059{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002060 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002061}
2062
2063/**
2064 * smack_task_getpgid - Smack access check for getpgid
2065 * @p: the object task
2066 *
2067 * Returns 0 if current can read the object task, error code otherwise
2068 */
2069static int smack_task_getpgid(struct task_struct *p)
2070{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002071 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002072}
2073
2074/**
2075 * smack_task_getsid - Smack access check for getsid
2076 * @p: the object task
2077 *
2078 * Returns 0 if current can read the object task, error code otherwise
2079 */
2080static int smack_task_getsid(struct task_struct *p)
2081{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002082 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002083}
2084
2085/**
2086 * smack_task_getsecid - get the secid of the task
2087 * @p: the object task
2088 * @secid: where to put the result
2089 *
2090 * Sets the secid to contain a u32 version of the smack label.
2091 */
2092static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2093{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002094 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002095
2096 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002097}
2098
2099/**
2100 * smack_task_setnice - Smack check on setting nice
2101 * @p: the task object
2102 * @nice: unused
2103 *
2104 * Return 0 if write access is permitted
2105 */
2106static int smack_task_setnice(struct task_struct *p, int nice)
2107{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002108 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002109}
2110
2111/**
2112 * smack_task_setioprio - Smack check on setting ioprio
2113 * @p: the task object
2114 * @ioprio: unused
2115 *
2116 * Return 0 if write access is permitted
2117 */
2118static int smack_task_setioprio(struct task_struct *p, int ioprio)
2119{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002120 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002121}
2122
2123/**
2124 * smack_task_getioprio - Smack check on reading ioprio
2125 * @p: the task object
2126 *
2127 * Return 0 if read access is permitted
2128 */
2129static int smack_task_getioprio(struct task_struct *p)
2130{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002131 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002132}
2133
2134/**
2135 * smack_task_setscheduler - Smack check on setting scheduler
2136 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002137 *
2138 * Return 0 if read access is permitted
2139 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002140static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002141{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002142 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002143}
2144
2145/**
2146 * smack_task_getscheduler - Smack check on reading scheduler
2147 * @p: the task object
2148 *
2149 * Return 0 if read access is permitted
2150 */
2151static int smack_task_getscheduler(struct task_struct *p)
2152{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002153 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002154}
2155
2156/**
2157 * smack_task_movememory - Smack check on moving memory
2158 * @p: the task object
2159 *
2160 * Return 0 if write access is permitted
2161 */
2162static int smack_task_movememory(struct task_struct *p)
2163{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002164 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002165}
2166
2167/**
2168 * smack_task_kill - Smack check on signal delivery
2169 * @p: the task object
2170 * @info: unused
2171 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002172 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002173 *
2174 * Return 0 if write access is permitted
2175 *
Casey Schauflere114e472008-02-04 22:29:50 -08002176 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002177static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002178 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002179{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002180 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002181 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002182 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002183 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002184
Rafal Krypa18d872f2016-04-04 11:14:53 +02002185 if (!sig)
2186 return 0; /* null signal; existence test */
2187
Etienne Bassetecfcc532009-04-08 20:40:06 +02002188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2189 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002190 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002191 * Sending a signal requires that the sender
2192 * can write the receiver.
2193 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002194 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002195 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2196 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002197 return rc;
2198 }
Casey Schauflere114e472008-02-04 22:29:50 -08002199 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002200 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002201 * specific behavior. This is not clean. For one thing
2202 * we can't take privilege into account.
2203 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002204 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002205 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2206 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002207 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002208}
2209
2210/**
Casey Schauflere114e472008-02-04 22:29:50 -08002211 * smack_task_to_inode - copy task smack into the inode blob
2212 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002213 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002214 *
2215 * Sets the smack pointer in the inode security blob
2216 */
2217static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2218{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002219 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002220 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002221
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002222 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002223 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002224}
2225
2226/*
2227 * Socket hooks.
2228 */
2229
2230/**
2231 * smack_sk_alloc_security - Allocate a socket blob
2232 * @sk: the socket
2233 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002234 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002235 *
2236 * Assign Smack pointers to current
2237 *
2238 * Returns 0 on success, -ENOMEM is there's no memory
2239 */
2240static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2241{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002242 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002243 struct socket_smack *ssp;
2244
2245 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2246 if (ssp == NULL)
2247 return -ENOMEM;
2248
jooseong lee08382c92016-11-03 11:54:39 +01002249 /*
2250 * Sockets created by kernel threads receive web label.
2251 */
2252 if (unlikely(current->flags & PF_KTHREAD)) {
2253 ssp->smk_in = &smack_known_web;
2254 ssp->smk_out = &smack_known_web;
2255 } else {
2256 ssp->smk_in = skp;
2257 ssp->smk_out = skp;
2258 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002259 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002260
2261 sk->sk_security = ssp;
2262
2263 return 0;
2264}
2265
2266/**
2267 * smack_sk_free_security - Free a socket blob
2268 * @sk: the socket
2269 *
2270 * Clears the blob pointer
2271 */
2272static void smack_sk_free_security(struct sock *sk)
2273{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302274#ifdef SMACK_IPV6_PORT_LABELING
2275 struct smk_port_label *spp;
2276
2277 if (sk->sk_family == PF_INET6) {
2278 rcu_read_lock();
2279 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2280 if (spp->smk_sock != sk)
2281 continue;
2282 spp->smk_can_reuse = 1;
2283 break;
2284 }
2285 rcu_read_unlock();
2286 }
2287#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002288 kfree(sk->sk_security);
2289}
2290
2291/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002292* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002293* @sip: the object end
2294*
2295* looks for host based access restrictions
2296*
2297* This version will only be appropriate for really small sets of single label
2298* hosts. The caller is responsible for ensuring that the RCU read lock is
2299* taken before calling this function.
2300*
2301* Returns the label of the far end or NULL if it's not special.
2302*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002303static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002304{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002305 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002306 struct in_addr *siap = &sip->sin_addr;
2307
2308 if (siap->s_addr == 0)
2309 return NULL;
2310
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002311 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2312 /*
2313 * we break after finding the first match because
2314 * the list is sorted from longest to shortest mask
2315 * so we have found the most specific match
2316 */
2317 if (snp->smk_host.s_addr ==
2318 (siap->s_addr & snp->smk_mask.s_addr))
2319 return snp->smk_label;
2320
2321 return NULL;
2322}
2323
2324#if IS_ENABLED(CONFIG_IPV6)
2325/*
2326 * smk_ipv6_localhost - Check for local ipv6 host address
2327 * @sip: the address
2328 *
2329 * Returns boolean true if this is the localhost address
2330 */
2331static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2332{
2333 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2334 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2335
2336 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2337 ntohs(be16p[7]) == 1)
2338 return true;
2339 return false;
2340}
2341
2342/**
2343* smack_ipv6host_label - check host based restrictions
2344* @sip: the object end
2345*
2346* looks for host based access restrictions
2347*
2348* This version will only be appropriate for really small sets of single label
2349* hosts. The caller is responsible for ensuring that the RCU read lock is
2350* taken before calling this function.
2351*
2352* Returns the label of the far end or NULL if it's not special.
2353*/
2354static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2355{
2356 struct smk_net6addr *snp;
2357 struct in6_addr *sap = &sip->sin6_addr;
2358 int i;
2359 int found = 0;
2360
2361 /*
2362 * It's local. Don't look for a host label.
2363 */
2364 if (smk_ipv6_localhost(sip))
2365 return NULL;
2366
2367 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002368 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002369 * If the label is NULL the entry has
2370 * been renounced. Ignore it.
2371 */
2372 if (snp->smk_label == NULL)
2373 continue;
2374 /*
Paul Moore07feee82009-03-27 17:10:54 -04002375 * we break after finding the first match because
2376 * the list is sorted from longest to shortest mask
2377 * so we have found the most specific match
2378 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002379 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002380 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2381 snp->smk_host.s6_addr16[i]) {
2382 found = 0;
2383 break;
2384 }
Etienne Basset43031542009-03-27 17:11:01 -04002385 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002386 if (found)
2387 return snp->smk_label;
2388 }
Paul Moore07feee82009-03-27 17:10:54 -04002389
2390 return NULL;
2391}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002392#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002393
2394/**
Casey Schauflere114e472008-02-04 22:29:50 -08002395 * smack_netlabel - Set the secattr on a socket
2396 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002397 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002398 *
2399 * Convert the outbound smack value (smk_out) to a
2400 * secattr and attach it to the socket.
2401 *
2402 * Returns 0 on success or an error code
2403 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002404static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002405{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002406 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002407 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002408 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002409
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002410 /*
2411 * Usually the netlabel code will handle changing the
2412 * packet labeling based on the label.
2413 * The case of a single label host is different, because
2414 * a single label host should never get a labeled packet
2415 * even though the label is usually associated with a packet
2416 * label.
2417 */
2418 local_bh_disable();
2419 bh_lock_sock_nested(sk);
2420
2421 if (ssp->smk_out == smack_net_ambient ||
2422 labeled == SMACK_UNLABELED_SOCKET)
2423 netlbl_sock_delattr(sk);
2424 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002425 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002426 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002427 }
2428
2429 bh_unlock_sock(sk);
2430 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002431
Casey Schauflere114e472008-02-04 22:29:50 -08002432 return rc;
2433}
2434
2435/**
Paul Moore07feee82009-03-27 17:10:54 -04002436 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2437 * @sk: the socket
2438 * @sap: the destination address
2439 *
2440 * Set the correct secattr for the given socket based on the destination
2441 * address and perform any outbound access checks needed.
2442 *
2443 * Returns 0 on success or an error code.
2444 *
2445 */
2446static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2447{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002448 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002449 int rc;
2450 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002451 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002452 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002453 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002454
2455 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002456 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002457 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002458#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002459 struct lsm_network_audit net;
2460
Eric Paris48c62af2012-04-02 13:15:44 -04002461 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2462 ad.a.u.net->family = sap->sin_family;
2463 ad.a.u.net->dport = sap->sin_port;
2464 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002465#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002466 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002467 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002468 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2469 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002470 } else {
2471 sk_lbl = SMACK_CIPSO_SOCKET;
2472 rc = 0;
2473 }
2474 rcu_read_unlock();
2475 if (rc != 0)
2476 return rc;
2477
2478 return smack_netlabel(sk, sk_lbl);
2479}
2480
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002481#if IS_ENABLED(CONFIG_IPV6)
2482/**
2483 * smk_ipv6_check - check Smack access
2484 * @subject: subject Smack label
2485 * @object: object Smack label
2486 * @address: address
2487 * @act: the action being taken
2488 *
2489 * Check an IPv6 access
2490 */
2491static int smk_ipv6_check(struct smack_known *subject,
2492 struct smack_known *object,
2493 struct sockaddr_in6 *address, int act)
2494{
2495#ifdef CONFIG_AUDIT
2496 struct lsm_network_audit net;
2497#endif
2498 struct smk_audit_info ad;
2499 int rc;
2500
2501#ifdef CONFIG_AUDIT
2502 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2503 ad.a.u.net->family = PF_INET6;
2504 ad.a.u.net->dport = ntohs(address->sin6_port);
2505 if (act == SMK_RECEIVING)
2506 ad.a.u.net->v6info.saddr = address->sin6_addr;
2507 else
2508 ad.a.u.net->v6info.daddr = address->sin6_addr;
2509#endif
2510 rc = smk_access(subject, object, MAY_WRITE, &ad);
2511 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2512 return rc;
2513}
2514#endif /* CONFIG_IPV6 */
2515
2516#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002517/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002518 * smk_ipv6_port_label - Smack port access table management
2519 * @sock: socket
2520 * @address: address
2521 *
2522 * Create or update the port list entry
2523 */
2524static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2525{
2526 struct sock *sk = sock->sk;
2527 struct sockaddr_in6 *addr6;
2528 struct socket_smack *ssp = sock->sk->sk_security;
2529 struct smk_port_label *spp;
2530 unsigned short port = 0;
2531
2532 if (address == NULL) {
2533 /*
2534 * This operation is changing the Smack information
2535 * on the bound socket. Take the changes to the port
2536 * as well.
2537 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302538 rcu_read_lock();
2539 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002540 if (sk != spp->smk_sock)
2541 continue;
2542 spp->smk_in = ssp->smk_in;
2543 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302544 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002545 return;
2546 }
2547 /*
2548 * A NULL address is only used for updating existing
2549 * bound entries. If there isn't one, it's OK.
2550 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302551 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002552 return;
2553 }
2554
2555 addr6 = (struct sockaddr_in6 *)address;
2556 port = ntohs(addr6->sin6_port);
2557 /*
2558 * This is a special case that is safely ignored.
2559 */
2560 if (port == 0)
2561 return;
2562
2563 /*
2564 * Look for an existing port list entry.
2565 * This is an indication that a port is getting reused.
2566 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302567 rcu_read_lock();
2568 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302569 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002570 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302571 if (spp->smk_can_reuse != 1) {
2572 rcu_read_unlock();
2573 return;
2574 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002575 spp->smk_port = port;
2576 spp->smk_sock = sk;
2577 spp->smk_in = ssp->smk_in;
2578 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302579 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302580 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002581 return;
2582 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302583 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002584 /*
2585 * A new port entry is required.
2586 */
2587 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2588 if (spp == NULL)
2589 return;
2590
2591 spp->smk_port = port;
2592 spp->smk_sock = sk;
2593 spp->smk_in = ssp->smk_in;
2594 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302595 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302596 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002597
Vishal Goel3c7ce342016-11-23 10:31:08 +05302598 mutex_lock(&smack_ipv6_lock);
2599 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2600 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002601 return;
2602}
2603
2604/**
2605 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002606 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002607 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002608 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002609 *
2610 * Create or update the port list entry
2611 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002612static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002613 int act)
2614{
Casey Schauflerc6739442013-05-22 18:42:56 -07002615 struct smk_port_label *spp;
2616 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002617 struct smack_known *skp = NULL;
2618 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002619 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002620
2621 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002622 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002623 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002624 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002625 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002626 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002627 }
2628
2629 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002630 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002631 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002632 if (skp != NULL && object != NULL)
2633 return smk_ipv6_check(skp, object, address, act);
2634 if (skp == NULL)
2635 skp = smack_net_ambient;
2636 if (object == NULL)
2637 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002638
2639 /*
2640 * It's remote, so port lookup does no good.
2641 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002642 if (!smk_ipv6_localhost(address))
2643 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002644
2645 /*
2646 * It's local so the send check has to have passed.
2647 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002648 if (act == SMK_RECEIVING)
2649 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002650
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002651 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302652 rcu_read_lock();
2653 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302654 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002655 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002656 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002657 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002658 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002659 break;
2660 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302661 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002662
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002663 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002664}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002665#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002666
2667/**
Casey Schauflere114e472008-02-04 22:29:50 -08002668 * smack_inode_setsecurity - set smack xattrs
2669 * @inode: the object
2670 * @name: attribute name
2671 * @value: attribute value
2672 * @size: size of the attribute
2673 * @flags: unused
2674 *
2675 * Sets the named attribute in the appropriate blob
2676 *
2677 * Returns 0 on success, or an error code
2678 */
2679static int smack_inode_setsecurity(struct inode *inode, const char *name,
2680 const void *value, size_t size, int flags)
2681{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002682 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002683 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002684 struct socket_smack *ssp;
2685 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002686 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002687
Casey Schauflerf7112e62012-05-06 15:22:02 -07002688 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302689 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002690
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002691 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002692 if (IS_ERR(skp))
2693 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002694
2695 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002696 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002697 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002698 return 0;
2699 }
2700 /*
2701 * The rest of the Smack xattrs are only on sockets.
2702 */
2703 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2704 return -EOPNOTSUPP;
2705
2706 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002707 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002708 return -EOPNOTSUPP;
2709
2710 ssp = sock->sk->sk_security;
2711
2712 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002713 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002714 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002715 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002716 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002717 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2718 if (rc != 0)
2719 printk(KERN_WARNING
2720 "Smack: \"%s\" netlbl error %d.\n",
2721 __func__, -rc);
2722 }
Casey Schauflere114e472008-02-04 22:29:50 -08002723 } else
2724 return -EOPNOTSUPP;
2725
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002726#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002727 if (sock->sk->sk_family == PF_INET6)
2728 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002729#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002730
Casey Schauflere114e472008-02-04 22:29:50 -08002731 return 0;
2732}
2733
2734/**
2735 * smack_socket_post_create - finish socket setup
2736 * @sock: the socket
2737 * @family: protocol family
2738 * @type: unused
2739 * @protocol: unused
2740 * @kern: unused
2741 *
2742 * Sets the netlabel information on the socket
2743 *
2744 * Returns 0 on success, and error code otherwise
2745 */
2746static int smack_socket_post_create(struct socket *sock, int family,
2747 int type, int protocol, int kern)
2748{
Marcin Lis74123012015-01-22 15:40:33 +01002749 struct socket_smack *ssp;
2750
2751 if (sock->sk == NULL)
2752 return 0;
2753
2754 /*
2755 * Sockets created by kernel threads receive web label.
2756 */
2757 if (unlikely(current->flags & PF_KTHREAD)) {
2758 ssp = sock->sk->sk_security;
2759 ssp->smk_in = &smack_known_web;
2760 ssp->smk_out = &smack_known_web;
2761 }
2762
2763 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002764 return 0;
2765 /*
2766 * Set the outbound netlbl.
2767 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002768 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2769}
2770
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002771/**
2772 * smack_socket_socketpair - create socket pair
2773 * @socka: one socket
2774 * @sockb: another socket
2775 *
2776 * Cross reference the peer labels for SO_PEERSEC
2777 *
luanshia1a07f22019-07-05 10:35:20 +08002778 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002779 */
2780static int smack_socket_socketpair(struct socket *socka,
2781 struct socket *sockb)
2782{
2783 struct socket_smack *asp = socka->sk->sk_security;
2784 struct socket_smack *bsp = sockb->sk->sk_security;
2785
2786 asp->smk_packet = bsp->smk_out;
2787 bsp->smk_packet = asp->smk_out;
2788
2789 return 0;
2790}
2791
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002792#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002793/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002794 * smack_socket_bind - record port binding information.
2795 * @sock: the socket
2796 * @address: the port address
2797 * @addrlen: size of the address
2798 *
2799 * Records the label bound to a port.
2800 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002801 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002802 */
2803static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2804 int addrlen)
2805{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002806 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2807 if (addrlen < SIN6_LEN_RFC2133 ||
2808 address->sa_family != AF_INET6)
2809 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002810 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002811 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002812 return 0;
2813}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002814#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002815
2816/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002817 * smack_socket_connect - connect access check
2818 * @sock: the socket
2819 * @sap: the other end
2820 * @addrlen: size of sap
2821 *
2822 * Verifies that a connection may be possible
2823 *
2824 * Returns 0 on success, and error code otherwise
2825 */
2826static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2827 int addrlen)
2828{
Casey Schauflerc6739442013-05-22 18:42:56 -07002829 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002830
Casey Schauflerc6739442013-05-22 18:42:56 -07002831 if (sock->sk == NULL)
2832 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002833 if (sock->sk->sk_family != PF_INET &&
2834 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2835 return 0;
2836 if (addrlen < offsetofend(struct sockaddr, sa_family))
2837 return 0;
2838 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2839 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002840#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002841 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002842#endif
2843
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002844 if (addrlen < SIN6_LEN_RFC2133)
2845 return 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002846#ifdef SMACK_IPV6_SECMARK_LABELING
2847 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002848 if (rsp != NULL) {
2849 struct socket_smack *ssp = sock->sk->sk_security;
2850
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002851 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002852 SMK_CONNECTING);
2853 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002854#endif
2855#ifdef SMACK_IPV6_PORT_LABELING
2856 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2857#endif
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002858 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002859 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002860 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2861 return 0;
2862 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002863 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002864}
2865
2866/**
2867 * smack_flags_to_may - convert S_ to MAY_ values
2868 * @flags: the S_ value
2869 *
2870 * Returns the equivalent MAY_ value
2871 */
2872static int smack_flags_to_may(int flags)
2873{
2874 int may = 0;
2875
2876 if (flags & S_IRUGO)
2877 may |= MAY_READ;
2878 if (flags & S_IWUGO)
2879 may |= MAY_WRITE;
2880 if (flags & S_IXUGO)
2881 may |= MAY_EXEC;
2882
2883 return may;
2884}
2885
2886/**
2887 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2888 * @msg: the object
2889 *
2890 * Returns 0
2891 */
2892static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2893{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002894 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002895
Casey Schauflerecd5f822018-11-20 11:55:02 -08002896 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002897 return 0;
2898}
2899
2900/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002901 * smack_of_ipc - the smack pointer for the ipc
2902 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002903 *
2904 * Returns a pointer to the smack value
2905 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002906static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002907{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002908 struct smack_known **blob = smack_ipc(isp);
2909
2910 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002911}
2912
2913/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002914 * smack_ipc_alloc_security - Set the security blob for ipc
2915 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002916 *
2917 * Returns 0
2918 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002919static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002920{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002921 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002922
Casey Schaufler019bcca2018-09-21 17:19:54 -07002923 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002924 return 0;
2925}
2926
2927/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002928 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002929 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002930 * @access : access requested
2931 *
2932 * Returns 0 if current has the requested access, error code otherwise
2933 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002934static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002935{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002936 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002937 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002938 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002939
2940#ifdef CONFIG_AUDIT
2941 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002942 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002943#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002944 rc = smk_curacc(ssp, access, &ad);
2945 rc = smk_bu_current("shm", ssp, access, rc);
2946 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002947}
2948
2949/**
Casey Schauflere114e472008-02-04 22:29:50 -08002950 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002951 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002952 * @shmflg: access requested
2953 *
2954 * Returns 0 if current has the requested access, error code otherwise
2955 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002956static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002957{
Casey Schauflere114e472008-02-04 22:29:50 -08002958 int may;
2959
2960 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002961 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002962}
2963
2964/**
2965 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002966 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002967 * @cmd: what it wants to do
2968 *
2969 * Returns 0 if current has the requested access, error code otherwise
2970 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002971static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002972{
Casey Schauflere114e472008-02-04 22:29:50 -08002973 int may;
2974
2975 switch (cmd) {
2976 case IPC_STAT:
2977 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002978 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002979 may = MAY_READ;
2980 break;
2981 case IPC_SET:
2982 case SHM_LOCK:
2983 case SHM_UNLOCK:
2984 case IPC_RMID:
2985 may = MAY_READWRITE;
2986 break;
2987 case IPC_INFO:
2988 case SHM_INFO:
2989 /*
2990 * System level information.
2991 */
2992 return 0;
2993 default:
2994 return -EINVAL;
2995 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002996 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002997}
2998
2999/**
3000 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003001 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003002 * @shmaddr: unused
3003 * @shmflg: access requested
3004 *
3005 * Returns 0 if current has the requested access, error code otherwise
3006 */
luanshia1a07f22019-07-05 10:35:20 +08003007static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003008 int shmflg)
3009{
Casey Schauflere114e472008-02-04 22:29:50 -08003010 int may;
3011
3012 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003013 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003014}
3015
3016/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003017 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003018 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003019 * @access : access requested
3020 *
3021 * Returns 0 if current has the requested access, error code otherwise
3022 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003023static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003024{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003025 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003026 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003027 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003028
3029#ifdef CONFIG_AUDIT
3030 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003031 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003033 rc = smk_curacc(ssp, access, &ad);
3034 rc = smk_bu_current("sem", ssp, access, rc);
3035 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003036}
3037
3038/**
Casey Schauflere114e472008-02-04 22:29:50 -08003039 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003040 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003041 * @semflg: access requested
3042 *
3043 * Returns 0 if current has the requested access, error code otherwise
3044 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003045static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003046{
Casey Schauflere114e472008-02-04 22:29:50 -08003047 int may;
3048
3049 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003050 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003051}
3052
3053/**
3054 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003055 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003056 * @cmd: what it wants to do
3057 *
3058 * Returns 0 if current has the requested access, error code otherwise
3059 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003060static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003061{
Casey Schauflere114e472008-02-04 22:29:50 -08003062 int may;
3063
3064 switch (cmd) {
3065 case GETPID:
3066 case GETNCNT:
3067 case GETZCNT:
3068 case GETVAL:
3069 case GETALL:
3070 case IPC_STAT:
3071 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003072 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003073 may = MAY_READ;
3074 break;
3075 case SETVAL:
3076 case SETALL:
3077 case IPC_RMID:
3078 case IPC_SET:
3079 may = MAY_READWRITE;
3080 break;
3081 case IPC_INFO:
3082 case SEM_INFO:
3083 /*
3084 * System level information
3085 */
3086 return 0;
3087 default:
3088 return -EINVAL;
3089 }
3090
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003091 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003092}
3093
3094/**
3095 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003096 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003097 * @sops: unused
3098 * @nsops: unused
3099 * @alter: unused
3100 *
3101 * Treated as read and write in all cases.
3102 *
3103 * Returns 0 if access is allowed, error code otherwise
3104 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003105static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003106 unsigned nsops, int alter)
3107{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003108 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003109}
3110
3111/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003112 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003113 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003114 * @access : access requested
3115 *
3116 * return 0 if current has access, error otherwise
3117 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003118static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003119{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003120 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003121 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003122 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003123
3124#ifdef CONFIG_AUDIT
3125 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003126 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003127#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003128 rc = smk_curacc(msp, access, &ad);
3129 rc = smk_bu_current("msq", msp, access, rc);
3130 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003131}
3132
3133/**
Casey Schauflere114e472008-02-04 22:29:50 -08003134 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003135 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003136 * @msqflg: access requested
3137 *
3138 * Returns 0 if current has the requested access, error code otherwise
3139 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003140static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003141{
Casey Schauflere114e472008-02-04 22:29:50 -08003142 int may;
3143
3144 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003145 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003146}
3147
3148/**
3149 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003150 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003151 * @cmd: what it wants to do
3152 *
3153 * Returns 0 if current has the requested access, error code otherwise
3154 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003155static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003156{
Casey Schauflere114e472008-02-04 22:29:50 -08003157 int may;
3158
3159 switch (cmd) {
3160 case IPC_STAT:
3161 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003162 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003163 may = MAY_READ;
3164 break;
3165 case IPC_SET:
3166 case IPC_RMID:
3167 may = MAY_READWRITE;
3168 break;
3169 case IPC_INFO:
3170 case MSG_INFO:
3171 /*
3172 * System level information
3173 */
3174 return 0;
3175 default:
3176 return -EINVAL;
3177 }
3178
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003179 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003180}
3181
3182/**
3183 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003184 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003185 * @msg: unused
3186 * @msqflg: access requested
3187 *
3188 * Returns 0 if current has the requested access, error code otherwise
3189 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003190static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003191 int msqflg)
3192{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003193 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003194
Etienne Bassetecfcc532009-04-08 20:40:06 +02003195 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003196 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003197}
3198
3199/**
3200 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003201 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003202 * @msg: unused
3203 * @target: unused
3204 * @type: unused
3205 * @mode: unused
3206 *
3207 * Returns 0 if current has read and write access, error code otherwise
3208 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003209static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003210 struct task_struct *target, long type, int mode)
3211{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003212 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003213}
3214
3215/**
3216 * smack_ipc_permission - Smack access for ipc_permission()
3217 * @ipp: the object permissions
3218 * @flag: access requested
3219 *
3220 * Returns 0 if current has read and write access, error code otherwise
3221 */
3222static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3223{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003224 struct smack_known **blob = smack_ipc(ipp);
3225 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003226 int may = smack_flags_to_may(flag);
3227 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003228 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003229
Etienne Bassetecfcc532009-04-08 20:40:06 +02003230#ifdef CONFIG_AUDIT
3231 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3232 ad.a.u.ipc_id = ipp->id;
3233#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003234 rc = smk_curacc(iskp, may, &ad);
3235 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003236 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003237}
3238
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003239/**
3240 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003241 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003242 * @secid: where result will be saved
3243 */
3244static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3245{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003246 struct smack_known **blob = smack_ipc(ipp);
3247 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003248
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003249 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003250}
3251
Casey Schauflere114e472008-02-04 22:29:50 -08003252/**
3253 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003254 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003255 * @inode: the object
3256 *
3257 * Set the inode's security blob if it hasn't been done already.
3258 */
3259static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3260{
3261 struct super_block *sbp;
3262 struct superblock_smack *sbsp;
3263 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003264 struct smack_known *skp;
3265 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003266 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003267 char trattr[TRANS_TRUE_SIZE];
3268 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003269 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003270 struct dentry *dp;
3271
3272 if (inode == NULL)
3273 return;
3274
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003275 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003276
3277 mutex_lock(&isp->smk_lock);
3278 /*
3279 * If the inode is already instantiated
3280 * take the quick way out
3281 */
3282 if (isp->smk_flags & SMK_INODE_INSTANT)
3283 goto unlockandout;
3284
3285 sbp = inode->i_sb;
3286 sbsp = sbp->s_security;
3287 /*
3288 * We're going to use the superblock default label
3289 * if there's no label on the file.
3290 */
3291 final = sbsp->smk_default;
3292
3293 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003294 * If this is the root inode the superblock
3295 * may be in the process of initialization.
3296 * If that is the case use the root value out
3297 * of the superblock.
3298 */
3299 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003300 switch (sbp->s_magic) {
3301 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003302 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003303 /*
3304 * The cgroup filesystem is never mounted,
3305 * so there's no opportunity to set the mount
3306 * options.
3307 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003308 sbsp->smk_root = &smack_known_star;
3309 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003310 isp->smk_inode = sbsp->smk_root;
3311 break;
3312 case TMPFS_MAGIC:
3313 /*
3314 * What about shmem/tmpfs anonymous files with dentry
3315 * obtained from d_alloc_pseudo()?
3316 */
3317 isp->smk_inode = smk_of_current();
3318 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003319 case PIPEFS_MAGIC:
3320 isp->smk_inode = smk_of_current();
3321 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003322 case SOCKFS_MAGIC:
3323 /*
3324 * Socket access is controlled by the socket
3325 * structures associated with the task involved.
3326 */
3327 isp->smk_inode = &smack_known_star;
3328 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003329 default:
3330 isp->smk_inode = sbsp->smk_root;
3331 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003332 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003333 isp->smk_flags |= SMK_INODE_INSTANT;
3334 goto unlockandout;
3335 }
3336
3337 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003338 * This is pretty hackish.
3339 * Casey says that we shouldn't have to do
3340 * file system specific code, but it does help
3341 * with keeping it simple.
3342 */
3343 switch (sbp->s_magic) {
3344 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003345 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003346 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003347 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003348 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003349 * that the smack file system doesn't do
3350 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003351 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003352 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003353 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003354 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003355 break;
3356 case DEVPTS_SUPER_MAGIC:
3357 /*
3358 * devpts seems content with the label of the task.
3359 * Programs that change smack have to treat the
3360 * pty with respect.
3361 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003362 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003363 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003364 case PROC_SUPER_MAGIC:
3365 /*
3366 * Casey says procfs appears not to care.
3367 * The superblock default suffices.
3368 */
3369 break;
3370 case TMPFS_MAGIC:
3371 /*
3372 * Device labels should come from the filesystem,
3373 * but watch out, because they're volitile,
3374 * getting recreated on every reboot.
3375 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003376 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003377 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003378 * If a smack value has been set we want to use it,
3379 * but since tmpfs isn't giving us the opportunity
3380 * to set mount options simulate setting the
3381 * superblock default.
3382 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003383 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003384 default:
3385 /*
3386 * This isn't an understood special case.
3387 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003388 */
3389
3390 /*
3391 * UNIX domain sockets use lower level socket data.
3392 */
3393 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003394 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003395 break;
3396 }
3397 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003398 * No xattr support means, alas, no SMACK label.
3399 * Use the aforeapplied default.
3400 * It would be curious if the label of the task
3401 * does not match that assigned.
3402 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003403 if (!(inode->i_opflags & IOP_XATTR))
3404 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003405 /*
3406 * Get the dentry for xattr.
3407 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003408 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003409 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003410 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003411 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003412
3413 /*
3414 * Transmuting directory
3415 */
3416 if (S_ISDIR(inode->i_mode)) {
3417 /*
3418 * If this is a new directory and the label was
3419 * transmuted when the inode was initialized
3420 * set the transmute attribute on the directory
3421 * and mark the inode.
3422 *
3423 * If there is a transmute attribute on the
3424 * directory mark the inode.
3425 */
3426 if (isp->smk_flags & SMK_INODE_CHANGED) {
3427 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003428 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003429 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003430 TRANS_TRUE, TRANS_TRUE_SIZE,
3431 0);
3432 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003433 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003434 XATTR_NAME_SMACKTRANSMUTE, trattr,
3435 TRANS_TRUE_SIZE);
3436 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3437 TRANS_TRUE_SIZE) != 0)
3438 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003439 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003440 if (rc >= 0)
3441 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003442 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003443 /*
3444 * Don't let the exec or mmap label be "*" or "@".
3445 */
3446 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3447 if (IS_ERR(skp) || skp == &smack_known_star ||
3448 skp == &smack_known_web)
3449 skp = NULL;
3450 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003451
Casey Schaufler19760ad2013-12-16 16:27:26 -08003452 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003453 if (IS_ERR(skp) || skp == &smack_known_star ||
3454 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003455 skp = NULL;
3456 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003457
Casey Schauflere114e472008-02-04 22:29:50 -08003458 dput(dp);
3459 break;
3460 }
3461
3462 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003463 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003464 else
3465 isp->smk_inode = final;
3466
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003467 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003468
3469unlockandout:
3470 mutex_unlock(&isp->smk_lock);
3471 return;
3472}
3473
3474/**
3475 * smack_getprocattr - Smack process attribute access
3476 * @p: the object task
3477 * @name: the name of the attribute in /proc/.../attr
3478 * @value: where to put the result
3479 *
3480 * Places a copy of the task Smack into value
3481 *
3482 * Returns the length of the smack label or an error code
3483 */
3484static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3485{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003486 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003487 char *cp;
3488 int slen;
3489
3490 if (strcmp(name, "current") != 0)
3491 return -EINVAL;
3492
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003493 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003494 if (cp == NULL)
3495 return -ENOMEM;
3496
3497 slen = strlen(cp);
3498 *value = cp;
3499 return slen;
3500}
3501
3502/**
3503 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003504 * @name: the name of the attribute in /proc/.../attr
3505 * @value: the value to set
3506 * @size: the size of the value
3507 *
3508 * Sets the Smack value of the task. Only setting self
3509 * is permitted and only with privilege
3510 *
3511 * Returns the length of the smack label or an error code
3512 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003513static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003514{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003515 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003516 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003517 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003518 struct smack_known_list_elem *sklep;
3519 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003520
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003521 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003522 return -EPERM;
3523
Casey Schauflerf7112e62012-05-06 15:22:02 -07003524 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003525 return -EINVAL;
3526
3527 if (strcmp(name, "current") != 0)
3528 return -EINVAL;
3529
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003530 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003531 if (IS_ERR(skp))
3532 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003533
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003534 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303535 * No process is ever allowed the web ("@") label
3536 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003537 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303538 if (skp == &smack_known_web || skp == &smack_known_star)
3539 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003540
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003541 if (!smack_privileged(CAP_MAC_ADMIN)) {
3542 rc = -EPERM;
3543 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3544 if (sklep->smk_label == skp) {
3545 rc = 0;
3546 break;
3547 }
3548 if (rc)
3549 return rc;
3550 }
3551
David Howellsd84f4f92008-11-14 10:39:23 +11003552 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003553 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003554 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003555
Casey Schauflerb17103a2018-11-09 16:12:56 -08003556 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003557 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003558 /*
3559 * process can change its label only once
3560 */
3561 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003562
David Howellsd84f4f92008-11-14 10:39:23 +11003563 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003564 return size;
3565}
3566
3567/**
3568 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003569 * @sock: one sock
3570 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003571 * @newsk: unused
3572 *
3573 * Return 0 if a subject with the smack of sock could access
3574 * an object with the smack of other, otherwise an error code
3575 */
David S. Miller3610cda2011-01-05 15:38:53 -08003576static int smack_unix_stream_connect(struct sock *sock,
3577 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003578{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003579 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003580 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003581 struct socket_smack *ssp = sock->sk_security;
3582 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003583 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003584 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003585 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003586#ifdef CONFIG_AUDIT
3587 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003588#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003589
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003590 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3591 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003592 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003593#ifdef CONFIG_AUDIT
3594 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3595 smk_ad_setfield_u_net_sk(&ad, other);
3596#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003597 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3598 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003599 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003600 okp = osp->smk_out;
3601 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003602 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003603 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003604 MAY_WRITE, rc);
3605 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003606 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003607
Casey Schaufler975d5e52011-09-26 14:43:39 -07003608 /*
3609 * Cross reference the peer labels for SO_PEERSEC.
3610 */
3611 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003612 nsp->smk_packet = ssp->smk_out;
3613 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003614 }
3615
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003616 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003617}
3618
3619/**
3620 * smack_unix_may_send - Smack access on UDS
3621 * @sock: one socket
3622 * @other: the other socket
3623 *
3624 * Return 0 if a subject with the smack of sock could access
3625 * an object with the smack of other, otherwise an error code
3626 */
3627static int smack_unix_may_send(struct socket *sock, struct socket *other)
3628{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003629 struct socket_smack *ssp = sock->sk->sk_security;
3630 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003631 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003632 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003633
Kees Cook923e9a12012-04-10 13:26:44 -07003634#ifdef CONFIG_AUDIT
3635 struct lsm_network_audit net;
3636
Eric Paris48c62af2012-04-02 13:15:44 -04003637 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003638 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003639#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003640
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003641 if (smack_privileged(CAP_MAC_OVERRIDE))
3642 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003643
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003644 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3645 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003646 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003647}
3648
3649/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003650 * smack_socket_sendmsg - Smack check based on destination host
3651 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003652 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003653 * @size: the size of the message
3654 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003655 * Return 0 if the current subject can write to the destination host.
3656 * For IPv4 this is only a question if the destination is a single label host.
3657 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003658 */
3659static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3660 int size)
3661{
3662 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003663#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003664 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003665#endif
3666#ifdef SMACK_IPV6_SECMARK_LABELING
3667 struct socket_smack *ssp = sock->sk->sk_security;
3668 struct smack_known *rsp;
3669#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003670 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003671
3672 /*
3673 * Perfectly reasonable for this to be NULL
3674 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003675 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003676 return 0;
3677
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003678 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003679 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003680 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3681 sip->sin_family != AF_INET)
3682 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003683 rc = smack_netlabel_send(sock->sk, sip);
3684 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003685#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003686 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003687 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3688 sap->sin6_family != AF_INET6)
3689 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003690#ifdef SMACK_IPV6_SECMARK_LABELING
3691 rsp = smack_ipv6host_label(sap);
3692 if (rsp != NULL)
3693 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3694 SMK_CONNECTING);
3695#endif
3696#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003697 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003698#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003699#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003700 break;
3701 }
3702 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003703}
3704
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003705/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003706 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003707 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003708 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003709 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003710 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003711 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003712static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3713 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003714{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003715 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003716 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003717 int acat;
3718 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003719
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003720 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003721 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003722 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003723 * If there are flags but no level netlabel isn't
3724 * behaving the way we expect it to.
3725 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003726 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003727 * Without guidance regarding the smack value
3728 * for the packet fall back on the network
3729 * ambient value.
3730 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003731 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303732 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003733 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003734 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003735 /*
3736 * Compare the catsets. Use the netlbl APIs.
3737 */
3738 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3739 if ((skp->smk_netlabel.flags &
3740 NETLBL_SECATTR_MLS_CAT) == 0)
3741 found = 1;
3742 break;
3743 }
3744 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003745 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3746 acat + 1);
3747 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003748 skp->smk_netlabel.attr.mls.cat,
3749 kcat + 1);
3750 if (acat < 0 || kcat < 0)
3751 break;
3752 }
3753 if (acat == kcat) {
3754 found = 1;
3755 break;
3756 }
Casey Schauflere114e472008-02-04 22:29:50 -08003757 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003758 rcu_read_unlock();
3759
3760 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003761 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003762
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003763 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003764 return &smack_known_web;
3765 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003766 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003767 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003768 /*
3769 * Looks like a fallback, which gives us a secid.
3770 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003771 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003772 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003773 * Without guidance regarding the smack value
3774 * for the packet fall back on the network
3775 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003776 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003777 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003778}
3779
Casey Schaufler69f287a2014-12-12 17:08:40 -08003780#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003781static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003782{
Casey Schauflerc6739442013-05-22 18:42:56 -07003783 u8 nexthdr;
3784 int offset;
3785 int proto = -EINVAL;
3786 struct ipv6hdr _ipv6h;
3787 struct ipv6hdr *ip6;
3788 __be16 frag_off;
3789 struct tcphdr _tcph, *th;
3790 struct udphdr _udph, *uh;
3791 struct dccp_hdr _dccph, *dh;
3792
3793 sip->sin6_port = 0;
3794
3795 offset = skb_network_offset(skb);
3796 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3797 if (ip6 == NULL)
3798 return -EINVAL;
3799 sip->sin6_addr = ip6->saddr;
3800
3801 nexthdr = ip6->nexthdr;
3802 offset += sizeof(_ipv6h);
3803 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3804 if (offset < 0)
3805 return -EINVAL;
3806
3807 proto = nexthdr;
3808 switch (proto) {
3809 case IPPROTO_TCP:
3810 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3811 if (th != NULL)
3812 sip->sin6_port = th->source;
3813 break;
3814 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003815 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003816 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3817 if (uh != NULL)
3818 sip->sin6_port = uh->source;
3819 break;
3820 case IPPROTO_DCCP:
3821 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3822 if (dh != NULL)
3823 sip->sin6_port = dh->dccph_sport;
3824 break;
3825 }
3826 return proto;
3827}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003828#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003829
Casey Schauflere114e472008-02-04 22:29:50 -08003830/**
3831 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3832 * @sk: socket
3833 * @skb: packet
3834 *
3835 * Returns 0 if the packet should be delivered, an error code otherwise
3836 */
3837static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3838{
3839 struct netlbl_lsm_secattr secattr;
3840 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003841 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003842 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003843 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003844 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003845#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003846 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003847#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003848#if IS_ENABLED(CONFIG_IPV6)
3849 struct sockaddr_in6 sadd;
3850 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003851
3852 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3853 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003854#endif /* CONFIG_IPV6 */
3855
Piotr Sawicki129a9982018-07-19 11:42:58 +02003856 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003857 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003858#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3859 /*
3860 * If there is a secmark use it rather than the CIPSO label.
3861 * If there is no secmark fall back to CIPSO.
3862 * The secmark is assumed to reflect policy better.
3863 */
3864 if (skb && skb->secmark != 0) {
3865 skp = smack_from_secid(skb->secmark);
3866 goto access_check;
3867 }
3868#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003869 /*
3870 * Translate what netlabel gave us.
3871 */
3872 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003873
Piotr Sawicki129a9982018-07-19 11:42:58 +02003874 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003875 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003876 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003877 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003878 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003879
Casey Schauflerc6739442013-05-22 18:42:56 -07003880 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003881
Casey Schaufler69f287a2014-12-12 17:08:40 -08003882#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3883access_check:
3884#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003885#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003886 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003887 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003888 ad.a.u.net->netif = skb->skb_iif;
3889 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003890#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003891 /*
3892 * Receiving a packet requires that the other end
3893 * be able to write here. Read access is not required.
3894 * This is the simplist possible security model
3895 * for networking.
3896 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003897 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3898 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003899 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003900 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003901 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003902 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003903#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003904 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003905 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003906 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3907 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003908 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003909#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003910 if (skb && skb->secmark != 0)
3911 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003912 else if (smk_ipv6_localhost(&sadd))
3913 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003914 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003915 skp = smack_ipv6host_label(&sadd);
3916 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003917 skp = smack_net_ambient;
Jia-Ju Bai3f4287e2019-07-23 18:00:15 +08003918 if (skb == NULL)
3919 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003920#ifdef CONFIG_AUDIT
3921 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003922 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003923 ad.a.u.net->netif = skb->skb_iif;
3924 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3925#endif /* CONFIG_AUDIT */
3926 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3927 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3928 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003929#endif /* SMACK_IPV6_SECMARK_LABELING */
3930#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003931 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003932#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003933 if (rc != 0)
3934 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3935 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003936 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003937#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003938 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003939
Paul Moorea8134292008-10-10 10:16:31 -04003940 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003941}
3942
3943/**
3944 * smack_socket_getpeersec_stream - pull in packet label
3945 * @sock: the socket
3946 * @optval: user's destination
3947 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003948 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003949 *
3950 * returns zero on success, an error code otherwise
3951 */
3952static int smack_socket_getpeersec_stream(struct socket *sock,
3953 char __user *optval,
3954 int __user *optlen, unsigned len)
3955{
3956 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003957 char *rcp = "";
3958 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003959 int rc = 0;
3960
3961 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003962 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003963 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003964 slen = strlen(rcp) + 1;
3965 }
Casey Schauflere114e472008-02-04 22:29:50 -08003966
3967 if (slen > len)
3968 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003969 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003970 rc = -EFAULT;
3971
3972 if (put_user(slen, optlen) != 0)
3973 rc = -EFAULT;
3974
3975 return rc;
3976}
3977
3978
3979/**
3980 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003981 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003982 * @skb: packet data
3983 * @secid: pointer to where to put the secid of the packet
3984 *
3985 * Sets the netlabel socket state on sk from parent
3986 */
3987static int smack_socket_getpeersec_dgram(struct socket *sock,
3988 struct sk_buff *skb, u32 *secid)
3989
3990{
3991 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003992 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003993 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003994 int family = PF_UNSPEC;
3995 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003996 int rc;
3997
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003998 if (skb != NULL) {
3999 if (skb->protocol == htons(ETH_P_IP))
4000 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004001#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004002 else if (skb->protocol == htons(ETH_P_IPV6))
4003 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004004#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004005 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004006 if (family == PF_UNSPEC && sock != NULL)
4007 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004008
Casey Schaufler69f287a2014-12-12 17:08:40 -08004009 switch (family) {
4010 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004011 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004012 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004013 break;
4014 case PF_INET:
4015#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4016 s = skb->secmark;
4017 if (s != 0)
4018 break;
4019#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004020 /*
4021 * Translate what netlabel gave us.
4022 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004023 if (sock != NULL && sock->sk != NULL)
4024 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004025 netlbl_secattr_init(&secattr);
4026 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4027 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004028 skp = smack_from_secattr(&secattr, ssp);
4029 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004030 }
4031 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004032 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004033 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004034#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004035 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004036#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004037 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004038 }
4039 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004040 if (s == 0)
4041 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004042 return 0;
4043}
4044
4045/**
Paul Moore07feee82009-03-27 17:10:54 -04004046 * smack_sock_graft - Initialize a newly created socket with an existing sock
4047 * @sk: child sock
4048 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004049 *
Paul Moore07feee82009-03-27 17:10:54 -04004050 * Set the smk_{in,out} state of an existing sock based on the process that
4051 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004052 */
4053static void smack_sock_graft(struct sock *sk, struct socket *parent)
4054{
4055 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004056 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004057
Paul Moore07feee82009-03-27 17:10:54 -04004058 if (sk == NULL ||
4059 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004060 return;
4061
4062 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004063 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004064 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004065 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004066}
4067
4068/**
4069 * smack_inet_conn_request - Smack access check on connect
4070 * @sk: socket involved
4071 * @skb: packet
4072 * @req: unused
4073 *
4074 * Returns 0 if a task with the packet label could write to
4075 * the socket, otherwise an error code
4076 */
4077static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4078 struct request_sock *req)
4079{
Paul Moore07feee82009-03-27 17:10:54 -04004080 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004081 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004082 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004083 struct netlbl_lsm_secattr secattr;
4084 struct sockaddr_in addr;
4085 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004086 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004087 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004088 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004089#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004090 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004091#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004092
Casey Schaufler69f287a2014-12-12 17:08:40 -08004093#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004094 if (family == PF_INET6) {
4095 /*
4096 * Handle mapped IPv4 packets arriving
4097 * via IPv6 sockets. Don't set up netlabel
4098 * processing on IPv6.
4099 */
4100 if (skb->protocol == htons(ETH_P_IP))
4101 family = PF_INET;
4102 else
4103 return 0;
4104 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004105#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004106
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004107#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4108 /*
4109 * If there is a secmark use it rather than the CIPSO label.
4110 * If there is no secmark fall back to CIPSO.
4111 * The secmark is assumed to reflect policy better.
4112 */
4113 if (skb && skb->secmark != 0) {
4114 skp = smack_from_secid(skb->secmark);
4115 goto access_check;
4116 }
4117#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4118
Paul Moore07feee82009-03-27 17:10:54 -04004119 netlbl_secattr_init(&secattr);
4120 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004121 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004122 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004123 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004124 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004125 netlbl_secattr_destroy(&secattr);
4126
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004127#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4128access_check:
4129#endif
4130
Etienne Bassetecfcc532009-04-08 20:40:06 +02004131#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004132 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4133 ad.a.u.net->family = family;
4134 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004135 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4136#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004137 /*
Paul Moore07feee82009-03-27 17:10:54 -04004138 * Receiving a packet requires that the other end be able to write
4139 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004140 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004141 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4142 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004143 if (rc != 0)
4144 return rc;
4145
4146 /*
4147 * Save the peer's label in the request_sock so we can later setup
4148 * smk_packet in the child socket so that SO_PEERCRED can report it.
4149 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004150 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004151
4152 /*
4153 * We need to decide if we want to label the incoming connection here
4154 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004155 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004156 */
4157 hdr = ip_hdr(skb);
4158 addr.sin_addr.s_addr = hdr->saddr;
4159 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004160 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004161 rcu_read_unlock();
4162
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004163 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004164 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004165 else
Paul Moore07feee82009-03-27 17:10:54 -04004166 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004167
4168 return rc;
4169}
4170
Paul Moore07feee82009-03-27 17:10:54 -04004171/**
4172 * smack_inet_csk_clone - Copy the connection information to the new socket
4173 * @sk: the new socket
4174 * @req: the connection's request_sock
4175 *
4176 * Transfer the connection's peer label to the newly created socket.
4177 */
4178static void smack_inet_csk_clone(struct sock *sk,
4179 const struct request_sock *req)
4180{
4181 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004182 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004183
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004184 if (req->peer_secid != 0) {
4185 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004186 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004187 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004188 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004189}
4190
Casey Schauflere114e472008-02-04 22:29:50 -08004191/*
4192 * Key management security hooks
4193 *
4194 * Casey has not tested key support very heavily.
4195 * The permission check is most likely too restrictive.
4196 * If you care about keys please have a look.
4197 */
4198#ifdef CONFIG_KEYS
4199
4200/**
4201 * smack_key_alloc - Set the key security blob
4202 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004203 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004204 * @flags: unused
4205 *
4206 * No allocation required
4207 *
4208 * Returns 0
4209 */
David Howellsd84f4f92008-11-14 10:39:23 +11004210static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004211 unsigned long flags)
4212{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004213 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004214
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004215 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004216 return 0;
4217}
4218
4219/**
4220 * smack_key_free - Clear the key security blob
4221 * @key: the object
4222 *
4223 * Clear the blob pointer
4224 */
4225static void smack_key_free(struct key *key)
4226{
4227 key->security = NULL;
4228}
4229
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004230/**
Casey Schauflere114e472008-02-04 22:29:50 -08004231 * smack_key_permission - Smack access on a key
4232 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004233 * @cred: the credentials to use
David Howells8c0637e2020-05-12 15:16:29 +01004234 * @need_perm: requested key permission
Casey Schauflere114e472008-02-04 22:29:50 -08004235 *
4236 * Return 0 if the task has read and write to the object,
4237 * an error code otherwise
4238 */
4239static int smack_key_permission(key_ref_t key_ref,
David Howells8c0637e2020-05-12 15:16:29 +01004240 const struct cred *cred,
4241 enum key_need_perm need_perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004242{
4243 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004244 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004245 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004246 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004247 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004248
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004249 /*
4250 * Validate requested permissions
4251 */
David Howells8c0637e2020-05-12 15:16:29 +01004252 switch (need_perm) {
4253 case KEY_NEED_READ:
4254 case KEY_NEED_SEARCH:
4255 case KEY_NEED_VIEW:
4256 request |= MAY_READ;
4257 break;
4258 case KEY_NEED_WRITE:
4259 case KEY_NEED_LINK:
4260 case KEY_NEED_SETATTR:
4261 request |= MAY_WRITE;
4262 break;
4263 case KEY_NEED_UNSPECIFIED:
4264 case KEY_NEED_UNLINK:
4265 case KEY_SYSADMIN_OVERRIDE:
4266 case KEY_AUTHTOKEN_OVERRIDE:
4267 case KEY_DEFER_PERM_CHECK:
4268 return 0;
4269 default:
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004270 return -EINVAL;
David Howells8c0637e2020-05-12 15:16:29 +01004271 }
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004272
Casey Schauflere114e472008-02-04 22:29:50 -08004273 keyp = key_ref_to_ptr(key_ref);
4274 if (keyp == NULL)
4275 return -EINVAL;
4276 /*
4277 * If the key hasn't been initialized give it access so that
4278 * it may do so.
4279 */
4280 if (keyp->security == NULL)
4281 return 0;
4282 /*
4283 * This should not occur
4284 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004285 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004286 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004287
David Howellsa8478a62020-01-14 17:07:13 +00004288 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004289 return 0;
4290
Etienne Bassetecfcc532009-04-08 20:40:06 +02004291#ifdef CONFIG_AUDIT
4292 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4293 ad.a.u.key_struct.key = keyp->serial;
4294 ad.a.u.key_struct.key_desc = keyp->description;
4295#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07004296 rc = smk_access(tkp, keyp->security, request, &ad);
4297 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4298 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004299}
José Bollo7fc5f362015-02-17 15:41:22 +01004300
4301/*
4302 * smack_key_getsecurity - Smack label tagging the key
4303 * @key points to the key to be queried
4304 * @_buffer points to a pointer that should be set to point to the
4305 * resulting string (if no label or an error occurs).
4306 * Return the length of the string (including terminating NUL) or -ve if
4307 * an error.
4308 * May also return 0 (and a NULL buffer pointer) if there is no label.
4309 */
4310static int smack_key_getsecurity(struct key *key, char **_buffer)
4311{
4312 struct smack_known *skp = key->security;
4313 size_t length;
4314 char *copy;
4315
4316 if (key->security == NULL) {
4317 *_buffer = NULL;
4318 return 0;
4319 }
4320
4321 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4322 if (copy == NULL)
4323 return -ENOMEM;
4324 length = strlen(copy) + 1;
4325
4326 *_buffer = copy;
4327 return length;
4328}
4329
David Howellsa8478a62020-01-14 17:07:13 +00004330
4331#ifdef CONFIG_KEY_NOTIFICATIONS
4332/**
4333 * smack_watch_key - Smack access to watch a key for notifications.
4334 * @key: The key to be watched
4335 *
4336 * Return 0 if the @watch->cred has permission to read from the key object and
4337 * an error otherwise.
4338 */
4339static int smack_watch_key(struct key *key)
4340{
4341 struct smk_audit_info ad;
4342 struct smack_known *tkp = smk_of_current();
4343 int rc;
4344
4345 if (key == NULL)
4346 return -EINVAL;
4347 /*
4348 * If the key hasn't been initialized give it access so that
4349 * it may do so.
4350 */
4351 if (key->security == NULL)
4352 return 0;
4353 /*
4354 * This should not occur
4355 */
4356 if (tkp == NULL)
4357 return -EACCES;
4358
4359 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4360 return 0;
4361
4362#ifdef CONFIG_AUDIT
4363 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4364 ad.a.u.key_struct.key = key->serial;
4365 ad.a.u.key_struct.key_desc = key->description;
4366#endif
4367 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4368 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4369 return rc;
4370}
4371#endif /* CONFIG_KEY_NOTIFICATIONS */
Casey Schauflere114e472008-02-04 22:29:50 -08004372#endif /* CONFIG_KEYS */
4373
David Howellsa8478a62020-01-14 17:07:13 +00004374#ifdef CONFIG_WATCH_QUEUE
4375/**
4376 * smack_post_notification - Smack access to post a notification to a queue
4377 * @w_cred: The credentials of the watcher.
4378 * @cred: The credentials of the event source (may be NULL).
4379 * @n: The notification message to be posted.
4380 */
4381static int smack_post_notification(const struct cred *w_cred,
4382 const struct cred *cred,
4383 struct watch_notification *n)
4384{
4385 struct smk_audit_info ad;
4386 struct smack_known *subj, *obj;
4387 int rc;
4388
4389 /* Always let maintenance notifications through. */
4390 if (n->type == WATCH_TYPE_META)
4391 return 0;
4392
4393 if (!cred)
4394 return 0;
4395 subj = smk_of_task(smack_cred(cred));
4396 obj = smk_of_task(smack_cred(w_cred));
4397
4398 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4399 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4400 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4401 return rc;
4402}
4403#endif /* CONFIG_WATCH_QUEUE */
4404
Casey Schauflere114e472008-02-04 22:29:50 -08004405/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004406 * Smack Audit hooks
4407 *
4408 * Audit requires a unique representation of each Smack specific
4409 * rule. This unique representation is used to distinguish the
4410 * object to be audited from remaining kernel objects and also
4411 * works as a glue between the audit hooks.
4412 *
4413 * Since repository entries are added but never deleted, we'll use
4414 * the smack_known label address related to the given audit rule as
4415 * the needed unique representation. This also better fits the smack
4416 * model where nearly everything is a label.
4417 */
4418#ifdef CONFIG_AUDIT
4419
4420/**
4421 * smack_audit_rule_init - Initialize a smack audit rule
4422 * @field: audit rule fields given from user-space (audit.h)
4423 * @op: required testing operator (=, !=, >, <, ...)
4424 * @rulestr: smack label to be audited
4425 * @vrule: pointer to save our own audit rule representation
4426 *
4427 * Prepare to audit cases where (@field @op @rulestr) is true.
4428 * The label to be audited is created if necessay.
4429 */
4430static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4431{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004432 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004433 char **rule = (char **)vrule;
4434 *rule = NULL;
4435
4436 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4437 return -EINVAL;
4438
Al Viro5af75d82008-12-16 05:59:26 -05004439 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004440 return -EINVAL;
4441
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004442 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004443 if (IS_ERR(skp))
4444 return PTR_ERR(skp);
4445
4446 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004447
4448 return 0;
4449}
4450
4451/**
4452 * smack_audit_rule_known - Distinguish Smack audit rules
4453 * @krule: rule of interest, in Audit kernel representation format
4454 *
4455 * This is used to filter Smack rules from remaining Audit ones.
4456 * If it's proved that this rule belongs to us, the
4457 * audit_rule_match hook will be called to do the final judgement.
4458 */
4459static int smack_audit_rule_known(struct audit_krule *krule)
4460{
4461 struct audit_field *f;
4462 int i;
4463
4464 for (i = 0; i < krule->field_count; i++) {
4465 f = &krule->fields[i];
4466
4467 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4468 return 1;
4469 }
4470
4471 return 0;
4472}
4473
4474/**
4475 * smack_audit_rule_match - Audit given object ?
4476 * @secid: security id for identifying the object to test
4477 * @field: audit rule flags given from user-space
4478 * @op: required testing operator
4479 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004480 *
4481 * The core Audit hook. It's used to take the decision of
4482 * whether to audit or not to audit a given object.
4483 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004484static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004485{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004486 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004487 char *rule = vrule;
4488
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004489 if (unlikely(!rule)) {
4490 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004491 return -ENOENT;
4492 }
4493
4494 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4495 return 0;
4496
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004497 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004498
4499 /*
4500 * No need to do string comparisons. If a match occurs,
4501 * both pointers will point to the same smack_known
4502 * label.
4503 */
Al Viro5af75d82008-12-16 05:59:26 -05004504 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004505 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004506 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004507 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004508
4509 return 0;
4510}
4511
Casey Schaufler491a0b02016-01-26 15:08:35 -08004512/*
4513 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004514 * No memory was allocated.
4515 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004516
4517#endif /* CONFIG_AUDIT */
4518
Randy Dunlap251a2a92009-02-18 11:42:33 -08004519/**
David Quigley746df9b2013-05-22 12:50:35 -04004520 * smack_ismaclabel - check if xattr @name references a smack MAC label
4521 * @name: Full xattr name to check.
4522 */
4523static int smack_ismaclabel(const char *name)
4524{
4525 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4526}
4527
4528
4529/**
Casey Schauflere114e472008-02-04 22:29:50 -08004530 * smack_secid_to_secctx - return the smack label for a secid
4531 * @secid: incoming integer
4532 * @secdata: destination
4533 * @seclen: how long it is
4534 *
4535 * Exists for networking code.
4536 */
4537static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4538{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004539 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004540
Eric Parisd5630b92010-10-13 16:24:48 -04004541 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004542 *secdata = skp->smk_known;
4543 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004544 return 0;
4545}
4546
Randy Dunlap251a2a92009-02-18 11:42:33 -08004547/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004548 * smack_secctx_to_secid - return the secid for a smack label
4549 * @secdata: smack label
4550 * @seclen: how long result is
4551 * @secid: outgoing integer
4552 *
4553 * Exists for audit and networking code.
4554 */
David Howellse52c17642008-04-29 20:52:51 +01004555static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004556{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004557 struct smack_known *skp = smk_find_entry(secdata);
4558
4559 if (skp)
4560 *secid = skp->smk_secid;
4561 else
4562 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004563 return 0;
4564}
4565
Casey Schaufler491a0b02016-01-26 15:08:35 -08004566/*
4567 * There used to be a smack_release_secctx hook
4568 * that did nothing back when hooks were in a vector.
4569 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004570 */
Casey Schauflere114e472008-02-04 22:29:50 -08004571
David P. Quigley1ee65e32009-09-03 14:25:57 -04004572static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4573{
4574 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4575}
4576
4577static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4578{
4579 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4580}
4581
4582static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4583{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004584 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004585
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004586 *ctx = skp->smk_known;
4587 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004588 return 0;
4589}
4590
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004591static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4592{
4593
4594 struct task_smack *tsp;
4595 struct smack_known *skp;
4596 struct inode_smack *isp;
4597 struct cred *new_creds = *new;
4598
4599 if (new_creds == NULL) {
4600 new_creds = prepare_creds();
4601 if (new_creds == NULL)
4602 return -ENOMEM;
4603 }
4604
Casey Schauflerb17103a2018-11-09 16:12:56 -08004605 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004606
4607 /*
4608 * Get label from overlay inode and set it in create_sid
4609 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004610 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004611 skp = isp->smk_inode;
4612 tsp->smk_task = skp;
4613 *new = new_creds;
4614 return 0;
4615}
4616
4617static int smack_inode_copy_up_xattr(const char *name)
4618{
4619 /*
4620 * Return 1 if this is the smack access Smack attribute.
4621 */
4622 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4623 return 1;
4624
4625 return -EOPNOTSUPP;
4626}
4627
4628static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4629 struct qstr *name,
4630 const struct cred *old,
4631 struct cred *new)
4632{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004633 struct task_smack *otsp = smack_cred(old);
4634 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004635 struct inode_smack *isp;
4636 int may;
4637
4638 /*
4639 * Use the process credential unless all of
4640 * the transmuting criteria are met
4641 */
4642 ntsp->smk_task = otsp->smk_task;
4643
4644 /*
4645 * the attribute of the containing directory
4646 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004647 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004648
4649 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4650 rcu_read_lock();
4651 may = smk_access_entry(otsp->smk_task->smk_known,
4652 isp->smk_inode->smk_known,
4653 &otsp->smk_task->smk_rules);
4654 rcu_read_unlock();
4655
4656 /*
4657 * If the directory is transmuting and the rule
4658 * providing access is transmuting use the containing
4659 * directory label instead of the process label.
4660 */
4661 if (may > 0 && (may & MAY_TRANSMUTE))
4662 ntsp->smk_task = isp->smk_inode;
4663 }
4664 return 0;
4665}
4666
Casey Schauflerbbd36622018-11-12 09:30:56 -08004667struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4668 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004669 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004670 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004671 .lbs_ipc = sizeof(struct smack_known *),
4672 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004673};
4674
James Morrisca97d932017-02-15 00:18:51 +11004675static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004676 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4677 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4678 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004679
Al Viro0b520752018-12-23 16:02:47 -05004680 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004681 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4682
Casey Schauflere20b0432015-05-02 15:11:36 -07004683 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4684 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004685 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004686 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004687 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304688 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004689
Casey Schauflere20b0432015-05-02 15:11:36 -07004690 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004691
Casey Schauflere20b0432015-05-02 15:11:36 -07004692 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004693 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4694 LSM_HOOK_INIT(inode_link, smack_inode_link),
4695 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4696 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4697 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4698 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4699 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4700 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4701 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4702 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4703 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4704 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4705 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4706 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4707 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4708 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004709
Casey Schauflere20b0432015-05-02 15:11:36 -07004710 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004711 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4712 LSM_HOOK_INIT(file_lock, smack_file_lock),
4713 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4714 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4715 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4716 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4717 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4718 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004719
Casey Schauflere20b0432015-05-02 15:11:36 -07004720 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004721
Casey Schauflere20b0432015-05-02 15:11:36 -07004722 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4723 LSM_HOOK_INIT(cred_free, smack_cred_free),
4724 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4725 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004726 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004727 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4728 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4729 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4730 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4731 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4732 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4733 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4734 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4735 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4736 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4737 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4738 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4739 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004740 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004741
Casey Schauflere20b0432015-05-02 15:11:36 -07004742 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4743 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004744
Casey Schauflere20b0432015-05-02 15:11:36 -07004745 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004746
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004747 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004748 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4749 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4750 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4751 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004752
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004753 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004754 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4755 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4756 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004757
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004758 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004759 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4760 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4761 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004762
Casey Schauflere20b0432015-05-02 15:11:36 -07004763 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004764
Casey Schauflere20b0432015-05-02 15:11:36 -07004765 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4766 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004767
Casey Schauflere20b0432015-05-02 15:11:36 -07004768 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4769 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004770
Casey Schauflere20b0432015-05-02 15:11:36 -07004771 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004772 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004773#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004774 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004775#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004776 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4777 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4778 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4779 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4780 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4781 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4782 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4783 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4784 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4785 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004786
Casey Schauflere114e472008-02-04 22:29:50 -08004787 /* key management security hooks */
4788#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004789 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4790 LSM_HOOK_INIT(key_free, smack_key_free),
4791 LSM_HOOK_INIT(key_permission, smack_key_permission),
4792 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
David Howellsa8478a62020-01-14 17:07:13 +00004793#ifdef CONFIG_KEY_NOTIFICATIONS
4794 LSM_HOOK_INIT(watch_key, smack_watch_key),
4795#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004796#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004797
David Howellsa8478a62020-01-14 17:07:13 +00004798#ifdef CONFIG_WATCH_QUEUE
4799 LSM_HOOK_INIT(post_notification, smack_post_notification),
4800#endif
4801
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004802 /* Audit hooks */
4803#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004804 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4805 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4806 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004807#endif /* CONFIG_AUDIT */
4808
Casey Schauflere20b0432015-05-02 15:11:36 -07004809 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4810 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4811 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004812 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4813 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4814 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004815 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4816 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4817 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004818};
4819
Etienne Basset7198e2e2009-03-24 20:53:24 +01004820
Casey Schaufler86812bb2012-04-17 18:55:46 -07004821static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004822{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004823 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004824 * Initialize rule list locks
4825 */
4826 mutex_init(&smack_known_huh.smk_rules_lock);
4827 mutex_init(&smack_known_hat.smk_rules_lock);
4828 mutex_init(&smack_known_floor.smk_rules_lock);
4829 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004830 mutex_init(&smack_known_web.smk_rules_lock);
4831 /*
4832 * Initialize rule lists
4833 */
4834 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4835 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4836 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4837 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004838 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4839 /*
4840 * Create the known labels list
4841 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004842 smk_insert_entry(&smack_known_huh);
4843 smk_insert_entry(&smack_known_hat);
4844 smk_insert_entry(&smack_known_star);
4845 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004846 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004847}
4848
Casey Schauflere114e472008-02-04 22:29:50 -08004849/**
4850 * smack_init - initialize the smack system
4851 *
luanshia1a07f22019-07-05 10:35:20 +08004852 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004853 */
4854static __init int smack_init(void)
4855{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004856 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004857 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004858
Rohit1a5b4722014-10-15 17:40:41 +05304859 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4860 if (!smack_inode_cache)
4861 return -ENOMEM;
4862
Casey Schaufler4e328b02019-04-02 11:37:12 -07004863 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4864 if (!smack_rule_cache) {
4865 kmem_cache_destroy(smack_inode_cache);
4866 return -ENOMEM;
4867 }
4868
Casey Schauflerbbd36622018-11-12 09:30:56 -08004869 /*
4870 * Set the security state for the initial task.
4871 */
4872 tsp = smack_cred(cred);
4873 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4874
4875 /*
4876 * Register with LSM
4877 */
4878 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004879 smack_enabled = 1;
4880
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004881 pr_info("Smack: Initializing.\n");
4882#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4883 pr_info("Smack: Netfilter enabled.\n");
4884#endif
4885#ifdef SMACK_IPV6_PORT_LABELING
4886 pr_info("Smack: IPv6 port labeling enabled.\n");
4887#endif
4888#ifdef SMACK_IPV6_SECMARK_LABELING
4889 pr_info("Smack: IPv6 Netfilter enabled.\n");
4890#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004891
Casey Schaufler86812bb2012-04-17 18:55:46 -07004892 /* initialize the smack_known_list */
4893 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004894
Casey Schauflere114e472008-02-04 22:29:50 -08004895 return 0;
4896}
4897
4898/*
4899 * Smack requires early initialization in order to label
4900 * all processes and objects when they are created.
4901 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004902DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004903 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004904 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004905 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004906 .init = smack_init,
4907};