blob: 6d4883a43fffe86745b0d74077a1738981d05e01 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Casey Schauflere114e472008-02-04 22:29:50 -08002/*
3 * Simplified MAC Kernel (smack) security module
4 *
5 * This file contains the smack hook function implementations.
6 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02007 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08008 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03009 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -080010 *
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040012 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000013 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020014 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030015 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080016 */
17
18#include <linux/xattr.h>
19#include <linux/pagemap.h>
20#include <linux/mount.h>
21#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080022#include <linux/kd.h>
23#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040024#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/tcp.h>
26#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070027#include <linux/dccp.h>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020028#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030#include <linux/mutex.h>
Casey Schauflere114e472008-02-04 22:29:50 -080031#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070032#include <net/ip.h>
33#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100034#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070035#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050036#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030037#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000038#include <linux/msg.h>
39#include <linux/shm.h>
40#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053041#include <linux/parser.h>
David Howells2febd252018-11-01 23:07:24 +000042#include <linux/fs_context.h>
43#include <linux/fs_parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080044#include "smack.h"
45
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020046#define TRANS_TRUE "TRUE"
47#define TRANS_TRUE_SIZE 4
48
Casey Schauflerc6739442013-05-22 18:42:56 -070049#define SMK_CONNECTING 0
50#define SMK_RECEIVING 1
51#define SMK_SENDING 2
52
Arnd Bergmann00720f02020-04-08 21:04:31 +020053static DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080054static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler4e328b02019-04-02 11:37:12 -070055struct kmem_cache *smack_rule_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080056int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070057
Al Viroc3300aa2018-12-16 01:52:24 -050058#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
59static struct {
60 const char *name;
61 int len;
62 int opt;
63} smk_mount_opts[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +010064 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
Al Viroc3300aa2018-12-16 01:52:24 -050065 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
Vivek Trivedi3bf27892015-06-22 15:36:06 +053066};
Al Viroc3300aa2018-12-16 01:52:24 -050067#undef A
68
69static int match_opt_prefix(char *s, int l, char **arg)
70{
71 int i;
72
73 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
74 size_t len = smk_mount_opts[i].len;
75 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
76 continue;
77 if (len == l || s[len] != '=')
78 continue;
79 *arg = s + len + 1;
80 return smk_mount_opts[i].opt;
81 }
82 return Opt_error;
83}
Vivek Trivedi3bf27892015-06-22 15:36:06 +053084
Casey Schaufler3d04c922015-08-12 11:56:02 -070085#ifdef CONFIG_SECURITY_SMACK_BRINGUP
86static char *smk_bu_mess[] = {
87 "Bringup Error", /* Unused */
88 "Bringup", /* SMACK_BRINGUP_ALLOW */
89 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
90 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
91};
92
Casey Schauflerd166c802014-08-27 14:51:27 -070093static void smk_bu_mode(int mode, char *s)
94{
95 int i = 0;
96
97 if (mode & MAY_READ)
98 s[i++] = 'r';
99 if (mode & MAY_WRITE)
100 s[i++] = 'w';
101 if (mode & MAY_EXEC)
102 s[i++] = 'x';
103 if (mode & MAY_APPEND)
104 s[i++] = 'a';
105 if (mode & MAY_TRANSMUTE)
106 s[i++] = 't';
107 if (mode & MAY_LOCK)
108 s[i++] = 'l';
109 if (i == 0)
110 s[i++] = '-';
111 s[i] = '\0';
112}
113#endif
114
115#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200116static int smk_bu_note(char *note, struct smack_known *sskp,
117 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700118{
119 char acc[SMK_NUM_ACCESS_TYPE + 1];
120
121 if (rc <= 0)
122 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700123 if (rc > SMACK_UNCONFINED_OBJECT)
124 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700125
126 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700127 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200128 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700129 return 0;
130}
131#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200132#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700133#endif
134
135#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200136static int smk_bu_current(char *note, struct smack_known *oskp,
137 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700138{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800139 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700140 char acc[SMK_NUM_ACCESS_TYPE + 1];
141
142 if (rc <= 0)
143 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700144 if (rc > SMACK_UNCONFINED_OBJECT)
145 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700146
147 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700148 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200149 tsp->smk_task->smk_known, oskp->smk_known,
150 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700151 return 0;
152}
153#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200154#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700155#endif
156
157#ifdef CONFIG_SECURITY_SMACK_BRINGUP
158static int smk_bu_task(struct task_struct *otp, int mode, int rc)
159{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800160 struct task_smack *tsp = smack_cred(current_cred());
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300161 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700162 char acc[SMK_NUM_ACCESS_TYPE + 1];
163
164 if (rc <= 0)
165 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700166 if (rc > SMACK_UNCONFINED_OBJECT)
167 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700168
169 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700170 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300171 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700172 current->comm, otp->comm);
173 return 0;
174}
175#else
176#define smk_bu_task(otp, mode, RC) (RC)
177#endif
178
179#ifdef CONFIG_SECURITY_SMACK_BRINGUP
180static int smk_bu_inode(struct inode *inode, int mode, int rc)
181{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800182 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800183 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700184 char acc[SMK_NUM_ACCESS_TYPE + 1];
185
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700186 if (isp->smk_flags & SMK_INODE_IMPURE)
187 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
188 inode->i_sb->s_id, inode->i_ino, current->comm);
189
Casey Schauflerd166c802014-08-27 14:51:27 -0700190 if (rc <= 0)
191 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700192 if (rc > SMACK_UNCONFINED_OBJECT)
193 rc = 0;
194 if (rc == SMACK_UNCONFINED_SUBJECT &&
195 (mode & (MAY_WRITE | MAY_APPEND)))
196 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700197
198 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700199
200 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
201 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700202 inode->i_sb->s_id, inode->i_ino, current->comm);
203 return 0;
204}
205#else
206#define smk_bu_inode(inode, mode, RC) (RC)
207#endif
208
209#ifdef CONFIG_SECURITY_SMACK_BRINGUP
210static int smk_bu_file(struct file *file, int mode, int rc)
211{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800212 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700213 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800214 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800215 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700216 char acc[SMK_NUM_ACCESS_TYPE + 1];
217
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700218 if (isp->smk_flags & SMK_INODE_IMPURE)
219 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
220 inode->i_sb->s_id, inode->i_ino, current->comm);
221
Casey Schauflerd166c802014-08-27 14:51:27 -0700222 if (rc <= 0)
223 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700224 if (rc > SMACK_UNCONFINED_OBJECT)
225 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700226
227 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700228 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800229 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400230 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700231 current->comm);
232 return 0;
233}
234#else
235#define smk_bu_file(file, mode, RC) (RC)
236#endif
237
238#ifdef CONFIG_SECURITY_SMACK_BRINGUP
239static int smk_bu_credfile(const struct cred *cred, struct file *file,
240 int mode, int rc)
241{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800242 struct task_smack *tsp = smack_cred(cred);
Casey Schauflerd166c802014-08-27 14:51:27 -0700243 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500244 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800245 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700246 char acc[SMK_NUM_ACCESS_TYPE + 1];
247
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700248 if (isp->smk_flags & SMK_INODE_IMPURE)
249 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
250 inode->i_sb->s_id, inode->i_ino, current->comm);
251
Casey Schauflerd166c802014-08-27 14:51:27 -0700252 if (rc <= 0)
253 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700254 if (rc > SMACK_UNCONFINED_OBJECT)
255 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700256
257 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700258 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200259 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400260 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700261 current->comm);
262 return 0;
263}
264#else
265#define smk_bu_credfile(cred, file, mode, RC) (RC)
266#endif
267
Casey Schauflere114e472008-02-04 22:29:50 -0800268/**
269 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100270 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800271 * @ip: a pointer to the inode
272 * @dp: a pointer to the dentry
273 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200274 * Returns a pointer to the master list entry for the Smack label,
275 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800276 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700277static struct smack_known *smk_fetch(const char *name, struct inode *ip,
278 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800279{
280 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700281 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700282 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800283
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200284 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200285 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800286
Eric Biggerse5bfad32019-08-21 22:54:41 -0700287 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700288 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200289 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800290
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200291 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200292 if (rc < 0)
293 skp = ERR_PTR(rc);
294 else if (rc == 0)
295 skp = NULL;
296 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700297 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700298
299 kfree(buffer);
300
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700301 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800302}
303
304/**
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700305 * init_inode_smack - initialize an inode security blob
luanshia1a07f22019-07-05 10:35:20 +0800306 * @inode: inode to extract the info from
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200307 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800308 *
Casey Schauflere114e472008-02-04 22:29:50 -0800309 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700310static void init_inode_smack(struct inode *inode, struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800311{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700312 struct inode_smack *isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -0800313
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200314 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800315 isp->smk_flags = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800316}
317
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800318/**
Casey Schauflerbbd36622018-11-12 09:30:56 -0800319 * init_task_smack - initialize a task security blob
320 * @tsp: blob to initialize
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100321 * @task: a pointer to the Smack label for the running task
322 * @forked: a pointer to the Smack label for the forked task
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800323 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800324 */
Casey Schauflerbbd36622018-11-12 09:30:56 -0800325static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
326 struct smack_known *forked)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800327{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800328 tsp->smk_task = task;
329 tsp->smk_forked = forked;
330 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200331 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800332 mutex_init(&tsp->smk_rules_lock);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800333}
334
335/**
336 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100337 * @nhead: new rules header pointer
338 * @ohead: old rules header pointer
339 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800340 *
341 * Returns 0 on success, -ENOMEM on error
342 */
343static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
344 gfp_t gfp)
345{
346 struct smack_rule *nrp;
347 struct smack_rule *orp;
348 int rc = 0;
349
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800350 list_for_each_entry_rcu(orp, ohead, list) {
Casey Schaufler4e328b02019-04-02 11:37:12 -0700351 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800352 if (nrp == NULL) {
353 rc = -ENOMEM;
354 break;
355 }
356 *nrp = *orp;
357 list_add_rcu(&nrp->list, nhead);
358 }
359 return rc;
360}
361
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100362/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200363 * smk_copy_relabel - copy smk_relabel labels list
364 * @nhead: new rules header pointer
365 * @ohead: old rules header pointer
366 * @gfp: type of the memory for the allocation
367 *
368 * Returns 0 on success, -ENOMEM on error
369 */
370static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
371 gfp_t gfp)
372{
373 struct smack_known_list_elem *nklep;
374 struct smack_known_list_elem *oklep;
375
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200376 list_for_each_entry(oklep, ohead, list) {
377 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
378 if (nklep == NULL) {
379 smk_destroy_label_list(nhead);
380 return -ENOMEM;
381 }
382 nklep->smk_label = oklep->smk_label;
383 list_add(&nklep->list, nhead);
384 }
385
386 return 0;
387}
388
389/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100390 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
391 * @mode - input mode in form of PTRACE_MODE_*
392 *
393 * Returns a converted MAY_* mode usable by smack rules
394 */
395static inline unsigned int smk_ptrace_mode(unsigned int mode)
396{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800397 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100398 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800399 if (mode & PTRACE_MODE_READ)
400 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100401
402 return 0;
403}
404
405/**
406 * smk_ptrace_rule_check - helper for ptrace access
407 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200408 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100409 * @mode: ptrace attachment mode (PTRACE_MODE_*)
410 * @func: name of the function that called us, used for audit
411 *
412 * Returns 0 on access granted, -error on error
413 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200414static int smk_ptrace_rule_check(struct task_struct *tracer,
415 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100416 unsigned int mode, const char *func)
417{
418 int rc;
419 struct smk_audit_info ad, *saip = NULL;
420 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200421 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700422 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100423
424 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
425 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
426 smk_ad_setfield_u_tsk(&ad, tracer);
427 saip = &ad;
428 }
429
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300430 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700431 tracercred = __task_cred(tracer);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800432 tsp = smack_cred(tracercred);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200433 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100434
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100435 if ((mode & PTRACE_MODE_ATTACH) &&
436 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
437 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200438 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100439 rc = 0;
440 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
441 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700442 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100443 rc = 0;
444 else
445 rc = -EACCES;
446
447 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200448 smack_log(tracer_known->smk_known,
449 tracee_known->smk_known,
450 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100451
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300452 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100453 return rc;
454 }
455
456 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200457 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300458
459 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100460 return rc;
461}
462
Casey Schauflere114e472008-02-04 22:29:50 -0800463/*
464 * LSM hooks.
465 * We he, that is fun!
466 */
467
468/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000469 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800470 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100471 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800472 *
473 * Returns 0 if access is OK, an error code otherwise
474 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100475 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800476 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000477static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800478{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700479 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800480
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300481 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200482
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700483 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100484}
Casey Schauflere114e472008-02-04 22:29:50 -0800485
David Howells5cd9c582008-08-14 11:37:28 +0100486/**
487 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
488 * @ptp: parent task pointer
489 *
490 * Returns 0 if access is OK, an error code otherwise
491 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100492 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100493 */
494static int smack_ptrace_traceme(struct task_struct *ptp)
495{
496 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700497 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100498
Casey Schauflerb17103a2018-11-09 16:12:56 -0800499 skp = smk_of_task(smack_cred(current_cred()));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200500
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200501 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800502 return rc;
503}
504
505/**
506 * smack_syslog - Smack approval on syslog
luanshia1a07f22019-07-05 10:35:20 +0800507 * @typefrom_file: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800508 *
Casey Schauflere114e472008-02-04 22:29:50 -0800509 * Returns 0 on success, error code otherwise.
510 */
Eric Paris12b30522010-11-15 18:36:29 -0500511static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800512{
Eric Paris12b30522010-11-15 18:36:29 -0500513 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700514 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800515
Casey Schaufler1880eff2012-06-05 15:28:30 -0700516 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800517 return 0;
518
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800519 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800520 rc = -EACCES;
521
522 return rc;
523}
524
Casey Schauflere114e472008-02-04 22:29:50 -0800525/*
526 * Superblock Hooks.
527 */
528
529/**
530 * smack_sb_alloc_security - allocate a superblock blob
531 * @sb: the superblock getting the blob
532 *
533 * Returns 0 on success or -ENOMEM on error.
534 */
535static int smack_sb_alloc_security(struct super_block *sb)
536{
537 struct superblock_smack *sbsp;
538
539 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
540
541 if (sbsp == NULL)
542 return -ENOMEM;
543
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200544 sbsp->smk_root = &smack_known_floor;
545 sbsp->smk_default = &smack_known_floor;
546 sbsp->smk_floor = &smack_known_floor;
547 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700548 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500549 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700550 */
Casey Schauflere114e472008-02-04 22:29:50 -0800551 sb->s_security = sbsp;
552
553 return 0;
554}
555
556/**
557 * smack_sb_free_security - free a superblock blob
558 * @sb: the superblock getting the blob
559 *
560 */
561static void smack_sb_free_security(struct super_block *sb)
562{
563 kfree(sb->s_security);
564 sb->s_security = NULL;
565}
566
Al Viro12085b12018-12-13 15:18:05 -0500567struct smack_mnt_opts {
568 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
569};
570
Al Viro204cc0c2018-12-13 13:41:47 -0500571static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800572{
Al Viro12085b12018-12-13 15:18:05 -0500573 struct smack_mnt_opts *opts = mnt_opts;
574 kfree(opts->fsdefault);
575 kfree(opts->fsfloor);
576 kfree(opts->fshat);
577 kfree(opts->fsroot);
578 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500579 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800580}
581
Al Viro55c0e5b2018-12-16 01:09:45 -0500582static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530583{
Al Viro55c0e5b2018-12-16 01:09:45 -0500584 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530585
Al Viro55c0e5b2018-12-16 01:09:45 -0500586 if (!opts) {
587 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
588 if (!opts)
589 return -ENOMEM;
590 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530591 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500592 if (!s)
593 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530594
Al Viro55c0e5b2018-12-16 01:09:45 -0500595 switch (token) {
596 case Opt_fsdefault:
597 if (opts->fsdefault)
598 goto out_opt_err;
599 opts->fsdefault = s;
600 break;
601 case Opt_fsfloor:
602 if (opts->fsfloor)
603 goto out_opt_err;
604 opts->fsfloor = s;
605 break;
606 case Opt_fshat:
607 if (opts->fshat)
608 goto out_opt_err;
609 opts->fshat = s;
610 break;
611 case Opt_fsroot:
612 if (opts->fsroot)
613 goto out_opt_err;
614 opts->fsroot = s;
615 break;
616 case Opt_fstransmute:
617 if (opts->fstransmute)
618 goto out_opt_err;
619 opts->fstransmute = s;
620 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530621 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530622 return 0;
623
624out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530625 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500626 return -EINVAL;
627}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530628
Al Viro0b520752018-12-23 16:02:47 -0500629/**
630 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
631 * @fc: The new filesystem context.
632 * @src_fc: The source filesystem context being duplicated.
633 *
634 * Returns 0 on success or -ENOMEM on error.
635 */
636static int smack_fs_context_dup(struct fs_context *fc,
637 struct fs_context *src_fc)
638{
639 struct smack_mnt_opts *dst, *src = src_fc->security;
640
641 if (!src)
642 return 0;
643
644 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
645 if (!fc->security)
646 return -ENOMEM;
647 dst = fc->security;
648
649 if (src->fsdefault) {
650 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
651 if (!dst->fsdefault)
652 return -ENOMEM;
653 }
654 if (src->fsfloor) {
655 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
656 if (!dst->fsfloor)
657 return -ENOMEM;
658 }
659 if (src->fshat) {
660 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
661 if (!dst->fshat)
662 return -ENOMEM;
663 }
664 if (src->fsroot) {
665 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
666 if (!dst->fsroot)
667 return -ENOMEM;
668 }
669 if (src->fstransmute) {
670 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
671 if (!dst->fstransmute)
672 return -ENOMEM;
673 }
674 return 0;
675}
676
Al Virod7167b12019-09-07 07:23:15 -0400677static const struct fs_parameter_spec smack_fs_parameters[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +0100678 fsparam_string("smackfsdef", Opt_fsdefault),
679 fsparam_string("smackfsdefault", Opt_fsdefault),
680 fsparam_string("smackfsfloor", Opt_fsfloor),
681 fsparam_string("smackfshat", Opt_fshat),
682 fsparam_string("smackfsroot", Opt_fsroot),
683 fsparam_string("smackfstransmute", Opt_fstransmute),
David Howells2febd252018-11-01 23:07:24 +0000684 {}
685};
686
David Howells2febd252018-11-01 23:07:24 +0000687/**
688 * smack_fs_context_parse_param - Parse a single mount parameter
689 * @fc: The new filesystem context being constructed.
690 * @param: The parameter.
691 *
692 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
693 * error.
694 */
695static int smack_fs_context_parse_param(struct fs_context *fc,
696 struct fs_parameter *param)
697{
698 struct fs_parse_result result;
699 int opt, rc;
700
Al Virod7167b12019-09-07 07:23:15 -0400701 opt = fs_parse(fc, smack_fs_parameters, param, &result);
David Howells2febd252018-11-01 23:07:24 +0000702 if (opt < 0)
703 return opt;
704
705 rc = smack_add_opt(opt, param->string, &fc->security);
706 if (!rc)
707 param->string = NULL;
708 return rc;
709}
710
Al Virod2497e12018-12-16 01:37:06 -0500711static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530712{
Al Virod2497e12018-12-16 01:37:06 -0500713 char *from = options, *to = options;
714 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530715
Al Viroc3300aa2018-12-16 01:52:24 -0500716 while (1) {
717 char *next = strchr(from, ',');
718 int token, len, rc;
719 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530720
Al Viroc3300aa2018-12-16 01:52:24 -0500721 if (next)
722 len = next - from;
723 else
724 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530725
Al Viroc3300aa2018-12-16 01:52:24 -0500726 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500727 if (token != Opt_error) {
728 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
729 rc = smack_add_opt(token, arg, mnt_opts);
730 if (unlikely(rc)) {
731 kfree(arg);
732 if (*mnt_opts)
733 smack_free_mnt_opts(*mnt_opts);
734 *mnt_opts = NULL;
735 return rc;
736 }
737 } else {
738 if (!first) { // copy with preceding comma
739 from--;
740 len++;
741 }
742 if (to != from)
743 memmove(to, from, len);
744 to += len;
745 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530746 }
Al Viroc3300aa2018-12-16 01:52:24 -0500747 if (!from[len])
748 break;
749 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530750 }
Al Virod2497e12018-12-16 01:37:06 -0500751 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530752 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530753}
754
755/**
756 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800757 * @sb: the file system superblock
luanshia1a07f22019-07-05 10:35:20 +0800758 * @mnt_opts: Smack mount options
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530759 * @kern_flags: mount option from kernel space or user space
760 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800761 *
762 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530763 *
764 * Allow filesystems with binary mount data to explicitly set Smack mount
765 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800766 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530767static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500768 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530769 unsigned long kern_flags,
770 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800771{
772 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000773 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800774 struct superblock_smack *sp = sb->s_security;
775 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800776 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500777 struct smack_mnt_opts *opts = mnt_opts;
778 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800779
Seth Forshee9f50eda2015-09-23 15:16:06 -0500780 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800781 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700782
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700783 if (inode->i_security == NULL) {
784 int rc = lsm_inode_alloc(inode);
785
786 if (rc)
787 return rc;
788 }
789
Himanshu Shukla2097f592016-11-10 16:19:52 +0530790 if (!smack_privileged(CAP_MAC_ADMIN)) {
791 /*
792 * Unprivileged mounts don't get to specify Smack values.
793 */
Al Viro12085b12018-12-13 15:18:05 -0500794 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530795 return -EPERM;
796 /*
797 * Unprivileged mounts get root and default from the caller.
798 */
799 skp = smk_of_current();
800 sp->smk_root = skp;
801 sp->smk_default = skp;
802 /*
803 * For a handful of fs types with no user-controlled
804 * backing store it's okay to trust security labels
805 * in the filesystem. The rest are untrusted.
806 */
807 if (sb->s_user_ns != &init_user_ns &&
808 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
809 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500810 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530811 sp->smk_flags |= SMK_SB_UNTRUSTED;
812 }
813 }
814
Seth Forshee9f50eda2015-09-23 15:16:06 -0500815 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800816
Al Viro12085b12018-12-13 15:18:05 -0500817 if (opts) {
818 if (opts->fsdefault) {
819 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200820 if (IS_ERR(skp))
821 return PTR_ERR(skp);
822 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500823 }
824 if (opts->fsfloor) {
825 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530826 if (IS_ERR(skp))
827 return PTR_ERR(skp);
828 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500829 }
830 if (opts->fshat) {
831 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530832 if (IS_ERR(skp))
833 return PTR_ERR(skp);
834 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500835 }
836 if (opts->fsroot) {
837 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200838 if (IS_ERR(skp))
839 return PTR_ERR(skp);
840 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500841 }
842 if (opts->fstransmute) {
843 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200844 if (IS_ERR(skp))
845 return PTR_ERR(skp);
846 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500847 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800848 }
849 }
850
851 /*
852 * Initialize the root inode.
853 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700854 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800855
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700856 if (transmute) {
857 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700858 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700859 }
Casey Schauflere830b392013-05-22 18:43:07 -0700860
Casey Schauflere114e472008-02-04 22:29:50 -0800861 return 0;
862}
863
864/**
865 * smack_sb_statfs - Smack check on statfs
866 * @dentry: identifies the file system in question
867 *
868 * Returns 0 if current can read the floor of the filesystem,
869 * and error code otherwise
870 */
871static int smack_sb_statfs(struct dentry *dentry)
872{
873 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200874 int rc;
875 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800876
Eric Parisa2694342011-04-25 13:10:27 -0400877 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200878 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
879
880 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700881 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200882 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800883}
884
Casey Schauflere114e472008-02-04 22:29:50 -0800885/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800886 * BPRM hooks
887 */
888
Casey Schauflerce8a4322011-09-29 18:21:01 -0700889/**
890 * smack_bprm_set_creds - set creds for exec
891 * @bprm: the exec information
892 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100893 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700894 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800895static int smack_bprm_set_creds(struct linux_binprm *bprm)
896{
Al Viro496ad9a2013-01-23 17:07:38 -0500897 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800898 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800899 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500900 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800901 int rc;
902
Kees Cookddb4a142017-07-18 15:25:23 -0700903 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800904 return 0;
905
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800906 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300907 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800908 return 0;
909
Seth Forshee809c02e2016-04-26 14:36:22 -0500910 sbsp = inode->i_sb->s_security;
911 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
912 isp->smk_task != sbsp->smk_root)
913 return 0;
914
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300915 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100916 struct task_struct *tracer;
917 rc = 0;
918
919 rcu_read_lock();
920 tracer = ptrace_parent(current);
921 if (likely(tracer != NULL))
922 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200923 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100924 PTRACE_MODE_ATTACH,
925 __func__);
926 rcu_read_unlock();
927
928 if (rc != 0)
929 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200930 }
931 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300932 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800933
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300934 bsp->smk_task = isp->smk_task;
935 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800936
Kees Cookccbb6e12017-07-18 15:25:26 -0700937 /* Decide if this is a secure exec. */
938 if (bsp->smk_task != bsp->smk_forked)
939 bprm->secureexec = 1;
940
Casey Schaufler676dac42010-12-02 06:43:39 -0800941 return 0;
942}
943
944/*
Casey Schauflere114e472008-02-04 22:29:50 -0800945 * Inode hooks
946 */
947
948/**
949 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800950 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800951 *
luanshia1a07f22019-07-05 10:35:20 +0800952 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800953 */
954static int smack_inode_alloc_security(struct inode *inode)
955{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700956 struct smack_known *skp = smk_of_current();
957
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700958 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800959 return 0;
960}
961
962/**
Casey Schauflere114e472008-02-04 22:29:50 -0800963 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200964 * @inode: the newly created inode
965 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500966 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800967 * @name: where to put the attribute name
968 * @value: where to put the attribute value
969 * @len: where to put the length of the attribute
970 *
971 * Returns 0 if it all works out, -ENOMEM if there's no memory
972 */
973static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900974 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500975 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800976{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800977 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700978 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200979 struct smack_known *isp = smk_of_inode(inode);
980 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800981 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800982
Tetsuo Handa95489062013-07-25 05:44:02 +0900983 if (name)
984 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800985
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100986 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800987 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200988 may = smk_access_entry(skp->smk_known, dsp->smk_known,
989 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800990 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200991
992 /*
993 * If the access rule allows transmutation and
994 * the directory requests transmutation then
995 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700996 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200997 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800998 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700999 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001000 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001001 issp->smk_flags |= SMK_INODE_CHANGED;
1002 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001003
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001004 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001005 if (*value == NULL)
1006 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001007
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001008 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001009 }
Casey Schauflere114e472008-02-04 22:29:50 -08001010
1011 return 0;
1012}
1013
1014/**
1015 * smack_inode_link - Smack check on link
1016 * @old_dentry: the existing object
1017 * @dir: unused
1018 * @new_dentry: the new object
1019 *
1020 * Returns 0 if access is permitted, an error code otherwise
1021 */
1022static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1023 struct dentry *new_dentry)
1024{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001025 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001026 struct smk_audit_info ad;
1027 int rc;
1028
Eric Parisa2694342011-04-25 13:10:27 -04001029 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001030 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001031
David Howellsc6f493d2015-03-17 22:26:22 +00001032 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001033 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001034 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001035
David Howells88025652015-01-29 12:02:32 +00001036 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001037 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001038 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1039 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001040 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001041 }
1042
1043 return rc;
1044}
1045
1046/**
1047 * smack_inode_unlink - Smack check on inode deletion
1048 * @dir: containing directory object
1049 * @dentry: file to unlink
1050 *
1051 * Returns 0 if current can write the containing directory
1052 * and the object, error code otherwise
1053 */
1054static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1055{
David Howellsc6f493d2015-03-17 22:26:22 +00001056 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001057 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001058 int rc;
1059
Eric Parisa2694342011-04-25 13:10:27 -04001060 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001061 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1062
Casey Schauflere114e472008-02-04 22:29:50 -08001063 /*
1064 * You need write access to the thing you're unlinking
1065 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001066 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001067 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001068 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001069 /*
1070 * You also need write access to the containing directory
1071 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001072 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001073 smk_ad_setfield_u_fs_inode(&ad, dir);
1074 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001075 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001076 }
Casey Schauflere114e472008-02-04 22:29:50 -08001077 return rc;
1078}
1079
1080/**
1081 * smack_inode_rmdir - Smack check on directory deletion
1082 * @dir: containing directory object
1083 * @dentry: directory to unlink
1084 *
1085 * Returns 0 if current can write the containing directory
1086 * and the directory, error code otherwise
1087 */
1088static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1089{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001090 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001091 int rc;
1092
Eric Parisa2694342011-04-25 13:10:27 -04001093 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001094 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1095
Casey Schauflere114e472008-02-04 22:29:50 -08001096 /*
1097 * You need write access to the thing you're removing
1098 */
David Howellsc6f493d2015-03-17 22:26:22 +00001099 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1100 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001101 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001102 /*
1103 * You also need write access to the containing directory
1104 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 smk_ad_setfield_u_fs_inode(&ad, dir);
1107 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001108 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001109 }
Casey Schauflere114e472008-02-04 22:29:50 -08001110
1111 return rc;
1112}
1113
1114/**
1115 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001116 * @old_inode: unused
1117 * @old_dentry: the old object
1118 * @new_inode: unused
1119 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001120 *
1121 * Read and write access is required on both the old and
1122 * new directories.
1123 *
1124 * Returns 0 if access is permitted, an error code otherwise
1125 */
1126static int smack_inode_rename(struct inode *old_inode,
1127 struct dentry *old_dentry,
1128 struct inode *new_inode,
1129 struct dentry *new_dentry)
1130{
1131 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001132 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001133 struct smk_audit_info ad;
1134
Eric Parisa2694342011-04-25 13:10:27 -04001135 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001136 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001137
David Howellsc6f493d2015-03-17 22:26:22 +00001138 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001139 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001140 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001141
David Howells88025652015-01-29 12:02:32 +00001142 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001143 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001144 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1145 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001146 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001147 }
Casey Schauflere114e472008-02-04 22:29:50 -08001148 return rc;
1149}
1150
1151/**
1152 * smack_inode_permission - Smack version of permission()
1153 * @inode: the inode in question
1154 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001155 *
1156 * This is the important Smack hook.
1157 *
luanshia1a07f22019-07-05 10:35:20 +08001158 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001159 */
Al Viroe74f71e2011-06-20 19:38:15 -04001160static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001161{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001162 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001163 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001164 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001165 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001166
1167 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001168 /*
1169 * No permission to check. Existence test. Yup, it's there.
1170 */
1171 if (mask == 0)
1172 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001173
Seth Forshee9f50eda2015-09-23 15:16:06 -05001174 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1175 if (smk_of_inode(inode) != sbsp->smk_root)
1176 return -EACCES;
1177 }
1178
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001179 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001180 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001181 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001182 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001183 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001184 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1185 rc = smk_bu_inode(inode, mask, rc);
1186 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001187}
1188
1189/**
1190 * smack_inode_setattr - Smack check for setting attributes
1191 * @dentry: the object
1192 * @iattr: for the force flag
1193 *
1194 * Returns 0 if access is permitted, an error code otherwise
1195 */
1196static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1197{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001198 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001199 int rc;
1200
Casey Schauflere114e472008-02-04 22:29:50 -08001201 /*
1202 * Need to allow for clearing the setuid bit.
1203 */
1204 if (iattr->ia_valid & ATTR_FORCE)
1205 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001206 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001207 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001208
David Howellsc6f493d2015-03-17 22:26:22 +00001209 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1210 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001211 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001212}
1213
1214/**
1215 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001216 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001217 *
1218 * Returns 0 if access is permitted, an error code otherwise
1219 */
Al Viro3f7036a2015-03-08 19:28:30 -04001220static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001221{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001222 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001223 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001224 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001225
Eric Parisf48b7392011-04-25 12:54:27 -04001226 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001227 smk_ad_setfield_u_fs_path(&ad, *path);
1228 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1229 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001230 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001231}
1232
1233/**
1234 * smack_inode_setxattr - Smack check for setting xattrs
1235 * @dentry: the object
1236 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001237 * @value: value of the attribute
1238 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001239 * @flags: unused
1240 *
1241 * This protects the Smack attribute explicitly.
1242 *
1243 * Returns 0 if access is permitted, an error code otherwise
1244 */
David Howells8f0cfa52008-04-29 00:59:41 -07001245static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1246 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001247{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001248 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001249 struct smack_known *skp;
1250 int check_priv = 0;
1251 int check_import = 0;
1252 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001253 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001254
Casey Schaufler19760ad2013-12-16 16:27:26 -08001255 /*
1256 * Check label validity here so import won't fail in post_setxattr
1257 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001258 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1259 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001260 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1261 check_priv = 1;
1262 check_import = 1;
1263 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1264 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1265 check_priv = 1;
1266 check_import = 1;
1267 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001268 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001269 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001270 if (size != TRANS_TRUE_SIZE ||
1271 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1272 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001273 } else
1274 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1275
Casey Schaufler19760ad2013-12-16 16:27:26 -08001276 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1277 rc = -EPERM;
1278
1279 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001280 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001281 if (IS_ERR(skp))
1282 rc = PTR_ERR(skp);
1283 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001284 (skp == &smack_known_star || skp == &smack_known_web)))
1285 rc = -EINVAL;
1286 }
1287
Eric Parisa2694342011-04-25 13:10:27 -04001288 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001289 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1290
Casey Schauflerd166c802014-08-27 14:51:27 -07001291 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001292 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1293 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001294 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001295
1296 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001297}
1298
1299/**
1300 * smack_inode_post_setxattr - Apply the Smack update approved above
1301 * @dentry: object
1302 * @name: attribute name
1303 * @value: attribute value
1304 * @size: attribute size
1305 * @flags: unused
1306 *
1307 * Set the pointer in the inode blob to the entry found
1308 * in the master label list.
1309 */
David Howells8f0cfa52008-04-29 00:59:41 -07001310static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1311 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001312{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001313 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001314 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001315
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001316 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1317 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1318 return;
1319 }
1320
Casey Schaufler676dac42010-12-02 06:43:39 -08001321 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001322 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001323 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001324 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001325 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001326 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001327 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001328 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001329 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001330 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001331 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001332 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001333 }
Casey Schauflere114e472008-02-04 22:29:50 -08001334
1335 return;
1336}
1337
Casey Schauflerce8a4322011-09-29 18:21:01 -07001338/**
Casey Schauflere114e472008-02-04 22:29:50 -08001339 * smack_inode_getxattr - Smack check on getxattr
1340 * @dentry: the object
1341 * @name: unused
1342 *
1343 * Returns 0 if access is permitted, an error code otherwise
1344 */
David Howells8f0cfa52008-04-29 00:59:41 -07001345static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001346{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001347 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001348 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001349
Eric Parisa2694342011-04-25 13:10:27 -04001350 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001351 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1352
David Howellsc6f493d2015-03-17 22:26:22 +00001353 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1354 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001355 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001356}
1357
Casey Schauflerce8a4322011-09-29 18:21:01 -07001358/**
Casey Schauflere114e472008-02-04 22:29:50 -08001359 * smack_inode_removexattr - Smack check on removexattr
1360 * @dentry: the object
1361 * @name: name of the attribute
1362 *
1363 * Removing the Smack attribute requires CAP_MAC_ADMIN
1364 *
1365 * Returns 0 if access is permitted, an error code otherwise
1366 */
David Howells8f0cfa52008-04-29 00:59:41 -07001367static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001368{
Casey Schaufler676dac42010-12-02 06:43:39 -08001369 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001370 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001371 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001372
Casey Schauflerbcdca222008-02-23 15:24:04 -08001373 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1374 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001375 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001376 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001377 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301378 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001379 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001380 rc = -EPERM;
1381 } else
1382 rc = cap_inode_removexattr(dentry, name);
1383
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001384 if (rc != 0)
1385 return rc;
1386
Eric Parisa2694342011-04-25 13:10:27 -04001387 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001388 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001389
David Howellsc6f493d2015-03-17 22:26:22 +00001390 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1391 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001392 if (rc != 0)
1393 return rc;
1394
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001395 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001396 /*
1397 * Don't do anything special for these.
1398 * XATTR_NAME_SMACKIPIN
1399 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001400 */
José Bollo80124952016-01-12 21:23:40 +01001401 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001402 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001403 struct superblock_smack *sbsp = sbp->s_security;
1404
1405 isp->smk_inode = sbsp->smk_default;
1406 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001407 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001408 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001409 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001410 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1411 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001412
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001413 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001414}
1415
1416/**
1417 * smack_inode_getsecurity - get smack xattrs
1418 * @inode: the object
1419 * @name: attribute name
1420 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001421 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001422 *
1423 * Returns the size of the attribute or an error code
1424 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001425static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001426 const char *name, void **buffer,
1427 bool alloc)
1428{
1429 struct socket_smack *ssp;
1430 struct socket *sock;
1431 struct super_block *sbp;
1432 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001433 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001434
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001435 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001436 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001437 else {
1438 /*
1439 * The rest of the Smack xattrs are only on sockets.
1440 */
1441 sbp = ip->i_sb;
1442 if (sbp->s_magic != SOCKFS_MAGIC)
1443 return -EOPNOTSUPP;
1444
1445 sock = SOCKET_I(ip);
1446 if (sock == NULL || sock->sk == NULL)
1447 return -EOPNOTSUPP;
1448
1449 ssp = sock->sk->sk_security;
1450
1451 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1452 isp = ssp->smk_in;
1453 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1454 isp = ssp->smk_out;
1455 else
1456 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001457 }
1458
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001459 if (alloc) {
1460 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1461 if (*buffer == NULL)
1462 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001463 }
1464
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001465 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001466}
1467
1468
1469/**
1470 * smack_inode_listsecurity - list the Smack attributes
1471 * @inode: the object
1472 * @buffer: where they go
1473 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001474 */
1475static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1476 size_t buffer_size)
1477{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001478 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001479
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001480 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001481 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001482
1483 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001484}
1485
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001486/**
1487 * smack_inode_getsecid - Extract inode's security id
1488 * @inode: inode to extract the info from
1489 * @secid: where result will be saved
1490 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001491static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001492{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001493 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001494
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001495 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001496}
1497
Casey Schauflere114e472008-02-04 22:29:50 -08001498/*
1499 * File Hooks
1500 */
1501
Casey Schaufler491a0b02016-01-26 15:08:35 -08001502/*
1503 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001504 *
1505 * Should access checks be done on each read or write?
1506 * UNICOS and SELinux say yes.
1507 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1508 *
1509 * I'll say no for now. Smack does not do the frequent
1510 * label changing that SELinux does.
1511 */
Casey Schauflere114e472008-02-04 22:29:50 -08001512
1513/**
1514 * smack_file_alloc_security - assign a file security blob
1515 * @file: the object
1516 *
1517 * The security blob for a file is a pointer to the master
1518 * label list, so no allocation is done.
1519 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001520 * f_security is the owner security information. It
1521 * isn't used on file access checks, it's for send_sigio.
1522 *
Casey Schauflere114e472008-02-04 22:29:50 -08001523 * Returns 0
1524 */
1525static int smack_file_alloc_security(struct file *file)
1526{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001527 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001528
Casey Schauflerf28952a2018-11-12 09:38:53 -08001529 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001530 return 0;
1531}
1532
1533/**
Casey Schauflere114e472008-02-04 22:29:50 -08001534 * smack_file_ioctl - Smack check on ioctls
1535 * @file: the object
1536 * @cmd: what to do
1537 * @arg: unused
1538 *
1539 * Relies heavily on the correct use of the ioctl command conventions.
1540 *
1541 * Returns 0 if allowed, error code otherwise
1542 */
1543static int smack_file_ioctl(struct file *file, unsigned int cmd,
1544 unsigned long arg)
1545{
1546 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001547 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001548 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001549
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001550 if (unlikely(IS_PRIVATE(inode)))
1551 return 0;
1552
Eric Parisf48b7392011-04-25 12:54:27 -04001553 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001554 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001555
Casey Schauflerd166c802014-08-27 14:51:27 -07001556 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001557 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001558 rc = smk_bu_file(file, MAY_WRITE, rc);
1559 }
Casey Schauflere114e472008-02-04 22:29:50 -08001560
Casey Schauflerd166c802014-08-27 14:51:27 -07001561 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001562 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001563 rc = smk_bu_file(file, MAY_READ, rc);
1564 }
Casey Schauflere114e472008-02-04 22:29:50 -08001565
1566 return rc;
1567}
1568
1569/**
1570 * smack_file_lock - Smack check on file locking
1571 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001572 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001573 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001574 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001575 */
1576static int smack_file_lock(struct file *file, unsigned int cmd)
1577{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001578 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001579 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001580 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001581
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001582 if (unlikely(IS_PRIVATE(inode)))
1583 return 0;
1584
Eric Paris92f42502011-04-25 13:15:55 -04001585 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1586 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001587 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001588 rc = smk_bu_file(file, MAY_LOCK, rc);
1589 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001590}
1591
1592/**
1593 * smack_file_fcntl - Smack check on fcntl
1594 * @file: the object
1595 * @cmd: what action to check
1596 * @arg: unused
1597 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001598 * Generally these operations are harmless.
1599 * File locking operations present an obvious mechanism
1600 * for passing information, so they require write access.
1601 *
Casey Schauflere114e472008-02-04 22:29:50 -08001602 * Returns 0 if current has access, error code otherwise
1603 */
1604static int smack_file_fcntl(struct file *file, unsigned int cmd,
1605 unsigned long arg)
1606{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001607 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001608 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001609 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001610
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001611 if (unlikely(IS_PRIVATE(inode)))
1612 return 0;
1613
Casey Schauflere114e472008-02-04 22:29:50 -08001614 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001615 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001616 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001617 case F_SETLK:
1618 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001619 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1620 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001621 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001622 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001623 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001624 case F_SETOWN:
1625 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001626 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1627 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001628 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001629 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001630 break;
1631 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001632 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001633 }
1634
1635 return rc;
1636}
1637
1638/**
Al Viroe5467852012-05-30 13:30:51 -04001639 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001640 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1641 * if mapping anonymous memory.
1642 * @file contains the file structure for file to map (may be NULL).
1643 * @reqprot contains the protection requested by the application.
1644 * @prot contains the protection that will be applied by the kernel.
1645 * @flags contains the operational flags.
1646 * Return 0 if permission is granted.
1647 */
Al Viroe5467852012-05-30 13:30:51 -04001648static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001649 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001650 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001651{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001652 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001653 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001654 struct smack_rule *srp;
1655 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001656 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001657 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001658 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001659 int may;
1660 int mmay;
1661 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001662 int rc;
1663
Al Viro496ad9a2013-01-23 17:07:38 -05001664 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001665 return 0;
1666
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001667 if (unlikely(IS_PRIVATE(file_inode(file))))
1668 return 0;
1669
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001670 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001671 if (isp->smk_mmap == NULL)
1672 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001673 sbsp = file_inode(file)->i_sb->s_security;
1674 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1675 isp->smk_mmap != sbsp->smk_root)
1676 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001677 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001678
Casey Schauflerb17103a2018-11-09 16:12:56 -08001679 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001680 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001681 rc = 0;
1682
1683 rcu_read_lock();
1684 /*
1685 * For each Smack rule associated with the subject
1686 * label verify that the SMACK64MMAP also has access
1687 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001688 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001689 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001690 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001691 /*
1692 * Matching labels always allows access.
1693 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001694 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001695 continue;
1696 /*
1697 * If there is a matching local rule take
1698 * that into account as well.
1699 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001700 may = smk_access_entry(srp->smk_subject->smk_known,
1701 okp->smk_known,
1702 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001703 if (may == -ENOENT)
1704 may = srp->smk_access;
1705 else
1706 may &= srp->smk_access;
1707 /*
1708 * If may is zero the SMACK64MMAP subject can't
1709 * possibly have less access.
1710 */
1711 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001712 continue;
1713
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001714 /*
1715 * Fetch the global list entry.
1716 * If there isn't one a SMACK64MMAP subject
1717 * can't have as much access as current.
1718 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001719 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1720 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001721 if (mmay == -ENOENT) {
1722 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001723 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001724 }
1725 /*
1726 * If there is a local entry it modifies the
1727 * potential access, too.
1728 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001729 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1730 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001731 if (tmay != -ENOENT)
1732 mmay &= tmay;
1733
1734 /*
1735 * If there is any access available to current that is
1736 * not available to a SMACK64MMAP subject
1737 * deny access.
1738 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001739 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001740 rc = -EACCES;
1741 break;
1742 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001743 }
1744
1745 rcu_read_unlock();
1746
1747 return rc;
1748}
1749
1750/**
Casey Schauflere114e472008-02-04 22:29:50 -08001751 * smack_file_set_fowner - set the file security blob value
1752 * @file: object in question
1753 *
Casey Schauflere114e472008-02-04 22:29:50 -08001754 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001755static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001756{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001757 struct smack_known **blob = smack_file(file);
1758
1759 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001760}
1761
1762/**
1763 * smack_file_send_sigiotask - Smack on sigio
1764 * @tsk: The target task
1765 * @fown: the object the signal come from
1766 * @signum: unused
1767 *
1768 * Allow a privileged task to get signals even if it shouldn't
1769 *
1770 * Returns 0 if a subject with the object's smack could
1771 * write to the task, an error code otherwise.
1772 */
1773static int smack_file_send_sigiotask(struct task_struct *tsk,
1774 struct fown_struct *fown, int signum)
1775{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001776 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001777 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001778 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001779 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001780 struct file *file;
1781 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001782 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001783
1784 /*
1785 * struct fown_struct is never outside the context of a struct file
1786 */
1787 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001788
Etienne Bassetecfcc532009-04-08 20:40:06 +02001789 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001790 blob = smack_file(file);
1791 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001792 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1793 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001794
1795 rcu_read_lock();
1796 tcred = __task_cred(tsk);
1797 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001798 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001799 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001800
1801 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1802 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001803 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001804 return rc;
1805}
1806
1807/**
1808 * smack_file_receive - Smack file receive check
1809 * @file: the object
1810 *
1811 * Returns 0 if current has access, error code otherwise
1812 */
1813static int smack_file_receive(struct file *file)
1814{
Casey Schauflerd166c802014-08-27 14:51:27 -07001815 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001816 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001817 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001818 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001819 struct socket *sock;
1820 struct task_smack *tsp;
1821 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001822
Seung-Woo Kim97775822015-04-17 15:25:04 +09001823 if (unlikely(IS_PRIVATE(inode)))
1824 return 0;
1825
Casey Schaufler4482a442013-12-30 17:37:45 -08001826 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001827 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001828
Casey Schaufler51d59af2017-05-31 08:53:42 -07001829 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001830 sock = SOCKET_I(inode);
1831 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001832 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001833 /*
1834 * If the receiving process can't write to the
1835 * passed socket or if the passed socket can't
1836 * write to the receiving process don't accept
1837 * the passed socket.
1838 */
1839 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1840 rc = smk_bu_file(file, may, rc);
1841 if (rc < 0)
1842 return rc;
1843 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1844 rc = smk_bu_file(file, may, rc);
1845 return rc;
1846 }
Casey Schauflere114e472008-02-04 22:29:50 -08001847 /*
1848 * This code relies on bitmasks.
1849 */
1850 if (file->f_mode & FMODE_READ)
1851 may = MAY_READ;
1852 if (file->f_mode & FMODE_WRITE)
1853 may |= MAY_WRITE;
1854
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001855 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001856 rc = smk_bu_file(file, may, rc);
1857 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001858}
1859
Casey Schaufler531f1d42011-09-19 12:41:42 -07001860/**
Eric Paris83d49852012-04-04 13:45:40 -04001861 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001862 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001863 *
1864 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001865 * Allow the open only if the task has read access. There are
1866 * many read operations (e.g. fstat) that you can do with an
1867 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001868 *
luanshia1a07f22019-07-05 10:35:20 +08001869 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001870 */
Al Viro94817692018-07-10 14:13:18 -04001871static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001872{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001873 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001874 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001875 struct smk_audit_info ad;
1876 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001877
Casey Schauflera6834c02014-04-21 11:10:26 -07001878 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1879 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301880 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001881 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001882
1883 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001884}
1885
Casey Schauflere114e472008-02-04 22:29:50 -08001886/*
1887 * Task hooks
1888 */
1889
1890/**
David Howellsee18d642009-09-02 09:14:21 +01001891 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001892 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001893 * @gfp: the atomicity of any memory allocations
1894 *
1895 * Prepare a blank set of credentials for modification. This must allocate all
1896 * the memory the LSM module might require such that cred_transfer() can
1897 * complete without error.
1898 */
1899static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1900{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001901 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001902 return 0;
1903}
1904
1905
1906/**
David Howellsf1752ee2008-11-14 10:39:17 +11001907 * smack_cred_free - "free" task-level security credentials
1908 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001909 *
Casey Schauflere114e472008-02-04 22:29:50 -08001910 */
David Howellsf1752ee2008-11-14 10:39:17 +11001911static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001912{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001913 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001914 struct smack_rule *rp;
1915 struct list_head *l;
1916 struct list_head *n;
1917
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001918 smk_destroy_label_list(&tsp->smk_relabel);
1919
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001920 list_for_each_safe(l, n, &tsp->smk_rules) {
1921 rp = list_entry(l, struct smack_rule, list);
1922 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001923 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001924 }
Casey Schauflere114e472008-02-04 22:29:50 -08001925}
1926
1927/**
David Howellsd84f4f92008-11-14 10:39:23 +11001928 * smack_cred_prepare - prepare new set of credentials for modification
1929 * @new: the new credentials
1930 * @old: the original credentials
1931 * @gfp: the atomicity of any memory allocations
1932 *
1933 * Prepare a new set of credentials for modification.
1934 */
1935static int smack_cred_prepare(struct cred *new, const struct cred *old,
1936 gfp_t gfp)
1937{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001938 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001939 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001940 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001941
Casey Schauflerbbd36622018-11-12 09:30:56 -08001942 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301943
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001944 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1945 if (rc != 0)
1946 return rc;
1947
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001948 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1949 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001950 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001951}
1952
Randy Dunlap251a2a92009-02-18 11:42:33 -08001953/**
David Howellsee18d642009-09-02 09:14:21 +01001954 * smack_cred_transfer - Transfer the old credentials to the new credentials
1955 * @new: the new credentials
1956 * @old: the original credentials
1957 *
1958 * Fill in a set of blank credentials from another set of credentials.
1959 */
1960static void smack_cred_transfer(struct cred *new, const struct cred *old)
1961{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001962 struct task_smack *old_tsp = smack_cred(old);
1963 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001964
1965 new_tsp->smk_task = old_tsp->smk_task;
1966 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001967 mutex_init(&new_tsp->smk_rules_lock);
1968 INIT_LIST_HEAD(&new_tsp->smk_rules);
1969
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001970 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001971}
1972
1973/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001974 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001975 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001976 * @secid: where to put the result
1977 *
1978 * Sets the secid to contain a u32 version of the smack label.
1979 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001980static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001981{
1982 struct smack_known *skp;
1983
1984 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001985 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001986 *secid = skp->smk_secid;
1987 rcu_read_unlock();
1988}
1989
1990/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001991 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001992 * @new: points to the set of credentials to be modified.
1993 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001994 *
1995 * Set the security data for a kernel service.
1996 */
1997static int smack_kernel_act_as(struct cred *new, u32 secid)
1998{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001999 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002000
Casey Schaufler152f91d2016-11-14 09:38:15 -08002001 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002002 return 0;
2003}
2004
2005/**
2006 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002007 * @new: points to the set of credentials to be modified
2008 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002009 *
2010 * Set the file creation context in a set of credentials to the same
2011 * as the objective context of the specified inode
2012 */
2013static int smack_kernel_create_files_as(struct cred *new,
2014 struct inode *inode)
2015{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002016 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002017 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002018
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002019 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002020 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002021 return 0;
2022}
2023
2024/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002025 * smk_curacc_on_task - helper to log task related access
2026 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002027 * @access: the access requested
2028 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002029 *
2030 * Return 0 if access is permitted
2031 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002032static int smk_curacc_on_task(struct task_struct *p, int access,
2033 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002034{
2035 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002036 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002037 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002038
Casey Schaufler531f1d42011-09-19 12:41:42 -07002039 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002040 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002041 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002042 rc = smk_bu_task(p, access, rc);
2043 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002044}
2045
2046/**
Casey Schauflere114e472008-02-04 22:29:50 -08002047 * smack_task_setpgid - Smack check on setting pgid
2048 * @p: the task object
2049 * @pgid: unused
2050 *
2051 * Return 0 if write access is permitted
2052 */
2053static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2054{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002055 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002056}
2057
2058/**
2059 * smack_task_getpgid - Smack access check for getpgid
2060 * @p: the object task
2061 *
2062 * Returns 0 if current can read the object task, error code otherwise
2063 */
2064static int smack_task_getpgid(struct task_struct *p)
2065{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002066 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002067}
2068
2069/**
2070 * smack_task_getsid - Smack access check for getsid
2071 * @p: the object task
2072 *
2073 * Returns 0 if current can read the object task, error code otherwise
2074 */
2075static int smack_task_getsid(struct task_struct *p)
2076{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002077 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002078}
2079
2080/**
2081 * smack_task_getsecid - get the secid of the task
2082 * @p: the object task
2083 * @secid: where to put the result
2084 *
2085 * Sets the secid to contain a u32 version of the smack label.
2086 */
2087static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2088{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002089 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002090
2091 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002092}
2093
2094/**
2095 * smack_task_setnice - Smack check on setting nice
2096 * @p: the task object
2097 * @nice: unused
2098 *
2099 * Return 0 if write access is permitted
2100 */
2101static int smack_task_setnice(struct task_struct *p, int nice)
2102{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002103 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002104}
2105
2106/**
2107 * smack_task_setioprio - Smack check on setting ioprio
2108 * @p: the task object
2109 * @ioprio: unused
2110 *
2111 * Return 0 if write access is permitted
2112 */
2113static int smack_task_setioprio(struct task_struct *p, int ioprio)
2114{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002115 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002116}
2117
2118/**
2119 * smack_task_getioprio - Smack check on reading ioprio
2120 * @p: the task object
2121 *
2122 * Return 0 if read access is permitted
2123 */
2124static int smack_task_getioprio(struct task_struct *p)
2125{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002126 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002127}
2128
2129/**
2130 * smack_task_setscheduler - Smack check on setting scheduler
2131 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002132 *
2133 * Return 0 if read access is permitted
2134 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002135static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002136{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002137 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002138}
2139
2140/**
2141 * smack_task_getscheduler - Smack check on reading scheduler
2142 * @p: the task object
2143 *
2144 * Return 0 if read access is permitted
2145 */
2146static int smack_task_getscheduler(struct task_struct *p)
2147{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002148 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002149}
2150
2151/**
2152 * smack_task_movememory - Smack check on moving memory
2153 * @p: the task object
2154 *
2155 * Return 0 if write access is permitted
2156 */
2157static int smack_task_movememory(struct task_struct *p)
2158{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002159 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002160}
2161
2162/**
2163 * smack_task_kill - Smack check on signal delivery
2164 * @p: the task object
2165 * @info: unused
2166 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002167 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002168 *
2169 * Return 0 if write access is permitted
2170 *
Casey Schauflere114e472008-02-04 22:29:50 -08002171 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002172static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002173 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002174{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002175 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002176 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002177 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002178 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002179
Rafal Krypa18d872f2016-04-04 11:14:53 +02002180 if (!sig)
2181 return 0; /* null signal; existence test */
2182
Etienne Bassetecfcc532009-04-08 20:40:06 +02002183 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2184 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002185 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002186 * Sending a signal requires that the sender
2187 * can write the receiver.
2188 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002189 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002190 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2191 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002192 return rc;
2193 }
Casey Schauflere114e472008-02-04 22:29:50 -08002194 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002195 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002196 * specific behavior. This is not clean. For one thing
2197 * we can't take privilege into account.
2198 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002199 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002200 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2201 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002202 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002203}
2204
2205/**
Casey Schauflere114e472008-02-04 22:29:50 -08002206 * smack_task_to_inode - copy task smack into the inode blob
2207 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002208 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002209 *
2210 * Sets the smack pointer in the inode security blob
2211 */
2212static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2213{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002214 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002215 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002216
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002217 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002218 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002219}
2220
2221/*
2222 * Socket hooks.
2223 */
2224
2225/**
2226 * smack_sk_alloc_security - Allocate a socket blob
2227 * @sk: the socket
2228 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002229 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002230 *
2231 * Assign Smack pointers to current
2232 *
2233 * Returns 0 on success, -ENOMEM is there's no memory
2234 */
2235static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2236{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002237 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002238 struct socket_smack *ssp;
2239
2240 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2241 if (ssp == NULL)
2242 return -ENOMEM;
2243
jooseong lee08382c92016-11-03 11:54:39 +01002244 /*
2245 * Sockets created by kernel threads receive web label.
2246 */
2247 if (unlikely(current->flags & PF_KTHREAD)) {
2248 ssp->smk_in = &smack_known_web;
2249 ssp->smk_out = &smack_known_web;
2250 } else {
2251 ssp->smk_in = skp;
2252 ssp->smk_out = skp;
2253 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002254 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002255
2256 sk->sk_security = ssp;
2257
2258 return 0;
2259}
2260
2261/**
2262 * smack_sk_free_security - Free a socket blob
2263 * @sk: the socket
2264 *
2265 * Clears the blob pointer
2266 */
2267static void smack_sk_free_security(struct sock *sk)
2268{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302269#ifdef SMACK_IPV6_PORT_LABELING
2270 struct smk_port_label *spp;
2271
2272 if (sk->sk_family == PF_INET6) {
2273 rcu_read_lock();
2274 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2275 if (spp->smk_sock != sk)
2276 continue;
2277 spp->smk_can_reuse = 1;
2278 break;
2279 }
2280 rcu_read_unlock();
2281 }
2282#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002283 kfree(sk->sk_security);
2284}
2285
2286/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002287* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002288* @sip: the object end
2289*
2290* looks for host based access restrictions
2291*
2292* This version will only be appropriate for really small sets of single label
2293* hosts. The caller is responsible for ensuring that the RCU read lock is
2294* taken before calling this function.
2295*
2296* Returns the label of the far end or NULL if it's not special.
2297*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002298static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002299{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002300 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002301 struct in_addr *siap = &sip->sin_addr;
2302
2303 if (siap->s_addr == 0)
2304 return NULL;
2305
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002306 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2307 /*
2308 * we break after finding the first match because
2309 * the list is sorted from longest to shortest mask
2310 * so we have found the most specific match
2311 */
2312 if (snp->smk_host.s_addr ==
2313 (siap->s_addr & snp->smk_mask.s_addr))
2314 return snp->smk_label;
2315
2316 return NULL;
2317}
2318
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002319/*
2320 * smk_ipv6_localhost - Check for local ipv6 host address
2321 * @sip: the address
2322 *
2323 * Returns boolean true if this is the localhost address
2324 */
2325static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2326{
2327 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2328 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2329
2330 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2331 ntohs(be16p[7]) == 1)
2332 return true;
2333 return false;
2334}
2335
2336/**
2337* smack_ipv6host_label - check host based restrictions
2338* @sip: the object end
2339*
2340* looks for host based access restrictions
2341*
2342* This version will only be appropriate for really small sets of single label
2343* hosts. The caller is responsible for ensuring that the RCU read lock is
2344* taken before calling this function.
2345*
2346* Returns the label of the far end or NULL if it's not special.
2347*/
2348static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2349{
2350 struct smk_net6addr *snp;
2351 struct in6_addr *sap = &sip->sin6_addr;
2352 int i;
2353 int found = 0;
2354
2355 /*
2356 * It's local. Don't look for a host label.
2357 */
2358 if (smk_ipv6_localhost(sip))
2359 return NULL;
2360
2361 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002362 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002363 * If the label is NULL the entry has
2364 * been renounced. Ignore it.
2365 */
2366 if (snp->smk_label == NULL)
2367 continue;
2368 /*
Paul Moore07feee82009-03-27 17:10:54 -04002369 * we break after finding the first match because
2370 * the list is sorted from longest to shortest mask
2371 * so we have found the most specific match
2372 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002373 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002374 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2375 snp->smk_host.s6_addr16[i]) {
2376 found = 0;
2377 break;
2378 }
Etienne Basset43031542009-03-27 17:11:01 -04002379 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002380 if (found)
2381 return snp->smk_label;
2382 }
Paul Moore07feee82009-03-27 17:10:54 -04002383
2384 return NULL;
2385}
2386
2387/**
Casey Schauflere114e472008-02-04 22:29:50 -08002388 * smack_netlabel - Set the secattr on a socket
2389 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002390 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002391 *
2392 * Convert the outbound smack value (smk_out) to a
2393 * secattr and attach it to the socket.
2394 *
2395 * Returns 0 on success or an error code
2396 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002397static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002398{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002399 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002400 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002401 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002402
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002403 /*
2404 * Usually the netlabel code will handle changing the
2405 * packet labeling based on the label.
2406 * The case of a single label host is different, because
2407 * a single label host should never get a labeled packet
2408 * even though the label is usually associated with a packet
2409 * label.
2410 */
2411 local_bh_disable();
2412 bh_lock_sock_nested(sk);
2413
2414 if (ssp->smk_out == smack_net_ambient ||
2415 labeled == SMACK_UNLABELED_SOCKET)
2416 netlbl_sock_delattr(sk);
2417 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002418 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002419 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002420 }
2421
2422 bh_unlock_sock(sk);
2423 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002424
Casey Schauflere114e472008-02-04 22:29:50 -08002425 return rc;
2426}
2427
2428/**
Paul Moore07feee82009-03-27 17:10:54 -04002429 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2430 * @sk: the socket
2431 * @sap: the destination address
2432 *
2433 * Set the correct secattr for the given socket based on the destination
2434 * address and perform any outbound access checks needed.
2435 *
2436 * Returns 0 on success or an error code.
2437 *
2438 */
2439static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2440{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002441 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002442 int rc;
2443 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002444 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002445 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002446 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002447
2448 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002449 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002450 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002451#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002452 struct lsm_network_audit net;
2453
Eric Paris48c62af2012-04-02 13:15:44 -04002454 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2455 ad.a.u.net->family = sap->sin_family;
2456 ad.a.u.net->dport = sap->sin_port;
2457 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002458#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002459 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002460 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002461 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2462 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002463 } else {
2464 sk_lbl = SMACK_CIPSO_SOCKET;
2465 rc = 0;
2466 }
2467 rcu_read_unlock();
2468 if (rc != 0)
2469 return rc;
2470
2471 return smack_netlabel(sk, sk_lbl);
2472}
2473
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002474/**
2475 * smk_ipv6_check - check Smack access
2476 * @subject: subject Smack label
2477 * @object: object Smack label
2478 * @address: address
2479 * @act: the action being taken
2480 *
2481 * Check an IPv6 access
2482 */
2483static int smk_ipv6_check(struct smack_known *subject,
2484 struct smack_known *object,
2485 struct sockaddr_in6 *address, int act)
2486{
2487#ifdef CONFIG_AUDIT
2488 struct lsm_network_audit net;
2489#endif
2490 struct smk_audit_info ad;
2491 int rc;
2492
2493#ifdef CONFIG_AUDIT
2494 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2495 ad.a.u.net->family = PF_INET6;
2496 ad.a.u.net->dport = ntohs(address->sin6_port);
2497 if (act == SMK_RECEIVING)
2498 ad.a.u.net->v6info.saddr = address->sin6_addr;
2499 else
2500 ad.a.u.net->v6info.daddr = address->sin6_addr;
2501#endif
2502 rc = smk_access(subject, object, MAY_WRITE, &ad);
2503 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2504 return rc;
2505}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002506
2507#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002508/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002509 * smk_ipv6_port_label - Smack port access table management
2510 * @sock: socket
2511 * @address: address
2512 *
2513 * Create or update the port list entry
2514 */
2515static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2516{
2517 struct sock *sk = sock->sk;
2518 struct sockaddr_in6 *addr6;
2519 struct socket_smack *ssp = sock->sk->sk_security;
2520 struct smk_port_label *spp;
2521 unsigned short port = 0;
2522
2523 if (address == NULL) {
2524 /*
2525 * This operation is changing the Smack information
2526 * on the bound socket. Take the changes to the port
2527 * as well.
2528 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302529 rcu_read_lock();
2530 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002531 if (sk != spp->smk_sock)
2532 continue;
2533 spp->smk_in = ssp->smk_in;
2534 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302535 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002536 return;
2537 }
2538 /*
2539 * A NULL address is only used for updating existing
2540 * bound entries. If there isn't one, it's OK.
2541 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302542 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002543 return;
2544 }
2545
2546 addr6 = (struct sockaddr_in6 *)address;
2547 port = ntohs(addr6->sin6_port);
2548 /*
2549 * This is a special case that is safely ignored.
2550 */
2551 if (port == 0)
2552 return;
2553
2554 /*
2555 * Look for an existing port list entry.
2556 * This is an indication that a port is getting reused.
2557 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302558 rcu_read_lock();
2559 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302560 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002561 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302562 if (spp->smk_can_reuse != 1) {
2563 rcu_read_unlock();
2564 return;
2565 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002566 spp->smk_port = port;
2567 spp->smk_sock = sk;
2568 spp->smk_in = ssp->smk_in;
2569 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302570 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302571 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002572 return;
2573 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302574 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002575 /*
2576 * A new port entry is required.
2577 */
2578 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2579 if (spp == NULL)
2580 return;
2581
2582 spp->smk_port = port;
2583 spp->smk_sock = sk;
2584 spp->smk_in = ssp->smk_in;
2585 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302586 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302587 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002588
Vishal Goel3c7ce342016-11-23 10:31:08 +05302589 mutex_lock(&smack_ipv6_lock);
2590 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2591 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002592 return;
2593}
Arnd Bergmann00720f02020-04-08 21:04:31 +02002594#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002595
2596/**
2597 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002598 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002599 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002600 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002601 *
2602 * Create or update the port list entry
2603 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002604static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002605 int act)
2606{
Casey Schauflerc6739442013-05-22 18:42:56 -07002607 struct smk_port_label *spp;
2608 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002609 struct smack_known *skp = NULL;
2610 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002611 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002612
2613 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002614 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002615 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002616 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002617 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002618 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002619 }
2620
2621 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002622 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002624 if (skp != NULL && object != NULL)
2625 return smk_ipv6_check(skp, object, address, act);
2626 if (skp == NULL)
2627 skp = smack_net_ambient;
2628 if (object == NULL)
2629 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002630
2631 /*
2632 * It's remote, so port lookup does no good.
2633 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002634 if (!smk_ipv6_localhost(address))
2635 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002636
2637 /*
2638 * It's local so the send check has to have passed.
2639 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002640 if (act == SMK_RECEIVING)
2641 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002642
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002643 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302644 rcu_read_lock();
2645 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302646 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002647 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002648 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002649 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002650 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002651 break;
2652 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302653 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002654
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002655 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002656}
2657
2658/**
Casey Schauflere114e472008-02-04 22:29:50 -08002659 * smack_inode_setsecurity - set smack xattrs
2660 * @inode: the object
2661 * @name: attribute name
2662 * @value: attribute value
2663 * @size: size of the attribute
2664 * @flags: unused
2665 *
2666 * Sets the named attribute in the appropriate blob
2667 *
2668 * Returns 0 on success, or an error code
2669 */
2670static int smack_inode_setsecurity(struct inode *inode, const char *name,
2671 const void *value, size_t size, int flags)
2672{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002673 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002674 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002675 struct socket_smack *ssp;
2676 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002677 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002678
Casey Schauflerf7112e62012-05-06 15:22:02 -07002679 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302680 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002681
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002682 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002683 if (IS_ERR(skp))
2684 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002685
2686 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002687 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002688 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002689 return 0;
2690 }
2691 /*
2692 * The rest of the Smack xattrs are only on sockets.
2693 */
2694 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2695 return -EOPNOTSUPP;
2696
2697 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002698 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002699 return -EOPNOTSUPP;
2700
2701 ssp = sock->sk->sk_security;
2702
2703 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002704 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002705 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002706 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002707 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002708 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2709 if (rc != 0)
2710 printk(KERN_WARNING
2711 "Smack: \"%s\" netlbl error %d.\n",
2712 __func__, -rc);
2713 }
Casey Schauflere114e472008-02-04 22:29:50 -08002714 } else
2715 return -EOPNOTSUPP;
2716
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002717#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002718 if (sock->sk->sk_family == PF_INET6)
2719 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002720#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002721
Casey Schauflere114e472008-02-04 22:29:50 -08002722 return 0;
2723}
2724
2725/**
2726 * smack_socket_post_create - finish socket setup
2727 * @sock: the socket
2728 * @family: protocol family
2729 * @type: unused
2730 * @protocol: unused
2731 * @kern: unused
2732 *
2733 * Sets the netlabel information on the socket
2734 *
2735 * Returns 0 on success, and error code otherwise
2736 */
2737static int smack_socket_post_create(struct socket *sock, int family,
2738 int type, int protocol, int kern)
2739{
Marcin Lis74123012015-01-22 15:40:33 +01002740 struct socket_smack *ssp;
2741
2742 if (sock->sk == NULL)
2743 return 0;
2744
2745 /*
2746 * Sockets created by kernel threads receive web label.
2747 */
2748 if (unlikely(current->flags & PF_KTHREAD)) {
2749 ssp = sock->sk->sk_security;
2750 ssp->smk_in = &smack_known_web;
2751 ssp->smk_out = &smack_known_web;
2752 }
2753
2754 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002755 return 0;
2756 /*
2757 * Set the outbound netlbl.
2758 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002759 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2760}
2761
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002762/**
2763 * smack_socket_socketpair - create socket pair
2764 * @socka: one socket
2765 * @sockb: another socket
2766 *
2767 * Cross reference the peer labels for SO_PEERSEC
2768 *
luanshia1a07f22019-07-05 10:35:20 +08002769 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002770 */
2771static int smack_socket_socketpair(struct socket *socka,
2772 struct socket *sockb)
2773{
2774 struct socket_smack *asp = socka->sk->sk_security;
2775 struct socket_smack *bsp = sockb->sk->sk_security;
2776
2777 asp->smk_packet = bsp->smk_out;
2778 bsp->smk_packet = asp->smk_out;
2779
2780 return 0;
2781}
2782
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002783#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002784/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002785 * smack_socket_bind - record port binding information.
2786 * @sock: the socket
2787 * @address: the port address
2788 * @addrlen: size of the address
2789 *
2790 * Records the label bound to a port.
2791 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002792 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002793 */
2794static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2795 int addrlen)
2796{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002797 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2798 if (addrlen < SIN6_LEN_RFC2133 ||
2799 address->sa_family != AF_INET6)
2800 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002801 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002802 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002803 return 0;
2804}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002805#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002806
2807/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002808 * smack_socket_connect - connect access check
2809 * @sock: the socket
2810 * @sap: the other end
2811 * @addrlen: size of sap
2812 *
2813 * Verifies that a connection may be possible
2814 *
2815 * Returns 0 on success, and error code otherwise
2816 */
2817static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2818 int addrlen)
2819{
Casey Schauflerc6739442013-05-22 18:42:56 -07002820 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002821
Casey Schauflerc6739442013-05-22 18:42:56 -07002822 if (sock->sk == NULL)
2823 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002824 if (sock->sk->sk_family != PF_INET &&
2825 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2826 return 0;
2827 if (addrlen < offsetofend(struct sockaddr, sa_family))
2828 return 0;
2829 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2830 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002831 struct smack_known *rsp = NULL;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002832
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002833 if (addrlen < SIN6_LEN_RFC2133)
2834 return 0;
Arnd Bergmann00720f02020-04-08 21:04:31 +02002835 if (__is_defined(SMACK_IPV6_SECMARK_LABELING))
2836 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002837 if (rsp != NULL) {
2838 struct socket_smack *ssp = sock->sk->sk_security;
2839
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002840 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002841 SMK_CONNECTING);
2842 }
Arnd Bergmann00720f02020-04-08 21:04:31 +02002843 if (__is_defined(SMACK_IPV6_PORT_LABELING))
2844 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2845
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002846 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002847 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002848 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2849 return 0;
2850 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002851 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002852}
2853
2854/**
2855 * smack_flags_to_may - convert S_ to MAY_ values
2856 * @flags: the S_ value
2857 *
2858 * Returns the equivalent MAY_ value
2859 */
2860static int smack_flags_to_may(int flags)
2861{
2862 int may = 0;
2863
2864 if (flags & S_IRUGO)
2865 may |= MAY_READ;
2866 if (flags & S_IWUGO)
2867 may |= MAY_WRITE;
2868 if (flags & S_IXUGO)
2869 may |= MAY_EXEC;
2870
2871 return may;
2872}
2873
2874/**
2875 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2876 * @msg: the object
2877 *
2878 * Returns 0
2879 */
2880static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2881{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002882 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002883
Casey Schauflerecd5f822018-11-20 11:55:02 -08002884 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002885 return 0;
2886}
2887
2888/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002889 * smack_of_ipc - the smack pointer for the ipc
2890 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002891 *
2892 * Returns a pointer to the smack value
2893 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002894static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002895{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002896 struct smack_known **blob = smack_ipc(isp);
2897
2898 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002899}
2900
2901/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002902 * smack_ipc_alloc_security - Set the security blob for ipc
2903 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002904 *
2905 * Returns 0
2906 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002907static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002908{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002909 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002910
Casey Schaufler019bcca2018-09-21 17:19:54 -07002911 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002912 return 0;
2913}
2914
2915/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002916 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002917 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002918 * @access : access requested
2919 *
2920 * Returns 0 if current has the requested access, error code otherwise
2921 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002922static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002923{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002924 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002925 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002926 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002927
2928#ifdef CONFIG_AUDIT
2929 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002930 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002931#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002932 rc = smk_curacc(ssp, access, &ad);
2933 rc = smk_bu_current("shm", ssp, access, rc);
2934 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002935}
2936
2937/**
Casey Schauflere114e472008-02-04 22:29:50 -08002938 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002939 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002940 * @shmflg: access requested
2941 *
2942 * Returns 0 if current has the requested access, error code otherwise
2943 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002944static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002945{
Casey Schauflere114e472008-02-04 22:29:50 -08002946 int may;
2947
2948 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002949 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002950}
2951
2952/**
2953 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002954 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002955 * @cmd: what it wants to do
2956 *
2957 * Returns 0 if current has the requested access, error code otherwise
2958 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002959static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002960{
Casey Schauflere114e472008-02-04 22:29:50 -08002961 int may;
2962
2963 switch (cmd) {
2964 case IPC_STAT:
2965 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002966 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002967 may = MAY_READ;
2968 break;
2969 case IPC_SET:
2970 case SHM_LOCK:
2971 case SHM_UNLOCK:
2972 case IPC_RMID:
2973 may = MAY_READWRITE;
2974 break;
2975 case IPC_INFO:
2976 case SHM_INFO:
2977 /*
2978 * System level information.
2979 */
2980 return 0;
2981 default:
2982 return -EINVAL;
2983 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002984 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002985}
2986
2987/**
2988 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002989 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002990 * @shmaddr: unused
2991 * @shmflg: access requested
2992 *
2993 * Returns 0 if current has the requested access, error code otherwise
2994 */
luanshia1a07f22019-07-05 10:35:20 +08002995static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08002996 int shmflg)
2997{
Casey Schauflere114e472008-02-04 22:29:50 -08002998 int may;
2999
3000 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003001 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003002}
3003
3004/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003005 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003006 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003007 * @access : access requested
3008 *
3009 * Returns 0 if current has the requested access, error code otherwise
3010 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003011static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003012{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003013 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003014 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003015 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003016
3017#ifdef CONFIG_AUDIT
3018 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003019 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003020#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003021 rc = smk_curacc(ssp, access, &ad);
3022 rc = smk_bu_current("sem", ssp, access, rc);
3023 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003024}
3025
3026/**
Casey Schauflere114e472008-02-04 22:29:50 -08003027 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003028 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003029 * @semflg: access requested
3030 *
3031 * Returns 0 if current has the requested access, error code otherwise
3032 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003033static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003034{
Casey Schauflere114e472008-02-04 22:29:50 -08003035 int may;
3036
3037 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003038 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003039}
3040
3041/**
3042 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003043 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003044 * @cmd: what it wants to do
3045 *
3046 * Returns 0 if current has the requested access, error code otherwise
3047 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003048static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003049{
Casey Schauflere114e472008-02-04 22:29:50 -08003050 int may;
3051
3052 switch (cmd) {
3053 case GETPID:
3054 case GETNCNT:
3055 case GETZCNT:
3056 case GETVAL:
3057 case GETALL:
3058 case IPC_STAT:
3059 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003060 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003061 may = MAY_READ;
3062 break;
3063 case SETVAL:
3064 case SETALL:
3065 case IPC_RMID:
3066 case IPC_SET:
3067 may = MAY_READWRITE;
3068 break;
3069 case IPC_INFO:
3070 case SEM_INFO:
3071 /*
3072 * System level information
3073 */
3074 return 0;
3075 default:
3076 return -EINVAL;
3077 }
3078
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_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003084 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003085 * @sops: unused
3086 * @nsops: unused
3087 * @alter: unused
3088 *
3089 * Treated as read and write in all cases.
3090 *
3091 * Returns 0 if access is allowed, error code otherwise
3092 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003093static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003094 unsigned nsops, int alter)
3095{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003096 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003097}
3098
3099/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003100 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003101 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003102 * @access : access requested
3103 *
3104 * return 0 if current has access, error otherwise
3105 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003106static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003107{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003108 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003109 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003110 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003111
3112#ifdef CONFIG_AUDIT
3113 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003114 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003115#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003116 rc = smk_curacc(msp, access, &ad);
3117 rc = smk_bu_current("msq", msp, access, rc);
3118 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003119}
3120
3121/**
Casey Schauflere114e472008-02-04 22:29:50 -08003122 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003123 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003124 * @msqflg: access requested
3125 *
3126 * Returns 0 if current has the requested access, error code otherwise
3127 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003128static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003129{
Casey Schauflere114e472008-02-04 22:29:50 -08003130 int may;
3131
3132 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003133 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003134}
3135
3136/**
3137 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003138 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003139 * @cmd: what it wants to do
3140 *
3141 * Returns 0 if current has the requested access, error code otherwise
3142 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003143static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003144{
Casey Schauflere114e472008-02-04 22:29:50 -08003145 int may;
3146
3147 switch (cmd) {
3148 case IPC_STAT:
3149 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003150 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003151 may = MAY_READ;
3152 break;
3153 case IPC_SET:
3154 case IPC_RMID:
3155 may = MAY_READWRITE;
3156 break;
3157 case IPC_INFO:
3158 case MSG_INFO:
3159 /*
3160 * System level information
3161 */
3162 return 0;
3163 default:
3164 return -EINVAL;
3165 }
3166
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003167 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003168}
3169
3170/**
3171 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003172 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003173 * @msg: unused
3174 * @msqflg: access requested
3175 *
3176 * Returns 0 if current has the requested access, error code otherwise
3177 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003178static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003179 int msqflg)
3180{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003181 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003182
Etienne Bassetecfcc532009-04-08 20:40:06 +02003183 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003184 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003185}
3186
3187/**
3188 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003189 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003190 * @msg: unused
3191 * @target: unused
3192 * @type: unused
3193 * @mode: unused
3194 *
3195 * Returns 0 if current has read and write access, error code otherwise
3196 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003197static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003198 struct task_struct *target, long type, int mode)
3199{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003200 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003201}
3202
3203/**
3204 * smack_ipc_permission - Smack access for ipc_permission()
3205 * @ipp: the object permissions
3206 * @flag: access requested
3207 *
3208 * Returns 0 if current has read and write access, error code otherwise
3209 */
3210static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3211{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003212 struct smack_known **blob = smack_ipc(ipp);
3213 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003214 int may = smack_flags_to_may(flag);
3215 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003216 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003217
Etienne Bassetecfcc532009-04-08 20:40:06 +02003218#ifdef CONFIG_AUDIT
3219 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3220 ad.a.u.ipc_id = ipp->id;
3221#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003222 rc = smk_curacc(iskp, may, &ad);
3223 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003224 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003225}
3226
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003227/**
3228 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003229 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003230 * @secid: where result will be saved
3231 */
3232static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3233{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003234 struct smack_known **blob = smack_ipc(ipp);
3235 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003236
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003237 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003238}
3239
Casey Schauflere114e472008-02-04 22:29:50 -08003240/**
3241 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003242 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003243 * @inode: the object
3244 *
3245 * Set the inode's security blob if it hasn't been done already.
3246 */
3247static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3248{
3249 struct super_block *sbp;
3250 struct superblock_smack *sbsp;
3251 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003252 struct smack_known *skp;
3253 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003254 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003255 char trattr[TRANS_TRUE_SIZE];
3256 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003257 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003258 struct dentry *dp;
3259
3260 if (inode == NULL)
3261 return;
3262
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003263 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003264
Casey Schauflere114e472008-02-04 22:29:50 -08003265 /*
3266 * If the inode is already instantiated
3267 * take the quick way out
3268 */
3269 if (isp->smk_flags & SMK_INODE_INSTANT)
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003270 return;
Casey Schauflere114e472008-02-04 22:29:50 -08003271
3272 sbp = inode->i_sb;
3273 sbsp = sbp->s_security;
3274 /*
3275 * We're going to use the superblock default label
3276 * if there's no label on the file.
3277 */
3278 final = sbsp->smk_default;
3279
3280 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003281 * If this is the root inode the superblock
3282 * may be in the process of initialization.
3283 * If that is the case use the root value out
3284 * of the superblock.
3285 */
3286 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003287 switch (sbp->s_magic) {
3288 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003289 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003290 /*
3291 * The cgroup filesystem is never mounted,
3292 * so there's no opportunity to set the mount
3293 * options.
3294 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003295 sbsp->smk_root = &smack_known_star;
3296 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003297 isp->smk_inode = sbsp->smk_root;
3298 break;
3299 case TMPFS_MAGIC:
3300 /*
3301 * What about shmem/tmpfs anonymous files with dentry
3302 * obtained from d_alloc_pseudo()?
3303 */
3304 isp->smk_inode = smk_of_current();
3305 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003306 case PIPEFS_MAGIC:
3307 isp->smk_inode = smk_of_current();
3308 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003309 case SOCKFS_MAGIC:
3310 /*
3311 * Socket access is controlled by the socket
3312 * structures associated with the task involved.
3313 */
3314 isp->smk_inode = &smack_known_star;
3315 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003316 default:
3317 isp->smk_inode = sbsp->smk_root;
3318 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003319 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003320 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schaufler921bb1c2020-04-24 15:23:04 -07003321 return;
Casey Schauflere97dcb02008-06-02 10:04:32 -07003322 }
3323
3324 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003325 * This is pretty hackish.
3326 * Casey says that we shouldn't have to do
3327 * file system specific code, but it does help
3328 * with keeping it simple.
3329 */
3330 switch (sbp->s_magic) {
3331 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003332 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003333 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003334 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003335 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003336 * that the smack file system doesn't do
3337 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003338 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003339 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003340 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003341 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003342 break;
3343 case DEVPTS_SUPER_MAGIC:
3344 /*
3345 * devpts seems content with the label of the task.
3346 * Programs that change smack have to treat the
3347 * pty with respect.
3348 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003349 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003350 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003351 case PROC_SUPER_MAGIC:
3352 /*
3353 * Casey says procfs appears not to care.
3354 * The superblock default suffices.
3355 */
3356 break;
3357 case TMPFS_MAGIC:
3358 /*
3359 * Device labels should come from the filesystem,
3360 * but watch out, because they're volitile,
3361 * getting recreated on every reboot.
3362 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003363 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003364 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003365 * If a smack value has been set we want to use it,
3366 * but since tmpfs isn't giving us the opportunity
3367 * to set mount options simulate setting the
3368 * superblock default.
3369 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003370 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003371 default:
3372 /*
3373 * This isn't an understood special case.
3374 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003375 */
3376
3377 /*
3378 * UNIX domain sockets use lower level socket data.
3379 */
3380 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003381 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003382 break;
3383 }
3384 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003385 * No xattr support means, alas, no SMACK label.
3386 * Use the aforeapplied default.
3387 * It would be curious if the label of the task
3388 * does not match that assigned.
3389 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003390 if (!(inode->i_opflags & IOP_XATTR))
3391 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003392 /*
3393 * Get the dentry for xattr.
3394 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003395 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003396 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003397 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003398 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003399
3400 /*
3401 * Transmuting directory
3402 */
3403 if (S_ISDIR(inode->i_mode)) {
3404 /*
3405 * If this is a new directory and the label was
3406 * transmuted when the inode was initialized
3407 * set the transmute attribute on the directory
3408 * and mark the inode.
3409 *
3410 * If there is a transmute attribute on the
3411 * directory mark the inode.
3412 */
3413 if (isp->smk_flags & SMK_INODE_CHANGED) {
3414 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003415 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003416 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003417 TRANS_TRUE, TRANS_TRUE_SIZE,
3418 0);
3419 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003420 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003421 XATTR_NAME_SMACKTRANSMUTE, trattr,
3422 TRANS_TRUE_SIZE);
3423 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3424 TRANS_TRUE_SIZE) != 0)
3425 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003426 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003427 if (rc >= 0)
3428 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003429 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003430 /*
3431 * Don't let the exec or mmap label be "*" or "@".
3432 */
3433 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3434 if (IS_ERR(skp) || skp == &smack_known_star ||
3435 skp == &smack_known_web)
3436 skp = NULL;
3437 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003438
Casey Schaufler19760ad2013-12-16 16:27:26 -08003439 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003440 if (IS_ERR(skp) || skp == &smack_known_star ||
3441 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003442 skp = NULL;
3443 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003444
Casey Schauflere114e472008-02-04 22:29:50 -08003445 dput(dp);
3446 break;
3447 }
3448
3449 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003450 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003451 else
3452 isp->smk_inode = final;
3453
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003454 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003455
Casey Schauflere114e472008-02-04 22:29:50 -08003456 return;
3457}
3458
3459/**
3460 * smack_getprocattr - Smack process attribute access
3461 * @p: the object task
3462 * @name: the name of the attribute in /proc/.../attr
3463 * @value: where to put the result
3464 *
3465 * Places a copy of the task Smack into value
3466 *
3467 * Returns the length of the smack label or an error code
3468 */
3469static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3470{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003471 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003472 char *cp;
3473 int slen;
3474
3475 if (strcmp(name, "current") != 0)
3476 return -EINVAL;
3477
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003478 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003479 if (cp == NULL)
3480 return -ENOMEM;
3481
3482 slen = strlen(cp);
3483 *value = cp;
3484 return slen;
3485}
3486
3487/**
3488 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003489 * @name: the name of the attribute in /proc/.../attr
3490 * @value: the value to set
3491 * @size: the size of the value
3492 *
3493 * Sets the Smack value of the task. Only setting self
3494 * is permitted and only with privilege
3495 *
3496 * Returns the length of the smack label or an error code
3497 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003498static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003499{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003500 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003501 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003502 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003503 struct smack_known_list_elem *sklep;
3504 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003505
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003506 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003507 return -EPERM;
3508
Casey Schauflerf7112e62012-05-06 15:22:02 -07003509 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003510 return -EINVAL;
3511
3512 if (strcmp(name, "current") != 0)
3513 return -EINVAL;
3514
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003515 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003516 if (IS_ERR(skp))
3517 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003518
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003519 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303520 * No process is ever allowed the web ("@") label
3521 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003522 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303523 if (skp == &smack_known_web || skp == &smack_known_star)
3524 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003525
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003526 if (!smack_privileged(CAP_MAC_ADMIN)) {
3527 rc = -EPERM;
3528 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3529 if (sklep->smk_label == skp) {
3530 rc = 0;
3531 break;
3532 }
3533 if (rc)
3534 return rc;
3535 }
3536
David Howellsd84f4f92008-11-14 10:39:23 +11003537 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003538 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003539 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003540
Casey Schauflerb17103a2018-11-09 16:12:56 -08003541 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003542 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003543 /*
3544 * process can change its label only once
3545 */
3546 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003547
David Howellsd84f4f92008-11-14 10:39:23 +11003548 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003549 return size;
3550}
3551
3552/**
3553 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003554 * @sock: one sock
3555 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003556 * @newsk: unused
3557 *
3558 * Return 0 if a subject with the smack of sock could access
3559 * an object with the smack of other, otherwise an error code
3560 */
David S. Miller3610cda2011-01-05 15:38:53 -08003561static int smack_unix_stream_connect(struct sock *sock,
3562 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003563{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003564 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003565 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003566 struct socket_smack *ssp = sock->sk_security;
3567 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003568 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003569 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003570 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003571#ifdef CONFIG_AUDIT
3572 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003573#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003574
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003575 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3576 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003577 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003578#ifdef CONFIG_AUDIT
3579 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3580 smk_ad_setfield_u_net_sk(&ad, other);
3581#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003582 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3583 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003584 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003585 okp = osp->smk_out;
3586 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003587 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003588 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003589 MAY_WRITE, rc);
3590 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003591 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003592
Casey Schaufler975d5e52011-09-26 14:43:39 -07003593 /*
3594 * Cross reference the peer labels for SO_PEERSEC.
3595 */
3596 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003597 nsp->smk_packet = ssp->smk_out;
3598 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003599 }
3600
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003601 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003602}
3603
3604/**
3605 * smack_unix_may_send - Smack access on UDS
3606 * @sock: one socket
3607 * @other: the other socket
3608 *
3609 * Return 0 if a subject with the smack of sock could access
3610 * an object with the smack of other, otherwise an error code
3611 */
3612static int smack_unix_may_send(struct socket *sock, struct socket *other)
3613{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003614 struct socket_smack *ssp = sock->sk->sk_security;
3615 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003616 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003617 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003618
Kees Cook923e9a12012-04-10 13:26:44 -07003619#ifdef CONFIG_AUDIT
3620 struct lsm_network_audit net;
3621
Eric Paris48c62af2012-04-02 13:15:44 -04003622 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003623 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003624#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003625
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003626 if (smack_privileged(CAP_MAC_OVERRIDE))
3627 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003628
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003629 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3630 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003631 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003632}
3633
3634/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003635 * smack_socket_sendmsg - Smack check based on destination host
3636 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003637 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003638 * @size: the size of the message
3639 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003640 * Return 0 if the current subject can write to the destination host.
3641 * For IPv4 this is only a question if the destination is a single label host.
3642 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003643 */
3644static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3645 int size)
3646{
3647 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003648#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003649 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003650#endif
3651#ifdef SMACK_IPV6_SECMARK_LABELING
3652 struct socket_smack *ssp = sock->sk->sk_security;
3653 struct smack_known *rsp;
3654#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003655 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003656
3657 /*
3658 * Perfectly reasonable for this to be NULL
3659 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003660 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003661 return 0;
3662
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003663 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003664 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003665 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3666 sip->sin_family != AF_INET)
3667 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003668 rc = smack_netlabel_send(sock->sk, sip);
3669 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003670#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003671 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003672 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3673 sap->sin6_family != AF_INET6)
3674 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003675#ifdef SMACK_IPV6_SECMARK_LABELING
3676 rsp = smack_ipv6host_label(sap);
3677 if (rsp != NULL)
3678 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3679 SMK_CONNECTING);
3680#endif
3681#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003682 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003683#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003684#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003685 break;
3686 }
3687 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003688}
3689
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003690/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003691 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003692 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003693 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003694 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003695 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003696 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003697static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3698 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003699{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003700 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003701 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003702 int acat;
3703 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003704
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003705 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003706 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003707 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003708 * If there are flags but no level netlabel isn't
3709 * behaving the way we expect it to.
3710 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003711 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003712 * Without guidance regarding the smack value
3713 * for the packet fall back on the network
3714 * ambient value.
3715 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003716 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303717 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003718 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003719 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003720 /*
3721 * Compare the catsets. Use the netlbl APIs.
3722 */
3723 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3724 if ((skp->smk_netlabel.flags &
3725 NETLBL_SECATTR_MLS_CAT) == 0)
3726 found = 1;
3727 break;
3728 }
3729 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003730 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3731 acat + 1);
3732 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003733 skp->smk_netlabel.attr.mls.cat,
3734 kcat + 1);
3735 if (acat < 0 || kcat < 0)
3736 break;
3737 }
3738 if (acat == kcat) {
3739 found = 1;
3740 break;
3741 }
Casey Schauflere114e472008-02-04 22:29:50 -08003742 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003743 rcu_read_unlock();
3744
3745 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003746 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003747
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003748 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003749 return &smack_known_web;
3750 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003751 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003752 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003753 /*
3754 * Looks like a fallback, which gives us a secid.
3755 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003756 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003757 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003758 * Without guidance regarding the smack value
3759 * for the packet fall back on the network
3760 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003761 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003762 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003763}
3764
Casey Schaufler69f287a2014-12-12 17:08:40 -08003765#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003766static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003767{
Casey Schauflerc6739442013-05-22 18:42:56 -07003768 u8 nexthdr;
3769 int offset;
3770 int proto = -EINVAL;
3771 struct ipv6hdr _ipv6h;
3772 struct ipv6hdr *ip6;
3773 __be16 frag_off;
3774 struct tcphdr _tcph, *th;
3775 struct udphdr _udph, *uh;
3776 struct dccp_hdr _dccph, *dh;
3777
3778 sip->sin6_port = 0;
3779
3780 offset = skb_network_offset(skb);
3781 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3782 if (ip6 == NULL)
3783 return -EINVAL;
3784 sip->sin6_addr = ip6->saddr;
3785
3786 nexthdr = ip6->nexthdr;
3787 offset += sizeof(_ipv6h);
3788 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3789 if (offset < 0)
3790 return -EINVAL;
3791
3792 proto = nexthdr;
3793 switch (proto) {
3794 case IPPROTO_TCP:
3795 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3796 if (th != NULL)
3797 sip->sin6_port = th->source;
3798 break;
3799 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003800 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003801 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3802 if (uh != NULL)
3803 sip->sin6_port = uh->source;
3804 break;
3805 case IPPROTO_DCCP:
3806 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3807 if (dh != NULL)
3808 sip->sin6_port = dh->dccph_sport;
3809 break;
3810 }
3811 return proto;
3812}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003813#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003814
Casey Schauflere114e472008-02-04 22:29:50 -08003815/**
3816 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3817 * @sk: socket
3818 * @skb: packet
3819 *
3820 * Returns 0 if the packet should be delivered, an error code otherwise
3821 */
3822static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3823{
3824 struct netlbl_lsm_secattr secattr;
3825 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003826 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003827 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003828 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003829 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003830#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003831 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003832#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003833#if IS_ENABLED(CONFIG_IPV6)
3834 struct sockaddr_in6 sadd;
3835 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003836
3837 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3838 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003839#endif /* CONFIG_IPV6 */
3840
Piotr Sawicki129a9982018-07-19 11:42:58 +02003841 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003842 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003843#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3844 /*
3845 * If there is a secmark use it rather than the CIPSO label.
3846 * If there is no secmark fall back to CIPSO.
3847 * The secmark is assumed to reflect policy better.
3848 */
3849 if (skb && skb->secmark != 0) {
3850 skp = smack_from_secid(skb->secmark);
3851 goto access_check;
3852 }
3853#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003854 /*
3855 * Translate what netlabel gave us.
3856 */
3857 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003858
Piotr Sawicki129a9982018-07-19 11:42:58 +02003859 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003860 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003861 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003862 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003863 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003864
Casey Schauflerc6739442013-05-22 18:42:56 -07003865 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003866
Casey Schaufler69f287a2014-12-12 17:08:40 -08003867#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3868access_check:
3869#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003870#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003871 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003872 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003873 ad.a.u.net->netif = skb->skb_iif;
3874 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003875#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003876 /*
3877 * Receiving a packet requires that the other end
3878 * be able to write here. Read access is not required.
3879 * This is the simplist possible security model
3880 * for networking.
3881 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003882 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3883 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003884 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003885 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003886 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003887 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003888#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003889 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003890 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003891 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3892 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003893 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003894#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003895 if (skb && skb->secmark != 0)
3896 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003897 else if (smk_ipv6_localhost(&sadd))
3898 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003899 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003900 skp = smack_ipv6host_label(&sadd);
3901 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003902 skp = smack_net_ambient;
Jia-Ju Bai3f4287e2019-07-23 18:00:15 +08003903 if (skb == NULL)
3904 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003905#ifdef CONFIG_AUDIT
3906 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003907 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003908 ad.a.u.net->netif = skb->skb_iif;
3909 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3910#endif /* CONFIG_AUDIT */
3911 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3912 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3913 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003914#endif /* SMACK_IPV6_SECMARK_LABELING */
3915#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003917#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003918 if (rc != 0)
3919 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3920 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003921 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003922#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003923 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003924
Paul Moorea8134292008-10-10 10:16:31 -04003925 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003926}
3927
3928/**
3929 * smack_socket_getpeersec_stream - pull in packet label
3930 * @sock: the socket
3931 * @optval: user's destination
3932 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003933 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003934 *
3935 * returns zero on success, an error code otherwise
3936 */
3937static int smack_socket_getpeersec_stream(struct socket *sock,
3938 char __user *optval,
3939 int __user *optlen, unsigned len)
3940{
3941 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003942 char *rcp = "";
3943 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003944 int rc = 0;
3945
3946 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003947 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003948 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003949 slen = strlen(rcp) + 1;
3950 }
Casey Schauflere114e472008-02-04 22:29:50 -08003951
3952 if (slen > len)
3953 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003954 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003955 rc = -EFAULT;
3956
3957 if (put_user(slen, optlen) != 0)
3958 rc = -EFAULT;
3959
3960 return rc;
3961}
3962
3963
3964/**
3965 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003966 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003967 * @skb: packet data
3968 * @secid: pointer to where to put the secid of the packet
3969 *
3970 * Sets the netlabel socket state on sk from parent
3971 */
3972static int smack_socket_getpeersec_dgram(struct socket *sock,
3973 struct sk_buff *skb, u32 *secid)
3974
3975{
3976 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003977 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003978 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003979 int family = PF_UNSPEC;
3980 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003981 int rc;
3982
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003983 if (skb != NULL) {
3984 if (skb->protocol == htons(ETH_P_IP))
3985 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003986#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003987 else if (skb->protocol == htons(ETH_P_IPV6))
3988 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003989#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003990 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003991 if (family == PF_UNSPEC && sock != NULL)
3992 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003993
Casey Schaufler69f287a2014-12-12 17:08:40 -08003994 switch (family) {
3995 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003996 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003997 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003998 break;
3999 case PF_INET:
4000#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4001 s = skb->secmark;
4002 if (s != 0)
4003 break;
4004#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004005 /*
4006 * Translate what netlabel gave us.
4007 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004008 if (sock != NULL && sock->sk != NULL)
4009 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004010 netlbl_secattr_init(&secattr);
4011 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4012 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004013 skp = smack_from_secattr(&secattr, ssp);
4014 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004015 }
4016 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004017 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004018 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004019#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004020 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004021#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004022 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004023 }
4024 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004025 if (s == 0)
4026 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004027 return 0;
4028}
4029
4030/**
Paul Moore07feee82009-03-27 17:10:54 -04004031 * smack_sock_graft - Initialize a newly created socket with an existing sock
4032 * @sk: child sock
4033 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004034 *
Paul Moore07feee82009-03-27 17:10:54 -04004035 * Set the smk_{in,out} state of an existing sock based on the process that
4036 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004037 */
4038static void smack_sock_graft(struct sock *sk, struct socket *parent)
4039{
4040 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004041 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004042
Paul Moore07feee82009-03-27 17:10:54 -04004043 if (sk == NULL ||
4044 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004045 return;
4046
4047 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004048 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004049 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004050 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004051}
4052
4053/**
4054 * smack_inet_conn_request - Smack access check on connect
4055 * @sk: socket involved
4056 * @skb: packet
4057 * @req: unused
4058 *
4059 * Returns 0 if a task with the packet label could write to
4060 * the socket, otherwise an error code
4061 */
4062static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4063 struct request_sock *req)
4064{
Paul Moore07feee82009-03-27 17:10:54 -04004065 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004066 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004067 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004068 struct netlbl_lsm_secattr secattr;
4069 struct sockaddr_in addr;
4070 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004071 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004072 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004073 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004074#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004075 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004076#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004077
Casey Schaufler69f287a2014-12-12 17:08:40 -08004078#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004079 if (family == PF_INET6) {
4080 /*
4081 * Handle mapped IPv4 packets arriving
4082 * via IPv6 sockets. Don't set up netlabel
4083 * processing on IPv6.
4084 */
4085 if (skb->protocol == htons(ETH_P_IP))
4086 family = PF_INET;
4087 else
4088 return 0;
4089 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004090#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004091
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004092#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4093 /*
4094 * If there is a secmark use it rather than the CIPSO label.
4095 * If there is no secmark fall back to CIPSO.
4096 * The secmark is assumed to reflect policy better.
4097 */
4098 if (skb && skb->secmark != 0) {
4099 skp = smack_from_secid(skb->secmark);
4100 goto access_check;
4101 }
4102#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4103
Paul Moore07feee82009-03-27 17:10:54 -04004104 netlbl_secattr_init(&secattr);
4105 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004106 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004107 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004108 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004109 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004110 netlbl_secattr_destroy(&secattr);
4111
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004112#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4113access_check:
4114#endif
4115
Etienne Bassetecfcc532009-04-08 20:40:06 +02004116#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004117 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4118 ad.a.u.net->family = family;
4119 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004120 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4121#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004122 /*
Paul Moore07feee82009-03-27 17:10:54 -04004123 * Receiving a packet requires that the other end be able to write
4124 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004125 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004126 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4127 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004128 if (rc != 0)
4129 return rc;
4130
4131 /*
4132 * Save the peer's label in the request_sock so we can later setup
4133 * smk_packet in the child socket so that SO_PEERCRED can report it.
4134 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004135 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004136
4137 /*
4138 * We need to decide if we want to label the incoming connection here
4139 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004140 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004141 */
4142 hdr = ip_hdr(skb);
4143 addr.sin_addr.s_addr = hdr->saddr;
4144 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004145 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004146 rcu_read_unlock();
4147
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004148 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004149 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004150 else
Paul Moore07feee82009-03-27 17:10:54 -04004151 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004152
4153 return rc;
4154}
4155
Paul Moore07feee82009-03-27 17:10:54 -04004156/**
4157 * smack_inet_csk_clone - Copy the connection information to the new socket
4158 * @sk: the new socket
4159 * @req: the connection's request_sock
4160 *
4161 * Transfer the connection's peer label to the newly created socket.
4162 */
4163static void smack_inet_csk_clone(struct sock *sk,
4164 const struct request_sock *req)
4165{
4166 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004167 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004168
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004169 if (req->peer_secid != 0) {
4170 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004171 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004172 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004173 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004174}
4175
Casey Schauflere114e472008-02-04 22:29:50 -08004176/*
4177 * Key management security hooks
4178 *
4179 * Casey has not tested key support very heavily.
4180 * The permission check is most likely too restrictive.
4181 * If you care about keys please have a look.
4182 */
4183#ifdef CONFIG_KEYS
4184
4185/**
4186 * smack_key_alloc - Set the key security blob
4187 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004188 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004189 * @flags: unused
4190 *
4191 * No allocation required
4192 *
4193 * Returns 0
4194 */
David Howellsd84f4f92008-11-14 10:39:23 +11004195static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004196 unsigned long flags)
4197{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004198 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004199
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004200 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004201 return 0;
4202}
4203
4204/**
4205 * smack_key_free - Clear the key security blob
4206 * @key: the object
4207 *
4208 * Clear the blob pointer
4209 */
4210static void smack_key_free(struct key *key)
4211{
4212 key->security = NULL;
4213}
4214
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004215/**
Casey Schauflere114e472008-02-04 22:29:50 -08004216 * smack_key_permission - Smack access on a key
4217 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004218 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004219 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004220 *
4221 * Return 0 if the task has read and write to the object,
4222 * an error code otherwise
4223 */
4224static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004225 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004226{
4227 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004228 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004229 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004230 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004231 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004232
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004233 /*
4234 * Validate requested permissions
4235 */
4236 if (perm & ~KEY_NEED_ALL)
4237 return -EINVAL;
4238
Casey Schauflere114e472008-02-04 22:29:50 -08004239 keyp = key_ref_to_ptr(key_ref);
4240 if (keyp == NULL)
4241 return -EINVAL;
4242 /*
4243 * If the key hasn't been initialized give it access so that
4244 * it may do so.
4245 */
4246 if (keyp->security == NULL)
4247 return 0;
4248 /*
4249 * This should not occur
4250 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004251 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004252 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004253
4254 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4255 return 0;
4256
Etienne Bassetecfcc532009-04-08 20:40:06 +02004257#ifdef CONFIG_AUDIT
4258 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4259 ad.a.u.key_struct.key = keyp->serial;
4260 ad.a.u.key_struct.key_desc = keyp->description;
4261#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004262 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4263 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004264 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004265 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004266 rc = smk_access(tkp, keyp->security, request, &ad);
4267 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4268 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004269}
José Bollo7fc5f362015-02-17 15:41:22 +01004270
4271/*
4272 * smack_key_getsecurity - Smack label tagging the key
4273 * @key points to the key to be queried
4274 * @_buffer points to a pointer that should be set to point to the
4275 * resulting string (if no label or an error occurs).
4276 * Return the length of the string (including terminating NUL) or -ve if
4277 * an error.
4278 * May also return 0 (and a NULL buffer pointer) if there is no label.
4279 */
4280static int smack_key_getsecurity(struct key *key, char **_buffer)
4281{
4282 struct smack_known *skp = key->security;
4283 size_t length;
4284 char *copy;
4285
4286 if (key->security == NULL) {
4287 *_buffer = NULL;
4288 return 0;
4289 }
4290
4291 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4292 if (copy == NULL)
4293 return -ENOMEM;
4294 length = strlen(copy) + 1;
4295
4296 *_buffer = copy;
4297 return length;
4298}
4299
Casey Schauflere114e472008-02-04 22:29:50 -08004300#endif /* CONFIG_KEYS */
4301
4302/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004303 * Smack Audit hooks
4304 *
4305 * Audit requires a unique representation of each Smack specific
4306 * rule. This unique representation is used to distinguish the
4307 * object to be audited from remaining kernel objects and also
4308 * works as a glue between the audit hooks.
4309 *
4310 * Since repository entries are added but never deleted, we'll use
4311 * the smack_known label address related to the given audit rule as
4312 * the needed unique representation. This also better fits the smack
4313 * model where nearly everything is a label.
4314 */
4315#ifdef CONFIG_AUDIT
4316
4317/**
4318 * smack_audit_rule_init - Initialize a smack audit rule
4319 * @field: audit rule fields given from user-space (audit.h)
4320 * @op: required testing operator (=, !=, >, <, ...)
4321 * @rulestr: smack label to be audited
4322 * @vrule: pointer to save our own audit rule representation
4323 *
4324 * Prepare to audit cases where (@field @op @rulestr) is true.
4325 * The label to be audited is created if necessay.
4326 */
4327static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4328{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004329 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004330 char **rule = (char **)vrule;
4331 *rule = NULL;
4332
4333 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4334 return -EINVAL;
4335
Al Viro5af75d82008-12-16 05:59:26 -05004336 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004337 return -EINVAL;
4338
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004339 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004340 if (IS_ERR(skp))
4341 return PTR_ERR(skp);
4342
4343 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004344
4345 return 0;
4346}
4347
4348/**
4349 * smack_audit_rule_known - Distinguish Smack audit rules
4350 * @krule: rule of interest, in Audit kernel representation format
4351 *
4352 * This is used to filter Smack rules from remaining Audit ones.
4353 * If it's proved that this rule belongs to us, the
4354 * audit_rule_match hook will be called to do the final judgement.
4355 */
4356static int smack_audit_rule_known(struct audit_krule *krule)
4357{
4358 struct audit_field *f;
4359 int i;
4360
4361 for (i = 0; i < krule->field_count; i++) {
4362 f = &krule->fields[i];
4363
4364 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4365 return 1;
4366 }
4367
4368 return 0;
4369}
4370
4371/**
4372 * smack_audit_rule_match - Audit given object ?
4373 * @secid: security id for identifying the object to test
4374 * @field: audit rule flags given from user-space
4375 * @op: required testing operator
4376 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004377 *
4378 * The core Audit hook. It's used to take the decision of
4379 * whether to audit or not to audit a given object.
4380 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004381static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004382{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004383 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004384 char *rule = vrule;
4385
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004386 if (unlikely(!rule)) {
4387 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004388 return -ENOENT;
4389 }
4390
4391 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4392 return 0;
4393
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004394 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004395
4396 /*
4397 * No need to do string comparisons. If a match occurs,
4398 * both pointers will point to the same smack_known
4399 * label.
4400 */
Al Viro5af75d82008-12-16 05:59:26 -05004401 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004402 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004403 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004404 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004405
4406 return 0;
4407}
4408
Casey Schaufler491a0b02016-01-26 15:08:35 -08004409/*
4410 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004411 * No memory was allocated.
4412 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004413
4414#endif /* CONFIG_AUDIT */
4415
Randy Dunlap251a2a92009-02-18 11:42:33 -08004416/**
David Quigley746df9b2013-05-22 12:50:35 -04004417 * smack_ismaclabel - check if xattr @name references a smack MAC label
4418 * @name: Full xattr name to check.
4419 */
4420static int smack_ismaclabel(const char *name)
4421{
4422 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4423}
4424
4425
4426/**
Casey Schauflere114e472008-02-04 22:29:50 -08004427 * smack_secid_to_secctx - return the smack label for a secid
4428 * @secid: incoming integer
4429 * @secdata: destination
4430 * @seclen: how long it is
4431 *
4432 * Exists for networking code.
4433 */
4434static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4435{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004436 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004437
Eric Parisd5630b92010-10-13 16:24:48 -04004438 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004439 *secdata = skp->smk_known;
4440 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004441 return 0;
4442}
4443
Randy Dunlap251a2a92009-02-18 11:42:33 -08004444/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004445 * smack_secctx_to_secid - return the secid for a smack label
4446 * @secdata: smack label
4447 * @seclen: how long result is
4448 * @secid: outgoing integer
4449 *
4450 * Exists for audit and networking code.
4451 */
David Howellse52c17642008-04-29 20:52:51 +01004452static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004453{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004454 struct smack_known *skp = smk_find_entry(secdata);
4455
4456 if (skp)
4457 *secid = skp->smk_secid;
4458 else
4459 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004460 return 0;
4461}
4462
Casey Schaufler491a0b02016-01-26 15:08:35 -08004463/*
4464 * There used to be a smack_release_secctx hook
4465 * that did nothing back when hooks were in a vector.
4466 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004467 */
Casey Schauflere114e472008-02-04 22:29:50 -08004468
David P. Quigley1ee65e32009-09-03 14:25:57 -04004469static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4470{
4471 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4472}
4473
4474static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4475{
4476 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4477}
4478
4479static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4480{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004481 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004482
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004483 *ctx = skp->smk_known;
4484 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004485 return 0;
4486}
4487
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004488static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4489{
4490
4491 struct task_smack *tsp;
4492 struct smack_known *skp;
4493 struct inode_smack *isp;
4494 struct cred *new_creds = *new;
4495
4496 if (new_creds == NULL) {
4497 new_creds = prepare_creds();
4498 if (new_creds == NULL)
4499 return -ENOMEM;
4500 }
4501
Casey Schauflerb17103a2018-11-09 16:12:56 -08004502 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004503
4504 /*
4505 * Get label from overlay inode and set it in create_sid
4506 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004507 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004508 skp = isp->smk_inode;
4509 tsp->smk_task = skp;
4510 *new = new_creds;
4511 return 0;
4512}
4513
4514static int smack_inode_copy_up_xattr(const char *name)
4515{
4516 /*
4517 * Return 1 if this is the smack access Smack attribute.
4518 */
4519 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4520 return 1;
4521
4522 return -EOPNOTSUPP;
4523}
4524
4525static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4526 struct qstr *name,
4527 const struct cred *old,
4528 struct cred *new)
4529{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004530 struct task_smack *otsp = smack_cred(old);
4531 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004532 struct inode_smack *isp;
4533 int may;
4534
4535 /*
4536 * Use the process credential unless all of
4537 * the transmuting criteria are met
4538 */
4539 ntsp->smk_task = otsp->smk_task;
4540
4541 /*
4542 * the attribute of the containing directory
4543 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004544 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004545
4546 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4547 rcu_read_lock();
4548 may = smk_access_entry(otsp->smk_task->smk_known,
4549 isp->smk_inode->smk_known,
4550 &otsp->smk_task->smk_rules);
4551 rcu_read_unlock();
4552
4553 /*
4554 * If the directory is transmuting and the rule
4555 * providing access is transmuting use the containing
4556 * directory label instead of the process label.
4557 */
4558 if (may > 0 && (may & MAY_TRANSMUTE))
4559 ntsp->smk_task = isp->smk_inode;
4560 }
4561 return 0;
4562}
4563
Casey Schauflerbbd36622018-11-12 09:30:56 -08004564struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4565 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004566 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004567 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004568 .lbs_ipc = sizeof(struct smack_known *),
4569 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004570};
4571
James Morrisca97d932017-02-15 00:18:51 +11004572static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004573 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4574 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4575 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004576
Al Viro0b520752018-12-23 16:02:47 -05004577 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004578 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4579
Casey Schauflere20b0432015-05-02 15:11:36 -07004580 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4581 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004582 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004583 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004584 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304585 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004586
Casey Schauflere20b0432015-05-02 15:11:36 -07004587 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004588
Casey Schauflere20b0432015-05-02 15:11:36 -07004589 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004590 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4591 LSM_HOOK_INIT(inode_link, smack_inode_link),
4592 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4593 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4594 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4595 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4596 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4597 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4598 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4599 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4600 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4601 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4602 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4603 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4604 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4605 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004606
Casey Schauflere20b0432015-05-02 15:11:36 -07004607 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004608 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4609 LSM_HOOK_INIT(file_lock, smack_file_lock),
4610 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4611 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4612 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4613 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4614 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4615 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004616
Casey Schauflere20b0432015-05-02 15:11:36 -07004617 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004618
Casey Schauflere20b0432015-05-02 15:11:36 -07004619 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4620 LSM_HOOK_INIT(cred_free, smack_cred_free),
4621 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4622 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004623 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004624 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4625 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4626 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4627 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4628 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4629 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4630 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4631 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4632 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4633 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4634 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4635 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4636 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004637 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004638
Casey Schauflere20b0432015-05-02 15:11:36 -07004639 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4640 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004641
Casey Schauflere20b0432015-05-02 15:11:36 -07004642 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004643
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004644 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004645 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4646 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4647 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4648 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004649
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004650 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004651 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4652 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4653 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004654
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004655 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004656 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4657 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4658 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004659
Casey Schauflere20b0432015-05-02 15:11:36 -07004660 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004661
Casey Schauflere20b0432015-05-02 15:11:36 -07004662 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4663 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004664
Casey Schauflere20b0432015-05-02 15:11:36 -07004665 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4666 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004667
Casey Schauflere20b0432015-05-02 15:11:36 -07004668 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004669 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004670#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004671 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004672#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004673 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4674 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4675 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4676 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4677 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4678 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4679 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4680 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4681 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4682 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004683
Casey Schauflere114e472008-02-04 22:29:50 -08004684 /* key management security hooks */
4685#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004686 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4687 LSM_HOOK_INIT(key_free, smack_key_free),
4688 LSM_HOOK_INIT(key_permission, smack_key_permission),
4689 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004690#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004691
4692 /* Audit hooks */
4693#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004694 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4695 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4696 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004697#endif /* CONFIG_AUDIT */
4698
Casey Schauflere20b0432015-05-02 15:11:36 -07004699 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4700 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4701 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004702 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4703 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4704 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004705 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4706 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4707 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004708};
4709
Etienne Basset7198e2e2009-03-24 20:53:24 +01004710
Casey Schaufler86812bb2012-04-17 18:55:46 -07004711static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004712{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004713 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004714 * Initialize rule list locks
4715 */
4716 mutex_init(&smack_known_huh.smk_rules_lock);
4717 mutex_init(&smack_known_hat.smk_rules_lock);
4718 mutex_init(&smack_known_floor.smk_rules_lock);
4719 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004720 mutex_init(&smack_known_web.smk_rules_lock);
4721 /*
4722 * Initialize rule lists
4723 */
4724 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4725 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4726 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4727 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004728 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4729 /*
4730 * Create the known labels list
4731 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004732 smk_insert_entry(&smack_known_huh);
4733 smk_insert_entry(&smack_known_hat);
4734 smk_insert_entry(&smack_known_star);
4735 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004736 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004737}
4738
Casey Schauflere114e472008-02-04 22:29:50 -08004739/**
4740 * smack_init - initialize the smack system
4741 *
luanshia1a07f22019-07-05 10:35:20 +08004742 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004743 */
4744static __init int smack_init(void)
4745{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004746 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004747 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004748
Casey Schaufler4e328b02019-04-02 11:37:12 -07004749 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
Casey Schaufler4ca75282020-04-28 15:00:26 -07004750 if (!smack_rule_cache)
Casey Schaufler4e328b02019-04-02 11:37:12 -07004751 return -ENOMEM;
Casey Schaufler4e328b02019-04-02 11:37:12 -07004752
Casey Schauflerbbd36622018-11-12 09:30:56 -08004753 /*
4754 * Set the security state for the initial task.
4755 */
4756 tsp = smack_cred(cred);
4757 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4758
4759 /*
4760 * Register with LSM
4761 */
4762 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004763 smack_enabled = 1;
4764
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004765 pr_info("Smack: Initializing.\n");
4766#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4767 pr_info("Smack: Netfilter enabled.\n");
4768#endif
4769#ifdef SMACK_IPV6_PORT_LABELING
4770 pr_info("Smack: IPv6 port labeling enabled.\n");
4771#endif
4772#ifdef SMACK_IPV6_SECMARK_LABELING
4773 pr_info("Smack: IPv6 Netfilter enabled.\n");
4774#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004775
Casey Schaufler86812bb2012-04-17 18:55:46 -07004776 /* initialize the smack_known_list */
4777 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004778
Casey Schauflere114e472008-02-04 22:29:50 -08004779 return 0;
4780}
4781
4782/*
4783 * Smack requires early initialization in order to label
4784 * all processes and objects when they are created.
4785 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004786DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004787 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004788 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004789 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004790 .init = smack_init,
4791};