blob: a5a78aef6403acc0239b9c4d584dfdc8c241e78f [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;
Roberto Sassu0e345042023-05-08 19:02:33 +02001432 struct inode_smack *ispp;
1433 size_t label_len;
1434 char *label = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08001435
Roberto Sassu0e345042023-05-08 19:02:33 +02001436 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001437 isp = smk_of_inode(inode);
Roberto Sassu0e345042023-05-08 19:02:33 +02001438 } else if (strcmp(name, XATTR_SMACK_TRANSMUTE) == 0) {
1439 ispp = smack_inode(inode);
1440 if (ispp->smk_flags & SMK_INODE_TRANSMUTE)
1441 label = TRANS_TRUE;
1442 else
1443 label = "";
1444 } else {
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001445 /*
1446 * The rest of the Smack xattrs are only on sockets.
1447 */
1448 sbp = ip->i_sb;
1449 if (sbp->s_magic != SOCKFS_MAGIC)
1450 return -EOPNOTSUPP;
1451
1452 sock = SOCKET_I(ip);
1453 if (sock == NULL || sock->sk == NULL)
1454 return -EOPNOTSUPP;
1455
1456 ssp = sock->sk->sk_security;
1457
1458 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1459 isp = ssp->smk_in;
1460 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1461 isp = ssp->smk_out;
1462 else
1463 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001464 }
1465
Roberto Sassu0e345042023-05-08 19:02:33 +02001466 if (!label)
1467 label = isp->smk_known;
1468
1469 label_len = strlen(label);
1470
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001471 if (alloc) {
Roberto Sassu0e345042023-05-08 19:02:33 +02001472 *buffer = kstrdup(label, GFP_KERNEL);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001473 if (*buffer == NULL)
1474 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001475 }
1476
Roberto Sassu0e345042023-05-08 19:02:33 +02001477 return label_len;
Casey Schauflere114e472008-02-04 22:29:50 -08001478}
1479
1480
1481/**
1482 * smack_inode_listsecurity - list the Smack attributes
1483 * @inode: the object
1484 * @buffer: where they go
1485 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001486 */
1487static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1488 size_t buffer_size)
1489{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001490 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001491
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001492 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001493 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001494
1495 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001496}
1497
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001498/**
1499 * smack_inode_getsecid - Extract inode's security id
1500 * @inode: inode to extract the info from
1501 * @secid: where result will be saved
1502 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001503static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001504{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001505 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001506
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001507 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001508}
1509
Casey Schauflere114e472008-02-04 22:29:50 -08001510/*
1511 * File Hooks
1512 */
1513
Casey Schaufler491a0b02016-01-26 15:08:35 -08001514/*
1515 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001516 *
1517 * Should access checks be done on each read or write?
1518 * UNICOS and SELinux say yes.
1519 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1520 *
1521 * I'll say no for now. Smack does not do the frequent
1522 * label changing that SELinux does.
1523 */
Casey Schauflere114e472008-02-04 22:29:50 -08001524
1525/**
1526 * smack_file_alloc_security - assign a file security blob
1527 * @file: the object
1528 *
1529 * The security blob for a file is a pointer to the master
1530 * label list, so no allocation is done.
1531 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001532 * f_security is the owner security information. It
1533 * isn't used on file access checks, it's for send_sigio.
1534 *
Casey Schauflere114e472008-02-04 22:29:50 -08001535 * Returns 0
1536 */
1537static int smack_file_alloc_security(struct file *file)
1538{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001539 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001540
Casey Schauflerf28952a2018-11-12 09:38:53 -08001541 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001542 return 0;
1543}
1544
1545/**
Casey Schauflere114e472008-02-04 22:29:50 -08001546 * smack_file_ioctl - Smack check on ioctls
1547 * @file: the object
1548 * @cmd: what to do
1549 * @arg: unused
1550 *
1551 * Relies heavily on the correct use of the ioctl command conventions.
1552 *
1553 * Returns 0 if allowed, error code otherwise
1554 */
1555static int smack_file_ioctl(struct file *file, unsigned int cmd,
1556 unsigned long arg)
1557{
1558 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001559 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001560 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001561
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001562 if (unlikely(IS_PRIVATE(inode)))
1563 return 0;
1564
Eric Parisf48b7392011-04-25 12:54:27 -04001565 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001566 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001567
Casey Schauflerd166c802014-08-27 14:51:27 -07001568 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001569 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001570 rc = smk_bu_file(file, MAY_WRITE, rc);
1571 }
Casey Schauflere114e472008-02-04 22:29:50 -08001572
Casey Schauflerd166c802014-08-27 14:51:27 -07001573 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001574 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001575 rc = smk_bu_file(file, MAY_READ, rc);
1576 }
Casey Schauflere114e472008-02-04 22:29:50 -08001577
1578 return rc;
1579}
1580
1581/**
1582 * smack_file_lock - Smack check on file locking
1583 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001584 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001585 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001586 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001587 */
1588static int smack_file_lock(struct file *file, unsigned int cmd)
1589{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001590 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001591 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001592 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001593
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001594 if (unlikely(IS_PRIVATE(inode)))
1595 return 0;
1596
Eric Paris92f42502011-04-25 13:15:55 -04001597 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1598 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001599 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001600 rc = smk_bu_file(file, MAY_LOCK, rc);
1601 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001602}
1603
1604/**
1605 * smack_file_fcntl - Smack check on fcntl
1606 * @file: the object
1607 * @cmd: what action to check
1608 * @arg: unused
1609 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001610 * Generally these operations are harmless.
1611 * File locking operations present an obvious mechanism
1612 * for passing information, so they require write access.
1613 *
Casey Schauflere114e472008-02-04 22:29:50 -08001614 * Returns 0 if current has access, error code otherwise
1615 */
1616static int smack_file_fcntl(struct file *file, unsigned int cmd,
1617 unsigned long arg)
1618{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001619 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001620 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001621 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001622
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001623 if (unlikely(IS_PRIVATE(inode)))
1624 return 0;
1625
Casey Schauflere114e472008-02-04 22:29:50 -08001626 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001627 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001628 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001629 case F_SETLK:
1630 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001631 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1632 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001633 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001634 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001635 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001636 case F_SETOWN:
1637 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001638 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1639 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001640 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001641 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001642 break;
1643 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001644 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001645 }
1646
1647 return rc;
1648}
1649
1650/**
Al Viroe5467852012-05-30 13:30:51 -04001651 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001652 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1653 * if mapping anonymous memory.
1654 * @file contains the file structure for file to map (may be NULL).
1655 * @reqprot contains the protection requested by the application.
1656 * @prot contains the protection that will be applied by the kernel.
1657 * @flags contains the operational flags.
1658 * Return 0 if permission is granted.
1659 */
Al Viroe5467852012-05-30 13:30:51 -04001660static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001661 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001662 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001663{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001664 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001665 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001666 struct smack_rule *srp;
1667 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001668 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001669 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001670 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001671 int may;
1672 int mmay;
1673 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001674 int rc;
1675
Al Viro496ad9a2013-01-23 17:07:38 -05001676 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001677 return 0;
1678
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001679 if (unlikely(IS_PRIVATE(file_inode(file))))
1680 return 0;
1681
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001682 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001683 if (isp->smk_mmap == NULL)
1684 return 0;
Casey Schaufler1aea7802021-04-22 17:41:15 +02001685 sbsp = smack_superblock(file_inode(file)->i_sb);
Seth Forshee809c02e2016-04-26 14:36:22 -05001686 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1687 isp->smk_mmap != sbsp->smk_root)
1688 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001689 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001690
Casey Schauflerb17103a2018-11-09 16:12:56 -08001691 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001692 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001693 rc = 0;
1694
1695 rcu_read_lock();
1696 /*
1697 * For each Smack rule associated with the subject
1698 * label verify that the SMACK64MMAP also has access
1699 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001700 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001701 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001702 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001703 /*
1704 * Matching labels always allows access.
1705 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001706 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001707 continue;
1708 /*
1709 * If there is a matching local rule take
1710 * that into account as well.
1711 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001712 may = smk_access_entry(srp->smk_subject->smk_known,
1713 okp->smk_known,
1714 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001715 if (may == -ENOENT)
1716 may = srp->smk_access;
1717 else
1718 may &= srp->smk_access;
1719 /*
1720 * If may is zero the SMACK64MMAP subject can't
1721 * possibly have less access.
1722 */
1723 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001724 continue;
1725
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001726 /*
1727 * Fetch the global list entry.
1728 * If there isn't one a SMACK64MMAP subject
1729 * can't have as much access as current.
1730 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001731 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1732 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001733 if (mmay == -ENOENT) {
1734 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001735 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001736 }
1737 /*
1738 * If there is a local entry it modifies the
1739 * potential access, too.
1740 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001741 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1742 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001743 if (tmay != -ENOENT)
1744 mmay &= tmay;
1745
1746 /*
1747 * If there is any access available to current that is
1748 * not available to a SMACK64MMAP subject
1749 * deny access.
1750 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001751 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001752 rc = -EACCES;
1753 break;
1754 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001755 }
1756
1757 rcu_read_unlock();
1758
1759 return rc;
1760}
1761
1762/**
Casey Schauflere114e472008-02-04 22:29:50 -08001763 * smack_file_set_fowner - set the file security blob value
1764 * @file: object in question
1765 *
Casey Schauflere114e472008-02-04 22:29:50 -08001766 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001767static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001768{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001769 struct smack_known **blob = smack_file(file);
1770
1771 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001772}
1773
1774/**
1775 * smack_file_send_sigiotask - Smack on sigio
1776 * @tsk: The target task
1777 * @fown: the object the signal come from
1778 * @signum: unused
1779 *
1780 * Allow a privileged task to get signals even if it shouldn't
1781 *
1782 * Returns 0 if a subject with the object's smack could
1783 * write to the task, an error code otherwise.
1784 */
1785static int smack_file_send_sigiotask(struct task_struct *tsk,
1786 struct fown_struct *fown, int signum)
1787{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001788 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001789 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001790 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001791 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001792 struct file *file;
1793 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001794 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001795
1796 /*
1797 * struct fown_struct is never outside the context of a struct file
1798 */
1799 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001800
Etienne Bassetecfcc532009-04-08 20:40:06 +02001801 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001802 blob = smack_file(file);
1803 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001804 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1805 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001806
1807 rcu_read_lock();
1808 tcred = __task_cred(tsk);
1809 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001810 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001811 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001812
1813 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1814 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001815 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001816 return rc;
1817}
1818
1819/**
1820 * smack_file_receive - Smack file receive check
1821 * @file: the object
1822 *
1823 * Returns 0 if current has access, error code otherwise
1824 */
1825static int smack_file_receive(struct file *file)
1826{
Casey Schauflerd166c802014-08-27 14:51:27 -07001827 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001828 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001829 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001830 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001831 struct socket *sock;
1832 struct task_smack *tsp;
1833 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001834
Seung-Woo Kim97775822015-04-17 15:25:04 +09001835 if (unlikely(IS_PRIVATE(inode)))
1836 return 0;
1837
Casey Schaufler4482a442013-12-30 17:37:45 -08001838 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001839 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001840
Casey Schaufler51d59af2017-05-31 08:53:42 -07001841 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001842 sock = SOCKET_I(inode);
1843 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001844 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001845 /*
1846 * If the receiving process can't write to the
1847 * passed socket or if the passed socket can't
1848 * write to the receiving process don't accept
1849 * the passed socket.
1850 */
1851 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1852 rc = smk_bu_file(file, may, rc);
1853 if (rc < 0)
1854 return rc;
1855 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1856 rc = smk_bu_file(file, may, rc);
1857 return rc;
1858 }
Casey Schauflere114e472008-02-04 22:29:50 -08001859 /*
1860 * This code relies on bitmasks.
1861 */
1862 if (file->f_mode & FMODE_READ)
1863 may = MAY_READ;
1864 if (file->f_mode & FMODE_WRITE)
1865 may |= MAY_WRITE;
1866
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001867 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001868 rc = smk_bu_file(file, may, rc);
1869 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001870}
1871
Casey Schaufler531f1d42011-09-19 12:41:42 -07001872/**
Eric Paris83d49852012-04-04 13:45:40 -04001873 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001874 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001875 *
1876 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001877 * Allow the open only if the task has read access. There are
1878 * many read operations (e.g. fstat) that you can do with an
1879 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001880 *
luanshia1a07f22019-07-05 10:35:20 +08001881 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001882 */
Al Viro94817692018-07-10 14:13:18 -04001883static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001884{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001885 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001886 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001887 struct smk_audit_info ad;
1888 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001889
Casey Schauflera6834c02014-04-21 11:10:26 -07001890 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1891 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301892 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001893 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001894
1895 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001896}
1897
Casey Schauflere114e472008-02-04 22:29:50 -08001898/*
1899 * Task hooks
1900 */
1901
1902/**
David Howellsee18d642009-09-02 09:14:21 +01001903 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001904 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001905 * @gfp: the atomicity of any memory allocations
1906 *
1907 * Prepare a blank set of credentials for modification. This must allocate all
1908 * the memory the LSM module might require such that cred_transfer() can
1909 * complete without error.
1910 */
1911static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1912{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001913 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001914 return 0;
1915}
1916
1917
1918/**
David Howellsf1752ee2008-11-14 10:39:17 +11001919 * smack_cred_free - "free" task-level security credentials
1920 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001921 *
Casey Schauflere114e472008-02-04 22:29:50 -08001922 */
David Howellsf1752ee2008-11-14 10:39:17 +11001923static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001924{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001925 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001926 struct smack_rule *rp;
1927 struct list_head *l;
1928 struct list_head *n;
1929
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001930 smk_destroy_label_list(&tsp->smk_relabel);
1931
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001932 list_for_each_safe(l, n, &tsp->smk_rules) {
1933 rp = list_entry(l, struct smack_rule, list);
1934 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001935 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001936 }
Casey Schauflere114e472008-02-04 22:29:50 -08001937}
1938
1939/**
David Howellsd84f4f92008-11-14 10:39:23 +11001940 * smack_cred_prepare - prepare new set of credentials for modification
1941 * @new: the new credentials
1942 * @old: the original credentials
1943 * @gfp: the atomicity of any memory allocations
1944 *
1945 * Prepare a new set of credentials for modification.
1946 */
1947static int smack_cred_prepare(struct cred *new, const struct cred *old,
1948 gfp_t gfp)
1949{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001950 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001951 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001952 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001953
Casey Schauflerbbd36622018-11-12 09:30:56 -08001954 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301955
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001956 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1957 if (rc != 0)
1958 return rc;
1959
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001960 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1961 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001962 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001963}
1964
Randy Dunlap251a2a92009-02-18 11:42:33 -08001965/**
David Howellsee18d642009-09-02 09:14:21 +01001966 * smack_cred_transfer - Transfer the old credentials to the new credentials
1967 * @new: the new credentials
1968 * @old: the original credentials
1969 *
1970 * Fill in a set of blank credentials from another set of credentials.
1971 */
1972static void smack_cred_transfer(struct cred *new, const struct cred *old)
1973{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001974 struct task_smack *old_tsp = smack_cred(old);
1975 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001976
1977 new_tsp->smk_task = old_tsp->smk_task;
1978 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001979 mutex_init(&new_tsp->smk_rules_lock);
1980 INIT_LIST_HEAD(&new_tsp->smk_rules);
1981
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001982 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001983}
1984
1985/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001986 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001987 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001988 * @secid: where to put the result
1989 *
1990 * Sets the secid to contain a u32 version of the smack label.
1991 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001992static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001993{
1994 struct smack_known *skp;
1995
1996 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001997 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001998 *secid = skp->smk_secid;
1999 rcu_read_unlock();
2000}
2001
2002/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002003 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002004 * @new: points to the set of credentials to be modified.
2005 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002006 *
2007 * Set the security data for a kernel service.
2008 */
2009static int smack_kernel_act_as(struct cred *new, u32 secid)
2010{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002011 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002012
Casey Schaufler152f91d2016-11-14 09:38:15 -08002013 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002014 return 0;
2015}
2016
2017/**
2018 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002019 * @new: points to the set of credentials to be modified
2020 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002021 *
2022 * Set the file creation context in a set of credentials to the same
2023 * as the objective context of the specified inode
2024 */
2025static int smack_kernel_create_files_as(struct cred *new,
2026 struct inode *inode)
2027{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002028 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002029 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002030
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002031 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002032 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002033 return 0;
2034}
2035
2036/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002037 * smk_curacc_on_task - helper to log task related access
2038 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002039 * @access: the access requested
2040 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002041 *
2042 * Return 0 if access is permitted
2043 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002044static int smk_curacc_on_task(struct task_struct *p, int access,
2045 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002046{
2047 struct smk_audit_info ad;
Paul Moorea3727a82021-09-23 09:50:11 -04002048 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002049 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002050
Casey Schaufler531f1d42011-09-19 12:41:42 -07002051 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002052 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002053 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002054 rc = smk_bu_task(p, access, rc);
2055 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002056}
2057
2058/**
Casey Schauflere114e472008-02-04 22:29:50 -08002059 * smack_task_setpgid - Smack check on setting pgid
2060 * @p: the task object
2061 * @pgid: unused
2062 *
2063 * Return 0 if write access is permitted
2064 */
2065static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2066{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002067 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002068}
2069
2070/**
2071 * smack_task_getpgid - Smack access check for getpgid
2072 * @p: the object task
2073 *
2074 * Returns 0 if current can read the object task, error code otherwise
2075 */
2076static int smack_task_getpgid(struct task_struct *p)
2077{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002078 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002079}
2080
2081/**
2082 * smack_task_getsid - Smack access check for getsid
2083 * @p: the object task
2084 *
2085 * Returns 0 if current can read the object task, error code otherwise
2086 */
2087static int smack_task_getsid(struct task_struct *p)
2088{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002089 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002090}
2091
2092/**
Paul Moore1fb057d2021-02-19 15:04:58 -05002093 * smack_task_getsecid_subj - get the subjective secid of the task
2094 * @p: the task
Casey Schauflere114e472008-02-04 22:29:50 -08002095 * @secid: where to put the result
2096 *
Paul Moore1fb057d2021-02-19 15:04:58 -05002097 * Sets the secid to contain a u32 version of the task's subjective smack label.
Casey Schauflere114e472008-02-04 22:29:50 -08002098 */
Paul Moore1fb057d2021-02-19 15:04:58 -05002099static void smack_task_getsecid_subj(struct task_struct *p, u32 *secid)
Casey Schauflere114e472008-02-04 22:29:50 -08002100{
Paul Moore1fb057d2021-02-19 15:04:58 -05002101 struct smack_known *skp = smk_of_task_struct_subj(p);
2102
2103 *secid = skp->smk_secid;
2104}
2105
2106/**
2107 * smack_task_getsecid_obj - get the objective secid of the task
2108 * @p: the task
2109 * @secid: where to put the result
2110 *
2111 * Sets the secid to contain a u32 version of the task's objective smack label.
2112 */
2113static void smack_task_getsecid_obj(struct task_struct *p, u32 *secid)
2114{
2115 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002116
2117 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002118}
2119
2120/**
2121 * smack_task_setnice - Smack check on setting nice
2122 * @p: the task object
2123 * @nice: unused
2124 *
2125 * Return 0 if write access is permitted
2126 */
2127static int smack_task_setnice(struct task_struct *p, int nice)
2128{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002129 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002130}
2131
2132/**
2133 * smack_task_setioprio - Smack check on setting ioprio
2134 * @p: the task object
2135 * @ioprio: unused
2136 *
2137 * Return 0 if write access is permitted
2138 */
2139static int smack_task_setioprio(struct task_struct *p, int ioprio)
2140{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002141 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002142}
2143
2144/**
2145 * smack_task_getioprio - Smack check on reading ioprio
2146 * @p: the task object
2147 *
2148 * Return 0 if read access is permitted
2149 */
2150static int smack_task_getioprio(struct task_struct *p)
2151{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002152 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002153}
2154
2155/**
2156 * smack_task_setscheduler - Smack check on setting scheduler
2157 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002158 *
2159 * Return 0 if read access is permitted
2160 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002161static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002162{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002163 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002164}
2165
2166/**
2167 * smack_task_getscheduler - Smack check on reading scheduler
2168 * @p: the task object
2169 *
2170 * Return 0 if read access is permitted
2171 */
2172static int smack_task_getscheduler(struct task_struct *p)
2173{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002174 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002175}
2176
2177/**
2178 * smack_task_movememory - Smack check on moving memory
2179 * @p: the task object
2180 *
2181 * Return 0 if write access is permitted
2182 */
2183static int smack_task_movememory(struct task_struct *p)
2184{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002185 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002186}
2187
2188/**
2189 * smack_task_kill - Smack check on signal delivery
2190 * @p: the task object
2191 * @info: unused
2192 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002193 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002194 *
2195 * Return 0 if write access is permitted
2196 *
Casey Schauflere114e472008-02-04 22:29:50 -08002197 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002198static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002199 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002200{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002201 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002202 struct smack_known *skp;
Paul Moore1fb057d2021-02-19 15:04:58 -05002203 struct smack_known *tkp = smk_of_task_struct_obj(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002204 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002205
Rafal Krypa18d872f2016-04-04 11:14:53 +02002206 if (!sig)
2207 return 0; /* null signal; existence test */
2208
Etienne Bassetecfcc532009-04-08 20:40:06 +02002209 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2210 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002211 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002212 * Sending a signal requires that the sender
2213 * can write the receiver.
2214 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002215 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002216 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2217 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002218 return rc;
2219 }
Casey Schauflere114e472008-02-04 22:29:50 -08002220 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002221 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002222 * specific behavior. This is not clean. For one thing
2223 * we can't take privilege into account.
2224 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002225 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002226 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2227 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002228 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002229}
2230
2231/**
Casey Schauflere114e472008-02-04 22:29:50 -08002232 * smack_task_to_inode - copy task smack into the inode blob
2233 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002234 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002235 *
2236 * Sets the smack pointer in the inode security blob
2237 */
2238static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2239{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002240 struct inode_smack *isp = smack_inode(inode);
Paul Moore1fb057d2021-02-19 15:04:58 -05002241 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002242
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002243 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002244 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002245}
2246
2247/*
2248 * Socket hooks.
2249 */
2250
2251/**
2252 * smack_sk_alloc_security - Allocate a socket blob
2253 * @sk: the socket
2254 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002255 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002256 *
2257 * Assign Smack pointers to current
2258 *
2259 * Returns 0 on success, -ENOMEM is there's no memory
2260 */
2261static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2262{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002263 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002264 struct socket_smack *ssp;
2265
2266 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2267 if (ssp == NULL)
2268 return -ENOMEM;
2269
jooseong lee08382c92016-11-03 11:54:39 +01002270 /*
2271 * Sockets created by kernel threads receive web label.
2272 */
2273 if (unlikely(current->flags & PF_KTHREAD)) {
2274 ssp->smk_in = &smack_known_web;
2275 ssp->smk_out = &smack_known_web;
2276 } else {
2277 ssp->smk_in = skp;
2278 ssp->smk_out = skp;
2279 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002280 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002281
2282 sk->sk_security = ssp;
2283
2284 return 0;
2285}
2286
2287/**
2288 * smack_sk_free_security - Free a socket blob
2289 * @sk: the socket
2290 *
2291 * Clears the blob pointer
2292 */
2293static void smack_sk_free_security(struct sock *sk)
2294{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302295#ifdef SMACK_IPV6_PORT_LABELING
2296 struct smk_port_label *spp;
2297
2298 if (sk->sk_family == PF_INET6) {
2299 rcu_read_lock();
2300 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2301 if (spp->smk_sock != sk)
2302 continue;
2303 spp->smk_can_reuse = 1;
2304 break;
2305 }
2306 rcu_read_unlock();
2307 }
2308#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002309 kfree(sk->sk_security);
2310}
2311
2312/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002313* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002314* @sip: the object end
2315*
2316* looks for host based access restrictions
2317*
2318* This version will only be appropriate for really small sets of single label
2319* hosts. The caller is responsible for ensuring that the RCU read lock is
2320* taken before calling this function.
2321*
2322* Returns the label of the far end or NULL if it's not special.
2323*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002324static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002325{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002326 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002327 struct in_addr *siap = &sip->sin_addr;
2328
2329 if (siap->s_addr == 0)
2330 return NULL;
2331
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002332 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2333 /*
2334 * we break after finding the first match because
2335 * the list is sorted from longest to shortest mask
2336 * so we have found the most specific match
2337 */
2338 if (snp->smk_host.s_addr ==
2339 (siap->s_addr & snp->smk_mask.s_addr))
2340 return snp->smk_label;
2341
2342 return NULL;
2343}
2344
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002345/*
2346 * smk_ipv6_localhost - Check for local ipv6 host address
2347 * @sip: the address
2348 *
2349 * Returns boolean true if this is the localhost address
2350 */
2351static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2352{
2353 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2354 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2355
2356 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2357 ntohs(be16p[7]) == 1)
2358 return true;
2359 return false;
2360}
2361
2362/**
2363* smack_ipv6host_label - check host based restrictions
2364* @sip: the object end
2365*
2366* looks for host based access restrictions
2367*
2368* This version will only be appropriate for really small sets of single label
2369* hosts. The caller is responsible for ensuring that the RCU read lock is
2370* taken before calling this function.
2371*
2372* Returns the label of the far end or NULL if it's not special.
2373*/
2374static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2375{
2376 struct smk_net6addr *snp;
2377 struct in6_addr *sap = &sip->sin6_addr;
2378 int i;
2379 int found = 0;
2380
2381 /*
2382 * It's local. Don't look for a host label.
2383 */
2384 if (smk_ipv6_localhost(sip))
2385 return NULL;
2386
2387 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002388 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002389 * If the label is NULL the entry has
2390 * been renounced. Ignore it.
2391 */
2392 if (snp->smk_label == NULL)
2393 continue;
2394 /*
Paul Moore07feee82009-03-27 17:10:54 -04002395 * we break after finding the first match because
2396 * the list is sorted from longest to shortest mask
2397 * so we have found the most specific match
2398 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002399 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002400 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2401 snp->smk_host.s6_addr16[i]) {
2402 found = 0;
2403 break;
2404 }
Etienne Basset43031542009-03-27 17:11:01 -04002405 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002406 if (found)
2407 return snp->smk_label;
2408 }
Paul Moore07feee82009-03-27 17:10:54 -04002409
2410 return NULL;
2411}
2412
2413/**
Casey Schauflera2af0312020-08-11 17:39:42 -07002414 * smack_netlbl_add - Set the secattr on a socket
Casey Schauflere114e472008-02-04 22:29:50 -08002415 * @sk: the socket
2416 *
Casey Schauflera2af0312020-08-11 17:39:42 -07002417 * Attach the outbound smack value (smk_out) to the socket.
Casey Schauflere114e472008-02-04 22:29:50 -08002418 *
2419 * Returns 0 on success or an error code
2420 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002421static int smack_netlbl_add(struct sock *sk)
Casey Schauflere114e472008-02-04 22:29:50 -08002422{
Paul Moore07feee82009-03-27 17:10:54 -04002423 struct socket_smack *ssp = sk->sk_security;
Casey Schauflera2af0312020-08-11 17:39:42 -07002424 struct smack_known *skp = ssp->smk_out;
2425 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002426
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002427 local_bh_disable();
2428 bh_lock_sock_nested(sk);
2429
Casey Schauflera2af0312020-08-11 17:39:42 -07002430 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2431 switch (rc) {
2432 case 0:
2433 ssp->smk_state = SMK_NETLBL_LABELED;
2434 break;
2435 case -EDESTADDRREQ:
2436 ssp->smk_state = SMK_NETLBL_REQSKB;
2437 rc = 0;
2438 break;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002439 }
2440
2441 bh_unlock_sock(sk);
2442 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002443
Casey Schauflere114e472008-02-04 22:29:50 -08002444 return rc;
2445}
2446
2447/**
Casey Schauflera2af0312020-08-11 17:39:42 -07002448 * smack_netlbl_delete - Remove the secattr from a socket
2449 * @sk: the socket
2450 *
2451 * Remove the outbound smack value from a socket
2452 */
2453static void smack_netlbl_delete(struct sock *sk)
2454{
2455 struct socket_smack *ssp = sk->sk_security;
2456
2457 /*
2458 * Take the label off the socket if one is set.
2459 */
2460 if (ssp->smk_state != SMK_NETLBL_LABELED)
2461 return;
2462
2463 local_bh_disable();
2464 bh_lock_sock_nested(sk);
2465 netlbl_sock_delattr(sk);
2466 bh_unlock_sock(sk);
2467 local_bh_enable();
2468 ssp->smk_state = SMK_NETLBL_UNLABELED;
2469}
2470
2471/**
2472 * smk_ipv4_check - Perform IPv4 host access checks
Paul Moore07feee82009-03-27 17:10:54 -04002473 * @sk: the socket
2474 * @sap: the destination address
2475 *
2476 * Set the correct secattr for the given socket based on the destination
2477 * address and perform any outbound access checks needed.
2478 *
2479 * Returns 0 on success or an error code.
2480 *
2481 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002482static int smk_ipv4_check(struct sock *sk, struct sockaddr_in *sap)
Paul Moore07feee82009-03-27 17:10:54 -04002483{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002484 struct smack_known *skp;
Casey Schauflera2af0312020-08-11 17:39:42 -07002485 int rc = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002486 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002487 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002488 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002489
2490 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002491 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002492 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002493#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002494 struct lsm_network_audit net;
2495
Eric Paris48c62af2012-04-02 13:15:44 -04002496 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2497 ad.a.u.net->family = sap->sin_family;
2498 ad.a.u.net->dport = sap->sin_port;
2499 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002500#endif
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002501 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002502 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2503 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Casey Schauflera2af0312020-08-11 17:39:42 -07002504 /*
2505 * Clear the socket netlabel if it's set.
2506 */
2507 if (!rc)
2508 smack_netlbl_delete(sk);
Paul Moore07feee82009-03-27 17:10:54 -04002509 }
2510 rcu_read_unlock();
Paul Moore07feee82009-03-27 17:10:54 -04002511
Casey Schauflera2af0312020-08-11 17:39:42 -07002512 return rc;
Paul Moore07feee82009-03-27 17:10:54 -04002513}
2514
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002515/**
2516 * smk_ipv6_check - check Smack access
2517 * @subject: subject Smack label
2518 * @object: object Smack label
2519 * @address: address
2520 * @act: the action being taken
2521 *
2522 * Check an IPv6 access
2523 */
2524static int smk_ipv6_check(struct smack_known *subject,
2525 struct smack_known *object,
2526 struct sockaddr_in6 *address, int act)
2527{
2528#ifdef CONFIG_AUDIT
2529 struct lsm_network_audit net;
2530#endif
2531 struct smk_audit_info ad;
2532 int rc;
2533
2534#ifdef CONFIG_AUDIT
2535 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2536 ad.a.u.net->family = PF_INET6;
Casey Schaufler05ba7d02022-02-28 15:45:32 -08002537 ad.a.u.net->dport = address->sin6_port;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002538 if (act == SMK_RECEIVING)
2539 ad.a.u.net->v6info.saddr = address->sin6_addr;
2540 else
2541 ad.a.u.net->v6info.daddr = address->sin6_addr;
2542#endif
2543 rc = smk_access(subject, object, MAY_WRITE, &ad);
2544 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2545 return rc;
2546}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002547
2548#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002549/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002550 * smk_ipv6_port_label - Smack port access table management
2551 * @sock: socket
2552 * @address: address
2553 *
2554 * Create or update the port list entry
2555 */
2556static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2557{
2558 struct sock *sk = sock->sk;
2559 struct sockaddr_in6 *addr6;
2560 struct socket_smack *ssp = sock->sk->sk_security;
2561 struct smk_port_label *spp;
2562 unsigned short port = 0;
2563
2564 if (address == NULL) {
2565 /*
2566 * This operation is changing the Smack information
2567 * on the bound socket. Take the changes to the port
2568 * as well.
2569 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302570 rcu_read_lock();
2571 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002572 if (sk != spp->smk_sock)
2573 continue;
2574 spp->smk_in = ssp->smk_in;
2575 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302576 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002577 return;
2578 }
2579 /*
2580 * A NULL address is only used for updating existing
2581 * bound entries. If there isn't one, it's OK.
2582 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302583 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002584 return;
2585 }
2586
2587 addr6 = (struct sockaddr_in6 *)address;
2588 port = ntohs(addr6->sin6_port);
2589 /*
2590 * This is a special case that is safely ignored.
2591 */
2592 if (port == 0)
2593 return;
2594
2595 /*
2596 * Look for an existing port list entry.
2597 * This is an indication that a port is getting reused.
2598 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302599 rcu_read_lock();
2600 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302601 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002602 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302603 if (spp->smk_can_reuse != 1) {
2604 rcu_read_unlock();
2605 return;
2606 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002607 spp->smk_port = port;
2608 spp->smk_sock = sk;
2609 spp->smk_in = ssp->smk_in;
2610 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302611 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302612 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002613 return;
2614 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302615 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002616 /*
2617 * A new port entry is required.
2618 */
2619 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2620 if (spp == NULL)
2621 return;
2622
2623 spp->smk_port = port;
2624 spp->smk_sock = sk;
2625 spp->smk_in = ssp->smk_in;
2626 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302627 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302628 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002629
Vishal Goel3c7ce342016-11-23 10:31:08 +05302630 mutex_lock(&smack_ipv6_lock);
2631 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2632 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002633 return;
2634}
Arnd Bergmann00720f02020-04-08 21:04:31 +02002635#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002636
2637/**
2638 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002639 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002640 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002641 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002642 *
2643 * Create or update the port list entry
2644 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002645static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002646 int act)
2647{
Casey Schauflerc6739442013-05-22 18:42:56 -07002648 struct smk_port_label *spp;
2649 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002650 struct smack_known *skp = NULL;
2651 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002652 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002653
2654 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002655 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002656 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002657 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002658 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002659 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002660 }
2661
2662 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002663 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002664 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002665 if (skp != NULL && object != NULL)
2666 return smk_ipv6_check(skp, object, address, act);
2667 if (skp == NULL)
2668 skp = smack_net_ambient;
2669 if (object == NULL)
2670 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002671
2672 /*
2673 * It's remote, so port lookup does no good.
2674 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002675 if (!smk_ipv6_localhost(address))
2676 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002677
2678 /*
2679 * It's local so the send check has to have passed.
2680 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002681 if (act == SMK_RECEIVING)
2682 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002683
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002684 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302685 rcu_read_lock();
2686 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302687 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002688 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002689 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002690 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002691 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002692 break;
2693 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302694 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002695
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002696 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002697}
2698
2699/**
Casey Schauflere114e472008-02-04 22:29:50 -08002700 * smack_inode_setsecurity - set smack xattrs
2701 * @inode: the object
2702 * @name: attribute name
2703 * @value: attribute value
2704 * @size: size of the attribute
2705 * @flags: unused
2706 *
2707 * Sets the named attribute in the appropriate blob
2708 *
2709 * Returns 0 on success, or an error code
2710 */
2711static int smack_inode_setsecurity(struct inode *inode, const char *name,
2712 const void *value, size_t size, int flags)
2713{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002714 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002715 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002716 struct socket_smack *ssp;
2717 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002718 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002719
Casey Schauflerf7112e62012-05-06 15:22:02 -07002720 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302721 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002722
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002723 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002724 if (IS_ERR(skp))
2725 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002726
2727 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002728 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002729 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002730 return 0;
2731 }
2732 /*
2733 * The rest of the Smack xattrs are only on sockets.
2734 */
2735 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2736 return -EOPNOTSUPP;
2737
2738 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002739 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002740 return -EOPNOTSUPP;
2741
2742 ssp = sock->sk->sk_security;
2743
2744 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002745 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002746 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002747 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002748 if (sock->sk->sk_family == PF_INET) {
Casey Schauflera2af0312020-08-11 17:39:42 -07002749 rc = smack_netlbl_add(sock->sk);
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002750 if (rc != 0)
2751 printk(KERN_WARNING
2752 "Smack: \"%s\" netlbl error %d.\n",
2753 __func__, -rc);
2754 }
Casey Schauflere114e472008-02-04 22:29:50 -08002755 } else
2756 return -EOPNOTSUPP;
2757
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002758#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002759 if (sock->sk->sk_family == PF_INET6)
2760 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002761#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002762
Casey Schauflere114e472008-02-04 22:29:50 -08002763 return 0;
2764}
2765
2766/**
2767 * smack_socket_post_create - finish socket setup
2768 * @sock: the socket
2769 * @family: protocol family
2770 * @type: unused
2771 * @protocol: unused
2772 * @kern: unused
2773 *
2774 * Sets the netlabel information on the socket
2775 *
2776 * Returns 0 on success, and error code otherwise
2777 */
2778static int smack_socket_post_create(struct socket *sock, int family,
2779 int type, int protocol, int kern)
2780{
Marcin Lis74123012015-01-22 15:40:33 +01002781 struct socket_smack *ssp;
2782
2783 if (sock->sk == NULL)
2784 return 0;
2785
2786 /*
2787 * Sockets created by kernel threads receive web label.
2788 */
2789 if (unlikely(current->flags & PF_KTHREAD)) {
2790 ssp = sock->sk->sk_security;
2791 ssp->smk_in = &smack_known_web;
2792 ssp->smk_out = &smack_known_web;
2793 }
2794
2795 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002796 return 0;
2797 /*
2798 * Set the outbound netlbl.
2799 */
Casey Schauflera2af0312020-08-11 17:39:42 -07002800 return smack_netlbl_add(sock->sk);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002801}
2802
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002803/**
2804 * smack_socket_socketpair - create socket pair
2805 * @socka: one socket
2806 * @sockb: another socket
2807 *
2808 * Cross reference the peer labels for SO_PEERSEC
2809 *
luanshia1a07f22019-07-05 10:35:20 +08002810 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002811 */
2812static int smack_socket_socketpair(struct socket *socka,
2813 struct socket *sockb)
2814{
2815 struct socket_smack *asp = socka->sk->sk_security;
2816 struct socket_smack *bsp = sockb->sk->sk_security;
2817
2818 asp->smk_packet = bsp->smk_out;
2819 bsp->smk_packet = asp->smk_out;
2820
2821 return 0;
2822}
2823
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002824#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002825/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002826 * smack_socket_bind - record port binding information.
2827 * @sock: the socket
2828 * @address: the port address
2829 * @addrlen: size of the address
2830 *
2831 * Records the label bound to a port.
2832 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002833 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002834 */
2835static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2836 int addrlen)
2837{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002838 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2839 if (addrlen < SIN6_LEN_RFC2133 ||
2840 address->sa_family != AF_INET6)
2841 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002842 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002843 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002844 return 0;
2845}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002846#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002847
2848/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002849 * smack_socket_connect - connect access check
2850 * @sock: the socket
2851 * @sap: the other end
2852 * @addrlen: size of sap
2853 *
2854 * Verifies that a connection may be possible
2855 *
2856 * Returns 0 on success, and error code otherwise
2857 */
2858static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2859 int addrlen)
2860{
Casey Schauflerc6739442013-05-22 18:42:56 -07002861 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002862
Casey Schauflerc6739442013-05-22 18:42:56 -07002863 if (sock->sk == NULL)
2864 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002865 if (sock->sk->sk_family != PF_INET &&
2866 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2867 return 0;
2868 if (addrlen < offsetofend(struct sockaddr, sa_family))
2869 return 0;
2870 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2871 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002872 struct smack_known *rsp = NULL;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002873
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002874 if (addrlen < SIN6_LEN_RFC2133)
2875 return 0;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002876 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2877 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002878 if (rsp != NULL) {
2879 struct socket_smack *ssp = sock->sk->sk_security;
2880
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002881 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002882 SMK_CONNECTING);
2883 }
Arnd Bergmann00720f02020-04-08 21:04:31 +02002884 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2885 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2886
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002887 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002888 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002889 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2890 return 0;
Casey Schauflera2af0312020-08-11 17:39:42 -07002891 rc = smk_ipv4_check(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002892 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002893}
2894
2895/**
2896 * smack_flags_to_may - convert S_ to MAY_ values
2897 * @flags: the S_ value
2898 *
2899 * Returns the equivalent MAY_ value
2900 */
2901static int smack_flags_to_may(int flags)
2902{
2903 int may = 0;
2904
2905 if (flags & S_IRUGO)
2906 may |= MAY_READ;
2907 if (flags & S_IWUGO)
2908 may |= MAY_WRITE;
2909 if (flags & S_IXUGO)
2910 may |= MAY_EXEC;
2911
2912 return may;
2913}
2914
2915/**
2916 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2917 * @msg: the object
2918 *
2919 * Returns 0
2920 */
2921static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2922{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002923 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002924
Casey Schauflerecd5f822018-11-20 11:55:02 -08002925 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002926 return 0;
2927}
2928
2929/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002930 * smack_of_ipc - the smack pointer for the ipc
2931 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002932 *
2933 * Returns a pointer to the smack value
2934 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002935static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002936{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002937 struct smack_known **blob = smack_ipc(isp);
2938
2939 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002940}
2941
2942/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002943 * smack_ipc_alloc_security - Set the security blob for ipc
2944 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002945 *
2946 * Returns 0
2947 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002948static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002949{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002950 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002951
Casey Schaufler019bcca2018-09-21 17:19:54 -07002952 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002953 return 0;
2954}
2955
2956/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002957 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002958 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002959 * @access : access requested
2960 *
2961 * Returns 0 if current has the requested access, error code otherwise
2962 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002963static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002964{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002965 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002966 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002967 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002968
2969#ifdef CONFIG_AUDIT
2970 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002971 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002972#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002973 rc = smk_curacc(ssp, access, &ad);
2974 rc = smk_bu_current("shm", ssp, access, rc);
2975 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002976}
2977
2978/**
Casey Schauflere114e472008-02-04 22:29:50 -08002979 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002980 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002981 * @shmflg: access requested
2982 *
2983 * Returns 0 if current has the requested access, error code otherwise
2984 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002985static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002986{
Casey Schauflere114e472008-02-04 22:29:50 -08002987 int may;
2988
2989 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002990 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002991}
2992
2993/**
2994 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002995 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002996 * @cmd: what it wants to do
2997 *
2998 * Returns 0 if current has the requested access, error code otherwise
2999 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003000static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003001{
Casey Schauflere114e472008-02-04 22:29:50 -08003002 int may;
3003
3004 switch (cmd) {
3005 case IPC_STAT:
3006 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07003007 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003008 may = MAY_READ;
3009 break;
3010 case IPC_SET:
3011 case SHM_LOCK:
3012 case SHM_UNLOCK:
3013 case IPC_RMID:
3014 may = MAY_READWRITE;
3015 break;
3016 case IPC_INFO:
3017 case SHM_INFO:
3018 /*
3019 * System level information.
3020 */
3021 return 0;
3022 default:
3023 return -EINVAL;
3024 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003025 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003026}
3027
3028/**
3029 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003030 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003031 * @shmaddr: unused
3032 * @shmflg: access requested
3033 *
3034 * Returns 0 if current has the requested access, error code otherwise
3035 */
luanshia1a07f22019-07-05 10:35:20 +08003036static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003037 int shmflg)
3038{
Casey Schauflere114e472008-02-04 22:29:50 -08003039 int may;
3040
3041 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003042 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003043}
3044
3045/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003046 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003047 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003048 * @access : access requested
3049 *
3050 * Returns 0 if current has the requested access, error code otherwise
3051 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003052static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003053{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003054 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003055 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003056 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003057
3058#ifdef CONFIG_AUDIT
3059 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003060 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003061#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003062 rc = smk_curacc(ssp, access, &ad);
3063 rc = smk_bu_current("sem", ssp, access, rc);
3064 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003065}
3066
3067/**
Casey Schauflere114e472008-02-04 22:29:50 -08003068 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003069 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003070 * @semflg: access requested
3071 *
3072 * Returns 0 if current has the requested access, error code otherwise
3073 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003074static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003075{
Casey Schauflere114e472008-02-04 22:29:50 -08003076 int may;
3077
3078 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003079 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003080}
3081
3082/**
3083 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003084 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003085 * @cmd: what it wants to do
3086 *
3087 * Returns 0 if current has the requested access, error code otherwise
3088 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003089static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003090{
Casey Schauflere114e472008-02-04 22:29:50 -08003091 int may;
3092
3093 switch (cmd) {
3094 case GETPID:
3095 case GETNCNT:
3096 case GETZCNT:
3097 case GETVAL:
3098 case GETALL:
3099 case IPC_STAT:
3100 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003101 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003102 may = MAY_READ;
3103 break;
3104 case SETVAL:
3105 case SETALL:
3106 case IPC_RMID:
3107 case IPC_SET:
3108 may = MAY_READWRITE;
3109 break;
3110 case IPC_INFO:
3111 case SEM_INFO:
3112 /*
3113 * System level information
3114 */
3115 return 0;
3116 default:
3117 return -EINVAL;
3118 }
3119
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003120 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003121}
3122
3123/**
3124 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003125 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003126 * @sops: unused
3127 * @nsops: unused
3128 * @alter: unused
3129 *
3130 * Treated as read and write in all cases.
3131 *
3132 * Returns 0 if access is allowed, error code otherwise
3133 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003134static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003135 unsigned nsops, int alter)
3136{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003137 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003138}
3139
3140/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003141 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003142 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003143 * @access : access requested
3144 *
3145 * return 0 if current has access, error otherwise
3146 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003147static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003148{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003149 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003150 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003151 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003152
3153#ifdef CONFIG_AUDIT
3154 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003155 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003156#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003157 rc = smk_curacc(msp, access, &ad);
3158 rc = smk_bu_current("msq", msp, access, rc);
3159 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003160}
3161
3162/**
Casey Schauflere114e472008-02-04 22:29:50 -08003163 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003164 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003165 * @msqflg: access requested
3166 *
3167 * Returns 0 if current has the requested access, error code otherwise
3168 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003169static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003170{
Casey Schauflere114e472008-02-04 22:29:50 -08003171 int may;
3172
3173 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003174 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003175}
3176
3177/**
3178 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003179 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003180 * @cmd: what it wants to do
3181 *
3182 * Returns 0 if current has the requested access, error code otherwise
3183 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003184static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003185{
Casey Schauflere114e472008-02-04 22:29:50 -08003186 int may;
3187
3188 switch (cmd) {
3189 case IPC_STAT:
3190 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003191 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003192 may = MAY_READ;
3193 break;
3194 case IPC_SET:
3195 case IPC_RMID:
3196 may = MAY_READWRITE;
3197 break;
3198 case IPC_INFO:
3199 case MSG_INFO:
3200 /*
3201 * System level information
3202 */
3203 return 0;
3204 default:
3205 return -EINVAL;
3206 }
3207
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003208 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003209}
3210
3211/**
3212 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003213 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003214 * @msg: unused
3215 * @msqflg: access requested
3216 *
3217 * Returns 0 if current has the requested access, error code otherwise
3218 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003219static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003220 int msqflg)
3221{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003222 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003223
Etienne Bassetecfcc532009-04-08 20:40:06 +02003224 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003225 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003226}
3227
3228/**
3229 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003230 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003231 * @msg: unused
3232 * @target: unused
3233 * @type: unused
3234 * @mode: unused
3235 *
3236 * Returns 0 if current has read and write access, error code otherwise
3237 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003238static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003239 struct task_struct *target, long type, int mode)
3240{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003241 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003242}
3243
3244/**
3245 * smack_ipc_permission - Smack access for ipc_permission()
3246 * @ipp: the object permissions
3247 * @flag: access requested
3248 *
3249 * Returns 0 if current has read and write access, error code otherwise
3250 */
3251static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3252{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003253 struct smack_known **blob = smack_ipc(ipp);
3254 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003255 int may = smack_flags_to_may(flag);
3256 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003257 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003258
Etienne Bassetecfcc532009-04-08 20:40:06 +02003259#ifdef CONFIG_AUDIT
3260 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3261 ad.a.u.ipc_id = ipp->id;
3262#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003263 rc = smk_curacc(iskp, may, &ad);
3264 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003265 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003266}
3267
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003268/**
3269 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003270 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003271 * @secid: where result will be saved
3272 */
3273static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3274{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003275 struct smack_known **blob = smack_ipc(ipp);
3276 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003277
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003278 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003279}
3280
Casey Schauflere114e472008-02-04 22:29:50 -08003281/**
3282 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003283 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003284 * @inode: the object
3285 *
3286 * Set the inode's security blob if it hasn't been done already.
3287 */
3288static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3289{
3290 struct super_block *sbp;
3291 struct superblock_smack *sbsp;
3292 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003293 struct smack_known *skp;
3294 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003295 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003296 char trattr[TRANS_TRUE_SIZE];
3297 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003298 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003299 struct dentry *dp;
3300
3301 if (inode == NULL)
3302 return;
3303
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003304 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003305
Casey Schauflere114e472008-02-04 22:29:50 -08003306 /*
3307 * If the inode is already instantiated
3308 * take the quick way out
3309 */
3310 if (isp->smk_flags & SMK_INODE_INSTANT)
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003311 return;
Casey Schauflere114e472008-02-04 22:29:50 -08003312
3313 sbp = inode->i_sb;
Casey Schaufler1aea7802021-04-22 17:41:15 +02003314 sbsp = smack_superblock(sbp);
Casey Schauflere114e472008-02-04 22:29:50 -08003315 /*
3316 * We're going to use the superblock default label
3317 * if there's no label on the file.
3318 */
3319 final = sbsp->smk_default;
3320
3321 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003322 * If this is the root inode the superblock
3323 * may be in the process of initialization.
3324 * If that is the case use the root value out
3325 * of the superblock.
3326 */
3327 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003328 switch (sbp->s_magic) {
3329 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003330 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003331 /*
3332 * The cgroup filesystem is never mounted,
3333 * so there's no opportunity to set the mount
3334 * options.
3335 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003336 sbsp->smk_root = &smack_known_star;
3337 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003338 isp->smk_inode = sbsp->smk_root;
3339 break;
3340 case TMPFS_MAGIC:
3341 /*
3342 * What about shmem/tmpfs anonymous files with dentry
3343 * obtained from d_alloc_pseudo()?
3344 */
3345 isp->smk_inode = smk_of_current();
3346 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003347 case PIPEFS_MAGIC:
3348 isp->smk_inode = smk_of_current();
3349 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003350 case SOCKFS_MAGIC:
3351 /*
3352 * Socket access is controlled by the socket
3353 * structures associated with the task involved.
3354 */
3355 isp->smk_inode = &smack_known_star;
3356 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003357 default:
3358 isp->smk_inode = sbsp->smk_root;
3359 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003360 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003361 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003362 return;
Casey Schauflere97dcb02008-06-02 10:04:32 -07003363 }
3364
3365 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003366 * This is pretty hackish.
3367 * Casey says that we shouldn't have to do
3368 * file system specific code, but it does help
3369 * with keeping it simple.
3370 */
3371 switch (sbp->s_magic) {
3372 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003373 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003374 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003375 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003376 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003377 * that the smack file system doesn't do
3378 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003379 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003380 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003381 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003382 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003383 break;
3384 case DEVPTS_SUPER_MAGIC:
3385 /*
3386 * devpts seems content with the label of the task.
3387 * Programs that change smack have to treat the
3388 * pty with respect.
3389 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003390 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003391 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003392 case PROC_SUPER_MAGIC:
3393 /*
3394 * Casey says procfs appears not to care.
3395 * The superblock default suffices.
3396 */
3397 break;
3398 case TMPFS_MAGIC:
3399 /*
3400 * Device labels should come from the filesystem,
3401 * but watch out, because they're volitile,
3402 * getting recreated on every reboot.
3403 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003404 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003405 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003406 * If a smack value has been set we want to use it,
3407 * but since tmpfs isn't giving us the opportunity
3408 * to set mount options simulate setting the
3409 * superblock default.
3410 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003411 fallthrough;
Casey Schauflere114e472008-02-04 22:29:50 -08003412 default:
3413 /*
3414 * This isn't an understood special case.
3415 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003416 */
3417
3418 /*
3419 * UNIX domain sockets use lower level socket data.
3420 */
3421 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003422 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003423 break;
3424 }
3425 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003426 * No xattr support means, alas, no SMACK label.
3427 * Use the aforeapplied default.
3428 * It would be curious if the label of the task
3429 * does not match that assigned.
3430 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003431 if (!(inode->i_opflags & IOP_XATTR))
3432 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003433 /*
3434 * Get the dentry for xattr.
3435 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003436 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003437 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003438 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003439 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003440
3441 /*
3442 * Transmuting directory
3443 */
3444 if (S_ISDIR(inode->i_mode)) {
3445 /*
3446 * If this is a new directory and the label was
3447 * transmuted when the inode was initialized
3448 * set the transmute attribute on the directory
3449 * and mark the inode.
3450 *
3451 * If there is a transmute attribute on the
3452 * directory mark the inode.
3453 */
3454 if (isp->smk_flags & SMK_INODE_CHANGED) {
3455 isp->smk_flags &= ~SMK_INODE_CHANGED;
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01003456 rc = __vfs_setxattr(&init_user_ns, dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003457 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003458 TRANS_TRUE, TRANS_TRUE_SIZE,
3459 0);
3460 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003461 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003462 XATTR_NAME_SMACKTRANSMUTE, trattr,
3463 TRANS_TRUE_SIZE);
3464 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3465 TRANS_TRUE_SIZE) != 0)
3466 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003467 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003468 if (rc >= 0)
3469 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003470 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003471 /*
3472 * Don't let the exec or mmap label be "*" or "@".
3473 */
3474 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3475 if (IS_ERR(skp) || skp == &smack_known_star ||
3476 skp == &smack_known_web)
3477 skp = NULL;
3478 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003479
Casey Schaufler19760ad2013-12-16 16:27:26 -08003480 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003481 if (IS_ERR(skp) || skp == &smack_known_star ||
3482 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003483 skp = NULL;
3484 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003485
Casey Schauflere114e472008-02-04 22:29:50 -08003486 dput(dp);
3487 break;
3488 }
3489
3490 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003491 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003492 else
3493 isp->smk_inode = final;
3494
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003495 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003496
Casey Schauflere114e472008-02-04 22:29:50 -08003497 return;
3498}
3499
3500/**
3501 * smack_getprocattr - Smack process attribute access
3502 * @p: the object task
3503 * @name: the name of the attribute in /proc/.../attr
3504 * @value: where to put the result
3505 *
3506 * Places a copy of the task Smack into value
3507 *
3508 * Returns the length of the smack label or an error code
3509 */
3510static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3511{
Paul Moorea3727a82021-09-23 09:50:11 -04003512 struct smack_known *skp = smk_of_task_struct_obj(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003513 char *cp;
3514 int slen;
3515
3516 if (strcmp(name, "current") != 0)
3517 return -EINVAL;
3518
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003519 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003520 if (cp == NULL)
3521 return -ENOMEM;
3522
3523 slen = strlen(cp);
3524 *value = cp;
3525 return slen;
3526}
3527
3528/**
3529 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003530 * @name: the name of the attribute in /proc/.../attr
3531 * @value: the value to set
3532 * @size: the size of the value
3533 *
3534 * Sets the Smack value of the task. Only setting self
3535 * is permitted and only with privilege
3536 *
3537 * Returns the length of the smack label or an error code
3538 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003539static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003540{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003541 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003542 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003543 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003544 struct smack_known_list_elem *sklep;
3545 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003546
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003547 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003548 return -EPERM;
3549
Casey Schauflerf7112e62012-05-06 15:22:02 -07003550 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003551 return -EINVAL;
3552
3553 if (strcmp(name, "current") != 0)
3554 return -EINVAL;
3555
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003556 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003557 if (IS_ERR(skp))
3558 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003559
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003560 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303561 * No process is ever allowed the web ("@") label
3562 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003563 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303564 if (skp == &smack_known_web || skp == &smack_known_star)
3565 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003566
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003567 if (!smack_privileged(CAP_MAC_ADMIN)) {
3568 rc = -EPERM;
3569 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3570 if (sklep->smk_label == skp) {
3571 rc = 0;
3572 break;
3573 }
3574 if (rc)
3575 return rc;
3576 }
3577
David Howellsd84f4f92008-11-14 10:39:23 +11003578 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003579 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003580 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003581
Casey Schauflerb17103a2018-11-09 16:12:56 -08003582 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003583 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003584 /*
3585 * process can change its label only once
3586 */
3587 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003588
David Howellsd84f4f92008-11-14 10:39:23 +11003589 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003590 return size;
3591}
3592
3593/**
3594 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003595 * @sock: one sock
3596 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003597 * @newsk: unused
3598 *
3599 * Return 0 if a subject with the smack of sock could access
3600 * an object with the smack of other, otherwise an error code
3601 */
David S. Miller3610cda2011-01-05 15:38:53 -08003602static int smack_unix_stream_connect(struct sock *sock,
3603 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003604{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003605 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003606 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003607 struct socket_smack *ssp = sock->sk_security;
3608 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003609 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003610 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003611 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003612#ifdef CONFIG_AUDIT
3613 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003614#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003615
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003616 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3617 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003618 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003619#ifdef CONFIG_AUDIT
3620 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3621 smk_ad_setfield_u_net_sk(&ad, other);
3622#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003623 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3624 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003625 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003626 okp = osp->smk_out;
3627 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003628 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003629 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003630 MAY_WRITE, rc);
3631 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003632 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003633
Casey Schaufler975d5e52011-09-26 14:43:39 -07003634 /*
3635 * Cross reference the peer labels for SO_PEERSEC.
3636 */
3637 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003638 nsp->smk_packet = ssp->smk_out;
3639 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003640 }
3641
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003642 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003643}
3644
3645/**
3646 * smack_unix_may_send - Smack access on UDS
3647 * @sock: one socket
3648 * @other: the other socket
3649 *
3650 * Return 0 if a subject with the smack of sock could access
3651 * an object with the smack of other, otherwise an error code
3652 */
3653static int smack_unix_may_send(struct socket *sock, struct socket *other)
3654{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003655 struct socket_smack *ssp = sock->sk->sk_security;
3656 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003657 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003658 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003659
Kees Cook923e9a12012-04-10 13:26:44 -07003660#ifdef CONFIG_AUDIT
3661 struct lsm_network_audit net;
3662
Eric Paris48c62af2012-04-02 13:15:44 -04003663 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003664 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003665#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003666
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003667 if (smack_privileged(CAP_MAC_OVERRIDE))
3668 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003669
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003670 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3671 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003672 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003673}
3674
3675/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003676 * smack_socket_sendmsg - Smack check based on destination host
3677 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003678 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003679 * @size: the size of the message
3680 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003681 * Return 0 if the current subject can write to the destination host.
3682 * For IPv4 this is only a question if the destination is a single label host.
3683 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003684 */
3685static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3686 int size)
3687{
3688 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003689#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003690 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003691#endif
3692#ifdef SMACK_IPV6_SECMARK_LABELING
3693 struct socket_smack *ssp = sock->sk->sk_security;
3694 struct smack_known *rsp;
3695#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003696 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003697
3698 /*
3699 * Perfectly reasonable for this to be NULL
3700 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003701 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003702 return 0;
3703
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003704 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003705 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003706 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3707 sip->sin_family != AF_INET)
3708 return -EINVAL;
Casey Schauflera2af0312020-08-11 17:39:42 -07003709 rc = smk_ipv4_check(sock->sk, sip);
Casey Schauflerc6739442013-05-22 18:42:56 -07003710 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003711#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003712 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003713 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3714 sap->sin6_family != AF_INET6)
3715 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003716#ifdef SMACK_IPV6_SECMARK_LABELING
3717 rsp = smack_ipv6host_label(sap);
3718 if (rsp != NULL)
3719 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3720 SMK_CONNECTING);
3721#endif
3722#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003723 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003724#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003725#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003726 break;
3727 }
3728 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003729}
3730
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003731/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003732 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003733 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003734 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003735 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003736 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003737 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003738static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3739 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003740{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003741 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003742 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003743 int acat;
3744 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003745
Casey Schaufler322dd632020-08-11 17:39:43 -07003746 /*
3747 * Netlabel found it in the cache.
3748 */
3749 if ((sap->flags & NETLBL_SECATTR_CACHE) != 0)
3750 return (struct smack_known *)sap->cache->data;
3751
3752 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3753 /*
3754 * Looks like a fallback, which gives us a secid.
3755 */
3756 return smack_from_secid(sap->attr.secid);
3757
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003758 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003759 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003760 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003761 * If there are flags but no level netlabel isn't
3762 * behaving the way we expect it to.
3763 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003764 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003765 * Without guidance regarding the smack value
3766 * for the packet fall back on the network
3767 * ambient value.
3768 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003769 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303770 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003771 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003772 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003773 /*
3774 * Compare the catsets. Use the netlbl APIs.
3775 */
3776 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3777 if ((skp->smk_netlabel.flags &
3778 NETLBL_SECATTR_MLS_CAT) == 0)
3779 found = 1;
3780 break;
3781 }
3782 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003783 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3784 acat + 1);
3785 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003786 skp->smk_netlabel.attr.mls.cat,
3787 kcat + 1);
3788 if (acat < 0 || kcat < 0)
3789 break;
3790 }
3791 if (acat == kcat) {
3792 found = 1;
3793 break;
3794 }
Casey Schauflere114e472008-02-04 22:29:50 -08003795 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003796 rcu_read_unlock();
3797
3798 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003799 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003800
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003801 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003802 return &smack_known_web;
3803 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003804 }
Casey Schauflere114e472008-02-04 22:29:50 -08003805 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003806 * Without guidance regarding the smack value
3807 * for the packet fall back on the network
3808 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003809 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003810 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003811}
3812
Casey Schaufler69f287a2014-12-12 17:08:40 -08003813#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003814static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003815{
Casey Schauflerc6739442013-05-22 18:42:56 -07003816 u8 nexthdr;
3817 int offset;
3818 int proto = -EINVAL;
3819 struct ipv6hdr _ipv6h;
3820 struct ipv6hdr *ip6;
3821 __be16 frag_off;
3822 struct tcphdr _tcph, *th;
3823 struct udphdr _udph, *uh;
3824 struct dccp_hdr _dccph, *dh;
3825
3826 sip->sin6_port = 0;
3827
3828 offset = skb_network_offset(skb);
3829 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3830 if (ip6 == NULL)
3831 return -EINVAL;
3832 sip->sin6_addr = ip6->saddr;
3833
3834 nexthdr = ip6->nexthdr;
3835 offset += sizeof(_ipv6h);
3836 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3837 if (offset < 0)
3838 return -EINVAL;
3839
3840 proto = nexthdr;
3841 switch (proto) {
3842 case IPPROTO_TCP:
3843 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3844 if (th != NULL)
3845 sip->sin6_port = th->source;
3846 break;
3847 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003848 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003849 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3850 if (uh != NULL)
3851 sip->sin6_port = uh->source;
3852 break;
3853 case IPPROTO_DCCP:
3854 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3855 if (dh != NULL)
3856 sip->sin6_port = dh->dccph_sport;
3857 break;
3858 }
3859 return proto;
3860}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003861#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003862
Casey Schauflere114e472008-02-04 22:29:50 -08003863/**
Casey Schaufler36be8122020-08-11 17:39:41 -07003864 * smack_from_skb - Smack data from the secmark in an skb
3865 * @skb: packet
3866 *
3867 * Returns smack_known of the secmark or NULL if that won't work.
3868 */
Casey Schauflerbf0afe62020-09-22 14:59:31 -07003869#ifdef CONFIG_NETWORK_SECMARK
Casey Schaufler36be8122020-08-11 17:39:41 -07003870static struct smack_known *smack_from_skb(struct sk_buff *skb)
3871{
3872 if (skb == NULL || skb->secmark == 0)
3873 return NULL;
3874
3875 return smack_from_secid(skb->secmark);
3876}
Casey Schauflerbf0afe62020-09-22 14:59:31 -07003877#else
3878static inline struct smack_known *smack_from_skb(struct sk_buff *skb)
3879{
3880 return NULL;
3881}
3882#endif
Casey Schaufler36be8122020-08-11 17:39:41 -07003883
3884/**
Casey Schauflera2af0312020-08-11 17:39:42 -07003885 * smack_from_netlbl - Smack data from the IP options in an skb
3886 * @sk: socket data came in on
3887 * @family: address family
3888 * @skb: packet
3889 *
Casey Schaufler322dd632020-08-11 17:39:43 -07003890 * Find the Smack label in the IP options. If it hasn't been
3891 * added to the netlabel cache, add it here.
3892 *
Casey Schauflera2af0312020-08-11 17:39:42 -07003893 * Returns smack_known of the IP options or NULL if that won't work.
3894 */
Florian Westphal41dd9592020-11-30 16:36:29 +01003895static struct smack_known *smack_from_netlbl(const struct sock *sk, u16 family,
Casey Schauflera2af0312020-08-11 17:39:42 -07003896 struct sk_buff *skb)
3897{
3898 struct netlbl_lsm_secattr secattr;
3899 struct socket_smack *ssp = NULL;
3900 struct smack_known *skp = NULL;
3901
3902 netlbl_secattr_init(&secattr);
3903
3904 if (sk)
3905 ssp = sk->sk_security;
Casey Schaufler322dd632020-08-11 17:39:43 -07003906
3907 if (netlbl_skbuff_getattr(skb, family, &secattr) == 0) {
Casey Schauflera2af0312020-08-11 17:39:42 -07003908 skp = smack_from_secattr(&secattr, ssp);
Casey Schaufler322dd632020-08-11 17:39:43 -07003909 if (secattr.flags & NETLBL_SECATTR_CACHEABLE)
Alex Shi9b0072e2020-11-08 14:45:42 +08003910 netlbl_cache_add(skb, family, &skp->smk_netlabel);
Casey Schaufler322dd632020-08-11 17:39:43 -07003911 }
Casey Schauflera2af0312020-08-11 17:39:42 -07003912
3913 netlbl_secattr_destroy(&secattr);
3914
3915 return skp;
3916}
3917
3918/**
Casey Schauflere114e472008-02-04 22:29:50 -08003919 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3920 * @sk: socket
3921 * @skb: packet
3922 *
3923 * Returns 0 if the packet should be delivered, an error code otherwise
3924 */
3925static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3926{
Casey Schauflere114e472008-02-04 22:29:50 -08003927 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003928 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003929 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003930 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003931 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003932#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003933 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003934#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003935#if IS_ENABLED(CONFIG_IPV6)
3936 struct sockaddr_in6 sadd;
3937 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003938
3939 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3940 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003941#endif /* CONFIG_IPV6 */
3942
Piotr Sawicki129a9982018-07-19 11:42:58 +02003943 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003944 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003945 /*
3946 * If there is a secmark use it rather than the CIPSO label.
3947 * If there is no secmark fall back to CIPSO.
3948 * The secmark is assumed to reflect policy better.
3949 */
Casey Schaufler36be8122020-08-11 17:39:41 -07003950 skp = smack_from_skb(skb);
Casey Schauflera2af0312020-08-11 17:39:42 -07003951 if (skp == NULL) {
3952 skp = smack_from_netlbl(sk, family, skb);
3953 if (skp == NULL)
3954 skp = smack_net_ambient;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003955 }
Casey Schauflere114e472008-02-04 22:29:50 -08003956
Etienne Bassetecfcc532009-04-08 20:40:06 +02003957#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003958 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003959 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003960 ad.a.u.net->netif = skb->skb_iif;
3961 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003962#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003963 /*
3964 * Receiving a packet requires that the other end
3965 * be able to write here. Read access is not required.
3966 * This is the simplist possible security model
3967 * for networking.
3968 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003969 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3970 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003971 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003972 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003973 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003974 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003975#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003976 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003977 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003978 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3979 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003980 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003981#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler36be8122020-08-11 17:39:41 -07003982 skp = smack_from_skb(skb);
3983 if (skp == NULL) {
3984 if (smk_ipv6_localhost(&sadd))
3985 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003986 skp = smack_ipv6host_label(&sadd);
Casey Schaufler36be8122020-08-11 17:39:41 -07003987 if (skp == NULL)
3988 skp = smack_net_ambient;
3989 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003990#ifdef CONFIG_AUDIT
3991 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003992 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003993 ad.a.u.net->netif = skb->skb_iif;
3994 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3995#endif /* CONFIG_AUDIT */
3996 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3997 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3998 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003999#endif /* SMACK_IPV6_SECMARK_LABELING */
4000#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004001 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004002#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02004003 if (rc != 0)
4004 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4005 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07004006 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004007#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07004008 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004009
Paul Moorea8134292008-10-10 10:16:31 -04004010 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004011}
4012
4013/**
4014 * smack_socket_getpeersec_stream - pull in packet label
4015 * @sock: the socket
4016 * @optval: user's destination
4017 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004018 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004019 *
4020 * returns zero on success, an error code otherwise
4021 */
4022static int smack_socket_getpeersec_stream(struct socket *sock,
4023 char __user *optval,
4024 int __user *optlen, unsigned len)
4025{
4026 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004027 char *rcp = "";
4028 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004029 int rc = 0;
4030
4031 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004032 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004033 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004034 slen = strlen(rcp) + 1;
4035 }
Casey Schauflere114e472008-02-04 22:29:50 -08004036
4037 if (slen > len)
4038 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004039 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004040 rc = -EFAULT;
4041
4042 if (put_user(slen, optlen) != 0)
4043 rc = -EFAULT;
4044
4045 return rc;
4046}
4047
4048
4049/**
4050 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004051 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004052 * @skb: packet data
4053 * @secid: pointer to where to put the secid of the packet
4054 *
4055 * Sets the netlabel socket state on sk from parent
4056 */
4057static int smack_socket_getpeersec_dgram(struct socket *sock,
4058 struct sk_buff *skb, u32 *secid)
4059
4060{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004061 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004062 struct smack_known *skp;
Casey Schauflera2af0312020-08-11 17:39:42 -07004063 struct sock *sk = NULL;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004064 int family = PF_UNSPEC;
4065 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004066
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004067 if (skb != NULL) {
4068 if (skb->protocol == htons(ETH_P_IP))
4069 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004070#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004071 else if (skb->protocol == htons(ETH_P_IPV6))
4072 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004073#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004074 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004075 if (family == PF_UNSPEC && sock != NULL)
4076 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004077
Casey Schaufler69f287a2014-12-12 17:08:40 -08004078 switch (family) {
4079 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004080 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004081 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004082 break;
4083 case PF_INET:
Casey Schaufler36be8122020-08-11 17:39:41 -07004084 skp = smack_from_skb(skb);
4085 if (skp) {
4086 s = skp->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004087 break;
Casey Schaufler36be8122020-08-11 17:39:41 -07004088 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004089 /*
4090 * Translate what netlabel gave us.
4091 */
Casey Schauflera2af0312020-08-11 17:39:42 -07004092 if (sock != NULL)
4093 sk = sock->sk;
4094 skp = smack_from_netlbl(sk, family, skb);
4095 if (skp != NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004096 s = skp->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004097 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004098 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004099#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler36be8122020-08-11 17:39:41 -07004100 skp = smack_from_skb(skb);
4101 if (skp)
4102 s = skp->smk_secid;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004103#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004104 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004105 }
4106 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004107 if (s == 0)
4108 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004109 return 0;
4110}
4111
4112/**
Paul Moore07feee82009-03-27 17:10:54 -04004113 * smack_sock_graft - Initialize a newly created socket with an existing sock
4114 * @sk: child sock
4115 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004116 *
Paul Moore07feee82009-03-27 17:10:54 -04004117 * Set the smk_{in,out} state of an existing sock based on the process that
4118 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004119 */
4120static void smack_sock_graft(struct sock *sk, struct socket *parent)
4121{
4122 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004123 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004124
Paul Moore07feee82009-03-27 17:10:54 -04004125 if (sk == NULL ||
4126 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004127 return;
4128
4129 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004130 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004131 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004132 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004133}
4134
4135/**
4136 * smack_inet_conn_request - Smack access check on connect
4137 * @sk: socket involved
4138 * @skb: packet
4139 * @req: unused
4140 *
4141 * Returns 0 if a task with the packet label could write to
4142 * the socket, otherwise an error code
4143 */
Florian Westphal41dd9592020-11-30 16:36:29 +01004144static int smack_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
Casey Schauflere114e472008-02-04 22:29:50 -08004145 struct request_sock *req)
4146{
Paul Moore07feee82009-03-27 17:10:54 -04004147 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004148 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004149 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004150 struct sockaddr_in addr;
4151 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004152 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004153 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004154 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004155#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004156 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004157#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004158
Casey Schaufler69f287a2014-12-12 17:08:40 -08004159#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004160 if (family == PF_INET6) {
4161 /*
4162 * Handle mapped IPv4 packets arriving
4163 * via IPv6 sockets. Don't set up netlabel
4164 * processing on IPv6.
4165 */
4166 if (skb->protocol == htons(ETH_P_IP))
4167 family = PF_INET;
4168 else
4169 return 0;
4170 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004171#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004172
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004173 /*
4174 * If there is a secmark use it rather than the CIPSO label.
4175 * If there is no secmark fall back to CIPSO.
4176 * The secmark is assumed to reflect policy better.
4177 */
Casey Schaufler36be8122020-08-11 17:39:41 -07004178 skp = smack_from_skb(skb);
Casey Schauflera2af0312020-08-11 17:39:42 -07004179 if (skp == NULL) {
4180 skp = smack_from_netlbl(sk, family, skb);
4181 if (skp == NULL)
4182 skp = &smack_known_huh;
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004183 }
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004184
Etienne Bassetecfcc532009-04-08 20:40:06 +02004185#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004186 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4187 ad.a.u.net->family = family;
4188 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004189 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4190#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004191 /*
Paul Moore07feee82009-03-27 17:10:54 -04004192 * Receiving a packet requires that the other end be able to write
4193 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004194 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004195 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4196 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004197 if (rc != 0)
4198 return rc;
4199
4200 /*
4201 * Save the peer's label in the request_sock so we can later setup
4202 * smk_packet in the child socket so that SO_PEERCRED can report it.
4203 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004204 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004205
4206 /*
4207 * We need to decide if we want to label the incoming connection here
4208 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004209 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004210 */
4211 hdr = ip_hdr(skb);
4212 addr.sin_addr.s_addr = hdr->saddr;
4213 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004214 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004215 rcu_read_unlock();
4216
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004217 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004218 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004219 else
Paul Moore07feee82009-03-27 17:10:54 -04004220 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004221
4222 return rc;
4223}
4224
Paul Moore07feee82009-03-27 17:10:54 -04004225/**
4226 * smack_inet_csk_clone - Copy the connection information to the new socket
4227 * @sk: the new socket
4228 * @req: the connection's request_sock
4229 *
4230 * Transfer the connection's peer label to the newly created socket.
4231 */
4232static void smack_inet_csk_clone(struct sock *sk,
4233 const struct request_sock *req)
4234{
4235 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004236 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004237
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004238 if (req->peer_secid != 0) {
4239 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004240 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004241 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004242 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004243}
4244
Casey Schauflere114e472008-02-04 22:29:50 -08004245/*
4246 * Key management security hooks
4247 *
4248 * Casey has not tested key support very heavily.
4249 * The permission check is most likely too restrictive.
4250 * If you care about keys please have a look.
4251 */
4252#ifdef CONFIG_KEYS
4253
4254/**
4255 * smack_key_alloc - Set the key security blob
4256 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004257 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004258 * @flags: unused
4259 *
4260 * No allocation required
4261 *
4262 * Returns 0
4263 */
David Howellsd84f4f92008-11-14 10:39:23 +11004264static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004265 unsigned long flags)
4266{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004267 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004268
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004269 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004270 return 0;
4271}
4272
4273/**
4274 * smack_key_free - Clear the key security blob
4275 * @key: the object
4276 *
4277 * Clear the blob pointer
4278 */
4279static void smack_key_free(struct key *key)
4280{
4281 key->security = NULL;
4282}
4283
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004284/**
Casey Schauflere114e472008-02-04 22:29:50 -08004285 * smack_key_permission - Smack access on a key
4286 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004287 * @cred: the credentials to use
David Howells8c0637e2020-05-12 15:16:29 +01004288 * @need_perm: requested key permission
Casey Schauflere114e472008-02-04 22:29:50 -08004289 *
4290 * Return 0 if the task has read and write to the object,
4291 * an error code otherwise
4292 */
4293static int smack_key_permission(key_ref_t key_ref,
David Howells8c0637e2020-05-12 15:16:29 +01004294 const struct cred *cred,
4295 enum key_need_perm need_perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004296{
4297 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004298 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004299 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004300 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004301 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004302
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004303 /*
4304 * Validate requested permissions
4305 */
David Howells8c0637e2020-05-12 15:16:29 +01004306 switch (need_perm) {
4307 case KEY_NEED_READ:
4308 case KEY_NEED_SEARCH:
4309 case KEY_NEED_VIEW:
4310 request |= MAY_READ;
4311 break;
4312 case KEY_NEED_WRITE:
4313 case KEY_NEED_LINK:
4314 case KEY_NEED_SETATTR:
4315 request |= MAY_WRITE;
4316 break;
4317 case KEY_NEED_UNSPECIFIED:
4318 case KEY_NEED_UNLINK:
4319 case KEY_SYSADMIN_OVERRIDE:
4320 case KEY_AUTHTOKEN_OVERRIDE:
4321 case KEY_DEFER_PERM_CHECK:
4322 return 0;
4323 default:
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004324 return -EINVAL;
David Howells8c0637e2020-05-12 15:16:29 +01004325 }
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004326
Casey Schauflere114e472008-02-04 22:29:50 -08004327 keyp = key_ref_to_ptr(key_ref);
4328 if (keyp == NULL)
4329 return -EINVAL;
4330 /*
4331 * If the key hasn't been initialized give it access so that
4332 * it may do so.
4333 */
4334 if (keyp->security == NULL)
4335 return 0;
4336 /*
4337 * This should not occur
4338 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004339 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004340 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004341
David Howellsa8478a62020-01-14 17:07:13 +00004342 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004343 return 0;
4344
Etienne Bassetecfcc532009-04-08 20:40:06 +02004345#ifdef CONFIG_AUDIT
4346 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4347 ad.a.u.key_struct.key = keyp->serial;
4348 ad.a.u.key_struct.key_desc = keyp->description;
4349#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07004350 rc = smk_access(tkp, keyp->security, request, &ad);
4351 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4352 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004353}
José Bollo7fc5f362015-02-17 15:41:22 +01004354
4355/*
4356 * smack_key_getsecurity - Smack label tagging the key
4357 * @key points to the key to be queried
4358 * @_buffer points to a pointer that should be set to point to the
4359 * resulting string (if no label or an error occurs).
4360 * Return the length of the string (including terminating NUL) or -ve if
4361 * an error.
4362 * May also return 0 (and a NULL buffer pointer) if there is no label.
4363 */
4364static int smack_key_getsecurity(struct key *key, char **_buffer)
4365{
4366 struct smack_known *skp = key->security;
4367 size_t length;
4368 char *copy;
4369
4370 if (key->security == NULL) {
4371 *_buffer = NULL;
4372 return 0;
4373 }
4374
4375 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4376 if (copy == NULL)
4377 return -ENOMEM;
4378 length = strlen(copy) + 1;
4379
4380 *_buffer = copy;
4381 return length;
4382}
4383
David Howellsa8478a62020-01-14 17:07:13 +00004384
4385#ifdef CONFIG_KEY_NOTIFICATIONS
4386/**
4387 * smack_watch_key - Smack access to watch a key for notifications.
4388 * @key: The key to be watched
4389 *
4390 * Return 0 if the @watch->cred has permission to read from the key object and
4391 * an error otherwise.
4392 */
4393static int smack_watch_key(struct key *key)
4394{
4395 struct smk_audit_info ad;
4396 struct smack_known *tkp = smk_of_current();
4397 int rc;
4398
4399 if (key == NULL)
4400 return -EINVAL;
4401 /*
4402 * If the key hasn't been initialized give it access so that
4403 * it may do so.
4404 */
4405 if (key->security == NULL)
4406 return 0;
4407 /*
4408 * This should not occur
4409 */
4410 if (tkp == NULL)
4411 return -EACCES;
4412
4413 if (smack_privileged_cred(CAP_MAC_OVERRIDE, current_cred()))
4414 return 0;
4415
4416#ifdef CONFIG_AUDIT
4417 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4418 ad.a.u.key_struct.key = key->serial;
4419 ad.a.u.key_struct.key_desc = key->description;
4420#endif
4421 rc = smk_access(tkp, key->security, MAY_READ, &ad);
4422 rc = smk_bu_note("key watch", tkp, key->security, MAY_READ, rc);
4423 return rc;
4424}
4425#endif /* CONFIG_KEY_NOTIFICATIONS */
Casey Schauflere114e472008-02-04 22:29:50 -08004426#endif /* CONFIG_KEYS */
4427
David Howellsa8478a62020-01-14 17:07:13 +00004428#ifdef CONFIG_WATCH_QUEUE
4429/**
4430 * smack_post_notification - Smack access to post a notification to a queue
4431 * @w_cred: The credentials of the watcher.
4432 * @cred: The credentials of the event source (may be NULL).
4433 * @n: The notification message to be posted.
4434 */
4435static int smack_post_notification(const struct cred *w_cred,
4436 const struct cred *cred,
4437 struct watch_notification *n)
4438{
4439 struct smk_audit_info ad;
4440 struct smack_known *subj, *obj;
4441 int rc;
4442
4443 /* Always let maintenance notifications through. */
4444 if (n->type == WATCH_TYPE_META)
4445 return 0;
4446
4447 if (!cred)
4448 return 0;
4449 subj = smk_of_task(smack_cred(cred));
4450 obj = smk_of_task(smack_cred(w_cred));
4451
4452 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_NOTIFICATION);
4453 rc = smk_access(subj, obj, MAY_WRITE, &ad);
4454 rc = smk_bu_note("notification", subj, obj, MAY_WRITE, rc);
4455 return rc;
4456}
4457#endif /* CONFIG_WATCH_QUEUE */
4458
Casey Schauflere114e472008-02-04 22:29:50 -08004459/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004460 * Smack Audit hooks
4461 *
4462 * Audit requires a unique representation of each Smack specific
4463 * rule. This unique representation is used to distinguish the
4464 * object to be audited from remaining kernel objects and also
4465 * works as a glue between the audit hooks.
4466 *
4467 * Since repository entries are added but never deleted, we'll use
4468 * the smack_known label address related to the given audit rule as
4469 * the needed unique representation. This also better fits the smack
4470 * model where nearly everything is a label.
4471 */
4472#ifdef CONFIG_AUDIT
4473
4474/**
4475 * smack_audit_rule_init - Initialize a smack audit rule
4476 * @field: audit rule fields given from user-space (audit.h)
4477 * @op: required testing operator (=, !=, >, <, ...)
4478 * @rulestr: smack label to be audited
4479 * @vrule: pointer to save our own audit rule representation
4480 *
4481 * Prepare to audit cases where (@field @op @rulestr) is true.
4482 * The label to be audited is created if necessay.
4483 */
4484static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4485{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004486 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004487 char **rule = (char **)vrule;
4488 *rule = NULL;
4489
4490 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4491 return -EINVAL;
4492
Al Viro5af75d82008-12-16 05:59:26 -05004493 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004494 return -EINVAL;
4495
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004496 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004497 if (IS_ERR(skp))
4498 return PTR_ERR(skp);
4499
4500 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004501
4502 return 0;
4503}
4504
4505/**
4506 * smack_audit_rule_known - Distinguish Smack audit rules
4507 * @krule: rule of interest, in Audit kernel representation format
4508 *
4509 * This is used to filter Smack rules from remaining Audit ones.
4510 * If it's proved that this rule belongs to us, the
4511 * audit_rule_match hook will be called to do the final judgement.
4512 */
4513static int smack_audit_rule_known(struct audit_krule *krule)
4514{
4515 struct audit_field *f;
4516 int i;
4517
4518 for (i = 0; i < krule->field_count; i++) {
4519 f = &krule->fields[i];
4520
4521 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4522 return 1;
4523 }
4524
4525 return 0;
4526}
4527
4528/**
4529 * smack_audit_rule_match - Audit given object ?
4530 * @secid: security id for identifying the object to test
4531 * @field: audit rule flags given from user-space
4532 * @op: required testing operator
4533 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004534 *
4535 * The core Audit hook. It's used to take the decision of
4536 * whether to audit or not to audit a given object.
4537 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004538static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004539{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004540 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004541 char *rule = vrule;
4542
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004543 if (unlikely(!rule)) {
4544 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004545 return -ENOENT;
4546 }
4547
4548 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4549 return 0;
4550
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004551 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004552
4553 /*
4554 * No need to do string comparisons. If a match occurs,
4555 * both pointers will point to the same smack_known
4556 * label.
4557 */
Al Viro5af75d82008-12-16 05:59:26 -05004558 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004559 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004560 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004561 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004562
4563 return 0;
4564}
4565
Casey Schaufler491a0b02016-01-26 15:08:35 -08004566/*
4567 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004568 * No memory was allocated.
4569 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004570
4571#endif /* CONFIG_AUDIT */
4572
Randy Dunlap251a2a92009-02-18 11:42:33 -08004573/**
David Quigley746df9b2013-05-22 12:50:35 -04004574 * smack_ismaclabel - check if xattr @name references a smack MAC label
4575 * @name: Full xattr name to check.
4576 */
4577static int smack_ismaclabel(const char *name)
4578{
4579 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4580}
4581
4582
4583/**
Casey Schauflere114e472008-02-04 22:29:50 -08004584 * smack_secid_to_secctx - return the smack label for a secid
4585 * @secid: incoming integer
4586 * @secdata: destination
4587 * @seclen: how long it is
4588 *
4589 * Exists for networking code.
4590 */
4591static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4592{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004593 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004594
Eric Parisd5630b92010-10-13 16:24:48 -04004595 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004596 *secdata = skp->smk_known;
4597 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004598 return 0;
4599}
4600
Randy Dunlap251a2a92009-02-18 11:42:33 -08004601/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004602 * smack_secctx_to_secid - return the secid for a smack label
4603 * @secdata: smack label
4604 * @seclen: how long result is
4605 * @secid: outgoing integer
4606 *
4607 * Exists for audit and networking code.
4608 */
David Howellse52c17642008-04-29 20:52:51 +01004609static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004610{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004611 struct smack_known *skp = smk_find_entry(secdata);
4612
4613 if (skp)
4614 *secid = skp->smk_secid;
4615 else
4616 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004617 return 0;
4618}
4619
Casey Schaufler491a0b02016-01-26 15:08:35 -08004620/*
4621 * There used to be a smack_release_secctx hook
4622 * that did nothing back when hooks were in a vector.
4623 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004624 */
Casey Schauflere114e472008-02-04 22:29:50 -08004625
David P. Quigley1ee65e32009-09-03 14:25:57 -04004626static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4627{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01004628 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx,
4629 ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004630}
4631
4632static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4633{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01004634 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SMACK,
4635 ctx, ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004636}
4637
4638static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4639{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004640 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004641
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004642 *ctx = skp->smk_known;
4643 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004644 return 0;
4645}
4646
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004647static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4648{
4649
4650 struct task_smack *tsp;
4651 struct smack_known *skp;
4652 struct inode_smack *isp;
4653 struct cred *new_creds = *new;
4654
4655 if (new_creds == NULL) {
4656 new_creds = prepare_creds();
4657 if (new_creds == NULL)
4658 return -ENOMEM;
4659 }
4660
Casey Schauflerb17103a2018-11-09 16:12:56 -08004661 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004662
4663 /*
4664 * Get label from overlay inode and set it in create_sid
4665 */
Vishal Goelefce75b2021-09-17 13:08:14 +05304666 isp = smack_inode(d_inode(dentry));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004667 skp = isp->smk_inode;
4668 tsp->smk_task = skp;
4669 *new = new_creds;
4670 return 0;
4671}
4672
4673static int smack_inode_copy_up_xattr(const char *name)
4674{
4675 /*
4676 * Return 1 if this is the smack access Smack attribute.
4677 */
4678 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4679 return 1;
4680
4681 return -EOPNOTSUPP;
4682}
4683
4684static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4685 struct qstr *name,
4686 const struct cred *old,
4687 struct cred *new)
4688{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004689 struct task_smack *otsp = smack_cred(old);
4690 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004691 struct inode_smack *isp;
4692 int may;
4693
4694 /*
4695 * Use the process credential unless all of
4696 * the transmuting criteria are met
4697 */
4698 ntsp->smk_task = otsp->smk_task;
4699
4700 /*
4701 * the attribute of the containing directory
4702 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004703 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004704
4705 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4706 rcu_read_lock();
4707 may = smk_access_entry(otsp->smk_task->smk_known,
4708 isp->smk_inode->smk_known,
4709 &otsp->smk_task->smk_rules);
4710 rcu_read_unlock();
4711
4712 /*
4713 * If the directory is transmuting and the rule
4714 * providing access is transmuting use the containing
4715 * directory label instead of the process label.
4716 */
Roberto Sassu3586b3f2023-05-08 19:02:34 +02004717 if (may > 0 && (may & MAY_TRANSMUTE)) {
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004718 ntsp->smk_task = isp->smk_inode;
Roberto Sassu3586b3f2023-05-08 19:02:34 +02004719 ntsp->smk_transmuted = ntsp->smk_task;
4720 }
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004721 }
4722 return 0;
4723}
4724
Casey Schauflerbbd36622018-11-12 09:30:56 -08004725struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4726 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004727 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004728 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004729 .lbs_ipc = sizeof(struct smack_known *),
4730 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schaufler1aea7802021-04-22 17:41:15 +02004731 .lbs_superblock = sizeof(struct superblock_smack),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004732};
4733
James Morrisca97d932017-02-15 00:18:51 +11004734static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004735 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4736 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4737 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004738
Al Viro0b520752018-12-23 16:02:47 -05004739 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004740 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4741
Casey Schauflere20b0432015-05-02 15:11:36 -07004742 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004743 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004744 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004745 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304746 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004747
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05004748 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004749
Casey Schauflere20b0432015-05-02 15:11:36 -07004750 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004751 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4752 LSM_HOOK_INIT(inode_link, smack_inode_link),
4753 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4754 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4755 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4756 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4757 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4758 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4759 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4760 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4761 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4762 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4763 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4764 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4765 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4766 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004767
Casey Schauflere20b0432015-05-02 15:11:36 -07004768 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004769 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
Alfred Piccioni4590f202023-12-19 10:09:09 +01004770 LSM_HOOK_INIT(file_ioctl_compat, smack_file_ioctl),
Casey Schauflere20b0432015-05-02 15:11:36 -07004771 LSM_HOOK_INIT(file_lock, smack_file_lock),
4772 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4773 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4774 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4775 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4776 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4777 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004778
Casey Schauflere20b0432015-05-02 15:11:36 -07004779 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004780
Casey Schauflere20b0432015-05-02 15:11:36 -07004781 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4782 LSM_HOOK_INIT(cred_free, smack_cred_free),
4783 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4784 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004785 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004786 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4787 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4788 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4789 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4790 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
Paul Moore1fb057d2021-02-19 15:04:58 -05004791 LSM_HOOK_INIT(task_getsecid_subj, smack_task_getsecid_subj),
4792 LSM_HOOK_INIT(task_getsecid_obj, smack_task_getsecid_obj),
Casey Schauflere20b0432015-05-02 15:11:36 -07004793 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4794 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4795 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4796 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4797 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4798 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4799 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004800 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004801
Casey Schauflere20b0432015-05-02 15:11:36 -07004802 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4803 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004804
Casey Schauflere20b0432015-05-02 15:11:36 -07004805 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004806
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004807 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004808 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4809 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4810 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4811 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004812
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004813 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004814 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4815 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4816 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004817
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004818 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004819 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4820 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4821 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004822
Casey Schauflere20b0432015-05-02 15:11:36 -07004823 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004824
Casey Schauflere20b0432015-05-02 15:11:36 -07004825 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4826 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004827
Casey Schauflere20b0432015-05-02 15:11:36 -07004828 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4829 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004830
Casey Schauflere20b0432015-05-02 15:11:36 -07004831 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004832 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004833#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004834 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004835#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004836 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4837 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4838 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4839 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4840 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4841 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4842 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4843 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4844 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4845 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004846
Casey Schauflere114e472008-02-04 22:29:50 -08004847 /* key management security hooks */
4848#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004849 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4850 LSM_HOOK_INIT(key_free, smack_key_free),
4851 LSM_HOOK_INIT(key_permission, smack_key_permission),
4852 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
David Howellsa8478a62020-01-14 17:07:13 +00004853#ifdef CONFIG_KEY_NOTIFICATIONS
4854 LSM_HOOK_INIT(watch_key, smack_watch_key),
4855#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004856#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004857
David Howellsa8478a62020-01-14 17:07:13 +00004858#ifdef CONFIG_WATCH_QUEUE
4859 LSM_HOOK_INIT(post_notification, smack_post_notification),
4860#endif
4861
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004862 /* Audit hooks */
4863#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004864 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4865 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4866 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004867#endif /* CONFIG_AUDIT */
4868
Casey Schauflere20b0432015-05-02 15:11:36 -07004869 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4870 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4871 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004872 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4873 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4874 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004875 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4876 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4877 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004878};
4879
Etienne Basset7198e2e2009-03-24 20:53:24 +01004880
Casey Schaufler86812bb2012-04-17 18:55:46 -07004881static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004882{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004883 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004884 * Initialize rule list locks
4885 */
4886 mutex_init(&smack_known_huh.smk_rules_lock);
4887 mutex_init(&smack_known_hat.smk_rules_lock);
4888 mutex_init(&smack_known_floor.smk_rules_lock);
4889 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004890 mutex_init(&smack_known_web.smk_rules_lock);
4891 /*
4892 * Initialize rule lists
4893 */
4894 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4895 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4896 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4897 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004898 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4899 /*
4900 * Create the known labels list
4901 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004902 smk_insert_entry(&smack_known_huh);
4903 smk_insert_entry(&smack_known_hat);
4904 smk_insert_entry(&smack_known_star);
4905 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004906 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004907}
4908
Casey Schauflere114e472008-02-04 22:29:50 -08004909/**
4910 * smack_init - initialize the smack system
4911 *
luanshia1a07f22019-07-05 10:35:20 +08004912 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004913 */
4914static __init int smack_init(void)
4915{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004916 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004917 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004918
Casey Schaufler4e328b02019-04-02 11:37:12 -07004919 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
Casey Schaufler4ca75282020-04-28 15:00:26 -07004920 if (!smack_rule_cache)
Casey Schaufler4e328b02019-04-02 11:37:12 -07004921 return -ENOMEM;
Casey Schaufler4e328b02019-04-02 11:37:12 -07004922
Casey Schauflerbbd36622018-11-12 09:30:56 -08004923 /*
4924 * Set the security state for the initial task.
4925 */
4926 tsp = smack_cred(cred);
4927 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4928
4929 /*
4930 * Register with LSM
4931 */
4932 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004933 smack_enabled = 1;
4934
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004935 pr_info("Smack: Initializing.\n");
4936#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4937 pr_info("Smack: Netfilter enabled.\n");
4938#endif
4939#ifdef SMACK_IPV6_PORT_LABELING
4940 pr_info("Smack: IPv6 port labeling enabled.\n");
4941#endif
4942#ifdef SMACK_IPV6_SECMARK_LABELING
4943 pr_info("Smack: IPv6 Netfilter enabled.\n");
4944#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004945
Casey Schaufler86812bb2012-04-17 18:55:46 -07004946 /* initialize the smack_known_list */
4947 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004948
Casey Schauflere114e472008-02-04 22:29:50 -08004949 return 0;
4950}
4951
4952/*
4953 * Smack requires early initialization in order to label
4954 * all processes and objects when they are created.
4955 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004956DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004957 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004958 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004959 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004960 .init = smack_init,
4961};