blob: 339356939018e0a279c916d1204c17e9d1f641e8 [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>
Casey Schauflere114e472008-02-04 22:29:50 -080044#include "smack.h"
45
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020046#define TRANS_TRUE "TRUE"
47#define TRANS_TRUE_SIZE 4
48
Casey Schauflerc6739442013-05-22 18:42:56 -070049#define SMK_CONNECTING 0
50#define SMK_RECEIVING 1
51#define SMK_SENDING 2
52
Arnd Bergmann00720f02020-04-08 21:04:31 +020053static DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080054static LIST_HEAD(smk_ipv6_port_list);
Rohit1a5b4722014-10-15 17:40:41 +053055static struct kmem_cache *smack_inode_cache;
Casey Schaufler4e328b02019-04-02 11:37:12 -070056struct kmem_cache *smack_rule_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080057int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070058
Al Viroc3300aa2018-12-16 01:52:24 -050059#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
60static struct {
61 const char *name;
62 int len;
63 int opt;
64} smk_mount_opts[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +010065 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
Al Viroc3300aa2018-12-16 01:52:24 -050066 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
Vivek Trivedi3bf27892015-06-22 15:36:06 +053067};
Al Viroc3300aa2018-12-16 01:52:24 -050068#undef A
69
70static int match_opt_prefix(char *s, int l, char **arg)
71{
72 int i;
73
74 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
75 size_t len = smk_mount_opts[i].len;
76 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
77 continue;
78 if (len == l || s[len] != '=')
79 continue;
80 *arg = s + len + 1;
81 return smk_mount_opts[i].opt;
82 }
83 return Opt_error;
84}
Vivek Trivedi3bf27892015-06-22 15:36:06 +053085
Casey Schaufler3d04c922015-08-12 11:56:02 -070086#ifdef CONFIG_SECURITY_SMACK_BRINGUP
87static char *smk_bu_mess[] = {
88 "Bringup Error", /* Unused */
89 "Bringup", /* SMACK_BRINGUP_ALLOW */
90 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
91 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
92};
93
Casey Schauflerd166c802014-08-27 14:51:27 -070094static void smk_bu_mode(int mode, char *s)
95{
96 int i = 0;
97
98 if (mode & MAY_READ)
99 s[i++] = 'r';
100 if (mode & MAY_WRITE)
101 s[i++] = 'w';
102 if (mode & MAY_EXEC)
103 s[i++] = 'x';
104 if (mode & MAY_APPEND)
105 s[i++] = 'a';
106 if (mode & MAY_TRANSMUTE)
107 s[i++] = 't';
108 if (mode & MAY_LOCK)
109 s[i++] = 'l';
110 if (i == 0)
111 s[i++] = '-';
112 s[i] = '\0';
113}
114#endif
115
116#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200117static int smk_bu_note(char *note, struct smack_known *sskp,
118 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700119{
120 char acc[SMK_NUM_ACCESS_TYPE + 1];
121
122 if (rc <= 0)
123 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700124 if (rc > SMACK_UNCONFINED_OBJECT)
125 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700126
127 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700128 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200129 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700130 return 0;
131}
132#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200133#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700134#endif
135
136#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200137static int smk_bu_current(char *note, struct smack_known *oskp,
138 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700139{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800140 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700141 char acc[SMK_NUM_ACCESS_TYPE + 1];
142
143 if (rc <= 0)
144 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700145 if (rc > SMACK_UNCONFINED_OBJECT)
146 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700147
148 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700149 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200150 tsp->smk_task->smk_known, oskp->smk_known,
151 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700152 return 0;
153}
154#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200155#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700156#endif
157
158#ifdef CONFIG_SECURITY_SMACK_BRINGUP
159static int smk_bu_task(struct task_struct *otp, int mode, int rc)
160{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800161 struct task_smack *tsp = smack_cred(current_cred());
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300162 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700163 char acc[SMK_NUM_ACCESS_TYPE + 1];
164
165 if (rc <= 0)
166 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700167 if (rc > SMACK_UNCONFINED_OBJECT)
168 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700169
170 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700171 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300172 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700173 current->comm, otp->comm);
174 return 0;
175}
176#else
177#define smk_bu_task(otp, mode, RC) (RC)
178#endif
179
180#ifdef CONFIG_SECURITY_SMACK_BRINGUP
181static int smk_bu_inode(struct inode *inode, int mode, int rc)
182{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800183 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800184 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700185 char acc[SMK_NUM_ACCESS_TYPE + 1];
186
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700187 if (isp->smk_flags & SMK_INODE_IMPURE)
188 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
189 inode->i_sb->s_id, inode->i_ino, current->comm);
190
Casey Schauflerd166c802014-08-27 14:51:27 -0700191 if (rc <= 0)
192 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700193 if (rc > SMACK_UNCONFINED_OBJECT)
194 rc = 0;
195 if (rc == SMACK_UNCONFINED_SUBJECT &&
196 (mode & (MAY_WRITE | MAY_APPEND)))
197 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700198
199 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700200
201 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
202 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700203 inode->i_sb->s_id, inode->i_ino, current->comm);
204 return 0;
205}
206#else
207#define smk_bu_inode(inode, mode, RC) (RC)
208#endif
209
210#ifdef CONFIG_SECURITY_SMACK_BRINGUP
211static int smk_bu_file(struct file *file, int mode, int rc)
212{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800213 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700214 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800215 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800216 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700217 char acc[SMK_NUM_ACCESS_TYPE + 1];
218
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700219 if (isp->smk_flags & SMK_INODE_IMPURE)
220 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
221 inode->i_sb->s_id, inode->i_ino, current->comm);
222
Casey Schauflerd166c802014-08-27 14:51:27 -0700223 if (rc <= 0)
224 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700225 if (rc > SMACK_UNCONFINED_OBJECT)
226 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700227
228 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700229 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800230 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400231 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700232 current->comm);
233 return 0;
234}
235#else
236#define smk_bu_file(file, mode, RC) (RC)
237#endif
238
239#ifdef CONFIG_SECURITY_SMACK_BRINGUP
240static int smk_bu_credfile(const struct cred *cred, struct file *file,
241 int mode, int rc)
242{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800243 struct task_smack *tsp = smack_cred(cred);
Casey Schauflerd166c802014-08-27 14:51:27 -0700244 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500245 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800246 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700247 char acc[SMK_NUM_ACCESS_TYPE + 1];
248
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700249 if (isp->smk_flags & SMK_INODE_IMPURE)
250 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
251 inode->i_sb->s_id, inode->i_ino, current->comm);
252
Casey Schauflerd166c802014-08-27 14:51:27 -0700253 if (rc <= 0)
254 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700255 if (rc > SMACK_UNCONFINED_OBJECT)
256 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700257
258 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700259 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200260 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400261 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700262 current->comm);
263 return 0;
264}
265#else
266#define smk_bu_credfile(cred, file, mode, RC) (RC)
267#endif
268
Casey Schauflere114e472008-02-04 22:29:50 -0800269/**
270 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100271 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800272 * @ip: a pointer to the inode
273 * @dp: a pointer to the dentry
274 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200275 * Returns a pointer to the master list entry for the Smack label,
276 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800277 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700278static struct smack_known *smk_fetch(const char *name, struct inode *ip,
279 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800280{
281 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700282 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700283 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800284
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200285 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200286 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800287
Eric Biggerse5bfad32019-08-21 22:54:41 -0700288 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700289 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200290 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800291
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200292 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200293 if (rc < 0)
294 skp = ERR_PTR(rc);
295 else if (rc == 0)
296 skp = NULL;
297 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700298 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700299
300 kfree(buffer);
301
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700302 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800303}
304
305/**
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700306 * init_inode_smack - initialize an inode security blob
luanshia1a07f22019-07-05 10:35:20 +0800307 * @inode: inode to extract the info from
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200308 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800309 *
Casey Schauflere114e472008-02-04 22:29:50 -0800310 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700311static void init_inode_smack(struct inode *inode, struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800312{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700313 struct inode_smack *isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -0800314
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200315 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800316 isp->smk_flags = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800317}
318
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800319/**
Casey Schauflerbbd36622018-11-12 09:30:56 -0800320 * init_task_smack - initialize a task security blob
321 * @tsp: blob to initialize
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100322 * @task: a pointer to the Smack label for the running task
323 * @forked: a pointer to the Smack label for the forked task
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800324 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800325 */
Casey Schauflerbbd36622018-11-12 09:30:56 -0800326static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
327 struct smack_known *forked)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800328{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800329 tsp->smk_task = task;
330 tsp->smk_forked = forked;
331 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200332 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800333 mutex_init(&tsp->smk_rules_lock);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800334}
335
336/**
337 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100338 * @nhead: new rules header pointer
339 * @ohead: old rules header pointer
340 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800341 *
342 * Returns 0 on success, -ENOMEM on error
343 */
344static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
345 gfp_t gfp)
346{
347 struct smack_rule *nrp;
348 struct smack_rule *orp;
349 int rc = 0;
350
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800351 list_for_each_entry_rcu(orp, ohead, list) {
Casey Schaufler4e328b02019-04-02 11:37:12 -0700352 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800353 if (nrp == NULL) {
354 rc = -ENOMEM;
355 break;
356 }
357 *nrp = *orp;
358 list_add_rcu(&nrp->list, nhead);
359 }
360 return rc;
361}
362
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100363/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200364 * smk_copy_relabel - copy smk_relabel labels list
365 * @nhead: new rules header pointer
366 * @ohead: old rules header pointer
367 * @gfp: type of the memory for the allocation
368 *
369 * Returns 0 on success, -ENOMEM on error
370 */
371static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
372 gfp_t gfp)
373{
374 struct smack_known_list_elem *nklep;
375 struct smack_known_list_elem *oklep;
376
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200377 list_for_each_entry(oklep, ohead, list) {
378 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
379 if (nklep == NULL) {
380 smk_destroy_label_list(nhead);
381 return -ENOMEM;
382 }
383 nklep->smk_label = oklep->smk_label;
384 list_add(&nklep->list, nhead);
385 }
386
387 return 0;
388}
389
390/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100391 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
392 * @mode - input mode in form of PTRACE_MODE_*
393 *
394 * Returns a converted MAY_* mode usable by smack rules
395 */
396static inline unsigned int smk_ptrace_mode(unsigned int mode)
397{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800398 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100399 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800400 if (mode & PTRACE_MODE_READ)
401 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100402
403 return 0;
404}
405
406/**
407 * smk_ptrace_rule_check - helper for ptrace access
408 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200409 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100410 * @mode: ptrace attachment mode (PTRACE_MODE_*)
411 * @func: name of the function that called us, used for audit
412 *
413 * Returns 0 on access granted, -error on error
414 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200415static int smk_ptrace_rule_check(struct task_struct *tracer,
416 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100417 unsigned int mode, const char *func)
418{
419 int rc;
420 struct smk_audit_info ad, *saip = NULL;
421 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200422 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700423 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100424
425 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
426 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
427 smk_ad_setfield_u_tsk(&ad, tracer);
428 saip = &ad;
429 }
430
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300431 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700432 tracercred = __task_cred(tracer);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800433 tsp = smack_cred(tracercred);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200434 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100435
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100436 if ((mode & PTRACE_MODE_ATTACH) &&
437 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
438 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200439 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100440 rc = 0;
441 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
442 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700443 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100444 rc = 0;
445 else
446 rc = -EACCES;
447
448 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200449 smack_log(tracer_known->smk_known,
450 tracee_known->smk_known,
451 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100452
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300453 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100454 return rc;
455 }
456
457 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200458 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300459
460 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100461 return rc;
462}
463
Casey Schauflere114e472008-02-04 22:29:50 -0800464/*
465 * LSM hooks.
466 * We he, that is fun!
467 */
468
469/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000470 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800471 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100472 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800473 *
474 * Returns 0 if access is OK, an error code otherwise
475 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100476 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800477 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000478static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800479{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700480 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800481
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300482 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200483
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700484 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100485}
Casey Schauflere114e472008-02-04 22:29:50 -0800486
David Howells5cd9c582008-08-14 11:37:28 +0100487/**
488 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
489 * @ptp: parent task pointer
490 *
491 * Returns 0 if access is OK, an error code otherwise
492 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100493 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100494 */
495static int smack_ptrace_traceme(struct task_struct *ptp)
496{
497 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700498 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100499
Casey Schauflerb17103a2018-11-09 16:12:56 -0800500 skp = smk_of_task(smack_cred(current_cred()));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200501
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200502 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800503 return rc;
504}
505
506/**
507 * smack_syslog - Smack approval on syslog
luanshia1a07f22019-07-05 10:35:20 +0800508 * @typefrom_file: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800509 *
Casey Schauflere114e472008-02-04 22:29:50 -0800510 * Returns 0 on success, error code otherwise.
511 */
Eric Paris12b30522010-11-15 18:36:29 -0500512static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800513{
Eric Paris12b30522010-11-15 18:36:29 -0500514 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700515 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800516
Casey Schaufler1880eff2012-06-05 15:28:30 -0700517 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800518 return 0;
519
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800520 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800521 rc = -EACCES;
522
523 return rc;
524}
525
Casey Schauflere114e472008-02-04 22:29:50 -0800526/*
527 * Superblock Hooks.
528 */
529
530/**
531 * smack_sb_alloc_security - allocate a superblock blob
532 * @sb: the superblock getting the blob
533 *
534 * Returns 0 on success or -ENOMEM on error.
535 */
536static int smack_sb_alloc_security(struct super_block *sb)
537{
538 struct superblock_smack *sbsp;
539
540 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
541
542 if (sbsp == NULL)
543 return -ENOMEM;
544
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200545 sbsp->smk_root = &smack_known_floor;
546 sbsp->smk_default = &smack_known_floor;
547 sbsp->smk_floor = &smack_known_floor;
548 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700549 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500550 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700551 */
Casey Schauflere114e472008-02-04 22:29:50 -0800552 sb->s_security = sbsp;
553
554 return 0;
555}
556
557/**
558 * smack_sb_free_security - free a superblock blob
559 * @sb: the superblock getting the blob
560 *
561 */
562static void smack_sb_free_security(struct super_block *sb)
563{
564 kfree(sb->s_security);
565 sb->s_security = NULL;
566}
567
Al Viro12085b12018-12-13 15:18:05 -0500568struct smack_mnt_opts {
569 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
570};
571
Al Viro204cc0c2018-12-13 13:41:47 -0500572static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800573{
Al Viro12085b12018-12-13 15:18:05 -0500574 struct smack_mnt_opts *opts = mnt_opts;
575 kfree(opts->fsdefault);
576 kfree(opts->fsfloor);
577 kfree(opts->fshat);
578 kfree(opts->fsroot);
579 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500580 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800581}
582
Al Viro55c0e5b2018-12-16 01:09:45 -0500583static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530584{
Al Viro55c0e5b2018-12-16 01:09:45 -0500585 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530586
Al Viro55c0e5b2018-12-16 01:09:45 -0500587 if (!opts) {
588 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
589 if (!opts)
590 return -ENOMEM;
591 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530592 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500593 if (!s)
594 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530595
Al Viro55c0e5b2018-12-16 01:09:45 -0500596 switch (token) {
597 case Opt_fsdefault:
598 if (opts->fsdefault)
599 goto out_opt_err;
600 opts->fsdefault = s;
601 break;
602 case Opt_fsfloor:
603 if (opts->fsfloor)
604 goto out_opt_err;
605 opts->fsfloor = s;
606 break;
607 case Opt_fshat:
608 if (opts->fshat)
609 goto out_opt_err;
610 opts->fshat = s;
611 break;
612 case Opt_fsroot:
613 if (opts->fsroot)
614 goto out_opt_err;
615 opts->fsroot = s;
616 break;
617 case Opt_fstransmute:
618 if (opts->fstransmute)
619 goto out_opt_err;
620 opts->fstransmute = s;
621 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530622 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530623 return 0;
624
625out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530626 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500627 return -EINVAL;
628}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530629
Al Viro0b520752018-12-23 16:02:47 -0500630/**
631 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
632 * @fc: The new filesystem context.
633 * @src_fc: The source filesystem context being duplicated.
634 *
635 * Returns 0 on success or -ENOMEM on error.
636 */
637static int smack_fs_context_dup(struct fs_context *fc,
638 struct fs_context *src_fc)
639{
640 struct smack_mnt_opts *dst, *src = src_fc->security;
641
642 if (!src)
643 return 0;
644
645 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
646 if (!fc->security)
647 return -ENOMEM;
648 dst = fc->security;
649
650 if (src->fsdefault) {
651 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
652 if (!dst->fsdefault)
653 return -ENOMEM;
654 }
655 if (src->fsfloor) {
656 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
657 if (!dst->fsfloor)
658 return -ENOMEM;
659 }
660 if (src->fshat) {
661 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
662 if (!dst->fshat)
663 return -ENOMEM;
664 }
665 if (src->fsroot) {
666 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
667 if (!dst->fsroot)
668 return -ENOMEM;
669 }
670 if (src->fstransmute) {
671 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
672 if (!dst->fstransmute)
673 return -ENOMEM;
674 }
675 return 0;
676}
677
Al Virod7167b12019-09-07 07:23:15 -0400678static const struct fs_parameter_spec smack_fs_parameters[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +0100679 fsparam_string("smackfsdef", Opt_fsdefault),
680 fsparam_string("smackfsdefault", Opt_fsdefault),
681 fsparam_string("smackfsfloor", Opt_fsfloor),
682 fsparam_string("smackfshat", Opt_fshat),
683 fsparam_string("smackfsroot", Opt_fsroot),
684 fsparam_string("smackfstransmute", Opt_fstransmute),
David Howells2febd252018-11-01 23:07:24 +0000685 {}
686};
687
David Howells2febd252018-11-01 23:07:24 +0000688/**
689 * smack_fs_context_parse_param - Parse a single mount parameter
690 * @fc: The new filesystem context being constructed.
691 * @param: The parameter.
692 *
693 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
694 * error.
695 */
696static int smack_fs_context_parse_param(struct fs_context *fc,
697 struct fs_parameter *param)
698{
699 struct fs_parse_result result;
700 int opt, rc;
701
Al Virod7167b12019-09-07 07:23:15 -0400702 opt = fs_parse(fc, smack_fs_parameters, param, &result);
David Howells2febd252018-11-01 23:07:24 +0000703 if (opt < 0)
704 return opt;
705
706 rc = smack_add_opt(opt, param->string, &fc->security);
707 if (!rc)
708 param->string = NULL;
709 return rc;
710}
711
Al Virod2497e12018-12-16 01:37:06 -0500712static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530713{
Al Virod2497e12018-12-16 01:37:06 -0500714 char *from = options, *to = options;
715 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530716
Al Viroc3300aa2018-12-16 01:52:24 -0500717 while (1) {
718 char *next = strchr(from, ',');
719 int token, len, rc;
720 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530721
Al Viroc3300aa2018-12-16 01:52:24 -0500722 if (next)
723 len = next - from;
724 else
725 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530726
Al Viroc3300aa2018-12-16 01:52:24 -0500727 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500728 if (token != Opt_error) {
729 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
730 rc = smack_add_opt(token, arg, mnt_opts);
731 if (unlikely(rc)) {
732 kfree(arg);
733 if (*mnt_opts)
734 smack_free_mnt_opts(*mnt_opts);
735 *mnt_opts = NULL;
736 return rc;
737 }
738 } else {
739 if (!first) { // copy with preceding comma
740 from--;
741 len++;
742 }
743 if (to != from)
744 memmove(to, from, len);
745 to += len;
746 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530747 }
Al Viroc3300aa2018-12-16 01:52:24 -0500748 if (!from[len])
749 break;
750 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530751 }
Al Virod2497e12018-12-16 01:37:06 -0500752 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530753 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530754}
755
756/**
757 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800758 * @sb: the file system superblock
luanshia1a07f22019-07-05 10:35:20 +0800759 * @mnt_opts: Smack mount options
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530760 * @kern_flags: mount option from kernel space or user space
761 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800762 *
763 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530764 *
765 * Allow filesystems with binary mount data to explicitly set Smack mount
766 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800767 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530768static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500769 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530770 unsigned long kern_flags,
771 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800772{
773 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000774 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800775 struct superblock_smack *sp = sb->s_security;
776 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800777 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500778 struct smack_mnt_opts *opts = mnt_opts;
779 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800780
Seth Forshee9f50eda2015-09-23 15:16:06 -0500781 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800782 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700783
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700784 if (inode->i_security == NULL) {
785 int rc = lsm_inode_alloc(inode);
786
787 if (rc)
788 return rc;
789 }
790
Himanshu Shukla2097f592016-11-10 16:19:52 +0530791 if (!smack_privileged(CAP_MAC_ADMIN)) {
792 /*
793 * Unprivileged mounts don't get to specify Smack values.
794 */
Al Viro12085b12018-12-13 15:18:05 -0500795 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530796 return -EPERM;
797 /*
798 * Unprivileged mounts get root and default from the caller.
799 */
800 skp = smk_of_current();
801 sp->smk_root = skp;
802 sp->smk_default = skp;
803 /*
804 * For a handful of fs types with no user-controlled
805 * backing store it's okay to trust security labels
806 * in the filesystem. The rest are untrusted.
807 */
808 if (sb->s_user_ns != &init_user_ns &&
809 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
810 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500811 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530812 sp->smk_flags |= SMK_SB_UNTRUSTED;
813 }
814 }
815
Seth Forshee9f50eda2015-09-23 15:16:06 -0500816 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800817
Al Viro12085b12018-12-13 15:18:05 -0500818 if (opts) {
819 if (opts->fsdefault) {
820 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200821 if (IS_ERR(skp))
822 return PTR_ERR(skp);
823 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500824 }
825 if (opts->fsfloor) {
826 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530827 if (IS_ERR(skp))
828 return PTR_ERR(skp);
829 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500830 }
831 if (opts->fshat) {
832 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530833 if (IS_ERR(skp))
834 return PTR_ERR(skp);
835 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500836 }
837 if (opts->fsroot) {
838 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200839 if (IS_ERR(skp))
840 return PTR_ERR(skp);
841 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500842 }
843 if (opts->fstransmute) {
844 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200845 if (IS_ERR(skp))
846 return PTR_ERR(skp);
847 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500848 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800849 }
850 }
851
852 /*
853 * Initialize the root inode.
854 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700855 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800856
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700857 if (transmute) {
858 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700859 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700860 }
Casey Schauflere830b392013-05-22 18:43:07 -0700861
Casey Schauflere114e472008-02-04 22:29:50 -0800862 return 0;
863}
864
865/**
866 * smack_sb_statfs - Smack check on statfs
867 * @dentry: identifies the file system in question
868 *
869 * Returns 0 if current can read the floor of the filesystem,
870 * and error code otherwise
871 */
872static int smack_sb_statfs(struct dentry *dentry)
873{
874 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200875 int rc;
876 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800877
Eric Parisa2694342011-04-25 13:10:27 -0400878 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200879 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
880
881 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700882 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200883 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800884}
885
Casey Schauflere114e472008-02-04 22:29:50 -0800886/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800887 * BPRM hooks
888 */
889
Casey Schauflerce8a4322011-09-29 18:21:01 -0700890/**
891 * smack_bprm_set_creds - set creds for exec
892 * @bprm: the exec information
893 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100894 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700895 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800896static int smack_bprm_set_creds(struct linux_binprm *bprm)
897{
Al Viro496ad9a2013-01-23 17:07:38 -0500898 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800899 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800900 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500901 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800902 int rc;
903
Kees Cookddb4a142017-07-18 15:25:23 -0700904 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800905 return 0;
906
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800907 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300908 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800909 return 0;
910
Seth Forshee809c02e2016-04-26 14:36:22 -0500911 sbsp = inode->i_sb->s_security;
912 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
913 isp->smk_task != sbsp->smk_root)
914 return 0;
915
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300916 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100917 struct task_struct *tracer;
918 rc = 0;
919
920 rcu_read_lock();
921 tracer = ptrace_parent(current);
922 if (likely(tracer != NULL))
923 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200924 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100925 PTRACE_MODE_ATTACH,
926 __func__);
927 rcu_read_unlock();
928
929 if (rc != 0)
930 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200931 }
932 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300933 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800934
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300935 bsp->smk_task = isp->smk_task;
936 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800937
Kees Cookccbb6e12017-07-18 15:25:26 -0700938 /* Decide if this is a secure exec. */
939 if (bsp->smk_task != bsp->smk_forked)
940 bprm->secureexec = 1;
941
Casey Schaufler676dac42010-12-02 06:43:39 -0800942 return 0;
943}
944
945/*
Casey Schauflere114e472008-02-04 22:29:50 -0800946 * Inode hooks
947 */
948
949/**
950 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800951 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800952 *
luanshia1a07f22019-07-05 10:35:20 +0800953 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800954 */
955static int smack_inode_alloc_security(struct inode *inode)
956{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700957 struct smack_known *skp = smk_of_current();
958
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700959 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800960 return 0;
961}
962
963/**
Casey Schauflere114e472008-02-04 22:29:50 -0800964 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200965 * @inode: the newly created inode
966 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500967 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800968 * @name: where to put the attribute name
969 * @value: where to put the attribute value
970 * @len: where to put the length of the attribute
971 *
972 * Returns 0 if it all works out, -ENOMEM if there's no memory
973 */
974static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900975 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500976 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800977{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800978 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700979 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200980 struct smack_known *isp = smk_of_inode(inode);
981 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800982 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800983
Tetsuo Handa95489062013-07-25 05:44:02 +0900984 if (name)
985 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800986
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100987 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800988 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200989 may = smk_access_entry(skp->smk_known, dsp->smk_known,
990 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800991 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200992
993 /*
994 * If the access rule allows transmutation and
995 * the directory requests transmutation then
996 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700997 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200998 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800999 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001000 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001001 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001002 issp->smk_flags |= SMK_INODE_CHANGED;
1003 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001004
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001005 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001006 if (*value == NULL)
1007 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001008
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001009 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001010 }
Casey Schauflere114e472008-02-04 22:29:50 -08001011
1012 return 0;
1013}
1014
1015/**
1016 * smack_inode_link - Smack check on link
1017 * @old_dentry: the existing object
1018 * @dir: unused
1019 * @new_dentry: the new object
1020 *
1021 * Returns 0 if access is permitted, an error code otherwise
1022 */
1023static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1024 struct dentry *new_dentry)
1025{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001026 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001027 struct smk_audit_info ad;
1028 int rc;
1029
Eric Parisa2694342011-04-25 13:10:27 -04001030 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001031 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001032
David Howellsc6f493d2015-03-17 22:26:22 +00001033 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001034 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001035 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001036
David Howells88025652015-01-29 12:02:32 +00001037 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001038 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001039 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1040 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001041 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001042 }
1043
1044 return rc;
1045}
1046
1047/**
1048 * smack_inode_unlink - Smack check on inode deletion
1049 * @dir: containing directory object
1050 * @dentry: file to unlink
1051 *
1052 * Returns 0 if current can write the containing directory
1053 * and the object, error code otherwise
1054 */
1055static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1056{
David Howellsc6f493d2015-03-17 22:26:22 +00001057 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001058 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001059 int rc;
1060
Eric Parisa2694342011-04-25 13:10:27 -04001061 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001062 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1063
Casey Schauflere114e472008-02-04 22:29:50 -08001064 /*
1065 * You need write access to the thing you're unlinking
1066 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001067 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001068 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001069 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001070 /*
1071 * You also need write access to the containing directory
1072 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001073 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001074 smk_ad_setfield_u_fs_inode(&ad, dir);
1075 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001076 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001077 }
Casey Schauflere114e472008-02-04 22:29:50 -08001078 return rc;
1079}
1080
1081/**
1082 * smack_inode_rmdir - Smack check on directory deletion
1083 * @dir: containing directory object
1084 * @dentry: directory to unlink
1085 *
1086 * Returns 0 if current can write the containing directory
1087 * and the directory, error code otherwise
1088 */
1089static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1090{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001091 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001092 int rc;
1093
Eric Parisa2694342011-04-25 13:10:27 -04001094 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001095 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1096
Casey Schauflere114e472008-02-04 22:29:50 -08001097 /*
1098 * You need write access to the thing you're removing
1099 */
David Howellsc6f493d2015-03-17 22:26:22 +00001100 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1101 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001103 /*
1104 * You also need write access to the containing directory
1105 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001106 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001107 smk_ad_setfield_u_fs_inode(&ad, dir);
1108 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001109 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001110 }
Casey Schauflere114e472008-02-04 22:29:50 -08001111
1112 return rc;
1113}
1114
1115/**
1116 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001117 * @old_inode: unused
1118 * @old_dentry: the old object
1119 * @new_inode: unused
1120 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001121 *
1122 * Read and write access is required on both the old and
1123 * new directories.
1124 *
1125 * Returns 0 if access is permitted, an error code otherwise
1126 */
1127static int smack_inode_rename(struct inode *old_inode,
1128 struct dentry *old_dentry,
1129 struct inode *new_inode,
1130 struct dentry *new_dentry)
1131{
1132 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001133 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001134 struct smk_audit_info ad;
1135
Eric Parisa2694342011-04-25 13:10:27 -04001136 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001137 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001138
David Howellsc6f493d2015-03-17 22:26:22 +00001139 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001140 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001141 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001142
David Howells88025652015-01-29 12:02:32 +00001143 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001144 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1146 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001147 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001148 }
Casey Schauflere114e472008-02-04 22:29:50 -08001149 return rc;
1150}
1151
1152/**
1153 * smack_inode_permission - Smack version of permission()
1154 * @inode: the inode in question
1155 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001156 *
1157 * This is the important Smack hook.
1158 *
luanshia1a07f22019-07-05 10:35:20 +08001159 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001160 */
Al Viroe74f71e2011-06-20 19:38:15 -04001161static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001162{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001163 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001164 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001165 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001166 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001167
1168 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001169 /*
1170 * No permission to check. Existence test. Yup, it's there.
1171 */
1172 if (mask == 0)
1173 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001174
Seth Forshee9f50eda2015-09-23 15:16:06 -05001175 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1176 if (smk_of_inode(inode) != sbsp->smk_root)
1177 return -EACCES;
1178 }
1179
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001180 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001181 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001182 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001183 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001184 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001185 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1186 rc = smk_bu_inode(inode, mask, rc);
1187 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001188}
1189
1190/**
1191 * smack_inode_setattr - Smack check for setting attributes
1192 * @dentry: the object
1193 * @iattr: for the force flag
1194 *
1195 * Returns 0 if access is permitted, an error code otherwise
1196 */
1197static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1198{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001199 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001200 int rc;
1201
Casey Schauflere114e472008-02-04 22:29:50 -08001202 /*
1203 * Need to allow for clearing the setuid bit.
1204 */
1205 if (iattr->ia_valid & ATTR_FORCE)
1206 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001207 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001208 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001209
David Howellsc6f493d2015-03-17 22:26:22 +00001210 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1211 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001212 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001213}
1214
1215/**
1216 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001217 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001218 *
1219 * Returns 0 if access is permitted, an error code otherwise
1220 */
Al Viro3f7036a2015-03-08 19:28:30 -04001221static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001222{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001223 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001224 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001225 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001226
Eric Parisf48b7392011-04-25 12:54:27 -04001227 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001228 smk_ad_setfield_u_fs_path(&ad, *path);
1229 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1230 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001231 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001232}
1233
1234/**
1235 * smack_inode_setxattr - Smack check for setting xattrs
1236 * @dentry: the object
1237 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001238 * @value: value of the attribute
1239 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001240 * @flags: unused
1241 *
1242 * This protects the Smack attribute explicitly.
1243 *
1244 * Returns 0 if access is permitted, an error code otherwise
1245 */
David Howells8f0cfa52008-04-29 00:59:41 -07001246static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1247 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001248{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001249 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001250 struct smack_known *skp;
1251 int check_priv = 0;
1252 int check_import = 0;
1253 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001254 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001255
Casey Schaufler19760ad2013-12-16 16:27:26 -08001256 /*
1257 * Check label validity here so import won't fail in post_setxattr
1258 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001259 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1260 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001261 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1262 check_priv = 1;
1263 check_import = 1;
1264 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1265 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1266 check_priv = 1;
1267 check_import = 1;
1268 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001269 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001270 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001271 if (size != TRANS_TRUE_SIZE ||
1272 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1273 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001274 } else
1275 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1276
Casey Schaufler19760ad2013-12-16 16:27:26 -08001277 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1278 rc = -EPERM;
1279
1280 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001281 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001282 if (IS_ERR(skp))
1283 rc = PTR_ERR(skp);
1284 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001285 (skp == &smack_known_star || skp == &smack_known_web)))
1286 rc = -EINVAL;
1287 }
1288
Eric Parisa2694342011-04-25 13:10:27 -04001289 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001290 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1291
Casey Schauflerd166c802014-08-27 14:51:27 -07001292 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001293 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1294 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001295 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001296
1297 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001298}
1299
1300/**
1301 * smack_inode_post_setxattr - Apply the Smack update approved above
1302 * @dentry: object
1303 * @name: attribute name
1304 * @value: attribute value
1305 * @size: attribute size
1306 * @flags: unused
1307 *
1308 * Set the pointer in the inode blob to the entry found
1309 * in the master label list.
1310 */
David Howells8f0cfa52008-04-29 00:59:41 -07001311static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1312 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001313{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001314 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001315 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001316
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001317 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1318 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1319 return;
1320 }
1321
Casey Schaufler676dac42010-12-02 06:43:39 -08001322 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001323 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001324 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001325 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001326 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 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))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001329 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001330 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 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_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001334 }
Casey Schauflere114e472008-02-04 22:29:50 -08001335
1336 return;
1337}
1338
Casey Schauflerce8a4322011-09-29 18:21:01 -07001339/**
Casey Schauflere114e472008-02-04 22:29:50 -08001340 * smack_inode_getxattr - Smack check on getxattr
1341 * @dentry: the object
1342 * @name: unused
1343 *
1344 * Returns 0 if access is permitted, an error code otherwise
1345 */
David Howells8f0cfa52008-04-29 00:59:41 -07001346static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001347{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001348 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001349 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001350
Eric Parisa2694342011-04-25 13:10:27 -04001351 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001352 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1353
David Howellsc6f493d2015-03-17 22:26:22 +00001354 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1355 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001356 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001357}
1358
Casey Schauflerce8a4322011-09-29 18:21:01 -07001359/**
Casey Schauflere114e472008-02-04 22:29:50 -08001360 * smack_inode_removexattr - Smack check on removexattr
1361 * @dentry: the object
1362 * @name: name of the attribute
1363 *
1364 * Removing the Smack attribute requires CAP_MAC_ADMIN
1365 *
1366 * Returns 0 if access is permitted, an error code otherwise
1367 */
David Howells8f0cfa52008-04-29 00:59:41 -07001368static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001369{
Casey Schaufler676dac42010-12-02 06:43:39 -08001370 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001371 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001372 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001373
Casey Schauflerbcdca222008-02-23 15:24:04 -08001374 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1375 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001376 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001377 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001378 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301379 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001380 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001381 rc = -EPERM;
1382 } else
1383 rc = cap_inode_removexattr(dentry, name);
1384
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001385 if (rc != 0)
1386 return rc;
1387
Eric Parisa2694342011-04-25 13:10:27 -04001388 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001389 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001390
David Howellsc6f493d2015-03-17 22:26:22 +00001391 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1392 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001393 if (rc != 0)
1394 return rc;
1395
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001396 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001397 /*
1398 * Don't do anything special for these.
1399 * XATTR_NAME_SMACKIPIN
1400 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001401 */
José Bollo80124952016-01-12 21:23:40 +01001402 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001403 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001404 struct superblock_smack *sbsp = sbp->s_security;
1405
1406 isp->smk_inode = sbsp->smk_default;
1407 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001408 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001409 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001410 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001411 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1412 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001413
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001414 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001415}
1416
1417/**
1418 * smack_inode_getsecurity - get smack xattrs
1419 * @inode: the object
1420 * @name: attribute name
1421 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001422 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001423 *
1424 * Returns the size of the attribute or an error code
1425 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001426static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001427 const char *name, void **buffer,
1428 bool alloc)
1429{
1430 struct socket_smack *ssp;
1431 struct socket *sock;
1432 struct super_block *sbp;
1433 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001434 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001435
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001436 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001437 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001438 else {
1439 /*
1440 * The rest of the Smack xattrs are only on sockets.
1441 */
1442 sbp = ip->i_sb;
1443 if (sbp->s_magic != SOCKFS_MAGIC)
1444 return -EOPNOTSUPP;
1445
1446 sock = SOCKET_I(ip);
1447 if (sock == NULL || sock->sk == NULL)
1448 return -EOPNOTSUPP;
1449
1450 ssp = sock->sk->sk_security;
1451
1452 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1453 isp = ssp->smk_in;
1454 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1455 isp = ssp->smk_out;
1456 else
1457 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001458 }
1459
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001460 if (alloc) {
1461 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1462 if (*buffer == NULL)
1463 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001464 }
1465
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001466 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001467}
1468
1469
1470/**
1471 * smack_inode_listsecurity - list the Smack attributes
1472 * @inode: the object
1473 * @buffer: where they go
1474 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001475 */
1476static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1477 size_t buffer_size)
1478{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001479 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001480
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001481 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001482 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001483
1484 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001485}
1486
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001487/**
1488 * smack_inode_getsecid - Extract inode's security id
1489 * @inode: inode to extract the info from
1490 * @secid: where result will be saved
1491 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001492static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001493{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001494 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001495
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001496 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001497}
1498
Casey Schauflere114e472008-02-04 22:29:50 -08001499/*
1500 * File Hooks
1501 */
1502
Casey Schaufler491a0b02016-01-26 15:08:35 -08001503/*
1504 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001505 *
1506 * Should access checks be done on each read or write?
1507 * UNICOS and SELinux say yes.
1508 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1509 *
1510 * I'll say no for now. Smack does not do the frequent
1511 * label changing that SELinux does.
1512 */
Casey Schauflere114e472008-02-04 22:29:50 -08001513
1514/**
1515 * smack_file_alloc_security - assign a file security blob
1516 * @file: the object
1517 *
1518 * The security blob for a file is a pointer to the master
1519 * label list, so no allocation is done.
1520 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001521 * f_security is the owner security information. It
1522 * isn't used on file access checks, it's for send_sigio.
1523 *
Casey Schauflere114e472008-02-04 22:29:50 -08001524 * Returns 0
1525 */
1526static int smack_file_alloc_security(struct file *file)
1527{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001528 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001529
Casey Schauflerf28952a2018-11-12 09:38:53 -08001530 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001531 return 0;
1532}
1533
1534/**
Casey Schauflere114e472008-02-04 22:29:50 -08001535 * smack_file_ioctl - Smack check on ioctls
1536 * @file: the object
1537 * @cmd: what to do
1538 * @arg: unused
1539 *
1540 * Relies heavily on the correct use of the ioctl command conventions.
1541 *
1542 * Returns 0 if allowed, error code otherwise
1543 */
1544static int smack_file_ioctl(struct file *file, unsigned int cmd,
1545 unsigned long arg)
1546{
1547 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001548 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001549 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001550
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001551 if (unlikely(IS_PRIVATE(inode)))
1552 return 0;
1553
Eric Parisf48b7392011-04-25 12:54:27 -04001554 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001555 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001556
Casey Schauflerd166c802014-08-27 14:51:27 -07001557 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001558 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001559 rc = smk_bu_file(file, MAY_WRITE, rc);
1560 }
Casey Schauflere114e472008-02-04 22:29:50 -08001561
Casey Schauflerd166c802014-08-27 14:51:27 -07001562 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001563 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001564 rc = smk_bu_file(file, MAY_READ, rc);
1565 }
Casey Schauflere114e472008-02-04 22:29:50 -08001566
1567 return rc;
1568}
1569
1570/**
1571 * smack_file_lock - Smack check on file locking
1572 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001573 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001574 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001575 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001576 */
1577static int smack_file_lock(struct file *file, unsigned int cmd)
1578{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001579 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001580 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001581 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001582
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001583 if (unlikely(IS_PRIVATE(inode)))
1584 return 0;
1585
Eric Paris92f42502011-04-25 13:15:55 -04001586 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1587 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001588 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001589 rc = smk_bu_file(file, MAY_LOCK, rc);
1590 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001591}
1592
1593/**
1594 * smack_file_fcntl - Smack check on fcntl
1595 * @file: the object
1596 * @cmd: what action to check
1597 * @arg: unused
1598 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001599 * Generally these operations are harmless.
1600 * File locking operations present an obvious mechanism
1601 * for passing information, so they require write access.
1602 *
Casey Schauflere114e472008-02-04 22:29:50 -08001603 * Returns 0 if current has access, error code otherwise
1604 */
1605static int smack_file_fcntl(struct file *file, unsigned int cmd,
1606 unsigned long arg)
1607{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001608 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001609 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001610 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001611
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001612 if (unlikely(IS_PRIVATE(inode)))
1613 return 0;
1614
Casey Schauflere114e472008-02-04 22:29:50 -08001615 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001616 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001617 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001618 case F_SETLK:
1619 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001620 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1621 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001622 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001623 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001624 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001625 case F_SETOWN:
1626 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001627 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1628 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001629 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001630 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001631 break;
1632 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001633 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001634 }
1635
1636 return rc;
1637}
1638
1639/**
Al Viroe5467852012-05-30 13:30:51 -04001640 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001641 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1642 * if mapping anonymous memory.
1643 * @file contains the file structure for file to map (may be NULL).
1644 * @reqprot contains the protection requested by the application.
1645 * @prot contains the protection that will be applied by the kernel.
1646 * @flags contains the operational flags.
1647 * Return 0 if permission is granted.
1648 */
Al Viroe5467852012-05-30 13:30:51 -04001649static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001650 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001651 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001652{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001653 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001654 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001655 struct smack_rule *srp;
1656 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001657 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001658 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001659 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001660 int may;
1661 int mmay;
1662 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001663 int rc;
1664
Al Viro496ad9a2013-01-23 17:07:38 -05001665 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001666 return 0;
1667
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001668 if (unlikely(IS_PRIVATE(file_inode(file))))
1669 return 0;
1670
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001671 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001672 if (isp->smk_mmap == NULL)
1673 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001674 sbsp = file_inode(file)->i_sb->s_security;
1675 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1676 isp->smk_mmap != sbsp->smk_root)
1677 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001678 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001679
Casey Schauflerb17103a2018-11-09 16:12:56 -08001680 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001681 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001682 rc = 0;
1683
1684 rcu_read_lock();
1685 /*
1686 * For each Smack rule associated with the subject
1687 * label verify that the SMACK64MMAP also has access
1688 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001689 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001690 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001691 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001692 /*
1693 * Matching labels always allows access.
1694 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001695 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001696 continue;
1697 /*
1698 * If there is a matching local rule take
1699 * that into account as well.
1700 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001701 may = smk_access_entry(srp->smk_subject->smk_known,
1702 okp->smk_known,
1703 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001704 if (may == -ENOENT)
1705 may = srp->smk_access;
1706 else
1707 may &= srp->smk_access;
1708 /*
1709 * If may is zero the SMACK64MMAP subject can't
1710 * possibly have less access.
1711 */
1712 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001713 continue;
1714
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001715 /*
1716 * Fetch the global list entry.
1717 * If there isn't one a SMACK64MMAP subject
1718 * can't have as much access as current.
1719 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001720 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1721 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001722 if (mmay == -ENOENT) {
1723 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001724 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001725 }
1726 /*
1727 * If there is a local entry it modifies the
1728 * potential access, too.
1729 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001730 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1731 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001732 if (tmay != -ENOENT)
1733 mmay &= tmay;
1734
1735 /*
1736 * If there is any access available to current that is
1737 * not available to a SMACK64MMAP subject
1738 * deny access.
1739 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001740 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001741 rc = -EACCES;
1742 break;
1743 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001744 }
1745
1746 rcu_read_unlock();
1747
1748 return rc;
1749}
1750
1751/**
Casey Schauflere114e472008-02-04 22:29:50 -08001752 * smack_file_set_fowner - set the file security blob value
1753 * @file: object in question
1754 *
Casey Schauflere114e472008-02-04 22:29:50 -08001755 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001756static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001757{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001758 struct smack_known **blob = smack_file(file);
1759
1760 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001761}
1762
1763/**
1764 * smack_file_send_sigiotask - Smack on sigio
1765 * @tsk: The target task
1766 * @fown: the object the signal come from
1767 * @signum: unused
1768 *
1769 * Allow a privileged task to get signals even if it shouldn't
1770 *
1771 * Returns 0 if a subject with the object's smack could
1772 * write to the task, an error code otherwise.
1773 */
1774static int smack_file_send_sigiotask(struct task_struct *tsk,
1775 struct fown_struct *fown, int signum)
1776{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001777 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001778 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001779 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001780 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001781 struct file *file;
1782 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001783 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001784
1785 /*
1786 * struct fown_struct is never outside the context of a struct file
1787 */
1788 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001789
Etienne Bassetecfcc532009-04-08 20:40:06 +02001790 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001791 blob = smack_file(file);
1792 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001793 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1794 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001795
1796 rcu_read_lock();
1797 tcred = __task_cred(tsk);
1798 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001799 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001800 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001801
1802 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1803 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001804 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001805 return rc;
1806}
1807
1808/**
1809 * smack_file_receive - Smack file receive check
1810 * @file: the object
1811 *
1812 * Returns 0 if current has access, error code otherwise
1813 */
1814static int smack_file_receive(struct file *file)
1815{
Casey Schauflerd166c802014-08-27 14:51:27 -07001816 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001817 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001818 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001819 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001820 struct socket *sock;
1821 struct task_smack *tsp;
1822 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001823
Seung-Woo Kim97775822015-04-17 15:25:04 +09001824 if (unlikely(IS_PRIVATE(inode)))
1825 return 0;
1826
Casey Schaufler4482a442013-12-30 17:37:45 -08001827 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001828 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001829
Casey Schaufler51d59af2017-05-31 08:53:42 -07001830 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001831 sock = SOCKET_I(inode);
1832 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001833 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001834 /*
1835 * If the receiving process can't write to the
1836 * passed socket or if the passed socket can't
1837 * write to the receiving process don't accept
1838 * the passed socket.
1839 */
1840 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1841 rc = smk_bu_file(file, may, rc);
1842 if (rc < 0)
1843 return rc;
1844 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1845 rc = smk_bu_file(file, may, rc);
1846 return rc;
1847 }
Casey Schauflere114e472008-02-04 22:29:50 -08001848 /*
1849 * This code relies on bitmasks.
1850 */
1851 if (file->f_mode & FMODE_READ)
1852 may = MAY_READ;
1853 if (file->f_mode & FMODE_WRITE)
1854 may |= MAY_WRITE;
1855
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001856 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001857 rc = smk_bu_file(file, may, rc);
1858 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001859}
1860
Casey Schaufler531f1d42011-09-19 12:41:42 -07001861/**
Eric Paris83d49852012-04-04 13:45:40 -04001862 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001863 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001864 *
1865 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001866 * Allow the open only if the task has read access. There are
1867 * many read operations (e.g. fstat) that you can do with an
1868 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001869 *
luanshia1a07f22019-07-05 10:35:20 +08001870 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001871 */
Al Viro94817692018-07-10 14:13:18 -04001872static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001873{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001874 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001875 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001876 struct smk_audit_info ad;
1877 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001878
Casey Schauflera6834c02014-04-21 11:10:26 -07001879 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1880 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301881 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001882 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001883
1884 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001885}
1886
Casey Schauflere114e472008-02-04 22:29:50 -08001887/*
1888 * Task hooks
1889 */
1890
1891/**
David Howellsee18d642009-09-02 09:14:21 +01001892 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001893 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001894 * @gfp: the atomicity of any memory allocations
1895 *
1896 * Prepare a blank set of credentials for modification. This must allocate all
1897 * the memory the LSM module might require such that cred_transfer() can
1898 * complete without error.
1899 */
1900static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1901{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001902 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001903 return 0;
1904}
1905
1906
1907/**
David Howellsf1752ee2008-11-14 10:39:17 +11001908 * smack_cred_free - "free" task-level security credentials
1909 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001910 *
Casey Schauflere114e472008-02-04 22:29:50 -08001911 */
David Howellsf1752ee2008-11-14 10:39:17 +11001912static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001913{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001914 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001915 struct smack_rule *rp;
1916 struct list_head *l;
1917 struct list_head *n;
1918
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001919 smk_destroy_label_list(&tsp->smk_relabel);
1920
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001921 list_for_each_safe(l, n, &tsp->smk_rules) {
1922 rp = list_entry(l, struct smack_rule, list);
1923 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001924 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001925 }
Casey Schauflere114e472008-02-04 22:29:50 -08001926}
1927
1928/**
David Howellsd84f4f92008-11-14 10:39:23 +11001929 * smack_cred_prepare - prepare new set of credentials for modification
1930 * @new: the new credentials
1931 * @old: the original credentials
1932 * @gfp: the atomicity of any memory allocations
1933 *
1934 * Prepare a new set of credentials for modification.
1935 */
1936static int smack_cred_prepare(struct cred *new, const struct cred *old,
1937 gfp_t gfp)
1938{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001939 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001940 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001941 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001942
Casey Schauflerbbd36622018-11-12 09:30:56 -08001943 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301944
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001945 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1946 if (rc != 0)
1947 return rc;
1948
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001949 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1950 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001951 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001952}
1953
Randy Dunlap251a2a92009-02-18 11:42:33 -08001954/**
David Howellsee18d642009-09-02 09:14:21 +01001955 * smack_cred_transfer - Transfer the old credentials to the new credentials
1956 * @new: the new credentials
1957 * @old: the original credentials
1958 *
1959 * Fill in a set of blank credentials from another set of credentials.
1960 */
1961static void smack_cred_transfer(struct cred *new, const struct cred *old)
1962{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001963 struct task_smack *old_tsp = smack_cred(old);
1964 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001965
1966 new_tsp->smk_task = old_tsp->smk_task;
1967 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001968 mutex_init(&new_tsp->smk_rules_lock);
1969 INIT_LIST_HEAD(&new_tsp->smk_rules);
1970
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001971 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001972}
1973
1974/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001975 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001976 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001977 * @secid: where to put the result
1978 *
1979 * Sets the secid to contain a u32 version of the smack label.
1980 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001981static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001982{
1983 struct smack_known *skp;
1984
1985 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001986 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001987 *secid = skp->smk_secid;
1988 rcu_read_unlock();
1989}
1990
1991/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001992 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001993 * @new: points to the set of credentials to be modified.
1994 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001995 *
1996 * Set the security data for a kernel service.
1997 */
1998static int smack_kernel_act_as(struct cred *new, u32 secid)
1999{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002000 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002001
Casey Schaufler152f91d2016-11-14 09:38:15 -08002002 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002003 return 0;
2004}
2005
2006/**
2007 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002008 * @new: points to the set of credentials to be modified
2009 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002010 *
2011 * Set the file creation context in a set of credentials to the same
2012 * as the objective context of the specified inode
2013 */
2014static int smack_kernel_create_files_as(struct cred *new,
2015 struct inode *inode)
2016{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002017 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002018 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002019
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002020 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002021 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002022 return 0;
2023}
2024
2025/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002026 * smk_curacc_on_task - helper to log task related access
2027 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002028 * @access: the access requested
2029 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002030 *
2031 * Return 0 if access is permitted
2032 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002033static int smk_curacc_on_task(struct task_struct *p, int access,
2034 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002035{
2036 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002037 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002038 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002039
Casey Schaufler531f1d42011-09-19 12:41:42 -07002040 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002041 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002042 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002043 rc = smk_bu_task(p, access, rc);
2044 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002045}
2046
2047/**
Casey Schauflere114e472008-02-04 22:29:50 -08002048 * smack_task_setpgid - Smack check on setting pgid
2049 * @p: the task object
2050 * @pgid: unused
2051 *
2052 * Return 0 if write access is permitted
2053 */
2054static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2055{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002056 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002057}
2058
2059/**
2060 * smack_task_getpgid - Smack access check for getpgid
2061 * @p: the object task
2062 *
2063 * Returns 0 if current can read the object task, error code otherwise
2064 */
2065static int smack_task_getpgid(struct task_struct *p)
2066{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002067 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002068}
2069
2070/**
2071 * smack_task_getsid - Smack access check for getsid
2072 * @p: the object task
2073 *
2074 * Returns 0 if current can read the object task, error code otherwise
2075 */
2076static int smack_task_getsid(struct task_struct *p)
2077{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002078 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002079}
2080
2081/**
2082 * smack_task_getsecid - get the secid of the task
2083 * @p: the object task
2084 * @secid: where to put the result
2085 *
2086 * Sets the secid to contain a u32 version of the smack label.
2087 */
2088static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2089{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002090 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002091
2092 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002093}
2094
2095/**
2096 * smack_task_setnice - Smack check on setting nice
2097 * @p: the task object
2098 * @nice: unused
2099 *
2100 * Return 0 if write access is permitted
2101 */
2102static int smack_task_setnice(struct task_struct *p, int nice)
2103{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002104 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002105}
2106
2107/**
2108 * smack_task_setioprio - Smack check on setting ioprio
2109 * @p: the task object
2110 * @ioprio: unused
2111 *
2112 * Return 0 if write access is permitted
2113 */
2114static int smack_task_setioprio(struct task_struct *p, int ioprio)
2115{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002116 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002117}
2118
2119/**
2120 * smack_task_getioprio - Smack check on reading ioprio
2121 * @p: the task object
2122 *
2123 * Return 0 if read access is permitted
2124 */
2125static int smack_task_getioprio(struct task_struct *p)
2126{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002127 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002128}
2129
2130/**
2131 * smack_task_setscheduler - Smack check on setting scheduler
2132 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002133 *
2134 * Return 0 if read access is permitted
2135 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002136static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002137{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002138 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002139}
2140
2141/**
2142 * smack_task_getscheduler - Smack check on reading scheduler
2143 * @p: the task object
2144 *
2145 * Return 0 if read access is permitted
2146 */
2147static int smack_task_getscheduler(struct task_struct *p)
2148{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002149 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002150}
2151
2152/**
2153 * smack_task_movememory - Smack check on moving memory
2154 * @p: the task object
2155 *
2156 * Return 0 if write access is permitted
2157 */
2158static int smack_task_movememory(struct task_struct *p)
2159{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002160 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002161}
2162
2163/**
2164 * smack_task_kill - Smack check on signal delivery
2165 * @p: the task object
2166 * @info: unused
2167 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002168 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002169 *
2170 * Return 0 if write access is permitted
2171 *
Casey Schauflere114e472008-02-04 22:29:50 -08002172 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002173static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002174 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002175{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002176 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002177 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002178 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002179 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002180
Rafal Krypa18d872f2016-04-04 11:14:53 +02002181 if (!sig)
2182 return 0; /* null signal; existence test */
2183
Etienne Bassetecfcc532009-04-08 20:40:06 +02002184 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2185 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002186 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002187 * Sending a signal requires that the sender
2188 * can write the receiver.
2189 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002190 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002191 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2192 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002193 return rc;
2194 }
Casey Schauflere114e472008-02-04 22:29:50 -08002195 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002196 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002197 * specific behavior. This is not clean. For one thing
2198 * we can't take privilege into account.
2199 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002200 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002201 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2202 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002203 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002204}
2205
2206/**
Casey Schauflere114e472008-02-04 22:29:50 -08002207 * smack_task_to_inode - copy task smack into the inode blob
2208 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002209 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002210 *
2211 * Sets the smack pointer in the inode security blob
2212 */
2213static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2214{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002215 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002216 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002217
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002218 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002219 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002220}
2221
2222/*
2223 * Socket hooks.
2224 */
2225
2226/**
2227 * smack_sk_alloc_security - Allocate a socket blob
2228 * @sk: the socket
2229 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002230 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002231 *
2232 * Assign Smack pointers to current
2233 *
2234 * Returns 0 on success, -ENOMEM is there's no memory
2235 */
2236static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2237{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002238 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002239 struct socket_smack *ssp;
2240
2241 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2242 if (ssp == NULL)
2243 return -ENOMEM;
2244
jooseong lee08382c92016-11-03 11:54:39 +01002245 /*
2246 * Sockets created by kernel threads receive web label.
2247 */
2248 if (unlikely(current->flags & PF_KTHREAD)) {
2249 ssp->smk_in = &smack_known_web;
2250 ssp->smk_out = &smack_known_web;
2251 } else {
2252 ssp->smk_in = skp;
2253 ssp->smk_out = skp;
2254 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002255 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002256
2257 sk->sk_security = ssp;
2258
2259 return 0;
2260}
2261
2262/**
2263 * smack_sk_free_security - Free a socket blob
2264 * @sk: the socket
2265 *
2266 * Clears the blob pointer
2267 */
2268static void smack_sk_free_security(struct sock *sk)
2269{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302270#ifdef SMACK_IPV6_PORT_LABELING
2271 struct smk_port_label *spp;
2272
2273 if (sk->sk_family == PF_INET6) {
2274 rcu_read_lock();
2275 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2276 if (spp->smk_sock != sk)
2277 continue;
2278 spp->smk_can_reuse = 1;
2279 break;
2280 }
2281 rcu_read_unlock();
2282 }
2283#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002284 kfree(sk->sk_security);
2285}
2286
2287/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002288* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002289* @sip: the object end
2290*
2291* looks for host based access restrictions
2292*
2293* This version will only be appropriate for really small sets of single label
2294* hosts. The caller is responsible for ensuring that the RCU read lock is
2295* taken before calling this function.
2296*
2297* Returns the label of the far end or NULL if it's not special.
2298*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002299static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002300{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002301 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002302 struct in_addr *siap = &sip->sin_addr;
2303
2304 if (siap->s_addr == 0)
2305 return NULL;
2306
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002307 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2308 /*
2309 * we break after finding the first match because
2310 * the list is sorted from longest to shortest mask
2311 * so we have found the most specific match
2312 */
2313 if (snp->smk_host.s_addr ==
2314 (siap->s_addr & snp->smk_mask.s_addr))
2315 return snp->smk_label;
2316
2317 return NULL;
2318}
2319
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002320/*
2321 * smk_ipv6_localhost - Check for local ipv6 host address
2322 * @sip: the address
2323 *
2324 * Returns boolean true if this is the localhost address
2325 */
2326static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2327{
2328 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2329 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2330
2331 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2332 ntohs(be16p[7]) == 1)
2333 return true;
2334 return false;
2335}
2336
2337/**
2338* smack_ipv6host_label - check host based restrictions
2339* @sip: the object end
2340*
2341* looks for host based access restrictions
2342*
2343* This version will only be appropriate for really small sets of single label
2344* hosts. The caller is responsible for ensuring that the RCU read lock is
2345* taken before calling this function.
2346*
2347* Returns the label of the far end or NULL if it's not special.
2348*/
2349static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2350{
2351 struct smk_net6addr *snp;
2352 struct in6_addr *sap = &sip->sin6_addr;
2353 int i;
2354 int found = 0;
2355
2356 /*
2357 * It's local. Don't look for a host label.
2358 */
2359 if (smk_ipv6_localhost(sip))
2360 return NULL;
2361
2362 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002363 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002364 * If the label is NULL the entry has
2365 * been renounced. Ignore it.
2366 */
2367 if (snp->smk_label == NULL)
2368 continue;
2369 /*
Paul Moore07feee82009-03-27 17:10:54 -04002370 * we break after finding the first match because
2371 * the list is sorted from longest to shortest mask
2372 * so we have found the most specific match
2373 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002374 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002375 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2376 snp->smk_host.s6_addr16[i]) {
2377 found = 0;
2378 break;
2379 }
Etienne Basset43031542009-03-27 17:11:01 -04002380 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002381 if (found)
2382 return snp->smk_label;
2383 }
Paul Moore07feee82009-03-27 17:10:54 -04002384
2385 return NULL;
2386}
2387
2388/**
Casey Schauflere114e472008-02-04 22:29:50 -08002389 * smack_netlabel - Set the secattr on a socket
2390 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002391 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002392 *
2393 * Convert the outbound smack value (smk_out) to a
2394 * secattr and attach it to the socket.
2395 *
2396 * Returns 0 on success or an error code
2397 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002398static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002399{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002400 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002401 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002402 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002403
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002404 /*
2405 * Usually the netlabel code will handle changing the
2406 * packet labeling based on the label.
2407 * The case of a single label host is different, because
2408 * a single label host should never get a labeled packet
2409 * even though the label is usually associated with a packet
2410 * label.
2411 */
2412 local_bh_disable();
2413 bh_lock_sock_nested(sk);
2414
2415 if (ssp->smk_out == smack_net_ambient ||
2416 labeled == SMACK_UNLABELED_SOCKET)
2417 netlbl_sock_delattr(sk);
2418 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002419 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002420 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002421 }
2422
2423 bh_unlock_sock(sk);
2424 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002425
Casey Schauflere114e472008-02-04 22:29:50 -08002426 return rc;
2427}
2428
2429/**
Paul Moore07feee82009-03-27 17:10:54 -04002430 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2431 * @sk: the socket
2432 * @sap: the destination address
2433 *
2434 * Set the correct secattr for the given socket based on the destination
2435 * address and perform any outbound access checks needed.
2436 *
2437 * Returns 0 on success or an error code.
2438 *
2439 */
2440static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2441{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002442 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002443 int rc;
2444 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002445 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002446 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002447 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002448
2449 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002450 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002451 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002452#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002453 struct lsm_network_audit net;
2454
Eric Paris48c62af2012-04-02 13:15:44 -04002455 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2456 ad.a.u.net->family = sap->sin_family;
2457 ad.a.u.net->dport = sap->sin_port;
2458 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002459#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002460 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002461 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002462 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2463 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002464 } else {
2465 sk_lbl = SMACK_CIPSO_SOCKET;
2466 rc = 0;
2467 }
2468 rcu_read_unlock();
2469 if (rc != 0)
2470 return rc;
2471
2472 return smack_netlabel(sk, sk_lbl);
2473}
2474
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002475/**
2476 * smk_ipv6_check - check Smack access
2477 * @subject: subject Smack label
2478 * @object: object Smack label
2479 * @address: address
2480 * @act: the action being taken
2481 *
2482 * Check an IPv6 access
2483 */
2484static int smk_ipv6_check(struct smack_known *subject,
2485 struct smack_known *object,
2486 struct sockaddr_in6 *address, int act)
2487{
2488#ifdef CONFIG_AUDIT
2489 struct lsm_network_audit net;
2490#endif
2491 struct smk_audit_info ad;
2492 int rc;
2493
2494#ifdef CONFIG_AUDIT
2495 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2496 ad.a.u.net->family = PF_INET6;
2497 ad.a.u.net->dport = ntohs(address->sin6_port);
2498 if (act == SMK_RECEIVING)
2499 ad.a.u.net->v6info.saddr = address->sin6_addr;
2500 else
2501 ad.a.u.net->v6info.daddr = address->sin6_addr;
2502#endif
2503 rc = smk_access(subject, object, MAY_WRITE, &ad);
2504 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2505 return rc;
2506}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002507
2508#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002509/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002510 * smk_ipv6_port_label - Smack port access table management
2511 * @sock: socket
2512 * @address: address
2513 *
2514 * Create or update the port list entry
2515 */
2516static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2517{
2518 struct sock *sk = sock->sk;
2519 struct sockaddr_in6 *addr6;
2520 struct socket_smack *ssp = sock->sk->sk_security;
2521 struct smk_port_label *spp;
2522 unsigned short port = 0;
2523
2524 if (address == NULL) {
2525 /*
2526 * This operation is changing the Smack information
2527 * on the bound socket. Take the changes to the port
2528 * as well.
2529 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302530 rcu_read_lock();
2531 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002532 if (sk != spp->smk_sock)
2533 continue;
2534 spp->smk_in = ssp->smk_in;
2535 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302536 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002537 return;
2538 }
2539 /*
2540 * A NULL address is only used for updating existing
2541 * bound entries. If there isn't one, it's OK.
2542 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302543 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002544 return;
2545 }
2546
2547 addr6 = (struct sockaddr_in6 *)address;
2548 port = ntohs(addr6->sin6_port);
2549 /*
2550 * This is a special case that is safely ignored.
2551 */
2552 if (port == 0)
2553 return;
2554
2555 /*
2556 * Look for an existing port list entry.
2557 * This is an indication that a port is getting reused.
2558 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302559 rcu_read_lock();
2560 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302561 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002562 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302563 if (spp->smk_can_reuse != 1) {
2564 rcu_read_unlock();
2565 return;
2566 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002567 spp->smk_port = port;
2568 spp->smk_sock = sk;
2569 spp->smk_in = ssp->smk_in;
2570 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302571 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302572 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002573 return;
2574 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302575 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002576 /*
2577 * A new port entry is required.
2578 */
2579 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2580 if (spp == NULL)
2581 return;
2582
2583 spp->smk_port = port;
2584 spp->smk_sock = sk;
2585 spp->smk_in = ssp->smk_in;
2586 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302587 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302588 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002589
Vishal Goel3c7ce342016-11-23 10:31:08 +05302590 mutex_lock(&smack_ipv6_lock);
2591 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2592 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002593 return;
2594}
Arnd Bergmann00720f02020-04-08 21:04:31 +02002595#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002596
2597/**
2598 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002599 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002600 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002601 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002602 *
2603 * Create or update the port list entry
2604 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002605static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002606 int act)
2607{
Casey Schauflerc6739442013-05-22 18:42:56 -07002608 struct smk_port_label *spp;
2609 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002610 struct smack_known *skp = NULL;
2611 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002612 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002613
2614 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002615 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002616 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002617 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002618 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002619 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002620 }
2621
2622 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002623 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002624 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002625 if (skp != NULL && object != NULL)
2626 return smk_ipv6_check(skp, object, address, act);
2627 if (skp == NULL)
2628 skp = smack_net_ambient;
2629 if (object == NULL)
2630 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002631
2632 /*
2633 * It's remote, so port lookup does no good.
2634 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002635 if (!smk_ipv6_localhost(address))
2636 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002637
2638 /*
2639 * It's local so the send check has to have passed.
2640 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002641 if (act == SMK_RECEIVING)
2642 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002643
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002644 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302645 rcu_read_lock();
2646 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302647 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002648 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002649 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002650 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002651 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002652 break;
2653 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302654 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002655
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002656 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002657}
2658
2659/**
Casey Schauflere114e472008-02-04 22:29:50 -08002660 * smack_inode_setsecurity - set smack xattrs
2661 * @inode: the object
2662 * @name: attribute name
2663 * @value: attribute value
2664 * @size: size of the attribute
2665 * @flags: unused
2666 *
2667 * Sets the named attribute in the appropriate blob
2668 *
2669 * Returns 0 on success, or an error code
2670 */
2671static int smack_inode_setsecurity(struct inode *inode, const char *name,
2672 const void *value, size_t size, int flags)
2673{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002674 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002675 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002676 struct socket_smack *ssp;
2677 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002678 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002679
Casey Schauflerf7112e62012-05-06 15:22:02 -07002680 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302681 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002682
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002683 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002684 if (IS_ERR(skp))
2685 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002686
2687 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002688 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002689 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002690 return 0;
2691 }
2692 /*
2693 * The rest of the Smack xattrs are only on sockets.
2694 */
2695 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2696 return -EOPNOTSUPP;
2697
2698 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002699 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002700 return -EOPNOTSUPP;
2701
2702 ssp = sock->sk->sk_security;
2703
2704 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002705 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002706 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002707 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002708 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002709 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2710 if (rc != 0)
2711 printk(KERN_WARNING
2712 "Smack: \"%s\" netlbl error %d.\n",
2713 __func__, -rc);
2714 }
Casey Schauflere114e472008-02-04 22:29:50 -08002715 } else
2716 return -EOPNOTSUPP;
2717
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002718#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002719 if (sock->sk->sk_family == PF_INET6)
2720 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002721#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002722
Casey Schauflere114e472008-02-04 22:29:50 -08002723 return 0;
2724}
2725
2726/**
2727 * smack_socket_post_create - finish socket setup
2728 * @sock: the socket
2729 * @family: protocol family
2730 * @type: unused
2731 * @protocol: unused
2732 * @kern: unused
2733 *
2734 * Sets the netlabel information on the socket
2735 *
2736 * Returns 0 on success, and error code otherwise
2737 */
2738static int smack_socket_post_create(struct socket *sock, int family,
2739 int type, int protocol, int kern)
2740{
Marcin Lis74123012015-01-22 15:40:33 +01002741 struct socket_smack *ssp;
2742
2743 if (sock->sk == NULL)
2744 return 0;
2745
2746 /*
2747 * Sockets created by kernel threads receive web label.
2748 */
2749 if (unlikely(current->flags & PF_KTHREAD)) {
2750 ssp = sock->sk->sk_security;
2751 ssp->smk_in = &smack_known_web;
2752 ssp->smk_out = &smack_known_web;
2753 }
2754
2755 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002756 return 0;
2757 /*
2758 * Set the outbound netlbl.
2759 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002760 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2761}
2762
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002763/**
2764 * smack_socket_socketpair - create socket pair
2765 * @socka: one socket
2766 * @sockb: another socket
2767 *
2768 * Cross reference the peer labels for SO_PEERSEC
2769 *
luanshia1a07f22019-07-05 10:35:20 +08002770 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002771 */
2772static int smack_socket_socketpair(struct socket *socka,
2773 struct socket *sockb)
2774{
2775 struct socket_smack *asp = socka->sk->sk_security;
2776 struct socket_smack *bsp = sockb->sk->sk_security;
2777
2778 asp->smk_packet = bsp->smk_out;
2779 bsp->smk_packet = asp->smk_out;
2780
2781 return 0;
2782}
2783
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002784#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002785/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002786 * smack_socket_bind - record port binding information.
2787 * @sock: the socket
2788 * @address: the port address
2789 * @addrlen: size of the address
2790 *
2791 * Records the label bound to a port.
2792 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002793 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002794 */
2795static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2796 int addrlen)
2797{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002798 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2799 if (addrlen < SIN6_LEN_RFC2133 ||
2800 address->sa_family != AF_INET6)
2801 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002802 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002803 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002804 return 0;
2805}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002806#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002807
2808/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002809 * smack_socket_connect - connect access check
2810 * @sock: the socket
2811 * @sap: the other end
2812 * @addrlen: size of sap
2813 *
2814 * Verifies that a connection may be possible
2815 *
2816 * Returns 0 on success, and error code otherwise
2817 */
2818static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2819 int addrlen)
2820{
Casey Schauflerc6739442013-05-22 18:42:56 -07002821 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002822
Casey Schauflerc6739442013-05-22 18:42:56 -07002823 if (sock->sk == NULL)
2824 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002825 if (sock->sk->sk_family != PF_INET &&
2826 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2827 return 0;
2828 if (addrlen < offsetofend(struct sockaddr, sa_family))
2829 return 0;
2830 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2831 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002832 struct smack_known *rsp = NULL;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002833
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002834 if (addrlen < SIN6_LEN_RFC2133)
2835 return 0;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002836 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2837 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002838 if (rsp != NULL) {
2839 struct socket_smack *ssp = sock->sk->sk_security;
2840
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002841 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002842 SMK_CONNECTING);
2843 }
Arnd Bergmann00720f02020-04-08 21:04:31 +02002844 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2845 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2846
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002847 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002848 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002849 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2850 return 0;
2851 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002852 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002853}
2854
2855/**
2856 * smack_flags_to_may - convert S_ to MAY_ values
2857 * @flags: the S_ value
2858 *
2859 * Returns the equivalent MAY_ value
2860 */
2861static int smack_flags_to_may(int flags)
2862{
2863 int may = 0;
2864
2865 if (flags & S_IRUGO)
2866 may |= MAY_READ;
2867 if (flags & S_IWUGO)
2868 may |= MAY_WRITE;
2869 if (flags & S_IXUGO)
2870 may |= MAY_EXEC;
2871
2872 return may;
2873}
2874
2875/**
2876 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2877 * @msg: the object
2878 *
2879 * Returns 0
2880 */
2881static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2882{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002883 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002884
Casey Schauflerecd5f822018-11-20 11:55:02 -08002885 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002886 return 0;
2887}
2888
2889/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002890 * smack_of_ipc - the smack pointer for the ipc
2891 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002892 *
2893 * Returns a pointer to the smack value
2894 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002895static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002896{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002897 struct smack_known **blob = smack_ipc(isp);
2898
2899 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002900}
2901
2902/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002903 * smack_ipc_alloc_security - Set the security blob for ipc
2904 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002905 *
2906 * Returns 0
2907 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002908static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002909{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002910 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002911
Casey Schaufler019bcca2018-09-21 17:19:54 -07002912 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002913 return 0;
2914}
2915
2916/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002917 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002918 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002919 * @access : access requested
2920 *
2921 * Returns 0 if current has the requested access, error code otherwise
2922 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002923static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002924{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002925 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002926 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002927 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002928
2929#ifdef CONFIG_AUDIT
2930 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002931 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002932#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002933 rc = smk_curacc(ssp, access, &ad);
2934 rc = smk_bu_current("shm", ssp, access, rc);
2935 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002936}
2937
2938/**
Casey Schauflere114e472008-02-04 22:29:50 -08002939 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002940 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002941 * @shmflg: access requested
2942 *
2943 * Returns 0 if current has the requested access, error code otherwise
2944 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002945static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002946{
Casey Schauflere114e472008-02-04 22:29:50 -08002947 int may;
2948
2949 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002950 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002951}
2952
2953/**
2954 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002955 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002956 * @cmd: what it wants to do
2957 *
2958 * Returns 0 if current has the requested access, error code otherwise
2959 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002960static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002961{
Casey Schauflere114e472008-02-04 22:29:50 -08002962 int may;
2963
2964 switch (cmd) {
2965 case IPC_STAT:
2966 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002967 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002968 may = MAY_READ;
2969 break;
2970 case IPC_SET:
2971 case SHM_LOCK:
2972 case SHM_UNLOCK:
2973 case IPC_RMID:
2974 may = MAY_READWRITE;
2975 break;
2976 case IPC_INFO:
2977 case SHM_INFO:
2978 /*
2979 * System level information.
2980 */
2981 return 0;
2982 default:
2983 return -EINVAL;
2984 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002985 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002986}
2987
2988/**
2989 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002990 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002991 * @shmaddr: unused
2992 * @shmflg: access requested
2993 *
2994 * Returns 0 if current has the requested access, error code otherwise
2995 */
luanshia1a07f22019-07-05 10:35:20 +08002996static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08002997 int shmflg)
2998{
Casey Schauflere114e472008-02-04 22:29:50 -08002999 int may;
3000
3001 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003002 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003003}
3004
3005/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003006 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003007 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003008 * @access : access requested
3009 *
3010 * Returns 0 if current has the requested access, error code otherwise
3011 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003012static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003013{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003014 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003015 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003016 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003017
3018#ifdef CONFIG_AUDIT
3019 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003020 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003021#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003022 rc = smk_curacc(ssp, access, &ad);
3023 rc = smk_bu_current("sem", ssp, access, rc);
3024 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003025}
3026
3027/**
Casey Schauflere114e472008-02-04 22:29:50 -08003028 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003029 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003030 * @semflg: access requested
3031 *
3032 * Returns 0 if current has the requested access, error code otherwise
3033 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003034static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003035{
Casey Schauflere114e472008-02-04 22:29:50 -08003036 int may;
3037
3038 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003039 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003040}
3041
3042/**
3043 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003044 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003045 * @cmd: what it wants to do
3046 *
3047 * Returns 0 if current has the requested access, error code otherwise
3048 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003049static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003050{
Casey Schauflere114e472008-02-04 22:29:50 -08003051 int may;
3052
3053 switch (cmd) {
3054 case GETPID:
3055 case GETNCNT:
3056 case GETZCNT:
3057 case GETVAL:
3058 case GETALL:
3059 case IPC_STAT:
3060 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003061 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003062 may = MAY_READ;
3063 break;
3064 case SETVAL:
3065 case SETALL:
3066 case IPC_RMID:
3067 case IPC_SET:
3068 may = MAY_READWRITE;
3069 break;
3070 case IPC_INFO:
3071 case SEM_INFO:
3072 /*
3073 * System level information
3074 */
3075 return 0;
3076 default:
3077 return -EINVAL;
3078 }
3079
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003080 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003081}
3082
3083/**
3084 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003085 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003086 * @sops: unused
3087 * @nsops: unused
3088 * @alter: unused
3089 *
3090 * Treated as read and write in all cases.
3091 *
3092 * Returns 0 if access is allowed, error code otherwise
3093 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003094static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003095 unsigned nsops, int alter)
3096{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003097 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003098}
3099
3100/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003101 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003102 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003103 * @access : access requested
3104 *
3105 * return 0 if current has access, error otherwise
3106 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003107static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003108{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003109 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003110 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003111 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003112
3113#ifdef CONFIG_AUDIT
3114 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003115 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003116#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003117 rc = smk_curacc(msp, access, &ad);
3118 rc = smk_bu_current("msq", msp, access, rc);
3119 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003120}
3121
3122/**
Casey Schauflere114e472008-02-04 22:29:50 -08003123 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003124 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003125 * @msqflg: access requested
3126 *
3127 * Returns 0 if current has the requested access, error code otherwise
3128 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003129static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003130{
Casey Schauflere114e472008-02-04 22:29:50 -08003131 int may;
3132
3133 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003134 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003135}
3136
3137/**
3138 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003139 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003140 * @cmd: what it wants to do
3141 *
3142 * Returns 0 if current has the requested access, error code otherwise
3143 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003144static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003145{
Casey Schauflere114e472008-02-04 22:29:50 -08003146 int may;
3147
3148 switch (cmd) {
3149 case IPC_STAT:
3150 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003151 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003152 may = MAY_READ;
3153 break;
3154 case IPC_SET:
3155 case IPC_RMID:
3156 may = MAY_READWRITE;
3157 break;
3158 case IPC_INFO:
3159 case MSG_INFO:
3160 /*
3161 * System level information
3162 */
3163 return 0;
3164 default:
3165 return -EINVAL;
3166 }
3167
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003168 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003169}
3170
3171/**
3172 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003173 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003174 * @msg: unused
3175 * @msqflg: access requested
3176 *
3177 * Returns 0 if current has the requested access, error code otherwise
3178 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003179static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003180 int msqflg)
3181{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003182 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003183
Etienne Bassetecfcc532009-04-08 20:40:06 +02003184 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003185 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003186}
3187
3188/**
3189 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003190 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003191 * @msg: unused
3192 * @target: unused
3193 * @type: unused
3194 * @mode: unused
3195 *
3196 * Returns 0 if current has read and write access, error code otherwise
3197 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003198static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003199 struct task_struct *target, long type, int mode)
3200{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003201 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003202}
3203
3204/**
3205 * smack_ipc_permission - Smack access for ipc_permission()
3206 * @ipp: the object permissions
3207 * @flag: access requested
3208 *
3209 * Returns 0 if current has read and write access, error code otherwise
3210 */
3211static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3212{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003213 struct smack_known **blob = smack_ipc(ipp);
3214 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003215 int may = smack_flags_to_may(flag);
3216 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003217 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003218
Etienne Bassetecfcc532009-04-08 20:40:06 +02003219#ifdef CONFIG_AUDIT
3220 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3221 ad.a.u.ipc_id = ipp->id;
3222#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003223 rc = smk_curacc(iskp, may, &ad);
3224 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003225 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003226}
3227
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003228/**
3229 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003230 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003231 * @secid: where result will be saved
3232 */
3233static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3234{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003235 struct smack_known **blob = smack_ipc(ipp);
3236 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003237
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003238 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003239}
3240
Casey Schauflere114e472008-02-04 22:29:50 -08003241/**
3242 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003243 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003244 * @inode: the object
3245 *
3246 * Set the inode's security blob if it hasn't been done already.
3247 */
3248static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3249{
3250 struct super_block *sbp;
3251 struct superblock_smack *sbsp;
3252 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003253 struct smack_known *skp;
3254 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003255 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003256 char trattr[TRANS_TRUE_SIZE];
3257 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003258 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003259 struct dentry *dp;
3260
3261 if (inode == NULL)
3262 return;
3263
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003264 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003265
Casey Schauflere114e472008-02-04 22:29:50 -08003266 /*
3267 * If the inode is already instantiated
3268 * take the quick way out
3269 */
3270 if (isp->smk_flags & SMK_INODE_INSTANT)
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003271 return;
Casey Schauflere114e472008-02-04 22:29:50 -08003272
3273 sbp = inode->i_sb;
3274 sbsp = sbp->s_security;
3275 /*
3276 * We're going to use the superblock default label
3277 * if there's no label on the file.
3278 */
3279 final = sbsp->smk_default;
3280
3281 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003282 * If this is the root inode the superblock
3283 * may be in the process of initialization.
3284 * If that is the case use the root value out
3285 * of the superblock.
3286 */
3287 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003288 switch (sbp->s_magic) {
3289 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003290 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003291 /*
3292 * The cgroup filesystem is never mounted,
3293 * so there's no opportunity to set the mount
3294 * options.
3295 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003296 sbsp->smk_root = &smack_known_star;
3297 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003298 isp->smk_inode = sbsp->smk_root;
3299 break;
3300 case TMPFS_MAGIC:
3301 /*
3302 * What about shmem/tmpfs anonymous files with dentry
3303 * obtained from d_alloc_pseudo()?
3304 */
3305 isp->smk_inode = smk_of_current();
3306 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003307 case PIPEFS_MAGIC:
3308 isp->smk_inode = smk_of_current();
3309 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003310 case SOCKFS_MAGIC:
3311 /*
3312 * Socket access is controlled by the socket
3313 * structures associated with the task involved.
3314 */
3315 isp->smk_inode = &smack_known_star;
3316 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003317 default:
3318 isp->smk_inode = sbsp->smk_root;
3319 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003320 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003321 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003322 return;
Casey Schauflere97dcb02008-06-02 10:04:32 -07003323 }
3324
3325 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003326 * This is pretty hackish.
3327 * Casey says that we shouldn't have to do
3328 * file system specific code, but it does help
3329 * with keeping it simple.
3330 */
3331 switch (sbp->s_magic) {
3332 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003333 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003334 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003335 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003336 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003337 * that the smack file system doesn't do
3338 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003339 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003340 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003341 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003342 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003343 break;
3344 case DEVPTS_SUPER_MAGIC:
3345 /*
3346 * devpts seems content with the label of the task.
3347 * Programs that change smack have to treat the
3348 * pty with respect.
3349 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003350 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003351 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003352 case PROC_SUPER_MAGIC:
3353 /*
3354 * Casey says procfs appears not to care.
3355 * The superblock default suffices.
3356 */
3357 break;
3358 case TMPFS_MAGIC:
3359 /*
3360 * Device labels should come from the filesystem,
3361 * but watch out, because they're volitile,
3362 * getting recreated on every reboot.
3363 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003364 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003365 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003366 * If a smack value has been set we want to use it,
3367 * but since tmpfs isn't giving us the opportunity
3368 * to set mount options simulate setting the
3369 * superblock default.
3370 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003371 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003372 default:
3373 /*
3374 * This isn't an understood special case.
3375 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003376 */
3377
3378 /*
3379 * UNIX domain sockets use lower level socket data.
3380 */
3381 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003382 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003383 break;
3384 }
3385 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003386 * No xattr support means, alas, no SMACK label.
3387 * Use the aforeapplied default.
3388 * It would be curious if the label of the task
3389 * does not match that assigned.
3390 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003391 if (!(inode->i_opflags & IOP_XATTR))
3392 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003393 /*
3394 * Get the dentry for xattr.
3395 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003396 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003397 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003398 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003399 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003400
3401 /*
3402 * Transmuting directory
3403 */
3404 if (S_ISDIR(inode->i_mode)) {
3405 /*
3406 * If this is a new directory and the label was
3407 * transmuted when the inode was initialized
3408 * set the transmute attribute on the directory
3409 * and mark the inode.
3410 *
3411 * If there is a transmute attribute on the
3412 * directory mark the inode.
3413 */
3414 if (isp->smk_flags & SMK_INODE_CHANGED) {
3415 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003416 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003417 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003418 TRANS_TRUE, TRANS_TRUE_SIZE,
3419 0);
3420 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003421 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003422 XATTR_NAME_SMACKTRANSMUTE, trattr,
3423 TRANS_TRUE_SIZE);
3424 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3425 TRANS_TRUE_SIZE) != 0)
3426 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003427 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003428 if (rc >= 0)
3429 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003430 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003431 /*
3432 * Don't let the exec or mmap label be "*" or "@".
3433 */
3434 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3435 if (IS_ERR(skp) || skp == &smack_known_star ||
3436 skp == &smack_known_web)
3437 skp = NULL;
3438 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003439
Casey Schaufler19760ad2013-12-16 16:27:26 -08003440 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003441 if (IS_ERR(skp) || skp == &smack_known_star ||
3442 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003443 skp = NULL;
3444 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003445
Casey Schauflere114e472008-02-04 22:29:50 -08003446 dput(dp);
3447 break;
3448 }
3449
3450 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003451 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003452 else
3453 isp->smk_inode = final;
3454
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003455 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003456
Casey Schauflere114e472008-02-04 22:29:50 -08003457 return;
3458}
3459
3460/**
3461 * smack_getprocattr - Smack process attribute access
3462 * @p: the object task
3463 * @name: the name of the attribute in /proc/.../attr
3464 * @value: where to put the result
3465 *
3466 * Places a copy of the task Smack into value
3467 *
3468 * Returns the length of the smack label or an error code
3469 */
3470static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3471{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003472 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003473 char *cp;
3474 int slen;
3475
3476 if (strcmp(name, "current") != 0)
3477 return -EINVAL;
3478
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003479 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003480 if (cp == NULL)
3481 return -ENOMEM;
3482
3483 slen = strlen(cp);
3484 *value = cp;
3485 return slen;
3486}
3487
3488/**
3489 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003490 * @name: the name of the attribute in /proc/.../attr
3491 * @value: the value to set
3492 * @size: the size of the value
3493 *
3494 * Sets the Smack value of the task. Only setting self
3495 * is permitted and only with privilege
3496 *
3497 * Returns the length of the smack label or an error code
3498 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003499static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003500{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003501 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003502 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003503 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003504 struct smack_known_list_elem *sklep;
3505 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003506
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003507 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003508 return -EPERM;
3509
Casey Schauflerf7112e62012-05-06 15:22:02 -07003510 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003511 return -EINVAL;
3512
3513 if (strcmp(name, "current") != 0)
3514 return -EINVAL;
3515
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003516 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003517 if (IS_ERR(skp))
3518 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003519
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003520 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303521 * No process is ever allowed the web ("@") label
3522 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003523 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303524 if (skp == &smack_known_web || skp == &smack_known_star)
3525 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003526
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003527 if (!smack_privileged(CAP_MAC_ADMIN)) {
3528 rc = -EPERM;
3529 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3530 if (sklep->smk_label == skp) {
3531 rc = 0;
3532 break;
3533 }
3534 if (rc)
3535 return rc;
3536 }
3537
David Howellsd84f4f92008-11-14 10:39:23 +11003538 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003539 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003540 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003541
Casey Schauflerb17103a2018-11-09 16:12:56 -08003542 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003543 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003544 /*
3545 * process can change its label only once
3546 */
3547 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003548
David Howellsd84f4f92008-11-14 10:39:23 +11003549 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003550 return size;
3551}
3552
3553/**
3554 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003555 * @sock: one sock
3556 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003557 * @newsk: unused
3558 *
3559 * Return 0 if a subject with the smack of sock could access
3560 * an object with the smack of other, otherwise an error code
3561 */
David S. Miller3610cda2011-01-05 15:38:53 -08003562static int smack_unix_stream_connect(struct sock *sock,
3563 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003564{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003565 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003566 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003567 struct socket_smack *ssp = sock->sk_security;
3568 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003569 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003570 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003571 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003572#ifdef CONFIG_AUDIT
3573 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003574#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003575
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003576 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3577 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003578 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003579#ifdef CONFIG_AUDIT
3580 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3581 smk_ad_setfield_u_net_sk(&ad, other);
3582#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003583 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3584 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003585 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003586 okp = osp->smk_out;
3587 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003588 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003589 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003590 MAY_WRITE, rc);
3591 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003592 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003593
Casey Schaufler975d5e52011-09-26 14:43:39 -07003594 /*
3595 * Cross reference the peer labels for SO_PEERSEC.
3596 */
3597 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003598 nsp->smk_packet = ssp->smk_out;
3599 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003600 }
3601
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003602 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003603}
3604
3605/**
3606 * smack_unix_may_send - Smack access on UDS
3607 * @sock: one socket
3608 * @other: the other socket
3609 *
3610 * Return 0 if a subject with the smack of sock could access
3611 * an object with the smack of other, otherwise an error code
3612 */
3613static int smack_unix_may_send(struct socket *sock, struct socket *other)
3614{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003615 struct socket_smack *ssp = sock->sk->sk_security;
3616 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003617 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003618 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003619
Kees Cook923e9a12012-04-10 13:26:44 -07003620#ifdef CONFIG_AUDIT
3621 struct lsm_network_audit net;
3622
Eric Paris48c62af2012-04-02 13:15:44 -04003623 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003624 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003625#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003626
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003627 if (smack_privileged(CAP_MAC_OVERRIDE))
3628 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003629
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003630 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3631 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003632 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003633}
3634
3635/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003636 * smack_socket_sendmsg - Smack check based on destination host
3637 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003638 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003639 * @size: the size of the message
3640 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003641 * Return 0 if the current subject can write to the destination host.
3642 * For IPv4 this is only a question if the destination is a single label host.
3643 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003644 */
3645static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3646 int size)
3647{
3648 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003649#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003650 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003651#endif
3652#ifdef SMACK_IPV6_SECMARK_LABELING
3653 struct socket_smack *ssp = sock->sk->sk_security;
3654 struct smack_known *rsp;
3655#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003656 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003657
3658 /*
3659 * Perfectly reasonable for this to be NULL
3660 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003661 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003662 return 0;
3663
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003664 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003665 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003666 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3667 sip->sin_family != AF_INET)
3668 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003669 rc = smack_netlabel_send(sock->sk, sip);
3670 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003671#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003672 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003673 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3674 sap->sin6_family != AF_INET6)
3675 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003676#ifdef SMACK_IPV6_SECMARK_LABELING
3677 rsp = smack_ipv6host_label(sap);
3678 if (rsp != NULL)
3679 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3680 SMK_CONNECTING);
3681#endif
3682#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003683 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003684#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003685#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003686 break;
3687 }
3688 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003689}
3690
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003691/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003692 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003693 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003694 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003695 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003696 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003697 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003698static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3699 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003700{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003701 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003702 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003703 int acat;
3704 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003705
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003706 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003707 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003708 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003709 * If there are flags but no level netlabel isn't
3710 * behaving the way we expect it to.
3711 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003712 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003713 * Without guidance regarding the smack value
3714 * for the packet fall back on the network
3715 * ambient value.
3716 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003717 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303718 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003719 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003720 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003721 /*
3722 * Compare the catsets. Use the netlbl APIs.
3723 */
3724 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3725 if ((skp->smk_netlabel.flags &
3726 NETLBL_SECATTR_MLS_CAT) == 0)
3727 found = 1;
3728 break;
3729 }
3730 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003731 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3732 acat + 1);
3733 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003734 skp->smk_netlabel.attr.mls.cat,
3735 kcat + 1);
3736 if (acat < 0 || kcat < 0)
3737 break;
3738 }
3739 if (acat == kcat) {
3740 found = 1;
3741 break;
3742 }
Casey Schauflere114e472008-02-04 22:29:50 -08003743 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003744 rcu_read_unlock();
3745
3746 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003747 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003748
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003749 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003750 return &smack_known_web;
3751 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003752 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003753 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003754 /*
3755 * Looks like a fallback, which gives us a secid.
3756 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003757 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003758 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003759 * Without guidance regarding the smack value
3760 * for the packet fall back on the network
3761 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003762 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003763 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003764}
3765
Casey Schaufler69f287a2014-12-12 17:08:40 -08003766#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003767static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003768{
Casey Schauflerc6739442013-05-22 18:42:56 -07003769 u8 nexthdr;
3770 int offset;
3771 int proto = -EINVAL;
3772 struct ipv6hdr _ipv6h;
3773 struct ipv6hdr *ip6;
3774 __be16 frag_off;
3775 struct tcphdr _tcph, *th;
3776 struct udphdr _udph, *uh;
3777 struct dccp_hdr _dccph, *dh;
3778
3779 sip->sin6_port = 0;
3780
3781 offset = skb_network_offset(skb);
3782 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3783 if (ip6 == NULL)
3784 return -EINVAL;
3785 sip->sin6_addr = ip6->saddr;
3786
3787 nexthdr = ip6->nexthdr;
3788 offset += sizeof(_ipv6h);
3789 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3790 if (offset < 0)
3791 return -EINVAL;
3792
3793 proto = nexthdr;
3794 switch (proto) {
3795 case IPPROTO_TCP:
3796 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3797 if (th != NULL)
3798 sip->sin6_port = th->source;
3799 break;
3800 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003801 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003802 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3803 if (uh != NULL)
3804 sip->sin6_port = uh->source;
3805 break;
3806 case IPPROTO_DCCP:
3807 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3808 if (dh != NULL)
3809 sip->sin6_port = dh->dccph_sport;
3810 break;
3811 }
3812 return proto;
3813}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003814#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003815
Casey Schauflere114e472008-02-04 22:29:50 -08003816/**
3817 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3818 * @sk: socket
3819 * @skb: packet
3820 *
3821 * Returns 0 if the packet should be delivered, an error code otherwise
3822 */
3823static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3824{
3825 struct netlbl_lsm_secattr secattr;
3826 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003827 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003828 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003829 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003830 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003831#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003832 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003833#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003834#if IS_ENABLED(CONFIG_IPV6)
3835 struct sockaddr_in6 sadd;
3836 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003837
3838 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3839 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003840#endif /* CONFIG_IPV6 */
3841
Piotr Sawicki129a9982018-07-19 11:42:58 +02003842 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003843 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003844#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3845 /*
3846 * If there is a secmark use it rather than the CIPSO label.
3847 * If there is no secmark fall back to CIPSO.
3848 * The secmark is assumed to reflect policy better.
3849 */
3850 if (skb && skb->secmark != 0) {
3851 skp = smack_from_secid(skb->secmark);
3852 goto access_check;
3853 }
3854#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003855 /*
3856 * Translate what netlabel gave us.
3857 */
3858 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003859
Piotr Sawicki129a9982018-07-19 11:42:58 +02003860 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003861 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003862 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003863 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003864 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003865
Casey Schauflerc6739442013-05-22 18:42:56 -07003866 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003867
Casey Schaufler69f287a2014-12-12 17:08:40 -08003868#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3869access_check:
3870#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003871#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003872 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003873 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003874 ad.a.u.net->netif = skb->skb_iif;
3875 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003876#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003877 /*
3878 * Receiving a packet requires that the other end
3879 * be able to write here. Read access is not required.
3880 * This is the simplist possible security model
3881 * for networking.
3882 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003883 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3884 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003885 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003886 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003887 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003888 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003889#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003890 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003891 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003892 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3893 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003894 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003895#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003896 if (skb && skb->secmark != 0)
3897 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003898 else if (smk_ipv6_localhost(&sadd))
3899 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003900 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003901 skp = smack_ipv6host_label(&sadd);
3902 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003903 skp = smack_net_ambient;
Jia-Ju Bai3f4287e2019-07-23 18:00:15 +08003904 if (skb == NULL)
3905 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003906#ifdef CONFIG_AUDIT
3907 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003908 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003909 ad.a.u.net->netif = skb->skb_iif;
3910 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3911#endif /* CONFIG_AUDIT */
3912 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3913 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3914 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003915#endif /* SMACK_IPV6_SECMARK_LABELING */
3916#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003917 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003918#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003919 if (rc != 0)
3920 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3921 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003922 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003923#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003924 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003925
Paul Moorea8134292008-10-10 10:16:31 -04003926 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003927}
3928
3929/**
3930 * smack_socket_getpeersec_stream - pull in packet label
3931 * @sock: the socket
3932 * @optval: user's destination
3933 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003934 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003935 *
3936 * returns zero on success, an error code otherwise
3937 */
3938static int smack_socket_getpeersec_stream(struct socket *sock,
3939 char __user *optval,
3940 int __user *optlen, unsigned len)
3941{
3942 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003943 char *rcp = "";
3944 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003945 int rc = 0;
3946
3947 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003948 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003949 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003950 slen = strlen(rcp) + 1;
3951 }
Casey Schauflere114e472008-02-04 22:29:50 -08003952
3953 if (slen > len)
3954 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003955 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003956 rc = -EFAULT;
3957
3958 if (put_user(slen, optlen) != 0)
3959 rc = -EFAULT;
3960
3961 return rc;
3962}
3963
3964
3965/**
3966 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003967 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003968 * @skb: packet data
3969 * @secid: pointer to where to put the secid of the packet
3970 *
3971 * Sets the netlabel socket state on sk from parent
3972 */
3973static int smack_socket_getpeersec_dgram(struct socket *sock,
3974 struct sk_buff *skb, u32 *secid)
3975
3976{
3977 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003978 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003979 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003980 int family = PF_UNSPEC;
3981 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003982 int rc;
3983
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003984 if (skb != NULL) {
3985 if (skb->protocol == htons(ETH_P_IP))
3986 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003987#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003988 else if (skb->protocol == htons(ETH_P_IPV6))
3989 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003990#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003991 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003992 if (family == PF_UNSPEC && sock != NULL)
3993 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003994
Casey Schaufler69f287a2014-12-12 17:08:40 -08003995 switch (family) {
3996 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003997 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003998 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003999 break;
4000 case PF_INET:
4001#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4002 s = skb->secmark;
4003 if (s != 0)
4004 break;
4005#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004006 /*
4007 * Translate what netlabel gave us.
4008 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004009 if (sock != NULL && sock->sk != NULL)
4010 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004011 netlbl_secattr_init(&secattr);
4012 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4013 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004014 skp = smack_from_secattr(&secattr, ssp);
4015 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004016 }
4017 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004018 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004019 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004020#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004021 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004022#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004023 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004024 }
4025 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004026 if (s == 0)
4027 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004028 return 0;
4029}
4030
4031/**
Paul Moore07feee82009-03-27 17:10:54 -04004032 * smack_sock_graft - Initialize a newly created socket with an existing sock
4033 * @sk: child sock
4034 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004035 *
Paul Moore07feee82009-03-27 17:10:54 -04004036 * Set the smk_{in,out} state of an existing sock based on the process that
4037 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004038 */
4039static void smack_sock_graft(struct sock *sk, struct socket *parent)
4040{
4041 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004042 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004043
Paul Moore07feee82009-03-27 17:10:54 -04004044 if (sk == NULL ||
4045 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004046 return;
4047
4048 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004049 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004050 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004051 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004052}
4053
4054/**
4055 * smack_inet_conn_request - Smack access check on connect
4056 * @sk: socket involved
4057 * @skb: packet
4058 * @req: unused
4059 *
4060 * Returns 0 if a task with the packet label could write to
4061 * the socket, otherwise an error code
4062 */
4063static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4064 struct request_sock *req)
4065{
Paul Moore07feee82009-03-27 17:10:54 -04004066 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004067 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004068 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004069 struct netlbl_lsm_secattr secattr;
4070 struct sockaddr_in addr;
4071 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004072 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004073 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004074 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004075#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004076 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004077#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004078
Casey Schaufler69f287a2014-12-12 17:08:40 -08004079#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004080 if (family == PF_INET6) {
4081 /*
4082 * Handle mapped IPv4 packets arriving
4083 * via IPv6 sockets. Don't set up netlabel
4084 * processing on IPv6.
4085 */
4086 if (skb->protocol == htons(ETH_P_IP))
4087 family = PF_INET;
4088 else
4089 return 0;
4090 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004091#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004092
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004093#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4094 /*
4095 * If there is a secmark use it rather than the CIPSO label.
4096 * If there is no secmark fall back to CIPSO.
4097 * The secmark is assumed to reflect policy better.
4098 */
4099 if (skb && skb->secmark != 0) {
4100 skp = smack_from_secid(skb->secmark);
4101 goto access_check;
4102 }
4103#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4104
Paul Moore07feee82009-03-27 17:10:54 -04004105 netlbl_secattr_init(&secattr);
4106 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004107 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004108 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004109 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004110 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004111 netlbl_secattr_destroy(&secattr);
4112
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004113#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4114access_check:
4115#endif
4116
Etienne Bassetecfcc532009-04-08 20:40:06 +02004117#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004118 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4119 ad.a.u.net->family = family;
4120 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004121 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4122#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004123 /*
Paul Moore07feee82009-03-27 17:10:54 -04004124 * Receiving a packet requires that the other end be able to write
4125 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004126 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004127 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4128 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004129 if (rc != 0)
4130 return rc;
4131
4132 /*
4133 * Save the peer's label in the request_sock so we can later setup
4134 * smk_packet in the child socket so that SO_PEERCRED can report it.
4135 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004136 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004137
4138 /*
4139 * We need to decide if we want to label the incoming connection here
4140 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004141 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004142 */
4143 hdr = ip_hdr(skb);
4144 addr.sin_addr.s_addr = hdr->saddr;
4145 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004146 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004147 rcu_read_unlock();
4148
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004149 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004150 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004151 else
Paul Moore07feee82009-03-27 17:10:54 -04004152 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004153
4154 return rc;
4155}
4156
Paul Moore07feee82009-03-27 17:10:54 -04004157/**
4158 * smack_inet_csk_clone - Copy the connection information to the new socket
4159 * @sk: the new socket
4160 * @req: the connection's request_sock
4161 *
4162 * Transfer the connection's peer label to the newly created socket.
4163 */
4164static void smack_inet_csk_clone(struct sock *sk,
4165 const struct request_sock *req)
4166{
4167 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004168 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004169
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004170 if (req->peer_secid != 0) {
4171 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004172 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004173 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004174 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004175}
4176
Casey Schauflere114e472008-02-04 22:29:50 -08004177/*
4178 * Key management security hooks
4179 *
4180 * Casey has not tested key support very heavily.
4181 * The permission check is most likely too restrictive.
4182 * If you care about keys please have a look.
4183 */
4184#ifdef CONFIG_KEYS
4185
4186/**
4187 * smack_key_alloc - Set the key security blob
4188 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004189 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004190 * @flags: unused
4191 *
4192 * No allocation required
4193 *
4194 * Returns 0
4195 */
David Howellsd84f4f92008-11-14 10:39:23 +11004196static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004197 unsigned long flags)
4198{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004199 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004200
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004201 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004202 return 0;
4203}
4204
4205/**
4206 * smack_key_free - Clear the key security blob
4207 * @key: the object
4208 *
4209 * Clear the blob pointer
4210 */
4211static void smack_key_free(struct key *key)
4212{
4213 key->security = NULL;
4214}
4215
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004216/**
Casey Schauflere114e472008-02-04 22:29:50 -08004217 * smack_key_permission - Smack access on a key
4218 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004219 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004220 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004221 *
4222 * Return 0 if the task has read and write to the object,
4223 * an error code otherwise
4224 */
4225static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004226 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004227{
4228 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004229 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004230 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004231 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004232 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004233
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004234 /*
4235 * Validate requested permissions
4236 */
4237 if (perm & ~KEY_NEED_ALL)
4238 return -EINVAL;
4239
Casey Schauflere114e472008-02-04 22:29:50 -08004240 keyp = key_ref_to_ptr(key_ref);
4241 if (keyp == NULL)
4242 return -EINVAL;
4243 /*
4244 * If the key hasn't been initialized give it access so that
4245 * it may do so.
4246 */
4247 if (keyp->security == NULL)
4248 return 0;
4249 /*
4250 * This should not occur
4251 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004252 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004253 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004254
4255 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4256 return 0;
4257
Etienne Bassetecfcc532009-04-08 20:40:06 +02004258#ifdef CONFIG_AUDIT
4259 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4260 ad.a.u.key_struct.key = keyp->serial;
4261 ad.a.u.key_struct.key_desc = keyp->description;
4262#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004263 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4264 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004265 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004266 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004267 rc = smk_access(tkp, keyp->security, request, &ad);
4268 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4269 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004270}
José Bollo7fc5f362015-02-17 15:41:22 +01004271
4272/*
4273 * smack_key_getsecurity - Smack label tagging the key
4274 * @key points to the key to be queried
4275 * @_buffer points to a pointer that should be set to point to the
4276 * resulting string (if no label or an error occurs).
4277 * Return the length of the string (including terminating NUL) or -ve if
4278 * an error.
4279 * May also return 0 (and a NULL buffer pointer) if there is no label.
4280 */
4281static int smack_key_getsecurity(struct key *key, char **_buffer)
4282{
4283 struct smack_known *skp = key->security;
4284 size_t length;
4285 char *copy;
4286
4287 if (key->security == NULL) {
4288 *_buffer = NULL;
4289 return 0;
4290 }
4291
4292 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4293 if (copy == NULL)
4294 return -ENOMEM;
4295 length = strlen(copy) + 1;
4296
4297 *_buffer = copy;
4298 return length;
4299}
4300
Casey Schauflere114e472008-02-04 22:29:50 -08004301#endif /* CONFIG_KEYS */
4302
4303/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004304 * Smack Audit hooks
4305 *
4306 * Audit requires a unique representation of each Smack specific
4307 * rule. This unique representation is used to distinguish the
4308 * object to be audited from remaining kernel objects and also
4309 * works as a glue between the audit hooks.
4310 *
4311 * Since repository entries are added but never deleted, we'll use
4312 * the smack_known label address related to the given audit rule as
4313 * the needed unique representation. This also better fits the smack
4314 * model where nearly everything is a label.
4315 */
4316#ifdef CONFIG_AUDIT
4317
4318/**
4319 * smack_audit_rule_init - Initialize a smack audit rule
4320 * @field: audit rule fields given from user-space (audit.h)
4321 * @op: required testing operator (=, !=, >, <, ...)
4322 * @rulestr: smack label to be audited
4323 * @vrule: pointer to save our own audit rule representation
4324 *
4325 * Prepare to audit cases where (@field @op @rulestr) is true.
4326 * The label to be audited is created if necessay.
4327 */
4328static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4329{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004330 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004331 char **rule = (char **)vrule;
4332 *rule = NULL;
4333
4334 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4335 return -EINVAL;
4336
Al Viro5af75d82008-12-16 05:59:26 -05004337 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004338 return -EINVAL;
4339
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004340 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004341 if (IS_ERR(skp))
4342 return PTR_ERR(skp);
4343
4344 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004345
4346 return 0;
4347}
4348
4349/**
4350 * smack_audit_rule_known - Distinguish Smack audit rules
4351 * @krule: rule of interest, in Audit kernel representation format
4352 *
4353 * This is used to filter Smack rules from remaining Audit ones.
4354 * If it's proved that this rule belongs to us, the
4355 * audit_rule_match hook will be called to do the final judgement.
4356 */
4357static int smack_audit_rule_known(struct audit_krule *krule)
4358{
4359 struct audit_field *f;
4360 int i;
4361
4362 for (i = 0; i < krule->field_count; i++) {
4363 f = &krule->fields[i];
4364
4365 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4366 return 1;
4367 }
4368
4369 return 0;
4370}
4371
4372/**
4373 * smack_audit_rule_match - Audit given object ?
4374 * @secid: security id for identifying the object to test
4375 * @field: audit rule flags given from user-space
4376 * @op: required testing operator
4377 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004378 *
4379 * The core Audit hook. It's used to take the decision of
4380 * whether to audit or not to audit a given object.
4381 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004382static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004383{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004384 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004385 char *rule = vrule;
4386
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004387 if (unlikely(!rule)) {
4388 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004389 return -ENOENT;
4390 }
4391
4392 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4393 return 0;
4394
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004395 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004396
4397 /*
4398 * No need to do string comparisons. If a match occurs,
4399 * both pointers will point to the same smack_known
4400 * label.
4401 */
Al Viro5af75d82008-12-16 05:59:26 -05004402 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004403 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004404 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004405 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004406
4407 return 0;
4408}
4409
Casey Schaufler491a0b02016-01-26 15:08:35 -08004410/*
4411 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004412 * No memory was allocated.
4413 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004414
4415#endif /* CONFIG_AUDIT */
4416
Randy Dunlap251a2a92009-02-18 11:42:33 -08004417/**
David Quigley746df9b2013-05-22 12:50:35 -04004418 * smack_ismaclabel - check if xattr @name references a smack MAC label
4419 * @name: Full xattr name to check.
4420 */
4421static int smack_ismaclabel(const char *name)
4422{
4423 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4424}
4425
4426
4427/**
Casey Schauflere114e472008-02-04 22:29:50 -08004428 * smack_secid_to_secctx - return the smack label for a secid
4429 * @secid: incoming integer
4430 * @secdata: destination
4431 * @seclen: how long it is
4432 *
4433 * Exists for networking code.
4434 */
4435static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4436{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004437 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004438
Eric Parisd5630b92010-10-13 16:24:48 -04004439 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004440 *secdata = skp->smk_known;
4441 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004442 return 0;
4443}
4444
Randy Dunlap251a2a92009-02-18 11:42:33 -08004445/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004446 * smack_secctx_to_secid - return the secid for a smack label
4447 * @secdata: smack label
4448 * @seclen: how long result is
4449 * @secid: outgoing integer
4450 *
4451 * Exists for audit and networking code.
4452 */
David Howellse52c17642008-04-29 20:52:51 +01004453static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004454{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004455 struct smack_known *skp = smk_find_entry(secdata);
4456
4457 if (skp)
4458 *secid = skp->smk_secid;
4459 else
4460 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004461 return 0;
4462}
4463
Casey Schaufler491a0b02016-01-26 15:08:35 -08004464/*
4465 * There used to be a smack_release_secctx hook
4466 * that did nothing back when hooks were in a vector.
4467 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004468 */
Casey Schauflere114e472008-02-04 22:29:50 -08004469
David P. Quigley1ee65e32009-09-03 14:25:57 -04004470static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4471{
4472 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4473}
4474
4475static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4476{
4477 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4478}
4479
4480static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4481{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004482 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004483
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004484 *ctx = skp->smk_known;
4485 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004486 return 0;
4487}
4488
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004489static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4490{
4491
4492 struct task_smack *tsp;
4493 struct smack_known *skp;
4494 struct inode_smack *isp;
4495 struct cred *new_creds = *new;
4496
4497 if (new_creds == NULL) {
4498 new_creds = prepare_creds();
4499 if (new_creds == NULL)
4500 return -ENOMEM;
4501 }
4502
Casey Schauflerb17103a2018-11-09 16:12:56 -08004503 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004504
4505 /*
4506 * Get label from overlay inode and set it in create_sid
4507 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004508 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004509 skp = isp->smk_inode;
4510 tsp->smk_task = skp;
4511 *new = new_creds;
4512 return 0;
4513}
4514
4515static int smack_inode_copy_up_xattr(const char *name)
4516{
4517 /*
4518 * Return 1 if this is the smack access Smack attribute.
4519 */
4520 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4521 return 1;
4522
4523 return -EOPNOTSUPP;
4524}
4525
4526static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4527 struct qstr *name,
4528 const struct cred *old,
4529 struct cred *new)
4530{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004531 struct task_smack *otsp = smack_cred(old);
4532 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004533 struct inode_smack *isp;
4534 int may;
4535
4536 /*
4537 * Use the process credential unless all of
4538 * the transmuting criteria are met
4539 */
4540 ntsp->smk_task = otsp->smk_task;
4541
4542 /*
4543 * the attribute of the containing directory
4544 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004545 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004546
4547 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4548 rcu_read_lock();
4549 may = smk_access_entry(otsp->smk_task->smk_known,
4550 isp->smk_inode->smk_known,
4551 &otsp->smk_task->smk_rules);
4552 rcu_read_unlock();
4553
4554 /*
4555 * If the directory is transmuting and the rule
4556 * providing access is transmuting use the containing
4557 * directory label instead of the process label.
4558 */
4559 if (may > 0 && (may & MAY_TRANSMUTE))
4560 ntsp->smk_task = isp->smk_inode;
4561 }
4562 return 0;
4563}
4564
Casey Schauflerbbd36622018-11-12 09:30:56 -08004565struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4566 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004567 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004568 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004569 .lbs_ipc = sizeof(struct smack_known *),
4570 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004571};
4572
James Morrisca97d932017-02-15 00:18:51 +11004573static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004574 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4575 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4576 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004577
Al Viro0b520752018-12-23 16:02:47 -05004578 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004579 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4580
Casey Schauflere20b0432015-05-02 15:11:36 -07004581 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4582 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004583 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004584 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004585 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304586 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004587
Casey Schauflere20b0432015-05-02 15:11:36 -07004588 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004589
Casey Schauflere20b0432015-05-02 15:11:36 -07004590 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004591 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4592 LSM_HOOK_INIT(inode_link, smack_inode_link),
4593 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4594 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4595 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4596 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4597 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4598 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4599 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4600 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4601 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4602 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4603 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4604 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4605 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4606 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004607
Casey Schauflere20b0432015-05-02 15:11:36 -07004608 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004609 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4610 LSM_HOOK_INIT(file_lock, smack_file_lock),
4611 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4612 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4613 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4614 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4615 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4616 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004617
Casey Schauflere20b0432015-05-02 15:11:36 -07004618 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004619
Casey Schauflere20b0432015-05-02 15:11:36 -07004620 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4621 LSM_HOOK_INIT(cred_free, smack_cred_free),
4622 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4623 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004624 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004625 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4626 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4627 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4628 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4629 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4630 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4631 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4632 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4633 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4634 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4635 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4636 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4637 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004638 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004639
Casey Schauflere20b0432015-05-02 15:11:36 -07004640 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4641 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004642
Casey Schauflere20b0432015-05-02 15:11:36 -07004643 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004644
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004645 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004646 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4647 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4648 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4649 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004650
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004651 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004652 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4653 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4654 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004655
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004656 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004657 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4658 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4659 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004660
Casey Schauflere20b0432015-05-02 15:11:36 -07004661 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004662
Casey Schauflere20b0432015-05-02 15:11:36 -07004663 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4664 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004665
Casey Schauflere20b0432015-05-02 15:11:36 -07004666 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4667 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004668
Casey Schauflere20b0432015-05-02 15:11:36 -07004669 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004670 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004671#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004672 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004673#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004674 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4675 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4676 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4677 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4678 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4679 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4680 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4681 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4682 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4683 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004684
Casey Schauflere114e472008-02-04 22:29:50 -08004685 /* key management security hooks */
4686#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004687 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4688 LSM_HOOK_INIT(key_free, smack_key_free),
4689 LSM_HOOK_INIT(key_permission, smack_key_permission),
4690 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004691#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004692
4693 /* Audit hooks */
4694#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004695 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4696 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4697 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004698#endif /* CONFIG_AUDIT */
4699
Casey Schauflere20b0432015-05-02 15:11:36 -07004700 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4701 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4702 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004703 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4704 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4705 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004706 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4707 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4708 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004709};
4710
Etienne Basset7198e2e2009-03-24 20:53:24 +01004711
Casey Schaufler86812bb2012-04-17 18:55:46 -07004712static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004713{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004714 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004715 * Initialize rule list locks
4716 */
4717 mutex_init(&smack_known_huh.smk_rules_lock);
4718 mutex_init(&smack_known_hat.smk_rules_lock);
4719 mutex_init(&smack_known_floor.smk_rules_lock);
4720 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004721 mutex_init(&smack_known_web.smk_rules_lock);
4722 /*
4723 * Initialize rule lists
4724 */
4725 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4726 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4727 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4728 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004729 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4730 /*
4731 * Create the known labels list
4732 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004733 smk_insert_entry(&smack_known_huh);
4734 smk_insert_entry(&smack_known_hat);
4735 smk_insert_entry(&smack_known_star);
4736 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004737 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004738}
4739
Casey Schauflere114e472008-02-04 22:29:50 -08004740/**
4741 * smack_init - initialize the smack system
4742 *
luanshia1a07f22019-07-05 10:35:20 +08004743 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004744 */
4745static __init int smack_init(void)
4746{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004747 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004748 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004749
Rohit1a5b4722014-10-15 17:40:41 +05304750 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4751 if (!smack_inode_cache)
4752 return -ENOMEM;
4753
Casey Schaufler4e328b02019-04-02 11:37:12 -07004754 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4755 if (!smack_rule_cache) {
4756 kmem_cache_destroy(smack_inode_cache);
4757 return -ENOMEM;
4758 }
4759
Casey Schauflerbbd36622018-11-12 09:30:56 -08004760 /*
4761 * Set the security state for the initial task.
4762 */
4763 tsp = smack_cred(cred);
4764 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4765
4766 /*
4767 * Register with LSM
4768 */
4769 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004770 smack_enabled = 1;
4771
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004772 pr_info("Smack: Initializing.\n");
4773#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4774 pr_info("Smack: Netfilter enabled.\n");
4775#endif
4776#ifdef SMACK_IPV6_PORT_LABELING
4777 pr_info("Smack: IPv6 port labeling enabled.\n");
4778#endif
4779#ifdef SMACK_IPV6_SECMARK_LABELING
4780 pr_info("Smack: IPv6 Netfilter enabled.\n");
4781#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004782
Casey Schaufler86812bb2012-04-17 18:55:46 -07004783 /* initialize the smack_known_list */
4784 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004785
Casey Schauflere114e472008-02-04 22:29:50 -08004786 return 0;
4787}
4788
4789/*
4790 * Smack requires early initialization in order to label
4791 * all processes and objects when they are created.
4792 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004793DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004794 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004795 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004796 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004797 .init = smack_init,
4798};