blob: cd14bec4ad800c157150cb15b64ca45b55e7573c [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());
Paul Moore1fb057d2021-02-19 15:04:58 -0500162 struct smack_known *smk_task = smk_of_task_struct_obj(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
Paul Moore1fb057d2021-02-19 15:04:58 -0500482 skp = smk_of_task_struct_obj(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 */
Christian Brauner71bc3562021-01-21 14:19:29 +01001243static int smack_inode_setxattr(struct user_namespace *mnt_userns,
1244 struct dentry *dentry, const char *name,
David Howells8f0cfa52008-04-29 00:59:41 -07001245 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001246{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001247 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001248 struct smack_known *skp;
1249 int check_priv = 0;
1250 int check_import = 0;
1251 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001252 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001253
Casey Schaufler19760ad2013-12-16 16:27:26 -08001254 /*
1255 * Check label validity here so import won't fail in post_setxattr
1256 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001257 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1258 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001259 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1260 check_priv = 1;
1261 check_import = 1;
1262 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1263 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1264 check_priv = 1;
1265 check_import = 1;
1266 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001267 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001268 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001269 if (size != TRANS_TRUE_SIZE ||
1270 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1271 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001272 } else
1273 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1274
Casey Schaufler19760ad2013-12-16 16:27:26 -08001275 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1276 rc = -EPERM;
1277
1278 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001279 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001280 if (IS_ERR(skp))
1281 rc = PTR_ERR(skp);
1282 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001283 (skp == &smack_known_star || skp == &smack_known_web)))
1284 rc = -EINVAL;
1285 }
1286
Eric Parisa2694342011-04-25 13:10:27 -04001287 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001288 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1289
Casey Schauflerd166c802014-08-27 14:51:27 -07001290 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001291 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1292 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001293 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001294
1295 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001296}
1297
1298/**
1299 * smack_inode_post_setxattr - Apply the Smack update approved above
1300 * @dentry: object
1301 * @name: attribute name
1302 * @value: attribute value
1303 * @size: attribute size
1304 * @flags: unused
1305 *
1306 * Set the pointer in the inode blob to the entry found
1307 * in the master label list.
1308 */
David Howells8f0cfa52008-04-29 00:59:41 -07001309static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1310 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001311{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001312 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001313 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001314
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001315 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1316 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1317 return;
1318 }
1319
Casey Schaufler676dac42010-12-02 06:43:39 -08001320 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001321 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001322 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001323 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001324 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001325 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001326 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001327 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001328 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001329 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001330 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001331 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001332 }
Casey Schauflere114e472008-02-04 22:29:50 -08001333
1334 return;
1335}
1336
Casey Schauflerce8a4322011-09-29 18:21:01 -07001337/**
Casey Schauflere114e472008-02-04 22:29:50 -08001338 * smack_inode_getxattr - Smack check on getxattr
1339 * @dentry: the object
1340 * @name: unused
1341 *
1342 * Returns 0 if access is permitted, an error code otherwise
1343 */
David Howells8f0cfa52008-04-29 00:59:41 -07001344static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001345{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001346 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001347 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001348
Eric Parisa2694342011-04-25 13:10:27 -04001349 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001350 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1351
David Howellsc6f493d2015-03-17 22:26:22 +00001352 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1353 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001354 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001355}
1356
Casey Schauflerce8a4322011-09-29 18:21:01 -07001357/**
Casey Schauflere114e472008-02-04 22:29:50 -08001358 * smack_inode_removexattr - Smack check on removexattr
1359 * @dentry: the object
1360 * @name: name of the attribute
1361 *
1362 * Removing the Smack attribute requires CAP_MAC_ADMIN
1363 *
1364 * Returns 0 if access is permitted, an error code otherwise
1365 */
Christian Brauner71bc3562021-01-21 14:19:29 +01001366static int smack_inode_removexattr(struct user_namespace *mnt_userns,
1367 struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001368{
Casey Schaufler676dac42010-12-02 06:43:39 -08001369 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001370 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001371 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001372
Casey Schauflerbcdca222008-02-23 15:24:04 -08001373 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1374 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001375 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001376 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001377 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301378 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001379 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001380 rc = -EPERM;
1381 } else
Christian Brauner71bc3562021-01-21 14:19:29 +01001382 rc = cap_inode_removexattr(mnt_userns, dentry, name);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001383
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001384 if (rc != 0)
1385 return rc;
1386
Eric Parisa2694342011-04-25 13:10:27 -04001387 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001388 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001389
David Howellsc6f493d2015-03-17 22:26:22 +00001390 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1391 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001392 if (rc != 0)
1393 return rc;
1394
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001395 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001396 /*
1397 * Don't do anything special for these.
1398 * XATTR_NAME_SMACKIPIN
1399 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001400 */
José Bollo80124952016-01-12 21:23:40 +01001401 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001402 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001403 struct superblock_smack *sbsp = sbp->s_security;
1404
1405 isp->smk_inode = sbsp->smk_default;
1406 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001407 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001408 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001409 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001410 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1411 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001412
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001413 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001414}
1415
1416/**
1417 * smack_inode_getsecurity - get smack xattrs
1418 * @inode: the object
1419 * @name: attribute name
1420 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001421 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001422 *
1423 * Returns the size of the attribute or an error code
1424 */
Christian Brauner71bc3562021-01-21 14:19:29 +01001425static int smack_inode_getsecurity(struct user_namespace *mnt_userns,
1426 struct inode *inode, const char *name,
1427 void **buffer, bool alloc)
Casey Schauflere114e472008-02-04 22:29:50 -08001428{
1429 struct socket_smack *ssp;
1430 struct socket *sock;
1431 struct super_block *sbp;
1432 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001433 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001434
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001435 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001436 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001437 else {
1438 /*
1439 * The rest of the Smack xattrs are only on sockets.
1440 */
1441 sbp = ip->i_sb;
1442 if (sbp->s_magic != SOCKFS_MAGIC)
1443 return -EOPNOTSUPP;
1444
1445 sock = SOCKET_I(ip);
1446 if (sock == NULL || sock->sk == NULL)
1447 return -EOPNOTSUPP;
1448
1449 ssp = sock->sk->sk_security;
1450
1451 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1452 isp = ssp->smk_in;
1453 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1454 isp = ssp->smk_out;
1455 else
1456 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001457 }
1458
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001459 if (alloc) {
1460 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1461 if (*buffer == NULL)
1462 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001463 }
1464
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001465 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001466}
1467
1468
1469/**
1470 * smack_inode_listsecurity - list the Smack attributes
1471 * @inode: the object
1472 * @buffer: where they go
1473 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001474 */
1475static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1476 size_t buffer_size)
1477{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001478 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001479
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001480 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001481 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001482
1483 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001484}
1485
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001486/**
1487 * smack_inode_getsecid - Extract inode's security id
1488 * @inode: inode to extract the info from
1489 * @secid: where result will be saved
1490 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001491static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001492{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001493 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001494
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001495 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001496}
1497
Casey Schauflere114e472008-02-04 22:29:50 -08001498/*
1499 * File Hooks
1500 */
1501
Casey Schaufler491a0b02016-01-26 15:08:35 -08001502/*
1503 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001504 *
1505 * Should access checks be done on each read or write?
1506 * UNICOS and SELinux say yes.
1507 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1508 *
1509 * I'll say no for now. Smack does not do the frequent
1510 * label changing that SELinux does.
1511 */
Casey Schauflere114e472008-02-04 22:29:50 -08001512
1513/**
1514 * smack_file_alloc_security - assign a file security blob
1515 * @file: the object
1516 *
1517 * The security blob for a file is a pointer to the master
1518 * label list, so no allocation is done.
1519 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001520 * f_security is the owner security information. It
1521 * isn't used on file access checks, it's for send_sigio.
1522 *
Casey Schauflere114e472008-02-04 22:29:50 -08001523 * Returns 0
1524 */
1525static int smack_file_alloc_security(struct file *file)
1526{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001527 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001528
Casey Schauflerf28952a2018-11-12 09:38:53 -08001529 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001530 return 0;
1531}
1532
1533/**
Casey Schauflere114e472008-02-04 22:29:50 -08001534 * smack_file_ioctl - Smack check on ioctls
1535 * @file: the object
1536 * @cmd: what to do
1537 * @arg: unused
1538 *
1539 * Relies heavily on the correct use of the ioctl command conventions.
1540 *
1541 * Returns 0 if allowed, error code otherwise
1542 */
1543static int smack_file_ioctl(struct file *file, unsigned int cmd,
1544 unsigned long arg)
1545{
1546 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001547 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001548 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001549
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001550 if (unlikely(IS_PRIVATE(inode)))
1551 return 0;
1552
Eric Parisf48b7392011-04-25 12:54:27 -04001553 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001554 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001555
Casey Schauflerd166c802014-08-27 14:51:27 -07001556 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001557 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001558 rc = smk_bu_file(file, MAY_WRITE, rc);
1559 }
Casey Schauflere114e472008-02-04 22:29:50 -08001560
Casey Schauflerd166c802014-08-27 14:51:27 -07001561 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001562 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001563 rc = smk_bu_file(file, MAY_READ, rc);
1564 }
Casey Schauflere114e472008-02-04 22:29:50 -08001565
1566 return rc;
1567}
1568
1569/**
1570 * smack_file_lock - Smack check on file locking
1571 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001572 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001573 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001574 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001575 */
1576static int smack_file_lock(struct file *file, unsigned int cmd)
1577{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001578 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001579 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001580 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001581
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001582 if (unlikely(IS_PRIVATE(inode)))
1583 return 0;
1584
Eric Paris92f42502011-04-25 13:15:55 -04001585 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1586 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001587 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001588 rc = smk_bu_file(file, MAY_LOCK, rc);
1589 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001590}
1591
1592/**
1593 * smack_file_fcntl - Smack check on fcntl
1594 * @file: the object
1595 * @cmd: what action to check
1596 * @arg: unused
1597 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001598 * Generally these operations are harmless.
1599 * File locking operations present an obvious mechanism
1600 * for passing information, so they require write access.
1601 *
Casey Schauflere114e472008-02-04 22:29:50 -08001602 * Returns 0 if current has access, error code otherwise
1603 */
1604static int smack_file_fcntl(struct file *file, unsigned int cmd,
1605 unsigned long arg)
1606{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001607 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001608 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001609 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001610
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001611 if (unlikely(IS_PRIVATE(inode)))
1612 return 0;
1613
Casey Schauflere114e472008-02-04 22:29:50 -08001614 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001615 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001616 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001617 case F_SETLK:
1618 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001619 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1620 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001621 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001622 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001623 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001624 case F_SETOWN:
1625 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001626 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1627 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001628 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001629 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001630 break;
1631 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001632 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001633 }
1634
1635 return rc;
1636}
1637
1638/**
Al Viroe5467852012-05-30 13:30:51 -04001639 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001640 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1641 * if mapping anonymous memory.
1642 * @file contains the file structure for file to map (may be NULL).
1643 * @reqprot contains the protection requested by the application.
1644 * @prot contains the protection that will be applied by the kernel.
1645 * @flags contains the operational flags.
1646 * Return 0 if permission is granted.
1647 */
Al Viroe5467852012-05-30 13:30:51 -04001648static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001649 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001650 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001651{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001652 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001653 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001654 struct smack_rule *srp;
1655 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001656 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001657 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001658 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001659 int may;
1660 int mmay;
1661 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001662 int rc;
1663
Al Viro496ad9a2013-01-23 17:07:38 -05001664 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001665 return 0;
1666
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001667 if (unlikely(IS_PRIVATE(file_inode(file))))
1668 return 0;
1669
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001670 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001671 if (isp->smk_mmap == NULL)
1672 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001673 sbsp = file_inode(file)->i_sb->s_security;
1674 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1675 isp->smk_mmap != sbsp->smk_root)
1676 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001677 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001678
Casey Schauflerb17103a2018-11-09 16:12:56 -08001679 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001680 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001681 rc = 0;
1682
1683 rcu_read_lock();
1684 /*
1685 * For each Smack rule associated with the subject
1686 * label verify that the SMACK64MMAP also has access
1687 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001688 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001689 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001690 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001691 /*
1692 * Matching labels always allows access.
1693 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001694 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001695 continue;
1696 /*
1697 * If there is a matching local rule take
1698 * that into account as well.
1699 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001700 may = smk_access_entry(srp->smk_subject->smk_known,
1701 okp->smk_known,
1702 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001703 if (may == -ENOENT)
1704 may = srp->smk_access;
1705 else
1706 may &= srp->smk_access;
1707 /*
1708 * If may is zero the SMACK64MMAP subject can't
1709 * possibly have less access.
1710 */
1711 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001712 continue;
1713
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001714 /*
1715 * Fetch the global list entry.
1716 * If there isn't one a SMACK64MMAP subject
1717 * can't have as much access as current.
1718 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001719 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1720 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001721 if (mmay == -ENOENT) {
1722 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001723 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001724 }
1725 /*
1726 * If there is a local entry it modifies the
1727 * potential access, too.
1728 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001729 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1730 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001731 if (tmay != -ENOENT)
1732 mmay &= tmay;
1733
1734 /*
1735 * If there is any access available to current that is
1736 * not available to a SMACK64MMAP subject
1737 * deny access.
1738 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001739 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001740 rc = -EACCES;
1741 break;
1742 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001743 }
1744
1745 rcu_read_unlock();
1746
1747 return rc;
1748}
1749
1750/**
Casey Schauflere114e472008-02-04 22:29:50 -08001751 * smack_file_set_fowner - set the file security blob value
1752 * @file: object in question
1753 *
Casey Schauflere114e472008-02-04 22:29:50 -08001754 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001755static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001756{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001757 struct smack_known **blob = smack_file(file);
1758
1759 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001760}
1761
1762/**
1763 * smack_file_send_sigiotask - Smack on sigio
1764 * @tsk: The target task
1765 * @fown: the object the signal come from
1766 * @signum: unused
1767 *
1768 * Allow a privileged task to get signals even if it shouldn't
1769 *
1770 * Returns 0 if a subject with the object's smack could
1771 * write to the task, an error code otherwise.
1772 */
1773static int smack_file_send_sigiotask(struct task_struct *tsk,
1774 struct fown_struct *fown, int signum)
1775{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001776 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001777 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001778 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001779 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001780 struct file *file;
1781 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001782 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001783
1784 /*
1785 * struct fown_struct is never outside the context of a struct file
1786 */
1787 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001788
Etienne Bassetecfcc532009-04-08 20:40:06 +02001789 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001790 blob = smack_file(file);
1791 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001792 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1793 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001794
1795 rcu_read_lock();
1796 tcred = __task_cred(tsk);
1797 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001798 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001799 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001800
1801 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1802 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001803 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001804 return rc;
1805}
1806
1807/**
1808 * smack_file_receive - Smack file receive check
1809 * @file: the object
1810 *
1811 * Returns 0 if current has access, error code otherwise
1812 */
1813static int smack_file_receive(struct file *file)
1814{
Casey Schauflerd166c802014-08-27 14:51:27 -07001815 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001816 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001817 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001818 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001819 struct socket *sock;
1820 struct task_smack *tsp;
1821 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001822
Seung-Woo Kim97775822015-04-17 15:25:04 +09001823 if (unlikely(IS_PRIVATE(inode)))
1824 return 0;
1825
Casey Schaufler4482a442013-12-30 17:37:45 -08001826 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001827 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001828
Casey Schaufler51d59af2017-05-31 08:53:42 -07001829 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001830 sock = SOCKET_I(inode);
1831 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001832 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001833 /*
1834 * If the receiving process can't write to the
1835 * passed socket or if the passed socket can't
1836 * write to the receiving process don't accept
1837 * the passed socket.
1838 */
1839 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1840 rc = smk_bu_file(file, may, rc);
1841 if (rc < 0)
1842 return rc;
1843 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1844 rc = smk_bu_file(file, may, rc);
1845 return rc;
1846 }
Casey Schauflere114e472008-02-04 22:29:50 -08001847 /*
1848 * This code relies on bitmasks.
1849 */
1850 if (file->f_mode & FMODE_READ)
1851 may = MAY_READ;
1852 if (file->f_mode & FMODE_WRITE)
1853 may |= MAY_WRITE;
1854
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001855 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001856 rc = smk_bu_file(file, may, rc);
1857 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001858}
1859
Casey Schaufler531f1d42011-09-19 12:41:42 -07001860/**
Eric Paris83d49852012-04-04 13:45:40 -04001861 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001862 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001863 *
1864 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001865 * Allow the open only if the task has read access. There are
1866 * many read operations (e.g. fstat) that you can do with an
1867 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001868 *
luanshia1a07f22019-07-05 10:35:20 +08001869 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001870 */
Al Viro94817692018-07-10 14:13:18 -04001871static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001872{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001873 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001874 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001875 struct smk_audit_info ad;
1876 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001877
Casey Schauflera6834c02014-04-21 11:10:26 -07001878 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1879 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301880 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001881 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001882
1883 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001884}
1885
Casey Schauflere114e472008-02-04 22:29:50 -08001886/*
1887 * Task hooks
1888 */
1889
1890/**
David Howellsee18d642009-09-02 09:14:21 +01001891 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001892 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001893 * @gfp: the atomicity of any memory allocations
1894 *
1895 * Prepare a blank set of credentials for modification. This must allocate all
1896 * the memory the LSM module might require such that cred_transfer() can
1897 * complete without error.
1898 */
1899static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1900{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001901 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001902 return 0;
1903}
1904
1905
1906/**
David Howellsf1752ee2008-11-14 10:39:17 +11001907 * smack_cred_free - "free" task-level security credentials
1908 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001909 *
Casey Schauflere114e472008-02-04 22:29:50 -08001910 */
David Howellsf1752ee2008-11-14 10:39:17 +11001911static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001912{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001913 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001914 struct smack_rule *rp;
1915 struct list_head *l;
1916 struct list_head *n;
1917
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001918 smk_destroy_label_list(&tsp->smk_relabel);
1919
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001920 list_for_each_safe(l, n, &tsp->smk_rules) {
1921 rp = list_entry(l, struct smack_rule, list);
1922 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001923 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001924 }
Casey Schauflere114e472008-02-04 22:29:50 -08001925}
1926
1927/**
David Howellsd84f4f92008-11-14 10:39:23 +11001928 * smack_cred_prepare - prepare new set of credentials for modification
1929 * @new: the new credentials
1930 * @old: the original credentials
1931 * @gfp: the atomicity of any memory allocations
1932 *
1933 * Prepare a new set of credentials for modification.
1934 */
1935static int smack_cred_prepare(struct cred *new, const struct cred *old,
1936 gfp_t gfp)
1937{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001938 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001939 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001940 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001941
Casey Schauflerbbd36622018-11-12 09:30:56 -08001942 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301943
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001944 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1945 if (rc != 0)
1946 return rc;
1947
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001948 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1949 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001950 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001951}
1952
Randy Dunlap251a2a92009-02-18 11:42:33 -08001953/**
David Howellsee18d642009-09-02 09:14:21 +01001954 * smack_cred_transfer - Transfer the old credentials to the new credentials
1955 * @new: the new credentials
1956 * @old: the original credentials
1957 *
1958 * Fill in a set of blank credentials from another set of credentials.
1959 */
1960static void smack_cred_transfer(struct cred *new, const struct cred *old)
1961{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001962 struct task_smack *old_tsp = smack_cred(old);
1963 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001964
1965 new_tsp->smk_task = old_tsp->smk_task;
1966 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001967 mutex_init(&new_tsp->smk_rules_lock);
1968 INIT_LIST_HEAD(&new_tsp->smk_rules);
1969
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001970 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001971}
1972
1973/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001974 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001975 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001976 * @secid: where to put the result
1977 *
1978 * Sets the secid to contain a u32 version of the smack label.
1979 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001980static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001981{
1982 struct smack_known *skp;
1983
1984 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001985 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001986 *secid = skp->smk_secid;
1987 rcu_read_unlock();
1988}
1989
1990/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001991 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001992 * @new: points to the set of credentials to be modified.
1993 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001994 *
1995 * Set the security data for a kernel service.
1996 */
1997static int smack_kernel_act_as(struct cred *new, u32 secid)
1998{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001999 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002000
Casey Schaufler152f91d2016-11-14 09:38:15 -08002001 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002002 return 0;
2003}
2004
2005/**
2006 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002007 * @new: points to the set of credentials to be modified
2008 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002009 *
2010 * Set the file creation context in a set of credentials to the same
2011 * as the objective context of the specified inode
2012 */
2013static int smack_kernel_create_files_as(struct cred *new,
2014 struct inode *inode)
2015{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002016 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002017 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002018
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002019 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002020 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002021 return 0;
2022}
2023
2024/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002025 * smk_curacc_on_task - helper to log task related access
2026 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002027 * @access: the access requested
2028 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002029 *
2030 * Return 0 if access is permitted
2031 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002032static int smk_curacc_on_task(struct task_struct *p, int access,
2033 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002034{
2035 struct smk_audit_info ad;
Paul Moore1fb057d2021-02-19 15:04:58 -05002036 struct smack_known *skp = smk_of_task_struct_subj(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002037 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002038
Casey Schaufler531f1d42011-09-19 12:41:42 -07002039 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002040 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002041 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002042 rc = smk_bu_task(p, access, rc);
2043 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002044}
2045
2046/**
Casey Schauflere114e472008-02-04 22:29:50 -08002047 * smack_task_setpgid - Smack check on setting pgid
2048 * @p: the task object
2049 * @pgid: unused
2050 *
2051 * Return 0 if write access is permitted
2052 */
2053static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2054{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002055 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002056}
2057
2058/**
2059 * smack_task_getpgid - Smack access check for getpgid
2060 * @p: the object task
2061 *
2062 * Returns 0 if current can read the object task, error code otherwise
2063 */
2064static int smack_task_getpgid(struct task_struct *p)
2065{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002066 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002067}
2068
2069/**
2070 * smack_task_getsid - Smack access check for getsid
2071 * @p: the object task
2072 *
2073 * Returns 0 if current can read the object task, error code otherwise
2074 */
2075static int smack_task_getsid(struct task_struct *p)
2076{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002077 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002078}
2079
2080/**
Paul Moore1fb057d2021-02-19 15:04:58 -05002081 * smack_task_getsecid_subj - get the subjective secid of the task
2082 * @p: the task
Casey Schauflere114e472008-02-04 22:29:50 -08002083 * @secid: where to put the result
2084 *
Paul Moore1fb057d2021-02-19 15:04:58 -05002085 * Sets the secid to contain a u32 version of the task's subjective smack label.
Casey Schauflere114e472008-02-04 22:29:50 -08002086 */
Paul Moore1fb057d2021-02-19 15:04:58 -05002087static void smack_task_getsecid_subj(struct task_struct *p, u32 *secid)
Casey Schauflere114e472008-02-04 22:29:50 -08002088{
Paul Moore1fb057d2021-02-19 15:04:58 -05002089 struct smack_known *skp = smk_of_task_struct_subj(p);
2090
2091 *secid = skp->smk_secid;
2092}
2093
2094/**
2095 * smack_task_getsecid_obj - get the objective secid of the task
2096 * @p: the task
2097 * @secid: where to put the result
2098 *
2099 * Sets the secid to contain a u32 version of the task's objective smack label.
2100 */
2101static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2102{
2103 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002104
2105 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002106}
2107
2108/**
2109 * smack_task_setnice - Smack check on setting nice
2110 * @p: the task object
2111 * @nice: unused
2112 *
2113 * Return 0 if write access is permitted
2114 */
2115static int smack_task_setnice(struct task_struct *p, int nice)
2116{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002117 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002118}
2119
2120/**
2121 * smack_task_setioprio - Smack check on setting ioprio
2122 * @p: the task object
2123 * @ioprio: unused
2124 *
2125 * Return 0 if write access is permitted
2126 */
2127static int smack_task_setioprio(struct task_struct *p, int ioprio)
2128{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002129 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002130}
2131
2132/**
2133 * smack_task_getioprio - Smack check on reading ioprio
2134 * @p: the task object
2135 *
2136 * Return 0 if read access is permitted
2137 */
2138static int smack_task_getioprio(struct task_struct *p)
2139{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002140 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002141}
2142
2143/**
2144 * smack_task_setscheduler - Smack check on setting scheduler
2145 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002146 *
2147 * Return 0 if read access is permitted
2148 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002149static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002150{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002151 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002152}
2153
2154/**
2155 * smack_task_getscheduler - Smack check on reading scheduler
2156 * @p: the task object
2157 *
2158 * Return 0 if read access is permitted
2159 */
2160static int smack_task_getscheduler(struct task_struct *p)
2161{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002162 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002163}
2164
2165/**
2166 * smack_task_movememory - Smack check on moving memory
2167 * @p: the task object
2168 *
2169 * Return 0 if write access is permitted
2170 */
2171static int smack_task_movememory(struct task_struct *p)
2172{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002173 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002174}
2175
2176/**
2177 * smack_task_kill - Smack check on signal delivery
2178 * @p: the task object
2179 * @info: unused
2180 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002181 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002182 *
2183 * Return 0 if write access is permitted
2184 *
Casey Schauflere114e472008-02-04 22:29:50 -08002185 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002186static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002187 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002188{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002189 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002190 struct smack_known *skp;
Paul Moore1fb057d2021-02-19 15:04:58 -05002191 struct smack_known *tkp = smk_of_task_struct_obj(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002192 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002193
Rafal Krypa18d872f2016-04-04 11:14:53 +02002194 if (!sig)
2195 return 0; /* null signal; existence test */
2196
Etienne Bassetecfcc532009-04-08 20:40:06 +02002197 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2198 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002199 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002200 * Sending a signal requires that the sender
2201 * can write the receiver.
2202 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002203 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002204 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2205 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002206 return rc;
2207 }
Casey Schauflere114e472008-02-04 22:29:50 -08002208 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002209 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002210 * specific behavior. This is not clean. For one thing
2211 * we can't take privilege into account.
2212 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002213 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002214 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2215 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002216 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002217}
2218
2219/**
Casey Schauflere114e472008-02-04 22:29:50 -08002220 * smack_task_to_inode - copy task smack into the inode blob
2221 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002222 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002223 *
2224 * Sets the smack pointer in the inode security blob
2225 */
2226static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2227{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002228 struct inode_smack *isp = smack_inode(inode);
Paul Moore1fb057d2021-02-19 15:04:58 -05002229 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002230
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002231 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002232 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002233}
2234
2235/*
2236 * Socket hooks.
2237 */
2238
2239/**
2240 * smack_sk_alloc_security - Allocate a socket blob
2241 * @sk: the socket
2242 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002243 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002244 *
2245 * Assign Smack pointers to current
2246 *
2247 * Returns 0 on success, -ENOMEM is there's no memory
2248 */
2249static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2250{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002251 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002252 struct socket_smack *ssp;
2253
2254 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2255 if (ssp == NULL)
2256 return -ENOMEM;
2257
jooseong lee08382c92016-11-03 11:54:39 +01002258 /*
2259 * Sockets created by kernel threads receive web label.
2260 */
2261 if (unlikely(current->flags & PF_KTHREAD)) {
2262 ssp->smk_in = &smack_known_web;
2263 ssp->smk_out = &smack_known_web;
2264 } else {
2265 ssp->smk_in = skp;
2266 ssp->smk_out = skp;
2267 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002268 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002269
2270 sk->sk_security = ssp;
2271
2272 return 0;
2273}
2274
2275/**
2276 * smack_sk_free_security - Free a socket blob
2277 * @sk: the socket
2278 *
2279 * Clears the blob pointer
2280 */
2281static void smack_sk_free_security(struct sock *sk)
2282{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302283#ifdef SMACK_IPV6_PORT_LABELING
2284 struct smk_port_label *spp;
2285
2286 if (sk->sk_family == PF_INET6) {
2287 rcu_read_lock();
2288 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2289 if (spp->smk_sock != sk)
2290 continue;
2291 spp->smk_can_reuse = 1;
2292 break;
2293 }
2294 rcu_read_unlock();
2295 }
2296#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002297 kfree(sk->sk_security);
2298}
2299
2300/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002301* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002302* @sip: the object end
2303*
2304* looks for host based access restrictions
2305*
2306* This version will only be appropriate for really small sets of single label
2307* hosts. The caller is responsible for ensuring that the RCU read lock is
2308* taken before calling this function.
2309*
2310* Returns the label of the far end or NULL if it's not special.
2311*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002312static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002313{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002314 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002315 struct in_addr *siap = &sip->sin_addr;
2316
2317 if (siap->s_addr == 0)
2318 return NULL;
2319
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002320 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2321 /*
2322 * we break after finding the first match because
2323 * the list is sorted from longest to shortest mask
2324 * so we have found the most specific match
2325 */
2326 if (snp->smk_host.s_addr ==
2327 (siap->s_addr & snp->smk_mask.s_addr))
2328 return snp->smk_label;
2329
2330 return NULL;
2331}
2332
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002333/*
2334 * smk_ipv6_localhost - Check for local ipv6 host address
2335 * @sip: the address
2336 *
2337 * Returns boolean true if this is the localhost address
2338 */
2339static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2340{
2341 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2342 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2343
2344 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2345 ntohs(be16p[7]) == 1)
2346 return true;
2347 return false;
2348}
2349
2350/**
2351* smack_ipv6host_label - check host based restrictions
2352* @sip: the object end
2353*
2354* looks for host based access restrictions
2355*
2356* This version will only be appropriate for really small sets of single label
2357* hosts. The caller is responsible for ensuring that the RCU read lock is
2358* taken before calling this function.
2359*
2360* Returns the label of the far end or NULL if it's not special.
2361*/
2362static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2363{
2364 struct smk_net6addr *snp;
2365 struct in6_addr *sap = &sip->sin6_addr;
2366 int i;
2367 int found = 0;
2368
2369 /*
2370 * It's local. Don't look for a host label.
2371 */
2372 if (smk_ipv6_localhost(sip))
2373 return NULL;
2374
2375 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002376 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002377 * If the label is NULL the entry has
2378 * been renounced. Ignore it.
2379 */
2380 if (snp->smk_label == NULL)
2381 continue;
2382 /*
Paul Moore07feee82009-03-27 17:10:54 -04002383 * we break after finding the first match because
2384 * the list is sorted from longest to shortest mask
2385 * so we have found the most specific match
2386 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002387 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002388 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2389 snp->smk_host.s6_addr16[i]) {
2390 found = 0;
2391 break;
2392 }
Etienne Basset43031542009-03-27 17:11:01 -04002393 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002394 if (found)
2395 return snp->smk_label;
2396 }
Paul Moore07feee82009-03-27 17:10:54 -04002397
2398 return NULL;
2399}
2400
2401/**
Casey Schauflera2af0312020-08-11 17:39:42 -07002402 * smack_netlbl_add - Set the secattr on a socket
Casey Schauflere114e472008-02-04 22:29:50 -08002403 * @sk: the socket
2404 *
Casey Schauflera2af0312020-08-11 17:39:42 -07002405 * Attach the outbound smack value (smk_out) to the socket.
Casey Schauflere114e472008-02-04 22:29:50 -08002406 *
2407 * Returns 0 on success or an error code
2408 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002409static int smack_netlbl_add(struct sock *sk)
Casey Schauflere114e472008-02-04 22:29:50 -08002410{
Paul Moore07feee82009-03-27 17:10:54 -04002411 struct socket_smack *ssp = sk->sk_security;
Casey Schauflera2af0312020-08-11 17:39:42 -07002412 struct smack_known *skp = ssp->smk_out;
2413 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002414
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002415 local_bh_disable();
2416 bh_lock_sock_nested(sk);
2417
Casey Schauflera2af0312020-08-11 17:39:42 -07002418 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2419 switch (rc) {
2420 case 0:
2421 ssp->smk_state = SMK_NETLBL_LABELED;
2422 break;
2423 case -EDESTADDRREQ:
2424 ssp->smk_state = SMK_NETLBL_REQSKB;
2425 rc = 0;
2426 break;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002427 }
2428
2429 bh_unlock_sock(sk);
2430 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002431
Casey Schauflere114e472008-02-04 22:29:50 -08002432 return rc;
2433}
2434
2435/**
Casey Schauflera2af0312020-08-11 17:39:42 -07002436 * smack_netlbl_delete - Remove the secattr from a socket
2437 * @sk: the socket
2438 *
2439 * Remove the outbound smack value from a socket
2440 */
2441static void smack_netlbl_delete(struct sock *sk)
2442{
2443 struct socket_smack *ssp = sk->sk_security;
2444
2445 /*
2446 * Take the label off the socket if one is set.
2447 */
2448 if (ssp->smk_state != SMK_NETLBL_LABELED)
2449 return;
2450
2451 local_bh_disable();
2452 bh_lock_sock_nested(sk);
2453 netlbl_sock_delattr(sk);
2454 bh_unlock_sock(sk);
2455 local_bh_enable();
2456 ssp->smk_state = SMK_NETLBL_UNLABELED;
2457}
2458
2459/**
2460 * smk_ipv4_check - Perform IPv4 host access checks
Paul Moore07feee82009-03-27 17:10:54 -04002461 * @sk: the socket
2462 * @sap: the destination address
2463 *
2464 * Set the correct secattr for the given socket based on the destination
2465 * address and perform any outbound access checks needed.
2466 *
2467 * Returns 0 on success or an error code.
2468 *
2469 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002470static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
Paul Moore07feee82009-03-27 17:10:54 -04002471{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002472 struct smack_known *skp;
Casey Schauflera2af0312020-08-11 17:39:42 -07002473 int rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002474 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002475 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002476 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002477
2478 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002479 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002480 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002481#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002482 struct lsm_network_audit net;
2483
Eric Paris48c62af2012-04-02 13:15:44 -04002484 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2485 ad.a.u.net->family = sap->sin_family;
2486 ad.a.u.net->dport = sap->sin_port;
2487 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002488#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002489 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002490 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2491 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Casey Schauflera2af0312020-08-11 17:39:42 -07002492 /*
2493 * Clear the socket netlabel if it's set.
2494 */
2495 if (!rc)
2496 smack_netlbl_delete(sk);
Paul Moore07feee82009-03-27 17:10:54 -04002497 }
2498 rcu_read_unlock();
Paul Moore07feee82009-03-27 17:10:54 -04002499
Casey Schauflera2af0312020-08-11 17:39:42 -07002500 return rc;
Paul Moore07feee82009-03-27 17:10:54 -04002501}
2502
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002503/**
2504 * smk_ipv6_check - check Smack access
2505 * @subject: subject Smack label
2506 * @object: object Smack label
2507 * @address: address
2508 * @act: the action being taken
2509 *
2510 * Check an IPv6 access
2511 */
2512static int smk_ipv6_check(struct smack_known *subject,
2513 struct smack_known *object,
2514 struct sockaddr_in6 *address, int act)
2515{
2516#ifdef CONFIG_AUDIT
2517 struct lsm_network_audit net;
2518#endif
2519 struct smk_audit_info ad;
2520 int rc;
2521
2522#ifdef CONFIG_AUDIT
2523 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2524 ad.a.u.net->family = PF_INET6;
2525 ad.a.u.net->dport = ntohs(address->sin6_port);
2526 if (act == SMK_RECEIVING)
2527 ad.a.u.net->v6info.saddr = address->sin6_addr;
2528 else
2529 ad.a.u.net->v6info.daddr = address->sin6_addr;
2530#endif
2531 rc = smk_access(subject, object, MAY_WRITE, &ad);
2532 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2533 return rc;
2534}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002535
2536#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002537/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002538 * smk_ipv6_port_label - Smack port access table management
2539 * @sock: socket
2540 * @address: address
2541 *
2542 * Create or update the port list entry
2543 */
2544static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2545{
2546 struct sock *sk = sock->sk;
2547 struct sockaddr_in6 *addr6;
2548 struct socket_smack *ssp = sock->sk->sk_security;
2549 struct smk_port_label *spp;
2550 unsigned short port = 0;
2551
2552 if (address == NULL) {
2553 /*
2554 * This operation is changing the Smack information
2555 * on the bound socket. Take the changes to the port
2556 * as well.
2557 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302558 rcu_read_lock();
2559 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002560 if (sk != spp->smk_sock)
2561 continue;
2562 spp->smk_in = ssp->smk_in;
2563 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302564 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002565 return;
2566 }
2567 /*
2568 * A NULL address is only used for updating existing
2569 * bound entries. If there isn't one, it's OK.
2570 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302571 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002572 return;
2573 }
2574
2575 addr6 = (struct sockaddr_in6 *)address;
2576 port = ntohs(addr6->sin6_port);
2577 /*
2578 * This is a special case that is safely ignored.
2579 */
2580 if (port == 0)
2581 return;
2582
2583 /*
2584 * Look for an existing port list entry.
2585 * This is an indication that a port is getting reused.
2586 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302587 rcu_read_lock();
2588 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302589 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002590 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302591 if (spp->smk_can_reuse != 1) {
2592 rcu_read_unlock();
2593 return;
2594 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002595 spp->smk_port = port;
2596 spp->smk_sock = sk;
2597 spp->smk_in = ssp->smk_in;
2598 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302599 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302600 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002601 return;
2602 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302603 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002604 /*
2605 * A new port entry is required.
2606 */
2607 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2608 if (spp == NULL)
2609 return;
2610
2611 spp->smk_port = port;
2612 spp->smk_sock = sk;
2613 spp->smk_in = ssp->smk_in;
2614 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302615 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302616 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002617
Vishal Goel3c7ce342016-11-23 10:31:08 +05302618 mutex_lock(&smack_ipv6_lock);
2619 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2620 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002621 return;
2622}
Arnd Bergmann00720f02020-04-08 21:04:31 +02002623#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002624
2625/**
2626 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002627 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002628 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002629 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002630 *
2631 * Create or update the port list entry
2632 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002633static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002634 int act)
2635{
Casey Schauflerc6739442013-05-22 18:42:56 -07002636 struct smk_port_label *spp;
2637 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002638 struct smack_known *skp = NULL;
2639 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002640 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002641
2642 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002643 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002644 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002645 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002646 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002647 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002648 }
2649
2650 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002651 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002652 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002653 if (skp != NULL && object != NULL)
2654 return smk_ipv6_check(skp, object, address, act);
2655 if (skp == NULL)
2656 skp = smack_net_ambient;
2657 if (object == NULL)
2658 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002659
2660 /*
2661 * It's remote, so port lookup does no good.
2662 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002663 if (!smk_ipv6_localhost(address))
2664 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002665
2666 /*
2667 * It's local so the send check has to have passed.
2668 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002669 if (act == SMK_RECEIVING)
2670 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002671
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002672 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302673 rcu_read_lock();
2674 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302675 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002676 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002677 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002678 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002679 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002680 break;
2681 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302682 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002683
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002684 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002685}
2686
2687/**
Casey Schauflere114e472008-02-04 22:29:50 -08002688 * smack_inode_setsecurity - set smack xattrs
2689 * @inode: the object
2690 * @name: attribute name
2691 * @value: attribute value
2692 * @size: size of the attribute
2693 * @flags: unused
2694 *
2695 * Sets the named attribute in the appropriate blob
2696 *
2697 * Returns 0 on success, or an error code
2698 */
2699static int smack_inode_setsecurity(struct inode *inode, const char *name,
2700 const void *value, size_t size, int flags)
2701{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002702 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002703 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002704 struct socket_smack *ssp;
2705 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002706 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002707
Casey Schauflerf7112e62012-05-06 15:22:02 -07002708 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302709 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002710
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002711 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002712 if (IS_ERR(skp))
2713 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002714
2715 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002716 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002717 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002718 return 0;
2719 }
2720 /*
2721 * The rest of the Smack xattrs are only on sockets.
2722 */
2723 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2724 return -EOPNOTSUPP;
2725
2726 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002727 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002728 return -EOPNOTSUPP;
2729
2730 ssp = sock->sk->sk_security;
2731
2732 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002733 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002734 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002735 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002736 if (sock->sk->sk_family == PF_INET) {
Casey Schauflera2af0312020-08-11 17:39:42 -07002737 rc = smack_netlbl_add(sock->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002738 if (rc != 0)
2739 printk(KERN_WARNING
2740 "Smack: \"%s\" netlbl error %d.\n",
2741 __func__, -rc);
2742 }
Casey Schauflere114e472008-02-04 22:29:50 -08002743 } else
2744 return -EOPNOTSUPP;
2745
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002746#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002747 if (sock->sk->sk_family == PF_INET6)
2748 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002749#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002750
Casey Schauflere114e472008-02-04 22:29:50 -08002751 return 0;
2752}
2753
2754/**
2755 * smack_socket_post_create - finish socket setup
2756 * @sock: the socket
2757 * @family: protocol family
2758 * @type: unused
2759 * @protocol: unused
2760 * @kern: unused
2761 *
2762 * Sets the netlabel information on the socket
2763 *
2764 * Returns 0 on success, and error code otherwise
2765 */
2766static int smack_socket_post_create(struct socket *sock, int family,
2767 int type, int protocol, int kern)
2768{
Marcin Lis74123012015-01-22 15:40:33 +01002769 struct socket_smack *ssp;
2770
2771 if (sock->sk == NULL)
2772 return 0;
2773
2774 /*
2775 * Sockets created by kernel threads receive web label.
2776 */
2777 if (unlikely(current->flags & PF_KTHREAD)) {
2778 ssp = sock->sk->sk_security;
2779 ssp->smk_in = &smack_known_web;
2780 ssp->smk_out = &smack_known_web;
2781 }
2782
2783 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002784 return 0;
2785 /*
2786 * Set the outbound netlbl.
2787 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002788 return smack_netlbl_add(sock->sk);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002789}
2790
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002791/**
2792 * smack_socket_socketpair - create socket pair
2793 * @socka: one socket
2794 * @sockb: another socket
2795 *
2796 * Cross reference the peer labels for SO_PEERSEC
2797 *
luanshia1a07f22019-07-05 10:35:20 +08002798 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002799 */
2800static int smack_socket_socketpair(struct socket *socka,
2801 struct socket *sockb)
2802{
2803 struct socket_smack *asp = socka->sk->sk_security;
2804 struct socket_smack *bsp = sockb->sk->sk_security;
2805
2806 asp->smk_packet = bsp->smk_out;
2807 bsp->smk_packet = asp->smk_out;
2808
2809 return 0;
2810}
2811
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002812#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002813/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002814 * smack_socket_bind - record port binding information.
2815 * @sock: the socket
2816 * @address: the port address
2817 * @addrlen: size of the address
2818 *
2819 * Records the label bound to a port.
2820 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002821 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002822 */
2823static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2824 int addrlen)
2825{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002826 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2827 if (addrlen < SIN6_LEN_RFC2133 ||
2828 address->sa_family != AF_INET6)
2829 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002830 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002831 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002832 return 0;
2833}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002834#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002835
2836/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002837 * smack_socket_connect - connect access check
2838 * @sock: the socket
2839 * @sap: the other end
2840 * @addrlen: size of sap
2841 *
2842 * Verifies that a connection may be possible
2843 *
2844 * Returns 0 on success, and error code otherwise
2845 */
2846static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2847 int addrlen)
2848{
Casey Schauflerc6739442013-05-22 18:42:56 -07002849 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002850
Casey Schauflerc6739442013-05-22 18:42:56 -07002851 if (sock->sk == NULL)
2852 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002853 if (sock->sk->sk_family != PF_INET &&
2854 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2855 return 0;
2856 if (addrlen < offsetofend(struct sockaddr, sa_family))
2857 return 0;
2858 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2859 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002860 struct smack_known *rsp = NULL;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002861
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002862 if (addrlen < SIN6_LEN_RFC2133)
2863 return 0;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002864 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2865 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002866 if (rsp != NULL) {
2867 struct socket_smack *ssp = sock->sk->sk_security;
2868
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002869 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002870 SMK_CONNECTING);
2871 }
Arnd Bergmann00720f02020-04-08 21:04:31 +02002872 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2873 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2874
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002875 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002876 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002877 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2878 return 0;
Casey Schauflera2af0312020-08-11 17:39:42 -07002879 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002880 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002881}
2882
2883/**
2884 * smack_flags_to_may - convert S_ to MAY_ values
2885 * @flags: the S_ value
2886 *
2887 * Returns the equivalent MAY_ value
2888 */
2889static int smack_flags_to_may(int flags)
2890{
2891 int may = 0;
2892
2893 if (flags & S_IRUGO)
2894 may |= MAY_READ;
2895 if (flags & S_IWUGO)
2896 may |= MAY_WRITE;
2897 if (flags & S_IXUGO)
2898 may |= MAY_EXEC;
2899
2900 return may;
2901}
2902
2903/**
2904 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2905 * @msg: the object
2906 *
2907 * Returns 0
2908 */
2909static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2910{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002911 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002912
Casey Schauflerecd5f822018-11-20 11:55:02 -08002913 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002914 return 0;
2915}
2916
2917/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002918 * smack_of_ipc - the smack pointer for the ipc
2919 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002920 *
2921 * Returns a pointer to the smack value
2922 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002923static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002924{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002925 struct smack_known **blob = smack_ipc(isp);
2926
2927 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002928}
2929
2930/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002931 * smack_ipc_alloc_security - Set the security blob for ipc
2932 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002933 *
2934 * Returns 0
2935 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002936static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002937{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002938 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002939
Casey Schaufler019bcca2018-09-21 17:19:54 -07002940 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002941 return 0;
2942}
2943
2944/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002945 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002946 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002947 * @access : access requested
2948 *
2949 * Returns 0 if current has the requested access, error code otherwise
2950 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002951static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002952{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002953 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002954 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002955 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002956
2957#ifdef CONFIG_AUDIT
2958 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002959 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002960#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002961 rc = smk_curacc(ssp, access, &ad);
2962 rc = smk_bu_current("shm", ssp, access, rc);
2963 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002964}
2965
2966/**
Casey Schauflere114e472008-02-04 22:29:50 -08002967 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002968 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002969 * @shmflg: access requested
2970 *
2971 * Returns 0 if current has the requested access, error code otherwise
2972 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002973static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002974{
Casey Schauflere114e472008-02-04 22:29:50 -08002975 int may;
2976
2977 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002978 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002979}
2980
2981/**
2982 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002983 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002984 * @cmd: what it wants to do
2985 *
2986 * Returns 0 if current has the requested access, error code otherwise
2987 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002988static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002989{
Casey Schauflere114e472008-02-04 22:29:50 -08002990 int may;
2991
2992 switch (cmd) {
2993 case IPC_STAT:
2994 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002995 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002996 may = MAY_READ;
2997 break;
2998 case IPC_SET:
2999 case SHM_LOCK:
3000 case SHM_UNLOCK:
3001 case IPC_RMID:
3002 may = MAY_READWRITE;
3003 break;
3004 case IPC_INFO:
3005 case SHM_INFO:
3006 /*
3007 * System level information.
3008 */
3009 return 0;
3010 default:
3011 return -EINVAL;
3012 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003013 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003014}
3015
3016/**
3017 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003018 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003019 * @shmaddr: unused
3020 * @shmflg: access requested
3021 *
3022 * Returns 0 if current has the requested access, error code otherwise
3023 */
luanshia1a07f22019-07-05 10:35:20 +08003024static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003025 int shmflg)
3026{
Casey Schauflere114e472008-02-04 22:29:50 -08003027 int may;
3028
3029 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003030 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003031}
3032
3033/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003034 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003035 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003036 * @access : access requested
3037 *
3038 * Returns 0 if current has the requested access, error code otherwise
3039 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003040static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003041{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003042 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003043 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003044 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003045
3046#ifdef CONFIG_AUDIT
3047 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003048 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003049#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003050 rc = smk_curacc(ssp, access, &ad);
3051 rc = smk_bu_current("sem", ssp, access, rc);
3052 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003053}
3054
3055/**
Casey Schauflere114e472008-02-04 22:29:50 -08003056 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003057 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003058 * @semflg: access requested
3059 *
3060 * Returns 0 if current has the requested access, error code otherwise
3061 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003062static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003063{
Casey Schauflere114e472008-02-04 22:29:50 -08003064 int may;
3065
3066 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003067 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003068}
3069
3070/**
3071 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003072 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003073 * @cmd: what it wants to do
3074 *
3075 * Returns 0 if current has the requested access, error code otherwise
3076 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003077static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003078{
Casey Schauflere114e472008-02-04 22:29:50 -08003079 int may;
3080
3081 switch (cmd) {
3082 case GETPID:
3083 case GETNCNT:
3084 case GETZCNT:
3085 case GETVAL:
3086 case GETALL:
3087 case IPC_STAT:
3088 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003089 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003090 may = MAY_READ;
3091 break;
3092 case SETVAL:
3093 case SETALL:
3094 case IPC_RMID:
3095 case IPC_SET:
3096 may = MAY_READWRITE;
3097 break;
3098 case IPC_INFO:
3099 case SEM_INFO:
3100 /*
3101 * System level information
3102 */
3103 return 0;
3104 default:
3105 return -EINVAL;
3106 }
3107
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003108 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003109}
3110
3111/**
3112 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003113 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003114 * @sops: unused
3115 * @nsops: unused
3116 * @alter: unused
3117 *
3118 * Treated as read and write in all cases.
3119 *
3120 * Returns 0 if access is allowed, error code otherwise
3121 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003122static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003123 unsigned nsops, int alter)
3124{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003125 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003126}
3127
3128/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003129 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003130 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003131 * @access : access requested
3132 *
3133 * return 0 if current has access, error otherwise
3134 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003135static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003136{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003137 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003138 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003139 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003140
3141#ifdef CONFIG_AUDIT
3142 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003143 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003144#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003145 rc = smk_curacc(msp, access, &ad);
3146 rc = smk_bu_current("msq", msp, access, rc);
3147 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003148}
3149
3150/**
Casey Schauflere114e472008-02-04 22:29:50 -08003151 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003152 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003153 * @msqflg: access requested
3154 *
3155 * Returns 0 if current has the requested access, error code otherwise
3156 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003157static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003158{
Casey Schauflere114e472008-02-04 22:29:50 -08003159 int may;
3160
3161 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003162 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003163}
3164
3165/**
3166 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003167 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003168 * @cmd: what it wants to do
3169 *
3170 * Returns 0 if current has the requested access, error code otherwise
3171 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003172static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003173{
Casey Schauflere114e472008-02-04 22:29:50 -08003174 int may;
3175
3176 switch (cmd) {
3177 case IPC_STAT:
3178 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003179 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003180 may = MAY_READ;
3181 break;
3182 case IPC_SET:
3183 case IPC_RMID:
3184 may = MAY_READWRITE;
3185 break;
3186 case IPC_INFO:
3187 case MSG_INFO:
3188 /*
3189 * System level information
3190 */
3191 return 0;
3192 default:
3193 return -EINVAL;
3194 }
3195
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003196 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003197}
3198
3199/**
3200 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003201 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003202 * @msg: unused
3203 * @msqflg: access requested
3204 *
3205 * Returns 0 if current has the requested access, error code otherwise
3206 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003207static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003208 int msqflg)
3209{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003210 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003211
Etienne Bassetecfcc532009-04-08 20:40:06 +02003212 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003213 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003214}
3215
3216/**
3217 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003218 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003219 * @msg: unused
3220 * @target: unused
3221 * @type: unused
3222 * @mode: unused
3223 *
3224 * Returns 0 if current has read and write access, error code otherwise
3225 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003226static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003227 struct task_struct *target, long type, int mode)
3228{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003229 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003230}
3231
3232/**
3233 * smack_ipc_permission - Smack access for ipc_permission()
3234 * @ipp: the object permissions
3235 * @flag: access requested
3236 *
3237 * Returns 0 if current has read and write access, error code otherwise
3238 */
3239static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3240{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003241 struct smack_known **blob = smack_ipc(ipp);
3242 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003243 int may = smack_flags_to_may(flag);
3244 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003245 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003246
Etienne Bassetecfcc532009-04-08 20:40:06 +02003247#ifdef CONFIG_AUDIT
3248 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3249 ad.a.u.ipc_id = ipp->id;
3250#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003251 rc = smk_curacc(iskp, may, &ad);
3252 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003253 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003254}
3255
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003256/**
3257 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003258 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003259 * @secid: where result will be saved
3260 */
3261static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3262{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003263 struct smack_known **blob = smack_ipc(ipp);
3264 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003265
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003266 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003267}
3268
Casey Schauflere114e472008-02-04 22:29:50 -08003269/**
3270 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003271 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003272 * @inode: the object
3273 *
3274 * Set the inode's security blob if it hasn't been done already.
3275 */
3276static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3277{
3278 struct super_block *sbp;
3279 struct superblock_smack *sbsp;
3280 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003281 struct smack_known *skp;
3282 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003283 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003284 char trattr[TRANS_TRUE_SIZE];
3285 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003286 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003287 struct dentry *dp;
3288
3289 if (inode == NULL)
3290 return;
3291
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003292 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003293
Casey Schauflere114e472008-02-04 22:29:50 -08003294 /*
3295 * If the inode is already instantiated
3296 * take the quick way out
3297 */
3298 if (isp->smk_flags & SMK_INODE_INSTANT)
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003299 return;
Casey Schauflere114e472008-02-04 22:29:50 -08003300
3301 sbp = inode->i_sb;
3302 sbsp = sbp->s_security;
3303 /*
3304 * We're going to use the superblock default label
3305 * if there's no label on the file.
3306 */
3307 final = sbsp->smk_default;
3308
3309 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003310 * If this is the root inode the superblock
3311 * may be in the process of initialization.
3312 * If that is the case use the root value out
3313 * of the superblock.
3314 */
3315 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003316 switch (sbp->s_magic) {
3317 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003318 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003319 /*
3320 * The cgroup filesystem is never mounted,
3321 * so there's no opportunity to set the mount
3322 * options.
3323 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003324 sbsp->smk_root = &smack_known_star;
3325 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003326 isp->smk_inode = sbsp->smk_root;
3327 break;
3328 case TMPFS_MAGIC:
3329 /*
3330 * What about shmem/tmpfs anonymous files with dentry
3331 * obtained from d_alloc_pseudo()?
3332 */
3333 isp->smk_inode = smk_of_current();
3334 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003335 case PIPEFS_MAGIC:
3336 isp->smk_inode = smk_of_current();
3337 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003338 case SOCKFS_MAGIC:
3339 /*
3340 * Socket access is controlled by the socket
3341 * structures associated with the task involved.
3342 */
3343 isp->smk_inode = &smack_known_star;
3344 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003345 default:
3346 isp->smk_inode = sbsp->smk_root;
3347 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003348 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003349 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003350 return;
Casey Schauflere97dcb02008-06-02 10:04:32 -07003351 }
3352
3353 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003354 * This is pretty hackish.
3355 * Casey says that we shouldn't have to do
3356 * file system specific code, but it does help
3357 * with keeping it simple.
3358 */
3359 switch (sbp->s_magic) {
3360 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003361 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003362 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003363 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003364 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003365 * that the smack file system doesn't do
3366 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003367 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003368 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003369 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003370 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003371 break;
3372 case DEVPTS_SUPER_MAGIC:
3373 /*
3374 * devpts seems content with the label of the task.
3375 * Programs that change smack have to treat the
3376 * pty with respect.
3377 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003378 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003379 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003380 case PROC_SUPER_MAGIC:
3381 /*
3382 * Casey says procfs appears not to care.
3383 * The superblock default suffices.
3384 */
3385 break;
3386 case TMPFS_MAGIC:
3387 /*
3388 * Device labels should come from the filesystem,
3389 * but watch out, because they're volitile,
3390 * getting recreated on every reboot.
3391 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003392 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003393 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003394 * If a smack value has been set we want to use it,
3395 * but since tmpfs isn't giving us the opportunity
3396 * to set mount options simulate setting the
3397 * superblock default.
3398 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003399 fallthrough;
Casey Schauflere114e472008-02-04 22:29:50 -08003400 default:
3401 /*
3402 * This isn't an understood special case.
3403 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003404 */
3405
3406 /*
3407 * UNIX domain sockets use lower level socket data.
3408 */
3409 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003410 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003411 break;
3412 }
3413 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003414 * No xattr support means, alas, no SMACK label.
3415 * Use the aforeapplied default.
3416 * It would be curious if the label of the task
3417 * does not match that assigned.
3418 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003419 if (!(inode->i_opflags & IOP_XATTR))
3420 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003421 /*
3422 * Get the dentry for xattr.
3423 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003424 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003425 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003426 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003427 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003428
3429 /*
3430 * Transmuting directory
3431 */
3432 if (S_ISDIR(inode->i_mode)) {
3433 /*
3434 * If this is a new directory and the label was
3435 * transmuted when the inode was initialized
3436 * set the transmute attribute on the directory
3437 * and mark the inode.
3438 *
3439 * If there is a transmute attribute on the
3440 * directory mark the inode.
3441 */
3442 if (isp->smk_flags & SMK_INODE_CHANGED) {
3443 isp->smk_flags &= ~SMK_INODE_CHANGED;
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01003444 rc = __vfs_setxattr(&init_user_ns, dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003445 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003446 TRANS_TRUE, TRANS_TRUE_SIZE,
3447 0);
3448 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003449 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003450 XATTR_NAME_SMACKTRANSMUTE, trattr,
3451 TRANS_TRUE_SIZE);
3452 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3453 TRANS_TRUE_SIZE) != 0)
3454 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003455 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003456 if (rc >= 0)
3457 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003458 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003459 /*
3460 * Don't let the exec or mmap label be "*" or "@".
3461 */
3462 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3463 if (IS_ERR(skp) || skp == &smack_known_star ||
3464 skp == &smack_known_web)
3465 skp = NULL;
3466 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003467
Casey Schaufler19760ad2013-12-16 16:27:26 -08003468 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003469 if (IS_ERR(skp) || skp == &smack_known_star ||
3470 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003471 skp = NULL;
3472 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003473
Casey Schauflere114e472008-02-04 22:29:50 -08003474 dput(dp);
3475 break;
3476 }
3477
3478 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003479 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003480 else
3481 isp->smk_inode = final;
3482
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003483 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003484
Casey Schauflere114e472008-02-04 22:29:50 -08003485 return;
3486}
3487
3488/**
3489 * smack_getprocattr - Smack process attribute access
3490 * @p: the object task
3491 * @name: the name of the attribute in /proc/.../attr
3492 * @value: where to put the result
3493 *
3494 * Places a copy of the task Smack into value
3495 *
3496 * Returns the length of the smack label or an error code
3497 */
3498static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3499{
Paul Moore1fb057d2021-02-19 15:04:58 -05003500 struct smack_known *skp = smk_of_task_struct_subj(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003501 char *cp;
3502 int slen;
3503
3504 if (strcmp(name, "current") != 0)
3505 return -EINVAL;
3506
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003507 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003508 if (cp == NULL)
3509 return -ENOMEM;
3510
3511 slen = strlen(cp);
3512 *value = cp;
3513 return slen;
3514}
3515
3516/**
3517 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003518 * @name: the name of the attribute in /proc/.../attr
3519 * @value: the value to set
3520 * @size: the size of the value
3521 *
3522 * Sets the Smack value of the task. Only setting self
3523 * is permitted and only with privilege
3524 *
3525 * Returns the length of the smack label or an error code
3526 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003527static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003528{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003529 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003530 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003531 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003532 struct smack_known_list_elem *sklep;
3533 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003534
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003535 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003536 return -EPERM;
3537
Casey Schauflerf7112e62012-05-06 15:22:02 -07003538 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003539 return -EINVAL;
3540
3541 if (strcmp(name, "current") != 0)
3542 return -EINVAL;
3543
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003544 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003545 if (IS_ERR(skp))
3546 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003547
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003548 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303549 * No process is ever allowed the web ("@") label
3550 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003551 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303552 if (skp == &smack_known_web || skp == &smack_known_star)
3553 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003554
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003555 if (!smack_privileged(CAP_MAC_ADMIN)) {
3556 rc = -EPERM;
3557 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3558 if (sklep->smk_label == skp) {
3559 rc = 0;
3560 break;
3561 }
3562 if (rc)
3563 return rc;
3564 }
3565
David Howellsd84f4f92008-11-14 10:39:23 +11003566 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003567 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003568 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003569
Casey Schauflerb17103a2018-11-09 16:12:56 -08003570 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003571 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003572 /*
3573 * process can change its label only once
3574 */
3575 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003576
David Howellsd84f4f92008-11-14 10:39:23 +11003577 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003578 return size;
3579}
3580
3581/**
3582 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003583 * @sock: one sock
3584 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003585 * @newsk: unused
3586 *
3587 * Return 0 if a subject with the smack of sock could access
3588 * an object with the smack of other, otherwise an error code
3589 */
David S. Miller3610cda2011-01-05 15:38:53 -08003590static int smack_unix_stream_connect(struct sock *sock,
3591 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003592{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003593 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003594 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003595 struct socket_smack *ssp = sock->sk_security;
3596 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003597 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003598 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003599 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003600#ifdef CONFIG_AUDIT
3601 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003602#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003603
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003604 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3605 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003606 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003607#ifdef CONFIG_AUDIT
3608 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3609 smk_ad_setfield_u_net_sk(&ad, other);
3610#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003611 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3612 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003613 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003614 okp = osp->smk_out;
3615 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003616 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003617 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003618 MAY_WRITE, rc);
3619 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003620 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003621
Casey Schaufler975d5e52011-09-26 14:43:39 -07003622 /*
3623 * Cross reference the peer labels for SO_PEERSEC.
3624 */
3625 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003626 nsp->smk_packet = ssp->smk_out;
3627 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003628 }
3629
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003630 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003631}
3632
3633/**
3634 * smack_unix_may_send - Smack access on UDS
3635 * @sock: one socket
3636 * @other: the other socket
3637 *
3638 * Return 0 if a subject with the smack of sock could access
3639 * an object with the smack of other, otherwise an error code
3640 */
3641static int smack_unix_may_send(struct socket *sock, struct socket *other)
3642{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003643 struct socket_smack *ssp = sock->sk->sk_security;
3644 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003645 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003646 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003647
Kees Cook923e9a12012-04-10 13:26:44 -07003648#ifdef CONFIG_AUDIT
3649 struct lsm_network_audit net;
3650
Eric Paris48c62af2012-04-02 13:15:44 -04003651 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003652 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003653#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003654
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003655 if (smack_privileged(CAP_MAC_OVERRIDE))
3656 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003657
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003658 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3659 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003660 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003661}
3662
3663/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003664 * smack_socket_sendmsg - Smack check based on destination host
3665 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003666 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003667 * @size: the size of the message
3668 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003669 * Return 0 if the current subject can write to the destination host.
3670 * For IPv4 this is only a question if the destination is a single label host.
3671 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003672 */
3673static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3674 int size)
3675{
3676 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003677#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003678 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003679#endif
3680#ifdef SMACK_IPV6_SECMARK_LABELING
3681 struct socket_smack *ssp = sock->sk->sk_security;
3682 struct smack_known *rsp;
3683#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003684 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003685
3686 /*
3687 * Perfectly reasonable for this to be NULL
3688 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003689 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003690 return 0;
3691
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003692 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003693 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003694 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3695 sip->sin_family != AF_INET)
3696 return -EINVAL;
Casey Schauflera2af0312020-08-11 17:39:42 -07003697 rc = smk_ipv4_check(sock->sk, sip);
Casey Schauflerc6739442013-05-22 18:42:56 -07003698 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003699#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003700 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003701 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3702 sap->sin6_family != AF_INET6)
3703 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003704#ifdef SMACK_IPV6_SECMARK_LABELING
3705 rsp = smack_ipv6host_label(sap);
3706 if (rsp != NULL)
3707 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3708 SMK_CONNECTING);
3709#endif
3710#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003711 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003712#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003713#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003714 break;
3715 }
3716 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003717}
3718
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003719/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003720 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003721 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003722 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003723 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003724 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003725 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003726static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3727 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003728{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003729 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003730 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003731 int acat;
3732 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003733
Casey Schaufler322dd632020-08-11 17:39:43 -07003734 /*
3735 * Netlabel found it in the cache.
3736 */
3737 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3738 return (struct smack_known *)sap->cache->data;
3739
3740 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3741 /*
3742 * Looks like a fallback, which gives us a secid.
3743 */
3744 return smack_from_secid(sap->attr.secid);
3745
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003746 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003747 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003748 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003749 * If there are flags but no level netlabel isn't
3750 * behaving the way we expect it to.
3751 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003752 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003753 * Without guidance regarding the smack value
3754 * for the packet fall back on the network
3755 * ambient value.
3756 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003757 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303758 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003759 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003760 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003761 /*
3762 * Compare the catsets. Use the netlbl APIs.
3763 */
3764 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3765 if ((skp->smk_netlabel.flags &
3766 NETLBL_SECATTR_MLS_CAT) == 0)
3767 found = 1;
3768 break;
3769 }
3770 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003771 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3772 acat + 1);
3773 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003774 skp->smk_netlabel.attr.mls.cat,
3775 kcat + 1);
3776 if (acat < 0 || kcat < 0)
3777 break;
3778 }
3779 if (acat == kcat) {
3780 found = 1;
3781 break;
3782 }
Casey Schauflere114e472008-02-04 22:29:50 -08003783 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003784 rcu_read_unlock();
3785
3786 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003787 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003788
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003789 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003790 return &smack_known_web;
3791 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003792 }
Casey Schauflere114e472008-02-04 22:29:50 -08003793 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003794 * Without guidance regarding the smack value
3795 * for the packet fall back on the network
3796 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003797 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003798 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003799}
3800
Casey Schaufler69f287a2014-12-12 17:08:40 -08003801#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003802static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003803{
Casey Schauflerc6739442013-05-22 18:42:56 -07003804 u8 nexthdr;
3805 int offset;
3806 int proto = -EINVAL;
3807 struct ipv6hdr _ipv6h;
3808 struct ipv6hdr *ip6;
3809 __be16 frag_off;
3810 struct tcphdr _tcph, *th;
3811 struct udphdr _udph, *uh;
3812 struct dccp_hdr _dccph, *dh;
3813
3814 sip->sin6_port = 0;
3815
3816 offset = skb_network_offset(skb);
3817 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3818 if (ip6 == NULL)
3819 return -EINVAL;
3820 sip->sin6_addr = ip6->saddr;
3821
3822 nexthdr = ip6->nexthdr;
3823 offset += sizeof(_ipv6h);
3824 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3825 if (offset < 0)
3826 return -EINVAL;
3827
3828 proto = nexthdr;
3829 switch (proto) {
3830 case IPPROTO_TCP:
3831 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3832 if (th != NULL)
3833 sip->sin6_port = th->source;
3834 break;
3835 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003836 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003837 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3838 if (uh != NULL)
3839 sip->sin6_port = uh->source;
3840 break;
3841 case IPPROTO_DCCP:
3842 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3843 if (dh != NULL)
3844 sip->sin6_port = dh->dccph_sport;
3845 break;
3846 }
3847 return proto;
3848}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003849#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003850
Casey Schauflere114e472008-02-04 22:29:50 -08003851/**
Casey Schaufler36be8122020-08-11 17:39:41 -07003852 * smack_from_skb - Smack data from the secmark in an skb
3853 * @skb: packet
3854 *
3855 * Returns smack_known of the secmark or NULL if that won't work.
3856 */
Casey Schauflerbf0afe62020-09-22 14:59:31 -07003857#ifdef CONFIG_NETWORK_SECMARK
Casey Schaufler36be8122020-08-11 17:39:41 -07003858static struct smack_known *smack_from_skb(struct sk_buff *skb)
3859{
3860 if (skb == NULL || skb->secmark == 0)
3861 return NULL;
3862
3863 return smack_from_secid(skb->secmark);
3864}
Casey Schauflerbf0afe62020-09-22 14:59:31 -07003865#else
3866static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3867{
3868 return NULL;
3869}
3870#endif
Casey Schaufler36be8122020-08-11 17:39:41 -07003871
3872/**
Casey Schauflera2af0312020-08-11 17:39:42 -07003873 * smack_from_netlbl - Smack data from the IP options in an skb
3874 * @sk: socket data came in on
3875 * @family: address family
3876 * @skb: packet
3877 *
Casey Schaufler322dd632020-08-11 17:39:43 -07003878 * Find the Smack label in the IP options. If it hasn't been
3879 * added to the netlabel cache, add it here.
3880 *
Casey Schauflera2af0312020-08-11 17:39:42 -07003881 * Returns smack_known of the IP options or NULL if that won't work.
3882 */
Florian Westphal41dd9592020-11-30 16:36:29 +01003883static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
Casey Schauflera2af0312020-08-11 17:39:42 -07003884 struct sk_buff *skb)
3885{
3886 struct netlbl_lsm_secattr secattr;
3887 struct socket_smack *ssp = NULL;
3888 struct smack_known *skp = NULL;
3889
3890 netlbl_secattr_init(&secattr);
3891
3892 if (sk)
3893 ssp = sk->sk_security;
Casey Schaufler322dd632020-08-11 17:39:43 -07003894
3895 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
Casey Schauflera2af0312020-08-11 17:39:42 -07003896 skp = smack_from_secattr(&secattr, ssp);
Casey Schaufler322dd632020-08-11 17:39:43 -07003897 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
Alex Shi9b0072e2020-11-08 14:45:42 +08003898 netlbl_cache_add(skb, family, &skp->smk_netlabel);
Casey Schaufler322dd632020-08-11 17:39:43 -07003899 }
Casey Schauflera2af0312020-08-11 17:39:42 -07003900
3901 netlbl_secattr_destroy(&secattr);
3902
3903 return skp;
3904}
3905
3906/**
Casey Schauflere114e472008-02-04 22:29:50 -08003907 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3908 * @sk: socket
3909 * @skb: packet
3910 *
3911 * Returns 0 if the packet should be delivered, an error code otherwise
3912 */
3913static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3914{
Casey Schauflere114e472008-02-04 22:29:50 -08003915 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003917 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003918 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003919 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003920#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003921 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003922#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003923#if IS_ENABLED(CONFIG_IPV6)
3924 struct sockaddr_in6 sadd;
3925 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003926
3927 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3928 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003929#endif /* CONFIG_IPV6 */
3930
Piotr Sawicki129a9982018-07-19 11:42:58 +02003931 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003932 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003933 /*
3934 * If there is a secmark use it rather than the CIPSO label.
3935 * If there is no secmark fall back to CIPSO.
3936 * The secmark is assumed to reflect policy better.
3937 */
Casey Schaufler36be8122020-08-11 17:39:41 -07003938 skp = smack_from_skb(skb);
Casey Schauflera2af0312020-08-11 17:39:42 -07003939 if (skp == NULL) {
3940 skp = smack_from_netlbl(sk, family, skb);
3941 if (skp == NULL)
3942 skp = smack_net_ambient;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003943 }
Casey Schauflere114e472008-02-04 22:29:50 -08003944
Etienne Bassetecfcc532009-04-08 20:40:06 +02003945#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003946 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003947 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003948 ad.a.u.net->netif = skb->skb_iif;
3949 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003950#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003951 /*
3952 * Receiving a packet requires that the other end
3953 * be able to write here. Read access is not required.
3954 * This is the simplist possible security model
3955 * for networking.
3956 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003957 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3958 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003959 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003960 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003961 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003962 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003963#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003964 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003965 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003966 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3967 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003968 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003969#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler36be8122020-08-11 17:39:41 -07003970 skp = smack_from_skb(skb);
3971 if (skp == NULL) {
3972 if (smk_ipv6_localhost(&sadd))
3973 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003974 skp = smack_ipv6host_label(&sadd);
Casey Schaufler36be8122020-08-11 17:39:41 -07003975 if (skp == NULL)
3976 skp = smack_net_ambient;
3977 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003978#ifdef CONFIG_AUDIT
3979 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003980 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003981 ad.a.u.net->netif = skb->skb_iif;
3982 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3983#endif /* CONFIG_AUDIT */
3984 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3985 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3986 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003987#endif /* SMACK_IPV6_SECMARK_LABELING */
3988#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003989 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003990#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003991 if (rc != 0)
3992 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3993 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003994 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003995#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003996 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003997
Paul Moorea8134292008-10-10 10:16:31 -04003998 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003999}
4000
4001/**
4002 * smack_socket_getpeersec_stream - pull in packet label
4003 * @sock: the socket
4004 * @optval: user's destination
4005 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004006 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004007 *
4008 * returns zero on success, an error code otherwise
4009 */
4010static int smack_socket_getpeersec_stream(struct socket *sock,
4011 char __user *optval,
4012 int __user *optlen, unsigned len)
4013{
4014 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004015 char *rcp = "";
4016 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004017 int rc = 0;
4018
4019 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004020 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004021 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004022 slen = strlen(rcp) + 1;
4023 }
Casey Schauflere114e472008-02-04 22:29:50 -08004024
4025 if (slen > len)
4026 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004027 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004028 rc = -EFAULT;
4029
4030 if (put_user(slen, optlen) != 0)
4031 rc = -EFAULT;
4032
4033 return rc;
4034}
4035
4036
4037/**
4038 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004039 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004040 * @skb: packet data
4041 * @secid: pointer to where to put the secid of the packet
4042 *
4043 * Sets the netlabel socket state on sk from parent
4044 */
4045static int smack_socket_getpeersec_dgram(struct socket *sock,
4046 struct sk_buff *skb, u32 *secid)
4047
4048{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004049 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004050 struct smack_known *skp;
Casey Schauflera2af0312020-08-11 17:39:42 -07004051 struct sock *sk = NULL;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004052 int family = PF_UNSPEC;
4053 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004054
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004055 if (skb != NULL) {
4056 if (skb->protocol == htons(ETH_P_IP))
4057 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004058#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004059 else if (skb->protocol == htons(ETH_P_IPV6))
4060 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004061#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004062 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004063 if (family == PF_UNSPEC && sock != NULL)
4064 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004065
Casey Schaufler69f287a2014-12-12 17:08:40 -08004066 switch (family) {
4067 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004068 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004069 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004070 break;
4071 case PF_INET:
Casey Schaufler36be8122020-08-11 17:39:41 -07004072 skp = smack_from_skb(skb);
4073 if (skp) {
4074 s = skp->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004075 break;
Casey Schaufler36be8122020-08-11 17:39:41 -07004076 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004077 /*
4078 * Translate what netlabel gave us.
4079 */
Casey Schauflera2af0312020-08-11 17:39:42 -07004080 if (sock != NULL)
4081 sk = sock->sk;
4082 skp = smack_from_netlbl(sk, family, skb);
4083 if (skp != NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004084 s = skp->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004085 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004086 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004087#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler36be8122020-08-11 17:39:41 -07004088 skp = smack_from_skb(skb);
4089 if (skp)
4090 s = skp->smk_secid;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004091#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004092 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004093 }
4094 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004095 if (s == 0)
4096 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004097 return 0;
4098}
4099
4100/**
Paul Moore07feee82009-03-27 17:10:54 -04004101 * smack_sock_graft - Initialize a newly created socket with an existing sock
4102 * @sk: child sock
4103 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004104 *
Paul Moore07feee82009-03-27 17:10:54 -04004105 * Set the smk_{in,out} state of an existing sock based on the process that
4106 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004107 */
4108static void smack_sock_graft(struct sock *sk, struct socket *parent)
4109{
4110 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004111 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004112
Paul Moore07feee82009-03-27 17:10:54 -04004113 if (sk == NULL ||
4114 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004115 return;
4116
4117 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004118 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004119 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004120 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004121}
4122
4123/**
4124 * smack_inet_conn_request - Smack access check on connect
4125 * @sk: socket involved
4126 * @skb: packet
4127 * @req: unused
4128 *
4129 * Returns 0 if a task with the packet label could write to
4130 * the socket, otherwise an error code
4131 */
Florian Westphal41dd9592020-11-30 16:36:29 +01004132static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
Casey Schauflere114e472008-02-04 22:29:50 -08004133 struct request_sock *req)
4134{
Paul Moore07feee82009-03-27 17:10:54 -04004135 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004136 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004137 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004138 struct sockaddr_in addr;
4139 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004140 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004141 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004142 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004143#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004144 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004145#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004146
Casey Schaufler69f287a2014-12-12 17:08:40 -08004147#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004148 if (family == PF_INET6) {
4149 /*
4150 * Handle mapped IPv4 packets arriving
4151 * via IPv6 sockets. Don't set up netlabel
4152 * processing on IPv6.
4153 */
4154 if (skb->protocol == htons(ETH_P_IP))
4155 family = PF_INET;
4156 else
4157 return 0;
4158 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004159#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004160
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004161 /*
4162 * If there is a secmark use it rather than the CIPSO label.
4163 * If there is no secmark fall back to CIPSO.
4164 * The secmark is assumed to reflect policy better.
4165 */
Casey Schaufler36be8122020-08-11 17:39:41 -07004166 skp = smack_from_skb(skb);
Casey Schauflera2af0312020-08-11 17:39:42 -07004167 if (skp == NULL) {
4168 skp = smack_from_netlbl(sk, family, skb);
4169 if (skp == NULL)
4170 skp = &smack_known_huh;
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004171 }
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004172
Etienne Bassetecfcc532009-04-08 20:40:06 +02004173#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004174 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4175 ad.a.u.net->family = family;
4176 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004177 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4178#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004179 /*
Paul Moore07feee82009-03-27 17:10:54 -04004180 * Receiving a packet requires that the other end be able to write
4181 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004182 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004183 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4184 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004185 if (rc != 0)
4186 return rc;
4187
4188 /*
4189 * Save the peer's label in the request_sock so we can later setup
4190 * smk_packet in the child socket so that SO_PEERCRED can report it.
4191 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004192 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004193
4194 /*
4195 * We need to decide if we want to label the incoming connection here
4196 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004197 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004198 */
4199 hdr = ip_hdr(skb);
4200 addr.sin_addr.s_addr = hdr->saddr;
4201 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004202 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004203 rcu_read_unlock();
4204
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004205 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004206 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004207 else
Paul Moore07feee82009-03-27 17:10:54 -04004208 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004209
4210 return rc;
4211}
4212
Paul Moore07feee82009-03-27 17:10:54 -04004213/**
4214 * smack_inet_csk_clone - Copy the connection information to the new socket
4215 * @sk: the new socket
4216 * @req: the connection's request_sock
4217 *
4218 * Transfer the connection's peer label to the newly created socket.
4219 */
4220static void smack_inet_csk_clone(struct sock *sk,
4221 const struct request_sock *req)
4222{
4223 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004224 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004225
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004226 if (req->peer_secid != 0) {
4227 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004228 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004229 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004230 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004231}
4232
Casey Schauflere114e472008-02-04 22:29:50 -08004233/*
4234 * Key management security hooks
4235 *
4236 * Casey has not tested key support very heavily.
4237 * The permission check is most likely too restrictive.
4238 * If you care about keys please have a look.
4239 */
4240#ifdef CONFIG_KEYS
4241
4242/**
4243 * smack_key_alloc - Set the key security blob
4244 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004245 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004246 * @flags: unused
4247 *
4248 * No allocation required
4249 *
4250 * Returns 0
4251 */
David Howellsd84f4f92008-11-14 10:39:23 +11004252static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004253 unsigned long flags)
4254{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004255 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004256
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004257 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004258 return 0;
4259}
4260
4261/**
4262 * smack_key_free - Clear the key security blob
4263 * @key: the object
4264 *
4265 * Clear the blob pointer
4266 */
4267static void smack_key_free(struct key *key)
4268{
4269 key->security = NULL;
4270}
4271
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004272/**
Casey Schauflere114e472008-02-04 22:29:50 -08004273 * smack_key_permission - Smack access on a key
4274 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004275 * @cred: the credentials to use
David Howells8c0637e2020-05-12 15:16:29 +01004276 * @need_perm: requested key permission
Casey Schauflere114e472008-02-04 22:29:50 -08004277 *
4278 * Return 0 if the task has read and write to the object,
4279 * an error code otherwise
4280 */
4281static int smack_key_permission(key_ref_t key_ref,
David Howells8c0637e2020-05-12 15:16:29 +01004282 const struct cred *cred,
4283 enum key_need_perm need_perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004284{
4285 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004286 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004287 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004288 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004289 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004290
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004291 /*
4292 * Validate requested permissions
4293 */
David Howells8c0637e2020-05-12 15:16:29 +01004294 switch (need_perm) {
4295 case KEY_NEED_READ:
4296 case KEY_NEED_SEARCH:
4297 case KEY_NEED_VIEW:
4298 request |= MAY_READ;
4299 break;
4300 case KEY_NEED_WRITE:
4301 case KEY_NEED_LINK:
4302 case KEY_NEED_SETATTR:
4303 request |= MAY_WRITE;
4304 break;
4305 case KEY_NEED_UNSPECIFIED:
4306 case KEY_NEED_UNLINK:
4307 case KEY_SYSADMIN_OVERRIDE:
4308 case KEY_AUTHTOKEN_OVERRIDE:
4309 case KEY_DEFER_PERM_CHECK:
4310 return 0;
4311 default:
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004312 return -EINVAL;
David Howells8c0637e2020-05-12 15:16:29 +01004313 }
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004314
Casey Schauflere114e472008-02-04 22:29:50 -08004315 keyp = key_ref_to_ptr(key_ref);
4316 if (keyp == NULL)
4317 return -EINVAL;
4318 /*
4319 * If the key hasn't been initialized give it access so that
4320 * it may do so.
4321 */
4322 if (keyp->security == NULL)
4323 return 0;
4324 /*
4325 * This should not occur
4326 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004327 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004328 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004329
David Howellsa8478a62020-01-14 17:07:13 +00004330 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004331 return 0;
4332
Etienne Bassetecfcc532009-04-08 20:40:06 +02004333#ifdef CONFIG_AUDIT
4334 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4335 ad.a.u.key_struct.key = keyp->serial;
4336 ad.a.u.key_struct.key_desc = keyp->description;
4337#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07004338 rc = smk_access(tkp, keyp->security, request, &ad);
4339 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4340 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004341}
José Bollo7fc5f362015-02-17 15:41:22 +01004342
4343/*
4344 * smack_key_getsecurity - Smack label tagging the key
4345 * @key points to the key to be queried
4346 * @_buffer points to a pointer that should be set to point to the
4347 * resulting string (if no label or an error occurs).
4348 * Return the length of the string (including terminating NUL) or -ve if
4349 * an error.
4350 * May also return 0 (and a NULL buffer pointer) if there is no label.
4351 */
4352static int smack_key_getsecurity(struct key *key, char **_buffer)
4353{
4354 struct smack_known *skp = key->security;
4355 size_t length;
4356 char *copy;
4357
4358 if (key->security == NULL) {
4359 *_buffer = NULL;
4360 return 0;
4361 }
4362
4363 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4364 if (copy == NULL)
4365 return -ENOMEM;
4366 length = strlen(copy) + 1;
4367
4368 *_buffer = copy;
4369 return length;
4370}
4371
David Howellsa8478a62020-01-14 17:07:13 +00004372
4373#ifdef CONFIG_KEY_NOTIFICATIONS
4374/**
4375 * smack_watch_key - Smack access to watch a key for notifications.
4376 * @key: The key to be watched
4377 *
4378 * Return 0 if the @watch->cred has permission to read from the key object and
4379 * an error otherwise.
4380 */
4381static int smack_watch_key(struct key *key)
4382{
4383 struct smk_audit_info ad;
4384 struct smack_known *tkp = smk_of_current();
4385 int rc;
4386
4387 if (key == NULL)
4388 return -EINVAL;
4389 /*
4390 * If the key hasn't been initialized give it access so that
4391 * it may do so.
4392 */
4393 if (key->security == NULL)
4394 return 0;
4395 /*
4396 * This should not occur
4397 */
4398 if (tkp == NULL)
4399 return -EACCES;
4400
4401 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4402 return 0;
4403
4404#ifdef CONFIG_AUDIT
4405 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4406 ad.a.u.key_struct.key = key->serial;
4407 ad.a.u.key_struct.key_desc = key->description;
4408#endif
4409 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4410 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4411 return rc;
4412}
4413#endif /* CONFIG_KEY_NOTIFICATIONS */
Casey Schauflere114e472008-02-04 22:29:50 -08004414#endif /* CONFIG_KEYS */
4415
David Howellsa8478a62020-01-14 17:07:13 +00004416#ifdef CONFIG_WATCH_QUEUE
4417/**
4418 * smack_post_notification - Smack access to post a notification to a queue
4419 * @w_cred: The credentials of the watcher.
4420 * @cred: The credentials of the event source (may be NULL).
4421 * @n: The notification message to be posted.
4422 */
4423static int smack_post_notification(const struct cred *w_cred,
4424 const struct cred *cred,
4425 struct watch_notification *n)
4426{
4427 struct smk_audit_info ad;
4428 struct smack_known *subj, *obj;
4429 int rc;
4430
4431 /* Always let maintenance notifications through. */
4432 if (n->type == WATCH_TYPE_META)
4433 return 0;
4434
4435 if (!cred)
4436 return 0;
4437 subj = smk_of_task(smack_cred(cred));
4438 obj = smk_of_task(smack_cred(w_cred));
4439
4440 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4441 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4442 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4443 return rc;
4444}
4445#endif /* CONFIG_WATCH_QUEUE */
4446
Casey Schauflere114e472008-02-04 22:29:50 -08004447/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004448 * Smack Audit hooks
4449 *
4450 * Audit requires a unique representation of each Smack specific
4451 * rule. This unique representation is used to distinguish the
4452 * object to be audited from remaining kernel objects and also
4453 * works as a glue between the audit hooks.
4454 *
4455 * Since repository entries are added but never deleted, we'll use
4456 * the smack_known label address related to the given audit rule as
4457 * the needed unique representation. This also better fits the smack
4458 * model where nearly everything is a label.
4459 */
4460#ifdef CONFIG_AUDIT
4461
4462/**
4463 * smack_audit_rule_init - Initialize a smack audit rule
4464 * @field: audit rule fields given from user-space (audit.h)
4465 * @op: required testing operator (=, !=, >, <, ...)
4466 * @rulestr: smack label to be audited
4467 * @vrule: pointer to save our own audit rule representation
4468 *
4469 * Prepare to audit cases where (@field @op @rulestr) is true.
4470 * The label to be audited is created if necessay.
4471 */
4472static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4473{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004474 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004475 char **rule = (char **)vrule;
4476 *rule = NULL;
4477
4478 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4479 return -EINVAL;
4480
Al Viro5af75d82008-12-16 05:59:26 -05004481 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004482 return -EINVAL;
4483
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004484 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004485 if (IS_ERR(skp))
4486 return PTR_ERR(skp);
4487
4488 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004489
4490 return 0;
4491}
4492
4493/**
4494 * smack_audit_rule_known - Distinguish Smack audit rules
4495 * @krule: rule of interest, in Audit kernel representation format
4496 *
4497 * This is used to filter Smack rules from remaining Audit ones.
4498 * If it's proved that this rule belongs to us, the
4499 * audit_rule_match hook will be called to do the final judgement.
4500 */
4501static int smack_audit_rule_known(struct audit_krule *krule)
4502{
4503 struct audit_field *f;
4504 int i;
4505
4506 for (i = 0; i < krule->field_count; i++) {
4507 f = &krule->fields[i];
4508
4509 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4510 return 1;
4511 }
4512
4513 return 0;
4514}
4515
4516/**
4517 * smack_audit_rule_match - Audit given object ?
4518 * @secid: security id for identifying the object to test
4519 * @field: audit rule flags given from user-space
4520 * @op: required testing operator
4521 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004522 *
4523 * The core Audit hook. It's used to take the decision of
4524 * whether to audit or not to audit a given object.
4525 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004526static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004527{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004528 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004529 char *rule = vrule;
4530
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004531 if (unlikely(!rule)) {
4532 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004533 return -ENOENT;
4534 }
4535
4536 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4537 return 0;
4538
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004539 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004540
4541 /*
4542 * No need to do string comparisons. If a match occurs,
4543 * both pointers will point to the same smack_known
4544 * label.
4545 */
Al Viro5af75d82008-12-16 05:59:26 -05004546 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004547 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004548 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004549 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004550
4551 return 0;
4552}
4553
Casey Schaufler491a0b02016-01-26 15:08:35 -08004554/*
4555 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004556 * No memory was allocated.
4557 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004558
4559#endif /* CONFIG_AUDIT */
4560
Randy Dunlap251a2a92009-02-18 11:42:33 -08004561/**
David Quigley746df9b2013-05-22 12:50:35 -04004562 * smack_ismaclabel - check if xattr @name references a smack MAC label
4563 * @name: Full xattr name to check.
4564 */
4565static int smack_ismaclabel(const char *name)
4566{
4567 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4568}
4569
4570
4571/**
Casey Schauflere114e472008-02-04 22:29:50 -08004572 * smack_secid_to_secctx - return the smack label for a secid
4573 * @secid: incoming integer
4574 * @secdata: destination
4575 * @seclen: how long it is
4576 *
4577 * Exists for networking code.
4578 */
4579static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4580{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004581 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004582
Eric Parisd5630b92010-10-13 16:24:48 -04004583 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004584 *secdata = skp->smk_known;
4585 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004586 return 0;
4587}
4588
Randy Dunlap251a2a92009-02-18 11:42:33 -08004589/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004590 * smack_secctx_to_secid - return the secid for a smack label
4591 * @secdata: smack label
4592 * @seclen: how long result is
4593 * @secid: outgoing integer
4594 *
4595 * Exists for audit and networking code.
4596 */
David Howellse52c17642008-04-29 20:52:51 +01004597static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004598{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004599 struct smack_known *skp = smk_find_entry(secdata);
4600
4601 if (skp)
4602 *secid = skp->smk_secid;
4603 else
4604 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004605 return 0;
4606}
4607
Casey Schaufler491a0b02016-01-26 15:08:35 -08004608/*
4609 * There used to be a smack_release_secctx hook
4610 * that did nothing back when hooks were in a vector.
4611 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004612 */
Casey Schauflere114e472008-02-04 22:29:50 -08004613
David P. Quigley1ee65e32009-09-03 14:25:57 -04004614static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4615{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01004616 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4617 ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004618}
4619
4620static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4621{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01004622 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4623 ctx, ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004624}
4625
4626static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4627{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004628 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004629
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004630 *ctx = skp->smk_known;
4631 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004632 return 0;
4633}
4634
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004635static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4636{
4637
4638 struct task_smack *tsp;
4639 struct smack_known *skp;
4640 struct inode_smack *isp;
4641 struct cred *new_creds = *new;
4642
4643 if (new_creds == NULL) {
4644 new_creds = prepare_creds();
4645 if (new_creds == NULL)
4646 return -ENOMEM;
4647 }
4648
Casey Schauflerb17103a2018-11-09 16:12:56 -08004649 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004650
4651 /*
4652 * Get label from overlay inode and set it in create_sid
4653 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004654 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004655 skp = isp->smk_inode;
4656 tsp->smk_task = skp;
4657 *new = new_creds;
4658 return 0;
4659}
4660
4661static int smack_inode_copy_up_xattr(const char *name)
4662{
4663 /*
4664 * Return 1 if this is the smack access Smack attribute.
4665 */
4666 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4667 return 1;
4668
4669 return -EOPNOTSUPP;
4670}
4671
4672static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4673 struct qstr *name,
4674 const struct cred *old,
4675 struct cred *new)
4676{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004677 struct task_smack *otsp = smack_cred(old);
4678 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004679 struct inode_smack *isp;
4680 int may;
4681
4682 /*
4683 * Use the process credential unless all of
4684 * the transmuting criteria are met
4685 */
4686 ntsp->smk_task = otsp->smk_task;
4687
4688 /*
4689 * the attribute of the containing directory
4690 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004691 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004692
4693 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4694 rcu_read_lock();
4695 may = smk_access_entry(otsp->smk_task->smk_known,
4696 isp->smk_inode->smk_known,
4697 &otsp->smk_task->smk_rules);
4698 rcu_read_unlock();
4699
4700 /*
4701 * If the directory is transmuting and the rule
4702 * providing access is transmuting use the containing
4703 * directory label instead of the process label.
4704 */
4705 if (may > 0 && (may & MAY_TRANSMUTE))
4706 ntsp->smk_task = isp->smk_inode;
4707 }
4708 return 0;
4709}
4710
Casey Schauflerbbd36622018-11-12 09:30:56 -08004711struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4712 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004713 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004714 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004715 .lbs_ipc = sizeof(struct smack_known *),
4716 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004717};
4718
James Morrisca97d932017-02-15 00:18:51 +11004719static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004720 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4721 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4722 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004723
Al Viro0b520752018-12-23 16:02:47 -05004724 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004725 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4726
Casey Schauflere20b0432015-05-02 15:11:36 -07004727 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4728 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004729 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004730 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004731 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304732 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004733
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05004734 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004735
Casey Schauflere20b0432015-05-02 15:11:36 -07004736 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004737 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4738 LSM_HOOK_INIT(inode_link, smack_inode_link),
4739 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4740 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4741 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4742 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4743 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4744 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4745 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4746 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4747 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4748 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4749 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4750 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4751 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4752 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004753
Casey Schauflere20b0432015-05-02 15:11:36 -07004754 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004755 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4756 LSM_HOOK_INIT(file_lock, smack_file_lock),
4757 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4758 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4759 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4760 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4761 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4762 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004763
Casey Schauflere20b0432015-05-02 15:11:36 -07004764 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004765
Casey Schauflere20b0432015-05-02 15:11:36 -07004766 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4767 LSM_HOOK_INIT(cred_free, smack_cred_free),
4768 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4769 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004770 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004771 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4772 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4773 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4774 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4775 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
Paul Moore1fb057d2021-02-19 15:04:58 -05004776 LSM_HOOK_INIT(task_getsecid_subj, smack_task_getsecid_subj),
4777 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
Casey Schauflere20b0432015-05-02 15:11:36 -07004778 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4779 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4780 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4781 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4782 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4783 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4784 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004785 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004786
Casey Schauflere20b0432015-05-02 15:11:36 -07004787 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4788 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004789
Casey Schauflere20b0432015-05-02 15:11:36 -07004790 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004791
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004792 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004793 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4794 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4795 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4796 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004797
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004798 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004799 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4800 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4801 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004802
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004803 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004804 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4805 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4806 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004807
Casey Schauflere20b0432015-05-02 15:11:36 -07004808 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004809
Casey Schauflere20b0432015-05-02 15:11:36 -07004810 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4811 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004812
Casey Schauflere20b0432015-05-02 15:11:36 -07004813 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4814 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004815
Casey Schauflere20b0432015-05-02 15:11:36 -07004816 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004817 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004818#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004819 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004820#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004821 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4822 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4823 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4824 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4825 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4826 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4827 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4828 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4829 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4830 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004831
Casey Schauflere114e472008-02-04 22:29:50 -08004832 /* key management security hooks */
4833#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004834 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4835 LSM_HOOK_INIT(key_free, smack_key_free),
4836 LSM_HOOK_INIT(key_permission, smack_key_permission),
4837 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
David Howellsa8478a62020-01-14 17:07:13 +00004838#ifdef CONFIG_KEY_NOTIFICATIONS
4839 LSM_HOOK_INIT(watch_key, smack_watch_key),
4840#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004841#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004842
David Howellsa8478a62020-01-14 17:07:13 +00004843#ifdef CONFIG_WATCH_QUEUE
4844 LSM_HOOK_INIT(post_notification, smack_post_notification),
4845#endif
4846
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004847 /* Audit hooks */
4848#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004849 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4850 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4851 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004852#endif /* CONFIG_AUDIT */
4853
Casey Schauflere20b0432015-05-02 15:11:36 -07004854 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4855 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4856 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004857 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4858 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4859 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004860 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4861 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4862 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004863};
4864
Etienne Basset7198e2e2009-03-24 20:53:24 +01004865
Casey Schaufler86812bb2012-04-17 18:55:46 -07004866static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004867{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004868 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004869 * Initialize rule list locks
4870 */
4871 mutex_init(&smack_known_huh.smk_rules_lock);
4872 mutex_init(&smack_known_hat.smk_rules_lock);
4873 mutex_init(&smack_known_floor.smk_rules_lock);
4874 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004875 mutex_init(&smack_known_web.smk_rules_lock);
4876 /*
4877 * Initialize rule lists
4878 */
4879 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4880 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4881 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4882 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004883 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4884 /*
4885 * Create the known labels list
4886 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004887 smk_insert_entry(&smack_known_huh);
4888 smk_insert_entry(&smack_known_hat);
4889 smk_insert_entry(&smack_known_star);
4890 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004891 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004892}
4893
Casey Schauflere114e472008-02-04 22:29:50 -08004894/**
4895 * smack_init - initialize the smack system
4896 *
luanshia1a07f22019-07-05 10:35:20 +08004897 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004898 */
4899static __init int smack_init(void)
4900{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004901 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004902 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004903
Casey Schaufler4e328b02019-04-02 11:37:12 -07004904 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
Casey Schaufler4ca75282020-04-28 15:00:26 -07004905 if (!smack_rule_cache)
Casey Schaufler4e328b02019-04-02 11:37:12 -07004906 return -ENOMEM;
Casey Schaufler4e328b02019-04-02 11:37:12 -07004907
Casey Schauflerbbd36622018-11-12 09:30:56 -08004908 /*
4909 * Set the security state for the initial task.
4910 */
4911 tsp = smack_cred(cred);
4912 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4913
4914 /*
4915 * Register with LSM
4916 */
4917 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004918 smack_enabled = 1;
4919
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004920 pr_info("Smack: Initializing.\n");
4921#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4922 pr_info("Smack: Netfilter enabled.\n");
4923#endif
4924#ifdef SMACK_IPV6_PORT_LABELING
4925 pr_info("Smack: IPv6 port labeling enabled.\n");
4926#endif
4927#ifdef SMACK_IPV6_SECMARK_LABELING
4928 pr_info("Smack: IPv6 Netfilter enabled.\n");
4929#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004930
Casey Schaufler86812bb2012-04-17 18:55:46 -07004931 /* initialize the smack_known_list */
4932 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004933
Casey Schauflere114e472008-02-04 22:29:50 -08004934 return 0;
4935}
4936
4937/*
4938 * Smack requires early initialization in order to label
4939 * all processes and objects when they are created.
4940 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004941DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004942 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004943 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004944 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004945 .init = smack_init,
4946};