blob: d479def4d6a0088de7c99a76f1d6e15481ad5069 [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>
Casey Schauflere114e472008-02-04 22:29:50 -080046#include "smack.h"
47
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020048#define TRANS_TRUE "TRUE"
49#define TRANS_TRUE_SIZE 4
50
Casey Schauflerc6739442013-05-22 18:42:56 -070051#define SMK_CONNECTING 0
52#define SMK_RECEIVING 1
53#define SMK_SENDING 2
54
Casey Schaufler21abb1e2015-07-22 14:25:31 -070055#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053056DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080057static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070058#endif
Rohit1a5b4722014-10-15 17:40:41 +053059static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080060int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070061
Al Viroc3300aa2018-12-16 01:52:24 -050062#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
63static struct {
64 const char *name;
65 int len;
66 int opt;
67} smk_mount_opts[] = {
68 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
Vivek Trivedi3bf27892015-06-22 15:36:06 +053069};
Al Viroc3300aa2018-12-16 01:52:24 -050070#undef A
71
72static int match_opt_prefix(char *s, int l, char **arg)
73{
74 int i;
75
76 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
77 size_t len = smk_mount_opts[i].len;
78 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
79 continue;
80 if (len == l || s[len] != '=')
81 continue;
82 *arg = s + len + 1;
83 return smk_mount_opts[i].opt;
84 }
85 return Opt_error;
86}
Vivek Trivedi3bf27892015-06-22 15:36:06 +053087
Casey Schaufler3d04c922015-08-12 11:56:02 -070088#ifdef CONFIG_SECURITY_SMACK_BRINGUP
89static char *smk_bu_mess[] = {
90 "Bringup Error", /* Unused */
91 "Bringup", /* SMACK_BRINGUP_ALLOW */
92 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
93 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
94};
95
Casey Schauflerd166c802014-08-27 14:51:27 -070096static void smk_bu_mode(int mode, char *s)
97{
98 int i = 0;
99
100 if (mode & MAY_READ)
101 s[i++] = 'r';
102 if (mode & MAY_WRITE)
103 s[i++] = 'w';
104 if (mode & MAY_EXEC)
105 s[i++] = 'x';
106 if (mode & MAY_APPEND)
107 s[i++] = 'a';
108 if (mode & MAY_TRANSMUTE)
109 s[i++] = 't';
110 if (mode & MAY_LOCK)
111 s[i++] = 'l';
112 if (i == 0)
113 s[i++] = '-';
114 s[i] = '\0';
115}
116#endif
117
118#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200119static int smk_bu_note(char *note, struct smack_known *sskp,
120 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700121{
122 char acc[SMK_NUM_ACCESS_TYPE + 1];
123
124 if (rc <= 0)
125 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700126 if (rc > SMACK_UNCONFINED_OBJECT)
127 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700128
129 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200131 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700132 return 0;
133}
134#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200135#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700136#endif
137
138#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200139static int smk_bu_current(char *note, struct smack_known *oskp,
140 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700141{
142 struct task_smack *tsp = current_security();
143 char acc[SMK_NUM_ACCESS_TYPE + 1];
144
145 if (rc <= 0)
146 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700147 if (rc > SMACK_UNCONFINED_OBJECT)
148 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700149
150 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200152 tsp->smk_task->smk_known, oskp->smk_known,
153 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700154 return 0;
155}
156#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200157#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700158#endif
159
160#ifdef CONFIG_SECURITY_SMACK_BRINGUP
161static int smk_bu_task(struct task_struct *otp, int mode, int rc)
162{
163 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300164 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700165 char acc[SMK_NUM_ACCESS_TYPE + 1];
166
167 if (rc <= 0)
168 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700169 if (rc > SMACK_UNCONFINED_OBJECT)
170 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700171
172 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300174 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700175 current->comm, otp->comm);
176 return 0;
177}
178#else
179#define smk_bu_task(otp, mode, RC) (RC)
180#endif
181
182#ifdef CONFIG_SECURITY_SMACK_BRINGUP
183static int smk_bu_inode(struct inode *inode, int mode, int rc)
184{
185 struct task_smack *tsp = current_security();
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700186 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700187 char acc[SMK_NUM_ACCESS_TYPE + 1];
188
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700189 if (isp->smk_flags & SMK_INODE_IMPURE)
190 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
191 inode->i_sb->s_id, inode->i_ino, current->comm);
192
Casey Schauflerd166c802014-08-27 14:51:27 -0700193 if (rc <= 0)
194 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700195 if (rc > SMACK_UNCONFINED_OBJECT)
196 rc = 0;
197 if (rc == SMACK_UNCONFINED_SUBJECT &&
198 (mode & (MAY_WRITE | MAY_APPEND)))
199 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700200
201 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700202
203 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
204 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700205 inode->i_sb->s_id, inode->i_ino, current->comm);
206 return 0;
207}
208#else
209#define smk_bu_inode(inode, mode, RC) (RC)
210#endif
211
212#ifdef CONFIG_SECURITY_SMACK_BRINGUP
213static int smk_bu_file(struct file *file, int mode, int rc)
214{
215 struct task_smack *tsp = current_security();
216 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800217 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700218 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700219 char acc[SMK_NUM_ACCESS_TYPE + 1];
220
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700221 if (isp->smk_flags & SMK_INODE_IMPURE)
222 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
223 inode->i_sb->s_id, inode->i_ino, current->comm);
224
Casey Schauflerd166c802014-08-27 14:51:27 -0700225 if (rc <= 0)
226 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700227 if (rc > SMACK_UNCONFINED_OBJECT)
228 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700229
230 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700231 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800232 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400233 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700234 current->comm);
235 return 0;
236}
237#else
238#define smk_bu_file(file, mode, RC) (RC)
239#endif
240
241#ifdef CONFIG_SECURITY_SMACK_BRINGUP
242static int smk_bu_credfile(const struct cred *cred, struct file *file,
243 int mode, int rc)
244{
245 struct task_smack *tsp = cred->security;
246 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500247 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700248 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700249 char acc[SMK_NUM_ACCESS_TYPE + 1];
250
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700251 if (isp->smk_flags & SMK_INODE_IMPURE)
252 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
253 inode->i_sb->s_id, inode->i_ino, current->comm);
254
Casey Schauflerd166c802014-08-27 14:51:27 -0700255 if (rc <= 0)
256 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700257 if (rc > SMACK_UNCONFINED_OBJECT)
258 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700259
260 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700261 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200262 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400263 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700264 current->comm);
265 return 0;
266}
267#else
268#define smk_bu_credfile(cred, file, mode, RC) (RC)
269#endif
270
Casey Schauflere114e472008-02-04 22:29:50 -0800271/**
272 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100273 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800274 * @ip: a pointer to the inode
275 * @dp: a pointer to the dentry
276 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200277 * Returns a pointer to the master list entry for the Smack label,
278 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800279 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700280static struct smack_known *smk_fetch(const char *name, struct inode *ip,
281 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800282{
283 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700284 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700285 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800286
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200287 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200288 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800289
Casey Schauflerf7112e62012-05-06 15:22:02 -0700290 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
291 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200292 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800293
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200294 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200295 if (rc < 0)
296 skp = ERR_PTR(rc);
297 else if (rc == 0)
298 skp = NULL;
299 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700300 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700301
302 kfree(buffer);
303
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700304 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800305}
306
307/**
308 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200309 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800310 *
311 * Returns the new blob or NULL if there's no memory available
312 */
Casey Schaufler1eddfe82015-07-30 14:35:14 -0700313static struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800314{
315 struct inode_smack *isp;
316
Rohit1a5b4722014-10-15 17:40:41 +0530317 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800318 if (isp == NULL)
319 return NULL;
320
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200321 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800322 isp->smk_flags = 0;
323 mutex_init(&isp->smk_lock);
324
325 return isp;
326}
327
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800328/**
329 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100330 * @task: a pointer to the Smack label for the running task
331 * @forked: a pointer to the Smack label for the forked task
332 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800333 *
334 * Returns the new blob or NULL if there's no memory available
335 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700336static struct task_smack *new_task_smack(struct smack_known *task,
337 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800338{
339 struct task_smack *tsp;
340
341 tsp = kzalloc(sizeof(struct task_smack), gfp);
342 if (tsp == NULL)
343 return NULL;
344
345 tsp->smk_task = task;
346 tsp->smk_forked = forked;
347 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200348 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800349 mutex_init(&tsp->smk_rules_lock);
350
351 return tsp;
352}
353
354/**
355 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100356 * @nhead: new rules header pointer
357 * @ohead: old rules header pointer
358 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800359 *
360 * Returns 0 on success, -ENOMEM on error
361 */
362static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
363 gfp_t gfp)
364{
365 struct smack_rule *nrp;
366 struct smack_rule *orp;
367 int rc = 0;
368
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800369 list_for_each_entry_rcu(orp, ohead, list) {
370 nrp = kzalloc(sizeof(struct smack_rule), gfp);
371 if (nrp == NULL) {
372 rc = -ENOMEM;
373 break;
374 }
375 *nrp = *orp;
376 list_add_rcu(&nrp->list, nhead);
377 }
378 return rc;
379}
380
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100381/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200382 * smk_copy_relabel - copy smk_relabel labels list
383 * @nhead: new rules header pointer
384 * @ohead: old rules header pointer
385 * @gfp: type of the memory for the allocation
386 *
387 * Returns 0 on success, -ENOMEM on error
388 */
389static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
390 gfp_t gfp)
391{
392 struct smack_known_list_elem *nklep;
393 struct smack_known_list_elem *oklep;
394
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200395 list_for_each_entry(oklep, ohead, list) {
396 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
397 if (nklep == NULL) {
398 smk_destroy_label_list(nhead);
399 return -ENOMEM;
400 }
401 nklep->smk_label = oklep->smk_label;
402 list_add(&nklep->list, nhead);
403 }
404
405 return 0;
406}
407
408/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100409 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
410 * @mode - input mode in form of PTRACE_MODE_*
411 *
412 * Returns a converted MAY_* mode usable by smack rules
413 */
414static inline unsigned int smk_ptrace_mode(unsigned int mode)
415{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800416 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100417 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800418 if (mode & PTRACE_MODE_READ)
419 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100420
421 return 0;
422}
423
424/**
425 * smk_ptrace_rule_check - helper for ptrace access
426 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200427 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100428 * @mode: ptrace attachment mode (PTRACE_MODE_*)
429 * @func: name of the function that called us, used for audit
430 *
431 * Returns 0 on access granted, -error on error
432 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200433static int smk_ptrace_rule_check(struct task_struct *tracer,
434 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100435 unsigned int mode, const char *func)
436{
437 int rc;
438 struct smk_audit_info ad, *saip = NULL;
439 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200440 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700441 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100442
443 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
444 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
445 smk_ad_setfield_u_tsk(&ad, tracer);
446 saip = &ad;
447 }
448
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300449 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700450 tracercred = __task_cred(tracer);
451 tsp = tracercred->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200452 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100453
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100454 if ((mode & PTRACE_MODE_ATTACH) &&
455 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
456 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200457 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100458 rc = 0;
459 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
460 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700461 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100462 rc = 0;
463 else
464 rc = -EACCES;
465
466 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200467 smack_log(tracer_known->smk_known,
468 tracee_known->smk_known,
469 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100470
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300471 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100472 return rc;
473 }
474
475 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200476 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300477
478 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100479 return rc;
480}
481
Casey Schauflere114e472008-02-04 22:29:50 -0800482/*
483 * LSM hooks.
484 * We he, that is fun!
485 */
486
487/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000488 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800489 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100490 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800491 *
492 * Returns 0 if access is OK, an error code otherwise
493 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100494 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800495 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000496static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800497{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700498 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800499
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300500 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200501
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700502 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100503}
Casey Schauflere114e472008-02-04 22:29:50 -0800504
David Howells5cd9c582008-08-14 11:37:28 +0100505/**
506 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
507 * @ptp: parent task pointer
508 *
509 * Returns 0 if access is OK, an error code otherwise
510 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100511 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100512 */
513static int smack_ptrace_traceme(struct task_struct *ptp)
514{
515 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700516 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100517
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100518 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200519
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200520 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800521 return rc;
522}
523
524/**
525 * smack_syslog - Smack approval on syslog
526 * @type: message type
527 *
Casey Schauflere114e472008-02-04 22:29:50 -0800528 * Returns 0 on success, error code otherwise.
529 */
Eric Paris12b30522010-11-15 18:36:29 -0500530static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800531{
Eric Paris12b30522010-11-15 18:36:29 -0500532 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700533 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800534
Casey Schaufler1880eff2012-06-05 15:28:30 -0700535 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800536 return 0;
537
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800538 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800539 rc = -EACCES;
540
541 return rc;
542}
543
544
545/*
546 * Superblock Hooks.
547 */
548
549/**
550 * smack_sb_alloc_security - allocate a superblock blob
551 * @sb: the superblock getting the blob
552 *
553 * Returns 0 on success or -ENOMEM on error.
554 */
555static int smack_sb_alloc_security(struct super_block *sb)
556{
557 struct superblock_smack *sbsp;
558
559 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
560
561 if (sbsp == NULL)
562 return -ENOMEM;
563
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200564 sbsp->smk_root = &smack_known_floor;
565 sbsp->smk_default = &smack_known_floor;
566 sbsp->smk_floor = &smack_known_floor;
567 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700568 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500569 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700570 */
Casey Schauflere114e472008-02-04 22:29:50 -0800571 sb->s_security = sbsp;
572
573 return 0;
574}
575
576/**
577 * smack_sb_free_security - free a superblock blob
578 * @sb: the superblock getting the blob
579 *
580 */
581static void smack_sb_free_security(struct super_block *sb)
582{
583 kfree(sb->s_security);
584 sb->s_security = NULL;
585}
586
Al Viro12085b12018-12-13 15:18:05 -0500587struct smack_mnt_opts {
588 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
589};
590
Al Viro204cc0c2018-12-13 13:41:47 -0500591static void smack_free_mnt_opts(void *mnt_opts)
592{
Al Viro12085b12018-12-13 15:18:05 -0500593 struct smack_mnt_opts *opts = mnt_opts;
594 kfree(opts->fsdefault);
595 kfree(opts->fsfloor);
596 kfree(opts->fshat);
597 kfree(opts->fsroot);
598 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500599 kfree(opts);
600}
601
Casey Schauflere114e472008-02-04 22:29:50 -0800602/**
603 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800604 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800605 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800606 *
607 * Returns 0 on success or -ENOMEM on error.
608 *
609 * Copy the Smack specific mount options out of the mount
610 * options list.
611 */
Eric Parise0007522008-03-05 10:31:54 -0500612static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800613{
614 char *cp, *commap, *otheropts, *dp;
615
Casey Schauflere114e472008-02-04 22:29:50 -0800616 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
617 if (otheropts == NULL)
618 return -ENOMEM;
619
620 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
621 if (strstr(cp, SMK_FSDEFAULT) == cp)
622 dp = smackopts;
623 else if (strstr(cp, SMK_FSFLOOR) == cp)
624 dp = smackopts;
625 else if (strstr(cp, SMK_FSHAT) == cp)
626 dp = smackopts;
627 else if (strstr(cp, SMK_FSROOT) == cp)
628 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700629 else if (strstr(cp, SMK_FSTRANS) == cp)
630 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800631 else
632 dp = otheropts;
633
634 commap = strchr(cp, ',');
635 if (commap != NULL)
636 *commap = '\0';
637
638 if (*dp != '\0')
639 strcat(dp, ",");
640 strcat(dp, cp);
641 }
642
643 strcpy(orig, otheropts);
644 free_page((unsigned long)otheropts);
645
646 return 0;
647}
648
Al Viro55c0e5b2018-12-16 01:09:45 -0500649static int smack_add_opt(int token, const char *s, void **mnt_opts)
650{
651 struct smack_mnt_opts *opts = *mnt_opts;
652
653 if (!opts) {
654 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
655 if (!opts)
656 return -ENOMEM;
657 *mnt_opts = opts;
658 }
659
660 if (!s)
661 return -ENOMEM;
662
663 switch (token) {
664 case Opt_fsdefault:
665 if (opts->fsdefault)
666 goto out_opt_err;
667 opts->fsdefault = s;
668 break;
669 case Opt_fsfloor:
670 if (opts->fsfloor)
671 goto out_opt_err;
672 opts->fsfloor = s;
673 break;
674 case Opt_fshat:
675 if (opts->fshat)
676 goto out_opt_err;
677 opts->fshat = s;
678 break;
679 case Opt_fsroot:
680 if (opts->fsroot)
681 goto out_opt_err;
682 opts->fsroot = s;
683 break;
684 case Opt_fstransmute:
685 if (opts->fstransmute)
686 goto out_opt_err;
687 opts->fstransmute = s;
688 break;
689 }
690 return 0;
691
692out_opt_err:
693 pr_warn("Smack: duplicate mount options\n");
694 return -EINVAL;
695}
696
Casey Schauflere114e472008-02-04 22:29:50 -0800697/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530698 * smack_parse_opts_str - parse Smack specific mount options
699 * @options: mount options string
700 * @opts: where to store converted mount opts
701 *
702 * Returns 0 on success or -ENOMEM on error.
703 *
704 * converts Smack specific mount options to generic security option format
705 */
706static int smack_parse_opts_str(char *options,
Al Viro204cc0c2018-12-13 13:41:47 -0500707 void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530708{
Al Viroc3300aa2018-12-16 01:52:24 -0500709 char *from = options;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530710
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530711 if (!options)
712 return 0;
713
Al Viroc3300aa2018-12-16 01:52:24 -0500714 while (1) {
715 char *next = strchr(from, ',');
716 int token, len, rc;
717 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530718
Al Viroc3300aa2018-12-16 01:52:24 -0500719 if (next)
720 len = next - from;
721 else
722 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530723
Al Viroc3300aa2018-12-16 01:52:24 -0500724 token = match_opt_prefix(from, len, &arg);
725 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
Al Viro55c0e5b2018-12-16 01:09:45 -0500726 rc = smack_add_opt(token, arg, mnt_opts);
727 if (unlikely(rc)) {
728 kfree(arg);
729 if (*mnt_opts)
730 smack_free_mnt_opts(*mnt_opts);
731 *mnt_opts = NULL;
732 return rc;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530733 }
Al Viroc3300aa2018-12-16 01:52:24 -0500734 if (!from[len])
735 break;
736 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530737 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530738 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530739}
740
Al Viro204cc0c2018-12-13 13:41:47 -0500741static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Al Viro5b400232018-12-12 20:13:29 -0500742{
743 char *s = (char *)get_zeroed_page(GFP_KERNEL);
744 int err;
745
746 if (!s)
747 return -ENOMEM;
748 err = smack_sb_copy_data(options, s);
749 if (!err)
Al Viro204cc0c2018-12-13 13:41:47 -0500750 err = smack_parse_opts_str(s, mnt_opts);
Al Viro5b400232018-12-12 20:13:29 -0500751 free_page((unsigned long)s);
752 return err;
753}
754
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530755/**
756 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800757 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530758 * @opts: Smack mount options
759 * @kern_flags: mount option from kernel space or user space
760 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800761 *
762 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530763 *
764 * Allow filesystems with binary mount data to explicitly set Smack mount
765 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800766 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530767static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500768 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530769 unsigned long kern_flags,
770 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800771{
772 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000773 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800774 struct superblock_smack *sp = sb->s_security;
775 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800776 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500777 struct smack_mnt_opts *opts = mnt_opts;
778 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800779
Seth Forshee9f50eda2015-09-23 15:16:06 -0500780 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800781 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700782
Himanshu Shukla2097f592016-11-10 16:19:52 +0530783 if (!smack_privileged(CAP_MAC_ADMIN)) {
784 /*
785 * Unprivileged mounts don't get to specify Smack values.
786 */
Al Viro12085b12018-12-13 15:18:05 -0500787 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530788 return -EPERM;
789 /*
790 * Unprivileged mounts get root and default from the caller.
791 */
792 skp = smk_of_current();
793 sp->smk_root = skp;
794 sp->smk_default = skp;
795 /*
796 * For a handful of fs types with no user-controlled
797 * backing store it's okay to trust security labels
798 * in the filesystem. The rest are untrusted.
799 */
800 if (sb->s_user_ns != &init_user_ns &&
801 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
802 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500803 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530804 sp->smk_flags |= SMK_SB_UNTRUSTED;
805 }
806 }
807
Seth Forshee9f50eda2015-09-23 15:16:06 -0500808 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800809
Al Viro12085b12018-12-13 15:18:05 -0500810 if (opts) {
811 if (opts->fsdefault) {
812 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200813 if (IS_ERR(skp))
814 return PTR_ERR(skp);
815 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500816 }
817 if (opts->fsfloor) {
818 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530819 if (IS_ERR(skp))
820 return PTR_ERR(skp);
821 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500822 }
823 if (opts->fshat) {
824 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530825 if (IS_ERR(skp))
826 return PTR_ERR(skp);
827 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500828 }
829 if (opts->fsroot) {
830 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200831 if (IS_ERR(skp))
832 return PTR_ERR(skp);
833 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500834 }
835 if (opts->fstransmute) {
836 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200837 if (IS_ERR(skp))
838 return PTR_ERR(skp);
839 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500840 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800841 }
842 }
843
844 /*
845 * Initialize the root inode.
846 */
847 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100848 if (isp == NULL) {
849 isp = new_inode_smack(sp->smk_root);
850 if (isp == NULL)
851 return -ENOMEM;
852 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700853 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800854 isp->smk_inode = sp->smk_root;
855
Casey Schauflere830b392013-05-22 18:43:07 -0700856 if (transmute)
857 isp->smk_flags |= SMK_INODE_TRANSMUTE;
858
Casey Schauflere114e472008-02-04 22:29:50 -0800859 return 0;
860}
861
862/**
863 * smack_sb_statfs - Smack check on statfs
864 * @dentry: identifies the file system in question
865 *
866 * Returns 0 if current can read the floor of the filesystem,
867 * and error code otherwise
868 */
869static int smack_sb_statfs(struct dentry *dentry)
870{
871 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200872 int rc;
873 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800874
Eric Parisa2694342011-04-25 13:10:27 -0400875 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200876 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
877
878 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700879 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200880 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800881}
882
Casey Schauflere114e472008-02-04 22:29:50 -0800883/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800884 * BPRM hooks
885 */
886
Casey Schauflerce8a4322011-09-29 18:21:01 -0700887/**
888 * smack_bprm_set_creds - set creds for exec
889 * @bprm: the exec information
890 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100891 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700892 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800893static int smack_bprm_set_creds(struct linux_binprm *bprm)
894{
Al Viro496ad9a2013-01-23 17:07:38 -0500895 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300896 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800897 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500898 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800899 int rc;
900
Kees Cookddb4a142017-07-18 15:25:23 -0700901 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800902 return 0;
903
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300904 isp = inode->i_security;
905 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800906 return 0;
907
Seth Forshee809c02e2016-04-26 14:36:22 -0500908 sbsp = inode->i_sb->s_security;
909 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
910 isp->smk_task != sbsp->smk_root)
911 return 0;
912
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300913 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100914 struct task_struct *tracer;
915 rc = 0;
916
917 rcu_read_lock();
918 tracer = ptrace_parent(current);
919 if (likely(tracer != NULL))
920 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200921 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100922 PTRACE_MODE_ATTACH,
923 __func__);
924 rcu_read_unlock();
925
926 if (rc != 0)
927 return rc;
928 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300929 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800930
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300931 bsp->smk_task = isp->smk_task;
932 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800933
Kees Cookccbb6e12017-07-18 15:25:26 -0700934 /* Decide if this is a secure exec. */
935 if (bsp->smk_task != bsp->smk_forked)
936 bprm->secureexec = 1;
937
Casey Schaufler676dac42010-12-02 06:43:39 -0800938 return 0;
939}
940
941/*
Casey Schauflere114e472008-02-04 22:29:50 -0800942 * Inode hooks
943 */
944
945/**
946 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800947 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800948 *
949 * Returns 0 if it gets a blob, -ENOMEM otherwise
950 */
951static int smack_inode_alloc_security(struct inode *inode)
952{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700953 struct smack_known *skp = smk_of_current();
954
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200955 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800956 if (inode->i_security == NULL)
957 return -ENOMEM;
958 return 0;
959}
960
961/**
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530962 * smack_inode_free_rcu - Free inode_smack blob from cache
963 * @head: the rcu_head for getting inode_smack pointer
964 *
965 * Call back function called from call_rcu() to free
966 * the i_security blob pointer in inode
967 */
968static void smack_inode_free_rcu(struct rcu_head *head)
969{
970 struct inode_smack *issp;
971
972 issp = container_of(head, struct inode_smack, smk_rcu);
973 kmem_cache_free(smack_inode_cache, issp);
974}
975
976/**
977 * smack_inode_free_security - free an inode blob using call_rcu()
Randy Dunlap251a2a92009-02-18 11:42:33 -0800978 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800979 *
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530980 * Clears the blob pointer in inode using RCU
Casey Schauflere114e472008-02-04 22:29:50 -0800981 */
982static void smack_inode_free_security(struct inode *inode)
983{
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530984 struct inode_smack *issp = inode->i_security;
985
986 /*
987 * The inode may still be referenced in a path walk and
988 * a call to smack_inode_permission() can be made
989 * after smack_inode_free_security() is called.
990 * To avoid race condition free the i_security via RCU
991 * and leave the current inode->i_security pointer intact.
992 * The inode will be freed after the RCU grace period too.
993 */
994 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
Casey Schauflere114e472008-02-04 22:29:50 -0800995}
996
997/**
998 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200999 * @inode: the newly created inode
1000 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -05001001 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001002 * @name: where to put the attribute name
1003 * @value: where to put the attribute value
1004 * @len: where to put the length of the attribute
1005 *
1006 * Returns 0 if it all works out, -ENOMEM if there's no memory
1007 */
1008static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09001009 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05001010 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -08001011{
Casey Schaufler2267b132012-03-13 19:14:19 -07001012 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001013 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001014 struct smack_known *isp = smk_of_inode(inode);
1015 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001016 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08001017
Tetsuo Handa95489062013-07-25 05:44:02 +09001018 if (name)
1019 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -08001020
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001021 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001022 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001023 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1024 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001025 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001026
1027 /*
1028 * If the access rule allows transmutation and
1029 * the directory requests transmutation then
1030 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001031 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001032 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001033 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001034 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001035 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001036 issp->smk_flags |= SMK_INODE_CHANGED;
1037 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001038
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001039 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001040 if (*value == NULL)
1041 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001042
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001043 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001044 }
Casey Schauflere114e472008-02-04 22:29:50 -08001045
1046 return 0;
1047}
1048
1049/**
1050 * smack_inode_link - Smack check on link
1051 * @old_dentry: the existing object
1052 * @dir: unused
1053 * @new_dentry: the new object
1054 *
1055 * Returns 0 if access is permitted, an error code otherwise
1056 */
1057static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1058 struct dentry *new_dentry)
1059{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001060 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001061 struct smk_audit_info ad;
1062 int rc;
1063
Eric Parisa2694342011-04-25 13:10:27 -04001064 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001065 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001066
David Howellsc6f493d2015-03-17 22:26:22 +00001067 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001068 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001069 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001070
David Howells88025652015-01-29 12:02:32 +00001071 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001072 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001073 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1074 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001075 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001076 }
1077
1078 return rc;
1079}
1080
1081/**
1082 * smack_inode_unlink - Smack check on inode deletion
1083 * @dir: containing directory object
1084 * @dentry: file to unlink
1085 *
1086 * Returns 0 if current can write the containing directory
1087 * and the object, error code otherwise
1088 */
1089static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1090{
David Howellsc6f493d2015-03-17 22:26:22 +00001091 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001092 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001093 int rc;
1094
Eric Parisa2694342011-04-25 13:10:27 -04001095 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001096 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1097
Casey Schauflere114e472008-02-04 22:29:50 -08001098 /*
1099 * You need write access to the thing you're unlinking
1100 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001101 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001102 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001104 /*
1105 * You also need write access to the containing directory
1106 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001107 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001108 smk_ad_setfield_u_fs_inode(&ad, dir);
1109 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001110 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001111 }
Casey Schauflere114e472008-02-04 22:29:50 -08001112 return rc;
1113}
1114
1115/**
1116 * smack_inode_rmdir - Smack check on directory deletion
1117 * @dir: containing directory object
1118 * @dentry: directory to unlink
1119 *
1120 * Returns 0 if current can write the containing directory
1121 * and the directory, error code otherwise
1122 */
1123static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1124{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001125 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001126 int rc;
1127
Eric Parisa2694342011-04-25 13:10:27 -04001128 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001129 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1130
Casey Schauflere114e472008-02-04 22:29:50 -08001131 /*
1132 * You need write access to the thing you're removing
1133 */
David Howellsc6f493d2015-03-17 22:26:22 +00001134 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1135 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001136 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001137 /*
1138 * You also need write access to the containing directory
1139 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001140 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001141 smk_ad_setfield_u_fs_inode(&ad, dir);
1142 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001143 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001144 }
Casey Schauflere114e472008-02-04 22:29:50 -08001145
1146 return rc;
1147}
1148
1149/**
1150 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001151 * @old_inode: unused
1152 * @old_dentry: the old object
1153 * @new_inode: unused
1154 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001155 *
1156 * Read and write access is required on both the old and
1157 * new directories.
1158 *
1159 * Returns 0 if access is permitted, an error code otherwise
1160 */
1161static int smack_inode_rename(struct inode *old_inode,
1162 struct dentry *old_dentry,
1163 struct inode *new_inode,
1164 struct dentry *new_dentry)
1165{
1166 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001167 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001168 struct smk_audit_info ad;
1169
Eric Parisa2694342011-04-25 13:10:27 -04001170 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001172
David Howellsc6f493d2015-03-17 22:26:22 +00001173 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001174 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001175 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001176
David Howells88025652015-01-29 12:02:32 +00001177 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001178 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001179 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1180 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001181 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001182 }
Casey Schauflere114e472008-02-04 22:29:50 -08001183 return rc;
1184}
1185
1186/**
1187 * smack_inode_permission - Smack version of permission()
1188 * @inode: the inode in question
1189 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001190 *
1191 * This is the important Smack hook.
1192 *
1193 * Returns 0 if access is permitted, -EACCES otherwise
1194 */
Al Viroe74f71e2011-06-20 19:38:15 -04001195static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001196{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001197 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001198 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001199 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001200 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001201
1202 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001203 /*
1204 * No permission to check. Existence test. Yup, it's there.
1205 */
1206 if (mask == 0)
1207 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001208
Seth Forshee9f50eda2015-09-23 15:16:06 -05001209 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1210 if (smk_of_inode(inode) != sbsp->smk_root)
1211 return -EACCES;
1212 }
1213
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001214 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001215 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001216 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001217 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001218 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001219 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1220 rc = smk_bu_inode(inode, mask, rc);
1221 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001222}
1223
1224/**
1225 * smack_inode_setattr - Smack check for setting attributes
1226 * @dentry: the object
1227 * @iattr: for the force flag
1228 *
1229 * Returns 0 if access is permitted, an error code otherwise
1230 */
1231static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1232{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001233 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001234 int rc;
1235
Casey Schauflere114e472008-02-04 22:29:50 -08001236 /*
1237 * Need to allow for clearing the setuid bit.
1238 */
1239 if (iattr->ia_valid & ATTR_FORCE)
1240 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001241 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001242 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001243
David Howellsc6f493d2015-03-17 22:26:22 +00001244 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1245 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001246 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001247}
1248
1249/**
1250 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001251 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001252 * @dentry: the object
1253 *
1254 * Returns 0 if access is permitted, an error code otherwise
1255 */
Al Viro3f7036a2015-03-08 19:28:30 -04001256static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001257{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001258 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001259 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001260 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001261
Eric Parisf48b7392011-04-25 12:54:27 -04001262 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001263 smk_ad_setfield_u_fs_path(&ad, *path);
1264 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1265 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001266 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001267}
1268
1269/**
1270 * smack_inode_setxattr - Smack check for setting xattrs
1271 * @dentry: the object
1272 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001273 * @value: value of the attribute
1274 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001275 * @flags: unused
1276 *
1277 * This protects the Smack attribute explicitly.
1278 *
1279 * Returns 0 if access is permitted, an error code otherwise
1280 */
David Howells8f0cfa52008-04-29 00:59:41 -07001281static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1282 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001283{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001284 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001285 struct smack_known *skp;
1286 int check_priv = 0;
1287 int check_import = 0;
1288 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001289 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001290
Casey Schaufler19760ad2013-12-16 16:27:26 -08001291 /*
1292 * Check label validity here so import won't fail in post_setxattr
1293 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001294 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1295 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001296 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1297 check_priv = 1;
1298 check_import = 1;
1299 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1300 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1301 check_priv = 1;
1302 check_import = 1;
1303 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001304 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001305 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001306 if (size != TRANS_TRUE_SIZE ||
1307 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1308 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001309 } else
1310 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1311
Casey Schaufler19760ad2013-12-16 16:27:26 -08001312 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1313 rc = -EPERM;
1314
1315 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001316 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001317 if (IS_ERR(skp))
1318 rc = PTR_ERR(skp);
1319 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001320 (skp == &smack_known_star || skp == &smack_known_web)))
1321 rc = -EINVAL;
1322 }
1323
Eric Parisa2694342011-04-25 13:10:27 -04001324 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001325 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1326
Casey Schauflerd166c802014-08-27 14:51:27 -07001327 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001328 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1329 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001330 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001331
1332 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001333}
1334
1335/**
1336 * smack_inode_post_setxattr - Apply the Smack update approved above
1337 * @dentry: object
1338 * @name: attribute name
1339 * @value: attribute value
1340 * @size: attribute size
1341 * @flags: unused
1342 *
1343 * Set the pointer in the inode blob to the entry found
1344 * in the master label list.
1345 */
David Howells8f0cfa52008-04-29 00:59:41 -07001346static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1347 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001348{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001349 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001350 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001351
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001352 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1353 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1354 return;
1355 }
1356
Casey Schaufler676dac42010-12-02 06:43:39 -08001357 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001358 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001359 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001360 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001361 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001362 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001363 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001364 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001365 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001366 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001367 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001368 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001369 }
Casey Schauflere114e472008-02-04 22:29:50 -08001370
1371 return;
1372}
1373
Casey Schauflerce8a4322011-09-29 18:21:01 -07001374/**
Casey Schauflere114e472008-02-04 22:29:50 -08001375 * smack_inode_getxattr - Smack check on getxattr
1376 * @dentry: the object
1377 * @name: unused
1378 *
1379 * Returns 0 if access is permitted, an error code otherwise
1380 */
David Howells8f0cfa52008-04-29 00:59:41 -07001381static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001382{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001383 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001384 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001385
Eric Parisa2694342011-04-25 13:10:27 -04001386 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001387 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1388
David Howellsc6f493d2015-03-17 22:26:22 +00001389 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1390 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001391 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001392}
1393
Casey Schauflerce8a4322011-09-29 18:21:01 -07001394/**
Casey Schauflere114e472008-02-04 22:29:50 -08001395 * smack_inode_removexattr - Smack check on removexattr
1396 * @dentry: the object
1397 * @name: name of the attribute
1398 *
1399 * Removing the Smack attribute requires CAP_MAC_ADMIN
1400 *
1401 * Returns 0 if access is permitted, an error code otherwise
1402 */
David Howells8f0cfa52008-04-29 00:59:41 -07001403static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001404{
Casey Schaufler676dac42010-12-02 06:43:39 -08001405 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001406 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001407 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001408
Casey Schauflerbcdca222008-02-23 15:24:04 -08001409 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1410 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001411 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001412 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001413 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301414 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001415 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001416 rc = -EPERM;
1417 } else
1418 rc = cap_inode_removexattr(dentry, name);
1419
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001420 if (rc != 0)
1421 return rc;
1422
Eric Parisa2694342011-04-25 13:10:27 -04001423 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001424 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001425
David Howellsc6f493d2015-03-17 22:26:22 +00001426 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1427 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001428 if (rc != 0)
1429 return rc;
1430
David Howellsc6f493d2015-03-17 22:26:22 +00001431 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001432 /*
1433 * Don't do anything special for these.
1434 * XATTR_NAME_SMACKIPIN
1435 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001436 */
José Bollo80124952016-01-12 21:23:40 +01001437 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001438 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001439 struct superblock_smack *sbsp = sbp->s_security;
1440
1441 isp->smk_inode = sbsp->smk_default;
1442 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001443 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001444 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001445 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001446 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1447 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001448
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001449 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001450}
1451
1452/**
1453 * smack_inode_getsecurity - get smack xattrs
1454 * @inode: the object
1455 * @name: attribute name
1456 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001457 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001458 *
1459 * Returns the size of the attribute or an error code
1460 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001461static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001462 const char *name, void **buffer,
1463 bool alloc)
1464{
1465 struct socket_smack *ssp;
1466 struct socket *sock;
1467 struct super_block *sbp;
1468 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001469 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001470
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001471 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001472 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001473 else {
1474 /*
1475 * The rest of the Smack xattrs are only on sockets.
1476 */
1477 sbp = ip->i_sb;
1478 if (sbp->s_magic != SOCKFS_MAGIC)
1479 return -EOPNOTSUPP;
1480
1481 sock = SOCKET_I(ip);
1482 if (sock == NULL || sock->sk == NULL)
1483 return -EOPNOTSUPP;
1484
1485 ssp = sock->sk->sk_security;
1486
1487 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1488 isp = ssp->smk_in;
1489 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1490 isp = ssp->smk_out;
1491 else
1492 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001493 }
1494
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001495 if (alloc) {
1496 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1497 if (*buffer == NULL)
1498 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001499 }
1500
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001501 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001502}
1503
1504
1505/**
1506 * smack_inode_listsecurity - list the Smack attributes
1507 * @inode: the object
1508 * @buffer: where they go
1509 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001510 */
1511static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1512 size_t buffer_size)
1513{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001514 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001515
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001516 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001517 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001518
1519 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001520}
1521
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001522/**
1523 * smack_inode_getsecid - Extract inode's security id
1524 * @inode: inode to extract the info from
1525 * @secid: where result will be saved
1526 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001527static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001528{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001529 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001530
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001531 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001532}
1533
Casey Schauflere114e472008-02-04 22:29:50 -08001534/*
1535 * File Hooks
1536 */
1537
Casey Schaufler491a0b02016-01-26 15:08:35 -08001538/*
1539 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001540 *
1541 * Should access checks be done on each read or write?
1542 * UNICOS and SELinux say yes.
1543 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1544 *
1545 * I'll say no for now. Smack does not do the frequent
1546 * label changing that SELinux does.
1547 */
Casey Schauflere114e472008-02-04 22:29:50 -08001548
1549/**
1550 * smack_file_alloc_security - assign a file security blob
1551 * @file: the object
1552 *
1553 * The security blob for a file is a pointer to the master
1554 * label list, so no allocation is done.
1555 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001556 * f_security is the owner security information. It
1557 * isn't used on file access checks, it's for send_sigio.
1558 *
Casey Schauflere114e472008-02-04 22:29:50 -08001559 * Returns 0
1560 */
1561static int smack_file_alloc_security(struct file *file)
1562{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001563 struct smack_known *skp = smk_of_current();
1564
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001565 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001566 return 0;
1567}
1568
1569/**
1570 * smack_file_free_security - clear a file security blob
1571 * @file: the object
1572 *
1573 * The security blob for a file is a pointer to the master
1574 * label list, so no memory is freed.
1575 */
1576static void smack_file_free_security(struct file *file)
1577{
1578 file->f_security = NULL;
1579}
1580
1581/**
1582 * smack_file_ioctl - Smack check on ioctls
1583 * @file: the object
1584 * @cmd: what to do
1585 * @arg: unused
1586 *
1587 * Relies heavily on the correct use of the ioctl command conventions.
1588 *
1589 * Returns 0 if allowed, error code otherwise
1590 */
1591static int smack_file_ioctl(struct file *file, unsigned int cmd,
1592 unsigned long arg)
1593{
1594 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001595 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001596 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001597
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001598 if (unlikely(IS_PRIVATE(inode)))
1599 return 0;
1600
Eric Parisf48b7392011-04-25 12:54:27 -04001601 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001602 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001603
Casey Schauflerd166c802014-08-27 14:51:27 -07001604 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001605 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001606 rc = smk_bu_file(file, MAY_WRITE, rc);
1607 }
Casey Schauflere114e472008-02-04 22:29:50 -08001608
Casey Schauflerd166c802014-08-27 14:51:27 -07001609 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001610 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001611 rc = smk_bu_file(file, MAY_READ, rc);
1612 }
Casey Schauflere114e472008-02-04 22:29:50 -08001613
1614 return rc;
1615}
1616
1617/**
1618 * smack_file_lock - Smack check on file locking
1619 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001620 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001621 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001622 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001623 */
1624static int smack_file_lock(struct file *file, unsigned int cmd)
1625{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001626 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001627 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001628 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001629
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001630 if (unlikely(IS_PRIVATE(inode)))
1631 return 0;
1632
Eric Paris92f42502011-04-25 13:15:55 -04001633 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1634 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001635 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001636 rc = smk_bu_file(file, MAY_LOCK, rc);
1637 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001638}
1639
1640/**
1641 * smack_file_fcntl - Smack check on fcntl
1642 * @file: the object
1643 * @cmd: what action to check
1644 * @arg: unused
1645 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001646 * Generally these operations are harmless.
1647 * File locking operations present an obvious mechanism
1648 * for passing information, so they require write access.
1649 *
Casey Schauflere114e472008-02-04 22:29:50 -08001650 * Returns 0 if current has access, error code otherwise
1651 */
1652static int smack_file_fcntl(struct file *file, unsigned int cmd,
1653 unsigned long arg)
1654{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001655 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001656 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001657 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001658
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001659 if (unlikely(IS_PRIVATE(inode)))
1660 return 0;
1661
Casey Schauflere114e472008-02-04 22:29:50 -08001662 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001663 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001664 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001665 case F_SETLK:
1666 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001667 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1668 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001669 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001670 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001671 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001672 case F_SETOWN:
1673 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001674 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1675 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001676 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001677 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001678 break;
1679 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001680 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001681 }
1682
1683 return rc;
1684}
1685
1686/**
Al Viroe5467852012-05-30 13:30:51 -04001687 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001688 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1689 * if mapping anonymous memory.
1690 * @file contains the file structure for file to map (may be NULL).
1691 * @reqprot contains the protection requested by the application.
1692 * @prot contains the protection that will be applied by the kernel.
1693 * @flags contains the operational flags.
1694 * Return 0 if permission is granted.
1695 */
Al Viroe5467852012-05-30 13:30:51 -04001696static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001697 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001698 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001699{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001700 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001701 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001702 struct smack_rule *srp;
1703 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001704 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001705 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001706 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001707 int may;
1708 int mmay;
1709 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001710 int rc;
1711
Al Viro496ad9a2013-01-23 17:07:38 -05001712 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001713 return 0;
1714
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001715 if (unlikely(IS_PRIVATE(file_inode(file))))
1716 return 0;
1717
Al Viro496ad9a2013-01-23 17:07:38 -05001718 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001719 if (isp->smk_mmap == NULL)
1720 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001721 sbsp = file_inode(file)->i_sb->s_security;
1722 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1723 isp->smk_mmap != sbsp->smk_root)
1724 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001725 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001726
1727 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001728 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001729 rc = 0;
1730
1731 rcu_read_lock();
1732 /*
1733 * For each Smack rule associated with the subject
1734 * label verify that the SMACK64MMAP also has access
1735 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001736 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001737 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001738 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001739 /*
1740 * Matching labels always allows access.
1741 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001742 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001743 continue;
1744 /*
1745 * If there is a matching local rule take
1746 * that into account as well.
1747 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001748 may = smk_access_entry(srp->smk_subject->smk_known,
1749 okp->smk_known,
1750 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001751 if (may == -ENOENT)
1752 may = srp->smk_access;
1753 else
1754 may &= srp->smk_access;
1755 /*
1756 * If may is zero the SMACK64MMAP subject can't
1757 * possibly have less access.
1758 */
1759 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001760 continue;
1761
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001762 /*
1763 * Fetch the global list entry.
1764 * If there isn't one a SMACK64MMAP subject
1765 * can't have as much access as current.
1766 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001767 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1768 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001769 if (mmay == -ENOENT) {
1770 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001771 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001772 }
1773 /*
1774 * If there is a local entry it modifies the
1775 * potential access, too.
1776 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001777 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1778 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001779 if (tmay != -ENOENT)
1780 mmay &= tmay;
1781
1782 /*
1783 * If there is any access available to current that is
1784 * not available to a SMACK64MMAP subject
1785 * deny access.
1786 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001787 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001788 rc = -EACCES;
1789 break;
1790 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001791 }
1792
1793 rcu_read_unlock();
1794
1795 return rc;
1796}
1797
1798/**
Casey Schauflere114e472008-02-04 22:29:50 -08001799 * smack_file_set_fowner - set the file security blob value
1800 * @file: object in question
1801 *
Casey Schauflere114e472008-02-04 22:29:50 -08001802 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001803static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001804{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001805 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001806}
1807
1808/**
1809 * smack_file_send_sigiotask - Smack on sigio
1810 * @tsk: The target task
1811 * @fown: the object the signal come from
1812 * @signum: unused
1813 *
1814 * Allow a privileged task to get signals even if it shouldn't
1815 *
1816 * Returns 0 if a subject with the object's smack could
1817 * write to the task, an error code otherwise.
1818 */
1819static int smack_file_send_sigiotask(struct task_struct *tsk,
1820 struct fown_struct *fown, int signum)
1821{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001822 struct smack_known *skp;
1823 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001824 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001825 struct file *file;
1826 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001827 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001828
1829 /*
1830 * struct fown_struct is never outside the context of a struct file
1831 */
1832 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001833
Etienne Bassetecfcc532009-04-08 20:40:06 +02001834 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001835 skp = file->f_security;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001836 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1837 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001838
1839 rcu_read_lock();
1840 tcred = __task_cred(tsk);
1841 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001842 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001843 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001844
1845 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1846 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001847 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001848 return rc;
1849}
1850
1851/**
1852 * smack_file_receive - Smack file receive check
1853 * @file: the object
1854 *
1855 * Returns 0 if current has access, error code otherwise
1856 */
1857static int smack_file_receive(struct file *file)
1858{
Casey Schauflerd166c802014-08-27 14:51:27 -07001859 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001860 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001861 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001862 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001863 struct socket *sock;
1864 struct task_smack *tsp;
1865 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001866
Seung-Woo Kim97775822015-04-17 15:25:04 +09001867 if (unlikely(IS_PRIVATE(inode)))
1868 return 0;
1869
Casey Schaufler4482a442013-12-30 17:37:45 -08001870 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001871 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001872
Casey Schaufler51d59af2017-05-31 08:53:42 -07001873 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001874 sock = SOCKET_I(inode);
1875 ssp = sock->sk->sk_security;
1876 tsp = current_security();
1877 /*
1878 * If the receiving process can't write to the
1879 * passed socket or if the passed socket can't
1880 * write to the receiving process don't accept
1881 * the passed socket.
1882 */
1883 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1884 rc = smk_bu_file(file, may, rc);
1885 if (rc < 0)
1886 return rc;
1887 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1888 rc = smk_bu_file(file, may, rc);
1889 return rc;
1890 }
Casey Schauflere114e472008-02-04 22:29:50 -08001891 /*
1892 * This code relies on bitmasks.
1893 */
1894 if (file->f_mode & FMODE_READ)
1895 may = MAY_READ;
1896 if (file->f_mode & FMODE_WRITE)
1897 may |= MAY_WRITE;
1898
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001899 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001900 rc = smk_bu_file(file, may, rc);
1901 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001902}
1903
Casey Schaufler531f1d42011-09-19 12:41:42 -07001904/**
Eric Paris83d49852012-04-04 13:45:40 -04001905 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001906 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001907 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001908 *
1909 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001910 * Allow the open only if the task has read access. There are
1911 * many read operations (e.g. fstat) that you can do with an
1912 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001913 *
1914 * Returns 0
1915 */
Al Viro94817692018-07-10 14:13:18 -04001916static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001917{
Al Viro94817692018-07-10 14:13:18 -04001918 struct task_smack *tsp = file->f_cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001919 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001920 struct smk_audit_info ad;
1921 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001922
Casey Schauflera6834c02014-04-21 11:10:26 -07001923 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1924 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301925 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001926 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001927
1928 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001929}
1930
Casey Schauflere114e472008-02-04 22:29:50 -08001931/*
1932 * Task hooks
1933 */
1934
1935/**
David Howellsee18d642009-09-02 09:14:21 +01001936 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1937 * @new: the new credentials
1938 * @gfp: the atomicity of any memory allocations
1939 *
1940 * Prepare a blank set of credentials for modification. This must allocate all
1941 * the memory the LSM module might require such that cred_transfer() can
1942 * complete without error.
1943 */
1944static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1945{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001946 struct task_smack *tsp;
1947
1948 tsp = new_task_smack(NULL, NULL, gfp);
1949 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001950 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001951
1952 cred->security = tsp;
1953
David Howellsee18d642009-09-02 09:14:21 +01001954 return 0;
1955}
1956
1957
1958/**
David Howellsf1752ee2008-11-14 10:39:17 +11001959 * smack_cred_free - "free" task-level security credentials
1960 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001961 *
Casey Schauflere114e472008-02-04 22:29:50 -08001962 */
David Howellsf1752ee2008-11-14 10:39:17 +11001963static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001964{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001965 struct task_smack *tsp = cred->security;
1966 struct smack_rule *rp;
1967 struct list_head *l;
1968 struct list_head *n;
1969
1970 if (tsp == NULL)
1971 return;
1972 cred->security = NULL;
1973
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001974 smk_destroy_label_list(&tsp->smk_relabel);
1975
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001976 list_for_each_safe(l, n, &tsp->smk_rules) {
1977 rp = list_entry(l, struct smack_rule, list);
1978 list_del(&rp->list);
1979 kfree(rp);
1980 }
1981 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001982}
1983
1984/**
David Howellsd84f4f92008-11-14 10:39:23 +11001985 * smack_cred_prepare - prepare new set of credentials for modification
1986 * @new: the new credentials
1987 * @old: the original credentials
1988 * @gfp: the atomicity of any memory allocations
1989 *
1990 * Prepare a new set of credentials for modification.
1991 */
1992static int smack_cred_prepare(struct cred *new, const struct cred *old,
1993 gfp_t gfp)
1994{
Casey Schaufler676dac42010-12-02 06:43:39 -08001995 struct task_smack *old_tsp = old->security;
1996 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001997 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001998
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001999 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002000 if (new_tsp == NULL)
2001 return -ENOMEM;
2002
Himanshu Shuklab437aba2016-11-10 16:17:02 +05302003 new->security = new_tsp;
2004
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002005 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2006 if (rc != 0)
2007 return rc;
2008
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002009 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2010 gfp);
2011 if (rc != 0)
2012 return rc;
2013
David Howellsd84f4f92008-11-14 10:39:23 +11002014 return 0;
2015}
2016
Randy Dunlap251a2a92009-02-18 11:42:33 -08002017/**
David Howellsee18d642009-09-02 09:14:21 +01002018 * smack_cred_transfer - Transfer the old credentials to the new credentials
2019 * @new: the new credentials
2020 * @old: the original credentials
2021 *
2022 * Fill in a set of blank credentials from another set of credentials.
2023 */
2024static void smack_cred_transfer(struct cred *new, const struct cred *old)
2025{
Casey Schaufler676dac42010-12-02 06:43:39 -08002026 struct task_smack *old_tsp = old->security;
2027 struct task_smack *new_tsp = new->security;
2028
2029 new_tsp->smk_task = old_tsp->smk_task;
2030 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002031 mutex_init(&new_tsp->smk_rules_lock);
2032 INIT_LIST_HEAD(&new_tsp->smk_rules);
2033
2034
2035 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01002036}
2037
2038/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08002039 * smack_cred_getsecid - get the secid corresponding to a creds structure
2040 * @c: the object creds
2041 * @secid: where to put the result
2042 *
2043 * Sets the secid to contain a u32 version of the smack label.
2044 */
2045static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2046{
2047 struct smack_known *skp;
2048
2049 rcu_read_lock();
2050 skp = smk_of_task(c->security);
2051 *secid = skp->smk_secid;
2052 rcu_read_unlock();
2053}
2054
2055/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002056 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002057 * @new: points to the set of credentials to be modified.
2058 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002059 *
2060 * Set the security data for a kernel service.
2061 */
2062static int smack_kernel_act_as(struct cred *new, u32 secid)
2063{
Casey Schaufler676dac42010-12-02 06:43:39 -08002064 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002065
Casey Schaufler152f91d2016-11-14 09:38:15 -08002066 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002067 return 0;
2068}
2069
2070/**
2071 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002072 * @new: points to the set of credentials to be modified
2073 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002074 *
2075 * Set the file creation context in a set of credentials to the same
2076 * as the objective context of the specified inode
2077 */
2078static int smack_kernel_create_files_as(struct cred *new,
2079 struct inode *inode)
2080{
2081 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002082 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002083
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002084 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002085 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002086 return 0;
2087}
2088
2089/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002090 * smk_curacc_on_task - helper to log task related access
2091 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002092 * @access: the access requested
2093 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002094 *
2095 * Return 0 if access is permitted
2096 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002097static int smk_curacc_on_task(struct task_struct *p, int access,
2098 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002099{
2100 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002101 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002102 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002103
Casey Schaufler531f1d42011-09-19 12:41:42 -07002104 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002105 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002106 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002107 rc = smk_bu_task(p, access, rc);
2108 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002109}
2110
2111/**
Casey Schauflere114e472008-02-04 22:29:50 -08002112 * smack_task_setpgid - Smack check on setting pgid
2113 * @p: the task object
2114 * @pgid: unused
2115 *
2116 * Return 0 if write access is permitted
2117 */
2118static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2119{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002120 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002121}
2122
2123/**
2124 * smack_task_getpgid - Smack access check for getpgid
2125 * @p: the object task
2126 *
2127 * Returns 0 if current can read the object task, error code otherwise
2128 */
2129static int smack_task_getpgid(struct task_struct *p)
2130{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002131 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002132}
2133
2134/**
2135 * smack_task_getsid - Smack access check for getsid
2136 * @p: the object task
2137 *
2138 * Returns 0 if current can read the object task, error code otherwise
2139 */
2140static int smack_task_getsid(struct task_struct *p)
2141{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002142 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002143}
2144
2145/**
2146 * smack_task_getsecid - get the secid of the task
2147 * @p: the object task
2148 * @secid: where to put the result
2149 *
2150 * Sets the secid to contain a u32 version of the smack label.
2151 */
2152static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2153{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002154 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002155
2156 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002157}
2158
2159/**
2160 * smack_task_setnice - Smack check on setting nice
2161 * @p: the task object
2162 * @nice: unused
2163 *
2164 * Return 0 if write access is permitted
2165 */
2166static int smack_task_setnice(struct task_struct *p, int nice)
2167{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002168 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002169}
2170
2171/**
2172 * smack_task_setioprio - Smack check on setting ioprio
2173 * @p: the task object
2174 * @ioprio: unused
2175 *
2176 * Return 0 if write access is permitted
2177 */
2178static int smack_task_setioprio(struct task_struct *p, int ioprio)
2179{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002180 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002181}
2182
2183/**
2184 * smack_task_getioprio - Smack check on reading ioprio
2185 * @p: the task object
2186 *
2187 * Return 0 if read access is permitted
2188 */
2189static int smack_task_getioprio(struct task_struct *p)
2190{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002191 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002192}
2193
2194/**
2195 * smack_task_setscheduler - Smack check on setting scheduler
2196 * @p: the task object
2197 * @policy: unused
2198 * @lp: unused
2199 *
2200 * Return 0 if read access is permitted
2201 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002202static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002203{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002204 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002205}
2206
2207/**
2208 * smack_task_getscheduler - Smack check on reading scheduler
2209 * @p: the task object
2210 *
2211 * Return 0 if read access is permitted
2212 */
2213static int smack_task_getscheduler(struct task_struct *p)
2214{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002215 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002216}
2217
2218/**
2219 * smack_task_movememory - Smack check on moving memory
2220 * @p: the task object
2221 *
2222 * Return 0 if write access is permitted
2223 */
2224static int smack_task_movememory(struct task_struct *p)
2225{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002226 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002227}
2228
2229/**
2230 * smack_task_kill - Smack check on signal delivery
2231 * @p: the task object
2232 * @info: unused
2233 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002234 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002235 *
2236 * Return 0 if write access is permitted
2237 *
Casey Schauflere114e472008-02-04 22:29:50 -08002238 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002239static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002240 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002241{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002242 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002243 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002244 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002245 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002246
Rafal Krypa18d872f2016-04-04 11:14:53 +02002247 if (!sig)
2248 return 0; /* null signal; existence test */
2249
Etienne Bassetecfcc532009-04-08 20:40:06 +02002250 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2251 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002252 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002253 * Sending a signal requires that the sender
2254 * can write the receiver.
2255 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002256 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002257 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2258 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002259 return rc;
2260 }
Casey Schauflere114e472008-02-04 22:29:50 -08002261 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002262 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002263 * specific behavior. This is not clean. For one thing
2264 * we can't take privilege into account.
2265 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002266 skp = smk_of_task(cred->security);
Casey Schauflerc60b9062016-08-30 10:31:39 -07002267 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2268 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002269 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002270}
2271
2272/**
Casey Schauflere114e472008-02-04 22:29:50 -08002273 * smack_task_to_inode - copy task smack into the inode blob
2274 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002275 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002276 *
2277 * Sets the smack pointer in the inode security blob
2278 */
2279static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2280{
2281 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002282 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002283
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002284 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002285 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002286}
2287
2288/*
2289 * Socket hooks.
2290 */
2291
2292/**
2293 * smack_sk_alloc_security - Allocate a socket blob
2294 * @sk: the socket
2295 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002296 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002297 *
2298 * Assign Smack pointers to current
2299 *
2300 * Returns 0 on success, -ENOMEM is there's no memory
2301 */
2302static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2303{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002304 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002305 struct socket_smack *ssp;
2306
2307 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2308 if (ssp == NULL)
2309 return -ENOMEM;
2310
jooseong lee08382c92016-11-03 11:54:39 +01002311 /*
2312 * Sockets created by kernel threads receive web label.
2313 */
2314 if (unlikely(current->flags & PF_KTHREAD)) {
2315 ssp->smk_in = &smack_known_web;
2316 ssp->smk_out = &smack_known_web;
2317 } else {
2318 ssp->smk_in = skp;
2319 ssp->smk_out = skp;
2320 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002321 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002322
2323 sk->sk_security = ssp;
2324
2325 return 0;
2326}
2327
2328/**
2329 * smack_sk_free_security - Free a socket blob
2330 * @sk: the socket
2331 *
2332 * Clears the blob pointer
2333 */
2334static void smack_sk_free_security(struct sock *sk)
2335{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302336#ifdef SMACK_IPV6_PORT_LABELING
2337 struct smk_port_label *spp;
2338
2339 if (sk->sk_family == PF_INET6) {
2340 rcu_read_lock();
2341 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2342 if (spp->smk_sock != sk)
2343 continue;
2344 spp->smk_can_reuse = 1;
2345 break;
2346 }
2347 rcu_read_unlock();
2348 }
2349#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002350 kfree(sk->sk_security);
2351}
2352
2353/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002354* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002355* @sip: the object end
2356*
2357* looks for host based access restrictions
2358*
2359* This version will only be appropriate for really small sets of single label
2360* hosts. The caller is responsible for ensuring that the RCU read lock is
2361* taken before calling this function.
2362*
2363* Returns the label of the far end or NULL if it's not special.
2364*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002365static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002366{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002367 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002368 struct in_addr *siap = &sip->sin_addr;
2369
2370 if (siap->s_addr == 0)
2371 return NULL;
2372
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002373 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2374 /*
2375 * we break after finding the first match because
2376 * the list is sorted from longest to shortest mask
2377 * so we have found the most specific match
2378 */
2379 if (snp->smk_host.s_addr ==
2380 (siap->s_addr & snp->smk_mask.s_addr))
2381 return snp->smk_label;
2382
2383 return NULL;
2384}
2385
2386#if IS_ENABLED(CONFIG_IPV6)
2387/*
2388 * smk_ipv6_localhost - Check for local ipv6 host address
2389 * @sip: the address
2390 *
2391 * Returns boolean true if this is the localhost address
2392 */
2393static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2394{
2395 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2396 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2397
2398 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2399 ntohs(be16p[7]) == 1)
2400 return true;
2401 return false;
2402}
2403
2404/**
2405* smack_ipv6host_label - check host based restrictions
2406* @sip: the object end
2407*
2408* looks for host based access restrictions
2409*
2410* This version will only be appropriate for really small sets of single label
2411* hosts. The caller is responsible for ensuring that the RCU read lock is
2412* taken before calling this function.
2413*
2414* Returns the label of the far end or NULL if it's not special.
2415*/
2416static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2417{
2418 struct smk_net6addr *snp;
2419 struct in6_addr *sap = &sip->sin6_addr;
2420 int i;
2421 int found = 0;
2422
2423 /*
2424 * It's local. Don't look for a host label.
2425 */
2426 if (smk_ipv6_localhost(sip))
2427 return NULL;
2428
2429 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002430 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002431 * If the label is NULL the entry has
2432 * been renounced. Ignore it.
2433 */
2434 if (snp->smk_label == NULL)
2435 continue;
2436 /*
Paul Moore07feee82009-03-27 17:10:54 -04002437 * we break after finding the first match because
2438 * the list is sorted from longest to shortest mask
2439 * so we have found the most specific match
2440 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002441 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002442 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2443 snp->smk_host.s6_addr16[i]) {
2444 found = 0;
2445 break;
2446 }
Etienne Basset43031542009-03-27 17:11:01 -04002447 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002448 if (found)
2449 return snp->smk_label;
2450 }
Paul Moore07feee82009-03-27 17:10:54 -04002451
2452 return NULL;
2453}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002454#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002455
2456/**
Casey Schauflere114e472008-02-04 22:29:50 -08002457 * smack_netlabel - Set the secattr on a socket
2458 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002459 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002460 *
2461 * Convert the outbound smack value (smk_out) to a
2462 * secattr and attach it to the socket.
2463 *
2464 * Returns 0 on success or an error code
2465 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002466static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002467{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002468 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002469 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002470 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002471
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002472 /*
2473 * Usually the netlabel code will handle changing the
2474 * packet labeling based on the label.
2475 * The case of a single label host is different, because
2476 * a single label host should never get a labeled packet
2477 * even though the label is usually associated with a packet
2478 * label.
2479 */
2480 local_bh_disable();
2481 bh_lock_sock_nested(sk);
2482
2483 if (ssp->smk_out == smack_net_ambient ||
2484 labeled == SMACK_UNLABELED_SOCKET)
2485 netlbl_sock_delattr(sk);
2486 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002487 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002488 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002489 }
2490
2491 bh_unlock_sock(sk);
2492 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002493
Casey Schauflere114e472008-02-04 22:29:50 -08002494 return rc;
2495}
2496
2497/**
Paul Moore07feee82009-03-27 17:10:54 -04002498 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2499 * @sk: the socket
2500 * @sap: the destination address
2501 *
2502 * Set the correct secattr for the given socket based on the destination
2503 * address and perform any outbound access checks needed.
2504 *
2505 * Returns 0 on success or an error code.
2506 *
2507 */
2508static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2509{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002510 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002511 int rc;
2512 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002513 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002514 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002515 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002516
2517 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002518 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002519 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002520#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002521 struct lsm_network_audit net;
2522
Eric Paris48c62af2012-04-02 13:15:44 -04002523 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2524 ad.a.u.net->family = sap->sin_family;
2525 ad.a.u.net->dport = sap->sin_port;
2526 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002527#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002528 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002529 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002530 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2531 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002532 } else {
2533 sk_lbl = SMACK_CIPSO_SOCKET;
2534 rc = 0;
2535 }
2536 rcu_read_unlock();
2537 if (rc != 0)
2538 return rc;
2539
2540 return smack_netlabel(sk, sk_lbl);
2541}
2542
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002543#if IS_ENABLED(CONFIG_IPV6)
2544/**
2545 * smk_ipv6_check - check Smack access
2546 * @subject: subject Smack label
2547 * @object: object Smack label
2548 * @address: address
2549 * @act: the action being taken
2550 *
2551 * Check an IPv6 access
2552 */
2553static int smk_ipv6_check(struct smack_known *subject,
2554 struct smack_known *object,
2555 struct sockaddr_in6 *address, int act)
2556{
2557#ifdef CONFIG_AUDIT
2558 struct lsm_network_audit net;
2559#endif
2560 struct smk_audit_info ad;
2561 int rc;
2562
2563#ifdef CONFIG_AUDIT
2564 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2565 ad.a.u.net->family = PF_INET6;
2566 ad.a.u.net->dport = ntohs(address->sin6_port);
2567 if (act == SMK_RECEIVING)
2568 ad.a.u.net->v6info.saddr = address->sin6_addr;
2569 else
2570 ad.a.u.net->v6info.daddr = address->sin6_addr;
2571#endif
2572 rc = smk_access(subject, object, MAY_WRITE, &ad);
2573 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2574 return rc;
2575}
2576#endif /* CONFIG_IPV6 */
2577
2578#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002579/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002580 * smk_ipv6_port_label - Smack port access table management
2581 * @sock: socket
2582 * @address: address
2583 *
2584 * Create or update the port list entry
2585 */
2586static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2587{
2588 struct sock *sk = sock->sk;
2589 struct sockaddr_in6 *addr6;
2590 struct socket_smack *ssp = sock->sk->sk_security;
2591 struct smk_port_label *spp;
2592 unsigned short port = 0;
2593
2594 if (address == NULL) {
2595 /*
2596 * This operation is changing the Smack information
2597 * on the bound socket. Take the changes to the port
2598 * as well.
2599 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302600 rcu_read_lock();
2601 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002602 if (sk != spp->smk_sock)
2603 continue;
2604 spp->smk_in = ssp->smk_in;
2605 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302606 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002607 return;
2608 }
2609 /*
2610 * A NULL address is only used for updating existing
2611 * bound entries. If there isn't one, it's OK.
2612 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302613 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002614 return;
2615 }
2616
2617 addr6 = (struct sockaddr_in6 *)address;
2618 port = ntohs(addr6->sin6_port);
2619 /*
2620 * This is a special case that is safely ignored.
2621 */
2622 if (port == 0)
2623 return;
2624
2625 /*
2626 * Look for an existing port list entry.
2627 * This is an indication that a port is getting reused.
2628 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302629 rcu_read_lock();
2630 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302631 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002632 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302633 if (spp->smk_can_reuse != 1) {
2634 rcu_read_unlock();
2635 return;
2636 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002637 spp->smk_port = port;
2638 spp->smk_sock = sk;
2639 spp->smk_in = ssp->smk_in;
2640 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302641 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302642 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002643 return;
2644 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302645 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002646 /*
2647 * A new port entry is required.
2648 */
2649 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2650 if (spp == NULL)
2651 return;
2652
2653 spp->smk_port = port;
2654 spp->smk_sock = sk;
2655 spp->smk_in = ssp->smk_in;
2656 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302657 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302658 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002659
Vishal Goel3c7ce342016-11-23 10:31:08 +05302660 mutex_lock(&smack_ipv6_lock);
2661 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2662 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002663 return;
2664}
2665
2666/**
2667 * smk_ipv6_port_check - check Smack port access
2668 * @sock: socket
2669 * @address: address
2670 *
2671 * Create or update the port list entry
2672 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002673static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002674 int act)
2675{
Casey Schauflerc6739442013-05-22 18:42:56 -07002676 struct smk_port_label *spp;
2677 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002678 struct smack_known *skp = NULL;
2679 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002680 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002681
2682 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002683 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002684 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002685 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002686 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002687 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002688 }
2689
2690 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002691 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002692 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002693 if (skp != NULL && object != NULL)
2694 return smk_ipv6_check(skp, object, address, act);
2695 if (skp == NULL)
2696 skp = smack_net_ambient;
2697 if (object == NULL)
2698 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002699
2700 /*
2701 * It's remote, so port lookup does no good.
2702 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002703 if (!smk_ipv6_localhost(address))
2704 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002705
2706 /*
2707 * It's local so the send check has to have passed.
2708 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002709 if (act == SMK_RECEIVING)
2710 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002711
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002712 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302713 rcu_read_lock();
2714 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302715 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002716 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002717 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002718 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002719 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002720 break;
2721 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302722 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002723
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002724 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002725}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002726#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002727
2728/**
Casey Schauflere114e472008-02-04 22:29:50 -08002729 * smack_inode_setsecurity - set smack xattrs
2730 * @inode: the object
2731 * @name: attribute name
2732 * @value: attribute value
2733 * @size: size of the attribute
2734 * @flags: unused
2735 *
2736 * Sets the named attribute in the appropriate blob
2737 *
2738 * Returns 0 on success, or an error code
2739 */
2740static int smack_inode_setsecurity(struct inode *inode, const char *name,
2741 const void *value, size_t size, int flags)
2742{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002743 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002744 struct inode_smack *nsp = inode->i_security;
2745 struct socket_smack *ssp;
2746 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002747 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002748
Casey Schauflerf7112e62012-05-06 15:22:02 -07002749 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302750 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002751
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002752 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002753 if (IS_ERR(skp))
2754 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002755
2756 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002757 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002758 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002759 return 0;
2760 }
2761 /*
2762 * The rest of the Smack xattrs are only on sockets.
2763 */
2764 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2765 return -EOPNOTSUPP;
2766
2767 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002768 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002769 return -EOPNOTSUPP;
2770
2771 ssp = sock->sk->sk_security;
2772
2773 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002774 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002775 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002776 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002777 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002778 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2779 if (rc != 0)
2780 printk(KERN_WARNING
2781 "Smack: \"%s\" netlbl error %d.\n",
2782 __func__, -rc);
2783 }
Casey Schauflere114e472008-02-04 22:29:50 -08002784 } else
2785 return -EOPNOTSUPP;
2786
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002787#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002788 if (sock->sk->sk_family == PF_INET6)
2789 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002790#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002791
Casey Schauflere114e472008-02-04 22:29:50 -08002792 return 0;
2793}
2794
2795/**
2796 * smack_socket_post_create - finish socket setup
2797 * @sock: the socket
2798 * @family: protocol family
2799 * @type: unused
2800 * @protocol: unused
2801 * @kern: unused
2802 *
2803 * Sets the netlabel information on the socket
2804 *
2805 * Returns 0 on success, and error code otherwise
2806 */
2807static int smack_socket_post_create(struct socket *sock, int family,
2808 int type, int protocol, int kern)
2809{
Marcin Lis74123012015-01-22 15:40:33 +01002810 struct socket_smack *ssp;
2811
2812 if (sock->sk == NULL)
2813 return 0;
2814
2815 /*
2816 * Sockets created by kernel threads receive web label.
2817 */
2818 if (unlikely(current->flags & PF_KTHREAD)) {
2819 ssp = sock->sk->sk_security;
2820 ssp->smk_in = &smack_known_web;
2821 ssp->smk_out = &smack_known_web;
2822 }
2823
2824 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002825 return 0;
2826 /*
2827 * Set the outbound netlbl.
2828 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002829 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2830}
2831
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002832/**
2833 * smack_socket_socketpair - create socket pair
2834 * @socka: one socket
2835 * @sockb: another socket
2836 *
2837 * Cross reference the peer labels for SO_PEERSEC
2838 *
2839 * Returns 0 on success, and error code otherwise
2840 */
2841static int smack_socket_socketpair(struct socket *socka,
2842 struct socket *sockb)
2843{
2844 struct socket_smack *asp = socka->sk->sk_security;
2845 struct socket_smack *bsp = sockb->sk->sk_security;
2846
2847 asp->smk_packet = bsp->smk_out;
2848 bsp->smk_packet = asp->smk_out;
2849
2850 return 0;
2851}
2852
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002853#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002854/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002855 * smack_socket_bind - record port binding information.
2856 * @sock: the socket
2857 * @address: the port address
2858 * @addrlen: size of the address
2859 *
2860 * Records the label bound to a port.
2861 *
2862 * Returns 0
2863 */
2864static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2865 int addrlen)
2866{
2867 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2868 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002869 return 0;
2870}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002871#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002872
2873/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002874 * smack_socket_connect - connect access check
2875 * @sock: the socket
2876 * @sap: the other end
2877 * @addrlen: size of sap
2878 *
2879 * Verifies that a connection may be possible
2880 *
2881 * Returns 0 on success, and error code otherwise
2882 */
2883static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2884 int addrlen)
2885{
Casey Schauflerc6739442013-05-22 18:42:56 -07002886 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002887#if IS_ENABLED(CONFIG_IPV6)
2888 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2889#endif
2890#ifdef SMACK_IPV6_SECMARK_LABELING
2891 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002892 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002893#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002894
Casey Schauflerc6739442013-05-22 18:42:56 -07002895 if (sock->sk == NULL)
2896 return 0;
2897
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002898#ifdef SMACK_IPV6_SECMARK_LABELING
2899 ssp = sock->sk->sk_security;
2900#endif
2901
Casey Schauflerc6739442013-05-22 18:42:56 -07002902 switch (sock->sk->sk_family) {
2903 case PF_INET:
2904 if (addrlen < sizeof(struct sockaddr_in))
2905 return -EINVAL;
2906 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2907 break;
2908 case PF_INET6:
2909 if (addrlen < sizeof(struct sockaddr_in6))
2910 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002911#ifdef SMACK_IPV6_SECMARK_LABELING
2912 rsp = smack_ipv6host_label(sip);
2913 if (rsp != NULL)
2914 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002915 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002916#endif
2917#ifdef SMACK_IPV6_PORT_LABELING
2918 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2919#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002920 break;
2921 }
2922 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002923}
2924
2925/**
2926 * smack_flags_to_may - convert S_ to MAY_ values
2927 * @flags: the S_ value
2928 *
2929 * Returns the equivalent MAY_ value
2930 */
2931static int smack_flags_to_may(int flags)
2932{
2933 int may = 0;
2934
2935 if (flags & S_IRUGO)
2936 may |= MAY_READ;
2937 if (flags & S_IWUGO)
2938 may |= MAY_WRITE;
2939 if (flags & S_IXUGO)
2940 may |= MAY_EXEC;
2941
2942 return may;
2943}
2944
2945/**
2946 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2947 * @msg: the object
2948 *
2949 * Returns 0
2950 */
2951static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2952{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002953 struct smack_known *skp = smk_of_current();
2954
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002955 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002956 return 0;
2957}
2958
2959/**
2960 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2961 * @msg: the object
2962 *
2963 * Clears the blob pointer
2964 */
2965static void smack_msg_msg_free_security(struct msg_msg *msg)
2966{
2967 msg->security = NULL;
2968}
2969
2970/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002971 * smack_of_ipc - the smack pointer for the ipc
2972 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002973 *
2974 * Returns a pointer to the smack value
2975 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002976static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002977{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002978 return (struct smack_known *)isp->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002979}
2980
2981/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002982 * smack_ipc_alloc_security - Set the security blob for ipc
2983 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002984 *
2985 * Returns 0
2986 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002987static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002988{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002989 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002990
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002991 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002992 return 0;
2993}
2994
2995/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002996 * smack_ipc_free_security - Clear the security blob for ipc
2997 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002998 *
2999 * Clears the blob pointer
3000 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003001static void smack_ipc_free_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08003002{
Casey Schauflere114e472008-02-04 22:29:50 -08003003 isp->security = NULL;
3004}
3005
3006/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003007 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003008 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003009 * @access : access requested
3010 *
3011 * Returns 0 if current has the requested access, error code otherwise
3012 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003013static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003014{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003015 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003016 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003017 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003018
3019#ifdef CONFIG_AUDIT
3020 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003021 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003022#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003023 rc = smk_curacc(ssp, access, &ad);
3024 rc = smk_bu_current("shm", ssp, access, rc);
3025 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003026}
3027
3028/**
Casey Schauflere114e472008-02-04 22:29:50 -08003029 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003030 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003031 * @shmflg: access requested
3032 *
3033 * Returns 0 if current has the requested access, error code otherwise
3034 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003035static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003036{
Casey Schauflere114e472008-02-04 22:29:50 -08003037 int may;
3038
3039 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003040 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003041}
3042
3043/**
3044 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003045 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003046 * @cmd: what it wants to do
3047 *
3048 * Returns 0 if current has the requested access, error code otherwise
3049 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003050static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003051{
Casey Schauflere114e472008-02-04 22:29:50 -08003052 int may;
3053
3054 switch (cmd) {
3055 case IPC_STAT:
3056 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07003057 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003058 may = MAY_READ;
3059 break;
3060 case IPC_SET:
3061 case SHM_LOCK:
3062 case SHM_UNLOCK:
3063 case IPC_RMID:
3064 may = MAY_READWRITE;
3065 break;
3066 case IPC_INFO:
3067 case SHM_INFO:
3068 /*
3069 * System level information.
3070 */
3071 return 0;
3072 default:
3073 return -EINVAL;
3074 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003075 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003076}
3077
3078/**
3079 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003080 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003081 * @shmaddr: unused
3082 * @shmflg: access requested
3083 *
3084 * Returns 0 if current has the requested access, error code otherwise
3085 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003086static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003087 int shmflg)
3088{
Casey Schauflere114e472008-02-04 22:29:50 -08003089 int may;
3090
3091 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003092 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003093}
3094
3095/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003096 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003097 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003098 * @access : access requested
3099 *
3100 * Returns 0 if current has the requested access, error code otherwise
3101 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003102static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003103{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003104 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003105 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003106 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003107
3108#ifdef CONFIG_AUDIT
3109 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003110 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003111#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003112 rc = smk_curacc(ssp, access, &ad);
3113 rc = smk_bu_current("sem", ssp, access, rc);
3114 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003115}
3116
3117/**
Casey Schauflere114e472008-02-04 22:29:50 -08003118 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003120 * @semflg: access requested
3121 *
3122 * Returns 0 if current has the requested access, error code otherwise
3123 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003124static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003125{
Casey Schauflere114e472008-02-04 22:29:50 -08003126 int may;
3127
3128 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003129 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003130}
3131
3132/**
3133 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003134 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003135 * @cmd: what it wants to do
3136 *
3137 * Returns 0 if current has the requested access, error code otherwise
3138 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003139static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003140{
Casey Schauflere114e472008-02-04 22:29:50 -08003141 int may;
3142
3143 switch (cmd) {
3144 case GETPID:
3145 case GETNCNT:
3146 case GETZCNT:
3147 case GETVAL:
3148 case GETALL:
3149 case IPC_STAT:
3150 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003151 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003152 may = MAY_READ;
3153 break;
3154 case SETVAL:
3155 case SETALL:
3156 case IPC_RMID:
3157 case IPC_SET:
3158 may = MAY_READWRITE;
3159 break;
3160 case IPC_INFO:
3161 case SEM_INFO:
3162 /*
3163 * System level information
3164 */
3165 return 0;
3166 default:
3167 return -EINVAL;
3168 }
3169
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003170 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003171}
3172
3173/**
3174 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003175 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003176 * @sops: unused
3177 * @nsops: unused
3178 * @alter: unused
3179 *
3180 * Treated as read and write in all cases.
3181 *
3182 * Returns 0 if access is allowed, error code otherwise
3183 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003184static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003185 unsigned nsops, int alter)
3186{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003187 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003188}
3189
3190/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003191 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003192 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003193 * @access : access requested
3194 *
3195 * return 0 if current has access, error otherwise
3196 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003197static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003198{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003199 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003200 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003201 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003202
3203#ifdef CONFIG_AUDIT
3204 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003205 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003206#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003207 rc = smk_curacc(msp, access, &ad);
3208 rc = smk_bu_current("msq", msp, access, rc);
3209 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003210}
3211
3212/**
Casey Schauflere114e472008-02-04 22:29:50 -08003213 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003214 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003215 * @msqflg: access requested
3216 *
3217 * Returns 0 if current has the requested access, error code otherwise
3218 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003219static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003220{
Casey Schauflere114e472008-02-04 22:29:50 -08003221 int may;
3222
3223 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003224 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003225}
3226
3227/**
3228 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003229 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003230 * @cmd: what it wants to do
3231 *
3232 * Returns 0 if current has the requested access, error code otherwise
3233 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003234static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003235{
Casey Schauflere114e472008-02-04 22:29:50 -08003236 int may;
3237
3238 switch (cmd) {
3239 case IPC_STAT:
3240 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003241 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003242 may = MAY_READ;
3243 break;
3244 case IPC_SET:
3245 case IPC_RMID:
3246 may = MAY_READWRITE;
3247 break;
3248 case IPC_INFO:
3249 case MSG_INFO:
3250 /*
3251 * System level information
3252 */
3253 return 0;
3254 default:
3255 return -EINVAL;
3256 }
3257
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003258 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003259}
3260
3261/**
3262 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003263 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003264 * @msg: unused
3265 * @msqflg: access requested
3266 *
3267 * Returns 0 if current has the requested access, error code otherwise
3268 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003269static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003270 int msqflg)
3271{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003272 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003273
Etienne Bassetecfcc532009-04-08 20:40:06 +02003274 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003275 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003276}
3277
3278/**
3279 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003280 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003281 * @msg: unused
3282 * @target: unused
3283 * @type: unused
3284 * @mode: unused
3285 *
3286 * Returns 0 if current has read and write access, error code otherwise
3287 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003288static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003289 struct task_struct *target, long type, int mode)
3290{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003291 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003292}
3293
3294/**
3295 * smack_ipc_permission - Smack access for ipc_permission()
3296 * @ipp: the object permissions
3297 * @flag: access requested
3298 *
3299 * Returns 0 if current has read and write access, error code otherwise
3300 */
3301static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3302{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003303 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003304 int may = smack_flags_to_may(flag);
3305 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003306 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003307
Etienne Bassetecfcc532009-04-08 20:40:06 +02003308#ifdef CONFIG_AUDIT
3309 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3310 ad.a.u.ipc_id = ipp->id;
3311#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003312 rc = smk_curacc(iskp, may, &ad);
3313 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003314 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003315}
3316
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003317/**
3318 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003319 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003320 * @secid: where result will be saved
3321 */
3322static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3323{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003324 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003325
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003326 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003327}
3328
Casey Schauflere114e472008-02-04 22:29:50 -08003329/**
3330 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003331 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003332 * @inode: the object
3333 *
3334 * Set the inode's security blob if it hasn't been done already.
3335 */
3336static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3337{
3338 struct super_block *sbp;
3339 struct superblock_smack *sbsp;
3340 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003341 struct smack_known *skp;
3342 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003343 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003344 char trattr[TRANS_TRUE_SIZE];
3345 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003346 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003347 struct dentry *dp;
3348
3349 if (inode == NULL)
3350 return;
3351
3352 isp = inode->i_security;
3353
3354 mutex_lock(&isp->smk_lock);
3355 /*
3356 * If the inode is already instantiated
3357 * take the quick way out
3358 */
3359 if (isp->smk_flags & SMK_INODE_INSTANT)
3360 goto unlockandout;
3361
3362 sbp = inode->i_sb;
3363 sbsp = sbp->s_security;
3364 /*
3365 * We're going to use the superblock default label
3366 * if there's no label on the file.
3367 */
3368 final = sbsp->smk_default;
3369
3370 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003371 * If this is the root inode the superblock
3372 * may be in the process of initialization.
3373 * If that is the case use the root value out
3374 * of the superblock.
3375 */
3376 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003377 switch (sbp->s_magic) {
3378 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003379 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003380 /*
3381 * The cgroup filesystem is never mounted,
3382 * so there's no opportunity to set the mount
3383 * options.
3384 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003385 sbsp->smk_root = &smack_known_star;
3386 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003387 isp->smk_inode = sbsp->smk_root;
3388 break;
3389 case TMPFS_MAGIC:
3390 /*
3391 * What about shmem/tmpfs anonymous files with dentry
3392 * obtained from d_alloc_pseudo()?
3393 */
3394 isp->smk_inode = smk_of_current();
3395 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003396 case PIPEFS_MAGIC:
3397 isp->smk_inode = smk_of_current();
3398 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003399 case SOCKFS_MAGIC:
3400 /*
3401 * Socket access is controlled by the socket
3402 * structures associated with the task involved.
3403 */
3404 isp->smk_inode = &smack_known_star;
3405 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003406 default:
3407 isp->smk_inode = sbsp->smk_root;
3408 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003409 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003410 isp->smk_flags |= SMK_INODE_INSTANT;
3411 goto unlockandout;
3412 }
3413
3414 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003415 * This is pretty hackish.
3416 * Casey says that we shouldn't have to do
3417 * file system specific code, but it does help
3418 * with keeping it simple.
3419 */
3420 switch (sbp->s_magic) {
3421 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003422 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003423 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003424 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003425 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003426 * that the smack file system doesn't do
3427 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003428 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003429 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003430 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003431 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003432 break;
3433 case DEVPTS_SUPER_MAGIC:
3434 /*
3435 * devpts seems content with the label of the task.
3436 * Programs that change smack have to treat the
3437 * pty with respect.
3438 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003439 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003440 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003441 case PROC_SUPER_MAGIC:
3442 /*
3443 * Casey says procfs appears not to care.
3444 * The superblock default suffices.
3445 */
3446 break;
3447 case TMPFS_MAGIC:
3448 /*
3449 * Device labels should come from the filesystem,
3450 * but watch out, because they're volitile,
3451 * getting recreated on every reboot.
3452 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003453 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003454 /*
Gustavo A. R. Silvab1fed3e2018-08-01 17:38:54 -05003455 * Fall through.
Casey Schauflere114e472008-02-04 22:29:50 -08003456 *
3457 * If a smack value has been set we want to use it,
3458 * but since tmpfs isn't giving us the opportunity
3459 * to set mount options simulate setting the
3460 * superblock default.
3461 */
3462 default:
3463 /*
3464 * This isn't an understood special case.
3465 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003466 */
3467
3468 /*
3469 * UNIX domain sockets use lower level socket data.
3470 */
3471 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003472 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003473 break;
3474 }
3475 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003476 * No xattr support means, alas, no SMACK label.
3477 * Use the aforeapplied default.
3478 * It would be curious if the label of the task
3479 * does not match that assigned.
3480 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003481 if (!(inode->i_opflags & IOP_XATTR))
3482 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003483 /*
3484 * Get the dentry for xattr.
3485 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003486 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003487 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003488 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003489 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003490
3491 /*
3492 * Transmuting directory
3493 */
3494 if (S_ISDIR(inode->i_mode)) {
3495 /*
3496 * If this is a new directory and the label was
3497 * transmuted when the inode was initialized
3498 * set the transmute attribute on the directory
3499 * and mark the inode.
3500 *
3501 * If there is a transmute attribute on the
3502 * directory mark the inode.
3503 */
3504 if (isp->smk_flags & SMK_INODE_CHANGED) {
3505 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003506 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003507 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003508 TRANS_TRUE, TRANS_TRUE_SIZE,
3509 0);
3510 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003511 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003512 XATTR_NAME_SMACKTRANSMUTE, trattr,
3513 TRANS_TRUE_SIZE);
3514 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3515 TRANS_TRUE_SIZE) != 0)
3516 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003517 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003518 if (rc >= 0)
3519 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003520 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003521 /*
3522 * Don't let the exec or mmap label be "*" or "@".
3523 */
3524 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3525 if (IS_ERR(skp) || skp == &smack_known_star ||
3526 skp == &smack_known_web)
3527 skp = NULL;
3528 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003529
Casey Schaufler19760ad2013-12-16 16:27:26 -08003530 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003531 if (IS_ERR(skp) || skp == &smack_known_star ||
3532 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003533 skp = NULL;
3534 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003535
Casey Schauflere114e472008-02-04 22:29:50 -08003536 dput(dp);
3537 break;
3538 }
3539
3540 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003541 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003542 else
3543 isp->smk_inode = final;
3544
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003545 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003546
3547unlockandout:
3548 mutex_unlock(&isp->smk_lock);
3549 return;
3550}
3551
3552/**
3553 * smack_getprocattr - Smack process attribute access
3554 * @p: the object task
3555 * @name: the name of the attribute in /proc/.../attr
3556 * @value: where to put the result
3557 *
3558 * Places a copy of the task Smack into value
3559 *
3560 * Returns the length of the smack label or an error code
3561 */
3562static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3563{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003564 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003565 char *cp;
3566 int slen;
3567
3568 if (strcmp(name, "current") != 0)
3569 return -EINVAL;
3570
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003571 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003572 if (cp == NULL)
3573 return -ENOMEM;
3574
3575 slen = strlen(cp);
3576 *value = cp;
3577 return slen;
3578}
3579
3580/**
3581 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003582 * @name: the name of the attribute in /proc/.../attr
3583 * @value: the value to set
3584 * @size: the size of the value
3585 *
3586 * Sets the Smack value of the task. Only setting self
3587 * is permitted and only with privilege
3588 *
3589 * Returns the length of the smack label or an error code
3590 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003591static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003592{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003593 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003594 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003595 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003596 struct smack_known_list_elem *sklep;
3597 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003598
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003599 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003600 return -EPERM;
3601
Casey Schauflerf7112e62012-05-06 15:22:02 -07003602 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003603 return -EINVAL;
3604
3605 if (strcmp(name, "current") != 0)
3606 return -EINVAL;
3607
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003608 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003609 if (IS_ERR(skp))
3610 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003611
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003612 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303613 * No process is ever allowed the web ("@") label
3614 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003615 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303616 if (skp == &smack_known_web || skp == &smack_known_star)
3617 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003618
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003619 if (!smack_privileged(CAP_MAC_ADMIN)) {
3620 rc = -EPERM;
3621 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3622 if (sklep->smk_label == skp) {
3623 rc = 0;
3624 break;
3625 }
3626 if (rc)
3627 return rc;
3628 }
3629
David Howellsd84f4f92008-11-14 10:39:23 +11003630 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003631 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003632 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003633
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003634 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003635 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003636 /*
3637 * process can change its label only once
3638 */
3639 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003640
David Howellsd84f4f92008-11-14 10:39:23 +11003641 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003642 return size;
3643}
3644
3645/**
3646 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003647 * @sock: one sock
3648 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003649 * @newsk: unused
3650 *
3651 * Return 0 if a subject with the smack of sock could access
3652 * an object with the smack of other, otherwise an error code
3653 */
David S. Miller3610cda2011-01-05 15:38:53 -08003654static int smack_unix_stream_connect(struct sock *sock,
3655 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003656{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003657 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003658 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003659 struct socket_smack *ssp = sock->sk_security;
3660 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003661 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003662 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003663 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003664#ifdef CONFIG_AUDIT
3665 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003666#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003667
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003668 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3669 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003670 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003671#ifdef CONFIG_AUDIT
3672 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3673 smk_ad_setfield_u_net_sk(&ad, other);
3674#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003675 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3676 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003677 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003678 okp = osp->smk_out;
3679 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003680 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003681 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003682 MAY_WRITE, rc);
3683 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003684 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003685
Casey Schaufler975d5e52011-09-26 14:43:39 -07003686 /*
3687 * Cross reference the peer labels for SO_PEERSEC.
3688 */
3689 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003690 nsp->smk_packet = ssp->smk_out;
3691 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003692 }
3693
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003694 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003695}
3696
3697/**
3698 * smack_unix_may_send - Smack access on UDS
3699 * @sock: one socket
3700 * @other: the other socket
3701 *
3702 * Return 0 if a subject with the smack of sock could access
3703 * an object with the smack of other, otherwise an error code
3704 */
3705static int smack_unix_may_send(struct socket *sock, struct socket *other)
3706{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003707 struct socket_smack *ssp = sock->sk->sk_security;
3708 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003709 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003710 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003711
Kees Cook923e9a12012-04-10 13:26:44 -07003712#ifdef CONFIG_AUDIT
3713 struct lsm_network_audit net;
3714
Eric Paris48c62af2012-04-02 13:15:44 -04003715 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003716 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003717#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003718
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003719 if (smack_privileged(CAP_MAC_OVERRIDE))
3720 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003721
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003722 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3723 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003724 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003725}
3726
3727/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003728 * smack_socket_sendmsg - Smack check based on destination host
3729 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003730 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003731 * @size: the size of the message
3732 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003733 * Return 0 if the current subject can write to the destination host.
3734 * For IPv4 this is only a question if the destination is a single label host.
3735 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003736 */
3737static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3738 int size)
3739{
3740 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003741#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003742 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003743#endif
3744#ifdef SMACK_IPV6_SECMARK_LABELING
3745 struct socket_smack *ssp = sock->sk->sk_security;
3746 struct smack_known *rsp;
3747#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003748 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003749
3750 /*
3751 * Perfectly reasonable for this to be NULL
3752 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003753 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003754 return 0;
3755
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003756 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003757 case AF_INET:
3758 rc = smack_netlabel_send(sock->sk, sip);
3759 break;
3760 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003761#ifdef SMACK_IPV6_SECMARK_LABELING
3762 rsp = smack_ipv6host_label(sap);
3763 if (rsp != NULL)
3764 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3765 SMK_CONNECTING);
3766#endif
3767#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003768 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003769#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003770 break;
3771 }
3772 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003773}
3774
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003775/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003776 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003777 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003778 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003779 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003780 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003781 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003782static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3783 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003784{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003785 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003786 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003787 int acat;
3788 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003789
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003790 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003791 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003792 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003793 * If there are flags but no level netlabel isn't
3794 * behaving the way we expect it to.
3795 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003796 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003797 * Without guidance regarding the smack value
3798 * for the packet fall back on the network
3799 * ambient value.
3800 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003801 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303802 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003803 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003804 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003805 /*
3806 * Compare the catsets. Use the netlbl APIs.
3807 */
3808 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3809 if ((skp->smk_netlabel.flags &
3810 NETLBL_SECATTR_MLS_CAT) == 0)
3811 found = 1;
3812 break;
3813 }
3814 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003815 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3816 acat + 1);
3817 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003818 skp->smk_netlabel.attr.mls.cat,
3819 kcat + 1);
3820 if (acat < 0 || kcat < 0)
3821 break;
3822 }
3823 if (acat == kcat) {
3824 found = 1;
3825 break;
3826 }
Casey Schauflere114e472008-02-04 22:29:50 -08003827 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003828 rcu_read_unlock();
3829
3830 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003831 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003832
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003833 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003834 return &smack_known_web;
3835 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003836 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003837 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003838 /*
3839 * Looks like a fallback, which gives us a secid.
3840 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003841 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003842 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003843 * Without guidance regarding the smack value
3844 * for the packet fall back on the network
3845 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003846 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003847 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003848}
3849
Casey Schaufler69f287a2014-12-12 17:08:40 -08003850#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003851static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003852{
Casey Schauflerc6739442013-05-22 18:42:56 -07003853 u8 nexthdr;
3854 int offset;
3855 int proto = -EINVAL;
3856 struct ipv6hdr _ipv6h;
3857 struct ipv6hdr *ip6;
3858 __be16 frag_off;
3859 struct tcphdr _tcph, *th;
3860 struct udphdr _udph, *uh;
3861 struct dccp_hdr _dccph, *dh;
3862
3863 sip->sin6_port = 0;
3864
3865 offset = skb_network_offset(skb);
3866 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3867 if (ip6 == NULL)
3868 return -EINVAL;
3869 sip->sin6_addr = ip6->saddr;
3870
3871 nexthdr = ip6->nexthdr;
3872 offset += sizeof(_ipv6h);
3873 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3874 if (offset < 0)
3875 return -EINVAL;
3876
3877 proto = nexthdr;
3878 switch (proto) {
3879 case IPPROTO_TCP:
3880 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3881 if (th != NULL)
3882 sip->sin6_port = th->source;
3883 break;
3884 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003885 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003886 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3887 if (uh != NULL)
3888 sip->sin6_port = uh->source;
3889 break;
3890 case IPPROTO_DCCP:
3891 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3892 if (dh != NULL)
3893 sip->sin6_port = dh->dccph_sport;
3894 break;
3895 }
3896 return proto;
3897}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003898#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003899
Casey Schauflere114e472008-02-04 22:29:50 -08003900/**
3901 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3902 * @sk: socket
3903 * @skb: packet
3904 *
3905 * Returns 0 if the packet should be delivered, an error code otherwise
3906 */
3907static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3908{
3909 struct netlbl_lsm_secattr secattr;
3910 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003911 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003912 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003913 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003914 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003915#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003916 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003917#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003918#if IS_ENABLED(CONFIG_IPV6)
3919 struct sockaddr_in6 sadd;
3920 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003921
3922 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3923 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003924#endif /* CONFIG_IPV6 */
3925
Piotr Sawicki129a9982018-07-19 11:42:58 +02003926 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003927 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003928#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3929 /*
3930 * If there is a secmark use it rather than the CIPSO label.
3931 * If there is no secmark fall back to CIPSO.
3932 * The secmark is assumed to reflect policy better.
3933 */
3934 if (skb && skb->secmark != 0) {
3935 skp = smack_from_secid(skb->secmark);
3936 goto access_check;
3937 }
3938#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003939 /*
3940 * Translate what netlabel gave us.
3941 */
3942 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003943
Piotr Sawicki129a9982018-07-19 11:42:58 +02003944 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003945 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003946 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003947 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003948 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003949
Casey Schauflerc6739442013-05-22 18:42:56 -07003950 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003951
Casey Schaufler69f287a2014-12-12 17:08:40 -08003952#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3953access_check:
3954#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003955#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003956 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003957 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003958 ad.a.u.net->netif = skb->skb_iif;
3959 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003960#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003961 /*
3962 * Receiving a packet requires that the other end
3963 * be able to write here. Read access is not required.
3964 * This is the simplist possible security model
3965 * for networking.
3966 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003967 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3968 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003969 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003970 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003971 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003972 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003973#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003974 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003975 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003976 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3977 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003978 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003979#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003980 if (skb && skb->secmark != 0)
3981 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003982 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003983 skp = smack_ipv6host_label(&sadd);
3984 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003985 skp = smack_net_ambient;
3986#ifdef CONFIG_AUDIT
3987 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003988 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003989 ad.a.u.net->netif = skb->skb_iif;
3990 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3991#endif /* CONFIG_AUDIT */
3992 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3993 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3994 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003995#endif /* SMACK_IPV6_SECMARK_LABELING */
3996#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003997 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003998#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003999 if (rc != 0)
4000 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
4001 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07004002 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004003#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07004004 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004005
Paul Moorea8134292008-10-10 10:16:31 -04004006 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004007}
4008
4009/**
4010 * smack_socket_getpeersec_stream - pull in packet label
4011 * @sock: the socket
4012 * @optval: user's destination
4013 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004014 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004015 *
4016 * returns zero on success, an error code otherwise
4017 */
4018static int smack_socket_getpeersec_stream(struct socket *sock,
4019 char __user *optval,
4020 int __user *optlen, unsigned len)
4021{
4022 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004023 char *rcp = "";
4024 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004025 int rc = 0;
4026
4027 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004028 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004029 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004030 slen = strlen(rcp) + 1;
4031 }
Casey Schauflere114e472008-02-04 22:29:50 -08004032
4033 if (slen > len)
4034 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004035 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004036 rc = -EFAULT;
4037
4038 if (put_user(slen, optlen) != 0)
4039 rc = -EFAULT;
4040
4041 return rc;
4042}
4043
4044
4045/**
4046 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004047 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004048 * @skb: packet data
4049 * @secid: pointer to where to put the secid of the packet
4050 *
4051 * Sets the netlabel socket state on sk from parent
4052 */
4053static int smack_socket_getpeersec_dgram(struct socket *sock,
4054 struct sk_buff *skb, u32 *secid)
4055
4056{
4057 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004058 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004059 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004060 int family = PF_UNSPEC;
4061 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004062 int rc;
4063
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004064 if (skb != NULL) {
4065 if (skb->protocol == htons(ETH_P_IP))
4066 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004067#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004068 else if (skb->protocol == htons(ETH_P_IPV6))
4069 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004070#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004071 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004072 if (family == PF_UNSPEC && sock != NULL)
4073 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004074
Casey Schaufler69f287a2014-12-12 17:08:40 -08004075 switch (family) {
4076 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004077 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004078 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004079 break;
4080 case PF_INET:
4081#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4082 s = skb->secmark;
4083 if (s != 0)
4084 break;
4085#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004086 /*
4087 * Translate what netlabel gave us.
4088 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004089 if (sock != NULL && sock->sk != NULL)
4090 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004091 netlbl_secattr_init(&secattr);
4092 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4093 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004094 skp = smack_from_secattr(&secattr, ssp);
4095 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004096 }
4097 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004098 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004099 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004100#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004101 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004102#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004103 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004104 }
4105 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004106 if (s == 0)
4107 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004108 return 0;
4109}
4110
4111/**
Paul Moore07feee82009-03-27 17:10:54 -04004112 * smack_sock_graft - Initialize a newly created socket with an existing sock
4113 * @sk: child sock
4114 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004115 *
Paul Moore07feee82009-03-27 17:10:54 -04004116 * Set the smk_{in,out} state of an existing sock based on the process that
4117 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004118 */
4119static void smack_sock_graft(struct sock *sk, struct socket *parent)
4120{
4121 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004122 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004123
Paul Moore07feee82009-03-27 17:10:54 -04004124 if (sk == NULL ||
4125 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004126 return;
4127
4128 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004129 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004130 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004131 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004132}
4133
4134/**
4135 * smack_inet_conn_request - Smack access check on connect
4136 * @sk: socket involved
4137 * @skb: packet
4138 * @req: unused
4139 *
4140 * Returns 0 if a task with the packet label could write to
4141 * the socket, otherwise an error code
4142 */
4143static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4144 struct request_sock *req)
4145{
Paul Moore07feee82009-03-27 17:10:54 -04004146 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004147 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004148 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004149 struct netlbl_lsm_secattr secattr;
4150 struct sockaddr_in addr;
4151 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004152 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004153 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004154 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004155#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004156 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004157#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004158
Casey Schaufler69f287a2014-12-12 17:08:40 -08004159#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004160 if (family == PF_INET6) {
4161 /*
4162 * Handle mapped IPv4 packets arriving
4163 * via IPv6 sockets. Don't set up netlabel
4164 * processing on IPv6.
4165 */
4166 if (skb->protocol == htons(ETH_P_IP))
4167 family = PF_INET;
4168 else
4169 return 0;
4170 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004171#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004172
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004173#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4174 /*
4175 * If there is a secmark use it rather than the CIPSO label.
4176 * If there is no secmark fall back to CIPSO.
4177 * The secmark is assumed to reflect policy better.
4178 */
4179 if (skb && skb->secmark != 0) {
4180 skp = smack_from_secid(skb->secmark);
4181 goto access_check;
4182 }
4183#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4184
Paul Moore07feee82009-03-27 17:10:54 -04004185 netlbl_secattr_init(&secattr);
4186 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004187 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004188 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004189 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004190 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004191 netlbl_secattr_destroy(&secattr);
4192
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004193#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4194access_check:
4195#endif
4196
Etienne Bassetecfcc532009-04-08 20:40:06 +02004197#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004198 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4199 ad.a.u.net->family = family;
4200 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004201 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4202#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004203 /*
Paul Moore07feee82009-03-27 17:10:54 -04004204 * Receiving a packet requires that the other end be able to write
4205 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004206 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004207 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4208 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004209 if (rc != 0)
4210 return rc;
4211
4212 /*
4213 * Save the peer's label in the request_sock so we can later setup
4214 * smk_packet in the child socket so that SO_PEERCRED can report it.
4215 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004216 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004217
4218 /*
4219 * We need to decide if we want to label the incoming connection here
4220 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004221 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004222 */
4223 hdr = ip_hdr(skb);
4224 addr.sin_addr.s_addr = hdr->saddr;
4225 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004226 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004227 rcu_read_unlock();
4228
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004229 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004230 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004231 else
Paul Moore07feee82009-03-27 17:10:54 -04004232 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004233
4234 return rc;
4235}
4236
Paul Moore07feee82009-03-27 17:10:54 -04004237/**
4238 * smack_inet_csk_clone - Copy the connection information to the new socket
4239 * @sk: the new socket
4240 * @req: the connection's request_sock
4241 *
4242 * Transfer the connection's peer label to the newly created socket.
4243 */
4244static void smack_inet_csk_clone(struct sock *sk,
4245 const struct request_sock *req)
4246{
4247 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004248 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004249
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004250 if (req->peer_secid != 0) {
4251 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004252 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004253 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004254 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004255}
4256
Casey Schauflere114e472008-02-04 22:29:50 -08004257/*
4258 * Key management security hooks
4259 *
4260 * Casey has not tested key support very heavily.
4261 * The permission check is most likely too restrictive.
4262 * If you care about keys please have a look.
4263 */
4264#ifdef CONFIG_KEYS
4265
4266/**
4267 * smack_key_alloc - Set the key security blob
4268 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004269 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004270 * @flags: unused
4271 *
4272 * No allocation required
4273 *
4274 * Returns 0
4275 */
David Howellsd84f4f92008-11-14 10:39:23 +11004276static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004277 unsigned long flags)
4278{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004279 struct smack_known *skp = smk_of_task(cred->security);
4280
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004281 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004282 return 0;
4283}
4284
4285/**
4286 * smack_key_free - Clear the key security blob
4287 * @key: the object
4288 *
4289 * Clear the blob pointer
4290 */
4291static void smack_key_free(struct key *key)
4292{
4293 key->security = NULL;
4294}
4295
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004296/**
Casey Schauflere114e472008-02-04 22:29:50 -08004297 * smack_key_permission - Smack access on a key
4298 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004299 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004300 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004301 *
4302 * Return 0 if the task has read and write to the object,
4303 * an error code otherwise
4304 */
4305static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004306 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004307{
4308 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004309 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004310 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004311 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004312 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004313
4314 keyp = key_ref_to_ptr(key_ref);
4315 if (keyp == NULL)
4316 return -EINVAL;
4317 /*
4318 * If the key hasn't been initialized give it access so that
4319 * it may do so.
4320 */
4321 if (keyp->security == NULL)
4322 return 0;
4323 /*
4324 * This should not occur
4325 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004326 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004327 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004328
4329 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4330 return 0;
4331
Etienne Bassetecfcc532009-04-08 20:40:06 +02004332#ifdef CONFIG_AUDIT
4333 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4334 ad.a.u.key_struct.key = keyp->serial;
4335 ad.a.u.key_struct.key_desc = keyp->description;
4336#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004337 if (perm & KEY_NEED_READ)
4338 request = MAY_READ;
4339 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4340 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004341 rc = smk_access(tkp, keyp->security, request, &ad);
4342 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4343 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004344}
José Bollo7fc5f362015-02-17 15:41:22 +01004345
4346/*
4347 * smack_key_getsecurity - Smack label tagging the key
4348 * @key points to the key to be queried
4349 * @_buffer points to a pointer that should be set to point to the
4350 * resulting string (if no label or an error occurs).
4351 * Return the length of the string (including terminating NUL) or -ve if
4352 * an error.
4353 * May also return 0 (and a NULL buffer pointer) if there is no label.
4354 */
4355static int smack_key_getsecurity(struct key *key, char **_buffer)
4356{
4357 struct smack_known *skp = key->security;
4358 size_t length;
4359 char *copy;
4360
4361 if (key->security == NULL) {
4362 *_buffer = NULL;
4363 return 0;
4364 }
4365
4366 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4367 if (copy == NULL)
4368 return -ENOMEM;
4369 length = strlen(copy) + 1;
4370
4371 *_buffer = copy;
4372 return length;
4373}
4374
Casey Schauflere114e472008-02-04 22:29:50 -08004375#endif /* CONFIG_KEYS */
4376
4377/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004378 * Smack Audit hooks
4379 *
4380 * Audit requires a unique representation of each Smack specific
4381 * rule. This unique representation is used to distinguish the
4382 * object to be audited from remaining kernel objects and also
4383 * works as a glue between the audit hooks.
4384 *
4385 * Since repository entries are added but never deleted, we'll use
4386 * the smack_known label address related to the given audit rule as
4387 * the needed unique representation. This also better fits the smack
4388 * model where nearly everything is a label.
4389 */
4390#ifdef CONFIG_AUDIT
4391
4392/**
4393 * smack_audit_rule_init - Initialize a smack audit rule
4394 * @field: audit rule fields given from user-space (audit.h)
4395 * @op: required testing operator (=, !=, >, <, ...)
4396 * @rulestr: smack label to be audited
4397 * @vrule: pointer to save our own audit rule representation
4398 *
4399 * Prepare to audit cases where (@field @op @rulestr) is true.
4400 * The label to be audited is created if necessay.
4401 */
4402static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4403{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004404 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004405 char **rule = (char **)vrule;
4406 *rule = NULL;
4407
4408 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4409 return -EINVAL;
4410
Al Viro5af75d82008-12-16 05:59:26 -05004411 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004412 return -EINVAL;
4413
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004414 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004415 if (IS_ERR(skp))
4416 return PTR_ERR(skp);
4417
4418 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004419
4420 return 0;
4421}
4422
4423/**
4424 * smack_audit_rule_known - Distinguish Smack audit rules
4425 * @krule: rule of interest, in Audit kernel representation format
4426 *
4427 * This is used to filter Smack rules from remaining Audit ones.
4428 * If it's proved that this rule belongs to us, the
4429 * audit_rule_match hook will be called to do the final judgement.
4430 */
4431static int smack_audit_rule_known(struct audit_krule *krule)
4432{
4433 struct audit_field *f;
4434 int i;
4435
4436 for (i = 0; i < krule->field_count; i++) {
4437 f = &krule->fields[i];
4438
4439 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4440 return 1;
4441 }
4442
4443 return 0;
4444}
4445
4446/**
4447 * smack_audit_rule_match - Audit given object ?
4448 * @secid: security id for identifying the object to test
4449 * @field: audit rule flags given from user-space
4450 * @op: required testing operator
4451 * @vrule: smack internal rule presentation
4452 * @actx: audit context associated with the check
4453 *
4454 * The core Audit hook. It's used to take the decision of
4455 * whether to audit or not to audit a given object.
4456 */
4457static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4458 struct audit_context *actx)
4459{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004460 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004461 char *rule = vrule;
4462
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004463 if (unlikely(!rule)) {
4464 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004465 return -ENOENT;
4466 }
4467
4468 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4469 return 0;
4470
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004471 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004472
4473 /*
4474 * No need to do string comparisons. If a match occurs,
4475 * both pointers will point to the same smack_known
4476 * label.
4477 */
Al Viro5af75d82008-12-16 05:59:26 -05004478 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004479 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004480 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004481 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004482
4483 return 0;
4484}
4485
Casey Schaufler491a0b02016-01-26 15:08:35 -08004486/*
4487 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004488 * No memory was allocated.
4489 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004490
4491#endif /* CONFIG_AUDIT */
4492
Randy Dunlap251a2a92009-02-18 11:42:33 -08004493/**
David Quigley746df9b2013-05-22 12:50:35 -04004494 * smack_ismaclabel - check if xattr @name references a smack MAC label
4495 * @name: Full xattr name to check.
4496 */
4497static int smack_ismaclabel(const char *name)
4498{
4499 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4500}
4501
4502
4503/**
Casey Schauflere114e472008-02-04 22:29:50 -08004504 * smack_secid_to_secctx - return the smack label for a secid
4505 * @secid: incoming integer
4506 * @secdata: destination
4507 * @seclen: how long it is
4508 *
4509 * Exists for networking code.
4510 */
4511static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4512{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004513 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004514
Eric Parisd5630b92010-10-13 16:24:48 -04004515 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004516 *secdata = skp->smk_known;
4517 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004518 return 0;
4519}
4520
Randy Dunlap251a2a92009-02-18 11:42:33 -08004521/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004522 * smack_secctx_to_secid - return the secid for a smack label
4523 * @secdata: smack label
4524 * @seclen: how long result is
4525 * @secid: outgoing integer
4526 *
4527 * Exists for audit and networking code.
4528 */
David Howellse52c17642008-04-29 20:52:51 +01004529static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004530{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004531 struct smack_known *skp = smk_find_entry(secdata);
4532
4533 if (skp)
4534 *secid = skp->smk_secid;
4535 else
4536 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004537 return 0;
4538}
4539
Casey Schaufler491a0b02016-01-26 15:08:35 -08004540/*
4541 * There used to be a smack_release_secctx hook
4542 * that did nothing back when hooks were in a vector.
4543 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004544 */
Casey Schauflere114e472008-02-04 22:29:50 -08004545
David P. Quigley1ee65e32009-09-03 14:25:57 -04004546static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4547{
4548 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4549}
4550
4551static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4552{
4553 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4554}
4555
4556static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4557{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004558 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004559
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004560 *ctx = skp->smk_known;
4561 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004562 return 0;
4563}
4564
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004565static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4566{
4567
4568 struct task_smack *tsp;
4569 struct smack_known *skp;
4570 struct inode_smack *isp;
4571 struct cred *new_creds = *new;
4572
4573 if (new_creds == NULL) {
4574 new_creds = prepare_creds();
4575 if (new_creds == NULL)
4576 return -ENOMEM;
4577 }
4578
4579 tsp = new_creds->security;
4580
4581 /*
4582 * Get label from overlay inode and set it in create_sid
4583 */
4584 isp = d_inode(dentry->d_parent)->i_security;
4585 skp = isp->smk_inode;
4586 tsp->smk_task = skp;
4587 *new = new_creds;
4588 return 0;
4589}
4590
4591static int smack_inode_copy_up_xattr(const char *name)
4592{
4593 /*
4594 * Return 1 if this is the smack access Smack attribute.
4595 */
4596 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4597 return 1;
4598
4599 return -EOPNOTSUPP;
4600}
4601
4602static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4603 struct qstr *name,
4604 const struct cred *old,
4605 struct cred *new)
4606{
4607 struct task_smack *otsp = old->security;
4608 struct task_smack *ntsp = new->security;
4609 struct inode_smack *isp;
4610 int may;
4611
4612 /*
4613 * Use the process credential unless all of
4614 * the transmuting criteria are met
4615 */
4616 ntsp->smk_task = otsp->smk_task;
4617
4618 /*
4619 * the attribute of the containing directory
4620 */
4621 isp = d_inode(dentry->d_parent)->i_security;
4622
4623 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4624 rcu_read_lock();
4625 may = smk_access_entry(otsp->smk_task->smk_known,
4626 isp->smk_inode->smk_known,
4627 &otsp->smk_task->smk_rules);
4628 rcu_read_unlock();
4629
4630 /*
4631 * If the directory is transmuting and the rule
4632 * providing access is transmuting use the containing
4633 * directory label instead of the process label.
4634 */
4635 if (may > 0 && (may & MAY_TRANSMUTE))
4636 ntsp->smk_task = isp->smk_inode;
4637 }
4638 return 0;
4639}
4640
James Morrisca97d932017-02-15 00:18:51 +11004641static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004642 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4643 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4644 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004645
Casey Schauflere20b0432015-05-02 15:11:36 -07004646 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4647 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004648 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004649 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004650 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304651 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004652
Casey Schauflere20b0432015-05-02 15:11:36 -07004653 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004654
Casey Schauflere20b0432015-05-02 15:11:36 -07004655 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4656 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4657 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4658 LSM_HOOK_INIT(inode_link, smack_inode_link),
4659 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4660 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4661 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4662 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4663 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4664 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4665 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4666 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4667 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4668 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4669 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4670 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4671 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4672 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004673
Casey Schauflere20b0432015-05-02 15:11:36 -07004674 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4675 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4676 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4677 LSM_HOOK_INIT(file_lock, smack_file_lock),
4678 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4679 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4680 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4681 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4682 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4683 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004684
Casey Schauflere20b0432015-05-02 15:11:36 -07004685 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004686
Casey Schauflere20b0432015-05-02 15:11:36 -07004687 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4688 LSM_HOOK_INIT(cred_free, smack_cred_free),
4689 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4690 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004691 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004692 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4693 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4694 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4695 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4696 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4697 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4698 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4699 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4700 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4701 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4702 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4703 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4704 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004705 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004706
Casey Schauflere20b0432015-05-02 15:11:36 -07004707 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4708 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004709
Casey Schauflere20b0432015-05-02 15:11:36 -07004710 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4711 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004712
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004713 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4714 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004715 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4716 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4717 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4718 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004719
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004720 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4721 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004722 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4723 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4724 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004725
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004726 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4727 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004728 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4729 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4730 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004731
Casey Schauflere20b0432015-05-02 15:11:36 -07004732 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004733
Casey Schauflere20b0432015-05-02 15:11:36 -07004734 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4735 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004736
Casey Schauflere20b0432015-05-02 15:11:36 -07004737 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4738 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004739
Casey Schauflere20b0432015-05-02 15:11:36 -07004740 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004741 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004742#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004743 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004744#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004745 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4746 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4747 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4748 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4749 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4750 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4751 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4752 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4753 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4754 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004755
Casey Schauflere114e472008-02-04 22:29:50 -08004756 /* key management security hooks */
4757#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004758 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4759 LSM_HOOK_INIT(key_free, smack_key_free),
4760 LSM_HOOK_INIT(key_permission, smack_key_permission),
4761 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004762#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004763
4764 /* Audit hooks */
4765#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004766 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4767 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4768 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004769#endif /* CONFIG_AUDIT */
4770
Casey Schauflere20b0432015-05-02 15:11:36 -07004771 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4772 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4773 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004774 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4775 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4776 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004777 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4778 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4779 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004780};
4781
Etienne Basset7198e2e2009-03-24 20:53:24 +01004782
Casey Schaufler86812bb2012-04-17 18:55:46 -07004783static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004784{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004785 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004786 * Initialize rule list locks
4787 */
4788 mutex_init(&smack_known_huh.smk_rules_lock);
4789 mutex_init(&smack_known_hat.smk_rules_lock);
4790 mutex_init(&smack_known_floor.smk_rules_lock);
4791 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004792 mutex_init(&smack_known_web.smk_rules_lock);
4793 /*
4794 * Initialize rule lists
4795 */
4796 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4797 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4798 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4799 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004800 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4801 /*
4802 * Create the known labels list
4803 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004804 smk_insert_entry(&smack_known_huh);
4805 smk_insert_entry(&smack_known_hat);
4806 smk_insert_entry(&smack_known_star);
4807 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004808 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004809}
4810
Casey Schauflere114e472008-02-04 22:29:50 -08004811/**
4812 * smack_init - initialize the smack system
4813 *
4814 * Returns 0
4815 */
4816static __init int smack_init(void)
4817{
David Howellsd84f4f92008-11-14 10:39:23 +11004818 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004819 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004820
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004821 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004822 return 0;
4823
Rohit1a5b4722014-10-15 17:40:41 +05304824 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4825 if (!smack_inode_cache)
4826 return -ENOMEM;
4827
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004828 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4829 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304830 if (tsp == NULL) {
4831 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004832 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304833 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004834
José Bollod21b7b02015-10-02 15:15:56 +02004835 smack_enabled = 1;
4836
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004837 pr_info("Smack: Initializing.\n");
4838#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4839 pr_info("Smack: Netfilter enabled.\n");
4840#endif
4841#ifdef SMACK_IPV6_PORT_LABELING
4842 pr_info("Smack: IPv6 port labeling enabled.\n");
4843#endif
4844#ifdef SMACK_IPV6_SECMARK_LABELING
4845 pr_info("Smack: IPv6 Netfilter enabled.\n");
4846#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004847
4848 /*
4849 * Set the security state for the initial task.
4850 */
David Howellsd84f4f92008-11-14 10:39:23 +11004851 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004852 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004853
Casey Schaufler86812bb2012-04-17 18:55:46 -07004854 /* initialize the smack_known_list */
4855 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004856
4857 /*
4858 * Register with LSM
4859 */
Casey Schauflerd69dece52017-01-18 17:09:05 -08004860 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
Casey Schauflere114e472008-02-04 22:29:50 -08004861
4862 return 0;
4863}
4864
4865/*
4866 * Smack requires early initialization in order to label
4867 * all processes and objects when they are created.
4868 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004869DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004870 .name = "smack",
Kees Cook3d6e5f62018-10-10 17:18:23 -07004871 .init = smack_init,
4872};