blob: 84df97d0d3c58022819a9ef78a1df8d7036857a5 [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;
Austin Kimbfc3cac2021-06-29 14:41:44 +010057int smack_enabled __initdata;
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{
Casey Schaufler1aea7802021-04-22 17:41:15 +0200538 struct superblock_smack *sbsp = smack_superblock(sb);
Casey Schauflere114e472008-02-04 22:29:50 -0800539
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200540 sbsp->smk_root = &smack_known_floor;
541 sbsp->smk_default = &smack_known_floor;
542 sbsp->smk_floor = &smack_known_floor;
543 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700544 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500545 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700546 */
Casey Schauflere114e472008-02-04 22:29:50 -0800547
548 return 0;
549}
550
Al Viro12085b12018-12-13 15:18:05 -0500551struct smack_mnt_opts {
552 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
553};
554
Al Viro204cc0c2018-12-13 13:41:47 -0500555static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800556{
Al Viro12085b12018-12-13 15:18:05 -0500557 struct smack_mnt_opts *opts = mnt_opts;
558 kfree(opts->fsdefault);
559 kfree(opts->fsfloor);
560 kfree(opts->fshat);
561 kfree(opts->fsroot);
562 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500563 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800564}
565
Al Viro55c0e5b2018-12-16 01:09:45 -0500566static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530567{
Al Viro55c0e5b2018-12-16 01:09:45 -0500568 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530569
Al Viro55c0e5b2018-12-16 01:09:45 -0500570 if (!opts) {
571 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
572 if (!opts)
573 return -ENOMEM;
574 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530575 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500576 if (!s)
577 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530578
Al Viro55c0e5b2018-12-16 01:09:45 -0500579 switch (token) {
580 case Opt_fsdefault:
581 if (opts->fsdefault)
582 goto out_opt_err;
583 opts->fsdefault = s;
584 break;
585 case Opt_fsfloor:
586 if (opts->fsfloor)
587 goto out_opt_err;
588 opts->fsfloor = s;
589 break;
590 case Opt_fshat:
591 if (opts->fshat)
592 goto out_opt_err;
593 opts->fshat = s;
594 break;
595 case Opt_fsroot:
596 if (opts->fsroot)
597 goto out_opt_err;
598 opts->fsroot = s;
599 break;
600 case Opt_fstransmute:
601 if (opts->fstransmute)
602 goto out_opt_err;
603 opts->fstransmute = s;
604 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530605 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530606 return 0;
607
608out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530609 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500610 return -EINVAL;
611}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530612
Al Viro0b520752018-12-23 16:02:47 -0500613/**
614 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
615 * @fc: The new filesystem context.
616 * @src_fc: The source filesystem context being duplicated.
617 *
618 * Returns 0 on success or -ENOMEM on error.
619 */
620static int smack_fs_context_dup(struct fs_context *fc,
621 struct fs_context *src_fc)
622{
623 struct smack_mnt_opts *dst, *src = src_fc->security;
624
625 if (!src)
626 return 0;
627
628 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
629 if (!fc->security)
630 return -ENOMEM;
631 dst = fc->security;
632
633 if (src->fsdefault) {
634 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
635 if (!dst->fsdefault)
636 return -ENOMEM;
637 }
638 if (src->fsfloor) {
639 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
640 if (!dst->fsfloor)
641 return -ENOMEM;
642 }
643 if (src->fshat) {
644 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
645 if (!dst->fshat)
646 return -ENOMEM;
647 }
648 if (src->fsroot) {
649 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
650 if (!dst->fsroot)
651 return -ENOMEM;
652 }
653 if (src->fstransmute) {
654 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
655 if (!dst->fstransmute)
656 return -ENOMEM;
657 }
658 return 0;
659}
660
Al Virod7167b12019-09-07 07:23:15 -0400661static const struct fs_parameter_spec smack_fs_parameters[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +0100662 fsparam_string("smackfsdef", Opt_fsdefault),
663 fsparam_string("smackfsdefault", Opt_fsdefault),
664 fsparam_string("smackfsfloor", Opt_fsfloor),
665 fsparam_string("smackfshat", Opt_fshat),
666 fsparam_string("smackfsroot", Opt_fsroot),
667 fsparam_string("smackfstransmute", Opt_fstransmute),
David Howells2febd252018-11-01 23:07:24 +0000668 {}
669};
670
David Howells2febd252018-11-01 23:07:24 +0000671/**
672 * smack_fs_context_parse_param - Parse a single mount parameter
673 * @fc: The new filesystem context being constructed.
674 * @param: The parameter.
675 *
676 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
677 * error.
678 */
679static int smack_fs_context_parse_param(struct fs_context *fc,
680 struct fs_parameter *param)
681{
682 struct fs_parse_result result;
683 int opt, rc;
684
Al Virod7167b12019-09-07 07:23:15 -0400685 opt = fs_parse(fc, smack_fs_parameters, param, &result);
David Howells2febd252018-11-01 23:07:24 +0000686 if (opt < 0)
687 return opt;
688
689 rc = smack_add_opt(opt, param->string, &fc->security);
690 if (!rc)
691 param->string = NULL;
692 return rc;
693}
694
Al Virod2497e12018-12-16 01:37:06 -0500695static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530696{
Al Virod2497e12018-12-16 01:37:06 -0500697 char *from = options, *to = options;
698 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530699
Al Viroc3300aa2018-12-16 01:52:24 -0500700 while (1) {
701 char *next = strchr(from, ',');
702 int token, len, rc;
703 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530704
Al Viroc3300aa2018-12-16 01:52:24 -0500705 if (next)
706 len = next - from;
707 else
708 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530709
Al Viroc3300aa2018-12-16 01:52:24 -0500710 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500711 if (token != Opt_error) {
712 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
713 rc = smack_add_opt(token, arg, mnt_opts);
714 if (unlikely(rc)) {
715 kfree(arg);
716 if (*mnt_opts)
717 smack_free_mnt_opts(*mnt_opts);
718 *mnt_opts = NULL;
719 return rc;
720 }
721 } else {
722 if (!first) { // copy with preceding comma
723 from--;
724 len++;
725 }
726 if (to != from)
727 memmove(to, from, len);
728 to += len;
729 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530730 }
Al Viroc3300aa2018-12-16 01:52:24 -0500731 if (!from[len])
732 break;
733 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530734 }
Al Virod2497e12018-12-16 01:37:06 -0500735 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530736 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530737}
738
739/**
740 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800741 * @sb: the file system superblock
luanshia1a07f22019-07-05 10:35:20 +0800742 * @mnt_opts: Smack mount options
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530743 * @kern_flags: mount option from kernel space or user space
744 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800745 *
746 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530747 *
748 * Allow filesystems with binary mount data to explicitly set Smack mount
749 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800750 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530751static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500752 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530753 unsigned long kern_flags,
754 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800755{
756 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000757 struct inode *inode = d_backing_inode(root);
Casey Schaufler1aea7802021-04-22 17:41:15 +0200758 struct superblock_smack *sp = smack_superblock(sb);
Casey Schauflere114e472008-02-04 22:29:50 -0800759 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800760 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500761 struct smack_mnt_opts *opts = mnt_opts;
762 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800763
Seth Forshee9f50eda2015-09-23 15:16:06 -0500764 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800765 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700766
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700767 if (inode->i_security == NULL) {
768 int rc = lsm_inode_alloc(inode);
769
770 if (rc)
771 return rc;
772 }
773
Himanshu Shukla2097f592016-11-10 16:19:52 +0530774 if (!smack_privileged(CAP_MAC_ADMIN)) {
775 /*
776 * Unprivileged mounts don't get to specify Smack values.
777 */
Al Viro12085b12018-12-13 15:18:05 -0500778 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530779 return -EPERM;
780 /*
781 * Unprivileged mounts get root and default from the caller.
782 */
783 skp = smk_of_current();
784 sp->smk_root = skp;
785 sp->smk_default = skp;
786 /*
787 * For a handful of fs types with no user-controlled
788 * backing store it's okay to trust security labels
789 * in the filesystem. The rest are untrusted.
790 */
791 if (sb->s_user_ns != &init_user_ns &&
792 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
793 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500794 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530795 sp->smk_flags |= SMK_SB_UNTRUSTED;
796 }
797 }
798
Seth Forshee9f50eda2015-09-23 15:16:06 -0500799 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800800
Al Viro12085b12018-12-13 15:18:05 -0500801 if (opts) {
802 if (opts->fsdefault) {
803 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200804 if (IS_ERR(skp))
805 return PTR_ERR(skp);
806 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500807 }
808 if (opts->fsfloor) {
809 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530810 if (IS_ERR(skp))
811 return PTR_ERR(skp);
812 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500813 }
814 if (opts->fshat) {
815 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530816 if (IS_ERR(skp))
817 return PTR_ERR(skp);
818 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500819 }
820 if (opts->fsroot) {
821 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200822 if (IS_ERR(skp))
823 return PTR_ERR(skp);
824 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500825 }
826 if (opts->fstransmute) {
827 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200828 if (IS_ERR(skp))
829 return PTR_ERR(skp);
830 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500831 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800832 }
833 }
834
835 /*
836 * Initialize the root inode.
837 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700838 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800839
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700840 if (transmute) {
841 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700842 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700843 }
Casey Schauflere830b392013-05-22 18:43:07 -0700844
Casey Schauflere114e472008-02-04 22:29:50 -0800845 return 0;
846}
847
848/**
849 * smack_sb_statfs - Smack check on statfs
850 * @dentry: identifies the file system in question
851 *
852 * Returns 0 if current can read the floor of the filesystem,
853 * and error code otherwise
854 */
855static int smack_sb_statfs(struct dentry *dentry)
856{
Casey Schaufler1aea7802021-04-22 17:41:15 +0200857 struct superblock_smack *sbp = smack_superblock(dentry->d_sb);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200858 int rc;
859 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800860
Eric Parisa2694342011-04-25 13:10:27 -0400861 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200862 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
863
864 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700865 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200866 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800867}
868
Casey Schauflere114e472008-02-04 22:29:50 -0800869/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800870 * BPRM hooks
871 */
872
Casey Schauflerce8a4322011-09-29 18:21:01 -0700873/**
Eric W. Biedermanb8bff592020-03-22 15:46:24 -0500874 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
Casey Schauflerce8a4322011-09-29 18:21:01 -0700875 * @bprm: the exec information
876 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100877 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700878 */
Eric W. Biedermanb8bff592020-03-22 15:46:24 -0500879static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
Casey Schaufler676dac42010-12-02 06:43:39 -0800880{
Al Viro496ad9a2013-01-23 17:07:38 -0500881 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800882 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800883 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500884 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800885 int rc;
886
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800887 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300888 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800889 return 0;
890
Casey Schaufler1aea7802021-04-22 17:41:15 +0200891 sbsp = smack_superblock(inode->i_sb);
Seth Forshee809c02e2016-04-26 14:36:22 -0500892 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
893 isp->smk_task != sbsp->smk_root)
894 return 0;
895
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300896 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100897 struct task_struct *tracer;
898 rc = 0;
899
900 rcu_read_lock();
901 tracer = ptrace_parent(current);
902 if (likely(tracer != NULL))
903 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200904 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100905 PTRACE_MODE_ATTACH,
906 __func__);
907 rcu_read_unlock();
908
909 if (rc != 0)
910 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200911 }
912 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300913 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800914
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300915 bsp->smk_task = isp->smk_task;
916 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800917
Kees Cookccbb6e12017-07-18 15:25:26 -0700918 /* Decide if this is a secure exec. */
919 if (bsp->smk_task != bsp->smk_forked)
920 bprm->secureexec = 1;
921
Casey Schaufler676dac42010-12-02 06:43:39 -0800922 return 0;
923}
924
925/*
Casey Schauflere114e472008-02-04 22:29:50 -0800926 * Inode hooks
927 */
928
929/**
930 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800931 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800932 *
luanshia1a07f22019-07-05 10:35:20 +0800933 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800934 */
935static int smack_inode_alloc_security(struct inode *inode)
936{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700937 struct smack_known *skp = smk_of_current();
938
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700939 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800940 return 0;
941}
942
943/**
Casey Schauflere114e472008-02-04 22:29:50 -0800944 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200945 * @inode: the newly created inode
946 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500947 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800948 * @name: where to put the attribute name
949 * @value: where to put the attribute value
950 * @len: where to put the length of the attribute
951 *
952 * Returns 0 if it all works out, -ENOMEM if there's no memory
953 */
954static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900955 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500956 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800957{
Roberto Sassu3586b3f2023-05-08 19:02:34 +0200958 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800959 struct inode_smack *issp = smack_inode(inode);
Roberto Sassu3586b3f2023-05-08 19:02:34 +0200960 struct smack_known *skp = smk_of_task(tsp);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200961 struct smack_known *isp = smk_of_inode(inode);
962 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800963 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800964
Tetsuo Handa95489062013-07-25 05:44:02 +0900965 if (name)
966 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800967
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100968 if (value && len) {
Roberto Sassu3586b3f2023-05-08 19:02:34 +0200969 /*
970 * If equal, transmuting already occurred in
971 * smack_dentry_create_files_as(). No need to check again.
972 */
973 if (tsp->smk_task != tsp->smk_transmuted) {
974 rcu_read_lock();
975 may = smk_access_entry(skp->smk_known, dsp->smk_known,
976 &skp->smk_rules);
977 rcu_read_unlock();
978 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200979
980 /*
Roberto Sassu3586b3f2023-05-08 19:02:34 +0200981 * In addition to having smk_task equal to smk_transmuted,
982 * if the access rule allows transmutation and the directory
983 * requests transmutation then by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700984 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200985 */
Roberto Sassu3586b3f2023-05-08 19:02:34 +0200986 if ((tsp->smk_task == tsp->smk_transmuted) ||
987 (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
988 smk_inode_transmutable(dir))) {
989 /*
990 * The caller of smack_dentry_create_files_as()
991 * should have overridden the current cred, so the
992 * inode label was already set correctly in
993 * smack_inode_alloc_security().
994 */
995 if (tsp->smk_task != tsp->smk_transmuted)
996 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700997 issp->smk_flags |= SMK_INODE_CHANGED;
998 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200999
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001000 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001001 if (*value == NULL)
1002 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001003
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001004 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001005 }
Casey Schauflere114e472008-02-04 22:29:50 -08001006
1007 return 0;
1008}
1009
1010/**
1011 * smack_inode_link - Smack check on link
1012 * @old_dentry: the existing object
1013 * @dir: unused
1014 * @new_dentry: the new object
1015 *
1016 * Returns 0 if access is permitted, an error code otherwise
1017 */
1018static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1019 struct dentry *new_dentry)
1020{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001021 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001022 struct smk_audit_info ad;
1023 int rc;
1024
Eric Parisa2694342011-04-25 13:10:27 -04001025 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001026 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001027
David Howellsc6f493d2015-03-17 22:26:22 +00001028 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001029 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001030 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001031
David Howells88025652015-01-29 12:02:32 +00001032 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001033 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001034 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1035 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001036 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001037 }
1038
1039 return rc;
1040}
1041
1042/**
1043 * smack_inode_unlink - Smack check on inode deletion
1044 * @dir: containing directory object
1045 * @dentry: file to unlink
1046 *
1047 * Returns 0 if current can write the containing directory
1048 * and the object, error code otherwise
1049 */
1050static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1051{
David Howellsc6f493d2015-03-17 22:26:22 +00001052 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001053 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001054 int rc;
1055
Eric Parisa2694342011-04-25 13:10:27 -04001056 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001057 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1058
Casey Schauflere114e472008-02-04 22:29:50 -08001059 /*
1060 * You need write access to the thing you're unlinking
1061 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001062 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001063 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001064 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001065 /*
1066 * You also need write access to the containing directory
1067 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001068 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001069 smk_ad_setfield_u_fs_inode(&ad, dir);
1070 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001071 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001072 }
Casey Schauflere114e472008-02-04 22:29:50 -08001073 return rc;
1074}
1075
1076/**
1077 * smack_inode_rmdir - Smack check on directory deletion
1078 * @dir: containing directory object
1079 * @dentry: directory to unlink
1080 *
1081 * Returns 0 if current can write the containing directory
1082 * and the directory, error code otherwise
1083 */
1084static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1085{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001086 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001087 int rc;
1088
Eric Parisa2694342011-04-25 13:10:27 -04001089 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001090 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1091
Casey Schauflere114e472008-02-04 22:29:50 -08001092 /*
1093 * You need write access to the thing you're removing
1094 */
David Howellsc6f493d2015-03-17 22:26:22 +00001095 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1096 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001097 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001098 /*
1099 * You also need write access to the containing directory
1100 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001101 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 smk_ad_setfield_u_fs_inode(&ad, dir);
1103 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001104 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001105 }
Casey Schauflere114e472008-02-04 22:29:50 -08001106
1107 return rc;
1108}
1109
1110/**
1111 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001112 * @old_inode: unused
1113 * @old_dentry: the old object
1114 * @new_inode: unused
1115 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001116 *
1117 * Read and write access is required on both the old and
1118 * new directories.
1119 *
1120 * Returns 0 if access is permitted, an error code otherwise
1121 */
1122static int smack_inode_rename(struct inode *old_inode,
1123 struct dentry *old_dentry,
1124 struct inode *new_inode,
1125 struct dentry *new_dentry)
1126{
1127 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001128 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001129 struct smk_audit_info ad;
1130
Eric Parisa2694342011-04-25 13:10:27 -04001131 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001132 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001133
David Howellsc6f493d2015-03-17 22:26:22 +00001134 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001135 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001136 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001137
David Howells88025652015-01-29 12:02:32 +00001138 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001139 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001140 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1141 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001142 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001143 }
Casey Schauflere114e472008-02-04 22:29:50 -08001144 return rc;
1145}
1146
1147/**
1148 * smack_inode_permission - Smack version of permission()
1149 * @inode: the inode in question
1150 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001151 *
1152 * This is the important Smack hook.
1153 *
luanshia1a07f22019-07-05 10:35:20 +08001154 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001155 */
Al Viroe74f71e2011-06-20 19:38:15 -04001156static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001157{
Casey Schaufler1aea7802021-04-22 17:41:15 +02001158 struct superblock_smack *sbsp = smack_superblock(inode->i_sb);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001159 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001160 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001161 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001162
1163 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001164 /*
1165 * No permission to check. Existence test. Yup, it's there.
1166 */
1167 if (mask == 0)
1168 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001169
Seth Forshee9f50eda2015-09-23 15:16:06 -05001170 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1171 if (smk_of_inode(inode) != sbsp->smk_root)
1172 return -EACCES;
1173 }
1174
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001175 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001176 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001177 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001178 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001179 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001180 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1181 rc = smk_bu_inode(inode, mask, rc);
1182 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001183}
1184
1185/**
1186 * smack_inode_setattr - Smack check for setting attributes
1187 * @dentry: the object
1188 * @iattr: for the force flag
1189 *
1190 * Returns 0 if access is permitted, an error code otherwise
1191 */
1192static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1193{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001194 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001195 int rc;
1196
Casey Schauflere114e472008-02-04 22:29:50 -08001197 /*
1198 * Need to allow for clearing the setuid bit.
1199 */
1200 if (iattr->ia_valid & ATTR_FORCE)
1201 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001202 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001203 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001204
David Howellsc6f493d2015-03-17 22:26:22 +00001205 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1206 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001207 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001208}
1209
1210/**
1211 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001212 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001213 *
1214 * Returns 0 if access is permitted, an error code otherwise
1215 */
Al Viro3f7036a2015-03-08 19:28:30 -04001216static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001217{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001218 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001219 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001220 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001221
Eric Parisf48b7392011-04-25 12:54:27 -04001222 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001223 smk_ad_setfield_u_fs_path(&ad, *path);
1224 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1225 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001226 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001227}
1228
1229/**
1230 * smack_inode_setxattr - Smack check for setting xattrs
1231 * @dentry: the object
1232 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001233 * @value: value of the attribute
1234 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001235 * @flags: unused
1236 *
1237 * This protects the Smack attribute explicitly.
1238 *
1239 * Returns 0 if access is permitted, an error code otherwise
1240 */
Christian Brauner71bc3562021-01-21 14:19:29 +01001241static int smack_inode_setxattr(struct user_namespace *mnt_userns,
1242 struct dentry *dentry, const char *name,
David Howells8f0cfa52008-04-29 00:59:41 -07001243 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001244{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001245 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001246 struct smack_known *skp;
1247 int check_priv = 0;
1248 int check_import = 0;
1249 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001250 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001251
Casey Schaufler19760ad2013-12-16 16:27:26 -08001252 /*
1253 * Check label validity here so import won't fail in post_setxattr
1254 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001255 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1256 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001257 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1258 check_priv = 1;
1259 check_import = 1;
1260 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1261 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1262 check_priv = 1;
1263 check_import = 1;
1264 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001265 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001266 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001267 if (size != TRANS_TRUE_SIZE ||
1268 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1269 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001270 } else
1271 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1272
Casey Schaufler19760ad2013-12-16 16:27:26 -08001273 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1274 rc = -EPERM;
1275
1276 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001277 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001278 if (IS_ERR(skp))
1279 rc = PTR_ERR(skp);
1280 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001281 (skp == &smack_known_star || skp == &smack_known_web)))
1282 rc = -EINVAL;
1283 }
1284
Eric Parisa2694342011-04-25 13:10:27 -04001285 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001286 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1287
Casey Schauflerd166c802014-08-27 14:51:27 -07001288 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001289 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1290 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001291 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001292
1293 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001294}
1295
1296/**
1297 * smack_inode_post_setxattr - Apply the Smack update approved above
1298 * @dentry: object
1299 * @name: attribute name
1300 * @value: attribute value
1301 * @size: attribute size
1302 * @flags: unused
1303 *
1304 * Set the pointer in the inode blob to the entry found
1305 * in the master label list.
1306 */
David Howells8f0cfa52008-04-29 00:59:41 -07001307static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1308 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001309{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001310 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001311 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001312
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001313 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1314 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1315 return;
1316 }
1317
Casey Schaufler676dac42010-12-02 06:43:39 -08001318 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001319 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001320 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001321 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001322 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001323 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001324 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001325 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001326 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001327 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001328 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001329 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001330 }
Casey Schauflere114e472008-02-04 22:29:50 -08001331
1332 return;
1333}
1334
Casey Schauflerce8a4322011-09-29 18:21:01 -07001335/**
Casey Schauflere114e472008-02-04 22:29:50 -08001336 * smack_inode_getxattr - Smack check on getxattr
1337 * @dentry: the object
1338 * @name: unused
1339 *
1340 * Returns 0 if access is permitted, an error code otherwise
1341 */
David Howells8f0cfa52008-04-29 00:59:41 -07001342static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001343{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001344 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001345 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001346
Eric Parisa2694342011-04-25 13:10:27 -04001347 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001348 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1349
David Howellsc6f493d2015-03-17 22:26:22 +00001350 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1351 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001352 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001353}
1354
Casey Schauflerce8a4322011-09-29 18:21:01 -07001355/**
Casey Schauflere114e472008-02-04 22:29:50 -08001356 * smack_inode_removexattr - Smack check on removexattr
1357 * @dentry: the object
1358 * @name: name of the attribute
1359 *
1360 * Removing the Smack attribute requires CAP_MAC_ADMIN
1361 *
1362 * Returns 0 if access is permitted, an error code otherwise
1363 */
Christian Brauner71bc3562021-01-21 14:19:29 +01001364static int smack_inode_removexattr(struct user_namespace *mnt_userns,
1365 struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001366{
Casey Schaufler676dac42010-12-02 06:43:39 -08001367 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001368 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001369 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001370
Casey Schauflerbcdca222008-02-23 15:24:04 -08001371 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1372 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001373 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001374 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001375 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301376 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001377 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001378 rc = -EPERM;
1379 } else
Christian Brauner71bc3562021-01-21 14:19:29 +01001380 rc = cap_inode_removexattr(mnt_userns, dentry, name);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001381
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001382 if (rc != 0)
1383 return rc;
1384
Eric Parisa2694342011-04-25 13:10:27 -04001385 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001386 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001387
David Howellsc6f493d2015-03-17 22:26:22 +00001388 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1389 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001390 if (rc != 0)
1391 return rc;
1392
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001393 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001394 /*
1395 * Don't do anything special for these.
1396 * XATTR_NAME_SMACKIPIN
1397 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001398 */
José Bollo80124952016-01-12 21:23:40 +01001399 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001400 struct super_block *sbp = dentry->d_sb;
Casey Schaufler1aea7802021-04-22 17:41:15 +02001401 struct superblock_smack *sbsp = smack_superblock(sbp);
José Bollo80124952016-01-12 21:23:40 +01001402
1403 isp->smk_inode = sbsp->smk_default;
1404 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001405 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001406 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001407 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001408 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1409 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001410
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001411 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001412}
1413
1414/**
1415 * smack_inode_getsecurity - get smack xattrs
1416 * @inode: the object
1417 * @name: attribute name
1418 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001419 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001420 *
1421 * Returns the size of the attribute or an error code
1422 */
Christian Brauner71bc3562021-01-21 14:19:29 +01001423static int smack_inode_getsecurity(struct user_namespace *mnt_userns,
1424 struct inode *inode, const char *name,
1425 void **buffer, bool alloc)
Casey Schauflere114e472008-02-04 22:29:50 -08001426{
1427 struct socket_smack *ssp;
1428 struct socket *sock;
1429 struct super_block *sbp;
1430 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001431 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001432
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001433 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001434 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001435 else {
1436 /*
1437 * The rest of the Smack xattrs are only on sockets.
1438 */
1439 sbp = ip->i_sb;
1440 if (sbp->s_magic != SOCKFS_MAGIC)
1441 return -EOPNOTSUPP;
1442
1443 sock = SOCKET_I(ip);
1444 if (sock == NULL || sock->sk == NULL)
1445 return -EOPNOTSUPP;
1446
1447 ssp = sock->sk->sk_security;
1448
1449 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1450 isp = ssp->smk_in;
1451 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1452 isp = ssp->smk_out;
1453 else
1454 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001455 }
1456
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001457 if (alloc) {
1458 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1459 if (*buffer == NULL)
1460 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001461 }
1462
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001463 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001464}
1465
1466
1467/**
1468 * smack_inode_listsecurity - list the Smack attributes
1469 * @inode: the object
1470 * @buffer: where they go
1471 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001472 */
1473static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1474 size_t buffer_size)
1475{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001476 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001477
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001478 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001479 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001480
1481 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001482}
1483
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001484/**
1485 * smack_inode_getsecid - Extract inode's security id
1486 * @inode: inode to extract the info from
1487 * @secid: where result will be saved
1488 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001489static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001490{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001491 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001492
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001493 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001494}
1495
Casey Schauflere114e472008-02-04 22:29:50 -08001496/*
1497 * File Hooks
1498 */
1499
Casey Schaufler491a0b02016-01-26 15:08:35 -08001500/*
1501 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001502 *
1503 * Should access checks be done on each read or write?
1504 * UNICOS and SELinux say yes.
1505 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1506 *
1507 * I'll say no for now. Smack does not do the frequent
1508 * label changing that SELinux does.
1509 */
Casey Schauflere114e472008-02-04 22:29:50 -08001510
1511/**
1512 * smack_file_alloc_security - assign a file security blob
1513 * @file: the object
1514 *
1515 * The security blob for a file is a pointer to the master
1516 * label list, so no allocation is done.
1517 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001518 * f_security is the owner security information. It
1519 * isn't used on file access checks, it's for send_sigio.
1520 *
Casey Schauflere114e472008-02-04 22:29:50 -08001521 * Returns 0
1522 */
1523static int smack_file_alloc_security(struct file *file)
1524{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001525 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001526
Casey Schauflerf28952a2018-11-12 09:38:53 -08001527 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001528 return 0;
1529}
1530
1531/**
Casey Schauflere114e472008-02-04 22:29:50 -08001532 * smack_file_ioctl - Smack check on ioctls
1533 * @file: the object
1534 * @cmd: what to do
1535 * @arg: unused
1536 *
1537 * Relies heavily on the correct use of the ioctl command conventions.
1538 *
1539 * Returns 0 if allowed, error code otherwise
1540 */
1541static int smack_file_ioctl(struct file *file, unsigned int cmd,
1542 unsigned long arg)
1543{
1544 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001545 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001546 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001547
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001548 if (unlikely(IS_PRIVATE(inode)))
1549 return 0;
1550
Eric Parisf48b7392011-04-25 12:54:27 -04001551 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001552 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001553
Casey Schauflerd166c802014-08-27 14:51:27 -07001554 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001555 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001556 rc = smk_bu_file(file, MAY_WRITE, rc);
1557 }
Casey Schauflere114e472008-02-04 22:29:50 -08001558
Casey Schauflerd166c802014-08-27 14:51:27 -07001559 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001560 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001561 rc = smk_bu_file(file, MAY_READ, rc);
1562 }
Casey Schauflere114e472008-02-04 22:29:50 -08001563
1564 return rc;
1565}
1566
1567/**
1568 * smack_file_lock - Smack check on file locking
1569 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001570 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001571 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001572 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001573 */
1574static int smack_file_lock(struct file *file, unsigned int cmd)
1575{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001576 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001577 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001578 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001579
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001580 if (unlikely(IS_PRIVATE(inode)))
1581 return 0;
1582
Eric Paris92f42502011-04-25 13:15:55 -04001583 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1584 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001585 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001586 rc = smk_bu_file(file, MAY_LOCK, rc);
1587 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001588}
1589
1590/**
1591 * smack_file_fcntl - Smack check on fcntl
1592 * @file: the object
1593 * @cmd: what action to check
1594 * @arg: unused
1595 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001596 * Generally these operations are harmless.
1597 * File locking operations present an obvious mechanism
1598 * for passing information, so they require write access.
1599 *
Casey Schauflere114e472008-02-04 22:29:50 -08001600 * Returns 0 if current has access, error code otherwise
1601 */
1602static int smack_file_fcntl(struct file *file, unsigned int cmd,
1603 unsigned long arg)
1604{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001605 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001606 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001607 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001608
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001609 if (unlikely(IS_PRIVATE(inode)))
1610 return 0;
1611
Casey Schauflere114e472008-02-04 22:29:50 -08001612 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001613 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001614 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001615 case F_SETLK:
1616 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001617 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1618 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001619 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001620 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001621 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001622 case F_SETOWN:
1623 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001624 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1625 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001626 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001627 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001628 break;
1629 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001630 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001631 }
1632
1633 return rc;
1634}
1635
1636/**
Al Viroe5467852012-05-30 13:30:51 -04001637 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001638 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1639 * if mapping anonymous memory.
1640 * @file contains the file structure for file to map (may be NULL).
1641 * @reqprot contains the protection requested by the application.
1642 * @prot contains the protection that will be applied by the kernel.
1643 * @flags contains the operational flags.
1644 * Return 0 if permission is granted.
1645 */
Al Viroe5467852012-05-30 13:30:51 -04001646static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001647 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001648 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001649{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001650 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001651 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001652 struct smack_rule *srp;
1653 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001654 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001655 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001656 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001657 int may;
1658 int mmay;
1659 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001660 int rc;
1661
Al Viro496ad9a2013-01-23 17:07:38 -05001662 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001663 return 0;
1664
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001665 if (unlikely(IS_PRIVATE(file_inode(file))))
1666 return 0;
1667
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001668 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001669 if (isp->smk_mmap == NULL)
1670 return 0;
Casey Schaufler1aea7802021-04-22 17:41:15 +02001671 sbsp = smack_superblock(file_inode(file)->i_sb);
Seth Forshee809c02e2016-04-26 14:36:22 -05001672 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1673 isp->smk_mmap != sbsp->smk_root)
1674 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001675 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001676
Casey Schauflerb17103a2018-11-09 16:12:56 -08001677 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001678 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001679 rc = 0;
1680
1681 rcu_read_lock();
1682 /*
1683 * For each Smack rule associated with the subject
1684 * label verify that the SMACK64MMAP also has access
1685 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001686 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001687 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001688 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001689 /*
1690 * Matching labels always allows access.
1691 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001692 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001693 continue;
1694 /*
1695 * If there is a matching local rule take
1696 * that into account as well.
1697 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001698 may = smk_access_entry(srp->smk_subject->smk_known,
1699 okp->smk_known,
1700 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001701 if (may == -ENOENT)
1702 may = srp->smk_access;
1703 else
1704 may &= srp->smk_access;
1705 /*
1706 * If may is zero the SMACK64MMAP subject can't
1707 * possibly have less access.
1708 */
1709 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001710 continue;
1711
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001712 /*
1713 * Fetch the global list entry.
1714 * If there isn't one a SMACK64MMAP subject
1715 * can't have as much access as current.
1716 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001717 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1718 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001719 if (mmay == -ENOENT) {
1720 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001721 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001722 }
1723 /*
1724 * If there is a local entry it modifies the
1725 * potential access, too.
1726 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001727 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1728 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001729 if (tmay != -ENOENT)
1730 mmay &= tmay;
1731
1732 /*
1733 * If there is any access available to current that is
1734 * not available to a SMACK64MMAP subject
1735 * deny access.
1736 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001737 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001738 rc = -EACCES;
1739 break;
1740 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001741 }
1742
1743 rcu_read_unlock();
1744
1745 return rc;
1746}
1747
1748/**
Casey Schauflere114e472008-02-04 22:29:50 -08001749 * smack_file_set_fowner - set the file security blob value
1750 * @file: object in question
1751 *
Casey Schauflere114e472008-02-04 22:29:50 -08001752 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001753static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001754{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001755 struct smack_known **blob = smack_file(file);
1756
1757 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001758}
1759
1760/**
1761 * smack_file_send_sigiotask - Smack on sigio
1762 * @tsk: The target task
1763 * @fown: the object the signal come from
1764 * @signum: unused
1765 *
1766 * Allow a privileged task to get signals even if it shouldn't
1767 *
1768 * Returns 0 if a subject with the object's smack could
1769 * write to the task, an error code otherwise.
1770 */
1771static int smack_file_send_sigiotask(struct task_struct *tsk,
1772 struct fown_struct *fown, int signum)
1773{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001774 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001775 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001776 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001777 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001778 struct file *file;
1779 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001780 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001781
1782 /*
1783 * struct fown_struct is never outside the context of a struct file
1784 */
1785 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001786
Etienne Bassetecfcc532009-04-08 20:40:06 +02001787 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001788 blob = smack_file(file);
1789 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001790 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1791 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001792
1793 rcu_read_lock();
1794 tcred = __task_cred(tsk);
1795 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001796 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001797 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001798
1799 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1800 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001801 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001802 return rc;
1803}
1804
1805/**
1806 * smack_file_receive - Smack file receive check
1807 * @file: the object
1808 *
1809 * Returns 0 if current has access, error code otherwise
1810 */
1811static int smack_file_receive(struct file *file)
1812{
Casey Schauflerd166c802014-08-27 14:51:27 -07001813 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001814 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001815 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001816 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001817 struct socket *sock;
1818 struct task_smack *tsp;
1819 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001820
Seung-Woo Kim97775822015-04-17 15:25:04 +09001821 if (unlikely(IS_PRIVATE(inode)))
1822 return 0;
1823
Casey Schaufler4482a442013-12-30 17:37:45 -08001824 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001825 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001826
Casey Schaufler51d59af2017-05-31 08:53:42 -07001827 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001828 sock = SOCKET_I(inode);
1829 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001830 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001831 /*
1832 * If the receiving process can't write to the
1833 * passed socket or if the passed socket can't
1834 * write to the receiving process don't accept
1835 * the passed socket.
1836 */
1837 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1838 rc = smk_bu_file(file, may, rc);
1839 if (rc < 0)
1840 return rc;
1841 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1842 rc = smk_bu_file(file, may, rc);
1843 return rc;
1844 }
Casey Schauflere114e472008-02-04 22:29:50 -08001845 /*
1846 * This code relies on bitmasks.
1847 */
1848 if (file->f_mode & FMODE_READ)
1849 may = MAY_READ;
1850 if (file->f_mode & FMODE_WRITE)
1851 may |= MAY_WRITE;
1852
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001853 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001854 rc = smk_bu_file(file, may, rc);
1855 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001856}
1857
Casey Schaufler531f1d42011-09-19 12:41:42 -07001858/**
Eric Paris83d49852012-04-04 13:45:40 -04001859 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001860 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001861 *
1862 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001863 * Allow the open only if the task has read access. There are
1864 * many read operations (e.g. fstat) that you can do with an
1865 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001866 *
luanshia1a07f22019-07-05 10:35:20 +08001867 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001868 */
Al Viro94817692018-07-10 14:13:18 -04001869static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001870{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001871 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001872 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001873 struct smk_audit_info ad;
1874 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001875
Casey Schauflera6834c02014-04-21 11:10:26 -07001876 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1877 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301878 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001879 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001880
1881 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001882}
1883
Casey Schauflere114e472008-02-04 22:29:50 -08001884/*
1885 * Task hooks
1886 */
1887
1888/**
David Howellsee18d642009-09-02 09:14:21 +01001889 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001890 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001891 * @gfp: the atomicity of any memory allocations
1892 *
1893 * Prepare a blank set of credentials for modification. This must allocate all
1894 * the memory the LSM module might require such that cred_transfer() can
1895 * complete without error.
1896 */
1897static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1898{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001899 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001900 return 0;
1901}
1902
1903
1904/**
David Howellsf1752ee2008-11-14 10:39:17 +11001905 * smack_cred_free - "free" task-level security credentials
1906 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001907 *
Casey Schauflere114e472008-02-04 22:29:50 -08001908 */
David Howellsf1752ee2008-11-14 10:39:17 +11001909static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001910{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001911 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001912 struct smack_rule *rp;
1913 struct list_head *l;
1914 struct list_head *n;
1915
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001916 smk_destroy_label_list(&tsp->smk_relabel);
1917
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001918 list_for_each_safe(l, n, &tsp->smk_rules) {
1919 rp = list_entry(l, struct smack_rule, list);
1920 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001921 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001922 }
Casey Schauflere114e472008-02-04 22:29:50 -08001923}
1924
1925/**
David Howellsd84f4f92008-11-14 10:39:23 +11001926 * smack_cred_prepare - prepare new set of credentials for modification
1927 * @new: the new credentials
1928 * @old: the original credentials
1929 * @gfp: the atomicity of any memory allocations
1930 *
1931 * Prepare a new set of credentials for modification.
1932 */
1933static int smack_cred_prepare(struct cred *new, const struct cred *old,
1934 gfp_t gfp)
1935{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001936 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001937 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001938 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001939
Casey Schauflerbbd36622018-11-12 09:30:56 -08001940 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301941
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001942 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1943 if (rc != 0)
1944 return rc;
1945
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001946 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1947 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001948 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001949}
1950
Randy Dunlap251a2a92009-02-18 11:42:33 -08001951/**
David Howellsee18d642009-09-02 09:14:21 +01001952 * smack_cred_transfer - Transfer the old credentials to the new credentials
1953 * @new: the new credentials
1954 * @old: the original credentials
1955 *
1956 * Fill in a set of blank credentials from another set of credentials.
1957 */
1958static void smack_cred_transfer(struct cred *new, const struct cred *old)
1959{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001960 struct task_smack *old_tsp = smack_cred(old);
1961 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001962
1963 new_tsp->smk_task = old_tsp->smk_task;
1964 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001965 mutex_init(&new_tsp->smk_rules_lock);
1966 INIT_LIST_HEAD(&new_tsp->smk_rules);
1967
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001968 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001969}
1970
1971/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001972 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001973 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001974 * @secid: where to put the result
1975 *
1976 * Sets the secid to contain a u32 version of the smack label.
1977 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001978static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001979{
1980 struct smack_known *skp;
1981
1982 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001983 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001984 *secid = skp->smk_secid;
1985 rcu_read_unlock();
1986}
1987
1988/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001989 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001990 * @new: points to the set of credentials to be modified.
1991 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001992 *
1993 * Set the security data for a kernel service.
1994 */
1995static int smack_kernel_act_as(struct cred *new, u32 secid)
1996{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001997 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11001998
Casey Schaufler152f91d2016-11-14 09:38:15 -08001999 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002000 return 0;
2001}
2002
2003/**
2004 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002005 * @new: points to the set of credentials to be modified
2006 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002007 *
2008 * Set the file creation context in a set of credentials to the same
2009 * as the objective context of the specified inode
2010 */
2011static int smack_kernel_create_files_as(struct cred *new,
2012 struct inode *inode)
2013{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002014 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002015 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002016
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002017 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002018 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002019 return 0;
2020}
2021
2022/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002023 * smk_curacc_on_task - helper to log task related access
2024 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002025 * @access: the access requested
2026 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002027 *
2028 * Return 0 if access is permitted
2029 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002030static int smk_curacc_on_task(struct task_struct *p, int access,
2031 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002032{
2033 struct smk_audit_info ad;
Paul Moorea3727a82021-09-23 09:50:11 -04002034 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002035 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002036
Casey Schaufler531f1d42011-09-19 12:41:42 -07002037 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002038 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002039 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002040 rc = smk_bu_task(p, access, rc);
2041 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002042}
2043
2044/**
Casey Schauflere114e472008-02-04 22:29:50 -08002045 * smack_task_setpgid - Smack check on setting pgid
2046 * @p: the task object
2047 * @pgid: unused
2048 *
2049 * Return 0 if write access is permitted
2050 */
2051static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2052{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002053 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002054}
2055
2056/**
2057 * smack_task_getpgid - Smack access check for getpgid
2058 * @p: the object task
2059 *
2060 * Returns 0 if current can read the object task, error code otherwise
2061 */
2062static int smack_task_getpgid(struct task_struct *p)
2063{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002064 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002065}
2066
2067/**
2068 * smack_task_getsid - Smack access check for getsid
2069 * @p: the object task
2070 *
2071 * Returns 0 if current can read the object task, error code otherwise
2072 */
2073static int smack_task_getsid(struct task_struct *p)
2074{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002075 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002076}
2077
2078/**
Paul Moore1fb057d2021-02-19 15:04:58 -05002079 * smack_task_getsecid_subj - get the subjective secid of the task
2080 * @p: the task
Casey Schauflere114e472008-02-04 22:29:50 -08002081 * @secid: where to put the result
2082 *
Paul Moore1fb057d2021-02-19 15:04:58 -05002083 * Sets the secid to contain a u32 version of the task's subjective smack label.
Casey Schauflere114e472008-02-04 22:29:50 -08002084 */
Paul Moore1fb057d2021-02-19 15:04:58 -05002085static void smack_task_getsecid_subj(struct task_struct *p, u32 *secid)
Casey Schauflere114e472008-02-04 22:29:50 -08002086{
Paul Moore1fb057d2021-02-19 15:04:58 -05002087 struct smack_known *skp = smk_of_task_struct_subj(p);
2088
2089 *secid = skp->smk_secid;
2090}
2091
2092/**
2093 * smack_task_getsecid_obj - get the objective secid of the task
2094 * @p: the task
2095 * @secid: where to put the result
2096 *
2097 * Sets the secid to contain a u32 version of the task's objective smack label.
2098 */
2099static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2100{
2101 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002102
2103 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002104}
2105
2106/**
2107 * smack_task_setnice - Smack check on setting nice
2108 * @p: the task object
2109 * @nice: unused
2110 *
2111 * Return 0 if write access is permitted
2112 */
2113static int smack_task_setnice(struct task_struct *p, int nice)
2114{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002115 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002116}
2117
2118/**
2119 * smack_task_setioprio - Smack check on setting ioprio
2120 * @p: the task object
2121 * @ioprio: unused
2122 *
2123 * Return 0 if write access is permitted
2124 */
2125static int smack_task_setioprio(struct task_struct *p, int ioprio)
2126{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002127 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002128}
2129
2130/**
2131 * smack_task_getioprio - Smack check on reading ioprio
2132 * @p: the task object
2133 *
2134 * Return 0 if read access is permitted
2135 */
2136static int smack_task_getioprio(struct task_struct *p)
2137{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002138 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002139}
2140
2141/**
2142 * smack_task_setscheduler - Smack check on setting scheduler
2143 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002144 *
2145 * Return 0 if read access is permitted
2146 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002147static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002148{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002149 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002150}
2151
2152/**
2153 * smack_task_getscheduler - Smack check on reading scheduler
2154 * @p: the task object
2155 *
2156 * Return 0 if read access is permitted
2157 */
2158static int smack_task_getscheduler(struct task_struct *p)
2159{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002160 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002161}
2162
2163/**
2164 * smack_task_movememory - Smack check on moving memory
2165 * @p: the task object
2166 *
2167 * Return 0 if write access is permitted
2168 */
2169static int smack_task_movememory(struct task_struct *p)
2170{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002171 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002172}
2173
2174/**
2175 * smack_task_kill - Smack check on signal delivery
2176 * @p: the task object
2177 * @info: unused
2178 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002179 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002180 *
2181 * Return 0 if write access is permitted
2182 *
Casey Schauflere114e472008-02-04 22:29:50 -08002183 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002184static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002185 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002186{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002187 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002188 struct smack_known *skp;
Paul Moore1fb057d2021-02-19 15:04:58 -05002189 struct smack_known *tkp = smk_of_task_struct_obj(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002190 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002191
Rafal Krypa18d872f2016-04-04 11:14:53 +02002192 if (!sig)
2193 return 0; /* null signal; existence test */
2194
Etienne Bassetecfcc532009-04-08 20:40:06 +02002195 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2196 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002197 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002198 * Sending a signal requires that the sender
2199 * can write the receiver.
2200 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002201 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002202 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2203 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002204 return rc;
2205 }
Casey Schauflere114e472008-02-04 22:29:50 -08002206 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002207 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002208 * specific behavior. This is not clean. For one thing
2209 * we can't take privilege into account.
2210 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002211 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002212 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2213 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002214 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002215}
2216
2217/**
Casey Schauflere114e472008-02-04 22:29:50 -08002218 * smack_task_to_inode - copy task smack into the inode blob
2219 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002220 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002221 *
2222 * Sets the smack pointer in the inode security blob
2223 */
2224static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2225{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002226 struct inode_smack *isp = smack_inode(inode);
Paul Moore1fb057d2021-02-19 15:04:58 -05002227 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002228
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002229 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002230 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002231}
2232
2233/*
2234 * Socket hooks.
2235 */
2236
2237/**
2238 * smack_sk_alloc_security - Allocate a socket blob
2239 * @sk: the socket
2240 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002241 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002242 *
2243 * Assign Smack pointers to current
2244 *
2245 * Returns 0 on success, -ENOMEM is there's no memory
2246 */
2247static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2248{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002249 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002250 struct socket_smack *ssp;
2251
2252 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2253 if (ssp == NULL)
2254 return -ENOMEM;
2255
jooseong lee08382c92016-11-03 11:54:39 +01002256 /*
2257 * Sockets created by kernel threads receive web label.
2258 */
2259 if (unlikely(current->flags & PF_KTHREAD)) {
2260 ssp->smk_in = &smack_known_web;
2261 ssp->smk_out = &smack_known_web;
2262 } else {
2263 ssp->smk_in = skp;
2264 ssp->smk_out = skp;
2265 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002266 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002267
2268 sk->sk_security = ssp;
2269
2270 return 0;
2271}
2272
2273/**
2274 * smack_sk_free_security - Free a socket blob
2275 * @sk: the socket
2276 *
2277 * Clears the blob pointer
2278 */
2279static void smack_sk_free_security(struct sock *sk)
2280{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302281#ifdef SMACK_IPV6_PORT_LABELING
2282 struct smk_port_label *spp;
2283
2284 if (sk->sk_family == PF_INET6) {
2285 rcu_read_lock();
2286 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2287 if (spp->smk_sock != sk)
2288 continue;
2289 spp->smk_can_reuse = 1;
2290 break;
2291 }
2292 rcu_read_unlock();
2293 }
2294#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002295 kfree(sk->sk_security);
2296}
2297
2298/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002299* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002300* @sip: the object end
2301*
2302* looks for host based access restrictions
2303*
2304* This version will only be appropriate for really small sets of single label
2305* hosts. The caller is responsible for ensuring that the RCU read lock is
2306* taken before calling this function.
2307*
2308* Returns the label of the far end or NULL if it's not special.
2309*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002310static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002311{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002312 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002313 struct in_addr *siap = &sip->sin_addr;
2314
2315 if (siap->s_addr == 0)
2316 return NULL;
2317
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002318 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2319 /*
2320 * we break after finding the first match because
2321 * the list is sorted from longest to shortest mask
2322 * so we have found the most specific match
2323 */
2324 if (snp->smk_host.s_addr ==
2325 (siap->s_addr & snp->smk_mask.s_addr))
2326 return snp->smk_label;
2327
2328 return NULL;
2329}
2330
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002331/*
2332 * smk_ipv6_localhost - Check for local ipv6 host address
2333 * @sip: the address
2334 *
2335 * Returns boolean true if this is the localhost address
2336 */
2337static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2338{
2339 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2340 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2341
2342 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2343 ntohs(be16p[7]) == 1)
2344 return true;
2345 return false;
2346}
2347
2348/**
2349* smack_ipv6host_label - check host based restrictions
2350* @sip: the object end
2351*
2352* looks for host based access restrictions
2353*
2354* This version will only be appropriate for really small sets of single label
2355* hosts. The caller is responsible for ensuring that the RCU read lock is
2356* taken before calling this function.
2357*
2358* Returns the label of the far end or NULL if it's not special.
2359*/
2360static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2361{
2362 struct smk_net6addr *snp;
2363 struct in6_addr *sap = &sip->sin6_addr;
2364 int i;
2365 int found = 0;
2366
2367 /*
2368 * It's local. Don't look for a host label.
2369 */
2370 if (smk_ipv6_localhost(sip))
2371 return NULL;
2372
2373 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002374 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002375 * If the label is NULL the entry has
2376 * been renounced. Ignore it.
2377 */
2378 if (snp->smk_label == NULL)
2379 continue;
2380 /*
Paul Moore07feee82009-03-27 17:10:54 -04002381 * we break after finding the first match because
2382 * the list is sorted from longest to shortest mask
2383 * so we have found the most specific match
2384 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002385 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002386 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2387 snp->smk_host.s6_addr16[i]) {
2388 found = 0;
2389 break;
2390 }
Etienne Basset43031542009-03-27 17:11:01 -04002391 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002392 if (found)
2393 return snp->smk_label;
2394 }
Paul Moore07feee82009-03-27 17:10:54 -04002395
2396 return NULL;
2397}
2398
2399/**
Casey Schauflera2af0312020-08-11 17:39:42 -07002400 * smack_netlbl_add - Set the secattr on a socket
Casey Schauflere114e472008-02-04 22:29:50 -08002401 * @sk: the socket
2402 *
Casey Schauflera2af0312020-08-11 17:39:42 -07002403 * Attach the outbound smack value (smk_out) to the socket.
Casey Schauflere114e472008-02-04 22:29:50 -08002404 *
2405 * Returns 0 on success or an error code
2406 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002407static int smack_netlbl_add(struct sock *sk)
Casey Schauflere114e472008-02-04 22:29:50 -08002408{
Paul Moore07feee82009-03-27 17:10:54 -04002409 struct socket_smack *ssp = sk->sk_security;
Casey Schauflera2af0312020-08-11 17:39:42 -07002410 struct smack_known *skp = ssp->smk_out;
2411 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002412
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002413 local_bh_disable();
2414 bh_lock_sock_nested(sk);
2415
Casey Schauflera2af0312020-08-11 17:39:42 -07002416 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2417 switch (rc) {
2418 case 0:
2419 ssp->smk_state = SMK_NETLBL_LABELED;
2420 break;
2421 case -EDESTADDRREQ:
2422 ssp->smk_state = SMK_NETLBL_REQSKB;
2423 rc = 0;
2424 break;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002425 }
2426
2427 bh_unlock_sock(sk);
2428 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002429
Casey Schauflere114e472008-02-04 22:29:50 -08002430 return rc;
2431}
2432
2433/**
Casey Schauflera2af0312020-08-11 17:39:42 -07002434 * smack_netlbl_delete - Remove the secattr from a socket
2435 * @sk: the socket
2436 *
2437 * Remove the outbound smack value from a socket
2438 */
2439static void smack_netlbl_delete(struct sock *sk)
2440{
2441 struct socket_smack *ssp = sk->sk_security;
2442
2443 /*
2444 * Take the label off the socket if one is set.
2445 */
2446 if (ssp->smk_state != SMK_NETLBL_LABELED)
2447 return;
2448
2449 local_bh_disable();
2450 bh_lock_sock_nested(sk);
2451 netlbl_sock_delattr(sk);
2452 bh_unlock_sock(sk);
2453 local_bh_enable();
2454 ssp->smk_state = SMK_NETLBL_UNLABELED;
2455}
2456
2457/**
2458 * smk_ipv4_check - Perform IPv4 host access checks
Paul Moore07feee82009-03-27 17:10:54 -04002459 * @sk: the socket
2460 * @sap: the destination address
2461 *
2462 * Set the correct secattr for the given socket based on the destination
2463 * address and perform any outbound access checks needed.
2464 *
2465 * Returns 0 on success or an error code.
2466 *
2467 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002468static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
Paul Moore07feee82009-03-27 17:10:54 -04002469{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002470 struct smack_known *skp;
Casey Schauflera2af0312020-08-11 17:39:42 -07002471 int rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002472 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002473 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002474 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002475
2476 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002477 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002478 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002479#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002480 struct lsm_network_audit net;
2481
Eric Paris48c62af2012-04-02 13:15:44 -04002482 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2483 ad.a.u.net->family = sap->sin_family;
2484 ad.a.u.net->dport = sap->sin_port;
2485 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002486#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002487 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002488 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2489 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Casey Schauflera2af0312020-08-11 17:39:42 -07002490 /*
2491 * Clear the socket netlabel if it's set.
2492 */
2493 if (!rc)
2494 smack_netlbl_delete(sk);
Paul Moore07feee82009-03-27 17:10:54 -04002495 }
2496 rcu_read_unlock();
Paul Moore07feee82009-03-27 17:10:54 -04002497
Casey Schauflera2af0312020-08-11 17:39:42 -07002498 return rc;
Paul Moore07feee82009-03-27 17:10:54 -04002499}
2500
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002501/**
2502 * smk_ipv6_check - check Smack access
2503 * @subject: subject Smack label
2504 * @object: object Smack label
2505 * @address: address
2506 * @act: the action being taken
2507 *
2508 * Check an IPv6 access
2509 */
2510static int smk_ipv6_check(struct smack_known *subject,
2511 struct smack_known *object,
2512 struct sockaddr_in6 *address, int act)
2513{
2514#ifdef CONFIG_AUDIT
2515 struct lsm_network_audit net;
2516#endif
2517 struct smk_audit_info ad;
2518 int rc;
2519
2520#ifdef CONFIG_AUDIT
2521 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2522 ad.a.u.net->family = PF_INET6;
Casey Schaufler05ba7d02022-02-28 15:45:32 -08002523 ad.a.u.net->dport = address->sin6_port;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002524 if (act == SMK_RECEIVING)
2525 ad.a.u.net->v6info.saddr = address->sin6_addr;
2526 else
2527 ad.a.u.net->v6info.daddr = address->sin6_addr;
2528#endif
2529 rc = smk_access(subject, object, MAY_WRITE, &ad);
2530 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2531 return rc;
2532}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002533
2534#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002535/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002536 * smk_ipv6_port_label - Smack port access table management
2537 * @sock: socket
2538 * @address: address
2539 *
2540 * Create or update the port list entry
2541 */
2542static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2543{
2544 struct sock *sk = sock->sk;
2545 struct sockaddr_in6 *addr6;
2546 struct socket_smack *ssp = sock->sk->sk_security;
2547 struct smk_port_label *spp;
2548 unsigned short port = 0;
2549
2550 if (address == NULL) {
2551 /*
2552 * This operation is changing the Smack information
2553 * on the bound socket. Take the changes to the port
2554 * as well.
2555 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302556 rcu_read_lock();
2557 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002558 if (sk != spp->smk_sock)
2559 continue;
2560 spp->smk_in = ssp->smk_in;
2561 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302562 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002563 return;
2564 }
2565 /*
2566 * A NULL address is only used for updating existing
2567 * bound entries. If there isn't one, it's OK.
2568 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302569 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002570 return;
2571 }
2572
2573 addr6 = (struct sockaddr_in6 *)address;
2574 port = ntohs(addr6->sin6_port);
2575 /*
2576 * This is a special case that is safely ignored.
2577 */
2578 if (port == 0)
2579 return;
2580
2581 /*
2582 * Look for an existing port list entry.
2583 * This is an indication that a port is getting reused.
2584 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302585 rcu_read_lock();
2586 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302587 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002588 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302589 if (spp->smk_can_reuse != 1) {
2590 rcu_read_unlock();
2591 return;
2592 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002593 spp->smk_port = port;
2594 spp->smk_sock = sk;
2595 spp->smk_in = ssp->smk_in;
2596 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302597 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302598 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002599 return;
2600 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302601 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002602 /*
2603 * A new port entry is required.
2604 */
2605 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2606 if (spp == NULL)
2607 return;
2608
2609 spp->smk_port = port;
2610 spp->smk_sock = sk;
2611 spp->smk_in = ssp->smk_in;
2612 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302613 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302614 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002615
Vishal Goel3c7ce342016-11-23 10:31:08 +05302616 mutex_lock(&smack_ipv6_lock);
2617 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2618 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002619 return;
2620}
Arnd Bergmann00720f02020-04-08 21:04:31 +02002621#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002622
2623/**
2624 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002625 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002626 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002627 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002628 *
2629 * Create or update the port list entry
2630 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002631static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002632 int act)
2633{
Casey Schauflerc6739442013-05-22 18:42:56 -07002634 struct smk_port_label *spp;
2635 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002636 struct smack_known *skp = NULL;
2637 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002638 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002639
2640 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002641 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002642 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002643 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002644 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002645 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002646 }
2647
2648 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002649 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002650 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002651 if (skp != NULL && object != NULL)
2652 return smk_ipv6_check(skp, object, address, act);
2653 if (skp == NULL)
2654 skp = smack_net_ambient;
2655 if (object == NULL)
2656 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002657
2658 /*
2659 * It's remote, so port lookup does no good.
2660 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002661 if (!smk_ipv6_localhost(address))
2662 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002663
2664 /*
2665 * It's local so the send check has to have passed.
2666 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002667 if (act == SMK_RECEIVING)
2668 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002669
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002670 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302671 rcu_read_lock();
2672 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302673 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002674 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002675 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002676 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002677 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002678 break;
2679 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302680 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002681
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002682 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002683}
2684
2685/**
Casey Schauflere114e472008-02-04 22:29:50 -08002686 * smack_inode_setsecurity - set smack xattrs
2687 * @inode: the object
2688 * @name: attribute name
2689 * @value: attribute value
2690 * @size: size of the attribute
2691 * @flags: unused
2692 *
2693 * Sets the named attribute in the appropriate blob
2694 *
2695 * Returns 0 on success, or an error code
2696 */
2697static int smack_inode_setsecurity(struct inode *inode, const char *name,
2698 const void *value, size_t size, int flags)
2699{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002700 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002701 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002702 struct socket_smack *ssp;
2703 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002704 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002705
Casey Schauflerf7112e62012-05-06 15:22:02 -07002706 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302707 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002708
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002709 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002710 if (IS_ERR(skp))
2711 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002712
2713 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002714 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002715 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002716 return 0;
2717 }
2718 /*
2719 * The rest of the Smack xattrs are only on sockets.
2720 */
2721 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2722 return -EOPNOTSUPP;
2723
2724 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002725 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002726 return -EOPNOTSUPP;
2727
2728 ssp = sock->sk->sk_security;
2729
2730 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002731 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002732 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002733 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002734 if (sock->sk->sk_family == PF_INET) {
Casey Schauflera2af0312020-08-11 17:39:42 -07002735 rc = smack_netlbl_add(sock->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002736 if (rc != 0)
2737 printk(KERN_WARNING
2738 "Smack: \"%s\" netlbl error %d.\n",
2739 __func__, -rc);
2740 }
Casey Schauflere114e472008-02-04 22:29:50 -08002741 } else
2742 return -EOPNOTSUPP;
2743
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002744#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002745 if (sock->sk->sk_family == PF_INET6)
2746 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002747#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002748
Casey Schauflere114e472008-02-04 22:29:50 -08002749 return 0;
2750}
2751
2752/**
2753 * smack_socket_post_create - finish socket setup
2754 * @sock: the socket
2755 * @family: protocol family
2756 * @type: unused
2757 * @protocol: unused
2758 * @kern: unused
2759 *
2760 * Sets the netlabel information on the socket
2761 *
2762 * Returns 0 on success, and error code otherwise
2763 */
2764static int smack_socket_post_create(struct socket *sock, int family,
2765 int type, int protocol, int kern)
2766{
Marcin Lis74123012015-01-22 15:40:33 +01002767 struct socket_smack *ssp;
2768
2769 if (sock->sk == NULL)
2770 return 0;
2771
2772 /*
2773 * Sockets created by kernel threads receive web label.
2774 */
2775 if (unlikely(current->flags & PF_KTHREAD)) {
2776 ssp = sock->sk->sk_security;
2777 ssp->smk_in = &smack_known_web;
2778 ssp->smk_out = &smack_known_web;
2779 }
2780
2781 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002782 return 0;
2783 /*
2784 * Set the outbound netlbl.
2785 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002786 return smack_netlbl_add(sock->sk);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002787}
2788
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002789/**
2790 * smack_socket_socketpair - create socket pair
2791 * @socka: one socket
2792 * @sockb: another socket
2793 *
2794 * Cross reference the peer labels for SO_PEERSEC
2795 *
luanshia1a07f22019-07-05 10:35:20 +08002796 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002797 */
2798static int smack_socket_socketpair(struct socket *socka,
2799 struct socket *sockb)
2800{
2801 struct socket_smack *asp = socka->sk->sk_security;
2802 struct socket_smack *bsp = sockb->sk->sk_security;
2803
2804 asp->smk_packet = bsp->smk_out;
2805 bsp->smk_packet = asp->smk_out;
2806
2807 return 0;
2808}
2809
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002810#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002811/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002812 * smack_socket_bind - record port binding information.
2813 * @sock: the socket
2814 * @address: the port address
2815 * @addrlen: size of the address
2816 *
2817 * Records the label bound to a port.
2818 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002819 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002820 */
2821static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2822 int addrlen)
2823{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002824 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2825 if (addrlen < SIN6_LEN_RFC2133 ||
2826 address->sa_family != AF_INET6)
2827 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002828 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002829 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002830 return 0;
2831}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002832#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002833
2834/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002835 * smack_socket_connect - connect access check
2836 * @sock: the socket
2837 * @sap: the other end
2838 * @addrlen: size of sap
2839 *
2840 * Verifies that a connection may be possible
2841 *
2842 * Returns 0 on success, and error code otherwise
2843 */
2844static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2845 int addrlen)
2846{
Casey Schauflerc6739442013-05-22 18:42:56 -07002847 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002848
Casey Schauflerc6739442013-05-22 18:42:56 -07002849 if (sock->sk == NULL)
2850 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002851 if (sock->sk->sk_family != PF_INET &&
2852 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2853 return 0;
2854 if (addrlen < offsetofend(struct sockaddr, sa_family))
2855 return 0;
2856 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2857 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002858 struct smack_known *rsp = NULL;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002859
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002860 if (addrlen < SIN6_LEN_RFC2133)
2861 return 0;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002862 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2863 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002864 if (rsp != NULL) {
2865 struct socket_smack *ssp = sock->sk->sk_security;
2866
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002867 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002868 SMK_CONNECTING);
2869 }
Arnd Bergmann00720f02020-04-08 21:04:31 +02002870 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2871 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2872
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002873 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002874 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002875 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2876 return 0;
Casey Schauflera2af0312020-08-11 17:39:42 -07002877 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002878 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002879}
2880
2881/**
2882 * smack_flags_to_may - convert S_ to MAY_ values
2883 * @flags: the S_ value
2884 *
2885 * Returns the equivalent MAY_ value
2886 */
2887static int smack_flags_to_may(int flags)
2888{
2889 int may = 0;
2890
2891 if (flags & S_IRUGO)
2892 may |= MAY_READ;
2893 if (flags & S_IWUGO)
2894 may |= MAY_WRITE;
2895 if (flags & S_IXUGO)
2896 may |= MAY_EXEC;
2897
2898 return may;
2899}
2900
2901/**
2902 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2903 * @msg: the object
2904 *
2905 * Returns 0
2906 */
2907static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2908{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002909 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002910
Casey Schauflerecd5f822018-11-20 11:55:02 -08002911 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002912 return 0;
2913}
2914
2915/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002916 * smack_of_ipc - the smack pointer for the ipc
2917 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002918 *
2919 * Returns a pointer to the smack value
2920 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002921static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002922{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002923 struct smack_known **blob = smack_ipc(isp);
2924
2925 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002926}
2927
2928/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002929 * smack_ipc_alloc_security - Set the security blob for ipc
2930 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002931 *
2932 * Returns 0
2933 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002934static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002935{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002936 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002937
Casey Schaufler019bcca2018-09-21 17:19:54 -07002938 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002939 return 0;
2940}
2941
2942/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002943 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002944 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002945 * @access : access requested
2946 *
2947 * Returns 0 if current has the requested access, error code otherwise
2948 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002949static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002950{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002951 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002952 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002953 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002954
2955#ifdef CONFIG_AUDIT
2956 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002957 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002958#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002959 rc = smk_curacc(ssp, access, &ad);
2960 rc = smk_bu_current("shm", ssp, access, rc);
2961 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002962}
2963
2964/**
Casey Schauflere114e472008-02-04 22:29:50 -08002965 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002966 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002967 * @shmflg: access requested
2968 *
2969 * Returns 0 if current has the requested access, error code otherwise
2970 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002971static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002972{
Casey Schauflere114e472008-02-04 22:29:50 -08002973 int may;
2974
2975 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002976 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002977}
2978
2979/**
2980 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002981 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002982 * @cmd: what it wants to do
2983 *
2984 * Returns 0 if current has the requested access, error code otherwise
2985 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002986static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002987{
Casey Schauflere114e472008-02-04 22:29:50 -08002988 int may;
2989
2990 switch (cmd) {
2991 case IPC_STAT:
2992 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002993 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002994 may = MAY_READ;
2995 break;
2996 case IPC_SET:
2997 case SHM_LOCK:
2998 case SHM_UNLOCK:
2999 case IPC_RMID:
3000 may = MAY_READWRITE;
3001 break;
3002 case IPC_INFO:
3003 case SHM_INFO:
3004 /*
3005 * System level information.
3006 */
3007 return 0;
3008 default:
3009 return -EINVAL;
3010 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003011 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003012}
3013
3014/**
3015 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003016 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003017 * @shmaddr: unused
3018 * @shmflg: access requested
3019 *
3020 * Returns 0 if current has the requested access, error code otherwise
3021 */
luanshia1a07f22019-07-05 10:35:20 +08003022static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003023 int shmflg)
3024{
Casey Schauflere114e472008-02-04 22:29:50 -08003025 int may;
3026
3027 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003028 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003029}
3030
3031/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003033 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003034 * @access : access requested
3035 *
3036 * Returns 0 if current has the requested access, error code otherwise
3037 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003038static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003039{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003040 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003041 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003042 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003043
3044#ifdef CONFIG_AUDIT
3045 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003046 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003047#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003048 rc = smk_curacc(ssp, access, &ad);
3049 rc = smk_bu_current("sem", ssp, access, rc);
3050 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003051}
3052
3053/**
Casey Schauflere114e472008-02-04 22:29:50 -08003054 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003055 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003056 * @semflg: access requested
3057 *
3058 * Returns 0 if current has the requested access, error code otherwise
3059 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003060static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003061{
Casey Schauflere114e472008-02-04 22:29:50 -08003062 int may;
3063
3064 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003065 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003066}
3067
3068/**
3069 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003070 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003071 * @cmd: what it wants to do
3072 *
3073 * Returns 0 if current has the requested access, error code otherwise
3074 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003075static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003076{
Casey Schauflere114e472008-02-04 22:29:50 -08003077 int may;
3078
3079 switch (cmd) {
3080 case GETPID:
3081 case GETNCNT:
3082 case GETZCNT:
3083 case GETVAL:
3084 case GETALL:
3085 case IPC_STAT:
3086 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003087 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003088 may = MAY_READ;
3089 break;
3090 case SETVAL:
3091 case SETALL:
3092 case IPC_RMID:
3093 case IPC_SET:
3094 may = MAY_READWRITE;
3095 break;
3096 case IPC_INFO:
3097 case SEM_INFO:
3098 /*
3099 * System level information
3100 */
3101 return 0;
3102 default:
3103 return -EINVAL;
3104 }
3105
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003106 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003107}
3108
3109/**
3110 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003111 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003112 * @sops: unused
3113 * @nsops: unused
3114 * @alter: unused
3115 *
3116 * Treated as read and write in all cases.
3117 *
3118 * Returns 0 if access is allowed, error code otherwise
3119 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003120static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003121 unsigned nsops, int alter)
3122{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003123 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003124}
3125
3126/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003127 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003128 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003129 * @access : access requested
3130 *
3131 * return 0 if current has access, error otherwise
3132 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003133static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003134{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003135 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003136 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003137 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003138
3139#ifdef CONFIG_AUDIT
3140 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003141 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003142#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003143 rc = smk_curacc(msp, access, &ad);
3144 rc = smk_bu_current("msq", msp, access, rc);
3145 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003146}
3147
3148/**
Casey Schauflere114e472008-02-04 22:29:50 -08003149 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003150 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003151 * @msqflg: access requested
3152 *
3153 * Returns 0 if current has the requested access, error code otherwise
3154 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003155static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003156{
Casey Schauflere114e472008-02-04 22:29:50 -08003157 int may;
3158
3159 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003160 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003161}
3162
3163/**
3164 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003165 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003166 * @cmd: what it wants to do
3167 *
3168 * Returns 0 if current has the requested access, error code otherwise
3169 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003170static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003171{
Casey Schauflere114e472008-02-04 22:29:50 -08003172 int may;
3173
3174 switch (cmd) {
3175 case IPC_STAT:
3176 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003177 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003178 may = MAY_READ;
3179 break;
3180 case IPC_SET:
3181 case IPC_RMID:
3182 may = MAY_READWRITE;
3183 break;
3184 case IPC_INFO:
3185 case MSG_INFO:
3186 /*
3187 * System level information
3188 */
3189 return 0;
3190 default:
3191 return -EINVAL;
3192 }
3193
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003194 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003195}
3196
3197/**
3198 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003199 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003200 * @msg: unused
3201 * @msqflg: access requested
3202 *
3203 * Returns 0 if current has the requested access, error code otherwise
3204 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003205static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003206 int msqflg)
3207{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003208 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003209
Etienne Bassetecfcc532009-04-08 20:40:06 +02003210 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003211 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003212}
3213
3214/**
3215 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003216 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003217 * @msg: unused
3218 * @target: unused
3219 * @type: unused
3220 * @mode: unused
3221 *
3222 * Returns 0 if current has read and write access, error code otherwise
3223 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003224static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003225 struct task_struct *target, long type, int mode)
3226{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003227 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003228}
3229
3230/**
3231 * smack_ipc_permission - Smack access for ipc_permission()
3232 * @ipp: the object permissions
3233 * @flag: access requested
3234 *
3235 * Returns 0 if current has read and write access, error code otherwise
3236 */
3237static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3238{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003239 struct smack_known **blob = smack_ipc(ipp);
3240 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003241 int may = smack_flags_to_may(flag);
3242 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003243 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003244
Etienne Bassetecfcc532009-04-08 20:40:06 +02003245#ifdef CONFIG_AUDIT
3246 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3247 ad.a.u.ipc_id = ipp->id;
3248#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003249 rc = smk_curacc(iskp, may, &ad);
3250 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003251 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003252}
3253
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003254/**
3255 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003256 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003257 * @secid: where result will be saved
3258 */
3259static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3260{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003261 struct smack_known **blob = smack_ipc(ipp);
3262 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003263
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003264 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003265}
3266
Casey Schauflere114e472008-02-04 22:29:50 -08003267/**
3268 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003269 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003270 * @inode: the object
3271 *
3272 * Set the inode's security blob if it hasn't been done already.
3273 */
3274static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3275{
3276 struct super_block *sbp;
3277 struct superblock_smack *sbsp;
3278 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003279 struct smack_known *skp;
3280 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003281 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003282 char trattr[TRANS_TRUE_SIZE];
3283 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003284 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003285 struct dentry *dp;
3286
3287 if (inode == NULL)
3288 return;
3289
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003290 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003291
Casey Schauflere114e472008-02-04 22:29:50 -08003292 /*
3293 * If the inode is already instantiated
3294 * take the quick way out
3295 */
3296 if (isp->smk_flags & SMK_INODE_INSTANT)
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003297 return;
Casey Schauflere114e472008-02-04 22:29:50 -08003298
3299 sbp = inode->i_sb;
Casey Schaufler1aea7802021-04-22 17:41:15 +02003300 sbsp = smack_superblock(sbp);
Casey Schauflere114e472008-02-04 22:29:50 -08003301 /*
3302 * We're going to use the superblock default label
3303 * if there's no label on the file.
3304 */
3305 final = sbsp->smk_default;
3306
3307 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003308 * If this is the root inode the superblock
3309 * may be in the process of initialization.
3310 * If that is the case use the root value out
3311 * of the superblock.
3312 */
3313 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003314 switch (sbp->s_magic) {
3315 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003316 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003317 /*
3318 * The cgroup filesystem is never mounted,
3319 * so there's no opportunity to set the mount
3320 * options.
3321 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003322 sbsp->smk_root = &smack_known_star;
3323 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003324 isp->smk_inode = sbsp->smk_root;
3325 break;
3326 case TMPFS_MAGIC:
3327 /*
3328 * What about shmem/tmpfs anonymous files with dentry
3329 * obtained from d_alloc_pseudo()?
3330 */
3331 isp->smk_inode = smk_of_current();
3332 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003333 case PIPEFS_MAGIC:
3334 isp->smk_inode = smk_of_current();
3335 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003336 case SOCKFS_MAGIC:
3337 /*
3338 * Socket access is controlled by the socket
3339 * structures associated with the task involved.
3340 */
3341 isp->smk_inode = &smack_known_star;
3342 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003343 default:
3344 isp->smk_inode = sbsp->smk_root;
3345 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003346 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003347 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003348 return;
Casey Schauflere97dcb02008-06-02 10:04:32 -07003349 }
3350
3351 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003352 * This is pretty hackish.
3353 * Casey says that we shouldn't have to do
3354 * file system specific code, but it does help
3355 * with keeping it simple.
3356 */
3357 switch (sbp->s_magic) {
3358 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003359 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003360 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003361 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003362 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003363 * that the smack file system doesn't do
3364 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003365 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003366 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003367 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003368 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003369 break;
3370 case DEVPTS_SUPER_MAGIC:
3371 /*
3372 * devpts seems content with the label of the task.
3373 * Programs that change smack have to treat the
3374 * pty with respect.
3375 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003376 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003377 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003378 case PROC_SUPER_MAGIC:
3379 /*
3380 * Casey says procfs appears not to care.
3381 * The superblock default suffices.
3382 */
3383 break;
3384 case TMPFS_MAGIC:
3385 /*
3386 * Device labels should come from the filesystem,
3387 * but watch out, because they're volitile,
3388 * getting recreated on every reboot.
3389 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003390 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003391 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003392 * If a smack value has been set we want to use it,
3393 * but since tmpfs isn't giving us the opportunity
3394 * to set mount options simulate setting the
3395 * superblock default.
3396 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003397 fallthrough;
Casey Schauflere114e472008-02-04 22:29:50 -08003398 default:
3399 /*
3400 * This isn't an understood special case.
3401 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003402 */
3403
3404 /*
3405 * UNIX domain sockets use lower level socket data.
3406 */
3407 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003408 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003409 break;
3410 }
3411 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003412 * No xattr support means, alas, no SMACK label.
3413 * Use the aforeapplied default.
3414 * It would be curious if the label of the task
3415 * does not match that assigned.
3416 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003417 if (!(inode->i_opflags & IOP_XATTR))
3418 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003419 /*
3420 * Get the dentry for xattr.
3421 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003422 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003423 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003424 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003425 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003426
3427 /*
3428 * Transmuting directory
3429 */
3430 if (S_ISDIR(inode->i_mode)) {
3431 /*
3432 * If this is a new directory and the label was
3433 * transmuted when the inode was initialized
3434 * set the transmute attribute on the directory
3435 * and mark the inode.
3436 *
3437 * If there is a transmute attribute on the
3438 * directory mark the inode.
3439 */
3440 if (isp->smk_flags & SMK_INODE_CHANGED) {
3441 isp->smk_flags &= ~SMK_INODE_CHANGED;
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01003442 rc = __vfs_setxattr(&init_user_ns, dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003443 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003444 TRANS_TRUE, TRANS_TRUE_SIZE,
3445 0);
3446 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003447 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003448 XATTR_NAME_SMACKTRANSMUTE, trattr,
3449 TRANS_TRUE_SIZE);
3450 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3451 TRANS_TRUE_SIZE) != 0)
3452 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003453 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003454 if (rc >= 0)
3455 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003456 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003457 /*
3458 * Don't let the exec or mmap label be "*" or "@".
3459 */
3460 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3461 if (IS_ERR(skp) || skp == &smack_known_star ||
3462 skp == &smack_known_web)
3463 skp = NULL;
3464 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003465
Casey Schaufler19760ad2013-12-16 16:27:26 -08003466 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003467 if (IS_ERR(skp) || skp == &smack_known_star ||
3468 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003469 skp = NULL;
3470 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003471
Casey Schauflere114e472008-02-04 22:29:50 -08003472 dput(dp);
3473 break;
3474 }
3475
3476 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003477 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003478 else
3479 isp->smk_inode = final;
3480
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003481 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003482
Casey Schauflere114e472008-02-04 22:29:50 -08003483 return;
3484}
3485
3486/**
3487 * smack_getprocattr - Smack process attribute access
3488 * @p: the object task
3489 * @name: the name of the attribute in /proc/.../attr
3490 * @value: where to put the result
3491 *
3492 * Places a copy of the task Smack into value
3493 *
3494 * Returns the length of the smack label or an error code
3495 */
3496static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3497{
Paul Moorea3727a82021-09-23 09:50:11 -04003498 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003499 char *cp;
3500 int slen;
3501
3502 if (strcmp(name, "current") != 0)
3503 return -EINVAL;
3504
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003505 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003506 if (cp == NULL)
3507 return -ENOMEM;
3508
3509 slen = strlen(cp);
3510 *value = cp;
3511 return slen;
3512}
3513
3514/**
3515 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003516 * @name: the name of the attribute in /proc/.../attr
3517 * @value: the value to set
3518 * @size: the size of the value
3519 *
3520 * Sets the Smack value of the task. Only setting self
3521 * is permitted and only with privilege
3522 *
3523 * Returns the length of the smack label or an error code
3524 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003525static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003526{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003527 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003528 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003529 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003530 struct smack_known_list_elem *sklep;
3531 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003532
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003533 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003534 return -EPERM;
3535
Casey Schauflerf7112e62012-05-06 15:22:02 -07003536 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003537 return -EINVAL;
3538
3539 if (strcmp(name, "current") != 0)
3540 return -EINVAL;
3541
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003542 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003543 if (IS_ERR(skp))
3544 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003545
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003546 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303547 * No process is ever allowed the web ("@") label
3548 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003549 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303550 if (skp == &smack_known_web || skp == &smack_known_star)
3551 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003552
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003553 if (!smack_privileged(CAP_MAC_ADMIN)) {
3554 rc = -EPERM;
3555 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3556 if (sklep->smk_label == skp) {
3557 rc = 0;
3558 break;
3559 }
3560 if (rc)
3561 return rc;
3562 }
3563
David Howellsd84f4f92008-11-14 10:39:23 +11003564 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003565 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003566 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003567
Casey Schauflerb17103a2018-11-09 16:12:56 -08003568 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003569 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003570 /*
3571 * process can change its label only once
3572 */
3573 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003574
David Howellsd84f4f92008-11-14 10:39:23 +11003575 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003576 return size;
3577}
3578
3579/**
3580 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003581 * @sock: one sock
3582 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003583 * @newsk: unused
3584 *
3585 * Return 0 if a subject with the smack of sock could access
3586 * an object with the smack of other, otherwise an error code
3587 */
David S. Miller3610cda2011-01-05 15:38:53 -08003588static int smack_unix_stream_connect(struct sock *sock,
3589 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003590{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003591 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003592 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003593 struct socket_smack *ssp = sock->sk_security;
3594 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003595 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003596 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003597 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003598#ifdef CONFIG_AUDIT
3599 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003600#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003601
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003602 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3603 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003604 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003605#ifdef CONFIG_AUDIT
3606 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3607 smk_ad_setfield_u_net_sk(&ad, other);
3608#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003609 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3610 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003611 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003612 okp = osp->smk_out;
3613 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003614 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003615 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003616 MAY_WRITE, rc);
3617 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003618 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003619
Casey Schaufler975d5e52011-09-26 14:43:39 -07003620 /*
3621 * Cross reference the peer labels for SO_PEERSEC.
3622 */
3623 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003624 nsp->smk_packet = ssp->smk_out;
3625 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003626 }
3627
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003628 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003629}
3630
3631/**
3632 * smack_unix_may_send - Smack access on UDS
3633 * @sock: one socket
3634 * @other: the other socket
3635 *
3636 * Return 0 if a subject with the smack of sock could access
3637 * an object with the smack of other, otherwise an error code
3638 */
3639static int smack_unix_may_send(struct socket *sock, struct socket *other)
3640{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003641 struct socket_smack *ssp = sock->sk->sk_security;
3642 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003643 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003644 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003645
Kees Cook923e9a12012-04-10 13:26:44 -07003646#ifdef CONFIG_AUDIT
3647 struct lsm_network_audit net;
3648
Eric Paris48c62af2012-04-02 13:15:44 -04003649 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003650 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003651#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003652
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003653 if (smack_privileged(CAP_MAC_OVERRIDE))
3654 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003655
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003656 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3657 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003658 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003659}
3660
3661/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003662 * smack_socket_sendmsg - Smack check based on destination host
3663 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003664 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003665 * @size: the size of the message
3666 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003667 * Return 0 if the current subject can write to the destination host.
3668 * For IPv4 this is only a question if the destination is a single label host.
3669 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003670 */
3671static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3672 int size)
3673{
3674 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003675#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003676 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003677#endif
3678#ifdef SMACK_IPV6_SECMARK_LABELING
3679 struct socket_smack *ssp = sock->sk->sk_security;
3680 struct smack_known *rsp;
3681#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003682 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003683
3684 /*
3685 * Perfectly reasonable for this to be NULL
3686 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003687 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003688 return 0;
3689
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003690 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003691 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003692 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3693 sip->sin_family != AF_INET)
3694 return -EINVAL;
Casey Schauflera2af0312020-08-11 17:39:42 -07003695 rc = smk_ipv4_check(sock->sk, sip);
Casey Schauflerc6739442013-05-22 18:42:56 -07003696 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003697#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003698 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003699 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3700 sap->sin6_family != AF_INET6)
3701 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003702#ifdef SMACK_IPV6_SECMARK_LABELING
3703 rsp = smack_ipv6host_label(sap);
3704 if (rsp != NULL)
3705 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3706 SMK_CONNECTING);
3707#endif
3708#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003709 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003710#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003711#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003712 break;
3713 }
3714 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003715}
3716
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003717/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003718 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003719 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003720 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003721 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003722 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003723 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003724static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3725 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003726{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003727 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003728 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003729 int acat;
3730 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003731
Casey Schaufler322dd632020-08-11 17:39:43 -07003732 /*
3733 * Netlabel found it in the cache.
3734 */
3735 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3736 return (struct smack_known *)sap->cache->data;
3737
3738 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3739 /*
3740 * Looks like a fallback, which gives us a secid.
3741 */
3742 return smack_from_secid(sap->attr.secid);
3743
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003744 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003745 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003746 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003747 * If there are flags but no level netlabel isn't
3748 * behaving the way we expect it to.
3749 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003750 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003751 * Without guidance regarding the smack value
3752 * for the packet fall back on the network
3753 * ambient value.
3754 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003755 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303756 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003757 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003758 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003759 /*
3760 * Compare the catsets. Use the netlbl APIs.
3761 */
3762 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3763 if ((skp->smk_netlabel.flags &
3764 NETLBL_SECATTR_MLS_CAT) == 0)
3765 found = 1;
3766 break;
3767 }
3768 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003769 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3770 acat + 1);
3771 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003772 skp->smk_netlabel.attr.mls.cat,
3773 kcat + 1);
3774 if (acat < 0 || kcat < 0)
3775 break;
3776 }
3777 if (acat == kcat) {
3778 found = 1;
3779 break;
3780 }
Casey Schauflere114e472008-02-04 22:29:50 -08003781 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003782 rcu_read_unlock();
3783
3784 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003785 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003786
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003787 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003788 return &smack_known_web;
3789 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003790 }
Casey Schauflere114e472008-02-04 22:29:50 -08003791 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003792 * Without guidance regarding the smack value
3793 * for the packet fall back on the network
3794 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003795 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003796 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003797}
3798
Casey Schaufler69f287a2014-12-12 17:08:40 -08003799#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003800static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003801{
Casey Schauflerc6739442013-05-22 18:42:56 -07003802 u8 nexthdr;
3803 int offset;
3804 int proto = -EINVAL;
3805 struct ipv6hdr _ipv6h;
3806 struct ipv6hdr *ip6;
3807 __be16 frag_off;
3808 struct tcphdr _tcph, *th;
3809 struct udphdr _udph, *uh;
3810 struct dccp_hdr _dccph, *dh;
3811
3812 sip->sin6_port = 0;
3813
3814 offset = skb_network_offset(skb);
3815 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3816 if (ip6 == NULL)
3817 return -EINVAL;
3818 sip->sin6_addr = ip6->saddr;
3819
3820 nexthdr = ip6->nexthdr;
3821 offset += sizeof(_ipv6h);
3822 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3823 if (offset < 0)
3824 return -EINVAL;
3825
3826 proto = nexthdr;
3827 switch (proto) {
3828 case IPPROTO_TCP:
3829 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3830 if (th != NULL)
3831 sip->sin6_port = th->source;
3832 break;
3833 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003834 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003835 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3836 if (uh != NULL)
3837 sip->sin6_port = uh->source;
3838 break;
3839 case IPPROTO_DCCP:
3840 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3841 if (dh != NULL)
3842 sip->sin6_port = dh->dccph_sport;
3843 break;
3844 }
3845 return proto;
3846}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003847#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003848
Casey Schauflere114e472008-02-04 22:29:50 -08003849/**
Casey Schaufler36be8122020-08-11 17:39:41 -07003850 * smack_from_skb - Smack data from the secmark in an skb
3851 * @skb: packet
3852 *
3853 * Returns smack_known of the secmark or NULL if that won't work.
3854 */
Casey Schauflerbf0afe62020-09-22 14:59:31 -07003855#ifdef CONFIG_NETWORK_SECMARK
Casey Schaufler36be8122020-08-11 17:39:41 -07003856static struct smack_known *smack_from_skb(struct sk_buff *skb)
3857{
3858 if (skb == NULL || skb->secmark == 0)
3859 return NULL;
3860
3861 return smack_from_secid(skb->secmark);
3862}
Casey Schauflerbf0afe62020-09-22 14:59:31 -07003863#else
3864static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3865{
3866 return NULL;
3867}
3868#endif
Casey Schaufler36be8122020-08-11 17:39:41 -07003869
3870/**
Casey Schauflera2af0312020-08-11 17:39:42 -07003871 * smack_from_netlbl - Smack data from the IP options in an skb
3872 * @sk: socket data came in on
3873 * @family: address family
3874 * @skb: packet
3875 *
Casey Schaufler322dd632020-08-11 17:39:43 -07003876 * Find the Smack label in the IP options. If it hasn't been
3877 * added to the netlabel cache, add it here.
3878 *
Casey Schauflera2af0312020-08-11 17:39:42 -07003879 * Returns smack_known of the IP options or NULL if that won't work.
3880 */
Florian Westphal41dd9592020-11-30 16:36:29 +01003881static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
Casey Schauflera2af0312020-08-11 17:39:42 -07003882 struct sk_buff *skb)
3883{
3884 struct netlbl_lsm_secattr secattr;
3885 struct socket_smack *ssp = NULL;
3886 struct smack_known *skp = NULL;
3887
3888 netlbl_secattr_init(&secattr);
3889
3890 if (sk)
3891 ssp = sk->sk_security;
Casey Schaufler322dd632020-08-11 17:39:43 -07003892
3893 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
Casey Schauflera2af0312020-08-11 17:39:42 -07003894 skp = smack_from_secattr(&secattr, ssp);
Casey Schaufler322dd632020-08-11 17:39:43 -07003895 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
Alex Shi9b0072e2020-11-08 14:45:42 +08003896 netlbl_cache_add(skb, family, &skp->smk_netlabel);
Casey Schaufler322dd632020-08-11 17:39:43 -07003897 }
Casey Schauflera2af0312020-08-11 17:39:42 -07003898
3899 netlbl_secattr_destroy(&secattr);
3900
3901 return skp;
3902}
3903
3904/**
Casey Schauflere114e472008-02-04 22:29:50 -08003905 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3906 * @sk: socket
3907 * @skb: packet
3908 *
3909 * Returns 0 if the packet should be delivered, an error code otherwise
3910 */
3911static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3912{
Casey Schauflere114e472008-02-04 22:29:50 -08003913 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003914 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003915 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003916 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003917 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003918#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003919 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003920#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003921#if IS_ENABLED(CONFIG_IPV6)
3922 struct sockaddr_in6 sadd;
3923 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003924
3925 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3926 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003927#endif /* CONFIG_IPV6 */
3928
Piotr Sawicki129a9982018-07-19 11:42:58 +02003929 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003930 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003931 /*
3932 * If there is a secmark use it rather than the CIPSO label.
3933 * If there is no secmark fall back to CIPSO.
3934 * The secmark is assumed to reflect policy better.
3935 */
Casey Schaufler36be8122020-08-11 17:39:41 -07003936 skp = smack_from_skb(skb);
Casey Schauflera2af0312020-08-11 17:39:42 -07003937 if (skp == NULL) {
3938 skp = smack_from_netlbl(sk, family, skb);
3939 if (skp == NULL)
3940 skp = smack_net_ambient;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003941 }
Casey Schauflere114e472008-02-04 22:29:50 -08003942
Etienne Bassetecfcc532009-04-08 20:40:06 +02003943#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003944 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003945 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003946 ad.a.u.net->netif = skb->skb_iif;
3947 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003948#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003949 /*
3950 * Receiving a packet requires that the other end
3951 * be able to write here. Read access is not required.
3952 * This is the simplist possible security model
3953 * for networking.
3954 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003955 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3956 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003957 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003958 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003959 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003960 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003961#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003962 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003963 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003964 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3965 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003966 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003967#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler36be8122020-08-11 17:39:41 -07003968 skp = smack_from_skb(skb);
3969 if (skp == NULL) {
3970 if (smk_ipv6_localhost(&sadd))
3971 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003972 skp = smack_ipv6host_label(&sadd);
Casey Schaufler36be8122020-08-11 17:39:41 -07003973 if (skp == NULL)
3974 skp = smack_net_ambient;
3975 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003976#ifdef CONFIG_AUDIT
3977 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003978 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003979 ad.a.u.net->netif = skb->skb_iif;
3980 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3981#endif /* CONFIG_AUDIT */
3982 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3983 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3984 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003985#endif /* SMACK_IPV6_SECMARK_LABELING */
3986#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003987 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003988#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003989 if (rc != 0)
3990 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3991 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003992 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003993#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003994 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003995
Paul Moorea8134292008-10-10 10:16:31 -04003996 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003997}
3998
3999/**
4000 * smack_socket_getpeersec_stream - pull in packet label
4001 * @sock: the socket
4002 * @optval: user's destination
4003 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004004 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004005 *
4006 * returns zero on success, an error code otherwise
4007 */
4008static int smack_socket_getpeersec_stream(struct socket *sock,
4009 char __user *optval,
4010 int __user *optlen, unsigned len)
4011{
4012 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004013 char *rcp = "";
4014 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004015 int rc = 0;
4016
4017 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004018 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004019 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004020 slen = strlen(rcp) + 1;
4021 }
Casey Schauflere114e472008-02-04 22:29:50 -08004022
4023 if (slen > len)
4024 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004025 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004026 rc = -EFAULT;
4027
4028 if (put_user(slen, optlen) != 0)
4029 rc = -EFAULT;
4030
4031 return rc;
4032}
4033
4034
4035/**
4036 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004037 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004038 * @skb: packet data
4039 * @secid: pointer to where to put the secid of the packet
4040 *
4041 * Sets the netlabel socket state on sk from parent
4042 */
4043static int smack_socket_getpeersec_dgram(struct socket *sock,
4044 struct sk_buff *skb, u32 *secid)
4045
4046{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004047 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004048 struct smack_known *skp;
Casey Schauflera2af0312020-08-11 17:39:42 -07004049 struct sock *sk = NULL;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004050 int family = PF_UNSPEC;
4051 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004052
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004053 if (skb != NULL) {
4054 if (skb->protocol == htons(ETH_P_IP))
4055 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004056#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004057 else if (skb->protocol == htons(ETH_P_IPV6))
4058 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004059#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004060 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004061 if (family == PF_UNSPEC && sock != NULL)
4062 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004063
Casey Schaufler69f287a2014-12-12 17:08:40 -08004064 switch (family) {
4065 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004066 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004067 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004068 break;
4069 case PF_INET:
Casey Schaufler36be8122020-08-11 17:39:41 -07004070 skp = smack_from_skb(skb);
4071 if (skp) {
4072 s = skp->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004073 break;
Casey Schaufler36be8122020-08-11 17:39:41 -07004074 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004075 /*
4076 * Translate what netlabel gave us.
4077 */
Casey Schauflera2af0312020-08-11 17:39:42 -07004078 if (sock != NULL)
4079 sk = sock->sk;
4080 skp = smack_from_netlbl(sk, family, skb);
4081 if (skp != NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004082 s = skp->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004083 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004084 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004085#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler36be8122020-08-11 17:39:41 -07004086 skp = smack_from_skb(skb);
4087 if (skp)
4088 s = skp->smk_secid;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004089#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004090 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004091 }
4092 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004093 if (s == 0)
4094 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004095 return 0;
4096}
4097
4098/**
Paul Moore07feee82009-03-27 17:10:54 -04004099 * smack_sock_graft - Initialize a newly created socket with an existing sock
4100 * @sk: child sock
4101 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004102 *
Paul Moore07feee82009-03-27 17:10:54 -04004103 * Set the smk_{in,out} state of an existing sock based on the process that
4104 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004105 */
4106static void smack_sock_graft(struct sock *sk, struct socket *parent)
4107{
4108 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004109 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004110
Paul Moore07feee82009-03-27 17:10:54 -04004111 if (sk == NULL ||
4112 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004113 return;
4114
4115 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004116 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004117 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004118 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004119}
4120
4121/**
4122 * smack_inet_conn_request - Smack access check on connect
4123 * @sk: socket involved
4124 * @skb: packet
4125 * @req: unused
4126 *
4127 * Returns 0 if a task with the packet label could write to
4128 * the socket, otherwise an error code
4129 */
Florian Westphal41dd9592020-11-30 16:36:29 +01004130static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
Casey Schauflere114e472008-02-04 22:29:50 -08004131 struct request_sock *req)
4132{
Paul Moore07feee82009-03-27 17:10:54 -04004133 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004134 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004135 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004136 struct sockaddr_in addr;
4137 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004138 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004139 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004140 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004141#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004142 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004143#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004144
Casey Schaufler69f287a2014-12-12 17:08:40 -08004145#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004146 if (family == PF_INET6) {
4147 /*
4148 * Handle mapped IPv4 packets arriving
4149 * via IPv6 sockets. Don't set up netlabel
4150 * processing on IPv6.
4151 */
4152 if (skb->protocol == htons(ETH_P_IP))
4153 family = PF_INET;
4154 else
4155 return 0;
4156 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004157#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004158
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004159 /*
4160 * If there is a secmark use it rather than the CIPSO label.
4161 * If there is no secmark fall back to CIPSO.
4162 * The secmark is assumed to reflect policy better.
4163 */
Casey Schaufler36be8122020-08-11 17:39:41 -07004164 skp = smack_from_skb(skb);
Casey Schauflera2af0312020-08-11 17:39:42 -07004165 if (skp == NULL) {
4166 skp = smack_from_netlbl(sk, family, skb);
4167 if (skp == NULL)
4168 skp = &smack_known_huh;
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004169 }
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004170
Etienne Bassetecfcc532009-04-08 20:40:06 +02004171#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004172 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4173 ad.a.u.net->family = family;
4174 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004175 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4176#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004177 /*
Paul Moore07feee82009-03-27 17:10:54 -04004178 * Receiving a packet requires that the other end be able to write
4179 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004180 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004181 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4182 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004183 if (rc != 0)
4184 return rc;
4185
4186 /*
4187 * Save the peer's label in the request_sock so we can later setup
4188 * smk_packet in the child socket so that SO_PEERCRED can report it.
4189 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004190 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004191
4192 /*
4193 * We need to decide if we want to label the incoming connection here
4194 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004195 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004196 */
4197 hdr = ip_hdr(skb);
4198 addr.sin_addr.s_addr = hdr->saddr;
4199 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004200 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004201 rcu_read_unlock();
4202
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004203 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004204 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004205 else
Paul Moore07feee82009-03-27 17:10:54 -04004206 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004207
4208 return rc;
4209}
4210
Paul Moore07feee82009-03-27 17:10:54 -04004211/**
4212 * smack_inet_csk_clone - Copy the connection information to the new socket
4213 * @sk: the new socket
4214 * @req: the connection's request_sock
4215 *
4216 * Transfer the connection's peer label to the newly created socket.
4217 */
4218static void smack_inet_csk_clone(struct sock *sk,
4219 const struct request_sock *req)
4220{
4221 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004222 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004223
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004224 if (req->peer_secid != 0) {
4225 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004226 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004227 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004228 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004229}
4230
Casey Schauflere114e472008-02-04 22:29:50 -08004231/*
4232 * Key management security hooks
4233 *
4234 * Casey has not tested key support very heavily.
4235 * The permission check is most likely too restrictive.
4236 * If you care about keys please have a look.
4237 */
4238#ifdef CONFIG_KEYS
4239
4240/**
4241 * smack_key_alloc - Set the key security blob
4242 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004243 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004244 * @flags: unused
4245 *
4246 * No allocation required
4247 *
4248 * Returns 0
4249 */
David Howellsd84f4f92008-11-14 10:39:23 +11004250static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004251 unsigned long flags)
4252{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004253 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004254
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004255 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004256 return 0;
4257}
4258
4259/**
4260 * smack_key_free - Clear the key security blob
4261 * @key: the object
4262 *
4263 * Clear the blob pointer
4264 */
4265static void smack_key_free(struct key *key)
4266{
4267 key->security = NULL;
4268}
4269
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004270/**
Casey Schauflere114e472008-02-04 22:29:50 -08004271 * smack_key_permission - Smack access on a key
4272 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004273 * @cred: the credentials to use
David Howells8c0637e2020-05-12 15:16:29 +01004274 * @need_perm: requested key permission
Casey Schauflere114e472008-02-04 22:29:50 -08004275 *
4276 * Return 0 if the task has read and write to the object,
4277 * an error code otherwise
4278 */
4279static int smack_key_permission(key_ref_t key_ref,
David Howells8c0637e2020-05-12 15:16:29 +01004280 const struct cred *cred,
4281 enum key_need_perm need_perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004282{
4283 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004284 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004285 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004286 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004287 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004288
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004289 /*
4290 * Validate requested permissions
4291 */
David Howells8c0637e2020-05-12 15:16:29 +01004292 switch (need_perm) {
4293 case KEY_NEED_READ:
4294 case KEY_NEED_SEARCH:
4295 case KEY_NEED_VIEW:
4296 request |= MAY_READ;
4297 break;
4298 case KEY_NEED_WRITE:
4299 case KEY_NEED_LINK:
4300 case KEY_NEED_SETATTR:
4301 request |= MAY_WRITE;
4302 break;
4303 case KEY_NEED_UNSPECIFIED:
4304 case KEY_NEED_UNLINK:
4305 case KEY_SYSADMIN_OVERRIDE:
4306 case KEY_AUTHTOKEN_OVERRIDE:
4307 case KEY_DEFER_PERM_CHECK:
4308 return 0;
4309 default:
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004310 return -EINVAL;
David Howells8c0637e2020-05-12 15:16:29 +01004311 }
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004312
Casey Schauflere114e472008-02-04 22:29:50 -08004313 keyp = key_ref_to_ptr(key_ref);
4314 if (keyp == NULL)
4315 return -EINVAL;
4316 /*
4317 * If the key hasn't been initialized give it access so that
4318 * it may do so.
4319 */
4320 if (keyp->security == NULL)
4321 return 0;
4322 /*
4323 * This should not occur
4324 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004325 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004326 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004327
David Howellsa8478a62020-01-14 17:07:13 +00004328 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004329 return 0;
4330
Etienne Bassetecfcc532009-04-08 20:40:06 +02004331#ifdef CONFIG_AUDIT
4332 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4333 ad.a.u.key_struct.key = keyp->serial;
4334 ad.a.u.key_struct.key_desc = keyp->description;
4335#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07004336 rc = smk_access(tkp, keyp->security, request, &ad);
4337 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4338 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004339}
José Bollo7fc5f362015-02-17 15:41:22 +01004340
4341/*
4342 * smack_key_getsecurity - Smack label tagging the key
4343 * @key points to the key to be queried
4344 * @_buffer points to a pointer that should be set to point to the
4345 * resulting string (if no label or an error occurs).
4346 * Return the length of the string (including terminating NUL) or -ve if
4347 * an error.
4348 * May also return 0 (and a NULL buffer pointer) if there is no label.
4349 */
4350static int smack_key_getsecurity(struct key *key, char **_buffer)
4351{
4352 struct smack_known *skp = key->security;
4353 size_t length;
4354 char *copy;
4355
4356 if (key->security == NULL) {
4357 *_buffer = NULL;
4358 return 0;
4359 }
4360
4361 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4362 if (copy == NULL)
4363 return -ENOMEM;
4364 length = strlen(copy) + 1;
4365
4366 *_buffer = copy;
4367 return length;
4368}
4369
David Howellsa8478a62020-01-14 17:07:13 +00004370
4371#ifdef CONFIG_KEY_NOTIFICATIONS
4372/**
4373 * smack_watch_key - Smack access to watch a key for notifications.
4374 * @key: The key to be watched
4375 *
4376 * Return 0 if the @watch->cred has permission to read from the key object and
4377 * an error otherwise.
4378 */
4379static int smack_watch_key(struct key *key)
4380{
4381 struct smk_audit_info ad;
4382 struct smack_known *tkp = smk_of_current();
4383 int rc;
4384
4385 if (key == NULL)
4386 return -EINVAL;
4387 /*
4388 * If the key hasn't been initialized give it access so that
4389 * it may do so.
4390 */
4391 if (key->security == NULL)
4392 return 0;
4393 /*
4394 * This should not occur
4395 */
4396 if (tkp == NULL)
4397 return -EACCES;
4398
4399 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4400 return 0;
4401
4402#ifdef CONFIG_AUDIT
4403 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4404 ad.a.u.key_struct.key = key->serial;
4405 ad.a.u.key_struct.key_desc = key->description;
4406#endif
4407 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4408 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4409 return rc;
4410}
4411#endif /* CONFIG_KEY_NOTIFICATIONS */
Casey Schauflere114e472008-02-04 22:29:50 -08004412#endif /* CONFIG_KEYS */
4413
David Howellsa8478a62020-01-14 17:07:13 +00004414#ifdef CONFIG_WATCH_QUEUE
4415/**
4416 * smack_post_notification - Smack access to post a notification to a queue
4417 * @w_cred: The credentials of the watcher.
4418 * @cred: The credentials of the event source (may be NULL).
4419 * @n: The notification message to be posted.
4420 */
4421static int smack_post_notification(const struct cred *w_cred,
4422 const struct cred *cred,
4423 struct watch_notification *n)
4424{
4425 struct smk_audit_info ad;
4426 struct smack_known *subj, *obj;
4427 int rc;
4428
4429 /* Always let maintenance notifications through. */
4430 if (n->type == WATCH_TYPE_META)
4431 return 0;
4432
4433 if (!cred)
4434 return 0;
4435 subj = smk_of_task(smack_cred(cred));
4436 obj = smk_of_task(smack_cred(w_cred));
4437
4438 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4439 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4440 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4441 return rc;
4442}
4443#endif /* CONFIG_WATCH_QUEUE */
4444
Casey Schauflere114e472008-02-04 22:29:50 -08004445/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004446 * Smack Audit hooks
4447 *
4448 * Audit requires a unique representation of each Smack specific
4449 * rule. This unique representation is used to distinguish the
4450 * object to be audited from remaining kernel objects and also
4451 * works as a glue between the audit hooks.
4452 *
4453 * Since repository entries are added but never deleted, we'll use
4454 * the smack_known label address related to the given audit rule as
4455 * the needed unique representation. This also better fits the smack
4456 * model where nearly everything is a label.
4457 */
4458#ifdef CONFIG_AUDIT
4459
4460/**
4461 * smack_audit_rule_init - Initialize a smack audit rule
4462 * @field: audit rule fields given from user-space (audit.h)
4463 * @op: required testing operator (=, !=, >, <, ...)
4464 * @rulestr: smack label to be audited
4465 * @vrule: pointer to save our own audit rule representation
4466 *
4467 * Prepare to audit cases where (@field @op @rulestr) is true.
4468 * The label to be audited is created if necessay.
4469 */
4470static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4471{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004472 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004473 char **rule = (char **)vrule;
4474 *rule = NULL;
4475
4476 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4477 return -EINVAL;
4478
Al Viro5af75d82008-12-16 05:59:26 -05004479 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004480 return -EINVAL;
4481
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004482 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004483 if (IS_ERR(skp))
4484 return PTR_ERR(skp);
4485
4486 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004487
4488 return 0;
4489}
4490
4491/**
4492 * smack_audit_rule_known - Distinguish Smack audit rules
4493 * @krule: rule of interest, in Audit kernel representation format
4494 *
4495 * This is used to filter Smack rules from remaining Audit ones.
4496 * If it's proved that this rule belongs to us, the
4497 * audit_rule_match hook will be called to do the final judgement.
4498 */
4499static int smack_audit_rule_known(struct audit_krule *krule)
4500{
4501 struct audit_field *f;
4502 int i;
4503
4504 for (i = 0; i < krule->field_count; i++) {
4505 f = &krule->fields[i];
4506
4507 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4508 return 1;
4509 }
4510
4511 return 0;
4512}
4513
4514/**
4515 * smack_audit_rule_match - Audit given object ?
4516 * @secid: security id for identifying the object to test
4517 * @field: audit rule flags given from user-space
4518 * @op: required testing operator
4519 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004520 *
4521 * The core Audit hook. It's used to take the decision of
4522 * whether to audit or not to audit a given object.
4523 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004524static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004525{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004526 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004527 char *rule = vrule;
4528
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004529 if (unlikely(!rule)) {
4530 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004531 return -ENOENT;
4532 }
4533
4534 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4535 return 0;
4536
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004537 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004538
4539 /*
4540 * No need to do string comparisons. If a match occurs,
4541 * both pointers will point to the same smack_known
4542 * label.
4543 */
Al Viro5af75d82008-12-16 05:59:26 -05004544 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004545 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004546 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004547 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004548
4549 return 0;
4550}
4551
Casey Schaufler491a0b02016-01-26 15:08:35 -08004552/*
4553 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004554 * No memory was allocated.
4555 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004556
4557#endif /* CONFIG_AUDIT */
4558
Randy Dunlap251a2a92009-02-18 11:42:33 -08004559/**
David Quigley746df9b2013-05-22 12:50:35 -04004560 * smack_ismaclabel - check if xattr @name references a smack MAC label
4561 * @name: Full xattr name to check.
4562 */
4563static int smack_ismaclabel(const char *name)
4564{
4565 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4566}
4567
4568
4569/**
Casey Schauflere114e472008-02-04 22:29:50 -08004570 * smack_secid_to_secctx - return the smack label for a secid
4571 * @secid: incoming integer
4572 * @secdata: destination
4573 * @seclen: how long it is
4574 *
4575 * Exists for networking code.
4576 */
4577static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4578{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004579 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004580
Eric Parisd5630b92010-10-13 16:24:48 -04004581 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004582 *secdata = skp->smk_known;
4583 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004584 return 0;
4585}
4586
Randy Dunlap251a2a92009-02-18 11:42:33 -08004587/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004588 * smack_secctx_to_secid - return the secid for a smack label
4589 * @secdata: smack label
4590 * @seclen: how long result is
4591 * @secid: outgoing integer
4592 *
4593 * Exists for audit and networking code.
4594 */
David Howellse52c17642008-04-29 20:52:51 +01004595static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004596{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004597 struct smack_known *skp = smk_find_entry(secdata);
4598
4599 if (skp)
4600 *secid = skp->smk_secid;
4601 else
4602 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004603 return 0;
4604}
4605
Casey Schaufler491a0b02016-01-26 15:08:35 -08004606/*
4607 * There used to be a smack_release_secctx hook
4608 * that did nothing back when hooks were in a vector.
4609 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004610 */
Casey Schauflere114e472008-02-04 22:29:50 -08004611
David P. Quigley1ee65e32009-09-03 14:25:57 -04004612static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4613{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01004614 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4615 ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004616}
4617
4618static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4619{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01004620 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4621 ctx, ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004622}
4623
4624static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4625{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004626 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004627
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004628 *ctx = skp->smk_known;
4629 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004630 return 0;
4631}
4632
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004633static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4634{
4635
4636 struct task_smack *tsp;
4637 struct smack_known *skp;
4638 struct inode_smack *isp;
4639 struct cred *new_creds = *new;
4640
4641 if (new_creds == NULL) {
4642 new_creds = prepare_creds();
4643 if (new_creds == NULL)
4644 return -ENOMEM;
4645 }
4646
Casey Schauflerb17103a2018-11-09 16:12:56 -08004647 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004648
4649 /*
4650 * Get label from overlay inode and set it in create_sid
4651 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004652 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004653 skp = isp->smk_inode;
4654 tsp->smk_task = skp;
4655 *new = new_creds;
4656 return 0;
4657}
4658
4659static int smack_inode_copy_up_xattr(const char *name)
4660{
4661 /*
4662 * Return 1 if this is the smack access Smack attribute.
4663 */
4664 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4665 return 1;
4666
4667 return -EOPNOTSUPP;
4668}
4669
4670static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4671 struct qstr *name,
4672 const struct cred *old,
4673 struct cred *new)
4674{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004675 struct task_smack *otsp = smack_cred(old);
4676 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004677 struct inode_smack *isp;
4678 int may;
4679
4680 /*
4681 * Use the process credential unless all of
4682 * the transmuting criteria are met
4683 */
4684 ntsp->smk_task = otsp->smk_task;
4685
4686 /*
4687 * the attribute of the containing directory
4688 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004689 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004690
4691 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4692 rcu_read_lock();
4693 may = smk_access_entry(otsp->smk_task->smk_known,
4694 isp->smk_inode->smk_known,
4695 &otsp->smk_task->smk_rules);
4696 rcu_read_unlock();
4697
4698 /*
4699 * If the directory is transmuting and the rule
4700 * providing access is transmuting use the containing
4701 * directory label instead of the process label.
4702 */
Roberto Sassu3586b3f2023-05-08 19:02:34 +02004703 if (may > 0 && (may & MAY_TRANSMUTE)) {
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004704 ntsp->smk_task = isp->smk_inode;
Roberto Sassu3586b3f2023-05-08 19:02:34 +02004705 ntsp->smk_transmuted = ntsp->smk_task;
4706 }
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004707 }
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 Schaufler1aea7802021-04-22 17:41:15 +02004717 .lbs_superblock = sizeof(struct superblock_smack),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004718};
4719
James Morrisca97d932017-02-15 00:18:51 +11004720static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004721 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4722 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4723 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004724
Al Viro0b520752018-12-23 16:02:47 -05004725 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004726 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4727
Casey Schauflere20b0432015-05-02 15:11:36 -07004728 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_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};