blob: d99450b4f51124f255932023f3b6c6e91efcb25e [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
19 */
20
21#include <linux/xattr.h>
22#include <linux/pagemap.h>
23#include <linux/mount.h>
24#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070030#include <linux/dccp.h>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020031#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080033#include <linux/mutex.h>
34#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080035#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070036#include <net/ip.h>
37#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100038#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070039#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050040#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030041#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000042#include <linux/msg.h>
43#include <linux/shm.h>
44#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053045#include <linux/parser.h>
David Howells2febd252018-11-01 23:07:24 +000046#include <linux/fs_context.h>
47#include <linux/fs_parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080048#include "smack.h"
49
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020050#define TRANS_TRUE "TRUE"
51#define TRANS_TRUE_SIZE 4
52
Casey Schauflerc6739442013-05-22 18:42:56 -070053#define SMK_CONNECTING 0
54#define SMK_RECEIVING 1
55#define SMK_SENDING 2
56
Casey Schaufler21abb1e2015-07-22 14:25:31 -070057#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053058DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080059static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070060#endif
Rohit1a5b4722014-10-15 17:40:41 +053061static struct kmem_cache *smack_inode_cache;
Casey Schaufler4e328b02019-04-02 11:37:12 -070062struct kmem_cache *smack_rule_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080063int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070064
Al Viroc3300aa2018-12-16 01:52:24 -050065#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
66static struct {
67 const char *name;
68 int len;
69 int opt;
70} smk_mount_opts[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +010071 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
Al Viroc3300aa2018-12-16 01:52:24 -050072 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
Vivek Trivedi3bf27892015-06-22 15:36:06 +053073};
Al Viroc3300aa2018-12-16 01:52:24 -050074#undef A
75
76static int match_opt_prefix(char *s, int l, char **arg)
77{
78 int i;
79
80 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
81 size_t len = smk_mount_opts[i].len;
82 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
83 continue;
84 if (len == l || s[len] != '=')
85 continue;
86 *arg = s + len + 1;
87 return smk_mount_opts[i].opt;
88 }
89 return Opt_error;
90}
Vivek Trivedi3bf27892015-06-22 15:36:06 +053091
Casey Schaufler3d04c922015-08-12 11:56:02 -070092#ifdef CONFIG_SECURITY_SMACK_BRINGUP
93static char *smk_bu_mess[] = {
94 "Bringup Error", /* Unused */
95 "Bringup", /* SMACK_BRINGUP_ALLOW */
96 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
97 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
98};
99
Casey Schauflerd166c802014-08-27 14:51:27 -0700100static void smk_bu_mode(int mode, char *s)
101{
102 int i = 0;
103
104 if (mode & MAY_READ)
105 s[i++] = 'r';
106 if (mode & MAY_WRITE)
107 s[i++] = 'w';
108 if (mode & MAY_EXEC)
109 s[i++] = 'x';
110 if (mode & MAY_APPEND)
111 s[i++] = 'a';
112 if (mode & MAY_TRANSMUTE)
113 s[i++] = 't';
114 if (mode & MAY_LOCK)
115 s[i++] = 'l';
116 if (i == 0)
117 s[i++] = '-';
118 s[i] = '\0';
119}
120#endif
121
122#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200123static int smk_bu_note(char *note, struct smack_known *sskp,
124 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700125{
126 char acc[SMK_NUM_ACCESS_TYPE + 1];
127
128 if (rc <= 0)
129 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700130 if (rc > SMACK_UNCONFINED_OBJECT)
131 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700132
133 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700134 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200135 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700136 return 0;
137}
138#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200139#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700140#endif
141
142#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200143static int smk_bu_current(char *note, struct smack_known *oskp,
144 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700145{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800146 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700147 char acc[SMK_NUM_ACCESS_TYPE + 1];
148
149 if (rc <= 0)
150 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700151 if (rc > SMACK_UNCONFINED_OBJECT)
152 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700153
154 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700155 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200156 tsp->smk_task->smk_known, oskp->smk_known,
157 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700158 return 0;
159}
160#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200161#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700162#endif
163
164#ifdef CONFIG_SECURITY_SMACK_BRINGUP
165static int smk_bu_task(struct task_struct *otp, int mode, int rc)
166{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800167 struct task_smack *tsp = smack_cred(current_cred());
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300168 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700169 char acc[SMK_NUM_ACCESS_TYPE + 1];
170
171 if (rc <= 0)
172 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700173 if (rc > SMACK_UNCONFINED_OBJECT)
174 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700175
176 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700177 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300178 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700179 current->comm, otp->comm);
180 return 0;
181}
182#else
183#define smk_bu_task(otp, mode, RC) (RC)
184#endif
185
186#ifdef CONFIG_SECURITY_SMACK_BRINGUP
187static int smk_bu_inode(struct inode *inode, int mode, int rc)
188{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800189 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800190 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700191 char acc[SMK_NUM_ACCESS_TYPE + 1];
192
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700193 if (isp->smk_flags & SMK_INODE_IMPURE)
194 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
195 inode->i_sb->s_id, inode->i_ino, current->comm);
196
Casey Schauflerd166c802014-08-27 14:51:27 -0700197 if (rc <= 0)
198 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700199 if (rc > SMACK_UNCONFINED_OBJECT)
200 rc = 0;
201 if (rc == SMACK_UNCONFINED_SUBJECT &&
202 (mode & (MAY_WRITE | MAY_APPEND)))
203 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700204
205 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700206
207 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
208 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700209 inode->i_sb->s_id, inode->i_ino, current->comm);
210 return 0;
211}
212#else
213#define smk_bu_inode(inode, mode, RC) (RC)
214#endif
215
216#ifdef CONFIG_SECURITY_SMACK_BRINGUP
217static int smk_bu_file(struct file *file, int mode, int rc)
218{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800219 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700220 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800221 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800222 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700223 char acc[SMK_NUM_ACCESS_TYPE + 1];
224
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700225 if (isp->smk_flags & SMK_INODE_IMPURE)
226 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
227 inode->i_sb->s_id, inode->i_ino, current->comm);
228
Casey Schauflerd166c802014-08-27 14:51:27 -0700229 if (rc <= 0)
230 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700231 if (rc > SMACK_UNCONFINED_OBJECT)
232 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700233
234 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700235 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800236 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400237 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700238 current->comm);
239 return 0;
240}
241#else
242#define smk_bu_file(file, mode, RC) (RC)
243#endif
244
245#ifdef CONFIG_SECURITY_SMACK_BRINGUP
246static int smk_bu_credfile(const struct cred *cred, struct file *file,
247 int mode, int rc)
248{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800249 struct task_smack *tsp = smack_cred(cred);
Casey Schauflerd166c802014-08-27 14:51:27 -0700250 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500251 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800252 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700253 char acc[SMK_NUM_ACCESS_TYPE + 1];
254
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700255 if (isp->smk_flags & SMK_INODE_IMPURE)
256 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
257 inode->i_sb->s_id, inode->i_ino, current->comm);
258
Casey Schauflerd166c802014-08-27 14:51:27 -0700259 if (rc <= 0)
260 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700261 if (rc > SMACK_UNCONFINED_OBJECT)
262 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700263
264 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700265 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200266 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400267 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700268 current->comm);
269 return 0;
270}
271#else
272#define smk_bu_credfile(cred, file, mode, RC) (RC)
273#endif
274
Casey Schauflere114e472008-02-04 22:29:50 -0800275/**
276 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100277 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800278 * @ip: a pointer to the inode
279 * @dp: a pointer to the dentry
280 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200281 * Returns a pointer to the master list entry for the Smack label,
282 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800283 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700284static struct smack_known *smk_fetch(const char *name, struct inode *ip,
285 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800286{
287 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700288 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700289 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800290
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200291 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200292 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800293
Casey Schauflerf7112e62012-05-06 15:22:02 -0700294 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
295 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200296 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800297
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200298 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200299 if (rc < 0)
300 skp = ERR_PTR(rc);
301 else if (rc == 0)
302 skp = NULL;
303 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700304 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700305
306 kfree(buffer);
307
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700308 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800309}
310
311/**
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700312 * init_inode_smack - initialize an inode security blob
313 * @isp: the blob to initialize
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200314 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800315 *
Casey Schauflere114e472008-02-04 22:29:50 -0800316 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700317static void init_inode_smack(struct inode *inode, struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800318{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700319 struct inode_smack *isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -0800320
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200321 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800322 isp->smk_flags = 0;
323 mutex_init(&isp->smk_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800324}
325
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800326/**
Casey Schauflerbbd36622018-11-12 09:30:56 -0800327 * init_task_smack - initialize a task security blob
328 * @tsp: blob to initialize
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100329 * @task: a pointer to the Smack label for the running task
330 * @forked: a pointer to the Smack label for the forked task
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800331 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800332 */
Casey Schauflerbbd36622018-11-12 09:30:56 -0800333static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
334 struct smack_known *forked)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800335{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800336 tsp->smk_task = task;
337 tsp->smk_forked = forked;
338 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200339 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800340 mutex_init(&tsp->smk_rules_lock);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800341}
342
343/**
344 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100345 * @nhead: new rules header pointer
346 * @ohead: old rules header pointer
347 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800348 *
349 * Returns 0 on success, -ENOMEM on error
350 */
351static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
352 gfp_t gfp)
353{
354 struct smack_rule *nrp;
355 struct smack_rule *orp;
356 int rc = 0;
357
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800358 list_for_each_entry_rcu(orp, ohead, list) {
Casey Schaufler4e328b02019-04-02 11:37:12 -0700359 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800360 if (nrp == NULL) {
361 rc = -ENOMEM;
362 break;
363 }
364 *nrp = *orp;
365 list_add_rcu(&nrp->list, nhead);
366 }
367 return rc;
368}
369
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100370/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200371 * smk_copy_relabel - copy smk_relabel labels list
372 * @nhead: new rules header pointer
373 * @ohead: old rules header pointer
374 * @gfp: type of the memory for the allocation
375 *
376 * Returns 0 on success, -ENOMEM on error
377 */
378static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
379 gfp_t gfp)
380{
381 struct smack_known_list_elem *nklep;
382 struct smack_known_list_elem *oklep;
383
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200384 list_for_each_entry(oklep, ohead, list) {
385 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
386 if (nklep == NULL) {
387 smk_destroy_label_list(nhead);
388 return -ENOMEM;
389 }
390 nklep->smk_label = oklep->smk_label;
391 list_add(&nklep->list, nhead);
392 }
393
394 return 0;
395}
396
397/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100398 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
399 * @mode - input mode in form of PTRACE_MODE_*
400 *
401 * Returns a converted MAY_* mode usable by smack rules
402 */
403static inline unsigned int smk_ptrace_mode(unsigned int mode)
404{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800405 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100406 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800407 if (mode & PTRACE_MODE_READ)
408 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100409
410 return 0;
411}
412
413/**
414 * smk_ptrace_rule_check - helper for ptrace access
415 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200416 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100417 * @mode: ptrace attachment mode (PTRACE_MODE_*)
418 * @func: name of the function that called us, used for audit
419 *
420 * Returns 0 on access granted, -error on error
421 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200422static int smk_ptrace_rule_check(struct task_struct *tracer,
423 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100424 unsigned int mode, const char *func)
425{
426 int rc;
427 struct smk_audit_info ad, *saip = NULL;
428 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200429 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700430 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100431
432 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
433 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
434 smk_ad_setfield_u_tsk(&ad, tracer);
435 saip = &ad;
436 }
437
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300438 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700439 tracercred = __task_cred(tracer);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800440 tsp = smack_cred(tracercred);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200441 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100442
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100443 if ((mode & PTRACE_MODE_ATTACH) &&
444 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
445 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200446 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100447 rc = 0;
448 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
449 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700450 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100451 rc = 0;
452 else
453 rc = -EACCES;
454
455 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200456 smack_log(tracer_known->smk_known,
457 tracee_known->smk_known,
458 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100459
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300460 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100461 return rc;
462 }
463
464 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200465 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300466
467 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100468 return rc;
469}
470
Casey Schauflere114e472008-02-04 22:29:50 -0800471/*
472 * LSM hooks.
473 * We he, that is fun!
474 */
475
476/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000477 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800478 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100479 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800480 *
481 * Returns 0 if access is OK, an error code otherwise
482 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100483 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800484 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000485static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800486{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700487 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800488
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300489 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200490
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700491 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100492}
Casey Schauflere114e472008-02-04 22:29:50 -0800493
David Howells5cd9c582008-08-14 11:37:28 +0100494/**
495 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
496 * @ptp: parent task pointer
497 *
498 * Returns 0 if access is OK, an error code otherwise
499 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100500 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100501 */
502static int smack_ptrace_traceme(struct task_struct *ptp)
503{
504 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700505 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100506
Casey Schauflerb17103a2018-11-09 16:12:56 -0800507 skp = smk_of_task(smack_cred(current_cred()));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200508
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200509 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800510 return rc;
511}
512
513/**
514 * smack_syslog - Smack approval on syslog
515 * @type: message type
516 *
Casey Schauflere114e472008-02-04 22:29:50 -0800517 * Returns 0 on success, error code otherwise.
518 */
Eric Paris12b30522010-11-15 18:36:29 -0500519static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800520{
Eric Paris12b30522010-11-15 18:36:29 -0500521 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700522 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800523
Casey Schaufler1880eff2012-06-05 15:28:30 -0700524 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800525 return 0;
526
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800527 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800528 rc = -EACCES;
529
530 return rc;
531}
532
Casey Schauflere114e472008-02-04 22:29:50 -0800533/*
534 * Superblock Hooks.
535 */
536
537/**
538 * smack_sb_alloc_security - allocate a superblock blob
539 * @sb: the superblock getting the blob
540 *
541 * Returns 0 on success or -ENOMEM on error.
542 */
543static int smack_sb_alloc_security(struct super_block *sb)
544{
545 struct superblock_smack *sbsp;
546
547 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
548
549 if (sbsp == NULL)
550 return -ENOMEM;
551
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200552 sbsp->smk_root = &smack_known_floor;
553 sbsp->smk_default = &smack_known_floor;
554 sbsp->smk_floor = &smack_known_floor;
555 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700556 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500557 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700558 */
Casey Schauflere114e472008-02-04 22:29:50 -0800559 sb->s_security = sbsp;
560
561 return 0;
562}
563
564/**
565 * smack_sb_free_security - free a superblock blob
566 * @sb: the superblock getting the blob
567 *
568 */
569static void smack_sb_free_security(struct super_block *sb)
570{
571 kfree(sb->s_security);
572 sb->s_security = NULL;
573}
574
Al Viro12085b12018-12-13 15:18:05 -0500575struct smack_mnt_opts {
576 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
577};
578
Al Viro204cc0c2018-12-13 13:41:47 -0500579static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800580{
Al Viro12085b12018-12-13 15:18:05 -0500581 struct smack_mnt_opts *opts = mnt_opts;
582 kfree(opts->fsdefault);
583 kfree(opts->fsfloor);
584 kfree(opts->fshat);
585 kfree(opts->fsroot);
586 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500587 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800588}
589
Al Viro55c0e5b2018-12-16 01:09:45 -0500590static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530591{
Al Viro55c0e5b2018-12-16 01:09:45 -0500592 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530593
Al Viro55c0e5b2018-12-16 01:09:45 -0500594 if (!opts) {
595 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
596 if (!opts)
597 return -ENOMEM;
598 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530599 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500600 if (!s)
601 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530602
Al Viro55c0e5b2018-12-16 01:09:45 -0500603 switch (token) {
604 case Opt_fsdefault:
605 if (opts->fsdefault)
606 goto out_opt_err;
607 opts->fsdefault = s;
608 break;
609 case Opt_fsfloor:
610 if (opts->fsfloor)
611 goto out_opt_err;
612 opts->fsfloor = s;
613 break;
614 case Opt_fshat:
615 if (opts->fshat)
616 goto out_opt_err;
617 opts->fshat = s;
618 break;
619 case Opt_fsroot:
620 if (opts->fsroot)
621 goto out_opt_err;
622 opts->fsroot = s;
623 break;
624 case Opt_fstransmute:
625 if (opts->fstransmute)
626 goto out_opt_err;
627 opts->fstransmute = s;
628 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530629 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530630 return 0;
631
632out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530633 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500634 return -EINVAL;
635}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530636
Al Viro0b520752018-12-23 16:02:47 -0500637/**
638 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
639 * @fc: The new filesystem context.
640 * @src_fc: The source filesystem context being duplicated.
641 *
642 * Returns 0 on success or -ENOMEM on error.
643 */
644static int smack_fs_context_dup(struct fs_context *fc,
645 struct fs_context *src_fc)
646{
647 struct smack_mnt_opts *dst, *src = src_fc->security;
648
649 if (!src)
650 return 0;
651
652 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
653 if (!fc->security)
654 return -ENOMEM;
655 dst = fc->security;
656
657 if (src->fsdefault) {
658 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
659 if (!dst->fsdefault)
660 return -ENOMEM;
661 }
662 if (src->fsfloor) {
663 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
664 if (!dst->fsfloor)
665 return -ENOMEM;
666 }
667 if (src->fshat) {
668 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
669 if (!dst->fshat)
670 return -ENOMEM;
671 }
672 if (src->fsroot) {
673 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
674 if (!dst->fsroot)
675 return -ENOMEM;
676 }
677 if (src->fstransmute) {
678 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
679 if (!dst->fstransmute)
680 return -ENOMEM;
681 }
682 return 0;
683}
684
David Howells2febd252018-11-01 23:07:24 +0000685static const struct fs_parameter_spec smack_param_specs[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +0100686 fsparam_string("smackfsdef", Opt_fsdefault),
687 fsparam_string("smackfsdefault", Opt_fsdefault),
688 fsparam_string("smackfsfloor", Opt_fsfloor),
689 fsparam_string("smackfshat", Opt_fshat),
690 fsparam_string("smackfsroot", Opt_fsroot),
691 fsparam_string("smackfstransmute", Opt_fstransmute),
David Howells2febd252018-11-01 23:07:24 +0000692 {}
693};
694
695static const struct fs_parameter_description smack_fs_parameters = {
696 .name = "smack",
697 .specs = smack_param_specs,
698};
699
700/**
701 * smack_fs_context_parse_param - Parse a single mount parameter
702 * @fc: The new filesystem context being constructed.
703 * @param: The parameter.
704 *
705 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
706 * error.
707 */
708static int smack_fs_context_parse_param(struct fs_context *fc,
709 struct fs_parameter *param)
710{
711 struct fs_parse_result result;
712 int opt, rc;
713
714 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
715 if (opt < 0)
716 return opt;
717
718 rc = smack_add_opt(opt, param->string, &fc->security);
719 if (!rc)
720 param->string = NULL;
721 return rc;
722}
723
Al Virod2497e12018-12-16 01:37:06 -0500724static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530725{
Al Virod2497e12018-12-16 01:37:06 -0500726 char *from = options, *to = options;
727 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530728
Al Viroc3300aa2018-12-16 01:52:24 -0500729 while (1) {
730 char *next = strchr(from, ',');
731 int token, len, rc;
732 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530733
Al Viroc3300aa2018-12-16 01:52:24 -0500734 if (next)
735 len = next - from;
736 else
737 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530738
Al Viroc3300aa2018-12-16 01:52:24 -0500739 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500740 if (token != Opt_error) {
741 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
742 rc = smack_add_opt(token, arg, mnt_opts);
743 if (unlikely(rc)) {
744 kfree(arg);
745 if (*mnt_opts)
746 smack_free_mnt_opts(*mnt_opts);
747 *mnt_opts = NULL;
748 return rc;
749 }
750 } else {
751 if (!first) { // copy with preceding comma
752 from--;
753 len++;
754 }
755 if (to != from)
756 memmove(to, from, len);
757 to += len;
758 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530759 }
Al Viroc3300aa2018-12-16 01:52:24 -0500760 if (!from[len])
761 break;
762 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530763 }
Al Virod2497e12018-12-16 01:37:06 -0500764 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530765 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530766}
767
768/**
769 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800770 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530771 * @opts: Smack mount options
772 * @kern_flags: mount option from kernel space or user space
773 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800774 *
775 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530776 *
777 * Allow filesystems with binary mount data to explicitly set Smack mount
778 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800779 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530780static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500781 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530782 unsigned long kern_flags,
783 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800784{
785 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000786 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800787 struct superblock_smack *sp = sb->s_security;
788 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800789 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500790 struct smack_mnt_opts *opts = mnt_opts;
791 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800792
Seth Forshee9f50eda2015-09-23 15:16:06 -0500793 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800794 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700795
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700796 if (inode->i_security == NULL) {
797 int rc = lsm_inode_alloc(inode);
798
799 if (rc)
800 return rc;
801 }
802
Himanshu Shukla2097f592016-11-10 16:19:52 +0530803 if (!smack_privileged(CAP_MAC_ADMIN)) {
804 /*
805 * Unprivileged mounts don't get to specify Smack values.
806 */
Al Viro12085b12018-12-13 15:18:05 -0500807 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530808 return -EPERM;
809 /*
810 * Unprivileged mounts get root and default from the caller.
811 */
812 skp = smk_of_current();
813 sp->smk_root = skp;
814 sp->smk_default = skp;
815 /*
816 * For a handful of fs types with no user-controlled
817 * backing store it's okay to trust security labels
818 * in the filesystem. The rest are untrusted.
819 */
820 if (sb->s_user_ns != &init_user_ns &&
821 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
822 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500823 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530824 sp->smk_flags |= SMK_SB_UNTRUSTED;
825 }
826 }
827
Seth Forshee9f50eda2015-09-23 15:16:06 -0500828 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800829
Al Viro12085b12018-12-13 15:18:05 -0500830 if (opts) {
831 if (opts->fsdefault) {
832 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200833 if (IS_ERR(skp))
834 return PTR_ERR(skp);
835 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500836 }
837 if (opts->fsfloor) {
838 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530839 if (IS_ERR(skp))
840 return PTR_ERR(skp);
841 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500842 }
843 if (opts->fshat) {
844 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530845 if (IS_ERR(skp))
846 return PTR_ERR(skp);
847 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500848 }
849 if (opts->fsroot) {
850 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200851 if (IS_ERR(skp))
852 return PTR_ERR(skp);
853 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500854 }
855 if (opts->fstransmute) {
856 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200857 if (IS_ERR(skp))
858 return PTR_ERR(skp);
859 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500860 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800861 }
862 }
863
864 /*
865 * Initialize the root inode.
866 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700867 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800868
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700869 if (transmute) {
870 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700871 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700872 }
Casey Schauflere830b392013-05-22 18:43:07 -0700873
Casey Schauflere114e472008-02-04 22:29:50 -0800874 return 0;
875}
876
877/**
878 * smack_sb_statfs - Smack check on statfs
879 * @dentry: identifies the file system in question
880 *
881 * Returns 0 if current can read the floor of the filesystem,
882 * and error code otherwise
883 */
884static int smack_sb_statfs(struct dentry *dentry)
885{
886 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200887 int rc;
888 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800889
Eric Parisa2694342011-04-25 13:10:27 -0400890 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200891 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
892
893 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700894 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200895 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800896}
897
Casey Schauflere114e472008-02-04 22:29:50 -0800898/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800899 * BPRM hooks
900 */
901
Casey Schauflerce8a4322011-09-29 18:21:01 -0700902/**
903 * smack_bprm_set_creds - set creds for exec
904 * @bprm: the exec information
905 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100906 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700907 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800908static int smack_bprm_set_creds(struct linux_binprm *bprm)
909{
Al Viro496ad9a2013-01-23 17:07:38 -0500910 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800911 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800912 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500913 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800914 int rc;
915
Kees Cookddb4a142017-07-18 15:25:23 -0700916 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800917 return 0;
918
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800919 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300920 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800921 return 0;
922
Seth Forshee809c02e2016-04-26 14:36:22 -0500923 sbsp = inode->i_sb->s_security;
924 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
925 isp->smk_task != sbsp->smk_root)
926 return 0;
927
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300928 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100929 struct task_struct *tracer;
930 rc = 0;
931
932 rcu_read_lock();
933 tracer = ptrace_parent(current);
934 if (likely(tracer != NULL))
935 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200936 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100937 PTRACE_MODE_ATTACH,
938 __func__);
939 rcu_read_unlock();
940
941 if (rc != 0)
942 return rc;
943 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300944 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800945
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300946 bsp->smk_task = isp->smk_task;
947 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800948
Kees Cookccbb6e12017-07-18 15:25:26 -0700949 /* Decide if this is a secure exec. */
950 if (bsp->smk_task != bsp->smk_forked)
951 bprm->secureexec = 1;
952
Casey Schaufler676dac42010-12-02 06:43:39 -0800953 return 0;
954}
955
956/*
Casey Schauflere114e472008-02-04 22:29:50 -0800957 * Inode hooks
958 */
959
960/**
961 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800962 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800963 *
964 * Returns 0 if it gets a blob, -ENOMEM otherwise
965 */
966static int smack_inode_alloc_security(struct inode *inode)
967{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700968 struct smack_known *skp = smk_of_current();
969
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700970 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800971 return 0;
972}
973
974/**
Casey Schauflere114e472008-02-04 22:29:50 -0800975 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200976 * @inode: the newly created inode
977 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500978 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800979 * @name: where to put the attribute name
980 * @value: where to put the attribute value
981 * @len: where to put the length of the attribute
982 *
983 * Returns 0 if it all works out, -ENOMEM if there's no memory
984 */
985static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900986 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500987 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800988{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800989 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700990 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200991 struct smack_known *isp = smk_of_inode(inode);
992 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800993 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800994
Tetsuo Handa95489062013-07-25 05:44:02 +0900995 if (name)
996 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800997
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100998 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800999 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001000 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1001 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001002 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001003
1004 /*
1005 * If the access rule allows transmutation and
1006 * the directory requests transmutation then
1007 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001008 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001009 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001010 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001011 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001012 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001013 issp->smk_flags |= SMK_INODE_CHANGED;
1014 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001015
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001016 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001017 if (*value == NULL)
1018 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001019
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001020 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001021 }
Casey Schauflere114e472008-02-04 22:29:50 -08001022
1023 return 0;
1024}
1025
1026/**
1027 * smack_inode_link - Smack check on link
1028 * @old_dentry: the existing object
1029 * @dir: unused
1030 * @new_dentry: the new object
1031 *
1032 * Returns 0 if access is permitted, an error code otherwise
1033 */
1034static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1035 struct dentry *new_dentry)
1036{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001037 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001038 struct smk_audit_info ad;
1039 int rc;
1040
Eric Parisa2694342011-04-25 13:10:27 -04001041 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001043
David Howellsc6f493d2015-03-17 22:26:22 +00001044 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001045 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001046 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001047
David Howells88025652015-01-29 12:02:32 +00001048 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001049 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001050 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1051 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001052 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001053 }
1054
1055 return rc;
1056}
1057
1058/**
1059 * smack_inode_unlink - Smack check on inode deletion
1060 * @dir: containing directory object
1061 * @dentry: file to unlink
1062 *
1063 * Returns 0 if current can write the containing directory
1064 * and the object, error code otherwise
1065 */
1066static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1067{
David Howellsc6f493d2015-03-17 22:26:22 +00001068 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001069 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001070 int rc;
1071
Eric Parisa2694342011-04-25 13:10:27 -04001072 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001073 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1074
Casey Schauflere114e472008-02-04 22:29:50 -08001075 /*
1076 * You need write access to the thing you're unlinking
1077 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001078 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001079 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001080 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001081 /*
1082 * You also need write access to the containing directory
1083 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001084 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 smk_ad_setfield_u_fs_inode(&ad, dir);
1086 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001087 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001088 }
Casey Schauflere114e472008-02-04 22:29:50 -08001089 return rc;
1090}
1091
1092/**
1093 * smack_inode_rmdir - Smack check on directory deletion
1094 * @dir: containing directory object
1095 * @dentry: directory to unlink
1096 *
1097 * Returns 0 if current can write the containing directory
1098 * and the directory, error code otherwise
1099 */
1100static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1101{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001103 int rc;
1104
Eric Parisa2694342011-04-25 13:10:27 -04001105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1107
Casey Schauflere114e472008-02-04 22:29:50 -08001108 /*
1109 * You need write access to the thing you're removing
1110 */
David Howellsc6f493d2015-03-17 22:26:22 +00001111 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1112 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001113 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001114 /*
1115 * You also need write access to the containing directory
1116 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001117 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 smk_ad_setfield_u_fs_inode(&ad, dir);
1119 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001120 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001121 }
Casey Schauflere114e472008-02-04 22:29:50 -08001122
1123 return rc;
1124}
1125
1126/**
1127 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001128 * @old_inode: unused
1129 * @old_dentry: the old object
1130 * @new_inode: unused
1131 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001132 *
1133 * Read and write access is required on both the old and
1134 * new directories.
1135 *
1136 * Returns 0 if access is permitted, an error code otherwise
1137 */
1138static int smack_inode_rename(struct inode *old_inode,
1139 struct dentry *old_dentry,
1140 struct inode *new_inode,
1141 struct dentry *new_dentry)
1142{
1143 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001144 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 struct smk_audit_info ad;
1146
Eric Parisa2694342011-04-25 13:10:27 -04001147 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001149
David Howellsc6f493d2015-03-17 22:26:22 +00001150 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001151 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001152 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001153
David Howells88025652015-01-29 12:02:32 +00001154 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001155 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001156 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1157 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001158 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001159 }
Casey Schauflere114e472008-02-04 22:29:50 -08001160 return rc;
1161}
1162
1163/**
1164 * smack_inode_permission - Smack version of permission()
1165 * @inode: the inode in question
1166 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001167 *
1168 * This is the important Smack hook.
1169 *
1170 * Returns 0 if access is permitted, -EACCES otherwise
1171 */
Al Viroe74f71e2011-06-20 19:38:15 -04001172static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001173{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001174 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001175 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001176 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001177 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001178
1179 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001180 /*
1181 * No permission to check. Existence test. Yup, it's there.
1182 */
1183 if (mask == 0)
1184 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001185
Seth Forshee9f50eda2015-09-23 15:16:06 -05001186 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1187 if (smk_of_inode(inode) != sbsp->smk_root)
1188 return -EACCES;
1189 }
1190
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001191 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001192 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001193 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001194 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001195 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001196 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1197 rc = smk_bu_inode(inode, mask, rc);
1198 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001199}
1200
1201/**
1202 * smack_inode_setattr - Smack check for setting attributes
1203 * @dentry: the object
1204 * @iattr: for the force flag
1205 *
1206 * Returns 0 if access is permitted, an error code otherwise
1207 */
1208static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1209{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001210 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001211 int rc;
1212
Casey Schauflere114e472008-02-04 22:29:50 -08001213 /*
1214 * Need to allow for clearing the setuid bit.
1215 */
1216 if (iattr->ia_valid & ATTR_FORCE)
1217 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001218 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001219 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001220
David Howellsc6f493d2015-03-17 22:26:22 +00001221 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1222 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001223 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001224}
1225
1226/**
1227 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001228 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001229 * @dentry: the object
1230 *
1231 * Returns 0 if access is permitted, an error code otherwise
1232 */
Al Viro3f7036a2015-03-08 19:28:30 -04001233static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001234{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001235 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001236 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001237 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001238
Eric Parisf48b7392011-04-25 12:54:27 -04001239 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001240 smk_ad_setfield_u_fs_path(&ad, *path);
1241 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1242 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001243 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001244}
1245
1246/**
1247 * smack_inode_setxattr - Smack check for setting xattrs
1248 * @dentry: the object
1249 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001250 * @value: value of the attribute
1251 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001252 * @flags: unused
1253 *
1254 * This protects the Smack attribute explicitly.
1255 *
1256 * Returns 0 if access is permitted, an error code otherwise
1257 */
David Howells8f0cfa52008-04-29 00:59:41 -07001258static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1259 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001260{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001261 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001262 struct smack_known *skp;
1263 int check_priv = 0;
1264 int check_import = 0;
1265 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001266 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001267
Casey Schaufler19760ad2013-12-16 16:27:26 -08001268 /*
1269 * Check label validity here so import won't fail in post_setxattr
1270 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001271 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1272 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001273 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1274 check_priv = 1;
1275 check_import = 1;
1276 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1277 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1278 check_priv = 1;
1279 check_import = 1;
1280 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001281 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001282 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001283 if (size != TRANS_TRUE_SIZE ||
1284 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1285 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001286 } else
1287 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1288
Casey Schaufler19760ad2013-12-16 16:27:26 -08001289 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1290 rc = -EPERM;
1291
1292 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001293 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001294 if (IS_ERR(skp))
1295 rc = PTR_ERR(skp);
1296 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001297 (skp == &smack_known_star || skp == &smack_known_web)))
1298 rc = -EINVAL;
1299 }
1300
Eric Parisa2694342011-04-25 13:10:27 -04001301 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001302 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1303
Casey Schauflerd166c802014-08-27 14:51:27 -07001304 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001305 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1306 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001307 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001308
1309 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001310}
1311
1312/**
1313 * smack_inode_post_setxattr - Apply the Smack update approved above
1314 * @dentry: object
1315 * @name: attribute name
1316 * @value: attribute value
1317 * @size: attribute size
1318 * @flags: unused
1319 *
1320 * Set the pointer in the inode blob to the entry found
1321 * in the master label list.
1322 */
David Howells8f0cfa52008-04-29 00:59:41 -07001323static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1324 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001325{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001326 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001327 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001328
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001329 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1330 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1331 return;
1332 }
1333
Casey Schaufler676dac42010-12-02 06:43:39 -08001334 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001335 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001336 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001337 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001338 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001339 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001340 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001341 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001342 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001343 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001344 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001345 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001346 }
Casey Schauflere114e472008-02-04 22:29:50 -08001347
1348 return;
1349}
1350
Casey Schauflerce8a4322011-09-29 18:21:01 -07001351/**
Casey Schauflere114e472008-02-04 22:29:50 -08001352 * smack_inode_getxattr - Smack check on getxattr
1353 * @dentry: the object
1354 * @name: unused
1355 *
1356 * Returns 0 if access is permitted, an error code otherwise
1357 */
David Howells8f0cfa52008-04-29 00:59:41 -07001358static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001359{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001361 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001362
Eric Parisa2694342011-04-25 13:10:27 -04001363 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001364 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1365
David Howellsc6f493d2015-03-17 22:26:22 +00001366 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1367 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001368 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001369}
1370
Casey Schauflerce8a4322011-09-29 18:21:01 -07001371/**
Casey Schauflere114e472008-02-04 22:29:50 -08001372 * smack_inode_removexattr - Smack check on removexattr
1373 * @dentry: the object
1374 * @name: name of the attribute
1375 *
1376 * Removing the Smack attribute requires CAP_MAC_ADMIN
1377 *
1378 * Returns 0 if access is permitted, an error code otherwise
1379 */
David Howells8f0cfa52008-04-29 00:59:41 -07001380static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001381{
Casey Schaufler676dac42010-12-02 06:43:39 -08001382 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001383 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001384 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001385
Casey Schauflerbcdca222008-02-23 15:24:04 -08001386 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1387 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001388 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001389 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001390 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301391 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001392 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001393 rc = -EPERM;
1394 } else
1395 rc = cap_inode_removexattr(dentry, name);
1396
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001397 if (rc != 0)
1398 return rc;
1399
Eric Parisa2694342011-04-25 13:10:27 -04001400 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001401 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001402
David Howellsc6f493d2015-03-17 22:26:22 +00001403 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1404 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001405 if (rc != 0)
1406 return rc;
1407
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001408 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001409 /*
1410 * Don't do anything special for these.
1411 * XATTR_NAME_SMACKIPIN
1412 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001413 */
José Bollo80124952016-01-12 21:23:40 +01001414 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001415 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001416 struct superblock_smack *sbsp = sbp->s_security;
1417
1418 isp->smk_inode = sbsp->smk_default;
1419 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001420 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001421 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001422 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001423 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1424 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001425
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001426 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001427}
1428
1429/**
1430 * smack_inode_getsecurity - get smack xattrs
1431 * @inode: the object
1432 * @name: attribute name
1433 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001434 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001435 *
1436 * Returns the size of the attribute or an error code
1437 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001438static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001439 const char *name, void **buffer,
1440 bool alloc)
1441{
1442 struct socket_smack *ssp;
1443 struct socket *sock;
1444 struct super_block *sbp;
1445 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001446 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001447
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001448 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001449 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001450 else {
1451 /*
1452 * The rest of the Smack xattrs are only on sockets.
1453 */
1454 sbp = ip->i_sb;
1455 if (sbp->s_magic != SOCKFS_MAGIC)
1456 return -EOPNOTSUPP;
1457
1458 sock = SOCKET_I(ip);
1459 if (sock == NULL || sock->sk == NULL)
1460 return -EOPNOTSUPP;
1461
1462 ssp = sock->sk->sk_security;
1463
1464 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1465 isp = ssp->smk_in;
1466 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1467 isp = ssp->smk_out;
1468 else
1469 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001470 }
1471
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001472 if (alloc) {
1473 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1474 if (*buffer == NULL)
1475 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001476 }
1477
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001478 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001479}
1480
1481
1482/**
1483 * smack_inode_listsecurity - list the Smack attributes
1484 * @inode: the object
1485 * @buffer: where they go
1486 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001487 */
1488static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1489 size_t buffer_size)
1490{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001491 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001492
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001493 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001494 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001495
1496 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001497}
1498
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001499/**
1500 * smack_inode_getsecid - Extract inode's security id
1501 * @inode: inode to extract the info from
1502 * @secid: where result will be saved
1503 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001504static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001505{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001506 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001507
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001508 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001509}
1510
Casey Schauflere114e472008-02-04 22:29:50 -08001511/*
1512 * File Hooks
1513 */
1514
Casey Schaufler491a0b02016-01-26 15:08:35 -08001515/*
1516 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001517 *
1518 * Should access checks be done on each read or write?
1519 * UNICOS and SELinux say yes.
1520 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1521 *
1522 * I'll say no for now. Smack does not do the frequent
1523 * label changing that SELinux does.
1524 */
Casey Schauflere114e472008-02-04 22:29:50 -08001525
1526/**
1527 * smack_file_alloc_security - assign a file security blob
1528 * @file: the object
1529 *
1530 * The security blob for a file is a pointer to the master
1531 * label list, so no allocation is done.
1532 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001533 * f_security is the owner security information. It
1534 * isn't used on file access checks, it's for send_sigio.
1535 *
Casey Schauflere114e472008-02-04 22:29:50 -08001536 * Returns 0
1537 */
1538static int smack_file_alloc_security(struct file *file)
1539{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001540 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001541
Casey Schauflerf28952a2018-11-12 09:38:53 -08001542 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001543 return 0;
1544}
1545
1546/**
Casey Schauflere114e472008-02-04 22:29:50 -08001547 * smack_file_ioctl - Smack check on ioctls
1548 * @file: the object
1549 * @cmd: what to do
1550 * @arg: unused
1551 *
1552 * Relies heavily on the correct use of the ioctl command conventions.
1553 *
1554 * Returns 0 if allowed, error code otherwise
1555 */
1556static int smack_file_ioctl(struct file *file, unsigned int cmd,
1557 unsigned long arg)
1558{
1559 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001560 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001561 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001562
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001563 if (unlikely(IS_PRIVATE(inode)))
1564 return 0;
1565
Eric Parisf48b7392011-04-25 12:54:27 -04001566 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001567 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001568
Casey Schauflerd166c802014-08-27 14:51:27 -07001569 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001570 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001571 rc = smk_bu_file(file, MAY_WRITE, rc);
1572 }
Casey Schauflere114e472008-02-04 22:29:50 -08001573
Casey Schauflerd166c802014-08-27 14:51:27 -07001574 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001575 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001576 rc = smk_bu_file(file, MAY_READ, rc);
1577 }
Casey Schauflere114e472008-02-04 22:29:50 -08001578
1579 return rc;
1580}
1581
1582/**
1583 * smack_file_lock - Smack check on file locking
1584 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001585 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001586 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001587 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001588 */
1589static int smack_file_lock(struct file *file, unsigned int cmd)
1590{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001591 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001592 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001593 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001594
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001595 if (unlikely(IS_PRIVATE(inode)))
1596 return 0;
1597
Eric Paris92f42502011-04-25 13:15:55 -04001598 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1599 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001600 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001601 rc = smk_bu_file(file, MAY_LOCK, rc);
1602 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001603}
1604
1605/**
1606 * smack_file_fcntl - Smack check on fcntl
1607 * @file: the object
1608 * @cmd: what action to check
1609 * @arg: unused
1610 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001611 * Generally these operations are harmless.
1612 * File locking operations present an obvious mechanism
1613 * for passing information, so they require write access.
1614 *
Casey Schauflere114e472008-02-04 22:29:50 -08001615 * Returns 0 if current has access, error code otherwise
1616 */
1617static int smack_file_fcntl(struct file *file, unsigned int cmd,
1618 unsigned long arg)
1619{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001620 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001621 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001622 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001623
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001624 if (unlikely(IS_PRIVATE(inode)))
1625 return 0;
1626
Casey Schauflere114e472008-02-04 22:29:50 -08001627 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001628 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001629 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001630 case F_SETLK:
1631 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001632 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1633 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001634 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001635 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001636 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001637 case F_SETOWN:
1638 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001639 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1640 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001641 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001642 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001643 break;
1644 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001645 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001646 }
1647
1648 return rc;
1649}
1650
1651/**
Al Viroe5467852012-05-30 13:30:51 -04001652 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001653 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1654 * if mapping anonymous memory.
1655 * @file contains the file structure for file to map (may be NULL).
1656 * @reqprot contains the protection requested by the application.
1657 * @prot contains the protection that will be applied by the kernel.
1658 * @flags contains the operational flags.
1659 * Return 0 if permission is granted.
1660 */
Al Viroe5467852012-05-30 13:30:51 -04001661static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001662 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001663 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001664{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001665 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001666 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001667 struct smack_rule *srp;
1668 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001669 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001670 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001671 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001672 int may;
1673 int mmay;
1674 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001675 int rc;
1676
Al Viro496ad9a2013-01-23 17:07:38 -05001677 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001678 return 0;
1679
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001680 if (unlikely(IS_PRIVATE(file_inode(file))))
1681 return 0;
1682
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001683 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001684 if (isp->smk_mmap == NULL)
1685 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001686 sbsp = file_inode(file)->i_sb->s_security;
1687 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1688 isp->smk_mmap != sbsp->smk_root)
1689 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001690 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001691
Casey Schauflerb17103a2018-11-09 16:12:56 -08001692 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001693 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001694 rc = 0;
1695
1696 rcu_read_lock();
1697 /*
1698 * For each Smack rule associated with the subject
1699 * label verify that the SMACK64MMAP also has access
1700 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001701 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001702 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001703 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001704 /*
1705 * Matching labels always allows access.
1706 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001707 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001708 continue;
1709 /*
1710 * If there is a matching local rule take
1711 * that into account as well.
1712 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001713 may = smk_access_entry(srp->smk_subject->smk_known,
1714 okp->smk_known,
1715 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001716 if (may == -ENOENT)
1717 may = srp->smk_access;
1718 else
1719 may &= srp->smk_access;
1720 /*
1721 * If may is zero the SMACK64MMAP subject can't
1722 * possibly have less access.
1723 */
1724 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001725 continue;
1726
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001727 /*
1728 * Fetch the global list entry.
1729 * If there isn't one a SMACK64MMAP subject
1730 * can't have as much access as current.
1731 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001732 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1733 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001734 if (mmay == -ENOENT) {
1735 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001736 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001737 }
1738 /*
1739 * If there is a local entry it modifies the
1740 * potential access, too.
1741 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001742 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1743 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001744 if (tmay != -ENOENT)
1745 mmay &= tmay;
1746
1747 /*
1748 * If there is any access available to current that is
1749 * not available to a SMACK64MMAP subject
1750 * deny access.
1751 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001752 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001753 rc = -EACCES;
1754 break;
1755 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001756 }
1757
1758 rcu_read_unlock();
1759
1760 return rc;
1761}
1762
1763/**
Casey Schauflere114e472008-02-04 22:29:50 -08001764 * smack_file_set_fowner - set the file security blob value
1765 * @file: object in question
1766 *
Casey Schauflere114e472008-02-04 22:29:50 -08001767 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001768static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001769{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001770 struct smack_known **blob = smack_file(file);
1771
1772 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001773}
1774
1775/**
1776 * smack_file_send_sigiotask - Smack on sigio
1777 * @tsk: The target task
1778 * @fown: the object the signal come from
1779 * @signum: unused
1780 *
1781 * Allow a privileged task to get signals even if it shouldn't
1782 *
1783 * Returns 0 if a subject with the object's smack could
1784 * write to the task, an error code otherwise.
1785 */
1786static int smack_file_send_sigiotask(struct task_struct *tsk,
1787 struct fown_struct *fown, int signum)
1788{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001789 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001790 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001791 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001792 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001793 struct file *file;
1794 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001795 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001796
1797 /*
1798 * struct fown_struct is never outside the context of a struct file
1799 */
1800 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001801
Etienne Bassetecfcc532009-04-08 20:40:06 +02001802 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001803 blob = smack_file(file);
1804 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001805 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1806 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001807
1808 rcu_read_lock();
1809 tcred = __task_cred(tsk);
1810 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001811 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001812 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001813
1814 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1815 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001816 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001817 return rc;
1818}
1819
1820/**
1821 * smack_file_receive - Smack file receive check
1822 * @file: the object
1823 *
1824 * Returns 0 if current has access, error code otherwise
1825 */
1826static int smack_file_receive(struct file *file)
1827{
Casey Schauflerd166c802014-08-27 14:51:27 -07001828 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001829 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001830 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001831 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001832 struct socket *sock;
1833 struct task_smack *tsp;
1834 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001835
Seung-Woo Kim97775822015-04-17 15:25:04 +09001836 if (unlikely(IS_PRIVATE(inode)))
1837 return 0;
1838
Casey Schaufler4482a442013-12-30 17:37:45 -08001839 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001840 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001841
Casey Schaufler51d59af2017-05-31 08:53:42 -07001842 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001843 sock = SOCKET_I(inode);
1844 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001845 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001846 /*
1847 * If the receiving process can't write to the
1848 * passed socket or if the passed socket can't
1849 * write to the receiving process don't accept
1850 * the passed socket.
1851 */
1852 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1853 rc = smk_bu_file(file, may, rc);
1854 if (rc < 0)
1855 return rc;
1856 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1857 rc = smk_bu_file(file, may, rc);
1858 return rc;
1859 }
Casey Schauflere114e472008-02-04 22:29:50 -08001860 /*
1861 * This code relies on bitmasks.
1862 */
1863 if (file->f_mode & FMODE_READ)
1864 may = MAY_READ;
1865 if (file->f_mode & FMODE_WRITE)
1866 may |= MAY_WRITE;
1867
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001868 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001869 rc = smk_bu_file(file, may, rc);
1870 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001871}
1872
Casey Schaufler531f1d42011-09-19 12:41:42 -07001873/**
Eric Paris83d49852012-04-04 13:45:40 -04001874 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001875 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001876 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001877 *
1878 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001879 * Allow the open only if the task has read access. There are
1880 * many read operations (e.g. fstat) that you can do with an
1881 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001882 *
1883 * Returns 0
1884 */
Al Viro94817692018-07-10 14:13:18 -04001885static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001886{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001887 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001888 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001889 struct smk_audit_info ad;
1890 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001891
Casey Schauflera6834c02014-04-21 11:10:26 -07001892 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1893 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301894 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001895 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001896
1897 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001898}
1899
Casey Schauflere114e472008-02-04 22:29:50 -08001900/*
1901 * Task hooks
1902 */
1903
1904/**
David Howellsee18d642009-09-02 09:14:21 +01001905 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1906 * @new: the new credentials
1907 * @gfp: the atomicity of any memory allocations
1908 *
1909 * Prepare a blank set of credentials for modification. This must allocate all
1910 * the memory the LSM module might require such that cred_transfer() can
1911 * complete without error.
1912 */
1913static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1914{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001915 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001916 return 0;
1917}
1918
1919
1920/**
David Howellsf1752ee2008-11-14 10:39:17 +11001921 * smack_cred_free - "free" task-level security credentials
1922 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001923 *
Casey Schauflere114e472008-02-04 22:29:50 -08001924 */
David Howellsf1752ee2008-11-14 10:39:17 +11001925static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001926{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001927 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001928 struct smack_rule *rp;
1929 struct list_head *l;
1930 struct list_head *n;
1931
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001932 smk_destroy_label_list(&tsp->smk_relabel);
1933
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001934 list_for_each_safe(l, n, &tsp->smk_rules) {
1935 rp = list_entry(l, struct smack_rule, list);
1936 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001937 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001938 }
Casey Schauflere114e472008-02-04 22:29:50 -08001939}
1940
1941/**
David Howellsd84f4f92008-11-14 10:39:23 +11001942 * smack_cred_prepare - prepare new set of credentials for modification
1943 * @new: the new credentials
1944 * @old: the original credentials
1945 * @gfp: the atomicity of any memory allocations
1946 *
1947 * Prepare a new set of credentials for modification.
1948 */
1949static int smack_cred_prepare(struct cred *new, const struct cred *old,
1950 gfp_t gfp)
1951{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001952 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001953 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001954 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001955
Casey Schauflerbbd36622018-11-12 09:30:56 -08001956 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301957
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001958 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1959 if (rc != 0)
1960 return rc;
1961
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001962 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1963 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001964 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001965}
1966
Randy Dunlap251a2a92009-02-18 11:42:33 -08001967/**
David Howellsee18d642009-09-02 09:14:21 +01001968 * smack_cred_transfer - Transfer the old credentials to the new credentials
1969 * @new: the new credentials
1970 * @old: the original credentials
1971 *
1972 * Fill in a set of blank credentials from another set of credentials.
1973 */
1974static void smack_cred_transfer(struct cred *new, const struct cred *old)
1975{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001976 struct task_smack *old_tsp = smack_cred(old);
1977 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001978
1979 new_tsp->smk_task = old_tsp->smk_task;
1980 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001981 mutex_init(&new_tsp->smk_rules_lock);
1982 INIT_LIST_HEAD(&new_tsp->smk_rules);
1983
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001984 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001985}
1986
1987/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001988 * smack_cred_getsecid - get the secid corresponding to a creds structure
1989 * @c: the object creds
1990 * @secid: where to put the result
1991 *
1992 * Sets the secid to contain a u32 version of the smack label.
1993 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001994static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001995{
1996 struct smack_known *skp;
1997
1998 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001999 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08002000 *secid = skp->smk_secid;
2001 rcu_read_unlock();
2002}
2003
2004/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002005 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002006 * @new: points to the set of credentials to be modified.
2007 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002008 *
2009 * Set the security data for a kernel service.
2010 */
2011static int smack_kernel_act_as(struct cred *new, u32 secid)
2012{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002013 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002014
Casey Schaufler152f91d2016-11-14 09:38:15 -08002015 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002016 return 0;
2017}
2018
2019/**
2020 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002021 * @new: points to the set of credentials to be modified
2022 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002023 *
2024 * Set the file creation context in a set of credentials to the same
2025 * as the objective context of the specified inode
2026 */
2027static int smack_kernel_create_files_as(struct cred *new,
2028 struct inode *inode)
2029{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002030 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002031 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002032
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002033 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002034 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002035 return 0;
2036}
2037
2038/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002039 * smk_curacc_on_task - helper to log task related access
2040 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002041 * @access: the access requested
2042 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002043 *
2044 * Return 0 if access is permitted
2045 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002046static int smk_curacc_on_task(struct task_struct *p, int access,
2047 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002048{
2049 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002050 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002051 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002052
Casey Schaufler531f1d42011-09-19 12:41:42 -07002053 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002054 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002055 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002056 rc = smk_bu_task(p, access, rc);
2057 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002058}
2059
2060/**
Casey Schauflere114e472008-02-04 22:29:50 -08002061 * smack_task_setpgid - Smack check on setting pgid
2062 * @p: the task object
2063 * @pgid: unused
2064 *
2065 * Return 0 if write access is permitted
2066 */
2067static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2068{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002069 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002070}
2071
2072/**
2073 * smack_task_getpgid - Smack access check for getpgid
2074 * @p: the object task
2075 *
2076 * Returns 0 if current can read the object task, error code otherwise
2077 */
2078static int smack_task_getpgid(struct task_struct *p)
2079{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002080 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002081}
2082
2083/**
2084 * smack_task_getsid - Smack access check for getsid
2085 * @p: the object task
2086 *
2087 * Returns 0 if current can read the object task, error code otherwise
2088 */
2089static int smack_task_getsid(struct task_struct *p)
2090{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002091 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002092}
2093
2094/**
2095 * smack_task_getsecid - get the secid of the task
2096 * @p: the object task
2097 * @secid: where to put the result
2098 *
2099 * Sets the secid to contain a u32 version of the smack label.
2100 */
2101static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2102{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002103 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002104
2105 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002106}
2107
2108/**
2109 * smack_task_setnice - Smack check on setting nice
2110 * @p: the task object
2111 * @nice: unused
2112 *
2113 * Return 0 if write access is permitted
2114 */
2115static int smack_task_setnice(struct task_struct *p, int nice)
2116{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002117 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002118}
2119
2120/**
2121 * smack_task_setioprio - Smack check on setting ioprio
2122 * @p: the task object
2123 * @ioprio: unused
2124 *
2125 * Return 0 if write access is permitted
2126 */
2127static int smack_task_setioprio(struct task_struct *p, int ioprio)
2128{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002129 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002130}
2131
2132/**
2133 * smack_task_getioprio - Smack check on reading ioprio
2134 * @p: the task object
2135 *
2136 * Return 0 if read access is permitted
2137 */
2138static int smack_task_getioprio(struct task_struct *p)
2139{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002140 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002141}
2142
2143/**
2144 * smack_task_setscheduler - Smack check on setting scheduler
2145 * @p: the task object
2146 * @policy: unused
2147 * @lp: unused
2148 *
2149 * Return 0 if read access is permitted
2150 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002151static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002152{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002153 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002154}
2155
2156/**
2157 * smack_task_getscheduler - Smack check on reading scheduler
2158 * @p: the task object
2159 *
2160 * Return 0 if read access is permitted
2161 */
2162static int smack_task_getscheduler(struct task_struct *p)
2163{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002164 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002165}
2166
2167/**
2168 * smack_task_movememory - Smack check on moving memory
2169 * @p: the task object
2170 *
2171 * Return 0 if write access is permitted
2172 */
2173static int smack_task_movememory(struct task_struct *p)
2174{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002175 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002176}
2177
2178/**
2179 * smack_task_kill - Smack check on signal delivery
2180 * @p: the task object
2181 * @info: unused
2182 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002183 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002184 *
2185 * Return 0 if write access is permitted
2186 *
Casey Schauflere114e472008-02-04 22:29:50 -08002187 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002188static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002189 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002190{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002191 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002192 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002193 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002194 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002195
Rafal Krypa18d872f2016-04-04 11:14:53 +02002196 if (!sig)
2197 return 0; /* null signal; existence test */
2198
Etienne Bassetecfcc532009-04-08 20:40:06 +02002199 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2200 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002201 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002202 * Sending a signal requires that the sender
2203 * can write the receiver.
2204 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002205 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002206 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2207 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002208 return rc;
2209 }
Casey Schauflere114e472008-02-04 22:29:50 -08002210 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002211 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002212 * specific behavior. This is not clean. For one thing
2213 * we can't take privilege into account.
2214 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002215 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002216 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2217 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002218 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002219}
2220
2221/**
Casey Schauflere114e472008-02-04 22:29:50 -08002222 * smack_task_to_inode - copy task smack into the inode blob
2223 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002224 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002225 *
2226 * Sets the smack pointer in the inode security blob
2227 */
2228static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2229{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002230 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002231 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002232
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002233 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002234 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002235}
2236
2237/*
2238 * Socket hooks.
2239 */
2240
2241/**
2242 * smack_sk_alloc_security - Allocate a socket blob
2243 * @sk: the socket
2244 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002245 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002246 *
2247 * Assign Smack pointers to current
2248 *
2249 * Returns 0 on success, -ENOMEM is there's no memory
2250 */
2251static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2252{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002253 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002254 struct socket_smack *ssp;
2255
2256 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2257 if (ssp == NULL)
2258 return -ENOMEM;
2259
jooseong lee08382c92016-11-03 11:54:39 +01002260 /*
2261 * Sockets created by kernel threads receive web label.
2262 */
2263 if (unlikely(current->flags & PF_KTHREAD)) {
2264 ssp->smk_in = &smack_known_web;
2265 ssp->smk_out = &smack_known_web;
2266 } else {
2267 ssp->smk_in = skp;
2268 ssp->smk_out = skp;
2269 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002270 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002271
2272 sk->sk_security = ssp;
2273
2274 return 0;
2275}
2276
2277/**
2278 * smack_sk_free_security - Free a socket blob
2279 * @sk: the socket
2280 *
2281 * Clears the blob pointer
2282 */
2283static void smack_sk_free_security(struct sock *sk)
2284{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302285#ifdef SMACK_IPV6_PORT_LABELING
2286 struct smk_port_label *spp;
2287
2288 if (sk->sk_family == PF_INET6) {
2289 rcu_read_lock();
2290 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2291 if (spp->smk_sock != sk)
2292 continue;
2293 spp->smk_can_reuse = 1;
2294 break;
2295 }
2296 rcu_read_unlock();
2297 }
2298#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002299 kfree(sk->sk_security);
2300}
2301
2302/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002303* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002304* @sip: the object end
2305*
2306* looks for host based access restrictions
2307*
2308* This version will only be appropriate for really small sets of single label
2309* hosts. The caller is responsible for ensuring that the RCU read lock is
2310* taken before calling this function.
2311*
2312* Returns the label of the far end or NULL if it's not special.
2313*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002314static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002315{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002316 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002317 struct in_addr *siap = &sip->sin_addr;
2318
2319 if (siap->s_addr == 0)
2320 return NULL;
2321
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002322 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2323 /*
2324 * we break after finding the first match because
2325 * the list is sorted from longest to shortest mask
2326 * so we have found the most specific match
2327 */
2328 if (snp->smk_host.s_addr ==
2329 (siap->s_addr & snp->smk_mask.s_addr))
2330 return snp->smk_label;
2331
2332 return NULL;
2333}
2334
2335#if IS_ENABLED(CONFIG_IPV6)
2336/*
2337 * smk_ipv6_localhost - Check for local ipv6 host address
2338 * @sip: the address
2339 *
2340 * Returns boolean true if this is the localhost address
2341 */
2342static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2343{
2344 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2345 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2346
2347 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2348 ntohs(be16p[7]) == 1)
2349 return true;
2350 return false;
2351}
2352
2353/**
2354* smack_ipv6host_label - check host based restrictions
2355* @sip: the object end
2356*
2357* looks for host based access restrictions
2358*
2359* This version will only be appropriate for really small sets of single label
2360* hosts. The caller is responsible for ensuring that the RCU read lock is
2361* taken before calling this function.
2362*
2363* Returns the label of the far end or NULL if it's not special.
2364*/
2365static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2366{
2367 struct smk_net6addr *snp;
2368 struct in6_addr *sap = &sip->sin6_addr;
2369 int i;
2370 int found = 0;
2371
2372 /*
2373 * It's local. Don't look for a host label.
2374 */
2375 if (smk_ipv6_localhost(sip))
2376 return NULL;
2377
2378 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002379 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002380 * If the label is NULL the entry has
2381 * been renounced. Ignore it.
2382 */
2383 if (snp->smk_label == NULL)
2384 continue;
2385 /*
Paul Moore07feee82009-03-27 17:10:54 -04002386 * we break after finding the first match because
2387 * the list is sorted from longest to shortest mask
2388 * so we have found the most specific match
2389 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002390 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002391 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2392 snp->smk_host.s6_addr16[i]) {
2393 found = 0;
2394 break;
2395 }
Etienne Basset43031542009-03-27 17:11:01 -04002396 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002397 if (found)
2398 return snp->smk_label;
2399 }
Paul Moore07feee82009-03-27 17:10:54 -04002400
2401 return NULL;
2402}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002403#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002404
2405/**
Casey Schauflere114e472008-02-04 22:29:50 -08002406 * smack_netlabel - Set the secattr on a socket
2407 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002408 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002409 *
2410 * Convert the outbound smack value (smk_out) to a
2411 * secattr and attach it to the socket.
2412 *
2413 * Returns 0 on success or an error code
2414 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002415static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002416{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002417 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002418 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002419 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002420
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002421 /*
2422 * Usually the netlabel code will handle changing the
2423 * packet labeling based on the label.
2424 * The case of a single label host is different, because
2425 * a single label host should never get a labeled packet
2426 * even though the label is usually associated with a packet
2427 * label.
2428 */
2429 local_bh_disable();
2430 bh_lock_sock_nested(sk);
2431
2432 if (ssp->smk_out == smack_net_ambient ||
2433 labeled == SMACK_UNLABELED_SOCKET)
2434 netlbl_sock_delattr(sk);
2435 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002436 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002437 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002438 }
2439
2440 bh_unlock_sock(sk);
2441 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002442
Casey Schauflere114e472008-02-04 22:29:50 -08002443 return rc;
2444}
2445
2446/**
Paul Moore07feee82009-03-27 17:10:54 -04002447 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2448 * @sk: the socket
2449 * @sap: the destination address
2450 *
2451 * Set the correct secattr for the given socket based on the destination
2452 * address and perform any outbound access checks needed.
2453 *
2454 * Returns 0 on success or an error code.
2455 *
2456 */
2457static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2458{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002459 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002460 int rc;
2461 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002462 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002463 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002464 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002465
2466 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002467 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002468 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002469#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002470 struct lsm_network_audit net;
2471
Eric Paris48c62af2012-04-02 13:15:44 -04002472 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2473 ad.a.u.net->family = sap->sin_family;
2474 ad.a.u.net->dport = sap->sin_port;
2475 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002476#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002477 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002478 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002479 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2480 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002481 } else {
2482 sk_lbl = SMACK_CIPSO_SOCKET;
2483 rc = 0;
2484 }
2485 rcu_read_unlock();
2486 if (rc != 0)
2487 return rc;
2488
2489 return smack_netlabel(sk, sk_lbl);
2490}
2491
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002492#if IS_ENABLED(CONFIG_IPV6)
2493/**
2494 * smk_ipv6_check - check Smack access
2495 * @subject: subject Smack label
2496 * @object: object Smack label
2497 * @address: address
2498 * @act: the action being taken
2499 *
2500 * Check an IPv6 access
2501 */
2502static int smk_ipv6_check(struct smack_known *subject,
2503 struct smack_known *object,
2504 struct sockaddr_in6 *address, int act)
2505{
2506#ifdef CONFIG_AUDIT
2507 struct lsm_network_audit net;
2508#endif
2509 struct smk_audit_info ad;
2510 int rc;
2511
2512#ifdef CONFIG_AUDIT
2513 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2514 ad.a.u.net->family = PF_INET6;
2515 ad.a.u.net->dport = ntohs(address->sin6_port);
2516 if (act == SMK_RECEIVING)
2517 ad.a.u.net->v6info.saddr = address->sin6_addr;
2518 else
2519 ad.a.u.net->v6info.daddr = address->sin6_addr;
2520#endif
2521 rc = smk_access(subject, object, MAY_WRITE, &ad);
2522 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2523 return rc;
2524}
2525#endif /* CONFIG_IPV6 */
2526
2527#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002528/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002529 * smk_ipv6_port_label - Smack port access table management
2530 * @sock: socket
2531 * @address: address
2532 *
2533 * Create or update the port list entry
2534 */
2535static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2536{
2537 struct sock *sk = sock->sk;
2538 struct sockaddr_in6 *addr6;
2539 struct socket_smack *ssp = sock->sk->sk_security;
2540 struct smk_port_label *spp;
2541 unsigned short port = 0;
2542
2543 if (address == NULL) {
2544 /*
2545 * This operation is changing the Smack information
2546 * on the bound socket. Take the changes to the port
2547 * as well.
2548 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302549 rcu_read_lock();
2550 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002551 if (sk != spp->smk_sock)
2552 continue;
2553 spp->smk_in = ssp->smk_in;
2554 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302555 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002556 return;
2557 }
2558 /*
2559 * A NULL address is only used for updating existing
2560 * bound entries. If there isn't one, it's OK.
2561 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302562 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002563 return;
2564 }
2565
2566 addr6 = (struct sockaddr_in6 *)address;
2567 port = ntohs(addr6->sin6_port);
2568 /*
2569 * This is a special case that is safely ignored.
2570 */
2571 if (port == 0)
2572 return;
2573
2574 /*
2575 * Look for an existing port list entry.
2576 * This is an indication that a port is getting reused.
2577 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302578 rcu_read_lock();
2579 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302580 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002581 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302582 if (spp->smk_can_reuse != 1) {
2583 rcu_read_unlock();
2584 return;
2585 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002586 spp->smk_port = port;
2587 spp->smk_sock = sk;
2588 spp->smk_in = ssp->smk_in;
2589 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302590 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302591 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002592 return;
2593 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302594 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002595 /*
2596 * A new port entry is required.
2597 */
2598 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2599 if (spp == NULL)
2600 return;
2601
2602 spp->smk_port = port;
2603 spp->smk_sock = sk;
2604 spp->smk_in = ssp->smk_in;
2605 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302606 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302607 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002608
Vishal Goel3c7ce342016-11-23 10:31:08 +05302609 mutex_lock(&smack_ipv6_lock);
2610 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2611 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002612 return;
2613}
2614
2615/**
2616 * smk_ipv6_port_check - check Smack port access
2617 * @sock: socket
2618 * @address: address
2619 *
2620 * Create or update the port list entry
2621 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002622static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 int act)
2624{
Casey Schauflerc6739442013-05-22 18:42:56 -07002625 struct smk_port_label *spp;
2626 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002627 struct smack_known *skp = NULL;
2628 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002629 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002630
2631 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002632 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002633 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002634 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002635 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002636 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002637 }
2638
2639 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002640 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002641 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002642 if (skp != NULL && object != NULL)
2643 return smk_ipv6_check(skp, object, address, act);
2644 if (skp == NULL)
2645 skp = smack_net_ambient;
2646 if (object == NULL)
2647 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002648
2649 /*
2650 * It's remote, so port lookup does no good.
2651 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002652 if (!smk_ipv6_localhost(address))
2653 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002654
2655 /*
2656 * It's local so the send check has to have passed.
2657 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002658 if (act == SMK_RECEIVING)
2659 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002660
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002661 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302662 rcu_read_lock();
2663 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302664 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002665 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002666 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002667 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002668 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002669 break;
2670 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302671 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002672
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002673 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002674}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002675#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002676
2677/**
Casey Schauflere114e472008-02-04 22:29:50 -08002678 * smack_inode_setsecurity - set smack xattrs
2679 * @inode: the object
2680 * @name: attribute name
2681 * @value: attribute value
2682 * @size: size of the attribute
2683 * @flags: unused
2684 *
2685 * Sets the named attribute in the appropriate blob
2686 *
2687 * Returns 0 on success, or an error code
2688 */
2689static int smack_inode_setsecurity(struct inode *inode, const char *name,
2690 const void *value, size_t size, int flags)
2691{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002692 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002693 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002694 struct socket_smack *ssp;
2695 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002696 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002697
Casey Schauflerf7112e62012-05-06 15:22:02 -07002698 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302699 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002700
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002701 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002702 if (IS_ERR(skp))
2703 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002704
2705 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002706 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002707 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002708 return 0;
2709 }
2710 /*
2711 * The rest of the Smack xattrs are only on sockets.
2712 */
2713 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2714 return -EOPNOTSUPP;
2715
2716 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002717 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002718 return -EOPNOTSUPP;
2719
2720 ssp = sock->sk->sk_security;
2721
2722 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002723 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002724 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002725 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002726 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002727 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2728 if (rc != 0)
2729 printk(KERN_WARNING
2730 "Smack: \"%s\" netlbl error %d.\n",
2731 __func__, -rc);
2732 }
Casey Schauflere114e472008-02-04 22:29:50 -08002733 } else
2734 return -EOPNOTSUPP;
2735
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002736#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002737 if (sock->sk->sk_family == PF_INET6)
2738 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002739#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002740
Casey Schauflere114e472008-02-04 22:29:50 -08002741 return 0;
2742}
2743
2744/**
2745 * smack_socket_post_create - finish socket setup
2746 * @sock: the socket
2747 * @family: protocol family
2748 * @type: unused
2749 * @protocol: unused
2750 * @kern: unused
2751 *
2752 * Sets the netlabel information on the socket
2753 *
2754 * Returns 0 on success, and error code otherwise
2755 */
2756static int smack_socket_post_create(struct socket *sock, int family,
2757 int type, int protocol, int kern)
2758{
Marcin Lis74123012015-01-22 15:40:33 +01002759 struct socket_smack *ssp;
2760
2761 if (sock->sk == NULL)
2762 return 0;
2763
2764 /*
2765 * Sockets created by kernel threads receive web label.
2766 */
2767 if (unlikely(current->flags & PF_KTHREAD)) {
2768 ssp = sock->sk->sk_security;
2769 ssp->smk_in = &smack_known_web;
2770 ssp->smk_out = &smack_known_web;
2771 }
2772
2773 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002774 return 0;
2775 /*
2776 * Set the outbound netlbl.
2777 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002778 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2779}
2780
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002781/**
2782 * smack_socket_socketpair - create socket pair
2783 * @socka: one socket
2784 * @sockb: another socket
2785 *
2786 * Cross reference the peer labels for SO_PEERSEC
2787 *
2788 * Returns 0 on success, and error code otherwise
2789 */
2790static int smack_socket_socketpair(struct socket *socka,
2791 struct socket *sockb)
2792{
2793 struct socket_smack *asp = socka->sk->sk_security;
2794 struct socket_smack *bsp = sockb->sk->sk_security;
2795
2796 asp->smk_packet = bsp->smk_out;
2797 bsp->smk_packet = asp->smk_out;
2798
2799 return 0;
2800}
2801
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002802#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002803/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002804 * smack_socket_bind - record port binding information.
2805 * @sock: the socket
2806 * @address: the port address
2807 * @addrlen: size of the address
2808 *
2809 * Records the label bound to a port.
2810 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002811 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002812 */
2813static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2814 int addrlen)
2815{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002816 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2817 if (addrlen < SIN6_LEN_RFC2133 ||
2818 address->sa_family != AF_INET6)
2819 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002820 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002821 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002822 return 0;
2823}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002824#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002825
2826/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002827 * smack_socket_connect - connect access check
2828 * @sock: the socket
2829 * @sap: the other end
2830 * @addrlen: size of sap
2831 *
2832 * Verifies that a connection may be possible
2833 *
2834 * Returns 0 on success, and error code otherwise
2835 */
2836static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2837 int addrlen)
2838{
Casey Schauflerc6739442013-05-22 18:42:56 -07002839 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002840#if IS_ENABLED(CONFIG_IPV6)
2841 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2842#endif
2843#ifdef SMACK_IPV6_SECMARK_LABELING
2844 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002845 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002846#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002847
Casey Schauflerc6739442013-05-22 18:42:56 -07002848 if (sock->sk == NULL)
2849 return 0;
2850
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002851#ifdef SMACK_IPV6_SECMARK_LABELING
2852 ssp = sock->sk->sk_security;
2853#endif
2854
Casey Schauflerc6739442013-05-22 18:42:56 -07002855 switch (sock->sk->sk_family) {
2856 case PF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002857 if (addrlen < sizeof(struct sockaddr_in) ||
2858 sap->sa_family != AF_INET)
Casey Schauflerc6739442013-05-22 18:42:56 -07002859 return -EINVAL;
2860 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2861 break;
2862 case PF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002863 if (addrlen < SIN6_LEN_RFC2133 || sap->sa_family != AF_INET6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002864 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002865#ifdef SMACK_IPV6_SECMARK_LABELING
2866 rsp = smack_ipv6host_label(sip);
2867 if (rsp != NULL)
2868 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002869 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002870#endif
2871#ifdef SMACK_IPV6_PORT_LABELING
2872 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2873#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002874 break;
2875 }
2876 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002877}
2878
2879/**
2880 * smack_flags_to_may - convert S_ to MAY_ values
2881 * @flags: the S_ value
2882 *
2883 * Returns the equivalent MAY_ value
2884 */
2885static int smack_flags_to_may(int flags)
2886{
2887 int may = 0;
2888
2889 if (flags & S_IRUGO)
2890 may |= MAY_READ;
2891 if (flags & S_IWUGO)
2892 may |= MAY_WRITE;
2893 if (flags & S_IXUGO)
2894 may |= MAY_EXEC;
2895
2896 return may;
2897}
2898
2899/**
2900 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2901 * @msg: the object
2902 *
2903 * Returns 0
2904 */
2905static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2906{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002907 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002908
Casey Schauflerecd5f822018-11-20 11:55:02 -08002909 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002910 return 0;
2911}
2912
2913/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002914 * smack_of_ipc - the smack pointer for the ipc
2915 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002916 *
2917 * Returns a pointer to the smack value
2918 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002919static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002920{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002921 struct smack_known **blob = smack_ipc(isp);
2922
2923 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002924}
2925
2926/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002927 * smack_ipc_alloc_security - Set the security blob for ipc
2928 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002929 *
2930 * Returns 0
2931 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002932static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002933{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002934 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002935
Casey Schaufler019bcca2018-09-21 17:19:54 -07002936 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002937 return 0;
2938}
2939
2940/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002941 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002942 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002943 * @access : access requested
2944 *
2945 * Returns 0 if current has the requested access, error code otherwise
2946 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002947static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002948{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002949 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002950 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002951 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002952
2953#ifdef CONFIG_AUDIT
2954 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002955 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002956#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002957 rc = smk_curacc(ssp, access, &ad);
2958 rc = smk_bu_current("shm", ssp, access, rc);
2959 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002960}
2961
2962/**
Casey Schauflere114e472008-02-04 22:29:50 -08002963 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002964 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002965 * @shmflg: access requested
2966 *
2967 * Returns 0 if current has the requested access, error code otherwise
2968 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002969static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002970{
Casey Schauflere114e472008-02-04 22:29:50 -08002971 int may;
2972
2973 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002974 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002975}
2976
2977/**
2978 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002979 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002980 * @cmd: what it wants to do
2981 *
2982 * Returns 0 if current has the requested access, error code otherwise
2983 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002984static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002985{
Casey Schauflere114e472008-02-04 22:29:50 -08002986 int may;
2987
2988 switch (cmd) {
2989 case IPC_STAT:
2990 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002991 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002992 may = MAY_READ;
2993 break;
2994 case IPC_SET:
2995 case SHM_LOCK:
2996 case SHM_UNLOCK:
2997 case IPC_RMID:
2998 may = MAY_READWRITE;
2999 break;
3000 case IPC_INFO:
3001 case SHM_INFO:
3002 /*
3003 * System level information.
3004 */
3005 return 0;
3006 default:
3007 return -EINVAL;
3008 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003009 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003010}
3011
3012/**
3013 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003014 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003015 * @shmaddr: unused
3016 * @shmflg: access requested
3017 *
3018 * Returns 0 if current has the requested access, error code otherwise
3019 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003020static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003021 int shmflg)
3022{
Casey Schauflere114e472008-02-04 22:29:50 -08003023 int may;
3024
3025 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003026 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003027}
3028
3029/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003030 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003031 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032 * @access : access requested
3033 *
3034 * Returns 0 if current has the requested access, error code otherwise
3035 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003036static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003037{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003038 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003039 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003040 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003041
3042#ifdef CONFIG_AUDIT
3043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003044 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003045#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003046 rc = smk_curacc(ssp, access, &ad);
3047 rc = smk_bu_current("sem", ssp, access, rc);
3048 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003049}
3050
3051/**
Casey Schauflere114e472008-02-04 22:29:50 -08003052 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003053 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003054 * @semflg: access requested
3055 *
3056 * Returns 0 if current has the requested access, error code otherwise
3057 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003058static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003059{
Casey Schauflere114e472008-02-04 22:29:50 -08003060 int may;
3061
3062 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003063 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003064}
3065
3066/**
3067 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003068 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003069 * @cmd: what it wants to do
3070 *
3071 * Returns 0 if current has the requested access, error code otherwise
3072 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003073static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003074{
Casey Schauflere114e472008-02-04 22:29:50 -08003075 int may;
3076
3077 switch (cmd) {
3078 case GETPID:
3079 case GETNCNT:
3080 case GETZCNT:
3081 case GETVAL:
3082 case GETALL:
3083 case IPC_STAT:
3084 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003085 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003086 may = MAY_READ;
3087 break;
3088 case SETVAL:
3089 case SETALL:
3090 case IPC_RMID:
3091 case IPC_SET:
3092 may = MAY_READWRITE;
3093 break;
3094 case IPC_INFO:
3095 case SEM_INFO:
3096 /*
3097 * System level information
3098 */
3099 return 0;
3100 default:
3101 return -EINVAL;
3102 }
3103
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003104 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003105}
3106
3107/**
3108 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003109 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003110 * @sops: unused
3111 * @nsops: unused
3112 * @alter: unused
3113 *
3114 * Treated as read and write in all cases.
3115 *
3116 * Returns 0 if access is allowed, error code otherwise
3117 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003118static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003119 unsigned nsops, int alter)
3120{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003121 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003122}
3123
3124/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003125 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003126 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003127 * @access : access requested
3128 *
3129 * return 0 if current has access, error otherwise
3130 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003131static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003132{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003133 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003134 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003135 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003136
3137#ifdef CONFIG_AUDIT
3138 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003139 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003140#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003141 rc = smk_curacc(msp, access, &ad);
3142 rc = smk_bu_current("msq", msp, access, rc);
3143 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003144}
3145
3146/**
Casey Schauflere114e472008-02-04 22:29:50 -08003147 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003148 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003149 * @msqflg: access requested
3150 *
3151 * Returns 0 if current has the requested access, error code otherwise
3152 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003153static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003154{
Casey Schauflere114e472008-02-04 22:29:50 -08003155 int may;
3156
3157 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003158 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003159}
3160
3161/**
3162 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003163 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003164 * @cmd: what it wants to do
3165 *
3166 * Returns 0 if current has the requested access, error code otherwise
3167 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003168static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003169{
Casey Schauflere114e472008-02-04 22:29:50 -08003170 int may;
3171
3172 switch (cmd) {
3173 case IPC_STAT:
3174 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003175 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003176 may = MAY_READ;
3177 break;
3178 case IPC_SET:
3179 case IPC_RMID:
3180 may = MAY_READWRITE;
3181 break;
3182 case IPC_INFO:
3183 case MSG_INFO:
3184 /*
3185 * System level information
3186 */
3187 return 0;
3188 default:
3189 return -EINVAL;
3190 }
3191
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003192 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003193}
3194
3195/**
3196 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003197 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003198 * @msg: unused
3199 * @msqflg: access requested
3200 *
3201 * Returns 0 if current has the requested access, error code otherwise
3202 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003203static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003204 int msqflg)
3205{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003206 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003207
Etienne Bassetecfcc532009-04-08 20:40:06 +02003208 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003209 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003210}
3211
3212/**
3213 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003214 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003215 * @msg: unused
3216 * @target: unused
3217 * @type: unused
3218 * @mode: unused
3219 *
3220 * Returns 0 if current has read and write access, error code otherwise
3221 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003222static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003223 struct task_struct *target, long type, int mode)
3224{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003225 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003226}
3227
3228/**
3229 * smack_ipc_permission - Smack access for ipc_permission()
3230 * @ipp: the object permissions
3231 * @flag: access requested
3232 *
3233 * Returns 0 if current has read and write access, error code otherwise
3234 */
3235static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3236{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003237 struct smack_known **blob = smack_ipc(ipp);
3238 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003239 int may = smack_flags_to_may(flag);
3240 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003241 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003242
Etienne Bassetecfcc532009-04-08 20:40:06 +02003243#ifdef CONFIG_AUDIT
3244 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3245 ad.a.u.ipc_id = ipp->id;
3246#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003247 rc = smk_curacc(iskp, may, &ad);
3248 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003249 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003250}
3251
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003252/**
3253 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003254 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003255 * @secid: where result will be saved
3256 */
3257static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3258{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003259 struct smack_known **blob = smack_ipc(ipp);
3260 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003261
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003262 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003263}
3264
Casey Schauflere114e472008-02-04 22:29:50 -08003265/**
3266 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003267 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003268 * @inode: the object
3269 *
3270 * Set the inode's security blob if it hasn't been done already.
3271 */
3272static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3273{
3274 struct super_block *sbp;
3275 struct superblock_smack *sbsp;
3276 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003277 struct smack_known *skp;
3278 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003279 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003280 char trattr[TRANS_TRUE_SIZE];
3281 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003282 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003283 struct dentry *dp;
3284
3285 if (inode == NULL)
3286 return;
3287
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003288 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003289
3290 mutex_lock(&isp->smk_lock);
3291 /*
3292 * If the inode is already instantiated
3293 * take the quick way out
3294 */
3295 if (isp->smk_flags & SMK_INODE_INSTANT)
3296 goto unlockandout;
3297
3298 sbp = inode->i_sb;
3299 sbsp = sbp->s_security;
3300 /*
3301 * We're going to use the superblock default label
3302 * if there's no label on the file.
3303 */
3304 final = sbsp->smk_default;
3305
3306 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003307 * If this is the root inode the superblock
3308 * may be in the process of initialization.
3309 * If that is the case use the root value out
3310 * of the superblock.
3311 */
3312 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003313 switch (sbp->s_magic) {
3314 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003315 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003316 /*
3317 * The cgroup filesystem is never mounted,
3318 * so there's no opportunity to set the mount
3319 * options.
3320 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003321 sbsp->smk_root = &smack_known_star;
3322 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003323 isp->smk_inode = sbsp->smk_root;
3324 break;
3325 case TMPFS_MAGIC:
3326 /*
3327 * What about shmem/tmpfs anonymous files with dentry
3328 * obtained from d_alloc_pseudo()?
3329 */
3330 isp->smk_inode = smk_of_current();
3331 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003332 case PIPEFS_MAGIC:
3333 isp->smk_inode = smk_of_current();
3334 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003335 case SOCKFS_MAGIC:
3336 /*
3337 * Socket access is controlled by the socket
3338 * structures associated with the task involved.
3339 */
3340 isp->smk_inode = &smack_known_star;
3341 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003342 default:
3343 isp->smk_inode = sbsp->smk_root;
3344 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003345 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003346 isp->smk_flags |= SMK_INODE_INSTANT;
3347 goto unlockandout;
3348 }
3349
3350 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003351 * This is pretty hackish.
3352 * Casey says that we shouldn't have to do
3353 * file system specific code, but it does help
3354 * with keeping it simple.
3355 */
3356 switch (sbp->s_magic) {
3357 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003358 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003359 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003360 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003361 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003362 * that the smack file system doesn't do
3363 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003364 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003365 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003366 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003367 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003368 break;
3369 case DEVPTS_SUPER_MAGIC:
3370 /*
3371 * devpts seems content with the label of the task.
3372 * Programs that change smack have to treat the
3373 * pty with respect.
3374 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003375 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003376 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003377 case PROC_SUPER_MAGIC:
3378 /*
3379 * Casey says procfs appears not to care.
3380 * The superblock default suffices.
3381 */
3382 break;
3383 case TMPFS_MAGIC:
3384 /*
3385 * Device labels should come from the filesystem,
3386 * but watch out, because they're volitile,
3387 * getting recreated on every reboot.
3388 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003389 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003390 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003391 * If a smack value has been set we want to use it,
3392 * but since tmpfs isn't giving us the opportunity
3393 * to set mount options simulate setting the
3394 * superblock default.
3395 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003396 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003397 default:
3398 /*
3399 * This isn't an understood special case.
3400 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003401 */
3402
3403 /*
3404 * UNIX domain sockets use lower level socket data.
3405 */
3406 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003407 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003408 break;
3409 }
3410 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003411 * No xattr support means, alas, no SMACK label.
3412 * Use the aforeapplied default.
3413 * It would be curious if the label of the task
3414 * does not match that assigned.
3415 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003416 if (!(inode->i_opflags & IOP_XATTR))
3417 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003418 /*
3419 * Get the dentry for xattr.
3420 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003421 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003422 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003423 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003424 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003425
3426 /*
3427 * Transmuting directory
3428 */
3429 if (S_ISDIR(inode->i_mode)) {
3430 /*
3431 * If this is a new directory and the label was
3432 * transmuted when the inode was initialized
3433 * set the transmute attribute on the directory
3434 * and mark the inode.
3435 *
3436 * If there is a transmute attribute on the
3437 * directory mark the inode.
3438 */
3439 if (isp->smk_flags & SMK_INODE_CHANGED) {
3440 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003441 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003442 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003443 TRANS_TRUE, TRANS_TRUE_SIZE,
3444 0);
3445 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003446 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003447 XATTR_NAME_SMACKTRANSMUTE, trattr,
3448 TRANS_TRUE_SIZE);
3449 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3450 TRANS_TRUE_SIZE) != 0)
3451 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003452 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003453 if (rc >= 0)
3454 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003455 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003456 /*
3457 * Don't let the exec or mmap label be "*" or "@".
3458 */
3459 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3460 if (IS_ERR(skp) || skp == &smack_known_star ||
3461 skp == &smack_known_web)
3462 skp = NULL;
3463 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003464
Casey Schaufler19760ad2013-12-16 16:27:26 -08003465 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003466 if (IS_ERR(skp) || skp == &smack_known_star ||
3467 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003468 skp = NULL;
3469 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003470
Casey Schauflere114e472008-02-04 22:29:50 -08003471 dput(dp);
3472 break;
3473 }
3474
3475 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003476 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003477 else
3478 isp->smk_inode = final;
3479
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003480 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003481
3482unlockandout:
3483 mutex_unlock(&isp->smk_lock);
3484 return;
3485}
3486
3487/**
3488 * smack_getprocattr - Smack process attribute access
3489 * @p: the object task
3490 * @name: the name of the attribute in /proc/.../attr
3491 * @value: where to put the result
3492 *
3493 * Places a copy of the task Smack into value
3494 *
3495 * Returns the length of the smack label or an error code
3496 */
3497static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3498{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003499 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003500 char *cp;
3501 int slen;
3502
3503 if (strcmp(name, "current") != 0)
3504 return -EINVAL;
3505
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003506 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003507 if (cp == NULL)
3508 return -ENOMEM;
3509
3510 slen = strlen(cp);
3511 *value = cp;
3512 return slen;
3513}
3514
3515/**
3516 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003517 * @name: the name of the attribute in /proc/.../attr
3518 * @value: the value to set
3519 * @size: the size of the value
3520 *
3521 * Sets the Smack value of the task. Only setting self
3522 * is permitted and only with privilege
3523 *
3524 * Returns the length of the smack label or an error code
3525 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003526static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003527{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003528 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003529 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003530 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003531 struct smack_known_list_elem *sklep;
3532 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003533
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003534 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003535 return -EPERM;
3536
Casey Schauflerf7112e62012-05-06 15:22:02 -07003537 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003538 return -EINVAL;
3539
3540 if (strcmp(name, "current") != 0)
3541 return -EINVAL;
3542
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003543 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003544 if (IS_ERR(skp))
3545 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003546
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003547 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303548 * No process is ever allowed the web ("@") label
3549 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003550 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303551 if (skp == &smack_known_web || skp == &smack_known_star)
3552 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003553
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003554 if (!smack_privileged(CAP_MAC_ADMIN)) {
3555 rc = -EPERM;
3556 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3557 if (sklep->smk_label == skp) {
3558 rc = 0;
3559 break;
3560 }
3561 if (rc)
3562 return rc;
3563 }
3564
David Howellsd84f4f92008-11-14 10:39:23 +11003565 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003566 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003567 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003568
Casey Schauflerb17103a2018-11-09 16:12:56 -08003569 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003570 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003571 /*
3572 * process can change its label only once
3573 */
3574 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003575
David Howellsd84f4f92008-11-14 10:39:23 +11003576 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003577 return size;
3578}
3579
3580/**
3581 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003582 * @sock: one sock
3583 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003584 * @newsk: unused
3585 *
3586 * Return 0 if a subject with the smack of sock could access
3587 * an object with the smack of other, otherwise an error code
3588 */
David S. Miller3610cda2011-01-05 15:38:53 -08003589static int smack_unix_stream_connect(struct sock *sock,
3590 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003591{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003592 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003593 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003594 struct socket_smack *ssp = sock->sk_security;
3595 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003596 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003597 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003598 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003599#ifdef CONFIG_AUDIT
3600 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003601#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003602
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003603 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3604 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003605 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003606#ifdef CONFIG_AUDIT
3607 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3608 smk_ad_setfield_u_net_sk(&ad, other);
3609#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003610 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3611 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003612 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003613 okp = osp->smk_out;
3614 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003615 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003616 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003617 MAY_WRITE, rc);
3618 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003619 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003620
Casey Schaufler975d5e52011-09-26 14:43:39 -07003621 /*
3622 * Cross reference the peer labels for SO_PEERSEC.
3623 */
3624 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003625 nsp->smk_packet = ssp->smk_out;
3626 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003627 }
3628
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003629 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003630}
3631
3632/**
3633 * smack_unix_may_send - Smack access on UDS
3634 * @sock: one socket
3635 * @other: the other socket
3636 *
3637 * Return 0 if a subject with the smack of sock could access
3638 * an object with the smack of other, otherwise an error code
3639 */
3640static int smack_unix_may_send(struct socket *sock, struct socket *other)
3641{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003642 struct socket_smack *ssp = sock->sk->sk_security;
3643 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003644 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003645 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003646
Kees Cook923e9a12012-04-10 13:26:44 -07003647#ifdef CONFIG_AUDIT
3648 struct lsm_network_audit net;
3649
Eric Paris48c62af2012-04-02 13:15:44 -04003650 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003651 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003652#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003653
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003654 if (smack_privileged(CAP_MAC_OVERRIDE))
3655 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003656
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003657 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3658 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003659 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003660}
3661
3662/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003663 * smack_socket_sendmsg - Smack check based on destination host
3664 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003665 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003666 * @size: the size of the message
3667 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003668 * Return 0 if the current subject can write to the destination host.
3669 * For IPv4 this is only a question if the destination is a single label host.
3670 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003671 */
3672static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3673 int size)
3674{
3675 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003676#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003677 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003678#endif
3679#ifdef SMACK_IPV6_SECMARK_LABELING
3680 struct socket_smack *ssp = sock->sk->sk_security;
3681 struct smack_known *rsp;
3682#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003683 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003684
3685 /*
3686 * Perfectly reasonable for this to be NULL
3687 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003688 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003689 return 0;
3690
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003691 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003692 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003693 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3694 sip->sin_family != AF_INET)
3695 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003696 rc = smack_netlabel_send(sock->sk, sip);
3697 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003698#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003699 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003700 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3701 sap->sin6_family != AF_INET6)
3702 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003703#ifdef SMACK_IPV6_SECMARK_LABELING
3704 rsp = smack_ipv6host_label(sap);
3705 if (rsp != NULL)
3706 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3707 SMK_CONNECTING);
3708#endif
3709#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003710 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003711#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003712#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003713 break;
3714 }
3715 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003716}
3717
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003718/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003719 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003720 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003721 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003722 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003723 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003724 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003725static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3726 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003727{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003728 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003729 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003730 int acat;
3731 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003732
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003733 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003734 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003735 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003736 * If there are flags but no level netlabel isn't
3737 * behaving the way we expect it to.
3738 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003739 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003740 * Without guidance regarding the smack value
3741 * for the packet fall back on the network
3742 * ambient value.
3743 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003744 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303745 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003746 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003747 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003748 /*
3749 * Compare the catsets. Use the netlbl APIs.
3750 */
3751 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3752 if ((skp->smk_netlabel.flags &
3753 NETLBL_SECATTR_MLS_CAT) == 0)
3754 found = 1;
3755 break;
3756 }
3757 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003758 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3759 acat + 1);
3760 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003761 skp->smk_netlabel.attr.mls.cat,
3762 kcat + 1);
3763 if (acat < 0 || kcat < 0)
3764 break;
3765 }
3766 if (acat == kcat) {
3767 found = 1;
3768 break;
3769 }
Casey Schauflere114e472008-02-04 22:29:50 -08003770 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003771 rcu_read_unlock();
3772
3773 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003774 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003775
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003776 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003777 return &smack_known_web;
3778 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003779 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003780 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003781 /*
3782 * Looks like a fallback, which gives us a secid.
3783 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003784 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003785 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003786 * Without guidance regarding the smack value
3787 * for the packet fall back on the network
3788 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003789 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003790 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003791}
3792
Casey Schaufler69f287a2014-12-12 17:08:40 -08003793#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003794static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003795{
Casey Schauflerc6739442013-05-22 18:42:56 -07003796 u8 nexthdr;
3797 int offset;
3798 int proto = -EINVAL;
3799 struct ipv6hdr _ipv6h;
3800 struct ipv6hdr *ip6;
3801 __be16 frag_off;
3802 struct tcphdr _tcph, *th;
3803 struct udphdr _udph, *uh;
3804 struct dccp_hdr _dccph, *dh;
3805
3806 sip->sin6_port = 0;
3807
3808 offset = skb_network_offset(skb);
3809 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3810 if (ip6 == NULL)
3811 return -EINVAL;
3812 sip->sin6_addr = ip6->saddr;
3813
3814 nexthdr = ip6->nexthdr;
3815 offset += sizeof(_ipv6h);
3816 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3817 if (offset < 0)
3818 return -EINVAL;
3819
3820 proto = nexthdr;
3821 switch (proto) {
3822 case IPPROTO_TCP:
3823 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3824 if (th != NULL)
3825 sip->sin6_port = th->source;
3826 break;
3827 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003828 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003829 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3830 if (uh != NULL)
3831 sip->sin6_port = uh->source;
3832 break;
3833 case IPPROTO_DCCP:
3834 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3835 if (dh != NULL)
3836 sip->sin6_port = dh->dccph_sport;
3837 break;
3838 }
3839 return proto;
3840}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003841#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003842
Casey Schauflere114e472008-02-04 22:29:50 -08003843/**
3844 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3845 * @sk: socket
3846 * @skb: packet
3847 *
3848 * Returns 0 if the packet should be delivered, an error code otherwise
3849 */
3850static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3851{
3852 struct netlbl_lsm_secattr secattr;
3853 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003854 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003855 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003856 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003857 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003858#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003859 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003860#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003861#if IS_ENABLED(CONFIG_IPV6)
3862 struct sockaddr_in6 sadd;
3863 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003864
3865 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3866 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003867#endif /* CONFIG_IPV6 */
3868
Piotr Sawicki129a9982018-07-19 11:42:58 +02003869 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003870 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003871#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3872 /*
3873 * If there is a secmark use it rather than the CIPSO label.
3874 * If there is no secmark fall back to CIPSO.
3875 * The secmark is assumed to reflect policy better.
3876 */
3877 if (skb && skb->secmark != 0) {
3878 skp = smack_from_secid(skb->secmark);
3879 goto access_check;
3880 }
3881#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003882 /*
3883 * Translate what netlabel gave us.
3884 */
3885 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003886
Piotr Sawicki129a9982018-07-19 11:42:58 +02003887 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003888 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003889 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003890 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003891 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003892
Casey Schauflerc6739442013-05-22 18:42:56 -07003893 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003894
Casey Schaufler69f287a2014-12-12 17:08:40 -08003895#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3896access_check:
3897#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003898#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003899 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003900 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003901 ad.a.u.net->netif = skb->skb_iif;
3902 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003903#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003904 /*
3905 * Receiving a packet requires that the other end
3906 * be able to write here. Read access is not required.
3907 * This is the simplist possible security model
3908 * for networking.
3909 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003910 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3911 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003912 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003913 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003914 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003915 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003917 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003918 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003919 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3920 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003921 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003922#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003923 if (skb && skb->secmark != 0)
3924 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003925 else if (smk_ipv6_localhost(&sadd))
3926 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003927 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003928 skp = smack_ipv6host_label(&sadd);
3929 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003930 skp = smack_net_ambient;
3931#ifdef CONFIG_AUDIT
3932 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003933 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003934 ad.a.u.net->netif = skb->skb_iif;
3935 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3936#endif /* CONFIG_AUDIT */
3937 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3938 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3939 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003940#endif /* SMACK_IPV6_SECMARK_LABELING */
3941#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003942 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003943#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003944 if (rc != 0)
3945 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3946 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003947 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003948#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003949 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003950
Paul Moorea8134292008-10-10 10:16:31 -04003951 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003952}
3953
3954/**
3955 * smack_socket_getpeersec_stream - pull in packet label
3956 * @sock: the socket
3957 * @optval: user's destination
3958 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003959 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003960 *
3961 * returns zero on success, an error code otherwise
3962 */
3963static int smack_socket_getpeersec_stream(struct socket *sock,
3964 char __user *optval,
3965 int __user *optlen, unsigned len)
3966{
3967 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003968 char *rcp = "";
3969 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003970 int rc = 0;
3971
3972 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003973 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003974 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003975 slen = strlen(rcp) + 1;
3976 }
Casey Schauflere114e472008-02-04 22:29:50 -08003977
3978 if (slen > len)
3979 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003980 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003981 rc = -EFAULT;
3982
3983 if (put_user(slen, optlen) != 0)
3984 rc = -EFAULT;
3985
3986 return rc;
3987}
3988
3989
3990/**
3991 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003992 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003993 * @skb: packet data
3994 * @secid: pointer to where to put the secid of the packet
3995 *
3996 * Sets the netlabel socket state on sk from parent
3997 */
3998static int smack_socket_getpeersec_dgram(struct socket *sock,
3999 struct sk_buff *skb, u32 *secid)
4000
4001{
4002 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004003 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004004 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004005 int family = PF_UNSPEC;
4006 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004007 int rc;
4008
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004009 if (skb != NULL) {
4010 if (skb->protocol == htons(ETH_P_IP))
4011 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004012#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004013 else if (skb->protocol == htons(ETH_P_IPV6))
4014 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004015#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004016 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004017 if (family == PF_UNSPEC && sock != NULL)
4018 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004019
Casey Schaufler69f287a2014-12-12 17:08:40 -08004020 switch (family) {
4021 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004022 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004023 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004024 break;
4025 case PF_INET:
4026#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4027 s = skb->secmark;
4028 if (s != 0)
4029 break;
4030#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004031 /*
4032 * Translate what netlabel gave us.
4033 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004034 if (sock != NULL && sock->sk != NULL)
4035 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004036 netlbl_secattr_init(&secattr);
4037 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4038 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004039 skp = smack_from_secattr(&secattr, ssp);
4040 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004041 }
4042 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004043 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004044 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004045#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004046 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004047#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004048 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004049 }
4050 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004051 if (s == 0)
4052 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004053 return 0;
4054}
4055
4056/**
Paul Moore07feee82009-03-27 17:10:54 -04004057 * smack_sock_graft - Initialize a newly created socket with an existing sock
4058 * @sk: child sock
4059 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004060 *
Paul Moore07feee82009-03-27 17:10:54 -04004061 * Set the smk_{in,out} state of an existing sock based on the process that
4062 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004063 */
4064static void smack_sock_graft(struct sock *sk, struct socket *parent)
4065{
4066 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004067 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004068
Paul Moore07feee82009-03-27 17:10:54 -04004069 if (sk == NULL ||
4070 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004071 return;
4072
4073 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004074 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004075 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004076 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004077}
4078
4079/**
4080 * smack_inet_conn_request - Smack access check on connect
4081 * @sk: socket involved
4082 * @skb: packet
4083 * @req: unused
4084 *
4085 * Returns 0 if a task with the packet label could write to
4086 * the socket, otherwise an error code
4087 */
4088static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4089 struct request_sock *req)
4090{
Paul Moore07feee82009-03-27 17:10:54 -04004091 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004092 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004093 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004094 struct netlbl_lsm_secattr secattr;
4095 struct sockaddr_in addr;
4096 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004097 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004098 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004099 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004100#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004101 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004102#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004103
Casey Schaufler69f287a2014-12-12 17:08:40 -08004104#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004105 if (family == PF_INET6) {
4106 /*
4107 * Handle mapped IPv4 packets arriving
4108 * via IPv6 sockets. Don't set up netlabel
4109 * processing on IPv6.
4110 */
4111 if (skb->protocol == htons(ETH_P_IP))
4112 family = PF_INET;
4113 else
4114 return 0;
4115 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004116#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004117
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004118#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4119 /*
4120 * If there is a secmark use it rather than the CIPSO label.
4121 * If there is no secmark fall back to CIPSO.
4122 * The secmark is assumed to reflect policy better.
4123 */
4124 if (skb && skb->secmark != 0) {
4125 skp = smack_from_secid(skb->secmark);
4126 goto access_check;
4127 }
4128#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4129
Paul Moore07feee82009-03-27 17:10:54 -04004130 netlbl_secattr_init(&secattr);
4131 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004132 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004133 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004134 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004135 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004136 netlbl_secattr_destroy(&secattr);
4137
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004138#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4139access_check:
4140#endif
4141
Etienne Bassetecfcc532009-04-08 20:40:06 +02004142#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004143 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4144 ad.a.u.net->family = family;
4145 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004146 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4147#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004148 /*
Paul Moore07feee82009-03-27 17:10:54 -04004149 * Receiving a packet requires that the other end be able to write
4150 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004151 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004152 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4153 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004154 if (rc != 0)
4155 return rc;
4156
4157 /*
4158 * Save the peer's label in the request_sock so we can later setup
4159 * smk_packet in the child socket so that SO_PEERCRED can report it.
4160 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004161 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004162
4163 /*
4164 * We need to decide if we want to label the incoming connection here
4165 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004166 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004167 */
4168 hdr = ip_hdr(skb);
4169 addr.sin_addr.s_addr = hdr->saddr;
4170 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004171 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004172 rcu_read_unlock();
4173
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004174 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004175 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004176 else
Paul Moore07feee82009-03-27 17:10:54 -04004177 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004178
4179 return rc;
4180}
4181
Paul Moore07feee82009-03-27 17:10:54 -04004182/**
4183 * smack_inet_csk_clone - Copy the connection information to the new socket
4184 * @sk: the new socket
4185 * @req: the connection's request_sock
4186 *
4187 * Transfer the connection's peer label to the newly created socket.
4188 */
4189static void smack_inet_csk_clone(struct sock *sk,
4190 const struct request_sock *req)
4191{
4192 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004193 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004194
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004195 if (req->peer_secid != 0) {
4196 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004197 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004198 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004199 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004200}
4201
Casey Schauflere114e472008-02-04 22:29:50 -08004202/*
4203 * Key management security hooks
4204 *
4205 * Casey has not tested key support very heavily.
4206 * The permission check is most likely too restrictive.
4207 * If you care about keys please have a look.
4208 */
4209#ifdef CONFIG_KEYS
4210
4211/**
4212 * smack_key_alloc - Set the key security blob
4213 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004214 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004215 * @flags: unused
4216 *
4217 * No allocation required
4218 *
4219 * Returns 0
4220 */
David Howellsd84f4f92008-11-14 10:39:23 +11004221static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004222 unsigned long flags)
4223{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004224 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004225
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004226 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004227 return 0;
4228}
4229
4230/**
4231 * smack_key_free - Clear the key security blob
4232 * @key: the object
4233 *
4234 * Clear the blob pointer
4235 */
4236static void smack_key_free(struct key *key)
4237{
4238 key->security = NULL;
4239}
4240
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004241/**
Casey Schauflere114e472008-02-04 22:29:50 -08004242 * smack_key_permission - Smack access on a key
4243 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004244 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004245 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004246 *
4247 * Return 0 if the task has read and write to the object,
4248 * an error code otherwise
4249 */
4250static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004251 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004252{
4253 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004254 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004255 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004256 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004257 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004258
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004259 /*
4260 * Validate requested permissions
4261 */
4262 if (perm & ~KEY_NEED_ALL)
4263 return -EINVAL;
4264
Casey Schauflere114e472008-02-04 22:29:50 -08004265 keyp = key_ref_to_ptr(key_ref);
4266 if (keyp == NULL)
4267 return -EINVAL;
4268 /*
4269 * If the key hasn't been initialized give it access so that
4270 * it may do so.
4271 */
4272 if (keyp->security == NULL)
4273 return 0;
4274 /*
4275 * This should not occur
4276 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004277 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004278 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004279
4280 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4281 return 0;
4282
Etienne Bassetecfcc532009-04-08 20:40:06 +02004283#ifdef CONFIG_AUDIT
4284 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4285 ad.a.u.key_struct.key = keyp->serial;
4286 ad.a.u.key_struct.key_desc = keyp->description;
4287#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004288 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4289 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004290 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004291 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004292 rc = smk_access(tkp, keyp->security, request, &ad);
4293 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4294 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004295}
José Bollo7fc5f362015-02-17 15:41:22 +01004296
4297/*
4298 * smack_key_getsecurity - Smack label tagging the key
4299 * @key points to the key to be queried
4300 * @_buffer points to a pointer that should be set to point to the
4301 * resulting string (if no label or an error occurs).
4302 * Return the length of the string (including terminating NUL) or -ve if
4303 * an error.
4304 * May also return 0 (and a NULL buffer pointer) if there is no label.
4305 */
4306static int smack_key_getsecurity(struct key *key, char **_buffer)
4307{
4308 struct smack_known *skp = key->security;
4309 size_t length;
4310 char *copy;
4311
4312 if (key->security == NULL) {
4313 *_buffer = NULL;
4314 return 0;
4315 }
4316
4317 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4318 if (copy == NULL)
4319 return -ENOMEM;
4320 length = strlen(copy) + 1;
4321
4322 *_buffer = copy;
4323 return length;
4324}
4325
Casey Schauflere114e472008-02-04 22:29:50 -08004326#endif /* CONFIG_KEYS */
4327
4328/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004329 * Smack Audit hooks
4330 *
4331 * Audit requires a unique representation of each Smack specific
4332 * rule. This unique representation is used to distinguish the
4333 * object to be audited from remaining kernel objects and also
4334 * works as a glue between the audit hooks.
4335 *
4336 * Since repository entries are added but never deleted, we'll use
4337 * the smack_known label address related to the given audit rule as
4338 * the needed unique representation. This also better fits the smack
4339 * model where nearly everything is a label.
4340 */
4341#ifdef CONFIG_AUDIT
4342
4343/**
4344 * smack_audit_rule_init - Initialize a smack audit rule
4345 * @field: audit rule fields given from user-space (audit.h)
4346 * @op: required testing operator (=, !=, >, <, ...)
4347 * @rulestr: smack label to be audited
4348 * @vrule: pointer to save our own audit rule representation
4349 *
4350 * Prepare to audit cases where (@field @op @rulestr) is true.
4351 * The label to be audited is created if necessay.
4352 */
4353static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4354{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004355 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004356 char **rule = (char **)vrule;
4357 *rule = NULL;
4358
4359 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4360 return -EINVAL;
4361
Al Viro5af75d82008-12-16 05:59:26 -05004362 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004363 return -EINVAL;
4364
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004365 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004366 if (IS_ERR(skp))
4367 return PTR_ERR(skp);
4368
4369 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004370
4371 return 0;
4372}
4373
4374/**
4375 * smack_audit_rule_known - Distinguish Smack audit rules
4376 * @krule: rule of interest, in Audit kernel representation format
4377 *
4378 * This is used to filter Smack rules from remaining Audit ones.
4379 * If it's proved that this rule belongs to us, the
4380 * audit_rule_match hook will be called to do the final judgement.
4381 */
4382static int smack_audit_rule_known(struct audit_krule *krule)
4383{
4384 struct audit_field *f;
4385 int i;
4386
4387 for (i = 0; i < krule->field_count; i++) {
4388 f = &krule->fields[i];
4389
4390 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4391 return 1;
4392 }
4393
4394 return 0;
4395}
4396
4397/**
4398 * smack_audit_rule_match - Audit given object ?
4399 * @secid: security id for identifying the object to test
4400 * @field: audit rule flags given from user-space
4401 * @op: required testing operator
4402 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004403 *
4404 * The core Audit hook. It's used to take the decision of
4405 * whether to audit or not to audit a given object.
4406 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004407static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004408{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004409 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004410 char *rule = vrule;
4411
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004412 if (unlikely(!rule)) {
4413 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004414 return -ENOENT;
4415 }
4416
4417 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4418 return 0;
4419
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004420 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004421
4422 /*
4423 * No need to do string comparisons. If a match occurs,
4424 * both pointers will point to the same smack_known
4425 * label.
4426 */
Al Viro5af75d82008-12-16 05:59:26 -05004427 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004428 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004429 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004430 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004431
4432 return 0;
4433}
4434
Casey Schaufler491a0b02016-01-26 15:08:35 -08004435/*
4436 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004437 * No memory was allocated.
4438 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004439
4440#endif /* CONFIG_AUDIT */
4441
Randy Dunlap251a2a92009-02-18 11:42:33 -08004442/**
David Quigley746df9b2013-05-22 12:50:35 -04004443 * smack_ismaclabel - check if xattr @name references a smack MAC label
4444 * @name: Full xattr name to check.
4445 */
4446static int smack_ismaclabel(const char *name)
4447{
4448 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4449}
4450
4451
4452/**
Casey Schauflere114e472008-02-04 22:29:50 -08004453 * smack_secid_to_secctx - return the smack label for a secid
4454 * @secid: incoming integer
4455 * @secdata: destination
4456 * @seclen: how long it is
4457 *
4458 * Exists for networking code.
4459 */
4460static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4461{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004462 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004463
Eric Parisd5630b92010-10-13 16:24:48 -04004464 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004465 *secdata = skp->smk_known;
4466 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004467 return 0;
4468}
4469
Randy Dunlap251a2a92009-02-18 11:42:33 -08004470/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004471 * smack_secctx_to_secid - return the secid for a smack label
4472 * @secdata: smack label
4473 * @seclen: how long result is
4474 * @secid: outgoing integer
4475 *
4476 * Exists for audit and networking code.
4477 */
David Howellse52c17642008-04-29 20:52:51 +01004478static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004479{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004480 struct smack_known *skp = smk_find_entry(secdata);
4481
4482 if (skp)
4483 *secid = skp->smk_secid;
4484 else
4485 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004486 return 0;
4487}
4488
Casey Schaufler491a0b02016-01-26 15:08:35 -08004489/*
4490 * There used to be a smack_release_secctx hook
4491 * that did nothing back when hooks were in a vector.
4492 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004493 */
Casey Schauflere114e472008-02-04 22:29:50 -08004494
David P. Quigley1ee65e32009-09-03 14:25:57 -04004495static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4496{
4497 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4498}
4499
4500static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4501{
4502 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4503}
4504
4505static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4506{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004507 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004508
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004509 *ctx = skp->smk_known;
4510 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004511 return 0;
4512}
4513
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004514static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4515{
4516
4517 struct task_smack *tsp;
4518 struct smack_known *skp;
4519 struct inode_smack *isp;
4520 struct cred *new_creds = *new;
4521
4522 if (new_creds == NULL) {
4523 new_creds = prepare_creds();
4524 if (new_creds == NULL)
4525 return -ENOMEM;
4526 }
4527
Casey Schauflerb17103a2018-11-09 16:12:56 -08004528 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004529
4530 /*
4531 * Get label from overlay inode and set it in create_sid
4532 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004533 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004534 skp = isp->smk_inode;
4535 tsp->smk_task = skp;
4536 *new = new_creds;
4537 return 0;
4538}
4539
4540static int smack_inode_copy_up_xattr(const char *name)
4541{
4542 /*
4543 * Return 1 if this is the smack access Smack attribute.
4544 */
4545 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4546 return 1;
4547
4548 return -EOPNOTSUPP;
4549}
4550
4551static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4552 struct qstr *name,
4553 const struct cred *old,
4554 struct cred *new)
4555{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004556 struct task_smack *otsp = smack_cred(old);
4557 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004558 struct inode_smack *isp;
4559 int may;
4560
4561 /*
4562 * Use the process credential unless all of
4563 * the transmuting criteria are met
4564 */
4565 ntsp->smk_task = otsp->smk_task;
4566
4567 /*
4568 * the attribute of the containing directory
4569 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004570 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004571
4572 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4573 rcu_read_lock();
4574 may = smk_access_entry(otsp->smk_task->smk_known,
4575 isp->smk_inode->smk_known,
4576 &otsp->smk_task->smk_rules);
4577 rcu_read_unlock();
4578
4579 /*
4580 * If the directory is transmuting and the rule
4581 * providing access is transmuting use the containing
4582 * directory label instead of the process label.
4583 */
4584 if (may > 0 && (may & MAY_TRANSMUTE))
4585 ntsp->smk_task = isp->smk_inode;
4586 }
4587 return 0;
4588}
4589
Casey Schauflerbbd36622018-11-12 09:30:56 -08004590struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4591 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004592 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004593 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004594 .lbs_ipc = sizeof(struct smack_known *),
4595 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004596};
4597
James Morrisca97d932017-02-15 00:18:51 +11004598static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004599 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4600 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4601 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004602
Al Viro0b520752018-12-23 16:02:47 -05004603 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004604 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4605
Casey Schauflere20b0432015-05-02 15:11:36 -07004606 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4607 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004608 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004609 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004610 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304611 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004612
Casey Schauflere20b0432015-05-02 15:11:36 -07004613 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004614
Casey Schauflere20b0432015-05-02 15:11:36 -07004615 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004616 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4617 LSM_HOOK_INIT(inode_link, smack_inode_link),
4618 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4619 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4620 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4621 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4622 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4623 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4624 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4625 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4626 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4627 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4628 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4629 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4630 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4631 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004632
Casey Schauflere20b0432015-05-02 15:11:36 -07004633 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004634 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4635 LSM_HOOK_INIT(file_lock, smack_file_lock),
4636 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4637 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4638 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4639 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4640 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4641 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004642
Casey Schauflere20b0432015-05-02 15:11:36 -07004643 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004644
Casey Schauflere20b0432015-05-02 15:11:36 -07004645 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4646 LSM_HOOK_INIT(cred_free, smack_cred_free),
4647 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4648 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004649 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004650 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4651 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4652 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4653 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4654 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4655 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4656 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4657 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4658 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4659 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4660 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4661 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4662 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004663 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004664
Casey Schauflere20b0432015-05-02 15:11:36 -07004665 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4666 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004667
Casey Schauflere20b0432015-05-02 15:11:36 -07004668 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004669
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004670 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004671 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4672 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4673 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4674 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004675
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004676 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004677 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4678 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4679 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004680
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004681 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004682 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4683 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4684 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004685
Casey Schauflere20b0432015-05-02 15:11:36 -07004686 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004687
Casey Schauflere20b0432015-05-02 15:11:36 -07004688 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4689 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004690
Casey Schauflere20b0432015-05-02 15:11:36 -07004691 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4692 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004693
Casey Schauflere20b0432015-05-02 15:11:36 -07004694 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004695 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004696#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004697 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004698#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004699 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4700 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4701 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4702 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4703 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4704 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4705 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4706 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4707 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4708 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004709
Casey Schauflere114e472008-02-04 22:29:50 -08004710 /* key management security hooks */
4711#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004712 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4713 LSM_HOOK_INIT(key_free, smack_key_free),
4714 LSM_HOOK_INIT(key_permission, smack_key_permission),
4715 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004716#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004717
4718 /* Audit hooks */
4719#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004720 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4721 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4722 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004723#endif /* CONFIG_AUDIT */
4724
Casey Schauflere20b0432015-05-02 15:11:36 -07004725 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4726 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4727 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004728 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4729 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4730 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004731 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4732 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4733 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004734};
4735
Etienne Basset7198e2e2009-03-24 20:53:24 +01004736
Casey Schaufler86812bb2012-04-17 18:55:46 -07004737static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004738{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004739 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004740 * Initialize rule list locks
4741 */
4742 mutex_init(&smack_known_huh.smk_rules_lock);
4743 mutex_init(&smack_known_hat.smk_rules_lock);
4744 mutex_init(&smack_known_floor.smk_rules_lock);
4745 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004746 mutex_init(&smack_known_web.smk_rules_lock);
4747 /*
4748 * Initialize rule lists
4749 */
4750 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4751 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4752 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4753 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004754 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4755 /*
4756 * Create the known labels list
4757 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004758 smk_insert_entry(&smack_known_huh);
4759 smk_insert_entry(&smack_known_hat);
4760 smk_insert_entry(&smack_known_star);
4761 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004762 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004763}
4764
Casey Schauflere114e472008-02-04 22:29:50 -08004765/**
4766 * smack_init - initialize the smack system
4767 *
4768 * Returns 0
4769 */
4770static __init int smack_init(void)
4771{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004772 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004773 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004774
Rohit1a5b4722014-10-15 17:40:41 +05304775 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4776 if (!smack_inode_cache)
4777 return -ENOMEM;
4778
Casey Schaufler4e328b02019-04-02 11:37:12 -07004779 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4780 if (!smack_rule_cache) {
4781 kmem_cache_destroy(smack_inode_cache);
4782 return -ENOMEM;
4783 }
4784
Casey Schauflerbbd36622018-11-12 09:30:56 -08004785 /*
4786 * Set the security state for the initial task.
4787 */
4788 tsp = smack_cred(cred);
4789 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4790
4791 /*
4792 * Register with LSM
4793 */
4794 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004795 smack_enabled = 1;
4796
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004797 pr_info("Smack: Initializing.\n");
4798#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4799 pr_info("Smack: Netfilter enabled.\n");
4800#endif
4801#ifdef SMACK_IPV6_PORT_LABELING
4802 pr_info("Smack: IPv6 port labeling enabled.\n");
4803#endif
4804#ifdef SMACK_IPV6_SECMARK_LABELING
4805 pr_info("Smack: IPv6 Netfilter enabled.\n");
4806#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004807
Casey Schaufler86812bb2012-04-17 18:55:46 -07004808 /* initialize the smack_known_list */
4809 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004810
Casey Schauflere114e472008-02-04 22:29:50 -08004811 return 0;
4812}
4813
4814/*
4815 * Smack requires early initialization in order to label
4816 * all processes and objects when they are created.
4817 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004818DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004819 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004820 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004821 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004822 .init = smack_init,
4823};