blob: 746e5743acccb8b3aa3d909018497af577568d6b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Casey Schauflere114e472008-02-04 22:29:50 -08002/*
3 * Simplified MAC Kernel (smack) security module
4 *
5 * This file contains the smack hook function implementations.
6 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02007 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08008 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03009 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -080010 *
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040012 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000013 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020014 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030015 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080016 */
17
18#include <linux/xattr.h>
19#include <linux/pagemap.h>
20#include <linux/mount.h>
21#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080022#include <linux/kd.h>
23#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040024#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/tcp.h>
26#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070027#include <linux/dccp.h>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020028#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030#include <linux/mutex.h>
Casey Schauflere114e472008-02-04 22:29:50 -080031#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070032#include <net/ip.h>
33#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100034#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070035#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050036#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030037#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000038#include <linux/msg.h>
39#include <linux/shm.h>
40#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053041#include <linux/parser.h>
David Howells2febd252018-11-01 23:07:24 +000042#include <linux/fs_context.h>
43#include <linux/fs_parser.h>
David Howellsa8478a62020-01-14 17:07:13 +000044#include <linux/watch_queue.h>
Casey Schauflere114e472008-02-04 22:29:50 -080045#include "smack.h"
46
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020047#define TRANS_TRUE "TRUE"
48#define TRANS_TRUE_SIZE 4
49
Casey Schauflerc6739442013-05-22 18:42:56 -070050#define SMK_CONNECTING 0
51#define SMK_RECEIVING 1
52#define SMK_SENDING 2
53
Arnd Bergmann00720f02020-04-08 21:04:31 +020054static DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080055static LIST_HEAD(smk_ipv6_port_list);
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/**
Eric W. Biedermanb8bff592020-03-22 15:46:24 -0500891 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
Casey Schauflerce8a4322011-09-29 18:21:01 -0700892 * @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 */
Eric W. Biedermanb8bff592020-03-22 15:46:24 -0500896static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
Casey Schaufler676dac42010-12-02 06:43:39 -0800897{
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
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800904 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300905 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800906 return 0;
907
Seth Forshee809c02e2016-04-26 14:36:22 -0500908 sbsp = inode->i_sb->s_security;
909 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
910 isp->smk_task != sbsp->smk_root)
911 return 0;
912
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300913 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100914 struct task_struct *tracer;
915 rc = 0;
916
917 rcu_read_lock();
918 tracer = ptrace_parent(current);
919 if (likely(tracer != NULL))
920 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200921 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100922 PTRACE_MODE_ATTACH,
923 __func__);
924 rcu_read_unlock();
925
926 if (rc != 0)
927 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200928 }
929 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300930 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800931
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300932 bsp->smk_task = isp->smk_task;
933 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800934
Kees Cookccbb6e12017-07-18 15:25:26 -0700935 /* Decide if this is a secure exec. */
936 if (bsp->smk_task != bsp->smk_forked)
937 bprm->secureexec = 1;
938
Casey Schaufler676dac42010-12-02 06:43:39 -0800939 return 0;
940}
941
942/*
Casey Schauflere114e472008-02-04 22:29:50 -0800943 * Inode hooks
944 */
945
946/**
947 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800948 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800949 *
luanshia1a07f22019-07-05 10:35:20 +0800950 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800951 */
952static int smack_inode_alloc_security(struct inode *inode)
953{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700954 struct smack_known *skp = smk_of_current();
955
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700956 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800957 return 0;
958}
959
960/**
Casey Schauflere114e472008-02-04 22:29:50 -0800961 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200962 * @inode: the newly created inode
963 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500964 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800965 * @name: where to put the attribute name
966 * @value: where to put the attribute value
967 * @len: where to put the length of the attribute
968 *
969 * Returns 0 if it all works out, -ENOMEM if there's no memory
970 */
971static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900972 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500973 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800974{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800975 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700976 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200977 struct smack_known *isp = smk_of_inode(inode);
978 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800979 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800980
Tetsuo Handa95489062013-07-25 05:44:02 +0900981 if (name)
982 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800983
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100984 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800985 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200986 may = smk_access_entry(skp->smk_known, dsp->smk_known,
987 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800988 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200989
990 /*
991 * If the access rule allows transmutation and
992 * the directory requests transmutation then
993 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700994 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200995 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800996 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700997 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200998 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700999 issp->smk_flags |= SMK_INODE_CHANGED;
1000 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001001
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001002 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001003 if (*value == NULL)
1004 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001005
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001006 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001007 }
Casey Schauflere114e472008-02-04 22:29:50 -08001008
1009 return 0;
1010}
1011
1012/**
1013 * smack_inode_link - Smack check on link
1014 * @old_dentry: the existing object
1015 * @dir: unused
1016 * @new_dentry: the new object
1017 *
1018 * Returns 0 if access is permitted, an error code otherwise
1019 */
1020static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1021 struct dentry *new_dentry)
1022{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001023 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001024 struct smk_audit_info ad;
1025 int rc;
1026
Eric Parisa2694342011-04-25 13:10:27 -04001027 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001028 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001029
David Howellsc6f493d2015-03-17 22:26:22 +00001030 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001031 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001032 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001033
David Howells88025652015-01-29 12:02:32 +00001034 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001035 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001036 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1037 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001038 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001039 }
1040
1041 return rc;
1042}
1043
1044/**
1045 * smack_inode_unlink - Smack check on inode deletion
1046 * @dir: containing directory object
1047 * @dentry: file to unlink
1048 *
1049 * Returns 0 if current can write the containing directory
1050 * and the object, error code otherwise
1051 */
1052static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1053{
David Howellsc6f493d2015-03-17 22:26:22 +00001054 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001055 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001056 int rc;
1057
Eric Parisa2694342011-04-25 13:10:27 -04001058 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001059 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1060
Casey Schauflere114e472008-02-04 22:29:50 -08001061 /*
1062 * You need write access to the thing you're unlinking
1063 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001064 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001065 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001066 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001067 /*
1068 * You also need write access to the containing directory
1069 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001070 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001071 smk_ad_setfield_u_fs_inode(&ad, dir);
1072 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001073 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001074 }
Casey Schauflere114e472008-02-04 22:29:50 -08001075 return rc;
1076}
1077
1078/**
1079 * smack_inode_rmdir - Smack check on directory deletion
1080 * @dir: containing directory object
1081 * @dentry: directory to unlink
1082 *
1083 * Returns 0 if current can write the containing directory
1084 * and the directory, error code otherwise
1085 */
1086static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1087{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001088 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001089 int rc;
1090
Eric Parisa2694342011-04-25 13:10:27 -04001091 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001092 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1093
Casey Schauflere114e472008-02-04 22:29:50 -08001094 /*
1095 * You need write access to the thing you're removing
1096 */
David Howellsc6f493d2015-03-17 22:26:22 +00001097 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1098 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001099 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001100 /*
1101 * You also need write access to the containing directory
1102 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001103 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001104 smk_ad_setfield_u_fs_inode(&ad, dir);
1105 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001106 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001107 }
Casey Schauflere114e472008-02-04 22:29:50 -08001108
1109 return rc;
1110}
1111
1112/**
1113 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001114 * @old_inode: unused
1115 * @old_dentry: the old object
1116 * @new_inode: unused
1117 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001118 *
1119 * Read and write access is required on both the old and
1120 * new directories.
1121 *
1122 * Returns 0 if access is permitted, an error code otherwise
1123 */
1124static int smack_inode_rename(struct inode *old_inode,
1125 struct dentry *old_dentry,
1126 struct inode *new_inode,
1127 struct dentry *new_dentry)
1128{
1129 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001130 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001131 struct smk_audit_info ad;
1132
Eric Parisa2694342011-04-25 13:10:27 -04001133 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001134 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001135
David Howellsc6f493d2015-03-17 22:26:22 +00001136 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001137 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001138 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001139
David Howells88025652015-01-29 12:02:32 +00001140 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001141 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001142 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1143 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001144 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001145 }
Casey Schauflere114e472008-02-04 22:29:50 -08001146 return rc;
1147}
1148
1149/**
1150 * smack_inode_permission - Smack version of permission()
1151 * @inode: the inode in question
1152 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001153 *
1154 * This is the important Smack hook.
1155 *
luanshia1a07f22019-07-05 10:35:20 +08001156 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001157 */
Al Viroe74f71e2011-06-20 19:38:15 -04001158static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001159{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001160 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001161 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001162 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001163 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001164
1165 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001166 /*
1167 * No permission to check. Existence test. Yup, it's there.
1168 */
1169 if (mask == 0)
1170 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001171
Seth Forshee9f50eda2015-09-23 15:16:06 -05001172 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1173 if (smk_of_inode(inode) != sbsp->smk_root)
1174 return -EACCES;
1175 }
1176
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001177 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001178 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001179 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001180 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001181 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001182 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1183 rc = smk_bu_inode(inode, mask, rc);
1184 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001185}
1186
1187/**
1188 * smack_inode_setattr - Smack check for setting attributes
1189 * @dentry: the object
1190 * @iattr: for the force flag
1191 *
1192 * Returns 0 if access is permitted, an error code otherwise
1193 */
1194static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1195{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001196 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001197 int rc;
1198
Casey Schauflere114e472008-02-04 22:29:50 -08001199 /*
1200 * Need to allow for clearing the setuid bit.
1201 */
1202 if (iattr->ia_valid & ATTR_FORCE)
1203 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001204 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001205 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001206
David Howellsc6f493d2015-03-17 22:26:22 +00001207 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1208 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001209 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001210}
1211
1212/**
1213 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001214 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001215 *
1216 * Returns 0 if access is permitted, an error code otherwise
1217 */
Al Viro3f7036a2015-03-08 19:28:30 -04001218static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001219{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001220 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001221 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001222 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001223
Eric Parisf48b7392011-04-25 12:54:27 -04001224 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001225 smk_ad_setfield_u_fs_path(&ad, *path);
1226 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1227 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001228 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001229}
1230
1231/**
1232 * smack_inode_setxattr - Smack check for setting xattrs
1233 * @dentry: the object
1234 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001235 * @value: value of the attribute
1236 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001237 * @flags: unused
1238 *
1239 * This protects the Smack attribute explicitly.
1240 *
1241 * Returns 0 if access is permitted, an error code otherwise
1242 */
David Howells8f0cfa52008-04-29 00:59:41 -07001243static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1244 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001245{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001246 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001247 struct smack_known *skp;
1248 int check_priv = 0;
1249 int check_import = 0;
1250 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001251 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001252
Casey Schaufler19760ad2013-12-16 16:27:26 -08001253 /*
1254 * Check label validity here so import won't fail in post_setxattr
1255 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001256 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1257 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001258 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1259 check_priv = 1;
1260 check_import = 1;
1261 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1262 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1263 check_priv = 1;
1264 check_import = 1;
1265 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001266 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001267 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001268 if (size != TRANS_TRUE_SIZE ||
1269 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1270 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001271 } else
1272 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1273
Casey Schaufler19760ad2013-12-16 16:27:26 -08001274 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1275 rc = -EPERM;
1276
1277 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001278 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001279 if (IS_ERR(skp))
1280 rc = PTR_ERR(skp);
1281 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001282 (skp == &smack_known_star || skp == &smack_known_web)))
1283 rc = -EINVAL;
1284 }
1285
Eric Parisa2694342011-04-25 13:10:27 -04001286 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001287 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1288
Casey Schauflerd166c802014-08-27 14:51:27 -07001289 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001290 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1291 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001292 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001293
1294 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001295}
1296
1297/**
1298 * smack_inode_post_setxattr - Apply the Smack update approved above
1299 * @dentry: object
1300 * @name: attribute name
1301 * @value: attribute value
1302 * @size: attribute size
1303 * @flags: unused
1304 *
1305 * Set the pointer in the inode blob to the entry found
1306 * in the master label list.
1307 */
David Howells8f0cfa52008-04-29 00:59:41 -07001308static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1309 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001310{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001311 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001312 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001313
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001314 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1315 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1316 return;
1317 }
1318
Casey Schaufler676dac42010-12-02 06:43:39 -08001319 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001320 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001321 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001322 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001323 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001324 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001325 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001326 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001327 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001328 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001329 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001330 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001331 }
Casey Schauflere114e472008-02-04 22:29:50 -08001332
1333 return;
1334}
1335
Casey Schauflerce8a4322011-09-29 18:21:01 -07001336/**
Casey Schauflere114e472008-02-04 22:29:50 -08001337 * smack_inode_getxattr - Smack check on getxattr
1338 * @dentry: the object
1339 * @name: unused
1340 *
1341 * Returns 0 if access is permitted, an error code otherwise
1342 */
David Howells8f0cfa52008-04-29 00:59:41 -07001343static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001344{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001345 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001346 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001347
Eric Parisa2694342011-04-25 13:10:27 -04001348 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001349 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1350
David Howellsc6f493d2015-03-17 22:26:22 +00001351 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1352 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001353 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001354}
1355
Casey Schauflerce8a4322011-09-29 18:21:01 -07001356/**
Casey Schauflere114e472008-02-04 22:29:50 -08001357 * smack_inode_removexattr - Smack check on removexattr
1358 * @dentry: the object
1359 * @name: name of the attribute
1360 *
1361 * Removing the Smack attribute requires CAP_MAC_ADMIN
1362 *
1363 * Returns 0 if access is permitted, an error code otherwise
1364 */
David Howells8f0cfa52008-04-29 00:59:41 -07001365static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001366{
Casey Schaufler676dac42010-12-02 06:43:39 -08001367 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001368 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001369 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001370
Casey Schauflerbcdca222008-02-23 15:24:04 -08001371 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1372 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001373 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001374 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001375 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301376 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001377 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001378 rc = -EPERM;
1379 } else
1380 rc = cap_inode_removexattr(dentry, name);
1381
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001382 if (rc != 0)
1383 return rc;
1384
Eric Parisa2694342011-04-25 13:10:27 -04001385 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001386 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001387
David Howellsc6f493d2015-03-17 22:26:22 +00001388 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1389 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001390 if (rc != 0)
1391 return rc;
1392
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001393 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001394 /*
1395 * Don't do anything special for these.
1396 * XATTR_NAME_SMACKIPIN
1397 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001398 */
José Bollo80124952016-01-12 21:23:40 +01001399 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001400 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001401 struct superblock_smack *sbsp = sbp->s_security;
1402
1403 isp->smk_inode = sbsp->smk_default;
1404 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001405 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001406 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001407 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001408 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1409 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001410
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001411 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001412}
1413
1414/**
1415 * smack_inode_getsecurity - get smack xattrs
1416 * @inode: the object
1417 * @name: attribute name
1418 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001419 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001420 *
1421 * Returns the size of the attribute or an error code
1422 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001423static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001424 const char *name, void **buffer,
1425 bool alloc)
1426{
1427 struct socket_smack *ssp;
1428 struct socket *sock;
1429 struct super_block *sbp;
1430 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001431 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001432
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001433 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001434 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001435 else {
1436 /*
1437 * The rest of the Smack xattrs are only on sockets.
1438 */
1439 sbp = ip->i_sb;
1440 if (sbp->s_magic != SOCKFS_MAGIC)
1441 return -EOPNOTSUPP;
1442
1443 sock = SOCKET_I(ip);
1444 if (sock == NULL || sock->sk == NULL)
1445 return -EOPNOTSUPP;
1446
1447 ssp = sock->sk->sk_security;
1448
1449 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1450 isp = ssp->smk_in;
1451 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1452 isp = ssp->smk_out;
1453 else
1454 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001455 }
1456
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001457 if (alloc) {
1458 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1459 if (*buffer == NULL)
1460 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001461 }
1462
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001463 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001464}
1465
1466
1467/**
1468 * smack_inode_listsecurity - list the Smack attributes
1469 * @inode: the object
1470 * @buffer: where they go
1471 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001472 */
1473static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1474 size_t buffer_size)
1475{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001476 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001477
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001478 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001479 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001480
1481 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001482}
1483
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001484/**
1485 * smack_inode_getsecid - Extract inode's security id
1486 * @inode: inode to extract the info from
1487 * @secid: where result will be saved
1488 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001489static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001490{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001491 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001492
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001493 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001494}
1495
Casey Schauflere114e472008-02-04 22:29:50 -08001496/*
1497 * File Hooks
1498 */
1499
Casey Schaufler491a0b02016-01-26 15:08:35 -08001500/*
1501 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001502 *
1503 * Should access checks be done on each read or write?
1504 * UNICOS and SELinux say yes.
1505 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1506 *
1507 * I'll say no for now. Smack does not do the frequent
1508 * label changing that SELinux does.
1509 */
Casey Schauflere114e472008-02-04 22:29:50 -08001510
1511/**
1512 * smack_file_alloc_security - assign a file security blob
1513 * @file: the object
1514 *
1515 * The security blob for a file is a pointer to the master
1516 * label list, so no allocation is done.
1517 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001518 * f_security is the owner security information. It
1519 * isn't used on file access checks, it's for send_sigio.
1520 *
Casey Schauflere114e472008-02-04 22:29:50 -08001521 * Returns 0
1522 */
1523static int smack_file_alloc_security(struct file *file)
1524{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001525 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001526
Casey Schauflerf28952a2018-11-12 09:38:53 -08001527 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001528 return 0;
1529}
1530
1531/**
Casey Schauflere114e472008-02-04 22:29:50 -08001532 * smack_file_ioctl - Smack check on ioctls
1533 * @file: the object
1534 * @cmd: what to do
1535 * @arg: unused
1536 *
1537 * Relies heavily on the correct use of the ioctl command conventions.
1538 *
1539 * Returns 0 if allowed, error code otherwise
1540 */
1541static int smack_file_ioctl(struct file *file, unsigned int cmd,
1542 unsigned long arg)
1543{
1544 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001545 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001546 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001547
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001548 if (unlikely(IS_PRIVATE(inode)))
1549 return 0;
1550
Eric Parisf48b7392011-04-25 12:54:27 -04001551 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001552 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001553
Casey Schauflerd166c802014-08-27 14:51:27 -07001554 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001555 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001556 rc = smk_bu_file(file, MAY_WRITE, rc);
1557 }
Casey Schauflere114e472008-02-04 22:29:50 -08001558
Casey Schauflerd166c802014-08-27 14:51:27 -07001559 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001560 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001561 rc = smk_bu_file(file, MAY_READ, rc);
1562 }
Casey Schauflere114e472008-02-04 22:29:50 -08001563
1564 return rc;
1565}
1566
1567/**
1568 * smack_file_lock - Smack check on file locking
1569 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001570 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001571 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001572 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001573 */
1574static int smack_file_lock(struct file *file, unsigned int cmd)
1575{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001576 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001577 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001578 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001579
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001580 if (unlikely(IS_PRIVATE(inode)))
1581 return 0;
1582
Eric Paris92f42502011-04-25 13:15:55 -04001583 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1584 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001585 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001586 rc = smk_bu_file(file, MAY_LOCK, rc);
1587 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001588}
1589
1590/**
1591 * smack_file_fcntl - Smack check on fcntl
1592 * @file: the object
1593 * @cmd: what action to check
1594 * @arg: unused
1595 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001596 * Generally these operations are harmless.
1597 * File locking operations present an obvious mechanism
1598 * for passing information, so they require write access.
1599 *
Casey Schauflere114e472008-02-04 22:29:50 -08001600 * Returns 0 if current has access, error code otherwise
1601 */
1602static int smack_file_fcntl(struct file *file, unsigned int cmd,
1603 unsigned long arg)
1604{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001605 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001606 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001607 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001608
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001609 if (unlikely(IS_PRIVATE(inode)))
1610 return 0;
1611
Casey Schauflere114e472008-02-04 22:29:50 -08001612 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001613 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001614 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001615 case F_SETLK:
1616 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001617 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1618 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001619 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001620 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001621 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001622 case F_SETOWN:
1623 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001624 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1625 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001626 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001627 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001628 break;
1629 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001630 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001631 }
1632
1633 return rc;
1634}
1635
1636/**
Al Viroe5467852012-05-30 13:30:51 -04001637 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001638 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1639 * if mapping anonymous memory.
1640 * @file contains the file structure for file to map (may be NULL).
1641 * @reqprot contains the protection requested by the application.
1642 * @prot contains the protection that will be applied by the kernel.
1643 * @flags contains the operational flags.
1644 * Return 0 if permission is granted.
1645 */
Al Viroe5467852012-05-30 13:30:51 -04001646static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001647 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001648 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001649{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001650 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001651 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001652 struct smack_rule *srp;
1653 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001654 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001655 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001656 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001657 int may;
1658 int mmay;
1659 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001660 int rc;
1661
Al Viro496ad9a2013-01-23 17:07:38 -05001662 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001663 return 0;
1664
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001665 if (unlikely(IS_PRIVATE(file_inode(file))))
1666 return 0;
1667
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001668 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001669 if (isp->smk_mmap == NULL)
1670 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001671 sbsp = file_inode(file)->i_sb->s_security;
1672 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1673 isp->smk_mmap != sbsp->smk_root)
1674 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001675 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001676
Casey Schauflerb17103a2018-11-09 16:12:56 -08001677 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001678 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001679 rc = 0;
1680
1681 rcu_read_lock();
1682 /*
1683 * For each Smack rule associated with the subject
1684 * label verify that the SMACK64MMAP also has access
1685 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001686 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001687 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001688 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001689 /*
1690 * Matching labels always allows access.
1691 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001692 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001693 continue;
1694 /*
1695 * If there is a matching local rule take
1696 * that into account as well.
1697 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001698 may = smk_access_entry(srp->smk_subject->smk_known,
1699 okp->smk_known,
1700 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001701 if (may == -ENOENT)
1702 may = srp->smk_access;
1703 else
1704 may &= srp->smk_access;
1705 /*
1706 * If may is zero the SMACK64MMAP subject can't
1707 * possibly have less access.
1708 */
1709 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001710 continue;
1711
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001712 /*
1713 * Fetch the global list entry.
1714 * If there isn't one a SMACK64MMAP subject
1715 * can't have as much access as current.
1716 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001717 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1718 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001719 if (mmay == -ENOENT) {
1720 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001721 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001722 }
1723 /*
1724 * If there is a local entry it modifies the
1725 * potential access, too.
1726 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001727 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1728 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001729 if (tmay != -ENOENT)
1730 mmay &= tmay;
1731
1732 /*
1733 * If there is any access available to current that is
1734 * not available to a SMACK64MMAP subject
1735 * deny access.
1736 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001737 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001738 rc = -EACCES;
1739 break;
1740 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001741 }
1742
1743 rcu_read_unlock();
1744
1745 return rc;
1746}
1747
1748/**
Casey Schauflere114e472008-02-04 22:29:50 -08001749 * smack_file_set_fowner - set the file security blob value
1750 * @file: object in question
1751 *
Casey Schauflere114e472008-02-04 22:29:50 -08001752 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001753static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001754{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001755 struct smack_known **blob = smack_file(file);
1756
1757 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001758}
1759
1760/**
1761 * smack_file_send_sigiotask - Smack on sigio
1762 * @tsk: The target task
1763 * @fown: the object the signal come from
1764 * @signum: unused
1765 *
1766 * Allow a privileged task to get signals even if it shouldn't
1767 *
1768 * Returns 0 if a subject with the object's smack could
1769 * write to the task, an error code otherwise.
1770 */
1771static int smack_file_send_sigiotask(struct task_struct *tsk,
1772 struct fown_struct *fown, int signum)
1773{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001774 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001775 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001776 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001777 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001778 struct file *file;
1779 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001780 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001781
1782 /*
1783 * struct fown_struct is never outside the context of a struct file
1784 */
1785 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001786
Etienne Bassetecfcc532009-04-08 20:40:06 +02001787 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001788 blob = smack_file(file);
1789 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001790 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1791 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001792
1793 rcu_read_lock();
1794 tcred = __task_cred(tsk);
1795 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001796 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001797 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001798
1799 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1800 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001801 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001802 return rc;
1803}
1804
1805/**
1806 * smack_file_receive - Smack file receive check
1807 * @file: the object
1808 *
1809 * Returns 0 if current has access, error code otherwise
1810 */
1811static int smack_file_receive(struct file *file)
1812{
Casey Schauflerd166c802014-08-27 14:51:27 -07001813 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001814 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001815 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001816 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001817 struct socket *sock;
1818 struct task_smack *tsp;
1819 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001820
Seung-Woo Kim97775822015-04-17 15:25:04 +09001821 if (unlikely(IS_PRIVATE(inode)))
1822 return 0;
1823
Casey Schaufler4482a442013-12-30 17:37:45 -08001824 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001825 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001826
Casey Schaufler51d59af2017-05-31 08:53:42 -07001827 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001828 sock = SOCKET_I(inode);
1829 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001830 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001831 /*
1832 * If the receiving process can't write to the
1833 * passed socket or if the passed socket can't
1834 * write to the receiving process don't accept
1835 * the passed socket.
1836 */
1837 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1838 rc = smk_bu_file(file, may, rc);
1839 if (rc < 0)
1840 return rc;
1841 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1842 rc = smk_bu_file(file, may, rc);
1843 return rc;
1844 }
Casey Schauflere114e472008-02-04 22:29:50 -08001845 /*
1846 * This code relies on bitmasks.
1847 */
1848 if (file->f_mode & FMODE_READ)
1849 may = MAY_READ;
1850 if (file->f_mode & FMODE_WRITE)
1851 may |= MAY_WRITE;
1852
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001853 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001854 rc = smk_bu_file(file, may, rc);
1855 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001856}
1857
Casey Schaufler531f1d42011-09-19 12:41:42 -07001858/**
Eric Paris83d49852012-04-04 13:45:40 -04001859 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001860 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001861 *
1862 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001863 * Allow the open only if the task has read access. There are
1864 * many read operations (e.g. fstat) that you can do with an
1865 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001866 *
luanshia1a07f22019-07-05 10:35:20 +08001867 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001868 */
Al Viro94817692018-07-10 14:13:18 -04001869static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001870{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001871 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001872 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001873 struct smk_audit_info ad;
1874 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001875
Casey Schauflera6834c02014-04-21 11:10:26 -07001876 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1877 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301878 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001879 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001880
1881 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001882}
1883
Casey Schauflere114e472008-02-04 22:29:50 -08001884/*
1885 * Task hooks
1886 */
1887
1888/**
David Howellsee18d642009-09-02 09:14:21 +01001889 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001890 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001891 * @gfp: the atomicity of any memory allocations
1892 *
1893 * Prepare a blank set of credentials for modification. This must allocate all
1894 * the memory the LSM module might require such that cred_transfer() can
1895 * complete without error.
1896 */
1897static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1898{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001899 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001900 return 0;
1901}
1902
1903
1904/**
David Howellsf1752ee2008-11-14 10:39:17 +11001905 * smack_cred_free - "free" task-level security credentials
1906 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001907 *
Casey Schauflere114e472008-02-04 22:29:50 -08001908 */
David Howellsf1752ee2008-11-14 10:39:17 +11001909static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001910{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001911 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001912 struct smack_rule *rp;
1913 struct list_head *l;
1914 struct list_head *n;
1915
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001916 smk_destroy_label_list(&tsp->smk_relabel);
1917
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001918 list_for_each_safe(l, n, &tsp->smk_rules) {
1919 rp = list_entry(l, struct smack_rule, list);
1920 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001921 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001922 }
Casey Schauflere114e472008-02-04 22:29:50 -08001923}
1924
1925/**
David Howellsd84f4f92008-11-14 10:39:23 +11001926 * smack_cred_prepare - prepare new set of credentials for modification
1927 * @new: the new credentials
1928 * @old: the original credentials
1929 * @gfp: the atomicity of any memory allocations
1930 *
1931 * Prepare a new set of credentials for modification.
1932 */
1933static int smack_cred_prepare(struct cred *new, const struct cred *old,
1934 gfp_t gfp)
1935{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001936 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001937 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001938 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001939
Casey Schauflerbbd36622018-11-12 09:30:56 -08001940 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301941
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001942 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1943 if (rc != 0)
1944 return rc;
1945
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001946 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1947 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001948 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001949}
1950
Randy Dunlap251a2a92009-02-18 11:42:33 -08001951/**
David Howellsee18d642009-09-02 09:14:21 +01001952 * smack_cred_transfer - Transfer the old credentials to the new credentials
1953 * @new: the new credentials
1954 * @old: the original credentials
1955 *
1956 * Fill in a set of blank credentials from another set of credentials.
1957 */
1958static void smack_cred_transfer(struct cred *new, const struct cred *old)
1959{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001960 struct task_smack *old_tsp = smack_cred(old);
1961 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001962
1963 new_tsp->smk_task = old_tsp->smk_task;
1964 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001965 mutex_init(&new_tsp->smk_rules_lock);
1966 INIT_LIST_HEAD(&new_tsp->smk_rules);
1967
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001968 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001969}
1970
1971/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001972 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001973 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001974 * @secid: where to put the result
1975 *
1976 * Sets the secid to contain a u32 version of the smack label.
1977 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001978static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001979{
1980 struct smack_known *skp;
1981
1982 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001983 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001984 *secid = skp->smk_secid;
1985 rcu_read_unlock();
1986}
1987
1988/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001989 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001990 * @new: points to the set of credentials to be modified.
1991 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001992 *
1993 * Set the security data for a kernel service.
1994 */
1995static int smack_kernel_act_as(struct cred *new, u32 secid)
1996{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001997 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11001998
Casey Schaufler152f91d2016-11-14 09:38:15 -08001999 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002000 return 0;
2001}
2002
2003/**
2004 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002005 * @new: points to the set of credentials to be modified
2006 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002007 *
2008 * Set the file creation context in a set of credentials to the same
2009 * as the objective context of the specified inode
2010 */
2011static int smack_kernel_create_files_as(struct cred *new,
2012 struct inode *inode)
2013{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002014 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002015 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002016
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002017 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002018 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002019 return 0;
2020}
2021
2022/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002023 * smk_curacc_on_task - helper to log task related access
2024 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002025 * @access: the access requested
2026 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002027 *
2028 * Return 0 if access is permitted
2029 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002030static int smk_curacc_on_task(struct task_struct *p, int access,
2031 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002032{
2033 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002034 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002035 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002036
Casey Schaufler531f1d42011-09-19 12:41:42 -07002037 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002038 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002039 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002040 rc = smk_bu_task(p, access, rc);
2041 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002042}
2043
2044/**
Casey Schauflere114e472008-02-04 22:29:50 -08002045 * smack_task_setpgid - Smack check on setting pgid
2046 * @p: the task object
2047 * @pgid: unused
2048 *
2049 * Return 0 if write access is permitted
2050 */
2051static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2052{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002053 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002054}
2055
2056/**
2057 * smack_task_getpgid - Smack access check for getpgid
2058 * @p: the object task
2059 *
2060 * Returns 0 if current can read the object task, error code otherwise
2061 */
2062static int smack_task_getpgid(struct task_struct *p)
2063{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002064 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002065}
2066
2067/**
2068 * smack_task_getsid - Smack access check for getsid
2069 * @p: the object task
2070 *
2071 * Returns 0 if current can read the object task, error code otherwise
2072 */
2073static int smack_task_getsid(struct task_struct *p)
2074{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002075 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002076}
2077
2078/**
2079 * smack_task_getsecid - get the secid of the task
2080 * @p: the object task
2081 * @secid: where to put the result
2082 *
2083 * Sets the secid to contain a u32 version of the smack label.
2084 */
2085static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2086{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002087 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002088
2089 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002090}
2091
2092/**
2093 * smack_task_setnice - Smack check on setting nice
2094 * @p: the task object
2095 * @nice: unused
2096 *
2097 * Return 0 if write access is permitted
2098 */
2099static int smack_task_setnice(struct task_struct *p, int nice)
2100{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002101 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002102}
2103
2104/**
2105 * smack_task_setioprio - Smack check on setting ioprio
2106 * @p: the task object
2107 * @ioprio: unused
2108 *
2109 * Return 0 if write access is permitted
2110 */
2111static int smack_task_setioprio(struct task_struct *p, int ioprio)
2112{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002113 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002114}
2115
2116/**
2117 * smack_task_getioprio - Smack check on reading ioprio
2118 * @p: the task object
2119 *
2120 * Return 0 if read access is permitted
2121 */
2122static int smack_task_getioprio(struct task_struct *p)
2123{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002124 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002125}
2126
2127/**
2128 * smack_task_setscheduler - Smack check on setting scheduler
2129 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002130 *
2131 * Return 0 if read access is permitted
2132 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002133static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002134{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002135 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002136}
2137
2138/**
2139 * smack_task_getscheduler - Smack check on reading scheduler
2140 * @p: the task object
2141 *
2142 * Return 0 if read access is permitted
2143 */
2144static int smack_task_getscheduler(struct task_struct *p)
2145{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002146 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002147}
2148
2149/**
2150 * smack_task_movememory - Smack check on moving memory
2151 * @p: the task object
2152 *
2153 * Return 0 if write access is permitted
2154 */
2155static int smack_task_movememory(struct task_struct *p)
2156{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002157 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002158}
2159
2160/**
2161 * smack_task_kill - Smack check on signal delivery
2162 * @p: the task object
2163 * @info: unused
2164 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002165 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002166 *
2167 * Return 0 if write access is permitted
2168 *
Casey Schauflere114e472008-02-04 22:29:50 -08002169 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002170static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002171 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002172{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002173 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002174 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002175 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002176 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002177
Rafal Krypa18d872f2016-04-04 11:14:53 +02002178 if (!sig)
2179 return 0; /* null signal; existence test */
2180
Etienne Bassetecfcc532009-04-08 20:40:06 +02002181 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2182 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002183 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002184 * Sending a signal requires that the sender
2185 * can write the receiver.
2186 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002187 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002188 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2189 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002190 return rc;
2191 }
Casey Schauflere114e472008-02-04 22:29:50 -08002192 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002193 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002194 * specific behavior. This is not clean. For one thing
2195 * we can't take privilege into account.
2196 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002197 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002198 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2199 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002200 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002201}
2202
2203/**
Casey Schauflere114e472008-02-04 22:29:50 -08002204 * smack_task_to_inode - copy task smack into the inode blob
2205 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002206 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002207 *
2208 * Sets the smack pointer in the inode security blob
2209 */
2210static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2211{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002212 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002213 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002214
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002215 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002216 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002217}
2218
2219/*
2220 * Socket hooks.
2221 */
2222
2223/**
2224 * smack_sk_alloc_security - Allocate a socket blob
2225 * @sk: the socket
2226 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002227 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002228 *
2229 * Assign Smack pointers to current
2230 *
2231 * Returns 0 on success, -ENOMEM is there's no memory
2232 */
2233static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2234{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002235 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002236 struct socket_smack *ssp;
2237
2238 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2239 if (ssp == NULL)
2240 return -ENOMEM;
2241
jooseong lee08382c92016-11-03 11:54:39 +01002242 /*
2243 * Sockets created by kernel threads receive web label.
2244 */
2245 if (unlikely(current->flags & PF_KTHREAD)) {
2246 ssp->smk_in = &smack_known_web;
2247 ssp->smk_out = &smack_known_web;
2248 } else {
2249 ssp->smk_in = skp;
2250 ssp->smk_out = skp;
2251 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002252 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002253
2254 sk->sk_security = ssp;
2255
2256 return 0;
2257}
2258
2259/**
2260 * smack_sk_free_security - Free a socket blob
2261 * @sk: the socket
2262 *
2263 * Clears the blob pointer
2264 */
2265static void smack_sk_free_security(struct sock *sk)
2266{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302267#ifdef SMACK_IPV6_PORT_LABELING
2268 struct smk_port_label *spp;
2269
2270 if (sk->sk_family == PF_INET6) {
2271 rcu_read_lock();
2272 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2273 if (spp->smk_sock != sk)
2274 continue;
2275 spp->smk_can_reuse = 1;
2276 break;
2277 }
2278 rcu_read_unlock();
2279 }
2280#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002281 kfree(sk->sk_security);
2282}
2283
2284/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002285* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002286* @sip: the object end
2287*
2288* looks for host based access restrictions
2289*
2290* This version will only be appropriate for really small sets of single label
2291* hosts. The caller is responsible for ensuring that the RCU read lock is
2292* taken before calling this function.
2293*
2294* Returns the label of the far end or NULL if it's not special.
2295*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002296static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002297{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002298 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002299 struct in_addr *siap = &sip->sin_addr;
2300
2301 if (siap->s_addr == 0)
2302 return NULL;
2303
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002304 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2305 /*
2306 * we break after finding the first match because
2307 * the list is sorted from longest to shortest mask
2308 * so we have found the most specific match
2309 */
2310 if (snp->smk_host.s_addr ==
2311 (siap->s_addr & snp->smk_mask.s_addr))
2312 return snp->smk_label;
2313
2314 return NULL;
2315}
2316
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002317/*
2318 * smk_ipv6_localhost - Check for local ipv6 host address
2319 * @sip: the address
2320 *
2321 * Returns boolean true if this is the localhost address
2322 */
2323static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2324{
2325 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2326 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2327
2328 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2329 ntohs(be16p[7]) == 1)
2330 return true;
2331 return false;
2332}
2333
2334/**
2335* smack_ipv6host_label - check host based restrictions
2336* @sip: the object end
2337*
2338* looks for host based access restrictions
2339*
2340* This version will only be appropriate for really small sets of single label
2341* hosts. The caller is responsible for ensuring that the RCU read lock is
2342* taken before calling this function.
2343*
2344* Returns the label of the far end or NULL if it's not special.
2345*/
2346static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2347{
2348 struct smk_net6addr *snp;
2349 struct in6_addr *sap = &sip->sin6_addr;
2350 int i;
2351 int found = 0;
2352
2353 /*
2354 * It's local. Don't look for a host label.
2355 */
2356 if (smk_ipv6_localhost(sip))
2357 return NULL;
2358
2359 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002360 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002361 * If the label is NULL the entry has
2362 * been renounced. Ignore it.
2363 */
2364 if (snp->smk_label == NULL)
2365 continue;
2366 /*
Paul Moore07feee82009-03-27 17:10:54 -04002367 * we break after finding the first match because
2368 * the list is sorted from longest to shortest mask
2369 * so we have found the most specific match
2370 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002371 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002372 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2373 snp->smk_host.s6_addr16[i]) {
2374 found = 0;
2375 break;
2376 }
Etienne Basset43031542009-03-27 17:11:01 -04002377 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002378 if (found)
2379 return snp->smk_label;
2380 }
Paul Moore07feee82009-03-27 17:10:54 -04002381
2382 return NULL;
2383}
2384
2385/**
Casey Schauflera2af0312020-08-11 17:39:42 -07002386 * smack_netlbl_add - Set the secattr on a socket
Casey Schauflere114e472008-02-04 22:29:50 -08002387 * @sk: the socket
2388 *
Casey Schauflera2af0312020-08-11 17:39:42 -07002389 * Attach the outbound smack value (smk_out) to the socket.
Casey Schauflere114e472008-02-04 22:29:50 -08002390 *
2391 * Returns 0 on success or an error code
2392 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002393static int smack_netlbl_add(struct sock *sk)
Casey Schauflere114e472008-02-04 22:29:50 -08002394{
Paul Moore07feee82009-03-27 17:10:54 -04002395 struct socket_smack *ssp = sk->sk_security;
Casey Schauflera2af0312020-08-11 17:39:42 -07002396 struct smack_known *skp = ssp->smk_out;
2397 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002398
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002399 local_bh_disable();
2400 bh_lock_sock_nested(sk);
2401
Casey Schauflera2af0312020-08-11 17:39:42 -07002402 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2403 switch (rc) {
2404 case 0:
2405 ssp->smk_state = SMK_NETLBL_LABELED;
2406 break;
2407 case -EDESTADDRREQ:
2408 ssp->smk_state = SMK_NETLBL_REQSKB;
2409 rc = 0;
2410 break;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002411 }
2412
2413 bh_unlock_sock(sk);
2414 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002415
Casey Schauflere114e472008-02-04 22:29:50 -08002416 return rc;
2417}
2418
2419/**
Casey Schauflera2af0312020-08-11 17:39:42 -07002420 * smack_netlbl_delete - Remove the secattr from a socket
2421 * @sk: the socket
2422 *
2423 * Remove the outbound smack value from a socket
2424 */
2425static void smack_netlbl_delete(struct sock *sk)
2426{
2427 struct socket_smack *ssp = sk->sk_security;
2428
2429 /*
2430 * Take the label off the socket if one is set.
2431 */
2432 if (ssp->smk_state != SMK_NETLBL_LABELED)
2433 return;
2434
2435 local_bh_disable();
2436 bh_lock_sock_nested(sk);
2437 netlbl_sock_delattr(sk);
2438 bh_unlock_sock(sk);
2439 local_bh_enable();
2440 ssp->smk_state = SMK_NETLBL_UNLABELED;
2441}
2442
2443/**
2444 * smk_ipv4_check - Perform IPv4 host access checks
Paul Moore07feee82009-03-27 17:10:54 -04002445 * @sk: the socket
2446 * @sap: the destination address
2447 *
2448 * Set the correct secattr for the given socket based on the destination
2449 * address and perform any outbound access checks needed.
2450 *
2451 * Returns 0 on success or an error code.
2452 *
2453 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002454static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
Paul Moore07feee82009-03-27 17:10:54 -04002455{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002456 struct smack_known *skp;
Casey Schauflera2af0312020-08-11 17:39:42 -07002457 int rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002458 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002459 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002460 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002461
2462 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002463 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002464 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002465#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002466 struct lsm_network_audit net;
2467
Eric Paris48c62af2012-04-02 13:15:44 -04002468 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2469 ad.a.u.net->family = sap->sin_family;
2470 ad.a.u.net->dport = sap->sin_port;
2471 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002472#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002473 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002474 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2475 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Casey Schauflera2af0312020-08-11 17:39:42 -07002476 /*
2477 * Clear the socket netlabel if it's set.
2478 */
2479 if (!rc)
2480 smack_netlbl_delete(sk);
Paul Moore07feee82009-03-27 17:10:54 -04002481 }
2482 rcu_read_unlock();
Paul Moore07feee82009-03-27 17:10:54 -04002483
Casey Schauflera2af0312020-08-11 17:39:42 -07002484 return rc;
Paul Moore07feee82009-03-27 17:10:54 -04002485}
2486
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002487/**
2488 * smk_ipv6_check - check Smack access
2489 * @subject: subject Smack label
2490 * @object: object Smack label
2491 * @address: address
2492 * @act: the action being taken
2493 *
2494 * Check an IPv6 access
2495 */
2496static int smk_ipv6_check(struct smack_known *subject,
2497 struct smack_known *object,
2498 struct sockaddr_in6 *address, int act)
2499{
2500#ifdef CONFIG_AUDIT
2501 struct lsm_network_audit net;
2502#endif
2503 struct smk_audit_info ad;
2504 int rc;
2505
2506#ifdef CONFIG_AUDIT
2507 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2508 ad.a.u.net->family = PF_INET6;
2509 ad.a.u.net->dport = ntohs(address->sin6_port);
2510 if (act == SMK_RECEIVING)
2511 ad.a.u.net->v6info.saddr = address->sin6_addr;
2512 else
2513 ad.a.u.net->v6info.daddr = address->sin6_addr;
2514#endif
2515 rc = smk_access(subject, object, MAY_WRITE, &ad);
2516 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2517 return rc;
2518}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002519
2520#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002521/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002522 * smk_ipv6_port_label - Smack port access table management
2523 * @sock: socket
2524 * @address: address
2525 *
2526 * Create or update the port list entry
2527 */
2528static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2529{
2530 struct sock *sk = sock->sk;
2531 struct sockaddr_in6 *addr6;
2532 struct socket_smack *ssp = sock->sk->sk_security;
2533 struct smk_port_label *spp;
2534 unsigned short port = 0;
2535
2536 if (address == NULL) {
2537 /*
2538 * This operation is changing the Smack information
2539 * on the bound socket. Take the changes to the port
2540 * as well.
2541 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302542 rcu_read_lock();
2543 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002544 if (sk != spp->smk_sock)
2545 continue;
2546 spp->smk_in = ssp->smk_in;
2547 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302548 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002549 return;
2550 }
2551 /*
2552 * A NULL address is only used for updating existing
2553 * bound entries. If there isn't one, it's OK.
2554 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302555 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002556 return;
2557 }
2558
2559 addr6 = (struct sockaddr_in6 *)address;
2560 port = ntohs(addr6->sin6_port);
2561 /*
2562 * This is a special case that is safely ignored.
2563 */
2564 if (port == 0)
2565 return;
2566
2567 /*
2568 * Look for an existing port list entry.
2569 * This is an indication that a port is getting reused.
2570 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302571 rcu_read_lock();
2572 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302573 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002574 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302575 if (spp->smk_can_reuse != 1) {
2576 rcu_read_unlock();
2577 return;
2578 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002579 spp->smk_port = port;
2580 spp->smk_sock = sk;
2581 spp->smk_in = ssp->smk_in;
2582 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302583 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302584 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002585 return;
2586 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302587 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002588 /*
2589 * A new port entry is required.
2590 */
2591 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2592 if (spp == NULL)
2593 return;
2594
2595 spp->smk_port = port;
2596 spp->smk_sock = sk;
2597 spp->smk_in = ssp->smk_in;
2598 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302599 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302600 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002601
Vishal Goel3c7ce342016-11-23 10:31:08 +05302602 mutex_lock(&smack_ipv6_lock);
2603 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2604 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002605 return;
2606}
Arnd Bergmann00720f02020-04-08 21:04:31 +02002607#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002608
2609/**
2610 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002611 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002612 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002613 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002614 *
2615 * Create or update the port list entry
2616 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002617static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002618 int act)
2619{
Casey Schauflerc6739442013-05-22 18:42:56 -07002620 struct smk_port_label *spp;
2621 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002622 struct smack_known *skp = NULL;
2623 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002624 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002625
2626 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002627 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002628 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002629 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002630 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002631 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002632 }
2633
2634 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002635 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002636 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002637 if (skp != NULL && object != NULL)
2638 return smk_ipv6_check(skp, object, address, act);
2639 if (skp == NULL)
2640 skp = smack_net_ambient;
2641 if (object == NULL)
2642 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002643
2644 /*
2645 * It's remote, so port lookup does no good.
2646 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002647 if (!smk_ipv6_localhost(address))
2648 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002649
2650 /*
2651 * It's local so the send check has to have passed.
2652 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002653 if (act == SMK_RECEIVING)
2654 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002655
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002656 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302657 rcu_read_lock();
2658 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302659 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002660 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002661 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002662 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002663 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002664 break;
2665 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302666 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002667
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002668 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002669}
2670
2671/**
Casey Schauflere114e472008-02-04 22:29:50 -08002672 * smack_inode_setsecurity - set smack xattrs
2673 * @inode: the object
2674 * @name: attribute name
2675 * @value: attribute value
2676 * @size: size of the attribute
2677 * @flags: unused
2678 *
2679 * Sets the named attribute in the appropriate blob
2680 *
2681 * Returns 0 on success, or an error code
2682 */
2683static int smack_inode_setsecurity(struct inode *inode, const char *name,
2684 const void *value, size_t size, int flags)
2685{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002686 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002687 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002688 struct socket_smack *ssp;
2689 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002690 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002691
Casey Schauflerf7112e62012-05-06 15:22:02 -07002692 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302693 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002694
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002695 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002696 if (IS_ERR(skp))
2697 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002698
2699 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002700 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002701 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002702 return 0;
2703 }
2704 /*
2705 * The rest of the Smack xattrs are only on sockets.
2706 */
2707 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2708 return -EOPNOTSUPP;
2709
2710 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002711 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002712 return -EOPNOTSUPP;
2713
2714 ssp = sock->sk->sk_security;
2715
2716 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002717 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002718 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002719 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002720 if (sock->sk->sk_family == PF_INET) {
Casey Schauflera2af0312020-08-11 17:39:42 -07002721 rc = smack_netlbl_add(sock->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002722 if (rc != 0)
2723 printk(KERN_WARNING
2724 "Smack: \"%s\" netlbl error %d.\n",
2725 __func__, -rc);
2726 }
Casey Schauflere114e472008-02-04 22:29:50 -08002727 } else
2728 return -EOPNOTSUPP;
2729
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002730#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002731 if (sock->sk->sk_family == PF_INET6)
2732 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002733#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002734
Casey Schauflere114e472008-02-04 22:29:50 -08002735 return 0;
2736}
2737
2738/**
2739 * smack_socket_post_create - finish socket setup
2740 * @sock: the socket
2741 * @family: protocol family
2742 * @type: unused
2743 * @protocol: unused
2744 * @kern: unused
2745 *
2746 * Sets the netlabel information on the socket
2747 *
2748 * Returns 0 on success, and error code otherwise
2749 */
2750static int smack_socket_post_create(struct socket *sock, int family,
2751 int type, int protocol, int kern)
2752{
Marcin Lis74123012015-01-22 15:40:33 +01002753 struct socket_smack *ssp;
2754
2755 if (sock->sk == NULL)
2756 return 0;
2757
2758 /*
2759 * Sockets created by kernel threads receive web label.
2760 */
2761 if (unlikely(current->flags & PF_KTHREAD)) {
2762 ssp = sock->sk->sk_security;
2763 ssp->smk_in = &smack_known_web;
2764 ssp->smk_out = &smack_known_web;
2765 }
2766
2767 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002768 return 0;
2769 /*
2770 * Set the outbound netlbl.
2771 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002772 return smack_netlbl_add(sock->sk);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002773}
2774
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002775/**
2776 * smack_socket_socketpair - create socket pair
2777 * @socka: one socket
2778 * @sockb: another socket
2779 *
2780 * Cross reference the peer labels for SO_PEERSEC
2781 *
luanshia1a07f22019-07-05 10:35:20 +08002782 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002783 */
2784static int smack_socket_socketpair(struct socket *socka,
2785 struct socket *sockb)
2786{
2787 struct socket_smack *asp = socka->sk->sk_security;
2788 struct socket_smack *bsp = sockb->sk->sk_security;
2789
2790 asp->smk_packet = bsp->smk_out;
2791 bsp->smk_packet = asp->smk_out;
2792
2793 return 0;
2794}
2795
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002796#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002797/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002798 * smack_socket_bind - record port binding information.
2799 * @sock: the socket
2800 * @address: the port address
2801 * @addrlen: size of the address
2802 *
2803 * Records the label bound to a port.
2804 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002805 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002806 */
2807static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2808 int addrlen)
2809{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002810 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2811 if (addrlen < SIN6_LEN_RFC2133 ||
2812 address->sa_family != AF_INET6)
2813 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002814 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002815 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002816 return 0;
2817}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002818#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002819
2820/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002821 * smack_socket_connect - connect access check
2822 * @sock: the socket
2823 * @sap: the other end
2824 * @addrlen: size of sap
2825 *
2826 * Verifies that a connection may be possible
2827 *
2828 * Returns 0 on success, and error code otherwise
2829 */
2830static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2831 int addrlen)
2832{
Casey Schauflerc6739442013-05-22 18:42:56 -07002833 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002834
Casey Schauflerc6739442013-05-22 18:42:56 -07002835 if (sock->sk == NULL)
2836 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002837 if (sock->sk->sk_family != PF_INET &&
2838 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2839 return 0;
2840 if (addrlen < offsetofend(struct sockaddr, sa_family))
2841 return 0;
2842 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2843 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002844 struct smack_known *rsp = NULL;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002845
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002846 if (addrlen < SIN6_LEN_RFC2133)
2847 return 0;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002848 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2849 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002850 if (rsp != NULL) {
2851 struct socket_smack *ssp = sock->sk->sk_security;
2852
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002853 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002854 SMK_CONNECTING);
2855 }
Arnd Bergmann00720f02020-04-08 21:04:31 +02002856 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2857 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2858
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002859 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002860 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002861 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2862 return 0;
Casey Schauflera2af0312020-08-11 17:39:42 -07002863 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002864 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002865}
2866
2867/**
2868 * smack_flags_to_may - convert S_ to MAY_ values
2869 * @flags: the S_ value
2870 *
2871 * Returns the equivalent MAY_ value
2872 */
2873static int smack_flags_to_may(int flags)
2874{
2875 int may = 0;
2876
2877 if (flags & S_IRUGO)
2878 may |= MAY_READ;
2879 if (flags & S_IWUGO)
2880 may |= MAY_WRITE;
2881 if (flags & S_IXUGO)
2882 may |= MAY_EXEC;
2883
2884 return may;
2885}
2886
2887/**
2888 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2889 * @msg: the object
2890 *
2891 * Returns 0
2892 */
2893static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2894{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002895 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002896
Casey Schauflerecd5f822018-11-20 11:55:02 -08002897 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002898 return 0;
2899}
2900
2901/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002902 * smack_of_ipc - the smack pointer for the ipc
2903 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002904 *
2905 * Returns a pointer to the smack value
2906 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002907static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002908{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002909 struct smack_known **blob = smack_ipc(isp);
2910
2911 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002912}
2913
2914/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002915 * smack_ipc_alloc_security - Set the security blob for ipc
2916 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002917 *
2918 * Returns 0
2919 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002920static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002921{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002922 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002923
Casey Schaufler019bcca2018-09-21 17:19:54 -07002924 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002925 return 0;
2926}
2927
2928/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002929 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002930 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002931 * @access : access requested
2932 *
2933 * Returns 0 if current has the requested access, error code otherwise
2934 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002935static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002936{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002937 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002938 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002939 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002940
2941#ifdef CONFIG_AUDIT
2942 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002943 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002944#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002945 rc = smk_curacc(ssp, access, &ad);
2946 rc = smk_bu_current("shm", ssp, access, rc);
2947 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002948}
2949
2950/**
Casey Schauflere114e472008-02-04 22:29:50 -08002951 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002952 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002953 * @shmflg: access requested
2954 *
2955 * Returns 0 if current has the requested access, error code otherwise
2956 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002957static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002958{
Casey Schauflere114e472008-02-04 22:29:50 -08002959 int may;
2960
2961 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002962 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002963}
2964
2965/**
2966 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002967 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002968 * @cmd: what it wants to do
2969 *
2970 * Returns 0 if current has the requested access, error code otherwise
2971 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002972static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002973{
Casey Schauflere114e472008-02-04 22:29:50 -08002974 int may;
2975
2976 switch (cmd) {
2977 case IPC_STAT:
2978 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002979 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002980 may = MAY_READ;
2981 break;
2982 case IPC_SET:
2983 case SHM_LOCK:
2984 case SHM_UNLOCK:
2985 case IPC_RMID:
2986 may = MAY_READWRITE;
2987 break;
2988 case IPC_INFO:
2989 case SHM_INFO:
2990 /*
2991 * System level information.
2992 */
2993 return 0;
2994 default:
2995 return -EINVAL;
2996 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002997 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002998}
2999
3000/**
3001 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003002 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003003 * @shmaddr: unused
3004 * @shmflg: access requested
3005 *
3006 * Returns 0 if current has the requested access, error code otherwise
3007 */
luanshia1a07f22019-07-05 10:35:20 +08003008static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003009 int shmflg)
3010{
Casey Schauflere114e472008-02-04 22:29:50 -08003011 int may;
3012
3013 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003014 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003015}
3016
3017/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003018 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003019 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003020 * @access : access requested
3021 *
3022 * Returns 0 if current has the requested access, error code otherwise
3023 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003024static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003025{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003026 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003027 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003028 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003029
3030#ifdef CONFIG_AUDIT
3031 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003032 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003033#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003034 rc = smk_curacc(ssp, access, &ad);
3035 rc = smk_bu_current("sem", ssp, access, rc);
3036 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003037}
3038
3039/**
Casey Schauflere114e472008-02-04 22:29:50 -08003040 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003041 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003042 * @semflg: access requested
3043 *
3044 * Returns 0 if current has the requested access, error code otherwise
3045 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003046static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003047{
Casey Schauflere114e472008-02-04 22:29:50 -08003048 int may;
3049
3050 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003051 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003052}
3053
3054/**
3055 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003056 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003057 * @cmd: what it wants to do
3058 *
3059 * Returns 0 if current has the requested access, error code otherwise
3060 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003061static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003062{
Casey Schauflere114e472008-02-04 22:29:50 -08003063 int may;
3064
3065 switch (cmd) {
3066 case GETPID:
3067 case GETNCNT:
3068 case GETZCNT:
3069 case GETVAL:
3070 case GETALL:
3071 case IPC_STAT:
3072 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003073 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003074 may = MAY_READ;
3075 break;
3076 case SETVAL:
3077 case SETALL:
3078 case IPC_RMID:
3079 case IPC_SET:
3080 may = MAY_READWRITE;
3081 break;
3082 case IPC_INFO:
3083 case SEM_INFO:
3084 /*
3085 * System level information
3086 */
3087 return 0;
3088 default:
3089 return -EINVAL;
3090 }
3091
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003092 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003093}
3094
3095/**
3096 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003097 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003098 * @sops: unused
3099 * @nsops: unused
3100 * @alter: unused
3101 *
3102 * Treated as read and write in all cases.
3103 *
3104 * Returns 0 if access is allowed, error code otherwise
3105 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003106static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003107 unsigned nsops, int alter)
3108{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003109 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003110}
3111
3112/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003113 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003114 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003115 * @access : access requested
3116 *
3117 * return 0 if current has access, error otherwise
3118 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003120{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003121 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003122 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003123 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003124
3125#ifdef CONFIG_AUDIT
3126 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003127 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003128#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003129 rc = smk_curacc(msp, access, &ad);
3130 rc = smk_bu_current("msq", msp, access, rc);
3131 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003132}
3133
3134/**
Casey Schauflere114e472008-02-04 22:29:50 -08003135 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003136 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003137 * @msqflg: access requested
3138 *
3139 * Returns 0 if current has the requested access, error code otherwise
3140 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003141static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003142{
Casey Schauflere114e472008-02-04 22:29:50 -08003143 int may;
3144
3145 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003146 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003147}
3148
3149/**
3150 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003151 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003152 * @cmd: what it wants to do
3153 *
3154 * Returns 0 if current has the requested access, error code otherwise
3155 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003156static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003157{
Casey Schauflere114e472008-02-04 22:29:50 -08003158 int may;
3159
3160 switch (cmd) {
3161 case IPC_STAT:
3162 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003163 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003164 may = MAY_READ;
3165 break;
3166 case IPC_SET:
3167 case IPC_RMID:
3168 may = MAY_READWRITE;
3169 break;
3170 case IPC_INFO:
3171 case MSG_INFO:
3172 /*
3173 * System level information
3174 */
3175 return 0;
3176 default:
3177 return -EINVAL;
3178 }
3179
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003180 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003181}
3182
3183/**
3184 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003185 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003186 * @msg: unused
3187 * @msqflg: access requested
3188 *
3189 * Returns 0 if current has the requested access, error code otherwise
3190 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003191static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003192 int msqflg)
3193{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003194 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003195
Etienne Bassetecfcc532009-04-08 20:40:06 +02003196 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003197 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003198}
3199
3200/**
3201 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003202 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003203 * @msg: unused
3204 * @target: unused
3205 * @type: unused
3206 * @mode: unused
3207 *
3208 * Returns 0 if current has read and write access, error code otherwise
3209 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003210static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003211 struct task_struct *target, long type, int mode)
3212{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003213 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003214}
3215
3216/**
3217 * smack_ipc_permission - Smack access for ipc_permission()
3218 * @ipp: the object permissions
3219 * @flag: access requested
3220 *
3221 * Returns 0 if current has read and write access, error code otherwise
3222 */
3223static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3224{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003225 struct smack_known **blob = smack_ipc(ipp);
3226 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003227 int may = smack_flags_to_may(flag);
3228 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003229 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003230
Etienne Bassetecfcc532009-04-08 20:40:06 +02003231#ifdef CONFIG_AUDIT
3232 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3233 ad.a.u.ipc_id = ipp->id;
3234#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003235 rc = smk_curacc(iskp, may, &ad);
3236 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003237 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003238}
3239
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003240/**
3241 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003242 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003243 * @secid: where result will be saved
3244 */
3245static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3246{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003247 struct smack_known **blob = smack_ipc(ipp);
3248 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003249
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003250 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003251}
3252
Casey Schauflere114e472008-02-04 22:29:50 -08003253/**
3254 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003255 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003256 * @inode: the object
3257 *
3258 * Set the inode's security blob if it hasn't been done already.
3259 */
3260static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3261{
3262 struct super_block *sbp;
3263 struct superblock_smack *sbsp;
3264 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003265 struct smack_known *skp;
3266 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003267 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003268 char trattr[TRANS_TRUE_SIZE];
3269 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003270 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003271 struct dentry *dp;
3272
3273 if (inode == NULL)
3274 return;
3275
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003276 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003277
Casey Schauflere114e472008-02-04 22:29:50 -08003278 /*
3279 * If the inode is already instantiated
3280 * take the quick way out
3281 */
3282 if (isp->smk_flags & SMK_INODE_INSTANT)
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003283 return;
Casey Schauflere114e472008-02-04 22:29:50 -08003284
3285 sbp = inode->i_sb;
3286 sbsp = sbp->s_security;
3287 /*
3288 * We're going to use the superblock default label
3289 * if there's no label on the file.
3290 */
3291 final = sbsp->smk_default;
3292
3293 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003294 * If this is the root inode the superblock
3295 * may be in the process of initialization.
3296 * If that is the case use the root value out
3297 * of the superblock.
3298 */
3299 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003300 switch (sbp->s_magic) {
3301 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003302 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003303 /*
3304 * The cgroup filesystem is never mounted,
3305 * so there's no opportunity to set the mount
3306 * options.
3307 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003308 sbsp->smk_root = &smack_known_star;
3309 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003310 isp->smk_inode = sbsp->smk_root;
3311 break;
3312 case TMPFS_MAGIC:
3313 /*
3314 * What about shmem/tmpfs anonymous files with dentry
3315 * obtained from d_alloc_pseudo()?
3316 */
3317 isp->smk_inode = smk_of_current();
3318 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003319 case PIPEFS_MAGIC:
3320 isp->smk_inode = smk_of_current();
3321 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003322 case SOCKFS_MAGIC:
3323 /*
3324 * Socket access is controlled by the socket
3325 * structures associated with the task involved.
3326 */
3327 isp->smk_inode = &smack_known_star;
3328 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003329 default:
3330 isp->smk_inode = sbsp->smk_root;
3331 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003332 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003333 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003334 return;
Casey Schauflere97dcb02008-06-02 10:04:32 -07003335 }
3336
3337 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003338 * This is pretty hackish.
3339 * Casey says that we shouldn't have to do
3340 * file system specific code, but it does help
3341 * with keeping it simple.
3342 */
3343 switch (sbp->s_magic) {
3344 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003345 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003346 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003347 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003348 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003349 * that the smack file system doesn't do
3350 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003351 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003352 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003353 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003354 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003355 break;
3356 case DEVPTS_SUPER_MAGIC:
3357 /*
3358 * devpts seems content with the label of the task.
3359 * Programs that change smack have to treat the
3360 * pty with respect.
3361 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003362 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003363 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003364 case PROC_SUPER_MAGIC:
3365 /*
3366 * Casey says procfs appears not to care.
3367 * The superblock default suffices.
3368 */
3369 break;
3370 case TMPFS_MAGIC:
3371 /*
3372 * Device labels should come from the filesystem,
3373 * but watch out, because they're volitile,
3374 * getting recreated on every reboot.
3375 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003376 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003377 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003378 * If a smack value has been set we want to use it,
3379 * but since tmpfs isn't giving us the opportunity
3380 * to set mount options simulate setting the
3381 * superblock default.
3382 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003383 fallthrough;
Casey Schauflere114e472008-02-04 22:29:50 -08003384 default:
3385 /*
3386 * This isn't an understood special case.
3387 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003388 */
3389
3390 /*
3391 * UNIX domain sockets use lower level socket data.
3392 */
3393 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003394 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003395 break;
3396 }
3397 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003398 * No xattr support means, alas, no SMACK label.
3399 * Use the aforeapplied default.
3400 * It would be curious if the label of the task
3401 * does not match that assigned.
3402 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003403 if (!(inode->i_opflags & IOP_XATTR))
3404 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003405 /*
3406 * Get the dentry for xattr.
3407 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003408 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003409 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003410 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003411 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003412
3413 /*
3414 * Transmuting directory
3415 */
3416 if (S_ISDIR(inode->i_mode)) {
3417 /*
3418 * If this is a new directory and the label was
3419 * transmuted when the inode was initialized
3420 * set the transmute attribute on the directory
3421 * and mark the inode.
3422 *
3423 * If there is a transmute attribute on the
3424 * directory mark the inode.
3425 */
3426 if (isp->smk_flags & SMK_INODE_CHANGED) {
3427 isp->smk_flags &= ~SMK_INODE_CHANGED;
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01003428 rc = __vfs_setxattr(&init_user_ns, dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003429 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003430 TRANS_TRUE, TRANS_TRUE_SIZE,
3431 0);
3432 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003433 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003434 XATTR_NAME_SMACKTRANSMUTE, trattr,
3435 TRANS_TRUE_SIZE);
3436 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3437 TRANS_TRUE_SIZE) != 0)
3438 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003439 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003440 if (rc >= 0)
3441 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003442 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003443 /*
3444 * Don't let the exec or mmap label be "*" or "@".
3445 */
3446 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3447 if (IS_ERR(skp) || skp == &smack_known_star ||
3448 skp == &smack_known_web)
3449 skp = NULL;
3450 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003451
Casey Schaufler19760ad2013-12-16 16:27:26 -08003452 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003453 if (IS_ERR(skp) || skp == &smack_known_star ||
3454 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003455 skp = NULL;
3456 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003457
Casey Schauflere114e472008-02-04 22:29:50 -08003458 dput(dp);
3459 break;
3460 }
3461
3462 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003463 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003464 else
3465 isp->smk_inode = final;
3466
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003467 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003468
Casey Schauflere114e472008-02-04 22:29:50 -08003469 return;
3470}
3471
3472/**
3473 * smack_getprocattr - Smack process attribute access
3474 * @p: the object task
3475 * @name: the name of the attribute in /proc/.../attr
3476 * @value: where to put the result
3477 *
3478 * Places a copy of the task Smack into value
3479 *
3480 * Returns the length of the smack label or an error code
3481 */
3482static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3483{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003484 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003485 char *cp;
3486 int slen;
3487
3488 if (strcmp(name, "current") != 0)
3489 return -EINVAL;
3490
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003491 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003492 if (cp == NULL)
3493 return -ENOMEM;
3494
3495 slen = strlen(cp);
3496 *value = cp;
3497 return slen;
3498}
3499
3500/**
3501 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003502 * @name: the name of the attribute in /proc/.../attr
3503 * @value: the value to set
3504 * @size: the size of the value
3505 *
3506 * Sets the Smack value of the task. Only setting self
3507 * is permitted and only with privilege
3508 *
3509 * Returns the length of the smack label or an error code
3510 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003511static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003512{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003513 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003514 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003515 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003516 struct smack_known_list_elem *sklep;
3517 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003518
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003519 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003520 return -EPERM;
3521
Casey Schauflerf7112e62012-05-06 15:22:02 -07003522 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003523 return -EINVAL;
3524
3525 if (strcmp(name, "current") != 0)
3526 return -EINVAL;
3527
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003528 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003529 if (IS_ERR(skp))
3530 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003531
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003532 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303533 * No process is ever allowed the web ("@") label
3534 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003535 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303536 if (skp == &smack_known_web || skp == &smack_known_star)
3537 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003538
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003539 if (!smack_privileged(CAP_MAC_ADMIN)) {
3540 rc = -EPERM;
3541 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3542 if (sklep->smk_label == skp) {
3543 rc = 0;
3544 break;
3545 }
3546 if (rc)
3547 return rc;
3548 }
3549
David Howellsd84f4f92008-11-14 10:39:23 +11003550 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003551 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003552 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003553
Casey Schauflerb17103a2018-11-09 16:12:56 -08003554 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003555 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003556 /*
3557 * process can change its label only once
3558 */
3559 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003560
David Howellsd84f4f92008-11-14 10:39:23 +11003561 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003562 return size;
3563}
3564
3565/**
3566 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003567 * @sock: one sock
3568 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003569 * @newsk: unused
3570 *
3571 * Return 0 if a subject with the smack of sock could access
3572 * an object with the smack of other, otherwise an error code
3573 */
David S. Miller3610cda2011-01-05 15:38:53 -08003574static int smack_unix_stream_connect(struct sock *sock,
3575 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003576{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003577 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003578 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003579 struct socket_smack *ssp = sock->sk_security;
3580 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003581 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003582 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003583 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003584#ifdef CONFIG_AUDIT
3585 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003586#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003587
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003588 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3589 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003590 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003591#ifdef CONFIG_AUDIT
3592 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3593 smk_ad_setfield_u_net_sk(&ad, other);
3594#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003595 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3596 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003597 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003598 okp = osp->smk_out;
3599 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003600 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003601 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003602 MAY_WRITE, rc);
3603 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003604 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003605
Casey Schaufler975d5e52011-09-26 14:43:39 -07003606 /*
3607 * Cross reference the peer labels for SO_PEERSEC.
3608 */
3609 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003610 nsp->smk_packet = ssp->smk_out;
3611 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003612 }
3613
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003614 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003615}
3616
3617/**
3618 * smack_unix_may_send - Smack access on UDS
3619 * @sock: one socket
3620 * @other: the other socket
3621 *
3622 * Return 0 if a subject with the smack of sock could access
3623 * an object with the smack of other, otherwise an error code
3624 */
3625static int smack_unix_may_send(struct socket *sock, struct socket *other)
3626{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003627 struct socket_smack *ssp = sock->sk->sk_security;
3628 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003629 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003630 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003631
Kees Cook923e9a12012-04-10 13:26:44 -07003632#ifdef CONFIG_AUDIT
3633 struct lsm_network_audit net;
3634
Eric Paris48c62af2012-04-02 13:15:44 -04003635 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003636 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003637#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003638
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003639 if (smack_privileged(CAP_MAC_OVERRIDE))
3640 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003641
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003642 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3643 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003644 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003645}
3646
3647/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003648 * smack_socket_sendmsg - Smack check based on destination host
3649 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003650 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003651 * @size: the size of the message
3652 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003653 * Return 0 if the current subject can write to the destination host.
3654 * For IPv4 this is only a question if the destination is a single label host.
3655 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003656 */
3657static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3658 int size)
3659{
3660 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003661#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003662 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003663#endif
3664#ifdef SMACK_IPV6_SECMARK_LABELING
3665 struct socket_smack *ssp = sock->sk->sk_security;
3666 struct smack_known *rsp;
3667#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003668 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003669
3670 /*
3671 * Perfectly reasonable for this to be NULL
3672 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003673 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003674 return 0;
3675
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003676 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003677 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003678 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3679 sip->sin_family != AF_INET)
3680 return -EINVAL;
Casey Schauflera2af0312020-08-11 17:39:42 -07003681 rc = smk_ipv4_check(sock->sk, sip);
Casey Schauflerc6739442013-05-22 18:42:56 -07003682 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003683#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003684 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003685 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3686 sap->sin6_family != AF_INET6)
3687 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003688#ifdef SMACK_IPV6_SECMARK_LABELING
3689 rsp = smack_ipv6host_label(sap);
3690 if (rsp != NULL)
3691 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3692 SMK_CONNECTING);
3693#endif
3694#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003695 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003696#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003697#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003698 break;
3699 }
3700 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003701}
3702
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003703/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003704 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003705 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003706 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003707 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003708 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003709 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003710static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3711 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003712{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003713 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003714 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003715 int acat;
3716 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003717
Casey Schaufler322dd632020-08-11 17:39:43 -07003718 /*
3719 * Netlabel found it in the cache.
3720 */
3721 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3722 return (struct smack_known *)sap->cache->data;
3723
3724 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3725 /*
3726 * Looks like a fallback, which gives us a secid.
3727 */
3728 return smack_from_secid(sap->attr.secid);
3729
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003730 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003731 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003732 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003733 * If there are flags but no level netlabel isn't
3734 * behaving the way we expect it to.
3735 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003736 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003737 * Without guidance regarding the smack value
3738 * for the packet fall back on the network
3739 * ambient value.
3740 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003741 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303742 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003743 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003744 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003745 /*
3746 * Compare the catsets. Use the netlbl APIs.
3747 */
3748 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3749 if ((skp->smk_netlabel.flags &
3750 NETLBL_SECATTR_MLS_CAT) == 0)
3751 found = 1;
3752 break;
3753 }
3754 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003755 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3756 acat + 1);
3757 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003758 skp->smk_netlabel.attr.mls.cat,
3759 kcat + 1);
3760 if (acat < 0 || kcat < 0)
3761 break;
3762 }
3763 if (acat == kcat) {
3764 found = 1;
3765 break;
3766 }
Casey Schauflere114e472008-02-04 22:29:50 -08003767 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003768 rcu_read_unlock();
3769
3770 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003771 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003772
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003773 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003774 return &smack_known_web;
3775 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003776 }
Casey Schauflere114e472008-02-04 22:29:50 -08003777 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003778 * Without guidance regarding the smack value
3779 * for the packet fall back on the network
3780 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003781 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003782 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003783}
3784
Casey Schaufler69f287a2014-12-12 17:08:40 -08003785#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003786static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003787{
Casey Schauflerc6739442013-05-22 18:42:56 -07003788 u8 nexthdr;
3789 int offset;
3790 int proto = -EINVAL;
3791 struct ipv6hdr _ipv6h;
3792 struct ipv6hdr *ip6;
3793 __be16 frag_off;
3794 struct tcphdr _tcph, *th;
3795 struct udphdr _udph, *uh;
3796 struct dccp_hdr _dccph, *dh;
3797
3798 sip->sin6_port = 0;
3799
3800 offset = skb_network_offset(skb);
3801 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3802 if (ip6 == NULL)
3803 return -EINVAL;
3804 sip->sin6_addr = ip6->saddr;
3805
3806 nexthdr = ip6->nexthdr;
3807 offset += sizeof(_ipv6h);
3808 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3809 if (offset < 0)
3810 return -EINVAL;
3811
3812 proto = nexthdr;
3813 switch (proto) {
3814 case IPPROTO_TCP:
3815 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3816 if (th != NULL)
3817 sip->sin6_port = th->source;
3818 break;
3819 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003820 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003821 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3822 if (uh != NULL)
3823 sip->sin6_port = uh->source;
3824 break;
3825 case IPPROTO_DCCP:
3826 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3827 if (dh != NULL)
3828 sip->sin6_port = dh->dccph_sport;
3829 break;
3830 }
3831 return proto;
3832}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003833#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003834
Casey Schauflere114e472008-02-04 22:29:50 -08003835/**
Casey Schaufler36be8122020-08-11 17:39:41 -07003836 * smack_from_skb - Smack data from the secmark in an skb
3837 * @skb: packet
3838 *
3839 * Returns smack_known of the secmark or NULL if that won't work.
3840 */
Casey Schauflerbf0afe62020-09-22 14:59:31 -07003841#ifdef CONFIG_NETWORK_SECMARK
Casey Schaufler36be8122020-08-11 17:39:41 -07003842static struct smack_known *smack_from_skb(struct sk_buff *skb)
3843{
3844 if (skb == NULL || skb->secmark == 0)
3845 return NULL;
3846
3847 return smack_from_secid(skb->secmark);
3848}
Casey Schauflerbf0afe62020-09-22 14:59:31 -07003849#else
3850static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3851{
3852 return NULL;
3853}
3854#endif
Casey Schaufler36be8122020-08-11 17:39:41 -07003855
3856/**
Casey Schauflera2af0312020-08-11 17:39:42 -07003857 * smack_from_netlbl - Smack data from the IP options in an skb
3858 * @sk: socket data came in on
3859 * @family: address family
3860 * @skb: packet
3861 *
Casey Schaufler322dd632020-08-11 17:39:43 -07003862 * Find the Smack label in the IP options. If it hasn't been
3863 * added to the netlabel cache, add it here.
3864 *
Casey Schauflera2af0312020-08-11 17:39:42 -07003865 * Returns smack_known of the IP options or NULL if that won't work.
3866 */
Florian Westphal41dd9592020-11-30 16:36:29 +01003867static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
Casey Schauflera2af0312020-08-11 17:39:42 -07003868 struct sk_buff *skb)
3869{
3870 struct netlbl_lsm_secattr secattr;
3871 struct socket_smack *ssp = NULL;
3872 struct smack_known *skp = NULL;
3873
3874 netlbl_secattr_init(&secattr);
3875
3876 if (sk)
3877 ssp = sk->sk_security;
Casey Schaufler322dd632020-08-11 17:39:43 -07003878
3879 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
Casey Schauflera2af0312020-08-11 17:39:42 -07003880 skp = smack_from_secattr(&secattr, ssp);
Casey Schaufler322dd632020-08-11 17:39:43 -07003881 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
Alex Shi9b0072e2020-11-08 14:45:42 +08003882 netlbl_cache_add(skb, family, &skp->smk_netlabel);
Casey Schaufler322dd632020-08-11 17:39:43 -07003883 }
Casey Schauflera2af0312020-08-11 17:39:42 -07003884
3885 netlbl_secattr_destroy(&secattr);
3886
3887 return skp;
3888}
3889
3890/**
Casey Schauflere114e472008-02-04 22:29:50 -08003891 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3892 * @sk: socket
3893 * @skb: packet
3894 *
3895 * Returns 0 if the packet should be delivered, an error code otherwise
3896 */
3897static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3898{
Casey Schauflere114e472008-02-04 22:29:50 -08003899 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003900 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003901 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003902 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003903 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003904#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003905 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003906#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003907#if IS_ENABLED(CONFIG_IPV6)
3908 struct sockaddr_in6 sadd;
3909 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003910
3911 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3912 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003913#endif /* CONFIG_IPV6 */
3914
Piotr Sawicki129a9982018-07-19 11:42:58 +02003915 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003916 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003917 /*
3918 * If there is a secmark use it rather than the CIPSO label.
3919 * If there is no secmark fall back to CIPSO.
3920 * The secmark is assumed to reflect policy better.
3921 */
Casey Schaufler36be8122020-08-11 17:39:41 -07003922 skp = smack_from_skb(skb);
Casey Schauflera2af0312020-08-11 17:39:42 -07003923 if (skp == NULL) {
3924 skp = smack_from_netlbl(sk, family, skb);
3925 if (skp == NULL)
3926 skp = smack_net_ambient;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003927 }
Casey Schauflere114e472008-02-04 22:29:50 -08003928
Etienne Bassetecfcc532009-04-08 20:40:06 +02003929#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003930 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003931 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003932 ad.a.u.net->netif = skb->skb_iif;
3933 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003934#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003935 /*
3936 * Receiving a packet requires that the other end
3937 * be able to write here. Read access is not required.
3938 * This is the simplist possible security model
3939 * for networking.
3940 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003941 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3942 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003943 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003944 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003945 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003946 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003947#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003948 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003949 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003950 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3951 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003952 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003953#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler36be8122020-08-11 17:39:41 -07003954 skp = smack_from_skb(skb);
3955 if (skp == NULL) {
3956 if (smk_ipv6_localhost(&sadd))
3957 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003958 skp = smack_ipv6host_label(&sadd);
Casey Schaufler36be8122020-08-11 17:39:41 -07003959 if (skp == NULL)
3960 skp = smack_net_ambient;
3961 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003962#ifdef CONFIG_AUDIT
3963 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003964 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003965 ad.a.u.net->netif = skb->skb_iif;
3966 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3967#endif /* CONFIG_AUDIT */
3968 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3969 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3970 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003971#endif /* SMACK_IPV6_SECMARK_LABELING */
3972#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003973 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003974#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003975 if (rc != 0)
3976 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3977 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003978 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003979#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003980 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003981
Paul Moorea8134292008-10-10 10:16:31 -04003982 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003983}
3984
3985/**
3986 * smack_socket_getpeersec_stream - pull in packet label
3987 * @sock: the socket
3988 * @optval: user's destination
3989 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003990 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003991 *
3992 * returns zero on success, an error code otherwise
3993 */
3994static int smack_socket_getpeersec_stream(struct socket *sock,
3995 char __user *optval,
3996 int __user *optlen, unsigned len)
3997{
3998 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003999 char *rcp = "";
4000 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004001 int rc = 0;
4002
4003 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004004 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004005 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004006 slen = strlen(rcp) + 1;
4007 }
Casey Schauflere114e472008-02-04 22:29:50 -08004008
4009 if (slen > len)
4010 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004011 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004012 rc = -EFAULT;
4013
4014 if (put_user(slen, optlen) != 0)
4015 rc = -EFAULT;
4016
4017 return rc;
4018}
4019
4020
4021/**
4022 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004023 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004024 * @skb: packet data
4025 * @secid: pointer to where to put the secid of the packet
4026 *
4027 * Sets the netlabel socket state on sk from parent
4028 */
4029static int smack_socket_getpeersec_dgram(struct socket *sock,
4030 struct sk_buff *skb, u32 *secid)
4031
4032{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004033 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004034 struct smack_known *skp;
Casey Schauflera2af0312020-08-11 17:39:42 -07004035 struct sock *sk = NULL;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004036 int family = PF_UNSPEC;
4037 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004038
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004039 if (skb != NULL) {
4040 if (skb->protocol == htons(ETH_P_IP))
4041 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004042#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004043 else if (skb->protocol == htons(ETH_P_IPV6))
4044 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004045#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004046 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004047 if (family == PF_UNSPEC && sock != NULL)
4048 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004049
Casey Schaufler69f287a2014-12-12 17:08:40 -08004050 switch (family) {
4051 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004052 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004053 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004054 break;
4055 case PF_INET:
Casey Schaufler36be8122020-08-11 17:39:41 -07004056 skp = smack_from_skb(skb);
4057 if (skp) {
4058 s = skp->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004059 break;
Casey Schaufler36be8122020-08-11 17:39:41 -07004060 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004061 /*
4062 * Translate what netlabel gave us.
4063 */
Casey Schauflera2af0312020-08-11 17:39:42 -07004064 if (sock != NULL)
4065 sk = sock->sk;
4066 skp = smack_from_netlbl(sk, family, skb);
4067 if (skp != NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004068 s = skp->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004069 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004070 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004071#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler36be8122020-08-11 17:39:41 -07004072 skp = smack_from_skb(skb);
4073 if (skp)
4074 s = skp->smk_secid;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004075#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004076 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004077 }
4078 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004079 if (s == 0)
4080 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004081 return 0;
4082}
4083
4084/**
Paul Moore07feee82009-03-27 17:10:54 -04004085 * smack_sock_graft - Initialize a newly created socket with an existing sock
4086 * @sk: child sock
4087 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004088 *
Paul Moore07feee82009-03-27 17:10:54 -04004089 * Set the smk_{in,out} state of an existing sock based on the process that
4090 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004091 */
4092static void smack_sock_graft(struct sock *sk, struct socket *parent)
4093{
4094 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004095 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004096
Paul Moore07feee82009-03-27 17:10:54 -04004097 if (sk == NULL ||
4098 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004099 return;
4100
4101 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004102 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004103 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004104 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004105}
4106
4107/**
4108 * smack_inet_conn_request - Smack access check on connect
4109 * @sk: socket involved
4110 * @skb: packet
4111 * @req: unused
4112 *
4113 * Returns 0 if a task with the packet label could write to
4114 * the socket, otherwise an error code
4115 */
Florian Westphal41dd9592020-11-30 16:36:29 +01004116static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
Casey Schauflere114e472008-02-04 22:29:50 -08004117 struct request_sock *req)
4118{
Paul Moore07feee82009-03-27 17:10:54 -04004119 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004120 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004121 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004122 struct sockaddr_in addr;
4123 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004124 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004125 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004126 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004127#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004128 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004129#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004130
Casey Schaufler69f287a2014-12-12 17:08:40 -08004131#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004132 if (family == PF_INET6) {
4133 /*
4134 * Handle mapped IPv4 packets arriving
4135 * via IPv6 sockets. Don't set up netlabel
4136 * processing on IPv6.
4137 */
4138 if (skb->protocol == htons(ETH_P_IP))
4139 family = PF_INET;
4140 else
4141 return 0;
4142 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004143#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004144
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004145 /*
4146 * If there is a secmark use it rather than the CIPSO label.
4147 * If there is no secmark fall back to CIPSO.
4148 * The secmark is assumed to reflect policy better.
4149 */
Casey Schaufler36be8122020-08-11 17:39:41 -07004150 skp = smack_from_skb(skb);
Casey Schauflera2af0312020-08-11 17:39:42 -07004151 if (skp == NULL) {
4152 skp = smack_from_netlbl(sk, family, skb);
4153 if (skp == NULL)
4154 skp = &smack_known_huh;
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004155 }
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004156
Etienne Bassetecfcc532009-04-08 20:40:06 +02004157#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004158 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4159 ad.a.u.net->family = family;
4160 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004161 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4162#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004163 /*
Paul Moore07feee82009-03-27 17:10:54 -04004164 * Receiving a packet requires that the other end be able to write
4165 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004166 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004167 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4168 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004169 if (rc != 0)
4170 return rc;
4171
4172 /*
4173 * Save the peer's label in the request_sock so we can later setup
4174 * smk_packet in the child socket so that SO_PEERCRED can report it.
4175 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004176 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004177
4178 /*
4179 * We need to decide if we want to label the incoming connection here
4180 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004181 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004182 */
4183 hdr = ip_hdr(skb);
4184 addr.sin_addr.s_addr = hdr->saddr;
4185 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004186 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004187 rcu_read_unlock();
4188
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004189 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004190 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004191 else
Paul Moore07feee82009-03-27 17:10:54 -04004192 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004193
4194 return rc;
4195}
4196
Paul Moore07feee82009-03-27 17:10:54 -04004197/**
4198 * smack_inet_csk_clone - Copy the connection information to the new socket
4199 * @sk: the new socket
4200 * @req: the connection's request_sock
4201 *
4202 * Transfer the connection's peer label to the newly created socket.
4203 */
4204static void smack_inet_csk_clone(struct sock *sk,
4205 const struct request_sock *req)
4206{
4207 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004208 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004209
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004210 if (req->peer_secid != 0) {
4211 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004212 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004213 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004214 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004215}
4216
Casey Schauflere114e472008-02-04 22:29:50 -08004217/*
4218 * Key management security hooks
4219 *
4220 * Casey has not tested key support very heavily.
4221 * The permission check is most likely too restrictive.
4222 * If you care about keys please have a look.
4223 */
4224#ifdef CONFIG_KEYS
4225
4226/**
4227 * smack_key_alloc - Set the key security blob
4228 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004229 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004230 * @flags: unused
4231 *
4232 * No allocation required
4233 *
4234 * Returns 0
4235 */
David Howellsd84f4f92008-11-14 10:39:23 +11004236static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004237 unsigned long flags)
4238{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004239 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004240
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004241 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004242 return 0;
4243}
4244
4245/**
4246 * smack_key_free - Clear the key security blob
4247 * @key: the object
4248 *
4249 * Clear the blob pointer
4250 */
4251static void smack_key_free(struct key *key)
4252{
4253 key->security = NULL;
4254}
4255
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004256/**
Casey Schauflere114e472008-02-04 22:29:50 -08004257 * smack_key_permission - Smack access on a key
4258 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004259 * @cred: the credentials to use
David Howells8c0637e2020-05-12 15:16:29 +01004260 * @need_perm: requested key permission
Casey Schauflere114e472008-02-04 22:29:50 -08004261 *
4262 * Return 0 if the task has read and write to the object,
4263 * an error code otherwise
4264 */
4265static int smack_key_permission(key_ref_t key_ref,
David Howells8c0637e2020-05-12 15:16:29 +01004266 const struct cred *cred,
4267 enum key_need_perm need_perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004268{
4269 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004270 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004271 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004272 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004273 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004274
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004275 /*
4276 * Validate requested permissions
4277 */
David Howells8c0637e2020-05-12 15:16:29 +01004278 switch (need_perm) {
4279 case KEY_NEED_READ:
4280 case KEY_NEED_SEARCH:
4281 case KEY_NEED_VIEW:
4282 request |= MAY_READ;
4283 break;
4284 case KEY_NEED_WRITE:
4285 case KEY_NEED_LINK:
4286 case KEY_NEED_SETATTR:
4287 request |= MAY_WRITE;
4288 break;
4289 case KEY_NEED_UNSPECIFIED:
4290 case KEY_NEED_UNLINK:
4291 case KEY_SYSADMIN_OVERRIDE:
4292 case KEY_AUTHTOKEN_OVERRIDE:
4293 case KEY_DEFER_PERM_CHECK:
4294 return 0;
4295 default:
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004296 return -EINVAL;
David Howells8c0637e2020-05-12 15:16:29 +01004297 }
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004298
Casey Schauflere114e472008-02-04 22:29:50 -08004299 keyp = key_ref_to_ptr(key_ref);
4300 if (keyp == NULL)
4301 return -EINVAL;
4302 /*
4303 * If the key hasn't been initialized give it access so that
4304 * it may do so.
4305 */
4306 if (keyp->security == NULL)
4307 return 0;
4308 /*
4309 * This should not occur
4310 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004311 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004312 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004313
David Howellsa8478a62020-01-14 17:07:13 +00004314 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004315 return 0;
4316
Etienne Bassetecfcc532009-04-08 20:40:06 +02004317#ifdef CONFIG_AUDIT
4318 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4319 ad.a.u.key_struct.key = keyp->serial;
4320 ad.a.u.key_struct.key_desc = keyp->description;
4321#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07004322 rc = smk_access(tkp, keyp->security, request, &ad);
4323 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4324 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004325}
José Bollo7fc5f362015-02-17 15:41:22 +01004326
4327/*
4328 * smack_key_getsecurity - Smack label tagging the key
4329 * @key points to the key to be queried
4330 * @_buffer points to a pointer that should be set to point to the
4331 * resulting string (if no label or an error occurs).
4332 * Return the length of the string (including terminating NUL) or -ve if
4333 * an error.
4334 * May also return 0 (and a NULL buffer pointer) if there is no label.
4335 */
4336static int smack_key_getsecurity(struct key *key, char **_buffer)
4337{
4338 struct smack_known *skp = key->security;
4339 size_t length;
4340 char *copy;
4341
4342 if (key->security == NULL) {
4343 *_buffer = NULL;
4344 return 0;
4345 }
4346
4347 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4348 if (copy == NULL)
4349 return -ENOMEM;
4350 length = strlen(copy) + 1;
4351
4352 *_buffer = copy;
4353 return length;
4354}
4355
David Howellsa8478a62020-01-14 17:07:13 +00004356
4357#ifdef CONFIG_KEY_NOTIFICATIONS
4358/**
4359 * smack_watch_key - Smack access to watch a key for notifications.
4360 * @key: The key to be watched
4361 *
4362 * Return 0 if the @watch->cred has permission to read from the key object and
4363 * an error otherwise.
4364 */
4365static int smack_watch_key(struct key *key)
4366{
4367 struct smk_audit_info ad;
4368 struct smack_known *tkp = smk_of_current();
4369 int rc;
4370
4371 if (key == NULL)
4372 return -EINVAL;
4373 /*
4374 * If the key hasn't been initialized give it access so that
4375 * it may do so.
4376 */
4377 if (key->security == NULL)
4378 return 0;
4379 /*
4380 * This should not occur
4381 */
4382 if (tkp == NULL)
4383 return -EACCES;
4384
4385 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4386 return 0;
4387
4388#ifdef CONFIG_AUDIT
4389 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4390 ad.a.u.key_struct.key = key->serial;
4391 ad.a.u.key_struct.key_desc = key->description;
4392#endif
4393 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4394 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4395 return rc;
4396}
4397#endif /* CONFIG_KEY_NOTIFICATIONS */
Casey Schauflere114e472008-02-04 22:29:50 -08004398#endif /* CONFIG_KEYS */
4399
David Howellsa8478a62020-01-14 17:07:13 +00004400#ifdef CONFIG_WATCH_QUEUE
4401/**
4402 * smack_post_notification - Smack access to post a notification to a queue
4403 * @w_cred: The credentials of the watcher.
4404 * @cred: The credentials of the event source (may be NULL).
4405 * @n: The notification message to be posted.
4406 */
4407static int smack_post_notification(const struct cred *w_cred,
4408 const struct cred *cred,
4409 struct watch_notification *n)
4410{
4411 struct smk_audit_info ad;
4412 struct smack_known *subj, *obj;
4413 int rc;
4414
4415 /* Always let maintenance notifications through. */
4416 if (n->type == WATCH_TYPE_META)
4417 return 0;
4418
4419 if (!cred)
4420 return 0;
4421 subj = smk_of_task(smack_cred(cred));
4422 obj = smk_of_task(smack_cred(w_cred));
4423
4424 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4425 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4426 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4427 return rc;
4428}
4429#endif /* CONFIG_WATCH_QUEUE */
4430
Casey Schauflere114e472008-02-04 22:29:50 -08004431/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004432 * Smack Audit hooks
4433 *
4434 * Audit requires a unique representation of each Smack specific
4435 * rule. This unique representation is used to distinguish the
4436 * object to be audited from remaining kernel objects and also
4437 * works as a glue between the audit hooks.
4438 *
4439 * Since repository entries are added but never deleted, we'll use
4440 * the smack_known label address related to the given audit rule as
4441 * the needed unique representation. This also better fits the smack
4442 * model where nearly everything is a label.
4443 */
4444#ifdef CONFIG_AUDIT
4445
4446/**
4447 * smack_audit_rule_init - Initialize a smack audit rule
4448 * @field: audit rule fields given from user-space (audit.h)
4449 * @op: required testing operator (=, !=, >, <, ...)
4450 * @rulestr: smack label to be audited
4451 * @vrule: pointer to save our own audit rule representation
4452 *
4453 * Prepare to audit cases where (@field @op @rulestr) is true.
4454 * The label to be audited is created if necessay.
4455 */
4456static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4457{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004458 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004459 char **rule = (char **)vrule;
4460 *rule = NULL;
4461
4462 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4463 return -EINVAL;
4464
Al Viro5af75d82008-12-16 05:59:26 -05004465 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004466 return -EINVAL;
4467
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004468 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004469 if (IS_ERR(skp))
4470 return PTR_ERR(skp);
4471
4472 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004473
4474 return 0;
4475}
4476
4477/**
4478 * smack_audit_rule_known - Distinguish Smack audit rules
4479 * @krule: rule of interest, in Audit kernel representation format
4480 *
4481 * This is used to filter Smack rules from remaining Audit ones.
4482 * If it's proved that this rule belongs to us, the
4483 * audit_rule_match hook will be called to do the final judgement.
4484 */
4485static int smack_audit_rule_known(struct audit_krule *krule)
4486{
4487 struct audit_field *f;
4488 int i;
4489
4490 for (i = 0; i < krule->field_count; i++) {
4491 f = &krule->fields[i];
4492
4493 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4494 return 1;
4495 }
4496
4497 return 0;
4498}
4499
4500/**
4501 * smack_audit_rule_match - Audit given object ?
4502 * @secid: security id for identifying the object to test
4503 * @field: audit rule flags given from user-space
4504 * @op: required testing operator
4505 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004506 *
4507 * The core Audit hook. It's used to take the decision of
4508 * whether to audit or not to audit a given object.
4509 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004510static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004511{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004512 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004513 char *rule = vrule;
4514
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004515 if (unlikely(!rule)) {
4516 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004517 return -ENOENT;
4518 }
4519
4520 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4521 return 0;
4522
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004523 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004524
4525 /*
4526 * No need to do string comparisons. If a match occurs,
4527 * both pointers will point to the same smack_known
4528 * label.
4529 */
Al Viro5af75d82008-12-16 05:59:26 -05004530 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004531 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004532 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004533 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004534
4535 return 0;
4536}
4537
Casey Schaufler491a0b02016-01-26 15:08:35 -08004538/*
4539 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004540 * No memory was allocated.
4541 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004542
4543#endif /* CONFIG_AUDIT */
4544
Randy Dunlap251a2a92009-02-18 11:42:33 -08004545/**
David Quigley746df9b2013-05-22 12:50:35 -04004546 * smack_ismaclabel - check if xattr @name references a smack MAC label
4547 * @name: Full xattr name to check.
4548 */
4549static int smack_ismaclabel(const char *name)
4550{
4551 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4552}
4553
4554
4555/**
Casey Schauflere114e472008-02-04 22:29:50 -08004556 * smack_secid_to_secctx - return the smack label for a secid
4557 * @secid: incoming integer
4558 * @secdata: destination
4559 * @seclen: how long it is
4560 *
4561 * Exists for networking code.
4562 */
4563static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4564{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004565 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004566
Eric Parisd5630b92010-10-13 16:24:48 -04004567 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004568 *secdata = skp->smk_known;
4569 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004570 return 0;
4571}
4572
Randy Dunlap251a2a92009-02-18 11:42:33 -08004573/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004574 * smack_secctx_to_secid - return the secid for a smack label
4575 * @secdata: smack label
4576 * @seclen: how long result is
4577 * @secid: outgoing integer
4578 *
4579 * Exists for audit and networking code.
4580 */
David Howellse52c17642008-04-29 20:52:51 +01004581static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004582{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004583 struct smack_known *skp = smk_find_entry(secdata);
4584
4585 if (skp)
4586 *secid = skp->smk_secid;
4587 else
4588 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004589 return 0;
4590}
4591
Casey Schaufler491a0b02016-01-26 15:08:35 -08004592/*
4593 * There used to be a smack_release_secctx hook
4594 * that did nothing back when hooks were in a vector.
4595 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004596 */
Casey Schauflere114e472008-02-04 22:29:50 -08004597
David P. Quigley1ee65e32009-09-03 14:25:57 -04004598static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4599{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01004600 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4601 ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004602}
4603
4604static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4605{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01004606 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4607 ctx, ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004608}
4609
4610static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4611{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004612 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004613
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004614 *ctx = skp->smk_known;
4615 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004616 return 0;
4617}
4618
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004619static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4620{
4621
4622 struct task_smack *tsp;
4623 struct smack_known *skp;
4624 struct inode_smack *isp;
4625 struct cred *new_creds = *new;
4626
4627 if (new_creds == NULL) {
4628 new_creds = prepare_creds();
4629 if (new_creds == NULL)
4630 return -ENOMEM;
4631 }
4632
Casey Schauflerb17103a2018-11-09 16:12:56 -08004633 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004634
4635 /*
4636 * Get label from overlay inode and set it in create_sid
4637 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004638 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004639 skp = isp->smk_inode;
4640 tsp->smk_task = skp;
4641 *new = new_creds;
4642 return 0;
4643}
4644
4645static int smack_inode_copy_up_xattr(const char *name)
4646{
4647 /*
4648 * Return 1 if this is the smack access Smack attribute.
4649 */
4650 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4651 return 1;
4652
4653 return -EOPNOTSUPP;
4654}
4655
4656static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4657 struct qstr *name,
4658 const struct cred *old,
4659 struct cred *new)
4660{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004661 struct task_smack *otsp = smack_cred(old);
4662 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004663 struct inode_smack *isp;
4664 int may;
4665
4666 /*
4667 * Use the process credential unless all of
4668 * the transmuting criteria are met
4669 */
4670 ntsp->smk_task = otsp->smk_task;
4671
4672 /*
4673 * the attribute of the containing directory
4674 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004675 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004676
4677 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4678 rcu_read_lock();
4679 may = smk_access_entry(otsp->smk_task->smk_known,
4680 isp->smk_inode->smk_known,
4681 &otsp->smk_task->smk_rules);
4682 rcu_read_unlock();
4683
4684 /*
4685 * If the directory is transmuting and the rule
4686 * providing access is transmuting use the containing
4687 * directory label instead of the process label.
4688 */
4689 if (may > 0 && (may & MAY_TRANSMUTE))
4690 ntsp->smk_task = isp->smk_inode;
4691 }
4692 return 0;
4693}
4694
Casey Schauflerbbd36622018-11-12 09:30:56 -08004695struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4696 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004697 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004698 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004699 .lbs_ipc = sizeof(struct smack_known *),
4700 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004701};
4702
James Morrisca97d932017-02-15 00:18:51 +11004703static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004704 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4705 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4706 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004707
Al Viro0b520752018-12-23 16:02:47 -05004708 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004709 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4710
Casey Schauflere20b0432015-05-02 15:11:36 -07004711 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4712 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004713 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004714 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004715 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304716 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004717
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05004718 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004719
Casey Schauflere20b0432015-05-02 15:11:36 -07004720 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004721 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4722 LSM_HOOK_INIT(inode_link, smack_inode_link),
4723 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4724 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4725 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4726 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4727 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4728 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4729 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4730 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4731 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4732 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4733 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4734 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4735 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4736 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004737
Casey Schauflere20b0432015-05-02 15:11:36 -07004738 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004739 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4740 LSM_HOOK_INIT(file_lock, smack_file_lock),
4741 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4742 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4743 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4744 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4745 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4746 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004747
Casey Schauflere20b0432015-05-02 15:11:36 -07004748 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004749
Casey Schauflere20b0432015-05-02 15:11:36 -07004750 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4751 LSM_HOOK_INIT(cred_free, smack_cred_free),
4752 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4753 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004754 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004755 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4756 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4757 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4758 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4759 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4760 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4761 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4762 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4763 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4764 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4765 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4766 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4767 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004768 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004769
Casey Schauflere20b0432015-05-02 15:11:36 -07004770 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4771 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004772
Casey Schauflere20b0432015-05-02 15:11:36 -07004773 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004774
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004775 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004776 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4777 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4778 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4779 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004780
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004781 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004782 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4783 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4784 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004785
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004786 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004787 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4788 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4789 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004790
Casey Schauflere20b0432015-05-02 15:11:36 -07004791 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004792
Casey Schauflere20b0432015-05-02 15:11:36 -07004793 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4794 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004795
Casey Schauflere20b0432015-05-02 15:11:36 -07004796 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4797 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004798
Casey Schauflere20b0432015-05-02 15:11:36 -07004799 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004800 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004801#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004802 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004803#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004804 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4805 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4806 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4807 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4808 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4809 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4810 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4811 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4812 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4813 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004814
Casey Schauflere114e472008-02-04 22:29:50 -08004815 /* key management security hooks */
4816#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004817 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4818 LSM_HOOK_INIT(key_free, smack_key_free),
4819 LSM_HOOK_INIT(key_permission, smack_key_permission),
4820 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
David Howellsa8478a62020-01-14 17:07:13 +00004821#ifdef CONFIG_KEY_NOTIFICATIONS
4822 LSM_HOOK_INIT(watch_key, smack_watch_key),
4823#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004824#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004825
David Howellsa8478a62020-01-14 17:07:13 +00004826#ifdef CONFIG_WATCH_QUEUE
4827 LSM_HOOK_INIT(post_notification, smack_post_notification),
4828#endif
4829
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004830 /* Audit hooks */
4831#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004832 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4833 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4834 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004835#endif /* CONFIG_AUDIT */
4836
Casey Schauflere20b0432015-05-02 15:11:36 -07004837 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4838 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4839 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004840 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4841 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4842 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004843 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4844 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4845 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004846};
4847
Etienne Basset7198e2e2009-03-24 20:53:24 +01004848
Casey Schaufler86812bb2012-04-17 18:55:46 -07004849static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004850{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004851 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004852 * Initialize rule list locks
4853 */
4854 mutex_init(&smack_known_huh.smk_rules_lock);
4855 mutex_init(&smack_known_hat.smk_rules_lock);
4856 mutex_init(&smack_known_floor.smk_rules_lock);
4857 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004858 mutex_init(&smack_known_web.smk_rules_lock);
4859 /*
4860 * Initialize rule lists
4861 */
4862 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4863 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4864 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4865 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004866 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4867 /*
4868 * Create the known labels list
4869 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004870 smk_insert_entry(&smack_known_huh);
4871 smk_insert_entry(&smack_known_hat);
4872 smk_insert_entry(&smack_known_star);
4873 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004874 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004875}
4876
Casey Schauflere114e472008-02-04 22:29:50 -08004877/**
4878 * smack_init - initialize the smack system
4879 *
luanshia1a07f22019-07-05 10:35:20 +08004880 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004881 */
4882static __init int smack_init(void)
4883{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004884 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004885 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004886
Casey Schaufler4e328b02019-04-02 11:37:12 -07004887 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
Casey Schaufler4ca75282020-04-28 15:00:26 -07004888 if (!smack_rule_cache)
Casey Schaufler4e328b02019-04-02 11:37:12 -07004889 return -ENOMEM;
Casey Schaufler4e328b02019-04-02 11:37:12 -07004890
Casey Schauflerbbd36622018-11-12 09:30:56 -08004891 /*
4892 * Set the security state for the initial task.
4893 */
4894 tsp = smack_cred(cred);
4895 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4896
4897 /*
4898 * Register with LSM
4899 */
4900 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004901 smack_enabled = 1;
4902
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004903 pr_info("Smack: Initializing.\n");
4904#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4905 pr_info("Smack: Netfilter enabled.\n");
4906#endif
4907#ifdef SMACK_IPV6_PORT_LABELING
4908 pr_info("Smack: IPv6 port labeling enabled.\n");
4909#endif
4910#ifdef SMACK_IPV6_SECMARK_LABELING
4911 pr_info("Smack: IPv6 Netfilter enabled.\n");
4912#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004913
Casey Schaufler86812bb2012-04-17 18:55:46 -07004914 /* initialize the smack_known_list */
4915 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004916
Casey Schauflere114e472008-02-04 22:29:50 -08004917 return 0;
4918}
4919
4920/*
4921 * Smack requires early initialization in order to label
4922 * all processes and objects when they are created.
4923 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004924DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004925 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004926 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004927 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004928 .init = smack_init,
4929};