blob: 0de725f88bedb24a0c0207b9953074316ee158f5 [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[] = {
71 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
Vivek Trivedi3bf27892015-06-22 15:36:06 +053072};
Al Viroc3300aa2018-12-16 01:52:24 -050073#undef A
74
75static int match_opt_prefix(char *s, int l, char **arg)
76{
77 int i;
78
79 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
80 size_t len = smk_mount_opts[i].len;
81 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
82 continue;
83 if (len == l || s[len] != '=')
84 continue;
85 *arg = s + len + 1;
86 return smk_mount_opts[i].opt;
87 }
88 return Opt_error;
89}
Vivek Trivedi3bf27892015-06-22 15:36:06 +053090
Casey Schaufler3d04c922015-08-12 11:56:02 -070091#ifdef CONFIG_SECURITY_SMACK_BRINGUP
92static char *smk_bu_mess[] = {
93 "Bringup Error", /* Unused */
94 "Bringup", /* SMACK_BRINGUP_ALLOW */
95 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
96 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
97};
98
Casey Schauflerd166c802014-08-27 14:51:27 -070099static void smk_bu_mode(int mode, char *s)
100{
101 int i = 0;
102
103 if (mode & MAY_READ)
104 s[i++] = 'r';
105 if (mode & MAY_WRITE)
106 s[i++] = 'w';
107 if (mode & MAY_EXEC)
108 s[i++] = 'x';
109 if (mode & MAY_APPEND)
110 s[i++] = 'a';
111 if (mode & MAY_TRANSMUTE)
112 s[i++] = 't';
113 if (mode & MAY_LOCK)
114 s[i++] = 'l';
115 if (i == 0)
116 s[i++] = '-';
117 s[i] = '\0';
118}
119#endif
120
121#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200122static int smk_bu_note(char *note, struct smack_known *sskp,
123 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700124{
125 char acc[SMK_NUM_ACCESS_TYPE + 1];
126
127 if (rc <= 0)
128 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700129 if (rc > SMACK_UNCONFINED_OBJECT)
130 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700131
132 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700133 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200134 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700135 return 0;
136}
137#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200138#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700139#endif
140
141#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200142static int smk_bu_current(char *note, struct smack_known *oskp,
143 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700144{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800145 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700146 char acc[SMK_NUM_ACCESS_TYPE + 1];
147
148 if (rc <= 0)
149 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700150 if (rc > SMACK_UNCONFINED_OBJECT)
151 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700152
153 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700154 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200155 tsp->smk_task->smk_known, oskp->smk_known,
156 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700157 return 0;
158}
159#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200160#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700161#endif
162
163#ifdef CONFIG_SECURITY_SMACK_BRINGUP
164static int smk_bu_task(struct task_struct *otp, int mode, int rc)
165{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800166 struct task_smack *tsp = smack_cred(current_cred());
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300167 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700168 char acc[SMK_NUM_ACCESS_TYPE + 1];
169
170 if (rc <= 0)
171 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700172 if (rc > SMACK_UNCONFINED_OBJECT)
173 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700174
175 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700176 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300177 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700178 current->comm, otp->comm);
179 return 0;
180}
181#else
182#define smk_bu_task(otp, mode, RC) (RC)
183#endif
184
185#ifdef CONFIG_SECURITY_SMACK_BRINGUP
186static int smk_bu_inode(struct inode *inode, int mode, int rc)
187{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800188 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800189 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700190 char acc[SMK_NUM_ACCESS_TYPE + 1];
191
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700192 if (isp->smk_flags & SMK_INODE_IMPURE)
193 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
194 inode->i_sb->s_id, inode->i_ino, current->comm);
195
Casey Schauflerd166c802014-08-27 14:51:27 -0700196 if (rc <= 0)
197 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700198 if (rc > SMACK_UNCONFINED_OBJECT)
199 rc = 0;
200 if (rc == SMACK_UNCONFINED_SUBJECT &&
201 (mode & (MAY_WRITE | MAY_APPEND)))
202 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700203
204 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700205
206 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
207 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700208 inode->i_sb->s_id, inode->i_ino, current->comm);
209 return 0;
210}
211#else
212#define smk_bu_inode(inode, mode, RC) (RC)
213#endif
214
215#ifdef CONFIG_SECURITY_SMACK_BRINGUP
216static int smk_bu_file(struct file *file, int mode, int rc)
217{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800218 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700219 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800220 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800221 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700222 char acc[SMK_NUM_ACCESS_TYPE + 1];
223
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700224 if (isp->smk_flags & SMK_INODE_IMPURE)
225 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
226 inode->i_sb->s_id, inode->i_ino, current->comm);
227
Casey Schauflerd166c802014-08-27 14:51:27 -0700228 if (rc <= 0)
229 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700230 if (rc > SMACK_UNCONFINED_OBJECT)
231 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700232
233 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700234 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800235 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400236 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700237 current->comm);
238 return 0;
239}
240#else
241#define smk_bu_file(file, mode, RC) (RC)
242#endif
243
244#ifdef CONFIG_SECURITY_SMACK_BRINGUP
245static int smk_bu_credfile(const struct cred *cred, struct file *file,
246 int mode, int rc)
247{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800248 struct task_smack *tsp = smack_cred(cred);
Casey Schauflerd166c802014-08-27 14:51:27 -0700249 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500250 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800251 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700252 char acc[SMK_NUM_ACCESS_TYPE + 1];
253
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700254 if (isp->smk_flags & SMK_INODE_IMPURE)
255 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
256 inode->i_sb->s_id, inode->i_ino, current->comm);
257
Casey Schauflerd166c802014-08-27 14:51:27 -0700258 if (rc <= 0)
259 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700260 if (rc > SMACK_UNCONFINED_OBJECT)
261 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700262
263 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700264 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200265 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400266 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700267 current->comm);
268 return 0;
269}
270#else
271#define smk_bu_credfile(cred, file, mode, RC) (RC)
272#endif
273
Casey Schauflere114e472008-02-04 22:29:50 -0800274/**
275 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100276 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800277 * @ip: a pointer to the inode
278 * @dp: a pointer to the dentry
279 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200280 * Returns a pointer to the master list entry for the Smack label,
281 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800282 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700283static struct smack_known *smk_fetch(const char *name, struct inode *ip,
284 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800285{
286 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700287 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700288 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800289
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200290 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200291 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800292
Casey Schauflerf7112e62012-05-06 15:22:02 -0700293 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
294 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200295 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800296
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200297 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200298 if (rc < 0)
299 skp = ERR_PTR(rc);
300 else if (rc == 0)
301 skp = NULL;
302 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700303 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700304
305 kfree(buffer);
306
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700307 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800308}
309
310/**
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700311 * init_inode_smack - initialize an inode security blob
312 * @isp: the blob to initialize
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200313 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800314 *
Casey Schauflere114e472008-02-04 22:29:50 -0800315 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700316static void init_inode_smack(struct inode *inode, struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800317{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700318 struct inode_smack *isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -0800319
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200320 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800321 isp->smk_flags = 0;
322 mutex_init(&isp->smk_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800323}
324
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800325/**
Casey Schauflerbbd36622018-11-12 09:30:56 -0800326 * init_task_smack - initialize a task security blob
327 * @tsp: blob to initialize
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100328 * @task: a pointer to the Smack label for the running task
329 * @forked: a pointer to the Smack label for the forked task
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800330 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800331 */
Casey Schauflerbbd36622018-11-12 09:30:56 -0800332static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
333 struct smack_known *forked)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800334{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800335 tsp->smk_task = task;
336 tsp->smk_forked = forked;
337 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200338 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800339 mutex_init(&tsp->smk_rules_lock);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800340}
341
342/**
343 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100344 * @nhead: new rules header pointer
345 * @ohead: old rules header pointer
346 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800347 *
348 * Returns 0 on success, -ENOMEM on error
349 */
350static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
351 gfp_t gfp)
352{
353 struct smack_rule *nrp;
354 struct smack_rule *orp;
355 int rc = 0;
356
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800357 list_for_each_entry_rcu(orp, ohead, list) {
Casey Schaufler4e328b02019-04-02 11:37:12 -0700358 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800359 if (nrp == NULL) {
360 rc = -ENOMEM;
361 break;
362 }
363 *nrp = *orp;
364 list_add_rcu(&nrp->list, nhead);
365 }
366 return rc;
367}
368
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100369/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200370 * smk_copy_relabel - copy smk_relabel labels list
371 * @nhead: new rules header pointer
372 * @ohead: old rules header pointer
373 * @gfp: type of the memory for the allocation
374 *
375 * Returns 0 on success, -ENOMEM on error
376 */
377static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
378 gfp_t gfp)
379{
380 struct smack_known_list_elem *nklep;
381 struct smack_known_list_elem *oklep;
382
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200383 list_for_each_entry(oklep, ohead, list) {
384 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
385 if (nklep == NULL) {
386 smk_destroy_label_list(nhead);
387 return -ENOMEM;
388 }
389 nklep->smk_label = oklep->smk_label;
390 list_add(&nklep->list, nhead);
391 }
392
393 return 0;
394}
395
396/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100397 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
398 * @mode - input mode in form of PTRACE_MODE_*
399 *
400 * Returns a converted MAY_* mode usable by smack rules
401 */
402static inline unsigned int smk_ptrace_mode(unsigned int mode)
403{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800404 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100405 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800406 if (mode & PTRACE_MODE_READ)
407 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100408
409 return 0;
410}
411
412/**
413 * smk_ptrace_rule_check - helper for ptrace access
414 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200415 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100416 * @mode: ptrace attachment mode (PTRACE_MODE_*)
417 * @func: name of the function that called us, used for audit
418 *
419 * Returns 0 on access granted, -error on error
420 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200421static int smk_ptrace_rule_check(struct task_struct *tracer,
422 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100423 unsigned int mode, const char *func)
424{
425 int rc;
426 struct smk_audit_info ad, *saip = NULL;
427 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200428 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700429 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100430
431 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
432 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
433 smk_ad_setfield_u_tsk(&ad, tracer);
434 saip = &ad;
435 }
436
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300437 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700438 tracercred = __task_cred(tracer);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800439 tsp = smack_cred(tracercred);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200440 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100441
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100442 if ((mode & PTRACE_MODE_ATTACH) &&
443 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
444 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200445 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100446 rc = 0;
447 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
448 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700449 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100450 rc = 0;
451 else
452 rc = -EACCES;
453
454 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200455 smack_log(tracer_known->smk_known,
456 tracee_known->smk_known,
457 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100458
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300459 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100460 return rc;
461 }
462
463 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200464 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300465
466 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100467 return rc;
468}
469
Casey Schauflere114e472008-02-04 22:29:50 -0800470/*
471 * LSM hooks.
472 * We he, that is fun!
473 */
474
475/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000476 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800477 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100478 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800479 *
480 * Returns 0 if access is OK, an error code otherwise
481 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100482 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800483 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000484static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800485{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700486 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800487
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300488 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200489
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700490 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100491}
Casey Schauflere114e472008-02-04 22:29:50 -0800492
David Howells5cd9c582008-08-14 11:37:28 +0100493/**
494 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
495 * @ptp: parent task pointer
496 *
497 * Returns 0 if access is OK, an error code otherwise
498 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100499 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100500 */
501static int smack_ptrace_traceme(struct task_struct *ptp)
502{
503 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700504 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100505
Casey Schauflerb17103a2018-11-09 16:12:56 -0800506 skp = smk_of_task(smack_cred(current_cred()));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200507
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200508 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800509 return rc;
510}
511
512/**
513 * smack_syslog - Smack approval on syslog
514 * @type: message type
515 *
Casey Schauflere114e472008-02-04 22:29:50 -0800516 * Returns 0 on success, error code otherwise.
517 */
Eric Paris12b30522010-11-15 18:36:29 -0500518static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800519{
Eric Paris12b30522010-11-15 18:36:29 -0500520 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700521 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800522
Casey Schaufler1880eff2012-06-05 15:28:30 -0700523 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800524 return 0;
525
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800526 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800527 rc = -EACCES;
528
529 return rc;
530}
531
Casey Schauflere114e472008-02-04 22:29:50 -0800532/*
533 * Superblock Hooks.
534 */
535
536/**
537 * smack_sb_alloc_security - allocate a superblock blob
538 * @sb: the superblock getting the blob
539 *
540 * Returns 0 on success or -ENOMEM on error.
541 */
542static int smack_sb_alloc_security(struct super_block *sb)
543{
544 struct superblock_smack *sbsp;
545
546 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
547
548 if (sbsp == NULL)
549 return -ENOMEM;
550
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200551 sbsp->smk_root = &smack_known_floor;
552 sbsp->smk_default = &smack_known_floor;
553 sbsp->smk_floor = &smack_known_floor;
554 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700555 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500556 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700557 */
Casey Schauflere114e472008-02-04 22:29:50 -0800558 sb->s_security = sbsp;
559
560 return 0;
561}
562
563/**
564 * smack_sb_free_security - free a superblock blob
565 * @sb: the superblock getting the blob
566 *
567 */
568static void smack_sb_free_security(struct super_block *sb)
569{
570 kfree(sb->s_security);
571 sb->s_security = NULL;
572}
573
Al Viro12085b12018-12-13 15:18:05 -0500574struct smack_mnt_opts {
575 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
576};
577
Al Viro204cc0c2018-12-13 13:41:47 -0500578static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800579{
Al Viro12085b12018-12-13 15:18:05 -0500580 struct smack_mnt_opts *opts = mnt_opts;
581 kfree(opts->fsdefault);
582 kfree(opts->fsfloor);
583 kfree(opts->fshat);
584 kfree(opts->fsroot);
585 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500586 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800587}
588
Al Viro55c0e5b2018-12-16 01:09:45 -0500589static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530590{
Al Viro55c0e5b2018-12-16 01:09:45 -0500591 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530592
Al Viro55c0e5b2018-12-16 01:09:45 -0500593 if (!opts) {
594 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
595 if (!opts)
596 return -ENOMEM;
597 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530598 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500599 if (!s)
600 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530601
Al Viro55c0e5b2018-12-16 01:09:45 -0500602 switch (token) {
603 case Opt_fsdefault:
604 if (opts->fsdefault)
605 goto out_opt_err;
606 opts->fsdefault = s;
607 break;
608 case Opt_fsfloor:
609 if (opts->fsfloor)
610 goto out_opt_err;
611 opts->fsfloor = s;
612 break;
613 case Opt_fshat:
614 if (opts->fshat)
615 goto out_opt_err;
616 opts->fshat = s;
617 break;
618 case Opt_fsroot:
619 if (opts->fsroot)
620 goto out_opt_err;
621 opts->fsroot = s;
622 break;
623 case Opt_fstransmute:
624 if (opts->fstransmute)
625 goto out_opt_err;
626 opts->fstransmute = s;
627 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530628 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530629 return 0;
630
631out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530632 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500633 return -EINVAL;
634}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530635
Al Viro0b520752018-12-23 16:02:47 -0500636/**
637 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
638 * @fc: The new filesystem context.
639 * @src_fc: The source filesystem context being duplicated.
640 *
641 * Returns 0 on success or -ENOMEM on error.
642 */
643static int smack_fs_context_dup(struct fs_context *fc,
644 struct fs_context *src_fc)
645{
646 struct smack_mnt_opts *dst, *src = src_fc->security;
647
648 if (!src)
649 return 0;
650
651 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
652 if (!fc->security)
653 return -ENOMEM;
654 dst = fc->security;
655
656 if (src->fsdefault) {
657 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
658 if (!dst->fsdefault)
659 return -ENOMEM;
660 }
661 if (src->fsfloor) {
662 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
663 if (!dst->fsfloor)
664 return -ENOMEM;
665 }
666 if (src->fshat) {
667 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
668 if (!dst->fshat)
669 return -ENOMEM;
670 }
671 if (src->fsroot) {
672 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
673 if (!dst->fsroot)
674 return -ENOMEM;
675 }
676 if (src->fstransmute) {
677 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
678 if (!dst->fstransmute)
679 return -ENOMEM;
680 }
681 return 0;
682}
683
David Howells2febd252018-11-01 23:07:24 +0000684static const struct fs_parameter_spec smack_param_specs[] = {
685 fsparam_string("fsdefault", Opt_fsdefault),
686 fsparam_string("fsfloor", Opt_fsfloor),
687 fsparam_string("fshat", Opt_fshat),
688 fsparam_string("fsroot", Opt_fsroot),
689 fsparam_string("fstransmute", Opt_fstransmute),
690 {}
691};
692
693static const struct fs_parameter_description smack_fs_parameters = {
694 .name = "smack",
695 .specs = smack_param_specs,
696};
697
698/**
699 * smack_fs_context_parse_param - Parse a single mount parameter
700 * @fc: The new filesystem context being constructed.
701 * @param: The parameter.
702 *
703 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
704 * error.
705 */
706static int smack_fs_context_parse_param(struct fs_context *fc,
707 struct fs_parameter *param)
708{
709 struct fs_parse_result result;
710 int opt, rc;
711
712 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
713 if (opt < 0)
714 return opt;
715
716 rc = smack_add_opt(opt, param->string, &fc->security);
717 if (!rc)
718 param->string = NULL;
719 return rc;
720}
721
Al Virod2497e12018-12-16 01:37:06 -0500722static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530723{
Al Virod2497e12018-12-16 01:37:06 -0500724 char *from = options, *to = options;
725 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530726
Al Viroc3300aa2018-12-16 01:52:24 -0500727 while (1) {
728 char *next = strchr(from, ',');
729 int token, len, rc;
730 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530731
Al Viroc3300aa2018-12-16 01:52:24 -0500732 if (next)
733 len = next - from;
734 else
735 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530736
Al Viroc3300aa2018-12-16 01:52:24 -0500737 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500738 if (token != Opt_error) {
739 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
740 rc = smack_add_opt(token, arg, mnt_opts);
741 if (unlikely(rc)) {
742 kfree(arg);
743 if (*mnt_opts)
744 smack_free_mnt_opts(*mnt_opts);
745 *mnt_opts = NULL;
746 return rc;
747 }
748 } else {
749 if (!first) { // copy with preceding comma
750 from--;
751 len++;
752 }
753 if (to != from)
754 memmove(to, from, len);
755 to += len;
756 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530757 }
Al Viroc3300aa2018-12-16 01:52:24 -0500758 if (!from[len])
759 break;
760 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530761 }
Al Virod2497e12018-12-16 01:37:06 -0500762 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530763 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530764}
765
766/**
767 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800768 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530769 * @opts: Smack mount options
770 * @kern_flags: mount option from kernel space or user space
771 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800772 *
773 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530774 *
775 * Allow filesystems with binary mount data to explicitly set Smack mount
776 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800777 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530778static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500779 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530780 unsigned long kern_flags,
781 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800782{
783 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000784 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800785 struct superblock_smack *sp = sb->s_security;
786 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800787 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500788 struct smack_mnt_opts *opts = mnt_opts;
789 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800790
Seth Forshee9f50eda2015-09-23 15:16:06 -0500791 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800792 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700793
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700794 if (inode->i_security == NULL) {
795 int rc = lsm_inode_alloc(inode);
796
797 if (rc)
798 return rc;
799 }
800
Himanshu Shukla2097f592016-11-10 16:19:52 +0530801 if (!smack_privileged(CAP_MAC_ADMIN)) {
802 /*
803 * Unprivileged mounts don't get to specify Smack values.
804 */
Al Viro12085b12018-12-13 15:18:05 -0500805 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530806 return -EPERM;
807 /*
808 * Unprivileged mounts get root and default from the caller.
809 */
810 skp = smk_of_current();
811 sp->smk_root = skp;
812 sp->smk_default = skp;
813 /*
814 * For a handful of fs types with no user-controlled
815 * backing store it's okay to trust security labels
816 * in the filesystem. The rest are untrusted.
817 */
818 if (sb->s_user_ns != &init_user_ns &&
819 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
820 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500821 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530822 sp->smk_flags |= SMK_SB_UNTRUSTED;
823 }
824 }
825
Seth Forshee9f50eda2015-09-23 15:16:06 -0500826 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800827
Al Viro12085b12018-12-13 15:18:05 -0500828 if (opts) {
829 if (opts->fsdefault) {
830 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200831 if (IS_ERR(skp))
832 return PTR_ERR(skp);
833 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500834 }
835 if (opts->fsfloor) {
836 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530837 if (IS_ERR(skp))
838 return PTR_ERR(skp);
839 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500840 }
841 if (opts->fshat) {
842 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530843 if (IS_ERR(skp))
844 return PTR_ERR(skp);
845 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500846 }
847 if (opts->fsroot) {
848 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200849 if (IS_ERR(skp))
850 return PTR_ERR(skp);
851 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500852 }
853 if (opts->fstransmute) {
854 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200855 if (IS_ERR(skp))
856 return PTR_ERR(skp);
857 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500858 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800859 }
860 }
861
862 /*
863 * Initialize the root inode.
864 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700865 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800866
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700867 if (transmute) {
868 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700869 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700870 }
Casey Schauflere830b392013-05-22 18:43:07 -0700871
Casey Schauflere114e472008-02-04 22:29:50 -0800872 return 0;
873}
874
875/**
876 * smack_sb_statfs - Smack check on statfs
877 * @dentry: identifies the file system in question
878 *
879 * Returns 0 if current can read the floor of the filesystem,
880 * and error code otherwise
881 */
882static int smack_sb_statfs(struct dentry *dentry)
883{
884 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200885 int rc;
886 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800887
Eric Parisa2694342011-04-25 13:10:27 -0400888 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200889 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
890
891 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700892 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200893 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800894}
895
Casey Schauflere114e472008-02-04 22:29:50 -0800896/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800897 * BPRM hooks
898 */
899
Casey Schauflerce8a4322011-09-29 18:21:01 -0700900/**
901 * smack_bprm_set_creds - set creds for exec
902 * @bprm: the exec information
903 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100904 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700905 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800906static int smack_bprm_set_creds(struct linux_binprm *bprm)
907{
Al Viro496ad9a2013-01-23 17:07:38 -0500908 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800909 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800910 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500911 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800912 int rc;
913
Kees Cookddb4a142017-07-18 15:25:23 -0700914 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800915 return 0;
916
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800917 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300918 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800919 return 0;
920
Seth Forshee809c02e2016-04-26 14:36:22 -0500921 sbsp = inode->i_sb->s_security;
922 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
923 isp->smk_task != sbsp->smk_root)
924 return 0;
925
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300926 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100927 struct task_struct *tracer;
928 rc = 0;
929
930 rcu_read_lock();
931 tracer = ptrace_parent(current);
932 if (likely(tracer != NULL))
933 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200934 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100935 PTRACE_MODE_ATTACH,
936 __func__);
937 rcu_read_unlock();
938
939 if (rc != 0)
940 return rc;
941 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300942 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800943
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300944 bsp->smk_task = isp->smk_task;
945 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800946
Kees Cookccbb6e12017-07-18 15:25:26 -0700947 /* Decide if this is a secure exec. */
948 if (bsp->smk_task != bsp->smk_forked)
949 bprm->secureexec = 1;
950
Casey Schaufler676dac42010-12-02 06:43:39 -0800951 return 0;
952}
953
954/*
Casey Schauflere114e472008-02-04 22:29:50 -0800955 * Inode hooks
956 */
957
958/**
959 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800960 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800961 *
962 * Returns 0 if it gets a blob, -ENOMEM otherwise
963 */
964static int smack_inode_alloc_security(struct inode *inode)
965{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700966 struct smack_known *skp = smk_of_current();
967
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700968 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800969 return 0;
970}
971
972/**
Casey Schauflere114e472008-02-04 22:29:50 -0800973 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200974 * @inode: the newly created inode
975 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500976 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800977 * @name: where to put the attribute name
978 * @value: where to put the attribute value
979 * @len: where to put the length of the attribute
980 *
981 * Returns 0 if it all works out, -ENOMEM if there's no memory
982 */
983static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900984 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500985 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800986{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800987 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700988 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200989 struct smack_known *isp = smk_of_inode(inode);
990 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800991 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800992
Tetsuo Handa95489062013-07-25 05:44:02 +0900993 if (name)
994 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800995
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100996 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800997 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200998 may = smk_access_entry(skp->smk_known, dsp->smk_known,
999 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001000 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001001
1002 /*
1003 * If the access rule allows transmutation and
1004 * the directory requests transmutation then
1005 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001006 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001007 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001008 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001009 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001010 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001011 issp->smk_flags |= SMK_INODE_CHANGED;
1012 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001013
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001014 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001015 if (*value == NULL)
1016 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001017
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001018 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001019 }
Casey Schauflere114e472008-02-04 22:29:50 -08001020
1021 return 0;
1022}
1023
1024/**
1025 * smack_inode_link - Smack check on link
1026 * @old_dentry: the existing object
1027 * @dir: unused
1028 * @new_dentry: the new object
1029 *
1030 * Returns 0 if access is permitted, an error code otherwise
1031 */
1032static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1033 struct dentry *new_dentry)
1034{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001035 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001036 struct smk_audit_info ad;
1037 int rc;
1038
Eric Parisa2694342011-04-25 13:10:27 -04001039 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001040 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001041
David Howellsc6f493d2015-03-17 22:26:22 +00001042 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001043 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001044 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001045
David Howells88025652015-01-29 12:02:32 +00001046 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001047 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001048 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1049 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001050 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001051 }
1052
1053 return rc;
1054}
1055
1056/**
1057 * smack_inode_unlink - Smack check on inode deletion
1058 * @dir: containing directory object
1059 * @dentry: file to unlink
1060 *
1061 * Returns 0 if current can write the containing directory
1062 * and the object, error code otherwise
1063 */
1064static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1065{
David Howellsc6f493d2015-03-17 22:26:22 +00001066 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001067 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001068 int rc;
1069
Eric Parisa2694342011-04-25 13:10:27 -04001070 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001071 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1072
Casey Schauflere114e472008-02-04 22:29:50 -08001073 /*
1074 * You need write access to the thing you're unlinking
1075 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001076 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001077 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001078 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001079 /*
1080 * You also need write access to the containing directory
1081 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001082 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001083 smk_ad_setfield_u_fs_inode(&ad, dir);
1084 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001085 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001086 }
Casey Schauflere114e472008-02-04 22:29:50 -08001087 return rc;
1088}
1089
1090/**
1091 * smack_inode_rmdir - Smack check on directory deletion
1092 * @dir: containing directory object
1093 * @dentry: directory to unlink
1094 *
1095 * Returns 0 if current can write the containing directory
1096 * and the directory, error code otherwise
1097 */
1098static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1099{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001100 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001101 int rc;
1102
Eric Parisa2694342011-04-25 13:10:27 -04001103 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001104 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1105
Casey Schauflere114e472008-02-04 22:29:50 -08001106 /*
1107 * You need write access to the thing you're removing
1108 */
David Howellsc6f493d2015-03-17 22:26:22 +00001109 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1110 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001111 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001112 /*
1113 * You also need write access to the containing directory
1114 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001115 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001116 smk_ad_setfield_u_fs_inode(&ad, dir);
1117 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001118 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001119 }
Casey Schauflere114e472008-02-04 22:29:50 -08001120
1121 return rc;
1122}
1123
1124/**
1125 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001126 * @old_inode: unused
1127 * @old_dentry: the old object
1128 * @new_inode: unused
1129 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001130 *
1131 * Read and write access is required on both the old and
1132 * new directories.
1133 *
1134 * Returns 0 if access is permitted, an error code otherwise
1135 */
1136static int smack_inode_rename(struct inode *old_inode,
1137 struct dentry *old_dentry,
1138 struct inode *new_inode,
1139 struct dentry *new_dentry)
1140{
1141 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001142 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001143 struct smk_audit_info ad;
1144
Eric Parisa2694342011-04-25 13:10:27 -04001145 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001146 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001147
David Howellsc6f493d2015-03-17 22:26:22 +00001148 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001149 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001150 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001151
David Howells88025652015-01-29 12:02:32 +00001152 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001153 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001154 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1155 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001156 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001157 }
Casey Schauflere114e472008-02-04 22:29:50 -08001158 return rc;
1159}
1160
1161/**
1162 * smack_inode_permission - Smack version of permission()
1163 * @inode: the inode in question
1164 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001165 *
1166 * This is the important Smack hook.
1167 *
1168 * Returns 0 if access is permitted, -EACCES otherwise
1169 */
Al Viroe74f71e2011-06-20 19:38:15 -04001170static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001171{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001172 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001173 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001174 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001175 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001176
1177 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001178 /*
1179 * No permission to check. Existence test. Yup, it's there.
1180 */
1181 if (mask == 0)
1182 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001183
Seth Forshee9f50eda2015-09-23 15:16:06 -05001184 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1185 if (smk_of_inode(inode) != sbsp->smk_root)
1186 return -EACCES;
1187 }
1188
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001189 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001190 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001191 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001192 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001193 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001194 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1195 rc = smk_bu_inode(inode, mask, rc);
1196 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001197}
1198
1199/**
1200 * smack_inode_setattr - Smack check for setting attributes
1201 * @dentry: the object
1202 * @iattr: for the force flag
1203 *
1204 * Returns 0 if access is permitted, an error code otherwise
1205 */
1206static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1207{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001208 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001209 int rc;
1210
Casey Schauflere114e472008-02-04 22:29:50 -08001211 /*
1212 * Need to allow for clearing the setuid bit.
1213 */
1214 if (iattr->ia_valid & ATTR_FORCE)
1215 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001216 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001217 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001218
David Howellsc6f493d2015-03-17 22:26:22 +00001219 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1220 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001221 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001222}
1223
1224/**
1225 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001226 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001227 * @dentry: the object
1228 *
1229 * Returns 0 if access is permitted, an error code otherwise
1230 */
Al Viro3f7036a2015-03-08 19:28:30 -04001231static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001232{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001233 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001234 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001235 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001236
Eric Parisf48b7392011-04-25 12:54:27 -04001237 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001238 smk_ad_setfield_u_fs_path(&ad, *path);
1239 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1240 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001241 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001242}
1243
1244/**
1245 * smack_inode_setxattr - Smack check for setting xattrs
1246 * @dentry: the object
1247 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001248 * @value: value of the attribute
1249 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001250 * @flags: unused
1251 *
1252 * This protects the Smack attribute explicitly.
1253 *
1254 * Returns 0 if access is permitted, an error code otherwise
1255 */
David Howells8f0cfa52008-04-29 00:59:41 -07001256static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1257 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001258{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001259 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001260 struct smack_known *skp;
1261 int check_priv = 0;
1262 int check_import = 0;
1263 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001264 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001265
Casey Schaufler19760ad2013-12-16 16:27:26 -08001266 /*
1267 * Check label validity here so import won't fail in post_setxattr
1268 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001269 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1270 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001271 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1272 check_priv = 1;
1273 check_import = 1;
1274 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1275 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1276 check_priv = 1;
1277 check_import = 1;
1278 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001279 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001280 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001281 if (size != TRANS_TRUE_SIZE ||
1282 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1283 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001284 } else
1285 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1286
Casey Schaufler19760ad2013-12-16 16:27:26 -08001287 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1288 rc = -EPERM;
1289
1290 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001291 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001292 if (IS_ERR(skp))
1293 rc = PTR_ERR(skp);
1294 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001295 (skp == &smack_known_star || skp == &smack_known_web)))
1296 rc = -EINVAL;
1297 }
1298
Eric Parisa2694342011-04-25 13:10:27 -04001299 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001300 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1301
Casey Schauflerd166c802014-08-27 14:51:27 -07001302 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001303 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1304 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001305 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001306
1307 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001308}
1309
1310/**
1311 * smack_inode_post_setxattr - Apply the Smack update approved above
1312 * @dentry: object
1313 * @name: attribute name
1314 * @value: attribute value
1315 * @size: attribute size
1316 * @flags: unused
1317 *
1318 * Set the pointer in the inode blob to the entry found
1319 * in the master label list.
1320 */
David Howells8f0cfa52008-04-29 00:59:41 -07001321static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1322 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001323{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001324 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001325 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001326
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001327 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1328 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1329 return;
1330 }
1331
Casey Schaufler676dac42010-12-02 06:43:39 -08001332 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001333 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001334 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001335 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001336 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001337 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001338 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001339 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001340 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001341 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001342 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001343 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001344 }
Casey Schauflere114e472008-02-04 22:29:50 -08001345
1346 return;
1347}
1348
Casey Schauflerce8a4322011-09-29 18:21:01 -07001349/**
Casey Schauflere114e472008-02-04 22:29:50 -08001350 * smack_inode_getxattr - Smack check on getxattr
1351 * @dentry: the object
1352 * @name: unused
1353 *
1354 * Returns 0 if access is permitted, an error code otherwise
1355 */
David Howells8f0cfa52008-04-29 00:59:41 -07001356static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001357{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001358 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001359 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360
Eric Parisa2694342011-04-25 13:10:27 -04001361 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001362 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1363
David Howellsc6f493d2015-03-17 22:26:22 +00001364 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1365 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001366 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001367}
1368
Casey Schauflerce8a4322011-09-29 18:21:01 -07001369/**
Casey Schauflere114e472008-02-04 22:29:50 -08001370 * smack_inode_removexattr - Smack check on removexattr
1371 * @dentry: the object
1372 * @name: name of the attribute
1373 *
1374 * Removing the Smack attribute requires CAP_MAC_ADMIN
1375 *
1376 * Returns 0 if access is permitted, an error code otherwise
1377 */
David Howells8f0cfa52008-04-29 00:59:41 -07001378static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001379{
Casey Schaufler676dac42010-12-02 06:43:39 -08001380 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001381 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001382 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001383
Casey Schauflerbcdca222008-02-23 15:24:04 -08001384 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1385 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001386 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001387 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001388 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301389 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001390 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001391 rc = -EPERM;
1392 } else
1393 rc = cap_inode_removexattr(dentry, name);
1394
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001395 if (rc != 0)
1396 return rc;
1397
Eric Parisa2694342011-04-25 13:10:27 -04001398 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001399 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001400
David Howellsc6f493d2015-03-17 22:26:22 +00001401 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1402 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001403 if (rc != 0)
1404 return rc;
1405
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001406 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001407 /*
1408 * Don't do anything special for these.
1409 * XATTR_NAME_SMACKIPIN
1410 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001411 */
José Bollo80124952016-01-12 21:23:40 +01001412 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001413 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001414 struct superblock_smack *sbsp = sbp->s_security;
1415
1416 isp->smk_inode = sbsp->smk_default;
1417 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001418 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001419 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001420 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001421 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1422 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001423
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001424 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001425}
1426
1427/**
1428 * smack_inode_getsecurity - get smack xattrs
1429 * @inode: the object
1430 * @name: attribute name
1431 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001432 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001433 *
1434 * Returns the size of the attribute or an error code
1435 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001436static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001437 const char *name, void **buffer,
1438 bool alloc)
1439{
1440 struct socket_smack *ssp;
1441 struct socket *sock;
1442 struct super_block *sbp;
1443 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001444 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001445
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001446 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001447 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001448 else {
1449 /*
1450 * The rest of the Smack xattrs are only on sockets.
1451 */
1452 sbp = ip->i_sb;
1453 if (sbp->s_magic != SOCKFS_MAGIC)
1454 return -EOPNOTSUPP;
1455
1456 sock = SOCKET_I(ip);
1457 if (sock == NULL || sock->sk == NULL)
1458 return -EOPNOTSUPP;
1459
1460 ssp = sock->sk->sk_security;
1461
1462 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1463 isp = ssp->smk_in;
1464 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1465 isp = ssp->smk_out;
1466 else
1467 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001468 }
1469
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001470 if (alloc) {
1471 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1472 if (*buffer == NULL)
1473 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001474 }
1475
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001476 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001477}
1478
1479
1480/**
1481 * smack_inode_listsecurity - list the Smack attributes
1482 * @inode: the object
1483 * @buffer: where they go
1484 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001485 */
1486static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1487 size_t buffer_size)
1488{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001489 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001490
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001491 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001492 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001493
1494 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001495}
1496
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001497/**
1498 * smack_inode_getsecid - Extract inode's security id
1499 * @inode: inode to extract the info from
1500 * @secid: where result will be saved
1501 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001502static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001503{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001504 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001505
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001506 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001507}
1508
Casey Schauflere114e472008-02-04 22:29:50 -08001509/*
1510 * File Hooks
1511 */
1512
Casey Schaufler491a0b02016-01-26 15:08:35 -08001513/*
1514 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001515 *
1516 * Should access checks be done on each read or write?
1517 * UNICOS and SELinux say yes.
1518 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1519 *
1520 * I'll say no for now. Smack does not do the frequent
1521 * label changing that SELinux does.
1522 */
Casey Schauflere114e472008-02-04 22:29:50 -08001523
1524/**
1525 * smack_file_alloc_security - assign a file security blob
1526 * @file: the object
1527 *
1528 * The security blob for a file is a pointer to the master
1529 * label list, so no allocation is done.
1530 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001531 * f_security is the owner security information. It
1532 * isn't used on file access checks, it's for send_sigio.
1533 *
Casey Schauflere114e472008-02-04 22:29:50 -08001534 * Returns 0
1535 */
1536static int smack_file_alloc_security(struct file *file)
1537{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001538 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001539
Casey Schauflerf28952a2018-11-12 09:38:53 -08001540 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001541 return 0;
1542}
1543
1544/**
Casey Schauflere114e472008-02-04 22:29:50 -08001545 * smack_file_ioctl - Smack check on ioctls
1546 * @file: the object
1547 * @cmd: what to do
1548 * @arg: unused
1549 *
1550 * Relies heavily on the correct use of the ioctl command conventions.
1551 *
1552 * Returns 0 if allowed, error code otherwise
1553 */
1554static int smack_file_ioctl(struct file *file, unsigned int cmd,
1555 unsigned long arg)
1556{
1557 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001558 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001559 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001560
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001561 if (unlikely(IS_PRIVATE(inode)))
1562 return 0;
1563
Eric Parisf48b7392011-04-25 12:54:27 -04001564 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001565 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001566
Casey Schauflerd166c802014-08-27 14:51:27 -07001567 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001568 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001569 rc = smk_bu_file(file, MAY_WRITE, rc);
1570 }
Casey Schauflere114e472008-02-04 22:29:50 -08001571
Casey Schauflerd166c802014-08-27 14:51:27 -07001572 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001573 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001574 rc = smk_bu_file(file, MAY_READ, rc);
1575 }
Casey Schauflere114e472008-02-04 22:29:50 -08001576
1577 return rc;
1578}
1579
1580/**
1581 * smack_file_lock - Smack check on file locking
1582 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001583 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001584 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001585 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001586 */
1587static int smack_file_lock(struct file *file, unsigned int cmd)
1588{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001589 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001590 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001591 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001592
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001593 if (unlikely(IS_PRIVATE(inode)))
1594 return 0;
1595
Eric Paris92f42502011-04-25 13:15:55 -04001596 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1597 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001598 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001599 rc = smk_bu_file(file, MAY_LOCK, rc);
1600 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001601}
1602
1603/**
1604 * smack_file_fcntl - Smack check on fcntl
1605 * @file: the object
1606 * @cmd: what action to check
1607 * @arg: unused
1608 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001609 * Generally these operations are harmless.
1610 * File locking operations present an obvious mechanism
1611 * for passing information, so they require write access.
1612 *
Casey Schauflere114e472008-02-04 22:29:50 -08001613 * Returns 0 if current has access, error code otherwise
1614 */
1615static int smack_file_fcntl(struct file *file, unsigned int cmd,
1616 unsigned long arg)
1617{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001618 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001619 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001620 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001621
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001622 if (unlikely(IS_PRIVATE(inode)))
1623 return 0;
1624
Casey Schauflere114e472008-02-04 22:29:50 -08001625 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001626 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001627 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001628 case F_SETLK:
1629 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001630 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1631 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001632 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001633 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001634 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001635 case F_SETOWN:
1636 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001637 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1638 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001639 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001640 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001641 break;
1642 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001643 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001644 }
1645
1646 return rc;
1647}
1648
1649/**
Al Viroe5467852012-05-30 13:30:51 -04001650 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001651 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1652 * if mapping anonymous memory.
1653 * @file contains the file structure for file to map (may be NULL).
1654 * @reqprot contains the protection requested by the application.
1655 * @prot contains the protection that will be applied by the kernel.
1656 * @flags contains the operational flags.
1657 * Return 0 if permission is granted.
1658 */
Al Viroe5467852012-05-30 13:30:51 -04001659static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001660 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001661 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001662{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001663 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001664 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001665 struct smack_rule *srp;
1666 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001667 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001668 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001669 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001670 int may;
1671 int mmay;
1672 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001673 int rc;
1674
Al Viro496ad9a2013-01-23 17:07:38 -05001675 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001676 return 0;
1677
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001678 if (unlikely(IS_PRIVATE(file_inode(file))))
1679 return 0;
1680
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001681 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001682 if (isp->smk_mmap == NULL)
1683 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001684 sbsp = file_inode(file)->i_sb->s_security;
1685 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1686 isp->smk_mmap != sbsp->smk_root)
1687 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001688 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001689
Casey Schauflerb17103a2018-11-09 16:12:56 -08001690 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001691 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001692 rc = 0;
1693
1694 rcu_read_lock();
1695 /*
1696 * For each Smack rule associated with the subject
1697 * label verify that the SMACK64MMAP also has access
1698 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001699 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001700 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001701 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001702 /*
1703 * Matching labels always allows access.
1704 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001705 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001706 continue;
1707 /*
1708 * If there is a matching local rule take
1709 * that into account as well.
1710 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001711 may = smk_access_entry(srp->smk_subject->smk_known,
1712 okp->smk_known,
1713 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001714 if (may == -ENOENT)
1715 may = srp->smk_access;
1716 else
1717 may &= srp->smk_access;
1718 /*
1719 * If may is zero the SMACK64MMAP subject can't
1720 * possibly have less access.
1721 */
1722 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001723 continue;
1724
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001725 /*
1726 * Fetch the global list entry.
1727 * If there isn't one a SMACK64MMAP subject
1728 * can't have as much access as current.
1729 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001730 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1731 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001732 if (mmay == -ENOENT) {
1733 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001734 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001735 }
1736 /*
1737 * If there is a local entry it modifies the
1738 * potential access, too.
1739 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001740 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1741 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001742 if (tmay != -ENOENT)
1743 mmay &= tmay;
1744
1745 /*
1746 * If there is any access available to current that is
1747 * not available to a SMACK64MMAP subject
1748 * deny access.
1749 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001750 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001751 rc = -EACCES;
1752 break;
1753 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001754 }
1755
1756 rcu_read_unlock();
1757
1758 return rc;
1759}
1760
1761/**
Casey Schauflere114e472008-02-04 22:29:50 -08001762 * smack_file_set_fowner - set the file security blob value
1763 * @file: object in question
1764 *
Casey Schauflere114e472008-02-04 22:29:50 -08001765 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001766static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001767{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001768 struct smack_known **blob = smack_file(file);
1769
1770 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001771}
1772
1773/**
1774 * smack_file_send_sigiotask - Smack on sigio
1775 * @tsk: The target task
1776 * @fown: the object the signal come from
1777 * @signum: unused
1778 *
1779 * Allow a privileged task to get signals even if it shouldn't
1780 *
1781 * Returns 0 if a subject with the object's smack could
1782 * write to the task, an error code otherwise.
1783 */
1784static int smack_file_send_sigiotask(struct task_struct *tsk,
1785 struct fown_struct *fown, int signum)
1786{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001787 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001788 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001789 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001790 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001791 struct file *file;
1792 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001793 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001794
1795 /*
1796 * struct fown_struct is never outside the context of a struct file
1797 */
1798 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001799
Etienne Bassetecfcc532009-04-08 20:40:06 +02001800 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001801 blob = smack_file(file);
1802 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001803 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1804 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001805
1806 rcu_read_lock();
1807 tcred = __task_cred(tsk);
1808 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001809 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001810 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001811
1812 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1813 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001814 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001815 return rc;
1816}
1817
1818/**
1819 * smack_file_receive - Smack file receive check
1820 * @file: the object
1821 *
1822 * Returns 0 if current has access, error code otherwise
1823 */
1824static int smack_file_receive(struct file *file)
1825{
Casey Schauflerd166c802014-08-27 14:51:27 -07001826 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001827 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001828 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001829 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001830 struct socket *sock;
1831 struct task_smack *tsp;
1832 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001833
Seung-Woo Kim97775822015-04-17 15:25:04 +09001834 if (unlikely(IS_PRIVATE(inode)))
1835 return 0;
1836
Casey Schaufler4482a442013-12-30 17:37:45 -08001837 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001838 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001839
Casey Schaufler51d59af2017-05-31 08:53:42 -07001840 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001841 sock = SOCKET_I(inode);
1842 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001843 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001844 /*
1845 * If the receiving process can't write to the
1846 * passed socket or if the passed socket can't
1847 * write to the receiving process don't accept
1848 * the passed socket.
1849 */
1850 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1851 rc = smk_bu_file(file, may, rc);
1852 if (rc < 0)
1853 return rc;
1854 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1855 rc = smk_bu_file(file, may, rc);
1856 return rc;
1857 }
Casey Schauflere114e472008-02-04 22:29:50 -08001858 /*
1859 * This code relies on bitmasks.
1860 */
1861 if (file->f_mode & FMODE_READ)
1862 may = MAY_READ;
1863 if (file->f_mode & FMODE_WRITE)
1864 may |= MAY_WRITE;
1865
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001866 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001867 rc = smk_bu_file(file, may, rc);
1868 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001869}
1870
Casey Schaufler531f1d42011-09-19 12:41:42 -07001871/**
Eric Paris83d49852012-04-04 13:45:40 -04001872 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001873 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001874 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001875 *
1876 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001877 * Allow the open only if the task has read access. There are
1878 * many read operations (e.g. fstat) that you can do with an
1879 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001880 *
1881 * Returns 0
1882 */
Al Viro94817692018-07-10 14:13:18 -04001883static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001884{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001885 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001886 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001887 struct smk_audit_info ad;
1888 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001889
Casey Schauflera6834c02014-04-21 11:10:26 -07001890 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1891 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301892 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001893 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001894
1895 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001896}
1897
Casey Schauflere114e472008-02-04 22:29:50 -08001898/*
1899 * Task hooks
1900 */
1901
1902/**
David Howellsee18d642009-09-02 09:14:21 +01001903 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1904 * @new: the new credentials
1905 * @gfp: the atomicity of any memory allocations
1906 *
1907 * Prepare a blank set of credentials for modification. This must allocate all
1908 * the memory the LSM module might require such that cred_transfer() can
1909 * complete without error.
1910 */
1911static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1912{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001913 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001914 return 0;
1915}
1916
1917
1918/**
David Howellsf1752ee2008-11-14 10:39:17 +11001919 * smack_cred_free - "free" task-level security credentials
1920 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001921 *
Casey Schauflere114e472008-02-04 22:29:50 -08001922 */
David Howellsf1752ee2008-11-14 10:39:17 +11001923static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001924{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001925 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001926 struct smack_rule *rp;
1927 struct list_head *l;
1928 struct list_head *n;
1929
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001930 smk_destroy_label_list(&tsp->smk_relabel);
1931
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001932 list_for_each_safe(l, n, &tsp->smk_rules) {
1933 rp = list_entry(l, struct smack_rule, list);
1934 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001935 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001936 }
Casey Schauflere114e472008-02-04 22:29:50 -08001937}
1938
1939/**
David Howellsd84f4f92008-11-14 10:39:23 +11001940 * smack_cred_prepare - prepare new set of credentials for modification
1941 * @new: the new credentials
1942 * @old: the original credentials
1943 * @gfp: the atomicity of any memory allocations
1944 *
1945 * Prepare a new set of credentials for modification.
1946 */
1947static int smack_cred_prepare(struct cred *new, const struct cred *old,
1948 gfp_t gfp)
1949{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001950 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001951 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001952 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001953
Casey Schauflerbbd36622018-11-12 09:30:56 -08001954 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301955
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001956 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1957 if (rc != 0)
1958 return rc;
1959
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001960 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1961 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001962 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001963}
1964
Randy Dunlap251a2a92009-02-18 11:42:33 -08001965/**
David Howellsee18d642009-09-02 09:14:21 +01001966 * smack_cred_transfer - Transfer the old credentials to the new credentials
1967 * @new: the new credentials
1968 * @old: the original credentials
1969 *
1970 * Fill in a set of blank credentials from another set of credentials.
1971 */
1972static void smack_cred_transfer(struct cred *new, const struct cred *old)
1973{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001974 struct task_smack *old_tsp = smack_cred(old);
1975 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001976
1977 new_tsp->smk_task = old_tsp->smk_task;
1978 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001979 mutex_init(&new_tsp->smk_rules_lock);
1980 INIT_LIST_HEAD(&new_tsp->smk_rules);
1981
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001982 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001983}
1984
1985/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001986 * smack_cred_getsecid - get the secid corresponding to a creds structure
1987 * @c: the object creds
1988 * @secid: where to put the result
1989 *
1990 * Sets the secid to contain a u32 version of the smack label.
1991 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001992static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001993{
1994 struct smack_known *skp;
1995
1996 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001997 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001998 *secid = skp->smk_secid;
1999 rcu_read_unlock();
2000}
2001
2002/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002003 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002004 * @new: points to the set of credentials to be modified.
2005 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002006 *
2007 * Set the security data for a kernel service.
2008 */
2009static int smack_kernel_act_as(struct cred *new, u32 secid)
2010{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002011 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002012
Casey Schaufler152f91d2016-11-14 09:38:15 -08002013 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002014 return 0;
2015}
2016
2017/**
2018 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002019 * @new: points to the set of credentials to be modified
2020 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002021 *
2022 * Set the file creation context in a set of credentials to the same
2023 * as the objective context of the specified inode
2024 */
2025static int smack_kernel_create_files_as(struct cred *new,
2026 struct inode *inode)
2027{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002028 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002029 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002030
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002031 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002032 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002033 return 0;
2034}
2035
2036/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002037 * smk_curacc_on_task - helper to log task related access
2038 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002039 * @access: the access requested
2040 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002041 *
2042 * Return 0 if access is permitted
2043 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002044static int smk_curacc_on_task(struct task_struct *p, int access,
2045 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002046{
2047 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002048 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002049 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002050
Casey Schaufler531f1d42011-09-19 12:41:42 -07002051 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002052 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002053 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002054 rc = smk_bu_task(p, access, rc);
2055 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002056}
2057
2058/**
Casey Schauflere114e472008-02-04 22:29:50 -08002059 * smack_task_setpgid - Smack check on setting pgid
2060 * @p: the task object
2061 * @pgid: unused
2062 *
2063 * Return 0 if write access is permitted
2064 */
2065static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2066{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002067 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002068}
2069
2070/**
2071 * smack_task_getpgid - Smack access check for getpgid
2072 * @p: the object task
2073 *
2074 * Returns 0 if current can read the object task, error code otherwise
2075 */
2076static int smack_task_getpgid(struct task_struct *p)
2077{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002078 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002079}
2080
2081/**
2082 * smack_task_getsid - Smack access check for getsid
2083 * @p: the object task
2084 *
2085 * Returns 0 if current can read the object task, error code otherwise
2086 */
2087static int smack_task_getsid(struct task_struct *p)
2088{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002089 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002090}
2091
2092/**
2093 * smack_task_getsecid - get the secid of the task
2094 * @p: the object task
2095 * @secid: where to put the result
2096 *
2097 * Sets the secid to contain a u32 version of the smack label.
2098 */
2099static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2100{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002101 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002102
2103 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002104}
2105
2106/**
2107 * smack_task_setnice - Smack check on setting nice
2108 * @p: the task object
2109 * @nice: unused
2110 *
2111 * Return 0 if write access is permitted
2112 */
2113static int smack_task_setnice(struct task_struct *p, int nice)
2114{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002115 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002116}
2117
2118/**
2119 * smack_task_setioprio - Smack check on setting ioprio
2120 * @p: the task object
2121 * @ioprio: unused
2122 *
2123 * Return 0 if write access is permitted
2124 */
2125static int smack_task_setioprio(struct task_struct *p, int ioprio)
2126{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002127 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002128}
2129
2130/**
2131 * smack_task_getioprio - Smack check on reading ioprio
2132 * @p: the task object
2133 *
2134 * Return 0 if read access is permitted
2135 */
2136static int smack_task_getioprio(struct task_struct *p)
2137{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002138 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002139}
2140
2141/**
2142 * smack_task_setscheduler - Smack check on setting scheduler
2143 * @p: the task object
2144 * @policy: unused
2145 * @lp: unused
2146 *
2147 * Return 0 if read access is permitted
2148 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002149static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002150{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002151 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002152}
2153
2154/**
2155 * smack_task_getscheduler - Smack check on reading scheduler
2156 * @p: the task object
2157 *
2158 * Return 0 if read access is permitted
2159 */
2160static int smack_task_getscheduler(struct task_struct *p)
2161{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002162 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002163}
2164
2165/**
2166 * smack_task_movememory - Smack check on moving memory
2167 * @p: the task object
2168 *
2169 * Return 0 if write access is permitted
2170 */
2171static int smack_task_movememory(struct task_struct *p)
2172{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002173 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002174}
2175
2176/**
2177 * smack_task_kill - Smack check on signal delivery
2178 * @p: the task object
2179 * @info: unused
2180 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002181 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002182 *
2183 * Return 0 if write access is permitted
2184 *
Casey Schauflere114e472008-02-04 22:29:50 -08002185 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002186static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002187 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002188{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002189 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002190 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002191 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002192 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002193
Rafal Krypa18d872f2016-04-04 11:14:53 +02002194 if (!sig)
2195 return 0; /* null signal; existence test */
2196
Etienne Bassetecfcc532009-04-08 20:40:06 +02002197 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2198 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002199 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002200 * Sending a signal requires that the sender
2201 * can write the receiver.
2202 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002203 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002204 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2205 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002206 return rc;
2207 }
Casey Schauflere114e472008-02-04 22:29:50 -08002208 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002209 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002210 * specific behavior. This is not clean. For one thing
2211 * we can't take privilege into account.
2212 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002213 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002214 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2215 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002216 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002217}
2218
2219/**
Casey Schauflere114e472008-02-04 22:29:50 -08002220 * smack_task_to_inode - copy task smack into the inode blob
2221 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002222 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002223 *
2224 * Sets the smack pointer in the inode security blob
2225 */
2226static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2227{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002228 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002229 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002230
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002231 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002232 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002233}
2234
2235/*
2236 * Socket hooks.
2237 */
2238
2239/**
2240 * smack_sk_alloc_security - Allocate a socket blob
2241 * @sk: the socket
2242 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002243 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002244 *
2245 * Assign Smack pointers to current
2246 *
2247 * Returns 0 on success, -ENOMEM is there's no memory
2248 */
2249static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2250{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002251 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002252 struct socket_smack *ssp;
2253
2254 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2255 if (ssp == NULL)
2256 return -ENOMEM;
2257
jooseong lee08382c92016-11-03 11:54:39 +01002258 /*
2259 * Sockets created by kernel threads receive web label.
2260 */
2261 if (unlikely(current->flags & PF_KTHREAD)) {
2262 ssp->smk_in = &smack_known_web;
2263 ssp->smk_out = &smack_known_web;
2264 } else {
2265 ssp->smk_in = skp;
2266 ssp->smk_out = skp;
2267 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002268 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002269
2270 sk->sk_security = ssp;
2271
2272 return 0;
2273}
2274
2275/**
2276 * smack_sk_free_security - Free a socket blob
2277 * @sk: the socket
2278 *
2279 * Clears the blob pointer
2280 */
2281static void smack_sk_free_security(struct sock *sk)
2282{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302283#ifdef SMACK_IPV6_PORT_LABELING
2284 struct smk_port_label *spp;
2285
2286 if (sk->sk_family == PF_INET6) {
2287 rcu_read_lock();
2288 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2289 if (spp->smk_sock != sk)
2290 continue;
2291 spp->smk_can_reuse = 1;
2292 break;
2293 }
2294 rcu_read_unlock();
2295 }
2296#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002297 kfree(sk->sk_security);
2298}
2299
2300/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002301* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002302* @sip: the object end
2303*
2304* looks for host based access restrictions
2305*
2306* This version will only be appropriate for really small sets of single label
2307* hosts. The caller is responsible for ensuring that the RCU read lock is
2308* taken before calling this function.
2309*
2310* Returns the label of the far end or NULL if it's not special.
2311*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002312static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002313{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002314 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002315 struct in_addr *siap = &sip->sin_addr;
2316
2317 if (siap->s_addr == 0)
2318 return NULL;
2319
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002320 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2321 /*
2322 * we break after finding the first match because
2323 * the list is sorted from longest to shortest mask
2324 * so we have found the most specific match
2325 */
2326 if (snp->smk_host.s_addr ==
2327 (siap->s_addr & snp->smk_mask.s_addr))
2328 return snp->smk_label;
2329
2330 return NULL;
2331}
2332
2333#if IS_ENABLED(CONFIG_IPV6)
2334/*
2335 * smk_ipv6_localhost - Check for local ipv6 host address
2336 * @sip: the address
2337 *
2338 * Returns boolean true if this is the localhost address
2339 */
2340static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2341{
2342 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2343 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2344
2345 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2346 ntohs(be16p[7]) == 1)
2347 return true;
2348 return false;
2349}
2350
2351/**
2352* smack_ipv6host_label - check host based restrictions
2353* @sip: the object end
2354*
2355* looks for host based access restrictions
2356*
2357* This version will only be appropriate for really small sets of single label
2358* hosts. The caller is responsible for ensuring that the RCU read lock is
2359* taken before calling this function.
2360*
2361* Returns the label of the far end or NULL if it's not special.
2362*/
2363static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2364{
2365 struct smk_net6addr *snp;
2366 struct in6_addr *sap = &sip->sin6_addr;
2367 int i;
2368 int found = 0;
2369
2370 /*
2371 * It's local. Don't look for a host label.
2372 */
2373 if (smk_ipv6_localhost(sip))
2374 return NULL;
2375
2376 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002377 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002378 * If the label is NULL the entry has
2379 * been renounced. Ignore it.
2380 */
2381 if (snp->smk_label == NULL)
2382 continue;
2383 /*
Paul Moore07feee82009-03-27 17:10:54 -04002384 * we break after finding the first match because
2385 * the list is sorted from longest to shortest mask
2386 * so we have found the most specific match
2387 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002388 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002389 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2390 snp->smk_host.s6_addr16[i]) {
2391 found = 0;
2392 break;
2393 }
Etienne Basset43031542009-03-27 17:11:01 -04002394 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002395 if (found)
2396 return snp->smk_label;
2397 }
Paul Moore07feee82009-03-27 17:10:54 -04002398
2399 return NULL;
2400}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002401#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002402
2403/**
Casey Schauflere114e472008-02-04 22:29:50 -08002404 * smack_netlabel - Set the secattr on a socket
2405 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002406 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002407 *
2408 * Convert the outbound smack value (smk_out) to a
2409 * secattr and attach it to the socket.
2410 *
2411 * Returns 0 on success or an error code
2412 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002413static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002414{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002415 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002416 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002417 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002418
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002419 /*
2420 * Usually the netlabel code will handle changing the
2421 * packet labeling based on the label.
2422 * The case of a single label host is different, because
2423 * a single label host should never get a labeled packet
2424 * even though the label is usually associated with a packet
2425 * label.
2426 */
2427 local_bh_disable();
2428 bh_lock_sock_nested(sk);
2429
2430 if (ssp->smk_out == smack_net_ambient ||
2431 labeled == SMACK_UNLABELED_SOCKET)
2432 netlbl_sock_delattr(sk);
2433 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002434 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002435 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002436 }
2437
2438 bh_unlock_sock(sk);
2439 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002440
Casey Schauflere114e472008-02-04 22:29:50 -08002441 return rc;
2442}
2443
2444/**
Paul Moore07feee82009-03-27 17:10:54 -04002445 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2446 * @sk: the socket
2447 * @sap: the destination address
2448 *
2449 * Set the correct secattr for the given socket based on the destination
2450 * address and perform any outbound access checks needed.
2451 *
2452 * Returns 0 on success or an error code.
2453 *
2454 */
2455static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2456{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002457 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002458 int rc;
2459 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002460 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002461 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002462 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002463
2464 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002465 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002466 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002467#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002468 struct lsm_network_audit net;
2469
Eric Paris48c62af2012-04-02 13:15:44 -04002470 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2471 ad.a.u.net->family = sap->sin_family;
2472 ad.a.u.net->dport = sap->sin_port;
2473 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002474#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002475 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002476 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002477 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2478 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002479 } else {
2480 sk_lbl = SMACK_CIPSO_SOCKET;
2481 rc = 0;
2482 }
2483 rcu_read_unlock();
2484 if (rc != 0)
2485 return rc;
2486
2487 return smack_netlabel(sk, sk_lbl);
2488}
2489
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002490#if IS_ENABLED(CONFIG_IPV6)
2491/**
2492 * smk_ipv6_check - check Smack access
2493 * @subject: subject Smack label
2494 * @object: object Smack label
2495 * @address: address
2496 * @act: the action being taken
2497 *
2498 * Check an IPv6 access
2499 */
2500static int smk_ipv6_check(struct smack_known *subject,
2501 struct smack_known *object,
2502 struct sockaddr_in6 *address, int act)
2503{
2504#ifdef CONFIG_AUDIT
2505 struct lsm_network_audit net;
2506#endif
2507 struct smk_audit_info ad;
2508 int rc;
2509
2510#ifdef CONFIG_AUDIT
2511 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2512 ad.a.u.net->family = PF_INET6;
2513 ad.a.u.net->dport = ntohs(address->sin6_port);
2514 if (act == SMK_RECEIVING)
2515 ad.a.u.net->v6info.saddr = address->sin6_addr;
2516 else
2517 ad.a.u.net->v6info.daddr = address->sin6_addr;
2518#endif
2519 rc = smk_access(subject, object, MAY_WRITE, &ad);
2520 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2521 return rc;
2522}
2523#endif /* CONFIG_IPV6 */
2524
2525#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002526/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002527 * smk_ipv6_port_label - Smack port access table management
2528 * @sock: socket
2529 * @address: address
2530 *
2531 * Create or update the port list entry
2532 */
2533static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2534{
2535 struct sock *sk = sock->sk;
2536 struct sockaddr_in6 *addr6;
2537 struct socket_smack *ssp = sock->sk->sk_security;
2538 struct smk_port_label *spp;
2539 unsigned short port = 0;
2540
2541 if (address == NULL) {
2542 /*
2543 * This operation is changing the Smack information
2544 * on the bound socket. Take the changes to the port
2545 * as well.
2546 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302547 rcu_read_lock();
2548 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002549 if (sk != spp->smk_sock)
2550 continue;
2551 spp->smk_in = ssp->smk_in;
2552 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302553 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002554 return;
2555 }
2556 /*
2557 * A NULL address is only used for updating existing
2558 * bound entries. If there isn't one, it's OK.
2559 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302560 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002561 return;
2562 }
2563
2564 addr6 = (struct sockaddr_in6 *)address;
2565 port = ntohs(addr6->sin6_port);
2566 /*
2567 * This is a special case that is safely ignored.
2568 */
2569 if (port == 0)
2570 return;
2571
2572 /*
2573 * Look for an existing port list entry.
2574 * This is an indication that a port is getting reused.
2575 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302576 rcu_read_lock();
2577 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302578 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002579 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302580 if (spp->smk_can_reuse != 1) {
2581 rcu_read_unlock();
2582 return;
2583 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002584 spp->smk_port = port;
2585 spp->smk_sock = sk;
2586 spp->smk_in = ssp->smk_in;
2587 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302588 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302589 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002590 return;
2591 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302592 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002593 /*
2594 * A new port entry is required.
2595 */
2596 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2597 if (spp == NULL)
2598 return;
2599
2600 spp->smk_port = port;
2601 spp->smk_sock = sk;
2602 spp->smk_in = ssp->smk_in;
2603 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302604 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302605 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002606
Vishal Goel3c7ce342016-11-23 10:31:08 +05302607 mutex_lock(&smack_ipv6_lock);
2608 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2609 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002610 return;
2611}
2612
2613/**
2614 * smk_ipv6_port_check - check Smack port access
2615 * @sock: socket
2616 * @address: address
2617 *
2618 * Create or update the port list entry
2619 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002620static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002621 int act)
2622{
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 struct smk_port_label *spp;
2624 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002625 struct smack_known *skp = NULL;
2626 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002627 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002628
2629 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002630 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002631 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002632 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002633 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002634 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002635 }
2636
2637 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002638 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002639 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002640 if (skp != NULL && object != NULL)
2641 return smk_ipv6_check(skp, object, address, act);
2642 if (skp == NULL)
2643 skp = smack_net_ambient;
2644 if (object == NULL)
2645 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002646
2647 /*
2648 * It's remote, so port lookup does no good.
2649 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002650 if (!smk_ipv6_localhost(address))
2651 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002652
2653 /*
2654 * It's local so the send check has to have passed.
2655 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002656 if (act == SMK_RECEIVING)
2657 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002658
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002659 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302660 rcu_read_lock();
2661 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302662 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002663 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002664 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002665 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002666 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002667 break;
2668 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302669 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002670
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002671 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002672}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002673#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002674
2675/**
Casey Schauflere114e472008-02-04 22:29:50 -08002676 * smack_inode_setsecurity - set smack xattrs
2677 * @inode: the object
2678 * @name: attribute name
2679 * @value: attribute value
2680 * @size: size of the attribute
2681 * @flags: unused
2682 *
2683 * Sets the named attribute in the appropriate blob
2684 *
2685 * Returns 0 on success, or an error code
2686 */
2687static int smack_inode_setsecurity(struct inode *inode, const char *name,
2688 const void *value, size_t size, int flags)
2689{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002690 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002691 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002692 struct socket_smack *ssp;
2693 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002694 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002695
Casey Schauflerf7112e62012-05-06 15:22:02 -07002696 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302697 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002698
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002699 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002700 if (IS_ERR(skp))
2701 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002702
2703 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002704 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002705 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002706 return 0;
2707 }
2708 /*
2709 * The rest of the Smack xattrs are only on sockets.
2710 */
2711 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2712 return -EOPNOTSUPP;
2713
2714 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002715 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002716 return -EOPNOTSUPP;
2717
2718 ssp = sock->sk->sk_security;
2719
2720 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002721 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002722 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002723 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002724 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002725 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2726 if (rc != 0)
2727 printk(KERN_WARNING
2728 "Smack: \"%s\" netlbl error %d.\n",
2729 __func__, -rc);
2730 }
Casey Schauflere114e472008-02-04 22:29:50 -08002731 } else
2732 return -EOPNOTSUPP;
2733
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002734#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002735 if (sock->sk->sk_family == PF_INET6)
2736 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002737#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002738
Casey Schauflere114e472008-02-04 22:29:50 -08002739 return 0;
2740}
2741
2742/**
2743 * smack_socket_post_create - finish socket setup
2744 * @sock: the socket
2745 * @family: protocol family
2746 * @type: unused
2747 * @protocol: unused
2748 * @kern: unused
2749 *
2750 * Sets the netlabel information on the socket
2751 *
2752 * Returns 0 on success, and error code otherwise
2753 */
2754static int smack_socket_post_create(struct socket *sock, int family,
2755 int type, int protocol, int kern)
2756{
Marcin Lis74123012015-01-22 15:40:33 +01002757 struct socket_smack *ssp;
2758
2759 if (sock->sk == NULL)
2760 return 0;
2761
2762 /*
2763 * Sockets created by kernel threads receive web label.
2764 */
2765 if (unlikely(current->flags & PF_KTHREAD)) {
2766 ssp = sock->sk->sk_security;
2767 ssp->smk_in = &smack_known_web;
2768 ssp->smk_out = &smack_known_web;
2769 }
2770
2771 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002772 return 0;
2773 /*
2774 * Set the outbound netlbl.
2775 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002776 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2777}
2778
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002779/**
2780 * smack_socket_socketpair - create socket pair
2781 * @socka: one socket
2782 * @sockb: another socket
2783 *
2784 * Cross reference the peer labels for SO_PEERSEC
2785 *
2786 * Returns 0 on success, and error code otherwise
2787 */
2788static int smack_socket_socketpair(struct socket *socka,
2789 struct socket *sockb)
2790{
2791 struct socket_smack *asp = socka->sk->sk_security;
2792 struct socket_smack *bsp = sockb->sk->sk_security;
2793
2794 asp->smk_packet = bsp->smk_out;
2795 bsp->smk_packet = asp->smk_out;
2796
2797 return 0;
2798}
2799
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002800#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002801/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002802 * smack_socket_bind - record port binding information.
2803 * @sock: the socket
2804 * @address: the port address
2805 * @addrlen: size of the address
2806 *
2807 * Records the label bound to a port.
2808 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002809 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002810 */
2811static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2812 int addrlen)
2813{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002814 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2815 if (addrlen < SIN6_LEN_RFC2133 ||
2816 address->sa_family != AF_INET6)
2817 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002818 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002819 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002820 return 0;
2821}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002822#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002823
2824/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002825 * smack_socket_connect - connect access check
2826 * @sock: the socket
2827 * @sap: the other end
2828 * @addrlen: size of sap
2829 *
2830 * Verifies that a connection may be possible
2831 *
2832 * Returns 0 on success, and error code otherwise
2833 */
2834static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2835 int addrlen)
2836{
Casey Schauflerc6739442013-05-22 18:42:56 -07002837 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002838#if IS_ENABLED(CONFIG_IPV6)
2839 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2840#endif
2841#ifdef SMACK_IPV6_SECMARK_LABELING
2842 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002843 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002844#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002845
Casey Schauflerc6739442013-05-22 18:42:56 -07002846 if (sock->sk == NULL)
2847 return 0;
2848
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002849#ifdef SMACK_IPV6_SECMARK_LABELING
2850 ssp = sock->sk->sk_security;
2851#endif
2852
Casey Schauflerc6739442013-05-22 18:42:56 -07002853 switch (sock->sk->sk_family) {
2854 case PF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002855 if (addrlen < sizeof(struct sockaddr_in) ||
2856 sap->sa_family != AF_INET)
Casey Schauflerc6739442013-05-22 18:42:56 -07002857 return -EINVAL;
2858 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2859 break;
2860 case PF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002861 if (addrlen < SIN6_LEN_RFC2133 || sap->sa_family != AF_INET6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002862 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002863#ifdef SMACK_IPV6_SECMARK_LABELING
2864 rsp = smack_ipv6host_label(sip);
2865 if (rsp != NULL)
2866 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002867 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002868#endif
2869#ifdef SMACK_IPV6_PORT_LABELING
2870 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2871#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002872 break;
2873 }
2874 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002875}
2876
2877/**
2878 * smack_flags_to_may - convert S_ to MAY_ values
2879 * @flags: the S_ value
2880 *
2881 * Returns the equivalent MAY_ value
2882 */
2883static int smack_flags_to_may(int flags)
2884{
2885 int may = 0;
2886
2887 if (flags & S_IRUGO)
2888 may |= MAY_READ;
2889 if (flags & S_IWUGO)
2890 may |= MAY_WRITE;
2891 if (flags & S_IXUGO)
2892 may |= MAY_EXEC;
2893
2894 return may;
2895}
2896
2897/**
2898 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2899 * @msg: the object
2900 *
2901 * Returns 0
2902 */
2903static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2904{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002905 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002906
Casey Schauflerecd5f822018-11-20 11:55:02 -08002907 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002908 return 0;
2909}
2910
2911/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002912 * smack_of_ipc - the smack pointer for the ipc
2913 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002914 *
2915 * Returns a pointer to the smack value
2916 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002917static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002918{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002919 struct smack_known **blob = smack_ipc(isp);
2920
2921 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002922}
2923
2924/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002925 * smack_ipc_alloc_security - Set the security blob for ipc
2926 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002927 *
2928 * Returns 0
2929 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002930static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002931{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002932 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002933
Casey Schaufler019bcca2018-09-21 17:19:54 -07002934 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002935 return 0;
2936}
2937
2938/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002939 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002940 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002941 * @access : access requested
2942 *
2943 * Returns 0 if current has the requested access, error code otherwise
2944 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002945static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002946{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002947 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002948 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002949 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002950
2951#ifdef CONFIG_AUDIT
2952 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002953 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002954#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002955 rc = smk_curacc(ssp, access, &ad);
2956 rc = smk_bu_current("shm", ssp, access, rc);
2957 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002958}
2959
2960/**
Casey Schauflere114e472008-02-04 22:29:50 -08002961 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002962 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002963 * @shmflg: access requested
2964 *
2965 * Returns 0 if current has the requested access, error code otherwise
2966 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002967static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002968{
Casey Schauflere114e472008-02-04 22:29:50 -08002969 int may;
2970
2971 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002972 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002973}
2974
2975/**
2976 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002977 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002978 * @cmd: what it wants to do
2979 *
2980 * Returns 0 if current has the requested access, error code otherwise
2981 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002982static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002983{
Casey Schauflere114e472008-02-04 22:29:50 -08002984 int may;
2985
2986 switch (cmd) {
2987 case IPC_STAT:
2988 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002989 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002990 may = MAY_READ;
2991 break;
2992 case IPC_SET:
2993 case SHM_LOCK:
2994 case SHM_UNLOCK:
2995 case IPC_RMID:
2996 may = MAY_READWRITE;
2997 break;
2998 case IPC_INFO:
2999 case SHM_INFO:
3000 /*
3001 * System level information.
3002 */
3003 return 0;
3004 default:
3005 return -EINVAL;
3006 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003007 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003008}
3009
3010/**
3011 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003012 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003013 * @shmaddr: unused
3014 * @shmflg: access requested
3015 *
3016 * Returns 0 if current has the requested access, error code otherwise
3017 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003018static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003019 int shmflg)
3020{
Casey Schauflere114e472008-02-04 22:29:50 -08003021 int may;
3022
3023 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003024 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003025}
3026
3027/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003028 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003029 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003030 * @access : access requested
3031 *
3032 * Returns 0 if current has the requested access, error code otherwise
3033 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003034static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003035{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003036 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003037 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003038 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003039
3040#ifdef CONFIG_AUDIT
3041 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003042 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003043#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003044 rc = smk_curacc(ssp, access, &ad);
3045 rc = smk_bu_current("sem", ssp, access, rc);
3046 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003047}
3048
3049/**
Casey Schauflere114e472008-02-04 22:29:50 -08003050 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003051 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003052 * @semflg: access requested
3053 *
3054 * Returns 0 if current has the requested access, error code otherwise
3055 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003056static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003057{
Casey Schauflere114e472008-02-04 22:29:50 -08003058 int may;
3059
3060 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003061 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003062}
3063
3064/**
3065 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003066 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003067 * @cmd: what it wants to do
3068 *
3069 * Returns 0 if current has the requested access, error code otherwise
3070 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003071static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003072{
Casey Schauflere114e472008-02-04 22:29:50 -08003073 int may;
3074
3075 switch (cmd) {
3076 case GETPID:
3077 case GETNCNT:
3078 case GETZCNT:
3079 case GETVAL:
3080 case GETALL:
3081 case IPC_STAT:
3082 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003083 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003084 may = MAY_READ;
3085 break;
3086 case SETVAL:
3087 case SETALL:
3088 case IPC_RMID:
3089 case IPC_SET:
3090 may = MAY_READWRITE;
3091 break;
3092 case IPC_INFO:
3093 case SEM_INFO:
3094 /*
3095 * System level information
3096 */
3097 return 0;
3098 default:
3099 return -EINVAL;
3100 }
3101
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003102 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003103}
3104
3105/**
3106 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003107 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003108 * @sops: unused
3109 * @nsops: unused
3110 * @alter: unused
3111 *
3112 * Treated as read and write in all cases.
3113 *
3114 * Returns 0 if access is allowed, error code otherwise
3115 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003116static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003117 unsigned nsops, int alter)
3118{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003120}
3121
3122/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003123 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003124 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003125 * @access : access requested
3126 *
3127 * return 0 if current has access, error otherwise
3128 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003129static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003130{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003131 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003132 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003133 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003134
3135#ifdef CONFIG_AUDIT
3136 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003137 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003138#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003139 rc = smk_curacc(msp, access, &ad);
3140 rc = smk_bu_current("msq", msp, access, rc);
3141 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003142}
3143
3144/**
Casey Schauflere114e472008-02-04 22:29:50 -08003145 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003146 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003147 * @msqflg: access requested
3148 *
3149 * Returns 0 if current has the requested access, error code otherwise
3150 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003151static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003152{
Casey Schauflere114e472008-02-04 22:29:50 -08003153 int may;
3154
3155 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003156 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003157}
3158
3159/**
3160 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003161 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003162 * @cmd: what it wants to do
3163 *
3164 * Returns 0 if current has the requested access, error code otherwise
3165 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003166static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003167{
Casey Schauflere114e472008-02-04 22:29:50 -08003168 int may;
3169
3170 switch (cmd) {
3171 case IPC_STAT:
3172 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003173 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003174 may = MAY_READ;
3175 break;
3176 case IPC_SET:
3177 case IPC_RMID:
3178 may = MAY_READWRITE;
3179 break;
3180 case IPC_INFO:
3181 case MSG_INFO:
3182 /*
3183 * System level information
3184 */
3185 return 0;
3186 default:
3187 return -EINVAL;
3188 }
3189
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003190 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003191}
3192
3193/**
3194 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003195 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003196 * @msg: unused
3197 * @msqflg: access requested
3198 *
3199 * Returns 0 if current has the requested access, error code otherwise
3200 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003201static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003202 int msqflg)
3203{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003204 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003205
Etienne Bassetecfcc532009-04-08 20:40:06 +02003206 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003207 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003208}
3209
3210/**
3211 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003212 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003213 * @msg: unused
3214 * @target: unused
3215 * @type: unused
3216 * @mode: unused
3217 *
3218 * Returns 0 if current has read and write access, error code otherwise
3219 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003220static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003221 struct task_struct *target, long type, int mode)
3222{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003223 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003224}
3225
3226/**
3227 * smack_ipc_permission - Smack access for ipc_permission()
3228 * @ipp: the object permissions
3229 * @flag: access requested
3230 *
3231 * Returns 0 if current has read and write access, error code otherwise
3232 */
3233static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3234{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003235 struct smack_known **blob = smack_ipc(ipp);
3236 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003237 int may = smack_flags_to_may(flag);
3238 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003239 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003240
Etienne Bassetecfcc532009-04-08 20:40:06 +02003241#ifdef CONFIG_AUDIT
3242 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3243 ad.a.u.ipc_id = ipp->id;
3244#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003245 rc = smk_curacc(iskp, may, &ad);
3246 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003247 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003248}
3249
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003250/**
3251 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003252 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003253 * @secid: where result will be saved
3254 */
3255static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3256{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003257 struct smack_known **blob = smack_ipc(ipp);
3258 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003259
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003260 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003261}
3262
Casey Schauflere114e472008-02-04 22:29:50 -08003263/**
3264 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003265 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003266 * @inode: the object
3267 *
3268 * Set the inode's security blob if it hasn't been done already.
3269 */
3270static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3271{
3272 struct super_block *sbp;
3273 struct superblock_smack *sbsp;
3274 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003275 struct smack_known *skp;
3276 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003277 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003278 char trattr[TRANS_TRUE_SIZE];
3279 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003280 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003281 struct dentry *dp;
3282
3283 if (inode == NULL)
3284 return;
3285
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003286 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003287
3288 mutex_lock(&isp->smk_lock);
3289 /*
3290 * If the inode is already instantiated
3291 * take the quick way out
3292 */
3293 if (isp->smk_flags & SMK_INODE_INSTANT)
3294 goto unlockandout;
3295
3296 sbp = inode->i_sb;
3297 sbsp = sbp->s_security;
3298 /*
3299 * We're going to use the superblock default label
3300 * if there's no label on the file.
3301 */
3302 final = sbsp->smk_default;
3303
3304 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003305 * If this is the root inode the superblock
3306 * may be in the process of initialization.
3307 * If that is the case use the root value out
3308 * of the superblock.
3309 */
3310 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003311 switch (sbp->s_magic) {
3312 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003313 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003314 /*
3315 * The cgroup filesystem is never mounted,
3316 * so there's no opportunity to set the mount
3317 * options.
3318 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003319 sbsp->smk_root = &smack_known_star;
3320 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003321 isp->smk_inode = sbsp->smk_root;
3322 break;
3323 case TMPFS_MAGIC:
3324 /*
3325 * What about shmem/tmpfs anonymous files with dentry
3326 * obtained from d_alloc_pseudo()?
3327 */
3328 isp->smk_inode = smk_of_current();
3329 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003330 case PIPEFS_MAGIC:
3331 isp->smk_inode = smk_of_current();
3332 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003333 case SOCKFS_MAGIC:
3334 /*
3335 * Socket access is controlled by the socket
3336 * structures associated with the task involved.
3337 */
3338 isp->smk_inode = &smack_known_star;
3339 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003340 default:
3341 isp->smk_inode = sbsp->smk_root;
3342 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003343 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003344 isp->smk_flags |= SMK_INODE_INSTANT;
3345 goto unlockandout;
3346 }
3347
3348 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003349 * This is pretty hackish.
3350 * Casey says that we shouldn't have to do
3351 * file system specific code, but it does help
3352 * with keeping it simple.
3353 */
3354 switch (sbp->s_magic) {
3355 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003356 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003357 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003358 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003359 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003360 * that the smack file system doesn't do
3361 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003362 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003363 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003364 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003365 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003366 break;
3367 case DEVPTS_SUPER_MAGIC:
3368 /*
3369 * devpts seems content with the label of the task.
3370 * Programs that change smack have to treat the
3371 * pty with respect.
3372 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003373 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003374 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003375 case PROC_SUPER_MAGIC:
3376 /*
3377 * Casey says procfs appears not to care.
3378 * The superblock default suffices.
3379 */
3380 break;
3381 case TMPFS_MAGIC:
3382 /*
3383 * Device labels should come from the filesystem,
3384 * but watch out, because they're volitile,
3385 * getting recreated on every reboot.
3386 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003387 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003388 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003389 * If a smack value has been set we want to use it,
3390 * but since tmpfs isn't giving us the opportunity
3391 * to set mount options simulate setting the
3392 * superblock default.
3393 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003394 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003395 default:
3396 /*
3397 * This isn't an understood special case.
3398 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003399 */
3400
3401 /*
3402 * UNIX domain sockets use lower level socket data.
3403 */
3404 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003405 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003406 break;
3407 }
3408 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003409 * No xattr support means, alas, no SMACK label.
3410 * Use the aforeapplied default.
3411 * It would be curious if the label of the task
3412 * does not match that assigned.
3413 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003414 if (!(inode->i_opflags & IOP_XATTR))
3415 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003416 /*
3417 * Get the dentry for xattr.
3418 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003419 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003420 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003421 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003422 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003423
3424 /*
3425 * Transmuting directory
3426 */
3427 if (S_ISDIR(inode->i_mode)) {
3428 /*
3429 * If this is a new directory and the label was
3430 * transmuted when the inode was initialized
3431 * set the transmute attribute on the directory
3432 * and mark the inode.
3433 *
3434 * If there is a transmute attribute on the
3435 * directory mark the inode.
3436 */
3437 if (isp->smk_flags & SMK_INODE_CHANGED) {
3438 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003439 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003440 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003441 TRANS_TRUE, TRANS_TRUE_SIZE,
3442 0);
3443 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003444 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003445 XATTR_NAME_SMACKTRANSMUTE, trattr,
3446 TRANS_TRUE_SIZE);
3447 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3448 TRANS_TRUE_SIZE) != 0)
3449 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003450 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003451 if (rc >= 0)
3452 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003453 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003454 /*
3455 * Don't let the exec or mmap label be "*" or "@".
3456 */
3457 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3458 if (IS_ERR(skp) || skp == &smack_known_star ||
3459 skp == &smack_known_web)
3460 skp = NULL;
3461 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003462
Casey Schaufler19760ad2013-12-16 16:27:26 -08003463 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003464 if (IS_ERR(skp) || skp == &smack_known_star ||
3465 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003466 skp = NULL;
3467 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003468
Casey Schauflere114e472008-02-04 22:29:50 -08003469 dput(dp);
3470 break;
3471 }
3472
3473 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003474 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003475 else
3476 isp->smk_inode = final;
3477
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003478 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003479
3480unlockandout:
3481 mutex_unlock(&isp->smk_lock);
3482 return;
3483}
3484
3485/**
3486 * smack_getprocattr - Smack process attribute access
3487 * @p: the object task
3488 * @name: the name of the attribute in /proc/.../attr
3489 * @value: where to put the result
3490 *
3491 * Places a copy of the task Smack into value
3492 *
3493 * Returns the length of the smack label or an error code
3494 */
3495static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3496{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003497 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003498 char *cp;
3499 int slen;
3500
3501 if (strcmp(name, "current") != 0)
3502 return -EINVAL;
3503
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003504 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003505 if (cp == NULL)
3506 return -ENOMEM;
3507
3508 slen = strlen(cp);
3509 *value = cp;
3510 return slen;
3511}
3512
3513/**
3514 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003515 * @name: the name of the attribute in /proc/.../attr
3516 * @value: the value to set
3517 * @size: the size of the value
3518 *
3519 * Sets the Smack value of the task. Only setting self
3520 * is permitted and only with privilege
3521 *
3522 * Returns the length of the smack label or an error code
3523 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003524static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003525{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003526 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003527 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003528 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003529 struct smack_known_list_elem *sklep;
3530 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003531
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003532 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003533 return -EPERM;
3534
Casey Schauflerf7112e62012-05-06 15:22:02 -07003535 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003536 return -EINVAL;
3537
3538 if (strcmp(name, "current") != 0)
3539 return -EINVAL;
3540
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003541 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003542 if (IS_ERR(skp))
3543 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003544
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003545 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303546 * No process is ever allowed the web ("@") label
3547 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003548 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303549 if (skp == &smack_known_web || skp == &smack_known_star)
3550 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003551
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003552 if (!smack_privileged(CAP_MAC_ADMIN)) {
3553 rc = -EPERM;
3554 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3555 if (sklep->smk_label == skp) {
3556 rc = 0;
3557 break;
3558 }
3559 if (rc)
3560 return rc;
3561 }
3562
David Howellsd84f4f92008-11-14 10:39:23 +11003563 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003564 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003565 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003566
Casey Schauflerb17103a2018-11-09 16:12:56 -08003567 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003568 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003569 /*
3570 * process can change its label only once
3571 */
3572 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003573
David Howellsd84f4f92008-11-14 10:39:23 +11003574 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003575 return size;
3576}
3577
3578/**
3579 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003580 * @sock: one sock
3581 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003582 * @newsk: unused
3583 *
3584 * Return 0 if a subject with the smack of sock could access
3585 * an object with the smack of other, otherwise an error code
3586 */
David S. Miller3610cda2011-01-05 15:38:53 -08003587static int smack_unix_stream_connect(struct sock *sock,
3588 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003589{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003590 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003591 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003592 struct socket_smack *ssp = sock->sk_security;
3593 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003594 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003595 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003596 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003597#ifdef CONFIG_AUDIT
3598 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003599#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003600
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003601 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3602 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003603 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003604#ifdef CONFIG_AUDIT
3605 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3606 smk_ad_setfield_u_net_sk(&ad, other);
3607#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003608 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3609 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003610 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003611 okp = osp->smk_out;
3612 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003613 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003614 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003615 MAY_WRITE, rc);
3616 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003617 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003618
Casey Schaufler975d5e52011-09-26 14:43:39 -07003619 /*
3620 * Cross reference the peer labels for SO_PEERSEC.
3621 */
3622 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003623 nsp->smk_packet = ssp->smk_out;
3624 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003625 }
3626
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003627 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003628}
3629
3630/**
3631 * smack_unix_may_send - Smack access on UDS
3632 * @sock: one socket
3633 * @other: the other socket
3634 *
3635 * Return 0 if a subject with the smack of sock could access
3636 * an object with the smack of other, otherwise an error code
3637 */
3638static int smack_unix_may_send(struct socket *sock, struct socket *other)
3639{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003640 struct socket_smack *ssp = sock->sk->sk_security;
3641 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003642 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003643 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003644
Kees Cook923e9a12012-04-10 13:26:44 -07003645#ifdef CONFIG_AUDIT
3646 struct lsm_network_audit net;
3647
Eric Paris48c62af2012-04-02 13:15:44 -04003648 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003649 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003650#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003651
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003652 if (smack_privileged(CAP_MAC_OVERRIDE))
3653 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003654
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003655 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3656 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003657 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003658}
3659
3660/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003661 * smack_socket_sendmsg - Smack check based on destination host
3662 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003663 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003664 * @size: the size of the message
3665 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003666 * Return 0 if the current subject can write to the destination host.
3667 * For IPv4 this is only a question if the destination is a single label host.
3668 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003669 */
3670static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3671 int size)
3672{
3673 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003674#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003675 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003676#endif
3677#ifdef SMACK_IPV6_SECMARK_LABELING
3678 struct socket_smack *ssp = sock->sk->sk_security;
3679 struct smack_known *rsp;
3680#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003681 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003682
3683 /*
3684 * Perfectly reasonable for this to be NULL
3685 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003686 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003687 return 0;
3688
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003689 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003690 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003691 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3692 sip->sin_family != AF_INET)
3693 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003694 rc = smack_netlabel_send(sock->sk, sip);
3695 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003696#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003697 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003698 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3699 sap->sin6_family != AF_INET6)
3700 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003701#ifdef SMACK_IPV6_SECMARK_LABELING
3702 rsp = smack_ipv6host_label(sap);
3703 if (rsp != NULL)
3704 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3705 SMK_CONNECTING);
3706#endif
3707#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003708 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003709#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003710#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003711 break;
3712 }
3713 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003714}
3715
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003716/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003717 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003718 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003719 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003720 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003721 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003722 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003723static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3724 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003725{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003726 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003727 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003728 int acat;
3729 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003730
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003731 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003732 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003733 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003734 * If there are flags but no level netlabel isn't
3735 * behaving the way we expect it to.
3736 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003737 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003738 * Without guidance regarding the smack value
3739 * for the packet fall back on the network
3740 * ambient value.
3741 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003742 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303743 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003744 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003745 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003746 /*
3747 * Compare the catsets. Use the netlbl APIs.
3748 */
3749 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3750 if ((skp->smk_netlabel.flags &
3751 NETLBL_SECATTR_MLS_CAT) == 0)
3752 found = 1;
3753 break;
3754 }
3755 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003756 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3757 acat + 1);
3758 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003759 skp->smk_netlabel.attr.mls.cat,
3760 kcat + 1);
3761 if (acat < 0 || kcat < 0)
3762 break;
3763 }
3764 if (acat == kcat) {
3765 found = 1;
3766 break;
3767 }
Casey Schauflere114e472008-02-04 22:29:50 -08003768 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003769 rcu_read_unlock();
3770
3771 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003772 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003773
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003774 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003775 return &smack_known_web;
3776 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003777 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003778 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003779 /*
3780 * Looks like a fallback, which gives us a secid.
3781 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003782 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003783 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003784 * Without guidance regarding the smack value
3785 * for the packet fall back on the network
3786 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003787 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003788 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003789}
3790
Casey Schaufler69f287a2014-12-12 17:08:40 -08003791#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003792static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003793{
Casey Schauflerc6739442013-05-22 18:42:56 -07003794 u8 nexthdr;
3795 int offset;
3796 int proto = -EINVAL;
3797 struct ipv6hdr _ipv6h;
3798 struct ipv6hdr *ip6;
3799 __be16 frag_off;
3800 struct tcphdr _tcph, *th;
3801 struct udphdr _udph, *uh;
3802 struct dccp_hdr _dccph, *dh;
3803
3804 sip->sin6_port = 0;
3805
3806 offset = skb_network_offset(skb);
3807 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3808 if (ip6 == NULL)
3809 return -EINVAL;
3810 sip->sin6_addr = ip6->saddr;
3811
3812 nexthdr = ip6->nexthdr;
3813 offset += sizeof(_ipv6h);
3814 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3815 if (offset < 0)
3816 return -EINVAL;
3817
3818 proto = nexthdr;
3819 switch (proto) {
3820 case IPPROTO_TCP:
3821 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3822 if (th != NULL)
3823 sip->sin6_port = th->source;
3824 break;
3825 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003826 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003827 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3828 if (uh != NULL)
3829 sip->sin6_port = uh->source;
3830 break;
3831 case IPPROTO_DCCP:
3832 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3833 if (dh != NULL)
3834 sip->sin6_port = dh->dccph_sport;
3835 break;
3836 }
3837 return proto;
3838}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003839#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003840
Casey Schauflere114e472008-02-04 22:29:50 -08003841/**
3842 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3843 * @sk: socket
3844 * @skb: packet
3845 *
3846 * Returns 0 if the packet should be delivered, an error code otherwise
3847 */
3848static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3849{
3850 struct netlbl_lsm_secattr secattr;
3851 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003852 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003853 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003854 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003855 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003856#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003857 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003858#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003859#if IS_ENABLED(CONFIG_IPV6)
3860 struct sockaddr_in6 sadd;
3861 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003862
3863 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3864 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003865#endif /* CONFIG_IPV6 */
3866
Piotr Sawicki129a9982018-07-19 11:42:58 +02003867 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003868 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003869#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3870 /*
3871 * If there is a secmark use it rather than the CIPSO label.
3872 * If there is no secmark fall back to CIPSO.
3873 * The secmark is assumed to reflect policy better.
3874 */
3875 if (skb && skb->secmark != 0) {
3876 skp = smack_from_secid(skb->secmark);
3877 goto access_check;
3878 }
3879#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003880 /*
3881 * Translate what netlabel gave us.
3882 */
3883 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003884
Piotr Sawicki129a9982018-07-19 11:42:58 +02003885 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003886 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003887 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003888 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003889 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003890
Casey Schauflerc6739442013-05-22 18:42:56 -07003891 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003892
Casey Schaufler69f287a2014-12-12 17:08:40 -08003893#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3894access_check:
3895#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003896#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003897 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003898 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003899 ad.a.u.net->netif = skb->skb_iif;
3900 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003901#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003902 /*
3903 * Receiving a packet requires that the other end
3904 * be able to write here. Read access is not required.
3905 * This is the simplist possible security model
3906 * for networking.
3907 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003908 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3909 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003910 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003911 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003912 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003913 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003914#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003915 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003917 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3918 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003919 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003920#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003921 if (skb && skb->secmark != 0)
3922 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003923 else if (smk_ipv6_localhost(&sadd))
3924 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003925 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003926 skp = smack_ipv6host_label(&sadd);
3927 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003928 skp = smack_net_ambient;
3929#ifdef CONFIG_AUDIT
3930 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003931 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003932 ad.a.u.net->netif = skb->skb_iif;
3933 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3934#endif /* CONFIG_AUDIT */
3935 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3936 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3937 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003938#endif /* SMACK_IPV6_SECMARK_LABELING */
3939#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003940 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003941#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003942 if (rc != 0)
3943 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3944 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003945 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003946#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003947 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003948
Paul Moorea8134292008-10-10 10:16:31 -04003949 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003950}
3951
3952/**
3953 * smack_socket_getpeersec_stream - pull in packet label
3954 * @sock: the socket
3955 * @optval: user's destination
3956 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003957 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003958 *
3959 * returns zero on success, an error code otherwise
3960 */
3961static int smack_socket_getpeersec_stream(struct socket *sock,
3962 char __user *optval,
3963 int __user *optlen, unsigned len)
3964{
3965 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003966 char *rcp = "";
3967 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003968 int rc = 0;
3969
3970 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003971 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003972 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003973 slen = strlen(rcp) + 1;
3974 }
Casey Schauflere114e472008-02-04 22:29:50 -08003975
3976 if (slen > len)
3977 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003978 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003979 rc = -EFAULT;
3980
3981 if (put_user(slen, optlen) != 0)
3982 rc = -EFAULT;
3983
3984 return rc;
3985}
3986
3987
3988/**
3989 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003990 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003991 * @skb: packet data
3992 * @secid: pointer to where to put the secid of the packet
3993 *
3994 * Sets the netlabel socket state on sk from parent
3995 */
3996static int smack_socket_getpeersec_dgram(struct socket *sock,
3997 struct sk_buff *skb, u32 *secid)
3998
3999{
4000 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004001 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004002 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004003 int family = PF_UNSPEC;
4004 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004005 int rc;
4006
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004007 if (skb != NULL) {
4008 if (skb->protocol == htons(ETH_P_IP))
4009 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004010#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004011 else if (skb->protocol == htons(ETH_P_IPV6))
4012 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004013#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004014 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004015 if (family == PF_UNSPEC && sock != NULL)
4016 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004017
Casey Schaufler69f287a2014-12-12 17:08:40 -08004018 switch (family) {
4019 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004020 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004021 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004022 break;
4023 case PF_INET:
4024#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4025 s = skb->secmark;
4026 if (s != 0)
4027 break;
4028#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004029 /*
4030 * Translate what netlabel gave us.
4031 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004032 if (sock != NULL && sock->sk != NULL)
4033 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004034 netlbl_secattr_init(&secattr);
4035 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4036 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004037 skp = smack_from_secattr(&secattr, ssp);
4038 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004039 }
4040 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004041 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004042 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004043#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004044 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004045#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004046 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004047 }
4048 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004049 if (s == 0)
4050 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004051 return 0;
4052}
4053
4054/**
Paul Moore07feee82009-03-27 17:10:54 -04004055 * smack_sock_graft - Initialize a newly created socket with an existing sock
4056 * @sk: child sock
4057 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004058 *
Paul Moore07feee82009-03-27 17:10:54 -04004059 * Set the smk_{in,out} state of an existing sock based on the process that
4060 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004061 */
4062static void smack_sock_graft(struct sock *sk, struct socket *parent)
4063{
4064 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004065 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004066
Paul Moore07feee82009-03-27 17:10:54 -04004067 if (sk == NULL ||
4068 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004069 return;
4070
4071 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004072 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004073 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004074 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004075}
4076
4077/**
4078 * smack_inet_conn_request - Smack access check on connect
4079 * @sk: socket involved
4080 * @skb: packet
4081 * @req: unused
4082 *
4083 * Returns 0 if a task with the packet label could write to
4084 * the socket, otherwise an error code
4085 */
4086static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4087 struct request_sock *req)
4088{
Paul Moore07feee82009-03-27 17:10:54 -04004089 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004090 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004091 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004092 struct netlbl_lsm_secattr secattr;
4093 struct sockaddr_in addr;
4094 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004095 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004096 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004097 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004098#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004099 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004100#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004101
Casey Schaufler69f287a2014-12-12 17:08:40 -08004102#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004103 if (family == PF_INET6) {
4104 /*
4105 * Handle mapped IPv4 packets arriving
4106 * via IPv6 sockets. Don't set up netlabel
4107 * processing on IPv6.
4108 */
4109 if (skb->protocol == htons(ETH_P_IP))
4110 family = PF_INET;
4111 else
4112 return 0;
4113 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004114#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004115
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004116#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4117 /*
4118 * If there is a secmark use it rather than the CIPSO label.
4119 * If there is no secmark fall back to CIPSO.
4120 * The secmark is assumed to reflect policy better.
4121 */
4122 if (skb && skb->secmark != 0) {
4123 skp = smack_from_secid(skb->secmark);
4124 goto access_check;
4125 }
4126#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4127
Paul Moore07feee82009-03-27 17:10:54 -04004128 netlbl_secattr_init(&secattr);
4129 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004130 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004131 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004132 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004133 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004134 netlbl_secattr_destroy(&secattr);
4135
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004136#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4137access_check:
4138#endif
4139
Etienne Bassetecfcc532009-04-08 20:40:06 +02004140#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004141 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4142 ad.a.u.net->family = family;
4143 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004144 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4145#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004146 /*
Paul Moore07feee82009-03-27 17:10:54 -04004147 * Receiving a packet requires that the other end be able to write
4148 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004149 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004150 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4151 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004152 if (rc != 0)
4153 return rc;
4154
4155 /*
4156 * Save the peer's label in the request_sock so we can later setup
4157 * smk_packet in the child socket so that SO_PEERCRED can report it.
4158 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004159 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004160
4161 /*
4162 * We need to decide if we want to label the incoming connection here
4163 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004164 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004165 */
4166 hdr = ip_hdr(skb);
4167 addr.sin_addr.s_addr = hdr->saddr;
4168 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004169 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004170 rcu_read_unlock();
4171
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004172 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004173 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004174 else
Paul Moore07feee82009-03-27 17:10:54 -04004175 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004176
4177 return rc;
4178}
4179
Paul Moore07feee82009-03-27 17:10:54 -04004180/**
4181 * smack_inet_csk_clone - Copy the connection information to the new socket
4182 * @sk: the new socket
4183 * @req: the connection's request_sock
4184 *
4185 * Transfer the connection's peer label to the newly created socket.
4186 */
4187static void smack_inet_csk_clone(struct sock *sk,
4188 const struct request_sock *req)
4189{
4190 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004191 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004192
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004193 if (req->peer_secid != 0) {
4194 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004195 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004196 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004197 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004198}
4199
Casey Schauflere114e472008-02-04 22:29:50 -08004200/*
4201 * Key management security hooks
4202 *
4203 * Casey has not tested key support very heavily.
4204 * The permission check is most likely too restrictive.
4205 * If you care about keys please have a look.
4206 */
4207#ifdef CONFIG_KEYS
4208
4209/**
4210 * smack_key_alloc - Set the key security blob
4211 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004212 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004213 * @flags: unused
4214 *
4215 * No allocation required
4216 *
4217 * Returns 0
4218 */
David Howellsd84f4f92008-11-14 10:39:23 +11004219static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004220 unsigned long flags)
4221{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004222 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004223
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004224 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004225 return 0;
4226}
4227
4228/**
4229 * smack_key_free - Clear the key security blob
4230 * @key: the object
4231 *
4232 * Clear the blob pointer
4233 */
4234static void smack_key_free(struct key *key)
4235{
4236 key->security = NULL;
4237}
4238
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004239/**
Casey Schauflere114e472008-02-04 22:29:50 -08004240 * smack_key_permission - Smack access on a key
4241 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004242 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004243 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004244 *
4245 * Return 0 if the task has read and write to the object,
4246 * an error code otherwise
4247 */
4248static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004249 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004250{
4251 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004252 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004253 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004254 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004255 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004256
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004257 /*
4258 * Validate requested permissions
4259 */
4260 if (perm & ~KEY_NEED_ALL)
4261 return -EINVAL;
4262
Casey Schauflere114e472008-02-04 22:29:50 -08004263 keyp = key_ref_to_ptr(key_ref);
4264 if (keyp == NULL)
4265 return -EINVAL;
4266 /*
4267 * If the key hasn't been initialized give it access so that
4268 * it may do so.
4269 */
4270 if (keyp->security == NULL)
4271 return 0;
4272 /*
4273 * This should not occur
4274 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004275 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004276 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004277
4278 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4279 return 0;
4280
Etienne Bassetecfcc532009-04-08 20:40:06 +02004281#ifdef CONFIG_AUDIT
4282 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4283 ad.a.u.key_struct.key = keyp->serial;
4284 ad.a.u.key_struct.key_desc = keyp->description;
4285#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004286 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4287 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004288 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004289 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004290 rc = smk_access(tkp, keyp->security, request, &ad);
4291 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4292 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004293}
José Bollo7fc5f362015-02-17 15:41:22 +01004294
4295/*
4296 * smack_key_getsecurity - Smack label tagging the key
4297 * @key points to the key to be queried
4298 * @_buffer points to a pointer that should be set to point to the
4299 * resulting string (if no label or an error occurs).
4300 * Return the length of the string (including terminating NUL) or -ve if
4301 * an error.
4302 * May also return 0 (and a NULL buffer pointer) if there is no label.
4303 */
4304static int smack_key_getsecurity(struct key *key, char **_buffer)
4305{
4306 struct smack_known *skp = key->security;
4307 size_t length;
4308 char *copy;
4309
4310 if (key->security == NULL) {
4311 *_buffer = NULL;
4312 return 0;
4313 }
4314
4315 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4316 if (copy == NULL)
4317 return -ENOMEM;
4318 length = strlen(copy) + 1;
4319
4320 *_buffer = copy;
4321 return length;
4322}
4323
Casey Schauflere114e472008-02-04 22:29:50 -08004324#endif /* CONFIG_KEYS */
4325
4326/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004327 * Smack Audit hooks
4328 *
4329 * Audit requires a unique representation of each Smack specific
4330 * rule. This unique representation is used to distinguish the
4331 * object to be audited from remaining kernel objects and also
4332 * works as a glue between the audit hooks.
4333 *
4334 * Since repository entries are added but never deleted, we'll use
4335 * the smack_known label address related to the given audit rule as
4336 * the needed unique representation. This also better fits the smack
4337 * model where nearly everything is a label.
4338 */
4339#ifdef CONFIG_AUDIT
4340
4341/**
4342 * smack_audit_rule_init - Initialize a smack audit rule
4343 * @field: audit rule fields given from user-space (audit.h)
4344 * @op: required testing operator (=, !=, >, <, ...)
4345 * @rulestr: smack label to be audited
4346 * @vrule: pointer to save our own audit rule representation
4347 *
4348 * Prepare to audit cases where (@field @op @rulestr) is true.
4349 * The label to be audited is created if necessay.
4350 */
4351static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4352{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004353 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004354 char **rule = (char **)vrule;
4355 *rule = NULL;
4356
4357 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4358 return -EINVAL;
4359
Al Viro5af75d82008-12-16 05:59:26 -05004360 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004361 return -EINVAL;
4362
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004363 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004364 if (IS_ERR(skp))
4365 return PTR_ERR(skp);
4366
4367 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004368
4369 return 0;
4370}
4371
4372/**
4373 * smack_audit_rule_known - Distinguish Smack audit rules
4374 * @krule: rule of interest, in Audit kernel representation format
4375 *
4376 * This is used to filter Smack rules from remaining Audit ones.
4377 * If it's proved that this rule belongs to us, the
4378 * audit_rule_match hook will be called to do the final judgement.
4379 */
4380static int smack_audit_rule_known(struct audit_krule *krule)
4381{
4382 struct audit_field *f;
4383 int i;
4384
4385 for (i = 0; i < krule->field_count; i++) {
4386 f = &krule->fields[i];
4387
4388 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4389 return 1;
4390 }
4391
4392 return 0;
4393}
4394
4395/**
4396 * smack_audit_rule_match - Audit given object ?
4397 * @secid: security id for identifying the object to test
4398 * @field: audit rule flags given from user-space
4399 * @op: required testing operator
4400 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004401 *
4402 * The core Audit hook. It's used to take the decision of
4403 * whether to audit or not to audit a given object.
4404 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004405static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004406{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004407 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004408 char *rule = vrule;
4409
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004410 if (unlikely(!rule)) {
4411 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004412 return -ENOENT;
4413 }
4414
4415 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4416 return 0;
4417
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004418 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004419
4420 /*
4421 * No need to do string comparisons. If a match occurs,
4422 * both pointers will point to the same smack_known
4423 * label.
4424 */
Al Viro5af75d82008-12-16 05:59:26 -05004425 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004426 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004427 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004428 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004429
4430 return 0;
4431}
4432
Casey Schaufler491a0b02016-01-26 15:08:35 -08004433/*
4434 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004435 * No memory was allocated.
4436 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004437
4438#endif /* CONFIG_AUDIT */
4439
Randy Dunlap251a2a92009-02-18 11:42:33 -08004440/**
David Quigley746df9b2013-05-22 12:50:35 -04004441 * smack_ismaclabel - check if xattr @name references a smack MAC label
4442 * @name: Full xattr name to check.
4443 */
4444static int smack_ismaclabel(const char *name)
4445{
4446 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4447}
4448
4449
4450/**
Casey Schauflere114e472008-02-04 22:29:50 -08004451 * smack_secid_to_secctx - return the smack label for a secid
4452 * @secid: incoming integer
4453 * @secdata: destination
4454 * @seclen: how long it is
4455 *
4456 * Exists for networking code.
4457 */
4458static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4459{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004460 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004461
Eric Parisd5630b92010-10-13 16:24:48 -04004462 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004463 *secdata = skp->smk_known;
4464 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004465 return 0;
4466}
4467
Randy Dunlap251a2a92009-02-18 11:42:33 -08004468/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004469 * smack_secctx_to_secid - return the secid for a smack label
4470 * @secdata: smack label
4471 * @seclen: how long result is
4472 * @secid: outgoing integer
4473 *
4474 * Exists for audit and networking code.
4475 */
David Howellse52c17642008-04-29 20:52:51 +01004476static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004477{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004478 struct smack_known *skp = smk_find_entry(secdata);
4479
4480 if (skp)
4481 *secid = skp->smk_secid;
4482 else
4483 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004484 return 0;
4485}
4486
Casey Schaufler491a0b02016-01-26 15:08:35 -08004487/*
4488 * There used to be a smack_release_secctx hook
4489 * that did nothing back when hooks were in a vector.
4490 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004491 */
Casey Schauflere114e472008-02-04 22:29:50 -08004492
David P. Quigley1ee65e32009-09-03 14:25:57 -04004493static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4494{
4495 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4496}
4497
4498static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4499{
4500 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4501}
4502
4503static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4504{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004505 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004506
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004507 *ctx = skp->smk_known;
4508 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004509 return 0;
4510}
4511
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004512static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4513{
4514
4515 struct task_smack *tsp;
4516 struct smack_known *skp;
4517 struct inode_smack *isp;
4518 struct cred *new_creds = *new;
4519
4520 if (new_creds == NULL) {
4521 new_creds = prepare_creds();
4522 if (new_creds == NULL)
4523 return -ENOMEM;
4524 }
4525
Casey Schauflerb17103a2018-11-09 16:12:56 -08004526 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004527
4528 /*
4529 * Get label from overlay inode and set it in create_sid
4530 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004531 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004532 skp = isp->smk_inode;
4533 tsp->smk_task = skp;
4534 *new = new_creds;
4535 return 0;
4536}
4537
4538static int smack_inode_copy_up_xattr(const char *name)
4539{
4540 /*
4541 * Return 1 if this is the smack access Smack attribute.
4542 */
4543 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4544 return 1;
4545
4546 return -EOPNOTSUPP;
4547}
4548
4549static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4550 struct qstr *name,
4551 const struct cred *old,
4552 struct cred *new)
4553{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004554 struct task_smack *otsp = smack_cred(old);
4555 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004556 struct inode_smack *isp;
4557 int may;
4558
4559 /*
4560 * Use the process credential unless all of
4561 * the transmuting criteria are met
4562 */
4563 ntsp->smk_task = otsp->smk_task;
4564
4565 /*
4566 * the attribute of the containing directory
4567 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004568 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004569
4570 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4571 rcu_read_lock();
4572 may = smk_access_entry(otsp->smk_task->smk_known,
4573 isp->smk_inode->smk_known,
4574 &otsp->smk_task->smk_rules);
4575 rcu_read_unlock();
4576
4577 /*
4578 * If the directory is transmuting and the rule
4579 * providing access is transmuting use the containing
4580 * directory label instead of the process label.
4581 */
4582 if (may > 0 && (may & MAY_TRANSMUTE))
4583 ntsp->smk_task = isp->smk_inode;
4584 }
4585 return 0;
4586}
4587
Casey Schauflerbbd36622018-11-12 09:30:56 -08004588struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4589 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004590 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004591 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004592 .lbs_ipc = sizeof(struct smack_known *),
4593 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004594};
4595
James Morrisca97d932017-02-15 00:18:51 +11004596static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004597 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4598 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4599 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004600
Al Viro0b520752018-12-23 16:02:47 -05004601 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004602 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4603
Casey Schauflere20b0432015-05-02 15:11:36 -07004604 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4605 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004606 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004607 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004608 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304609 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004610
Casey Schauflere20b0432015-05-02 15:11:36 -07004611 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004612
Casey Schauflere20b0432015-05-02 15:11:36 -07004613 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004614 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4615 LSM_HOOK_INIT(inode_link, smack_inode_link),
4616 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4617 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4618 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4619 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4620 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4621 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4622 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4623 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4624 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4625 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4626 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4627 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4628 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4629 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004630
Casey Schauflere20b0432015-05-02 15:11:36 -07004631 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004632 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4633 LSM_HOOK_INIT(file_lock, smack_file_lock),
4634 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4635 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4636 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4637 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4638 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4639 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004640
Casey Schauflere20b0432015-05-02 15:11:36 -07004641 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004642
Casey Schauflere20b0432015-05-02 15:11:36 -07004643 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4644 LSM_HOOK_INIT(cred_free, smack_cred_free),
4645 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4646 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004647 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004648 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4649 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4650 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4651 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4652 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4653 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4654 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4655 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4656 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4657 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4658 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4659 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4660 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004661 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004662
Casey Schauflere20b0432015-05-02 15:11:36 -07004663 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4664 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004665
Casey Schauflere20b0432015-05-02 15:11:36 -07004666 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004667
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004668 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004669 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4670 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4671 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4672 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004673
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004674 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004675 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4676 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4677 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004678
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004679 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004680 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4681 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4682 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004683
Casey Schauflere20b0432015-05-02 15:11:36 -07004684 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004685
Casey Schauflere20b0432015-05-02 15:11:36 -07004686 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4687 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004688
Casey Schauflere20b0432015-05-02 15:11:36 -07004689 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4690 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004691
Casey Schauflere20b0432015-05-02 15:11:36 -07004692 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004693 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004694#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004695 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004696#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004697 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4698 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4699 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4700 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4701 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4702 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4703 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4704 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4705 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4706 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004707
Casey Schauflere114e472008-02-04 22:29:50 -08004708 /* key management security hooks */
4709#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004710 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4711 LSM_HOOK_INIT(key_free, smack_key_free),
4712 LSM_HOOK_INIT(key_permission, smack_key_permission),
4713 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004714#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004715
4716 /* Audit hooks */
4717#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004718 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4719 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4720 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004721#endif /* CONFIG_AUDIT */
4722
Casey Schauflere20b0432015-05-02 15:11:36 -07004723 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4724 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4725 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004726 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4727 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4728 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004729 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4730 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4731 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004732};
4733
Etienne Basset7198e2e2009-03-24 20:53:24 +01004734
Casey Schaufler86812bb2012-04-17 18:55:46 -07004735static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004736{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004737 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004738 * Initialize rule list locks
4739 */
4740 mutex_init(&smack_known_huh.smk_rules_lock);
4741 mutex_init(&smack_known_hat.smk_rules_lock);
4742 mutex_init(&smack_known_floor.smk_rules_lock);
4743 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004744 mutex_init(&smack_known_web.smk_rules_lock);
4745 /*
4746 * Initialize rule lists
4747 */
4748 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4749 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4750 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4751 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004752 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4753 /*
4754 * Create the known labels list
4755 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004756 smk_insert_entry(&smack_known_huh);
4757 smk_insert_entry(&smack_known_hat);
4758 smk_insert_entry(&smack_known_star);
4759 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004760 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004761}
4762
Casey Schauflere114e472008-02-04 22:29:50 -08004763/**
4764 * smack_init - initialize the smack system
4765 *
4766 * Returns 0
4767 */
4768static __init int smack_init(void)
4769{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004770 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004771 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004772
Rohit1a5b4722014-10-15 17:40:41 +05304773 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4774 if (!smack_inode_cache)
4775 return -ENOMEM;
4776
Casey Schaufler4e328b02019-04-02 11:37:12 -07004777 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4778 if (!smack_rule_cache) {
4779 kmem_cache_destroy(smack_inode_cache);
4780 return -ENOMEM;
4781 }
4782
Casey Schauflerbbd36622018-11-12 09:30:56 -08004783 /*
4784 * Set the security state for the initial task.
4785 */
4786 tsp = smack_cred(cred);
4787 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4788
4789 /*
4790 * Register with LSM
4791 */
4792 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004793 smack_enabled = 1;
4794
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004795 pr_info("Smack: Initializing.\n");
4796#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4797 pr_info("Smack: Netfilter enabled.\n");
4798#endif
4799#ifdef SMACK_IPV6_PORT_LABELING
4800 pr_info("Smack: IPv6 port labeling enabled.\n");
4801#endif
4802#ifdef SMACK_IPV6_SECMARK_LABELING
4803 pr_info("Smack: IPv6 Netfilter enabled.\n");
4804#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004805
Casey Schaufler86812bb2012-04-17 18:55:46 -07004806 /* initialize the smack_known_list */
4807 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004808
Casey Schauflere114e472008-02-04 22:29:50 -08004809 return 0;
4810}
4811
4812/*
4813 * Smack requires early initialization in order to label
4814 * all processes and objects when they are created.
4815 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004816DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004817 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004818 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004819 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004820 .init = smack_init,
4821};