blob: 780733341d02280f5319165146036c5d0f7dbea8 [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)
Casey Schauflere114e472008-02-04 22:29:50 -0800592{
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);
Casey Schauflere114e472008-02-04 22:29:50 -0800600}
601
Al Viro55c0e5b2018-12-16 01:09:45 -0500602static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530603{
Al Viro55c0e5b2018-12-16 01:09:45 -0500604 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530605
Al Viro55c0e5b2018-12-16 01:09:45 -0500606 if (!opts) {
607 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
608 if (!opts)
609 return -ENOMEM;
610 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530611 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500612 if (!s)
613 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530614
Al Viro55c0e5b2018-12-16 01:09:45 -0500615 switch (token) {
616 case Opt_fsdefault:
617 if (opts->fsdefault)
618 goto out_opt_err;
619 opts->fsdefault = s;
620 break;
621 case Opt_fsfloor:
622 if (opts->fsfloor)
623 goto out_opt_err;
624 opts->fsfloor = s;
625 break;
626 case Opt_fshat:
627 if (opts->fshat)
628 goto out_opt_err;
629 opts->fshat = s;
630 break;
631 case Opt_fsroot:
632 if (opts->fsroot)
633 goto out_opt_err;
634 opts->fsroot = s;
635 break;
636 case Opt_fstransmute:
637 if (opts->fstransmute)
638 goto out_opt_err;
639 opts->fstransmute = s;
640 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530641 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530642 return 0;
643
644out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530645 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500646 return -EINVAL;
647}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530648
Al Virod2497e12018-12-16 01:37:06 -0500649static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530650{
Al Virod2497e12018-12-16 01:37:06 -0500651 char *from = options, *to = options;
652 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530653
Al Viroc3300aa2018-12-16 01:52:24 -0500654 while (1) {
655 char *next = strchr(from, ',');
656 int token, len, rc;
657 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530658
Al Viroc3300aa2018-12-16 01:52:24 -0500659 if (next)
660 len = next - from;
661 else
662 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530663
Al Viroc3300aa2018-12-16 01:52:24 -0500664 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500665 if (token != Opt_error) {
666 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
667 rc = smack_add_opt(token, arg, mnt_opts);
668 if (unlikely(rc)) {
669 kfree(arg);
670 if (*mnt_opts)
671 smack_free_mnt_opts(*mnt_opts);
672 *mnt_opts = NULL;
673 return rc;
674 }
675 } else {
676 if (!first) { // copy with preceding comma
677 from--;
678 len++;
679 }
680 if (to != from)
681 memmove(to, from, len);
682 to += len;
683 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530684 }
Al Viroc3300aa2018-12-16 01:52:24 -0500685 if (!from[len])
686 break;
687 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530688 }
Al Virod2497e12018-12-16 01:37:06 -0500689 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530690 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530691}
692
693/**
694 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800695 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530696 * @opts: Smack mount options
697 * @kern_flags: mount option from kernel space or user space
698 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800699 *
700 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530701 *
702 * Allow filesystems with binary mount data to explicitly set Smack mount
703 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800704 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530705static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500706 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530707 unsigned long kern_flags,
708 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800709{
710 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000711 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800712 struct superblock_smack *sp = sb->s_security;
713 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800714 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500715 struct smack_mnt_opts *opts = mnt_opts;
716 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800717
Seth Forshee9f50eda2015-09-23 15:16:06 -0500718 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800719 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700720
Himanshu Shukla2097f592016-11-10 16:19:52 +0530721 if (!smack_privileged(CAP_MAC_ADMIN)) {
722 /*
723 * Unprivileged mounts don't get to specify Smack values.
724 */
Al Viro12085b12018-12-13 15:18:05 -0500725 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530726 return -EPERM;
727 /*
728 * Unprivileged mounts get root and default from the caller.
729 */
730 skp = smk_of_current();
731 sp->smk_root = skp;
732 sp->smk_default = skp;
733 /*
734 * For a handful of fs types with no user-controlled
735 * backing store it's okay to trust security labels
736 * in the filesystem. The rest are untrusted.
737 */
738 if (sb->s_user_ns != &init_user_ns &&
739 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
740 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500741 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530742 sp->smk_flags |= SMK_SB_UNTRUSTED;
743 }
744 }
745
Seth Forshee9f50eda2015-09-23 15:16:06 -0500746 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800747
Al Viro12085b12018-12-13 15:18:05 -0500748 if (opts) {
749 if (opts->fsdefault) {
750 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200751 if (IS_ERR(skp))
752 return PTR_ERR(skp);
753 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500754 }
755 if (opts->fsfloor) {
756 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530757 if (IS_ERR(skp))
758 return PTR_ERR(skp);
759 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500760 }
761 if (opts->fshat) {
762 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530763 if (IS_ERR(skp))
764 return PTR_ERR(skp);
765 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500766 }
767 if (opts->fsroot) {
768 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200769 if (IS_ERR(skp))
770 return PTR_ERR(skp);
771 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500772 }
773 if (opts->fstransmute) {
774 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200775 if (IS_ERR(skp))
776 return PTR_ERR(skp);
777 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500778 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800779 }
780 }
781
782 /*
783 * Initialize the root inode.
784 */
785 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100786 if (isp == NULL) {
787 isp = new_inode_smack(sp->smk_root);
788 if (isp == NULL)
789 return -ENOMEM;
790 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700791 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800792 isp->smk_inode = sp->smk_root;
793
Casey Schauflere830b392013-05-22 18:43:07 -0700794 if (transmute)
795 isp->smk_flags |= SMK_INODE_TRANSMUTE;
796
Casey Schauflere114e472008-02-04 22:29:50 -0800797 return 0;
798}
799
800/**
801 * smack_sb_statfs - Smack check on statfs
802 * @dentry: identifies the file system in question
803 *
804 * Returns 0 if current can read the floor of the filesystem,
805 * and error code otherwise
806 */
807static int smack_sb_statfs(struct dentry *dentry)
808{
809 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200810 int rc;
811 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800812
Eric Parisa2694342011-04-25 13:10:27 -0400813 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200814 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
815
816 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700817 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200818 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800819}
820
Casey Schauflere114e472008-02-04 22:29:50 -0800821/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800822 * BPRM hooks
823 */
824
Casey Schauflerce8a4322011-09-29 18:21:01 -0700825/**
826 * smack_bprm_set_creds - set creds for exec
827 * @bprm: the exec information
828 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100829 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700830 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800831static int smack_bprm_set_creds(struct linux_binprm *bprm)
832{
Al Viro496ad9a2013-01-23 17:07:38 -0500833 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300834 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800835 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500836 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800837 int rc;
838
Kees Cookddb4a142017-07-18 15:25:23 -0700839 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800840 return 0;
841
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300842 isp = inode->i_security;
843 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800844 return 0;
845
Seth Forshee809c02e2016-04-26 14:36:22 -0500846 sbsp = inode->i_sb->s_security;
847 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
848 isp->smk_task != sbsp->smk_root)
849 return 0;
850
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300851 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100852 struct task_struct *tracer;
853 rc = 0;
854
855 rcu_read_lock();
856 tracer = ptrace_parent(current);
857 if (likely(tracer != NULL))
858 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200859 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100860 PTRACE_MODE_ATTACH,
861 __func__);
862 rcu_read_unlock();
863
864 if (rc != 0)
865 return rc;
866 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300867 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800868
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300869 bsp->smk_task = isp->smk_task;
870 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800871
Kees Cookccbb6e12017-07-18 15:25:26 -0700872 /* Decide if this is a secure exec. */
873 if (bsp->smk_task != bsp->smk_forked)
874 bprm->secureexec = 1;
875
Casey Schaufler676dac42010-12-02 06:43:39 -0800876 return 0;
877}
878
879/*
Casey Schauflere114e472008-02-04 22:29:50 -0800880 * Inode hooks
881 */
882
883/**
884 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800885 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800886 *
887 * Returns 0 if it gets a blob, -ENOMEM otherwise
888 */
889static int smack_inode_alloc_security(struct inode *inode)
890{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700891 struct smack_known *skp = smk_of_current();
892
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200893 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800894 if (inode->i_security == NULL)
895 return -ENOMEM;
896 return 0;
897}
898
899/**
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530900 * smack_inode_free_rcu - Free inode_smack blob from cache
901 * @head: the rcu_head for getting inode_smack pointer
902 *
903 * Call back function called from call_rcu() to free
904 * the i_security blob pointer in inode
905 */
906static void smack_inode_free_rcu(struct rcu_head *head)
907{
908 struct inode_smack *issp;
909
910 issp = container_of(head, struct inode_smack, smk_rcu);
911 kmem_cache_free(smack_inode_cache, issp);
912}
913
914/**
915 * smack_inode_free_security - free an inode blob using call_rcu()
Randy Dunlap251a2a92009-02-18 11:42:33 -0800916 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800917 *
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530918 * Clears the blob pointer in inode using RCU
Casey Schauflere114e472008-02-04 22:29:50 -0800919 */
920static void smack_inode_free_security(struct inode *inode)
921{
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530922 struct inode_smack *issp = inode->i_security;
923
924 /*
925 * The inode may still be referenced in a path walk and
926 * a call to smack_inode_permission() can be made
927 * after smack_inode_free_security() is called.
928 * To avoid race condition free the i_security via RCU
929 * and leave the current inode->i_security pointer intact.
930 * The inode will be freed after the RCU grace period too.
931 */
932 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
Casey Schauflere114e472008-02-04 22:29:50 -0800933}
934
935/**
936 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200937 * @inode: the newly created inode
938 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500939 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800940 * @name: where to put the attribute name
941 * @value: where to put the attribute value
942 * @len: where to put the length of the attribute
943 *
944 * Returns 0 if it all works out, -ENOMEM if there's no memory
945 */
946static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900947 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500948 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800949{
Casey Schaufler2267b132012-03-13 19:14:19 -0700950 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700951 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200952 struct smack_known *isp = smk_of_inode(inode);
953 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800954 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800955
Tetsuo Handa95489062013-07-25 05:44:02 +0900956 if (name)
957 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800958
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100959 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800960 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200961 may = smk_access_entry(skp->smk_known, dsp->smk_known,
962 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800963 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200964
965 /*
966 * If the access rule allows transmutation and
967 * the directory requests transmutation then
968 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700969 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200970 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800971 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700972 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200973 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700974 issp->smk_flags |= SMK_INODE_CHANGED;
975 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200976
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200977 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800978 if (*value == NULL)
979 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800980
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200981 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100982 }
Casey Schauflere114e472008-02-04 22:29:50 -0800983
984 return 0;
985}
986
987/**
988 * smack_inode_link - Smack check on link
989 * @old_dentry: the existing object
990 * @dir: unused
991 * @new_dentry: the new object
992 *
993 * Returns 0 if access is permitted, an error code otherwise
994 */
995static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
996 struct dentry *new_dentry)
997{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200998 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200999 struct smk_audit_info ad;
1000 int rc;
1001
Eric Parisa2694342011-04-25 13:10:27 -04001002 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001003 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001004
David Howellsc6f493d2015-03-17 22:26:22 +00001005 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001006 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001007 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001008
David Howells88025652015-01-29 12:02:32 +00001009 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001010 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001011 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1012 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001013 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001014 }
1015
1016 return rc;
1017}
1018
1019/**
1020 * smack_inode_unlink - Smack check on inode deletion
1021 * @dir: containing directory object
1022 * @dentry: file to unlink
1023 *
1024 * Returns 0 if current can write the containing directory
1025 * and the object, error code otherwise
1026 */
1027static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1028{
David Howellsc6f493d2015-03-17 22:26:22 +00001029 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001030 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001031 int rc;
1032
Eric Parisa2694342011-04-25 13:10:27 -04001033 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001034 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1035
Casey Schauflere114e472008-02-04 22:29:50 -08001036 /*
1037 * You need write access to the thing you're unlinking
1038 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001039 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001040 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001041 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001042 /*
1043 * You also need write access to the containing directory
1044 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001045 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001046 smk_ad_setfield_u_fs_inode(&ad, dir);
1047 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001048 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001049 }
Casey Schauflere114e472008-02-04 22:29:50 -08001050 return rc;
1051}
1052
1053/**
1054 * smack_inode_rmdir - Smack check on directory deletion
1055 * @dir: containing directory object
1056 * @dentry: directory to unlink
1057 *
1058 * Returns 0 if current can write the containing directory
1059 * and the directory, error code otherwise
1060 */
1061static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1062{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001063 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001064 int rc;
1065
Eric Parisa2694342011-04-25 13:10:27 -04001066 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001067 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1068
Casey Schauflere114e472008-02-04 22:29:50 -08001069 /*
1070 * You need write access to the thing you're removing
1071 */
David Howellsc6f493d2015-03-17 22:26:22 +00001072 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1073 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001074 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001075 /*
1076 * You also need write access to the containing directory
1077 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001078 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001079 smk_ad_setfield_u_fs_inode(&ad, dir);
1080 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001081 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001082 }
Casey Schauflere114e472008-02-04 22:29:50 -08001083
1084 return rc;
1085}
1086
1087/**
1088 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001089 * @old_inode: unused
1090 * @old_dentry: the old object
1091 * @new_inode: unused
1092 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001093 *
1094 * Read and write access is required on both the old and
1095 * new directories.
1096 *
1097 * Returns 0 if access is permitted, an error code otherwise
1098 */
1099static int smack_inode_rename(struct inode *old_inode,
1100 struct dentry *old_dentry,
1101 struct inode *new_inode,
1102 struct dentry *new_dentry)
1103{
1104 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001105 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 struct smk_audit_info ad;
1107
Eric Parisa2694342011-04-25 13:10:27 -04001108 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001109 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001110
David Howellsc6f493d2015-03-17 22:26:22 +00001111 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001112 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001113 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001114
David Howells88025652015-01-29 12:02:32 +00001115 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001116 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001117 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1118 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001119 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001120 }
Casey Schauflere114e472008-02-04 22:29:50 -08001121 return rc;
1122}
1123
1124/**
1125 * smack_inode_permission - Smack version of permission()
1126 * @inode: the inode in question
1127 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001128 *
1129 * This is the important Smack hook.
1130 *
1131 * Returns 0 if access is permitted, -EACCES otherwise
1132 */
Al Viroe74f71e2011-06-20 19:38:15 -04001133static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001134{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001135 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001136 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001137 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001138 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001139
1140 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001141 /*
1142 * No permission to check. Existence test. Yup, it's there.
1143 */
1144 if (mask == 0)
1145 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001146
Seth Forshee9f50eda2015-09-23 15:16:06 -05001147 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1148 if (smk_of_inode(inode) != sbsp->smk_root)
1149 return -EACCES;
1150 }
1151
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001152 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001153 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001154 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001155 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001156 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001157 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1158 rc = smk_bu_inode(inode, mask, rc);
1159 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001160}
1161
1162/**
1163 * smack_inode_setattr - Smack check for setting attributes
1164 * @dentry: the object
1165 * @iattr: for the force flag
1166 *
1167 * Returns 0 if access is permitted, an error code otherwise
1168 */
1169static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1170{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001172 int rc;
1173
Casey Schauflere114e472008-02-04 22:29:50 -08001174 /*
1175 * Need to allow for clearing the setuid bit.
1176 */
1177 if (iattr->ia_valid & ATTR_FORCE)
1178 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001179 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001180 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001181
David Howellsc6f493d2015-03-17 22:26:22 +00001182 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1183 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001184 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001185}
1186
1187/**
1188 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001189 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001190 * @dentry: the object
1191 *
1192 * Returns 0 if access is permitted, an error code otherwise
1193 */
Al Viro3f7036a2015-03-08 19:28:30 -04001194static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001195{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001196 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001197 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001198 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001199
Eric Parisf48b7392011-04-25 12:54:27 -04001200 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001201 smk_ad_setfield_u_fs_path(&ad, *path);
1202 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1203 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001204 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001205}
1206
1207/**
1208 * smack_inode_setxattr - Smack check for setting xattrs
1209 * @dentry: the object
1210 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001211 * @value: value of the attribute
1212 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001213 * @flags: unused
1214 *
1215 * This protects the Smack attribute explicitly.
1216 *
1217 * Returns 0 if access is permitted, an error code otherwise
1218 */
David Howells8f0cfa52008-04-29 00:59:41 -07001219static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1220 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001221{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001222 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001223 struct smack_known *skp;
1224 int check_priv = 0;
1225 int check_import = 0;
1226 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001227 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001228
Casey Schaufler19760ad2013-12-16 16:27:26 -08001229 /*
1230 * Check label validity here so import won't fail in post_setxattr
1231 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001232 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1233 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001234 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1235 check_priv = 1;
1236 check_import = 1;
1237 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1238 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1239 check_priv = 1;
1240 check_import = 1;
1241 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001242 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001243 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001244 if (size != TRANS_TRUE_SIZE ||
1245 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1246 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001247 } else
1248 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1249
Casey Schaufler19760ad2013-12-16 16:27:26 -08001250 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1251 rc = -EPERM;
1252
1253 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001254 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001255 if (IS_ERR(skp))
1256 rc = PTR_ERR(skp);
1257 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001258 (skp == &smack_known_star || skp == &smack_known_web)))
1259 rc = -EINVAL;
1260 }
1261
Eric Parisa2694342011-04-25 13:10:27 -04001262 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001263 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1264
Casey Schauflerd166c802014-08-27 14:51:27 -07001265 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001266 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1267 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001268 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001269
1270 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001271}
1272
1273/**
1274 * smack_inode_post_setxattr - Apply the Smack update approved above
1275 * @dentry: object
1276 * @name: attribute name
1277 * @value: attribute value
1278 * @size: attribute size
1279 * @flags: unused
1280 *
1281 * Set the pointer in the inode blob to the entry found
1282 * in the master label list.
1283 */
David Howells8f0cfa52008-04-29 00:59:41 -07001284static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1285 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001286{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001287 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001288 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001289
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001290 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1291 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1292 return;
1293 }
1294
Casey Schaufler676dac42010-12-02 06:43:39 -08001295 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001296 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001297 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001298 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001299 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001300 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001301 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001302 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001303 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001304 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001305 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001306 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001307 }
Casey Schauflere114e472008-02-04 22:29:50 -08001308
1309 return;
1310}
1311
Casey Schauflerce8a4322011-09-29 18:21:01 -07001312/**
Casey Schauflere114e472008-02-04 22:29:50 -08001313 * smack_inode_getxattr - Smack check on getxattr
1314 * @dentry: the object
1315 * @name: unused
1316 *
1317 * Returns 0 if access is permitted, an error code otherwise
1318 */
David Howells8f0cfa52008-04-29 00:59:41 -07001319static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001320{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001321 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001322 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001323
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
David Howellsc6f493d2015-03-17 22:26:22 +00001327 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1328 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001329 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001330}
1331
Casey Schauflerce8a4322011-09-29 18:21:01 -07001332/**
Casey Schauflere114e472008-02-04 22:29:50 -08001333 * smack_inode_removexattr - Smack check on removexattr
1334 * @dentry: the object
1335 * @name: name of the attribute
1336 *
1337 * Removing the Smack attribute requires CAP_MAC_ADMIN
1338 *
1339 * Returns 0 if access is permitted, an error code otherwise
1340 */
David Howells8f0cfa52008-04-29 00:59:41 -07001341static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001342{
Casey Schaufler676dac42010-12-02 06:43:39 -08001343 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001344 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001345 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001346
Casey Schauflerbcdca222008-02-23 15:24:04 -08001347 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1348 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001349 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001350 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001351 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301352 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001353 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001354 rc = -EPERM;
1355 } else
1356 rc = cap_inode_removexattr(dentry, name);
1357
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001358 if (rc != 0)
1359 return rc;
1360
Eric Parisa2694342011-04-25 13:10:27 -04001361 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001362 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001363
David Howellsc6f493d2015-03-17 22:26:22 +00001364 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1365 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001366 if (rc != 0)
1367 return rc;
1368
David Howellsc6f493d2015-03-17 22:26:22 +00001369 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001370 /*
1371 * Don't do anything special for these.
1372 * XATTR_NAME_SMACKIPIN
1373 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001374 */
José Bollo80124952016-01-12 21:23:40 +01001375 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001376 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001377 struct superblock_smack *sbsp = sbp->s_security;
1378
1379 isp->smk_inode = sbsp->smk_default;
1380 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001381 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001382 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001383 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001384 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1385 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001386
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001387 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001388}
1389
1390/**
1391 * smack_inode_getsecurity - get smack xattrs
1392 * @inode: the object
1393 * @name: attribute name
1394 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001395 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001396 *
1397 * Returns the size of the attribute or an error code
1398 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001399static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001400 const char *name, void **buffer,
1401 bool alloc)
1402{
1403 struct socket_smack *ssp;
1404 struct socket *sock;
1405 struct super_block *sbp;
1406 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001407 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001408
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001409 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001410 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001411 else {
1412 /*
1413 * The rest of the Smack xattrs are only on sockets.
1414 */
1415 sbp = ip->i_sb;
1416 if (sbp->s_magic != SOCKFS_MAGIC)
1417 return -EOPNOTSUPP;
1418
1419 sock = SOCKET_I(ip);
1420 if (sock == NULL || sock->sk == NULL)
1421 return -EOPNOTSUPP;
1422
1423 ssp = sock->sk->sk_security;
1424
1425 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1426 isp = ssp->smk_in;
1427 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1428 isp = ssp->smk_out;
1429 else
1430 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001431 }
1432
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001433 if (alloc) {
1434 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1435 if (*buffer == NULL)
1436 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001437 }
1438
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001439 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001440}
1441
1442
1443/**
1444 * smack_inode_listsecurity - list the Smack attributes
1445 * @inode: the object
1446 * @buffer: where they go
1447 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001448 */
1449static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1450 size_t buffer_size)
1451{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001452 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001453
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001454 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001455 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001456
1457 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001458}
1459
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001460/**
1461 * smack_inode_getsecid - Extract inode's security id
1462 * @inode: inode to extract the info from
1463 * @secid: where result will be saved
1464 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001465static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001466{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001467 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001468
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001469 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001470}
1471
Casey Schauflere114e472008-02-04 22:29:50 -08001472/*
1473 * File Hooks
1474 */
1475
Casey Schaufler491a0b02016-01-26 15:08:35 -08001476/*
1477 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001478 *
1479 * Should access checks be done on each read or write?
1480 * UNICOS and SELinux say yes.
1481 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1482 *
1483 * I'll say no for now. Smack does not do the frequent
1484 * label changing that SELinux does.
1485 */
Casey Schauflere114e472008-02-04 22:29:50 -08001486
1487/**
1488 * smack_file_alloc_security - assign a file security blob
1489 * @file: the object
1490 *
1491 * The security blob for a file is a pointer to the master
1492 * label list, so no allocation is done.
1493 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001494 * f_security is the owner security information. It
1495 * isn't used on file access checks, it's for send_sigio.
1496 *
Casey Schauflere114e472008-02-04 22:29:50 -08001497 * Returns 0
1498 */
1499static int smack_file_alloc_security(struct file *file)
1500{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001501 struct smack_known *skp = smk_of_current();
1502
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001503 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001504 return 0;
1505}
1506
1507/**
1508 * smack_file_free_security - clear a file security blob
1509 * @file: the object
1510 *
1511 * The security blob for a file is a pointer to the master
1512 * label list, so no memory is freed.
1513 */
1514static void smack_file_free_security(struct file *file)
1515{
1516 file->f_security = NULL;
1517}
1518
1519/**
1520 * smack_file_ioctl - Smack check on ioctls
1521 * @file: the object
1522 * @cmd: what to do
1523 * @arg: unused
1524 *
1525 * Relies heavily on the correct use of the ioctl command conventions.
1526 *
1527 * Returns 0 if allowed, error code otherwise
1528 */
1529static int smack_file_ioctl(struct file *file, unsigned int cmd,
1530 unsigned long arg)
1531{
1532 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001533 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001534 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001535
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001536 if (unlikely(IS_PRIVATE(inode)))
1537 return 0;
1538
Eric Parisf48b7392011-04-25 12:54:27 -04001539 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001540 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001541
Casey Schauflerd166c802014-08-27 14:51:27 -07001542 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001543 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001544 rc = smk_bu_file(file, MAY_WRITE, rc);
1545 }
Casey Schauflere114e472008-02-04 22:29:50 -08001546
Casey Schauflerd166c802014-08-27 14:51:27 -07001547 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001548 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001549 rc = smk_bu_file(file, MAY_READ, rc);
1550 }
Casey Schauflere114e472008-02-04 22:29:50 -08001551
1552 return rc;
1553}
1554
1555/**
1556 * smack_file_lock - Smack check on file locking
1557 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001558 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001559 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001560 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001561 */
1562static int smack_file_lock(struct file *file, unsigned int cmd)
1563{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001564 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001565 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001566 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001567
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001568 if (unlikely(IS_PRIVATE(inode)))
1569 return 0;
1570
Eric Paris92f42502011-04-25 13:15:55 -04001571 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1572 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001573 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001574 rc = smk_bu_file(file, MAY_LOCK, rc);
1575 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001576}
1577
1578/**
1579 * smack_file_fcntl - Smack check on fcntl
1580 * @file: the object
1581 * @cmd: what action to check
1582 * @arg: unused
1583 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001584 * Generally these operations are harmless.
1585 * File locking operations present an obvious mechanism
1586 * for passing information, so they require write access.
1587 *
Casey Schauflere114e472008-02-04 22:29:50 -08001588 * Returns 0 if current has access, error code otherwise
1589 */
1590static int smack_file_fcntl(struct file *file, unsigned int cmd,
1591 unsigned long arg)
1592{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001593 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001594 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001595 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001596
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001597 if (unlikely(IS_PRIVATE(inode)))
1598 return 0;
1599
Casey Schauflere114e472008-02-04 22:29:50 -08001600 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001601 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001602 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001603 case F_SETLK:
1604 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001605 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1606 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001607 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001608 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001609 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001610 case F_SETOWN:
1611 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001612 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1613 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001614 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001615 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001616 break;
1617 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001618 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001619 }
1620
1621 return rc;
1622}
1623
1624/**
Al Viroe5467852012-05-30 13:30:51 -04001625 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001626 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1627 * if mapping anonymous memory.
1628 * @file contains the file structure for file to map (may be NULL).
1629 * @reqprot contains the protection requested by the application.
1630 * @prot contains the protection that will be applied by the kernel.
1631 * @flags contains the operational flags.
1632 * Return 0 if permission is granted.
1633 */
Al Viroe5467852012-05-30 13:30:51 -04001634static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001635 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001636 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001637{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001638 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001639 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001640 struct smack_rule *srp;
1641 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001642 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001643 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001644 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001645 int may;
1646 int mmay;
1647 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001648 int rc;
1649
Al Viro496ad9a2013-01-23 17:07:38 -05001650 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001651 return 0;
1652
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001653 if (unlikely(IS_PRIVATE(file_inode(file))))
1654 return 0;
1655
Al Viro496ad9a2013-01-23 17:07:38 -05001656 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001657 if (isp->smk_mmap == NULL)
1658 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001659 sbsp = file_inode(file)->i_sb->s_security;
1660 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1661 isp->smk_mmap != sbsp->smk_root)
1662 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001663 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001664
1665 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001666 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001667 rc = 0;
1668
1669 rcu_read_lock();
1670 /*
1671 * For each Smack rule associated with the subject
1672 * label verify that the SMACK64MMAP also has access
1673 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001674 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001675 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001676 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001677 /*
1678 * Matching labels always allows access.
1679 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001680 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001681 continue;
1682 /*
1683 * If there is a matching local rule take
1684 * that into account as well.
1685 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001686 may = smk_access_entry(srp->smk_subject->smk_known,
1687 okp->smk_known,
1688 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001689 if (may == -ENOENT)
1690 may = srp->smk_access;
1691 else
1692 may &= srp->smk_access;
1693 /*
1694 * If may is zero the SMACK64MMAP subject can't
1695 * possibly have less access.
1696 */
1697 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001698 continue;
1699
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001700 /*
1701 * Fetch the global list entry.
1702 * If there isn't one a SMACK64MMAP subject
1703 * can't have as much access as current.
1704 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001705 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1706 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001707 if (mmay == -ENOENT) {
1708 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001709 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001710 }
1711 /*
1712 * If there is a local entry it modifies the
1713 * potential access, too.
1714 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001715 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1716 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001717 if (tmay != -ENOENT)
1718 mmay &= tmay;
1719
1720 /*
1721 * If there is any access available to current that is
1722 * not available to a SMACK64MMAP subject
1723 * deny access.
1724 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001725 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001726 rc = -EACCES;
1727 break;
1728 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001729 }
1730
1731 rcu_read_unlock();
1732
1733 return rc;
1734}
1735
1736/**
Casey Schauflere114e472008-02-04 22:29:50 -08001737 * smack_file_set_fowner - set the file security blob value
1738 * @file: object in question
1739 *
Casey Schauflere114e472008-02-04 22:29:50 -08001740 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001741static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001742{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001743 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001744}
1745
1746/**
1747 * smack_file_send_sigiotask - Smack on sigio
1748 * @tsk: The target task
1749 * @fown: the object the signal come from
1750 * @signum: unused
1751 *
1752 * Allow a privileged task to get signals even if it shouldn't
1753 *
1754 * Returns 0 if a subject with the object's smack could
1755 * write to the task, an error code otherwise.
1756 */
1757static int smack_file_send_sigiotask(struct task_struct *tsk,
1758 struct fown_struct *fown, int signum)
1759{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001760 struct smack_known *skp;
1761 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001762 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001763 struct file *file;
1764 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001765 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001766
1767 /*
1768 * struct fown_struct is never outside the context of a struct file
1769 */
1770 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001771
Etienne Bassetecfcc532009-04-08 20:40:06 +02001772 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001773 skp = file->f_security;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001774 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1775 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001776
1777 rcu_read_lock();
1778 tcred = __task_cred(tsk);
1779 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001780 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001781 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001782
1783 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1784 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001785 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001786 return rc;
1787}
1788
1789/**
1790 * smack_file_receive - Smack file receive check
1791 * @file: the object
1792 *
1793 * Returns 0 if current has access, error code otherwise
1794 */
1795static int smack_file_receive(struct file *file)
1796{
Casey Schauflerd166c802014-08-27 14:51:27 -07001797 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001798 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001799 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001800 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001801 struct socket *sock;
1802 struct task_smack *tsp;
1803 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001804
Seung-Woo Kim97775822015-04-17 15:25:04 +09001805 if (unlikely(IS_PRIVATE(inode)))
1806 return 0;
1807
Casey Schaufler4482a442013-12-30 17:37:45 -08001808 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001809 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001810
Casey Schaufler51d59af2017-05-31 08:53:42 -07001811 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001812 sock = SOCKET_I(inode);
1813 ssp = sock->sk->sk_security;
1814 tsp = current_security();
1815 /*
1816 * If the receiving process can't write to the
1817 * passed socket or if the passed socket can't
1818 * write to the receiving process don't accept
1819 * the passed socket.
1820 */
1821 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1822 rc = smk_bu_file(file, may, rc);
1823 if (rc < 0)
1824 return rc;
1825 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1826 rc = smk_bu_file(file, may, rc);
1827 return rc;
1828 }
Casey Schauflere114e472008-02-04 22:29:50 -08001829 /*
1830 * This code relies on bitmasks.
1831 */
1832 if (file->f_mode & FMODE_READ)
1833 may = MAY_READ;
1834 if (file->f_mode & FMODE_WRITE)
1835 may |= MAY_WRITE;
1836
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001837 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001838 rc = smk_bu_file(file, may, rc);
1839 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001840}
1841
Casey Schaufler531f1d42011-09-19 12:41:42 -07001842/**
Eric Paris83d49852012-04-04 13:45:40 -04001843 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001844 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001845 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001846 *
1847 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001848 * Allow the open only if the task has read access. There are
1849 * many read operations (e.g. fstat) that you can do with an
1850 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001851 *
1852 * Returns 0
1853 */
Al Viro94817692018-07-10 14:13:18 -04001854static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001855{
Al Viro94817692018-07-10 14:13:18 -04001856 struct task_smack *tsp = file->f_cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001857 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001858 struct smk_audit_info ad;
1859 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001860
Casey Schauflera6834c02014-04-21 11:10:26 -07001861 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1862 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301863 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001864 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001865
1866 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001867}
1868
Casey Schauflere114e472008-02-04 22:29:50 -08001869/*
1870 * Task hooks
1871 */
1872
1873/**
David Howellsee18d642009-09-02 09:14:21 +01001874 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1875 * @new: the new credentials
1876 * @gfp: the atomicity of any memory allocations
1877 *
1878 * Prepare a blank set of credentials for modification. This must allocate all
1879 * the memory the LSM module might require such that cred_transfer() can
1880 * complete without error.
1881 */
1882static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1883{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001884 struct task_smack *tsp;
1885
1886 tsp = new_task_smack(NULL, NULL, gfp);
1887 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001888 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001889
1890 cred->security = tsp;
1891
David Howellsee18d642009-09-02 09:14:21 +01001892 return 0;
1893}
1894
1895
1896/**
David Howellsf1752ee2008-11-14 10:39:17 +11001897 * smack_cred_free - "free" task-level security credentials
1898 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001899 *
Casey Schauflere114e472008-02-04 22:29:50 -08001900 */
David Howellsf1752ee2008-11-14 10:39:17 +11001901static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001902{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001903 struct task_smack *tsp = cred->security;
1904 struct smack_rule *rp;
1905 struct list_head *l;
1906 struct list_head *n;
1907
1908 if (tsp == NULL)
1909 return;
1910 cred->security = NULL;
1911
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001912 smk_destroy_label_list(&tsp->smk_relabel);
1913
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001914 list_for_each_safe(l, n, &tsp->smk_rules) {
1915 rp = list_entry(l, struct smack_rule, list);
1916 list_del(&rp->list);
1917 kfree(rp);
1918 }
1919 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001920}
1921
1922/**
David Howellsd84f4f92008-11-14 10:39:23 +11001923 * smack_cred_prepare - prepare new set of credentials for modification
1924 * @new: the new credentials
1925 * @old: the original credentials
1926 * @gfp: the atomicity of any memory allocations
1927 *
1928 * Prepare a new set of credentials for modification.
1929 */
1930static int smack_cred_prepare(struct cred *new, const struct cred *old,
1931 gfp_t gfp)
1932{
Casey Schaufler676dac42010-12-02 06:43:39 -08001933 struct task_smack *old_tsp = old->security;
1934 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001935 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001936
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001937 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001938 if (new_tsp == NULL)
1939 return -ENOMEM;
1940
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301941 new->security = new_tsp;
1942
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001943 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1944 if (rc != 0)
1945 return rc;
1946
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001947 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1948 gfp);
1949 if (rc != 0)
1950 return rc;
1951
David Howellsd84f4f92008-11-14 10:39:23 +11001952 return 0;
1953}
1954
Randy Dunlap251a2a92009-02-18 11:42:33 -08001955/**
David Howellsee18d642009-09-02 09:14:21 +01001956 * smack_cred_transfer - Transfer the old credentials to the new credentials
1957 * @new: the new credentials
1958 * @old: the original credentials
1959 *
1960 * Fill in a set of blank credentials from another set of credentials.
1961 */
1962static void smack_cred_transfer(struct cred *new, const struct cred *old)
1963{
Casey Schaufler676dac42010-12-02 06:43:39 -08001964 struct task_smack *old_tsp = old->security;
1965 struct task_smack *new_tsp = new->security;
1966
1967 new_tsp->smk_task = old_tsp->smk_task;
1968 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001969 mutex_init(&new_tsp->smk_rules_lock);
1970 INIT_LIST_HEAD(&new_tsp->smk_rules);
1971
1972
1973 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001974}
1975
1976/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001977 * smack_cred_getsecid - get the secid corresponding to a creds structure
1978 * @c: the object creds
1979 * @secid: where to put the result
1980 *
1981 * Sets the secid to contain a u32 version of the smack label.
1982 */
1983static void smack_cred_getsecid(const struct cred *c, u32 *secid)
1984{
1985 struct smack_known *skp;
1986
1987 rcu_read_lock();
1988 skp = smk_of_task(c->security);
1989 *secid = skp->smk_secid;
1990 rcu_read_unlock();
1991}
1992
1993/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001994 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001995 * @new: points to the set of credentials to be modified.
1996 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001997 *
1998 * Set the security data for a kernel service.
1999 */
2000static int smack_kernel_act_as(struct cred *new, u32 secid)
2001{
Casey Schaufler676dac42010-12-02 06:43:39 -08002002 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002003
Casey Schaufler152f91d2016-11-14 09:38:15 -08002004 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002005 return 0;
2006}
2007
2008/**
2009 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002010 * @new: points to the set of credentials to be modified
2011 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002012 *
2013 * Set the file creation context in a set of credentials to the same
2014 * as the objective context of the specified inode
2015 */
2016static int smack_kernel_create_files_as(struct cred *new,
2017 struct inode *inode)
2018{
2019 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002020 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002021
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002022 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002023 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002024 return 0;
2025}
2026
2027/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002028 * smk_curacc_on_task - helper to log task related access
2029 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002030 * @access: the access requested
2031 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002032 *
2033 * Return 0 if access is permitted
2034 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002035static int smk_curacc_on_task(struct task_struct *p, int access,
2036 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002037{
2038 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002039 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002040 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002041
Casey Schaufler531f1d42011-09-19 12:41:42 -07002042 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002043 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002044 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002045 rc = smk_bu_task(p, access, rc);
2046 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002047}
2048
2049/**
Casey Schauflere114e472008-02-04 22:29:50 -08002050 * smack_task_setpgid - Smack check on setting pgid
2051 * @p: the task object
2052 * @pgid: unused
2053 *
2054 * Return 0 if write access is permitted
2055 */
2056static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2057{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002058 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002059}
2060
2061/**
2062 * smack_task_getpgid - Smack access check for getpgid
2063 * @p: the object task
2064 *
2065 * Returns 0 if current can read the object task, error code otherwise
2066 */
2067static int smack_task_getpgid(struct task_struct *p)
2068{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002069 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002070}
2071
2072/**
2073 * smack_task_getsid - Smack access check for getsid
2074 * @p: the object task
2075 *
2076 * Returns 0 if current can read the object task, error code otherwise
2077 */
2078static int smack_task_getsid(struct task_struct *p)
2079{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002080 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002081}
2082
2083/**
2084 * smack_task_getsecid - get the secid of the task
2085 * @p: the object task
2086 * @secid: where to put the result
2087 *
2088 * Sets the secid to contain a u32 version of the smack label.
2089 */
2090static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2091{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002092 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002093
2094 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002095}
2096
2097/**
2098 * smack_task_setnice - Smack check on setting nice
2099 * @p: the task object
2100 * @nice: unused
2101 *
2102 * Return 0 if write access is permitted
2103 */
2104static int smack_task_setnice(struct task_struct *p, int nice)
2105{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002106 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002107}
2108
2109/**
2110 * smack_task_setioprio - Smack check on setting ioprio
2111 * @p: the task object
2112 * @ioprio: unused
2113 *
2114 * Return 0 if write access is permitted
2115 */
2116static int smack_task_setioprio(struct task_struct *p, int ioprio)
2117{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002118 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002119}
2120
2121/**
2122 * smack_task_getioprio - Smack check on reading ioprio
2123 * @p: the task object
2124 *
2125 * Return 0 if read access is permitted
2126 */
2127static int smack_task_getioprio(struct task_struct *p)
2128{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002129 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002130}
2131
2132/**
2133 * smack_task_setscheduler - Smack check on setting scheduler
2134 * @p: the task object
2135 * @policy: unused
2136 * @lp: unused
2137 *
2138 * Return 0 if read access is permitted
2139 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002140static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002141{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002142 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002143}
2144
2145/**
2146 * smack_task_getscheduler - Smack check on reading scheduler
2147 * @p: the task object
2148 *
2149 * Return 0 if read access is permitted
2150 */
2151static int smack_task_getscheduler(struct task_struct *p)
2152{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002153 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002154}
2155
2156/**
2157 * smack_task_movememory - Smack check on moving memory
2158 * @p: the task object
2159 *
2160 * Return 0 if write access is permitted
2161 */
2162static int smack_task_movememory(struct task_struct *p)
2163{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002164 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002165}
2166
2167/**
2168 * smack_task_kill - Smack check on signal delivery
2169 * @p: the task object
2170 * @info: unused
2171 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002172 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002173 *
2174 * Return 0 if write access is permitted
2175 *
Casey Schauflere114e472008-02-04 22:29:50 -08002176 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002177static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002178 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002179{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002180 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002181 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002182 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002183 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002184
Rafal Krypa18d872f2016-04-04 11:14:53 +02002185 if (!sig)
2186 return 0; /* null signal; existence test */
2187
Etienne Bassetecfcc532009-04-08 20:40:06 +02002188 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2189 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002190 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002191 * Sending a signal requires that the sender
2192 * can write the receiver.
2193 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002194 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002195 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2196 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002197 return rc;
2198 }
Casey Schauflere114e472008-02-04 22:29:50 -08002199 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002200 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002201 * specific behavior. This is not clean. For one thing
2202 * we can't take privilege into account.
2203 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002204 skp = smk_of_task(cred->security);
Casey Schauflerc60b9062016-08-30 10:31:39 -07002205 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2206 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002207 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002208}
2209
2210/**
Casey Schauflere114e472008-02-04 22:29:50 -08002211 * smack_task_to_inode - copy task smack into the inode blob
2212 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002213 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002214 *
2215 * Sets the smack pointer in the inode security blob
2216 */
2217static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2218{
2219 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002220 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002221
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002222 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002223 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002224}
2225
2226/*
2227 * Socket hooks.
2228 */
2229
2230/**
2231 * smack_sk_alloc_security - Allocate a socket blob
2232 * @sk: the socket
2233 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002234 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002235 *
2236 * Assign Smack pointers to current
2237 *
2238 * Returns 0 on success, -ENOMEM is there's no memory
2239 */
2240static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2241{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002242 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002243 struct socket_smack *ssp;
2244
2245 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2246 if (ssp == NULL)
2247 return -ENOMEM;
2248
jooseong lee08382c92016-11-03 11:54:39 +01002249 /*
2250 * Sockets created by kernel threads receive web label.
2251 */
2252 if (unlikely(current->flags & PF_KTHREAD)) {
2253 ssp->smk_in = &smack_known_web;
2254 ssp->smk_out = &smack_known_web;
2255 } else {
2256 ssp->smk_in = skp;
2257 ssp->smk_out = skp;
2258 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002259 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002260
2261 sk->sk_security = ssp;
2262
2263 return 0;
2264}
2265
2266/**
2267 * smack_sk_free_security - Free a socket blob
2268 * @sk: the socket
2269 *
2270 * Clears the blob pointer
2271 */
2272static void smack_sk_free_security(struct sock *sk)
2273{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302274#ifdef SMACK_IPV6_PORT_LABELING
2275 struct smk_port_label *spp;
2276
2277 if (sk->sk_family == PF_INET6) {
2278 rcu_read_lock();
2279 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2280 if (spp->smk_sock != sk)
2281 continue;
2282 spp->smk_can_reuse = 1;
2283 break;
2284 }
2285 rcu_read_unlock();
2286 }
2287#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002288 kfree(sk->sk_security);
2289}
2290
2291/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002292* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002293* @sip: the object end
2294*
2295* looks for host based access restrictions
2296*
2297* This version will only be appropriate for really small sets of single label
2298* hosts. The caller is responsible for ensuring that the RCU read lock is
2299* taken before calling this function.
2300*
2301* Returns the label of the far end or NULL if it's not special.
2302*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002303static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002304{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002305 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002306 struct in_addr *siap = &sip->sin_addr;
2307
2308 if (siap->s_addr == 0)
2309 return NULL;
2310
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002311 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2312 /*
2313 * we break after finding the first match because
2314 * the list is sorted from longest to shortest mask
2315 * so we have found the most specific match
2316 */
2317 if (snp->smk_host.s_addr ==
2318 (siap->s_addr & snp->smk_mask.s_addr))
2319 return snp->smk_label;
2320
2321 return NULL;
2322}
2323
2324#if IS_ENABLED(CONFIG_IPV6)
2325/*
2326 * smk_ipv6_localhost - Check for local ipv6 host address
2327 * @sip: the address
2328 *
2329 * Returns boolean true if this is the localhost address
2330 */
2331static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2332{
2333 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2334 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2335
2336 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2337 ntohs(be16p[7]) == 1)
2338 return true;
2339 return false;
2340}
2341
2342/**
2343* smack_ipv6host_label - check host based restrictions
2344* @sip: the object end
2345*
2346* looks for host based access restrictions
2347*
2348* This version will only be appropriate for really small sets of single label
2349* hosts. The caller is responsible for ensuring that the RCU read lock is
2350* taken before calling this function.
2351*
2352* Returns the label of the far end or NULL if it's not special.
2353*/
2354static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2355{
2356 struct smk_net6addr *snp;
2357 struct in6_addr *sap = &sip->sin6_addr;
2358 int i;
2359 int found = 0;
2360
2361 /*
2362 * It's local. Don't look for a host label.
2363 */
2364 if (smk_ipv6_localhost(sip))
2365 return NULL;
2366
2367 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002368 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002369 * If the label is NULL the entry has
2370 * been renounced. Ignore it.
2371 */
2372 if (snp->smk_label == NULL)
2373 continue;
2374 /*
Paul Moore07feee82009-03-27 17:10:54 -04002375 * 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 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002379 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002380 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2381 snp->smk_host.s6_addr16[i]) {
2382 found = 0;
2383 break;
2384 }
Etienne Basset43031542009-03-27 17:11:01 -04002385 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002386 if (found)
2387 return snp->smk_label;
2388 }
Paul Moore07feee82009-03-27 17:10:54 -04002389
2390 return NULL;
2391}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002392#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002393
2394/**
Casey Schauflere114e472008-02-04 22:29:50 -08002395 * smack_netlabel - Set the secattr on a socket
2396 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002397 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002398 *
2399 * Convert the outbound smack value (smk_out) to a
2400 * secattr and attach it to the socket.
2401 *
2402 * Returns 0 on success or an error code
2403 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002404static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002405{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002406 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002407 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002408 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002409
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002410 /*
2411 * Usually the netlabel code will handle changing the
2412 * packet labeling based on the label.
2413 * The case of a single label host is different, because
2414 * a single label host should never get a labeled packet
2415 * even though the label is usually associated with a packet
2416 * label.
2417 */
2418 local_bh_disable();
2419 bh_lock_sock_nested(sk);
2420
2421 if (ssp->smk_out == smack_net_ambient ||
2422 labeled == SMACK_UNLABELED_SOCKET)
2423 netlbl_sock_delattr(sk);
2424 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002425 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002426 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002427 }
2428
2429 bh_unlock_sock(sk);
2430 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002431
Casey Schauflere114e472008-02-04 22:29:50 -08002432 return rc;
2433}
2434
2435/**
Paul Moore07feee82009-03-27 17:10:54 -04002436 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2437 * @sk: the socket
2438 * @sap: the destination address
2439 *
2440 * Set the correct secattr for the given socket based on the destination
2441 * address and perform any outbound access checks needed.
2442 *
2443 * Returns 0 on success or an error code.
2444 *
2445 */
2446static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2447{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002448 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002449 int rc;
2450 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002451 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002452 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002453 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002454
2455 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002456 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002457 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002458#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002459 struct lsm_network_audit net;
2460
Eric Paris48c62af2012-04-02 13:15:44 -04002461 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2462 ad.a.u.net->family = sap->sin_family;
2463 ad.a.u.net->dport = sap->sin_port;
2464 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002465#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002466 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002467 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002468 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2469 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002470 } else {
2471 sk_lbl = SMACK_CIPSO_SOCKET;
2472 rc = 0;
2473 }
2474 rcu_read_unlock();
2475 if (rc != 0)
2476 return rc;
2477
2478 return smack_netlabel(sk, sk_lbl);
2479}
2480
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002481#if IS_ENABLED(CONFIG_IPV6)
2482/**
2483 * smk_ipv6_check - check Smack access
2484 * @subject: subject Smack label
2485 * @object: object Smack label
2486 * @address: address
2487 * @act: the action being taken
2488 *
2489 * Check an IPv6 access
2490 */
2491static int smk_ipv6_check(struct smack_known *subject,
2492 struct smack_known *object,
2493 struct sockaddr_in6 *address, int act)
2494{
2495#ifdef CONFIG_AUDIT
2496 struct lsm_network_audit net;
2497#endif
2498 struct smk_audit_info ad;
2499 int rc;
2500
2501#ifdef CONFIG_AUDIT
2502 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2503 ad.a.u.net->family = PF_INET6;
2504 ad.a.u.net->dport = ntohs(address->sin6_port);
2505 if (act == SMK_RECEIVING)
2506 ad.a.u.net->v6info.saddr = address->sin6_addr;
2507 else
2508 ad.a.u.net->v6info.daddr = address->sin6_addr;
2509#endif
2510 rc = smk_access(subject, object, MAY_WRITE, &ad);
2511 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2512 return rc;
2513}
2514#endif /* CONFIG_IPV6 */
2515
2516#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002517/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002518 * smk_ipv6_port_label - Smack port access table management
2519 * @sock: socket
2520 * @address: address
2521 *
2522 * Create or update the port list entry
2523 */
2524static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2525{
2526 struct sock *sk = sock->sk;
2527 struct sockaddr_in6 *addr6;
2528 struct socket_smack *ssp = sock->sk->sk_security;
2529 struct smk_port_label *spp;
2530 unsigned short port = 0;
2531
2532 if (address == NULL) {
2533 /*
2534 * This operation is changing the Smack information
2535 * on the bound socket. Take the changes to the port
2536 * as well.
2537 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302538 rcu_read_lock();
2539 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002540 if (sk != spp->smk_sock)
2541 continue;
2542 spp->smk_in = ssp->smk_in;
2543 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302544 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002545 return;
2546 }
2547 /*
2548 * A NULL address is only used for updating existing
2549 * bound entries. If there isn't one, it's OK.
2550 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302551 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002552 return;
2553 }
2554
2555 addr6 = (struct sockaddr_in6 *)address;
2556 port = ntohs(addr6->sin6_port);
2557 /*
2558 * This is a special case that is safely ignored.
2559 */
2560 if (port == 0)
2561 return;
2562
2563 /*
2564 * Look for an existing port list entry.
2565 * This is an indication that a port is getting reused.
2566 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302567 rcu_read_lock();
2568 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302569 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002570 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302571 if (spp->smk_can_reuse != 1) {
2572 rcu_read_unlock();
2573 return;
2574 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002575 spp->smk_port = port;
2576 spp->smk_sock = sk;
2577 spp->smk_in = ssp->smk_in;
2578 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302579 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302580 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002581 return;
2582 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302583 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002584 /*
2585 * A new port entry is required.
2586 */
2587 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2588 if (spp == NULL)
2589 return;
2590
2591 spp->smk_port = port;
2592 spp->smk_sock = sk;
2593 spp->smk_in = ssp->smk_in;
2594 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302595 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302596 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002597
Vishal Goel3c7ce342016-11-23 10:31:08 +05302598 mutex_lock(&smack_ipv6_lock);
2599 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2600 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002601 return;
2602}
2603
2604/**
2605 * smk_ipv6_port_check - check Smack port access
2606 * @sock: socket
2607 * @address: address
2608 *
2609 * Create or update the port list entry
2610 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002611static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002612 int act)
2613{
Casey Schauflerc6739442013-05-22 18:42:56 -07002614 struct smk_port_label *spp;
2615 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002616 struct smack_known *skp = NULL;
2617 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002618 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002619
2620 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002621 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002622 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002624 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002625 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002626 }
2627
2628 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002629 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002630 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002631 if (skp != NULL && object != NULL)
2632 return smk_ipv6_check(skp, object, address, act);
2633 if (skp == NULL)
2634 skp = smack_net_ambient;
2635 if (object == NULL)
2636 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002637
2638 /*
2639 * It's remote, so port lookup does no good.
2640 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002641 if (!smk_ipv6_localhost(address))
2642 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002643
2644 /*
2645 * It's local so the send check has to have passed.
2646 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002647 if (act == SMK_RECEIVING)
2648 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002649
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002650 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302651 rcu_read_lock();
2652 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302653 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002654 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002655 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002656 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002657 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002658 break;
2659 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302660 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002661
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002662 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002663}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002664#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002665
2666/**
Casey Schauflere114e472008-02-04 22:29:50 -08002667 * smack_inode_setsecurity - set smack xattrs
2668 * @inode: the object
2669 * @name: attribute name
2670 * @value: attribute value
2671 * @size: size of the attribute
2672 * @flags: unused
2673 *
2674 * Sets the named attribute in the appropriate blob
2675 *
2676 * Returns 0 on success, or an error code
2677 */
2678static int smack_inode_setsecurity(struct inode *inode, const char *name,
2679 const void *value, size_t size, int flags)
2680{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002681 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002682 struct inode_smack *nsp = inode->i_security;
2683 struct socket_smack *ssp;
2684 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002685 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002686
Casey Schauflerf7112e62012-05-06 15:22:02 -07002687 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302688 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002689
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002690 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002691 if (IS_ERR(skp))
2692 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002693
2694 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002695 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002696 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002697 return 0;
2698 }
2699 /*
2700 * The rest of the Smack xattrs are only on sockets.
2701 */
2702 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2703 return -EOPNOTSUPP;
2704
2705 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002706 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002707 return -EOPNOTSUPP;
2708
2709 ssp = sock->sk->sk_security;
2710
2711 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002712 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002713 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002714 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002715 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002716 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2717 if (rc != 0)
2718 printk(KERN_WARNING
2719 "Smack: \"%s\" netlbl error %d.\n",
2720 __func__, -rc);
2721 }
Casey Schauflere114e472008-02-04 22:29:50 -08002722 } else
2723 return -EOPNOTSUPP;
2724
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002725#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002726 if (sock->sk->sk_family == PF_INET6)
2727 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002728#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002729
Casey Schauflere114e472008-02-04 22:29:50 -08002730 return 0;
2731}
2732
2733/**
2734 * smack_socket_post_create - finish socket setup
2735 * @sock: the socket
2736 * @family: protocol family
2737 * @type: unused
2738 * @protocol: unused
2739 * @kern: unused
2740 *
2741 * Sets the netlabel information on the socket
2742 *
2743 * Returns 0 on success, and error code otherwise
2744 */
2745static int smack_socket_post_create(struct socket *sock, int family,
2746 int type, int protocol, int kern)
2747{
Marcin Lis74123012015-01-22 15:40:33 +01002748 struct socket_smack *ssp;
2749
2750 if (sock->sk == NULL)
2751 return 0;
2752
2753 /*
2754 * Sockets created by kernel threads receive web label.
2755 */
2756 if (unlikely(current->flags & PF_KTHREAD)) {
2757 ssp = sock->sk->sk_security;
2758 ssp->smk_in = &smack_known_web;
2759 ssp->smk_out = &smack_known_web;
2760 }
2761
2762 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002763 return 0;
2764 /*
2765 * Set the outbound netlbl.
2766 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002767 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2768}
2769
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002770/**
2771 * smack_socket_socketpair - create socket pair
2772 * @socka: one socket
2773 * @sockb: another socket
2774 *
2775 * Cross reference the peer labels for SO_PEERSEC
2776 *
2777 * Returns 0 on success, and error code otherwise
2778 */
2779static int smack_socket_socketpair(struct socket *socka,
2780 struct socket *sockb)
2781{
2782 struct socket_smack *asp = socka->sk->sk_security;
2783 struct socket_smack *bsp = sockb->sk->sk_security;
2784
2785 asp->smk_packet = bsp->smk_out;
2786 bsp->smk_packet = asp->smk_out;
2787
2788 return 0;
2789}
2790
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002791#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002792/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002793 * smack_socket_bind - record port binding information.
2794 * @sock: the socket
2795 * @address: the port address
2796 * @addrlen: size of the address
2797 *
2798 * Records the label bound to a port.
2799 *
2800 * Returns 0
2801 */
2802static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2803 int addrlen)
2804{
2805 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2806 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002807 return 0;
2808}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002809#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002810
2811/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002812 * smack_socket_connect - connect access check
2813 * @sock: the socket
2814 * @sap: the other end
2815 * @addrlen: size of sap
2816 *
2817 * Verifies that a connection may be possible
2818 *
2819 * Returns 0 on success, and error code otherwise
2820 */
2821static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2822 int addrlen)
2823{
Casey Schauflerc6739442013-05-22 18:42:56 -07002824 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002825#if IS_ENABLED(CONFIG_IPV6)
2826 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2827#endif
2828#ifdef SMACK_IPV6_SECMARK_LABELING
2829 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002830 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002831#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002832
Casey Schauflerc6739442013-05-22 18:42:56 -07002833 if (sock->sk == NULL)
2834 return 0;
2835
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002836#ifdef SMACK_IPV6_SECMARK_LABELING
2837 ssp = sock->sk->sk_security;
2838#endif
2839
Casey Schauflerc6739442013-05-22 18:42:56 -07002840 switch (sock->sk->sk_family) {
2841 case PF_INET:
2842 if (addrlen < sizeof(struct sockaddr_in))
2843 return -EINVAL;
2844 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2845 break;
2846 case PF_INET6:
2847 if (addrlen < sizeof(struct sockaddr_in6))
2848 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002849#ifdef SMACK_IPV6_SECMARK_LABELING
2850 rsp = smack_ipv6host_label(sip);
2851 if (rsp != NULL)
2852 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002853 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002854#endif
2855#ifdef SMACK_IPV6_PORT_LABELING
2856 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2857#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002858 break;
2859 }
2860 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002861}
2862
2863/**
2864 * smack_flags_to_may - convert S_ to MAY_ values
2865 * @flags: the S_ value
2866 *
2867 * Returns the equivalent MAY_ value
2868 */
2869static int smack_flags_to_may(int flags)
2870{
2871 int may = 0;
2872
2873 if (flags & S_IRUGO)
2874 may |= MAY_READ;
2875 if (flags & S_IWUGO)
2876 may |= MAY_WRITE;
2877 if (flags & S_IXUGO)
2878 may |= MAY_EXEC;
2879
2880 return may;
2881}
2882
2883/**
2884 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2885 * @msg: the object
2886 *
2887 * Returns 0
2888 */
2889static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2890{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002891 struct smack_known *skp = smk_of_current();
2892
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002893 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002894 return 0;
2895}
2896
2897/**
2898 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2899 * @msg: the object
2900 *
2901 * Clears the blob pointer
2902 */
2903static void smack_msg_msg_free_security(struct msg_msg *msg)
2904{
2905 msg->security = NULL;
2906}
2907
2908/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002909 * smack_of_ipc - the smack pointer for the ipc
2910 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002911 *
2912 * Returns a pointer to the smack value
2913 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002914static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002915{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002916 return (struct smack_known *)isp->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002917}
2918
2919/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002920 * smack_ipc_alloc_security - Set the security blob for ipc
2921 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002922 *
2923 * Returns 0
2924 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002925static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002926{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002927 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002928
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002929 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002930 return 0;
2931}
2932
2933/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002934 * smack_ipc_free_security - Clear the security blob for ipc
2935 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002936 *
2937 * Clears the blob pointer
2938 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002939static void smack_ipc_free_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002940{
Casey Schauflere114e472008-02-04 22:29:50 -08002941 isp->security = NULL;
2942}
2943
2944/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002945 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002946 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002947 * @access : access requested
2948 *
2949 * Returns 0 if current has the requested access, error code otherwise
2950 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002951static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002952{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002953 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002954 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002955 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002956
2957#ifdef CONFIG_AUDIT
2958 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002959 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002960#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002961 rc = smk_curacc(ssp, access, &ad);
2962 rc = smk_bu_current("shm", ssp, access, rc);
2963 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002964}
2965
2966/**
Casey Schauflere114e472008-02-04 22:29:50 -08002967 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002968 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002969 * @shmflg: access requested
2970 *
2971 * Returns 0 if current has the requested access, error code otherwise
2972 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002973static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002974{
Casey Schauflere114e472008-02-04 22:29:50 -08002975 int may;
2976
2977 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002978 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002979}
2980
2981/**
2982 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002983 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002984 * @cmd: what it wants to do
2985 *
2986 * Returns 0 if current has the requested access, error code otherwise
2987 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002988static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002989{
Casey Schauflere114e472008-02-04 22:29:50 -08002990 int may;
2991
2992 switch (cmd) {
2993 case IPC_STAT:
2994 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002995 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002996 may = MAY_READ;
2997 break;
2998 case IPC_SET:
2999 case SHM_LOCK:
3000 case SHM_UNLOCK:
3001 case IPC_RMID:
3002 may = MAY_READWRITE;
3003 break;
3004 case IPC_INFO:
3005 case SHM_INFO:
3006 /*
3007 * System level information.
3008 */
3009 return 0;
3010 default:
3011 return -EINVAL;
3012 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003013 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003014}
3015
3016/**
3017 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003018 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003019 * @shmaddr: unused
3020 * @shmflg: access requested
3021 *
3022 * Returns 0 if current has the requested access, error code otherwise
3023 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003024static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003025 int shmflg)
3026{
Casey Schauflere114e472008-02-04 22:29:50 -08003027 int may;
3028
3029 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003030 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003031}
3032
3033/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003034 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003035 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003036 * @access : access requested
3037 *
3038 * Returns 0 if current has the requested access, error code otherwise
3039 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003040static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003041{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003042 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003043 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003044 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003045
3046#ifdef CONFIG_AUDIT
3047 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003048 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003049#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003050 rc = smk_curacc(ssp, access, &ad);
3051 rc = smk_bu_current("sem", ssp, access, rc);
3052 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003053}
3054
3055/**
Casey Schauflere114e472008-02-04 22:29:50 -08003056 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003057 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003058 * @semflg: access requested
3059 *
3060 * Returns 0 if current has the requested access, error code otherwise
3061 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003062static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003063{
Casey Schauflere114e472008-02-04 22:29:50 -08003064 int may;
3065
3066 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003067 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003068}
3069
3070/**
3071 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003072 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003073 * @cmd: what it wants to do
3074 *
3075 * Returns 0 if current has the requested access, error code otherwise
3076 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003077static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003078{
Casey Schauflere114e472008-02-04 22:29:50 -08003079 int may;
3080
3081 switch (cmd) {
3082 case GETPID:
3083 case GETNCNT:
3084 case GETZCNT:
3085 case GETVAL:
3086 case GETALL:
3087 case IPC_STAT:
3088 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003089 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003090 may = MAY_READ;
3091 break;
3092 case SETVAL:
3093 case SETALL:
3094 case IPC_RMID:
3095 case IPC_SET:
3096 may = MAY_READWRITE;
3097 break;
3098 case IPC_INFO:
3099 case SEM_INFO:
3100 /*
3101 * System level information
3102 */
3103 return 0;
3104 default:
3105 return -EINVAL;
3106 }
3107
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003108 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003109}
3110
3111/**
3112 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003113 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003114 * @sops: unused
3115 * @nsops: unused
3116 * @alter: unused
3117 *
3118 * Treated as read and write in all cases.
3119 *
3120 * Returns 0 if access is allowed, error code otherwise
3121 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003122static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003123 unsigned nsops, int alter)
3124{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003125 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003126}
3127
3128/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003129 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003130 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003131 * @access : access requested
3132 *
3133 * return 0 if current has access, error otherwise
3134 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003135static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003136{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003137 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003138 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003139 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003140
3141#ifdef CONFIG_AUDIT
3142 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003143 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003144#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003145 rc = smk_curacc(msp, access, &ad);
3146 rc = smk_bu_current("msq", msp, access, rc);
3147 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003148}
3149
3150/**
Casey Schauflere114e472008-02-04 22:29:50 -08003151 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003152 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003153 * @msqflg: access requested
3154 *
3155 * Returns 0 if current has the requested access, error code otherwise
3156 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003157static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003158{
Casey Schauflere114e472008-02-04 22:29:50 -08003159 int may;
3160
3161 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003162 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003163}
3164
3165/**
3166 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003167 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003168 * @cmd: what it wants to do
3169 *
3170 * Returns 0 if current has the requested access, error code otherwise
3171 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003172static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003173{
Casey Schauflere114e472008-02-04 22:29:50 -08003174 int may;
3175
3176 switch (cmd) {
3177 case IPC_STAT:
3178 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003179 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003180 may = MAY_READ;
3181 break;
3182 case IPC_SET:
3183 case IPC_RMID:
3184 may = MAY_READWRITE;
3185 break;
3186 case IPC_INFO:
3187 case MSG_INFO:
3188 /*
3189 * System level information
3190 */
3191 return 0;
3192 default:
3193 return -EINVAL;
3194 }
3195
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003196 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003197}
3198
3199/**
3200 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003201 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003202 * @msg: unused
3203 * @msqflg: access requested
3204 *
3205 * Returns 0 if current has the requested access, error code otherwise
3206 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003207static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003208 int msqflg)
3209{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003210 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003211
Etienne Bassetecfcc532009-04-08 20:40:06 +02003212 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003213 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003214}
3215
3216/**
3217 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003218 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003219 * @msg: unused
3220 * @target: unused
3221 * @type: unused
3222 * @mode: unused
3223 *
3224 * Returns 0 if current has read and write access, error code otherwise
3225 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003226static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003227 struct task_struct *target, long type, int mode)
3228{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003229 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003230}
3231
3232/**
3233 * smack_ipc_permission - Smack access for ipc_permission()
3234 * @ipp: the object permissions
3235 * @flag: access requested
3236 *
3237 * Returns 0 if current has read and write access, error code otherwise
3238 */
3239static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3240{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003241 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003242 int may = smack_flags_to_may(flag);
3243 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003244 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003245
Etienne Bassetecfcc532009-04-08 20:40:06 +02003246#ifdef CONFIG_AUDIT
3247 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3248 ad.a.u.ipc_id = ipp->id;
3249#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003250 rc = smk_curacc(iskp, may, &ad);
3251 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003252 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003253}
3254
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003255/**
3256 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003257 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003258 * @secid: where result will be saved
3259 */
3260static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3261{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003262 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003263
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003264 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003265}
3266
Casey Schauflere114e472008-02-04 22:29:50 -08003267/**
3268 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003269 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003270 * @inode: the object
3271 *
3272 * Set the inode's security blob if it hasn't been done already.
3273 */
3274static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3275{
3276 struct super_block *sbp;
3277 struct superblock_smack *sbsp;
3278 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003279 struct smack_known *skp;
3280 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003281 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003282 char trattr[TRANS_TRUE_SIZE];
3283 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003284 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003285 struct dentry *dp;
3286
3287 if (inode == NULL)
3288 return;
3289
3290 isp = inode->i_security;
3291
3292 mutex_lock(&isp->smk_lock);
3293 /*
3294 * If the inode is already instantiated
3295 * take the quick way out
3296 */
3297 if (isp->smk_flags & SMK_INODE_INSTANT)
3298 goto unlockandout;
3299
3300 sbp = inode->i_sb;
3301 sbsp = sbp->s_security;
3302 /*
3303 * We're going to use the superblock default label
3304 * if there's no label on the file.
3305 */
3306 final = sbsp->smk_default;
3307
3308 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003309 * If this is the root inode the superblock
3310 * may be in the process of initialization.
3311 * If that is the case use the root value out
3312 * of the superblock.
3313 */
3314 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003315 switch (sbp->s_magic) {
3316 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003317 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003318 /*
3319 * The cgroup filesystem is never mounted,
3320 * so there's no opportunity to set the mount
3321 * options.
3322 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003323 sbsp->smk_root = &smack_known_star;
3324 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003325 isp->smk_inode = sbsp->smk_root;
3326 break;
3327 case TMPFS_MAGIC:
3328 /*
3329 * What about shmem/tmpfs anonymous files with dentry
3330 * obtained from d_alloc_pseudo()?
3331 */
3332 isp->smk_inode = smk_of_current();
3333 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003334 case PIPEFS_MAGIC:
3335 isp->smk_inode = smk_of_current();
3336 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003337 case SOCKFS_MAGIC:
3338 /*
3339 * Socket access is controlled by the socket
3340 * structures associated with the task involved.
3341 */
3342 isp->smk_inode = &smack_known_star;
3343 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003344 default:
3345 isp->smk_inode = sbsp->smk_root;
3346 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003347 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003348 isp->smk_flags |= SMK_INODE_INSTANT;
3349 goto unlockandout;
3350 }
3351
3352 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003353 * This is pretty hackish.
3354 * Casey says that we shouldn't have to do
3355 * file system specific code, but it does help
3356 * with keeping it simple.
3357 */
3358 switch (sbp->s_magic) {
3359 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003360 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003361 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003362 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003363 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003364 * that the smack file system doesn't do
3365 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003366 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003367 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003368 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003369 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003370 break;
3371 case DEVPTS_SUPER_MAGIC:
3372 /*
3373 * devpts seems content with the label of the task.
3374 * Programs that change smack have to treat the
3375 * pty with respect.
3376 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003377 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003378 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003379 case PROC_SUPER_MAGIC:
3380 /*
3381 * Casey says procfs appears not to care.
3382 * The superblock default suffices.
3383 */
3384 break;
3385 case TMPFS_MAGIC:
3386 /*
3387 * Device labels should come from the filesystem,
3388 * but watch out, because they're volitile,
3389 * getting recreated on every reboot.
3390 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003391 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003392 /*
Gustavo A. R. Silvab1fed3e2018-08-01 17:38:54 -05003393 * Fall through.
Casey Schauflere114e472008-02-04 22:29:50 -08003394 *
3395 * If a smack value has been set we want to use it,
3396 * but since tmpfs isn't giving us the opportunity
3397 * to set mount options simulate setting the
3398 * superblock default.
3399 */
3400 default:
3401 /*
3402 * This isn't an understood special case.
3403 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003404 */
3405
3406 /*
3407 * UNIX domain sockets use lower level socket data.
3408 */
3409 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003410 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003411 break;
3412 }
3413 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003414 * No xattr support means, alas, no SMACK label.
3415 * Use the aforeapplied default.
3416 * It would be curious if the label of the task
3417 * does not match that assigned.
3418 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003419 if (!(inode->i_opflags & IOP_XATTR))
3420 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003421 /*
3422 * Get the dentry for xattr.
3423 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003424 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003425 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003426 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003427 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003428
3429 /*
3430 * Transmuting directory
3431 */
3432 if (S_ISDIR(inode->i_mode)) {
3433 /*
3434 * If this is a new directory and the label was
3435 * transmuted when the inode was initialized
3436 * set the transmute attribute on the directory
3437 * and mark the inode.
3438 *
3439 * If there is a transmute attribute on the
3440 * directory mark the inode.
3441 */
3442 if (isp->smk_flags & SMK_INODE_CHANGED) {
3443 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003444 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003445 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003446 TRANS_TRUE, TRANS_TRUE_SIZE,
3447 0);
3448 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003449 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003450 XATTR_NAME_SMACKTRANSMUTE, trattr,
3451 TRANS_TRUE_SIZE);
3452 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3453 TRANS_TRUE_SIZE) != 0)
3454 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003455 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003456 if (rc >= 0)
3457 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003458 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003459 /*
3460 * Don't let the exec or mmap label be "*" or "@".
3461 */
3462 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3463 if (IS_ERR(skp) || skp == &smack_known_star ||
3464 skp == &smack_known_web)
3465 skp = NULL;
3466 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003467
Casey Schaufler19760ad2013-12-16 16:27:26 -08003468 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003469 if (IS_ERR(skp) || skp == &smack_known_star ||
3470 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003471 skp = NULL;
3472 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003473
Casey Schauflere114e472008-02-04 22:29:50 -08003474 dput(dp);
3475 break;
3476 }
3477
3478 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003479 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003480 else
3481 isp->smk_inode = final;
3482
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003483 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003484
3485unlockandout:
3486 mutex_unlock(&isp->smk_lock);
3487 return;
3488}
3489
3490/**
3491 * smack_getprocattr - Smack process attribute access
3492 * @p: the object task
3493 * @name: the name of the attribute in /proc/.../attr
3494 * @value: where to put the result
3495 *
3496 * Places a copy of the task Smack into value
3497 *
3498 * Returns the length of the smack label or an error code
3499 */
3500static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3501{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003502 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003503 char *cp;
3504 int slen;
3505
3506 if (strcmp(name, "current") != 0)
3507 return -EINVAL;
3508
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003509 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003510 if (cp == NULL)
3511 return -ENOMEM;
3512
3513 slen = strlen(cp);
3514 *value = cp;
3515 return slen;
3516}
3517
3518/**
3519 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003520 * @name: the name of the attribute in /proc/.../attr
3521 * @value: the value to set
3522 * @size: the size of the value
3523 *
3524 * Sets the Smack value of the task. Only setting self
3525 * is permitted and only with privilege
3526 *
3527 * Returns the length of the smack label or an error code
3528 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003529static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003530{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003531 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003532 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003533 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003534 struct smack_known_list_elem *sklep;
3535 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003536
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003537 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003538 return -EPERM;
3539
Casey Schauflerf7112e62012-05-06 15:22:02 -07003540 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003541 return -EINVAL;
3542
3543 if (strcmp(name, "current") != 0)
3544 return -EINVAL;
3545
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003546 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003547 if (IS_ERR(skp))
3548 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003549
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003550 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303551 * No process is ever allowed the web ("@") label
3552 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003553 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303554 if (skp == &smack_known_web || skp == &smack_known_star)
3555 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003556
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003557 if (!smack_privileged(CAP_MAC_ADMIN)) {
3558 rc = -EPERM;
3559 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3560 if (sklep->smk_label == skp) {
3561 rc = 0;
3562 break;
3563 }
3564 if (rc)
3565 return rc;
3566 }
3567
David Howellsd84f4f92008-11-14 10:39:23 +11003568 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003569 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003570 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003571
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003572 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003573 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003574 /*
3575 * process can change its label only once
3576 */
3577 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003578
David Howellsd84f4f92008-11-14 10:39:23 +11003579 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003580 return size;
3581}
3582
3583/**
3584 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003585 * @sock: one sock
3586 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003587 * @newsk: unused
3588 *
3589 * Return 0 if a subject with the smack of sock could access
3590 * an object with the smack of other, otherwise an error code
3591 */
David S. Miller3610cda2011-01-05 15:38:53 -08003592static int smack_unix_stream_connect(struct sock *sock,
3593 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003594{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003595 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003596 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003597 struct socket_smack *ssp = sock->sk_security;
3598 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003599 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003600 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003601 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003602#ifdef CONFIG_AUDIT
3603 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003604#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003605
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003606 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3607 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003608 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003609#ifdef CONFIG_AUDIT
3610 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3611 smk_ad_setfield_u_net_sk(&ad, other);
3612#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003613 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3614 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003615 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003616 okp = osp->smk_out;
3617 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003618 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003619 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003620 MAY_WRITE, rc);
3621 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003622 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003623
Casey Schaufler975d5e52011-09-26 14:43:39 -07003624 /*
3625 * Cross reference the peer labels for SO_PEERSEC.
3626 */
3627 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003628 nsp->smk_packet = ssp->smk_out;
3629 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003630 }
3631
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003632 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003633}
3634
3635/**
3636 * smack_unix_may_send - Smack access on UDS
3637 * @sock: one socket
3638 * @other: the other socket
3639 *
3640 * Return 0 if a subject with the smack of sock could access
3641 * an object with the smack of other, otherwise an error code
3642 */
3643static int smack_unix_may_send(struct socket *sock, struct socket *other)
3644{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003645 struct socket_smack *ssp = sock->sk->sk_security;
3646 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003647 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003648 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003649
Kees Cook923e9a12012-04-10 13:26:44 -07003650#ifdef CONFIG_AUDIT
3651 struct lsm_network_audit net;
3652
Eric Paris48c62af2012-04-02 13:15:44 -04003653 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003654 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003655#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003656
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003657 if (smack_privileged(CAP_MAC_OVERRIDE))
3658 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003659
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003660 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3661 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003662 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003663}
3664
3665/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003666 * smack_socket_sendmsg - Smack check based on destination host
3667 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003668 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003669 * @size: the size of the message
3670 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003671 * Return 0 if the current subject can write to the destination host.
3672 * For IPv4 this is only a question if the destination is a single label host.
3673 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003674 */
3675static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3676 int size)
3677{
3678 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003679#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003680 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003681#endif
3682#ifdef SMACK_IPV6_SECMARK_LABELING
3683 struct socket_smack *ssp = sock->sk->sk_security;
3684 struct smack_known *rsp;
3685#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003686 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003687
3688 /*
3689 * Perfectly reasonable for this to be NULL
3690 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003691 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003692 return 0;
3693
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003694 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003695 case AF_INET:
3696 rc = smack_netlabel_send(sock->sk, sip);
3697 break;
3698 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003699#ifdef SMACK_IPV6_SECMARK_LABELING
3700 rsp = smack_ipv6host_label(sap);
3701 if (rsp != NULL)
3702 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3703 SMK_CONNECTING);
3704#endif
3705#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003706 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003707#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003708 break;
3709 }
3710 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003711}
3712
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003713/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003714 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003715 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003716 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003717 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003718 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003719 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003720static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3721 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003722{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003723 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003724 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003725 int acat;
3726 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003727
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003728 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003729 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003730 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003731 * If there are flags but no level netlabel isn't
3732 * behaving the way we expect it to.
3733 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003734 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003735 * Without guidance regarding the smack value
3736 * for the packet fall back on the network
3737 * ambient value.
3738 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003739 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303740 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003741 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003742 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003743 /*
3744 * Compare the catsets. Use the netlbl APIs.
3745 */
3746 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3747 if ((skp->smk_netlabel.flags &
3748 NETLBL_SECATTR_MLS_CAT) == 0)
3749 found = 1;
3750 break;
3751 }
3752 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003753 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3754 acat + 1);
3755 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003756 skp->smk_netlabel.attr.mls.cat,
3757 kcat + 1);
3758 if (acat < 0 || kcat < 0)
3759 break;
3760 }
3761 if (acat == kcat) {
3762 found = 1;
3763 break;
3764 }
Casey Schauflere114e472008-02-04 22:29:50 -08003765 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003766 rcu_read_unlock();
3767
3768 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003769 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003770
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003771 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003772 return &smack_known_web;
3773 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003774 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003775 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003776 /*
3777 * Looks like a fallback, which gives us a secid.
3778 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003779 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003780 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003781 * Without guidance regarding the smack value
3782 * for the packet fall back on the network
3783 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003784 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003785 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003786}
3787
Casey Schaufler69f287a2014-12-12 17:08:40 -08003788#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003789static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003790{
Casey Schauflerc6739442013-05-22 18:42:56 -07003791 u8 nexthdr;
3792 int offset;
3793 int proto = -EINVAL;
3794 struct ipv6hdr _ipv6h;
3795 struct ipv6hdr *ip6;
3796 __be16 frag_off;
3797 struct tcphdr _tcph, *th;
3798 struct udphdr _udph, *uh;
3799 struct dccp_hdr _dccph, *dh;
3800
3801 sip->sin6_port = 0;
3802
3803 offset = skb_network_offset(skb);
3804 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3805 if (ip6 == NULL)
3806 return -EINVAL;
3807 sip->sin6_addr = ip6->saddr;
3808
3809 nexthdr = ip6->nexthdr;
3810 offset += sizeof(_ipv6h);
3811 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3812 if (offset < 0)
3813 return -EINVAL;
3814
3815 proto = nexthdr;
3816 switch (proto) {
3817 case IPPROTO_TCP:
3818 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3819 if (th != NULL)
3820 sip->sin6_port = th->source;
3821 break;
3822 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003823 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003824 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3825 if (uh != NULL)
3826 sip->sin6_port = uh->source;
3827 break;
3828 case IPPROTO_DCCP:
3829 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3830 if (dh != NULL)
3831 sip->sin6_port = dh->dccph_sport;
3832 break;
3833 }
3834 return proto;
3835}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003836#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003837
Casey Schauflere114e472008-02-04 22:29:50 -08003838/**
3839 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3840 * @sk: socket
3841 * @skb: packet
3842 *
3843 * Returns 0 if the packet should be delivered, an error code otherwise
3844 */
3845static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3846{
3847 struct netlbl_lsm_secattr secattr;
3848 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003849 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003850 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003851 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003852 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003853#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003854 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003855#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003856#if IS_ENABLED(CONFIG_IPV6)
3857 struct sockaddr_in6 sadd;
3858 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003859
3860 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3861 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003862#endif /* CONFIG_IPV6 */
3863
Piotr Sawicki129a9982018-07-19 11:42:58 +02003864 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003865 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003866#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3867 /*
3868 * If there is a secmark use it rather than the CIPSO label.
3869 * If there is no secmark fall back to CIPSO.
3870 * The secmark is assumed to reflect policy better.
3871 */
3872 if (skb && skb->secmark != 0) {
3873 skp = smack_from_secid(skb->secmark);
3874 goto access_check;
3875 }
3876#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003877 /*
3878 * Translate what netlabel gave us.
3879 */
3880 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003881
Piotr Sawicki129a9982018-07-19 11:42:58 +02003882 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003883 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003884 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003885 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003886 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003887
Casey Schauflerc6739442013-05-22 18:42:56 -07003888 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003889
Casey Schaufler69f287a2014-12-12 17:08:40 -08003890#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3891access_check:
3892#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003893#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003894 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003895 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003896 ad.a.u.net->netif = skb->skb_iif;
3897 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003898#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003899 /*
3900 * Receiving a packet requires that the other end
3901 * be able to write here. Read access is not required.
3902 * This is the simplist possible security model
3903 * for networking.
3904 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003905 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3906 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003907 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003908 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003909 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003910 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003911#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003912 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003913 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003914 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3915 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003917#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003918 if (skb && skb->secmark != 0)
3919 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003920 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003921 skp = smack_ipv6host_label(&sadd);
3922 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003923 skp = smack_net_ambient;
3924#ifdef CONFIG_AUDIT
3925 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003926 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003927 ad.a.u.net->netif = skb->skb_iif;
3928 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3929#endif /* CONFIG_AUDIT */
3930 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3931 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3932 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003933#endif /* SMACK_IPV6_SECMARK_LABELING */
3934#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003935 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003936#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003937 if (rc != 0)
3938 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3939 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003940 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003941#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003942 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003943
Paul Moorea8134292008-10-10 10:16:31 -04003944 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003945}
3946
3947/**
3948 * smack_socket_getpeersec_stream - pull in packet label
3949 * @sock: the socket
3950 * @optval: user's destination
3951 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003952 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003953 *
3954 * returns zero on success, an error code otherwise
3955 */
3956static int smack_socket_getpeersec_stream(struct socket *sock,
3957 char __user *optval,
3958 int __user *optlen, unsigned len)
3959{
3960 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003961 char *rcp = "";
3962 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003963 int rc = 0;
3964
3965 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003966 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003967 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003968 slen = strlen(rcp) + 1;
3969 }
Casey Schauflere114e472008-02-04 22:29:50 -08003970
3971 if (slen > len)
3972 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003973 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003974 rc = -EFAULT;
3975
3976 if (put_user(slen, optlen) != 0)
3977 rc = -EFAULT;
3978
3979 return rc;
3980}
3981
3982
3983/**
3984 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003985 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003986 * @skb: packet data
3987 * @secid: pointer to where to put the secid of the packet
3988 *
3989 * Sets the netlabel socket state on sk from parent
3990 */
3991static int smack_socket_getpeersec_dgram(struct socket *sock,
3992 struct sk_buff *skb, u32 *secid)
3993
3994{
3995 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003996 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003997 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003998 int family = PF_UNSPEC;
3999 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004000 int rc;
4001
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004002 if (skb != NULL) {
4003 if (skb->protocol == htons(ETH_P_IP))
4004 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004005#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004006 else if (skb->protocol == htons(ETH_P_IPV6))
4007 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004008#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004009 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004010 if (family == PF_UNSPEC && sock != NULL)
4011 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004012
Casey Schaufler69f287a2014-12-12 17:08:40 -08004013 switch (family) {
4014 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004015 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004016 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004017 break;
4018 case PF_INET:
4019#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4020 s = skb->secmark;
4021 if (s != 0)
4022 break;
4023#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004024 /*
4025 * Translate what netlabel gave us.
4026 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004027 if (sock != NULL && sock->sk != NULL)
4028 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004029 netlbl_secattr_init(&secattr);
4030 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4031 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004032 skp = smack_from_secattr(&secattr, ssp);
4033 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004034 }
4035 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004036 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004037 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004038#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004039 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004040#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004041 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004042 }
4043 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004044 if (s == 0)
4045 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004046 return 0;
4047}
4048
4049/**
Paul Moore07feee82009-03-27 17:10:54 -04004050 * smack_sock_graft - Initialize a newly created socket with an existing sock
4051 * @sk: child sock
4052 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004053 *
Paul Moore07feee82009-03-27 17:10:54 -04004054 * Set the smk_{in,out} state of an existing sock based on the process that
4055 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004056 */
4057static void smack_sock_graft(struct sock *sk, struct socket *parent)
4058{
4059 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004060 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004061
Paul Moore07feee82009-03-27 17:10:54 -04004062 if (sk == NULL ||
4063 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004064 return;
4065
4066 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004067 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004068 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004069 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004070}
4071
4072/**
4073 * smack_inet_conn_request - Smack access check on connect
4074 * @sk: socket involved
4075 * @skb: packet
4076 * @req: unused
4077 *
4078 * Returns 0 if a task with the packet label could write to
4079 * the socket, otherwise an error code
4080 */
4081static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4082 struct request_sock *req)
4083{
Paul Moore07feee82009-03-27 17:10:54 -04004084 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004085 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004086 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004087 struct netlbl_lsm_secattr secattr;
4088 struct sockaddr_in addr;
4089 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004090 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004091 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004092 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004093#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004094 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004095#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004096
Casey Schaufler69f287a2014-12-12 17:08:40 -08004097#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004098 if (family == PF_INET6) {
4099 /*
4100 * Handle mapped IPv4 packets arriving
4101 * via IPv6 sockets. Don't set up netlabel
4102 * processing on IPv6.
4103 */
4104 if (skb->protocol == htons(ETH_P_IP))
4105 family = PF_INET;
4106 else
4107 return 0;
4108 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004109#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004110
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004111#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4112 /*
4113 * If there is a secmark use it rather than the CIPSO label.
4114 * If there is no secmark fall back to CIPSO.
4115 * The secmark is assumed to reflect policy better.
4116 */
4117 if (skb && skb->secmark != 0) {
4118 skp = smack_from_secid(skb->secmark);
4119 goto access_check;
4120 }
4121#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4122
Paul Moore07feee82009-03-27 17:10:54 -04004123 netlbl_secattr_init(&secattr);
4124 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004125 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004126 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004127 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004128 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004129 netlbl_secattr_destroy(&secattr);
4130
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004131#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4132access_check:
4133#endif
4134
Etienne Bassetecfcc532009-04-08 20:40:06 +02004135#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004136 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4137 ad.a.u.net->family = family;
4138 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004139 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4140#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004141 /*
Paul Moore07feee82009-03-27 17:10:54 -04004142 * Receiving a packet requires that the other end be able to write
4143 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004144 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004145 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4146 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004147 if (rc != 0)
4148 return rc;
4149
4150 /*
4151 * Save the peer's label in the request_sock so we can later setup
4152 * smk_packet in the child socket so that SO_PEERCRED can report it.
4153 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004154 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004155
4156 /*
4157 * We need to decide if we want to label the incoming connection here
4158 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004159 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004160 */
4161 hdr = ip_hdr(skb);
4162 addr.sin_addr.s_addr = hdr->saddr;
4163 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004164 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004165 rcu_read_unlock();
4166
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004167 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004168 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004169 else
Paul Moore07feee82009-03-27 17:10:54 -04004170 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004171
4172 return rc;
4173}
4174
Paul Moore07feee82009-03-27 17:10:54 -04004175/**
4176 * smack_inet_csk_clone - Copy the connection information to the new socket
4177 * @sk: the new socket
4178 * @req: the connection's request_sock
4179 *
4180 * Transfer the connection's peer label to the newly created socket.
4181 */
4182static void smack_inet_csk_clone(struct sock *sk,
4183 const struct request_sock *req)
4184{
4185 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004186 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004187
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004188 if (req->peer_secid != 0) {
4189 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004190 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004191 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004192 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004193}
4194
Casey Schauflere114e472008-02-04 22:29:50 -08004195/*
4196 * Key management security hooks
4197 *
4198 * Casey has not tested key support very heavily.
4199 * The permission check is most likely too restrictive.
4200 * If you care about keys please have a look.
4201 */
4202#ifdef CONFIG_KEYS
4203
4204/**
4205 * smack_key_alloc - Set the key security blob
4206 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004207 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004208 * @flags: unused
4209 *
4210 * No allocation required
4211 *
4212 * Returns 0
4213 */
David Howellsd84f4f92008-11-14 10:39:23 +11004214static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004215 unsigned long flags)
4216{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004217 struct smack_known *skp = smk_of_task(cred->security);
4218
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004219 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004220 return 0;
4221}
4222
4223/**
4224 * smack_key_free - Clear the key security blob
4225 * @key: the object
4226 *
4227 * Clear the blob pointer
4228 */
4229static void smack_key_free(struct key *key)
4230{
4231 key->security = NULL;
4232}
4233
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004234/**
Casey Schauflere114e472008-02-04 22:29:50 -08004235 * smack_key_permission - Smack access on a key
4236 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004237 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004238 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004239 *
4240 * Return 0 if the task has read and write to the object,
4241 * an error code otherwise
4242 */
4243static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004244 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004245{
4246 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004247 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004248 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004249 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004250 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004251
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004252 /*
4253 * Validate requested permissions
4254 */
4255 if (perm & ~KEY_NEED_ALL)
4256 return -EINVAL;
4257
Casey Schauflere114e472008-02-04 22:29:50 -08004258 keyp = key_ref_to_ptr(key_ref);
4259 if (keyp == NULL)
4260 return -EINVAL;
4261 /*
4262 * If the key hasn't been initialized give it access so that
4263 * it may do so.
4264 */
4265 if (keyp->security == NULL)
4266 return 0;
4267 /*
4268 * This should not occur
4269 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004270 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004271 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004272
4273 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4274 return 0;
4275
Etienne Bassetecfcc532009-04-08 20:40:06 +02004276#ifdef CONFIG_AUDIT
4277 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4278 ad.a.u.key_struct.key = keyp->serial;
4279 ad.a.u.key_struct.key_desc = keyp->description;
4280#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004281 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4282 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004283 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004284 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004285 rc = smk_access(tkp, keyp->security, request, &ad);
4286 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4287 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004288}
José Bollo7fc5f362015-02-17 15:41:22 +01004289
4290/*
4291 * smack_key_getsecurity - Smack label tagging the key
4292 * @key points to the key to be queried
4293 * @_buffer points to a pointer that should be set to point to the
4294 * resulting string (if no label or an error occurs).
4295 * Return the length of the string (including terminating NUL) or -ve if
4296 * an error.
4297 * May also return 0 (and a NULL buffer pointer) if there is no label.
4298 */
4299static int smack_key_getsecurity(struct key *key, char **_buffer)
4300{
4301 struct smack_known *skp = key->security;
4302 size_t length;
4303 char *copy;
4304
4305 if (key->security == NULL) {
4306 *_buffer = NULL;
4307 return 0;
4308 }
4309
4310 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4311 if (copy == NULL)
4312 return -ENOMEM;
4313 length = strlen(copy) + 1;
4314
4315 *_buffer = copy;
4316 return length;
4317}
4318
Casey Schauflere114e472008-02-04 22:29:50 -08004319#endif /* CONFIG_KEYS */
4320
4321/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004322 * Smack Audit hooks
4323 *
4324 * Audit requires a unique representation of each Smack specific
4325 * rule. This unique representation is used to distinguish the
4326 * object to be audited from remaining kernel objects and also
4327 * works as a glue between the audit hooks.
4328 *
4329 * Since repository entries are added but never deleted, we'll use
4330 * the smack_known label address related to the given audit rule as
4331 * the needed unique representation. This also better fits the smack
4332 * model where nearly everything is a label.
4333 */
4334#ifdef CONFIG_AUDIT
4335
4336/**
4337 * smack_audit_rule_init - Initialize a smack audit rule
4338 * @field: audit rule fields given from user-space (audit.h)
4339 * @op: required testing operator (=, !=, >, <, ...)
4340 * @rulestr: smack label to be audited
4341 * @vrule: pointer to save our own audit rule representation
4342 *
4343 * Prepare to audit cases where (@field @op @rulestr) is true.
4344 * The label to be audited is created if necessay.
4345 */
4346static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4347{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004348 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004349 char **rule = (char **)vrule;
4350 *rule = NULL;
4351
4352 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4353 return -EINVAL;
4354
Al Viro5af75d82008-12-16 05:59:26 -05004355 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004356 return -EINVAL;
4357
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004358 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004359 if (IS_ERR(skp))
4360 return PTR_ERR(skp);
4361
4362 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004363
4364 return 0;
4365}
4366
4367/**
4368 * smack_audit_rule_known - Distinguish Smack audit rules
4369 * @krule: rule of interest, in Audit kernel representation format
4370 *
4371 * This is used to filter Smack rules from remaining Audit ones.
4372 * If it's proved that this rule belongs to us, the
4373 * audit_rule_match hook will be called to do the final judgement.
4374 */
4375static int smack_audit_rule_known(struct audit_krule *krule)
4376{
4377 struct audit_field *f;
4378 int i;
4379
4380 for (i = 0; i < krule->field_count; i++) {
4381 f = &krule->fields[i];
4382
4383 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4384 return 1;
4385 }
4386
4387 return 0;
4388}
4389
4390/**
4391 * smack_audit_rule_match - Audit given object ?
4392 * @secid: security id for identifying the object to test
4393 * @field: audit rule flags given from user-space
4394 * @op: required testing operator
4395 * @vrule: smack internal rule presentation
4396 * @actx: audit context associated with the check
4397 *
4398 * The core Audit hook. It's used to take the decision of
4399 * whether to audit or not to audit a given object.
4400 */
4401static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4402 struct audit_context *actx)
4403{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004404 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004405 char *rule = vrule;
4406
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004407 if (unlikely(!rule)) {
4408 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004409 return -ENOENT;
4410 }
4411
4412 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4413 return 0;
4414
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004415 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004416
4417 /*
4418 * No need to do string comparisons. If a match occurs,
4419 * both pointers will point to the same smack_known
4420 * label.
4421 */
Al Viro5af75d82008-12-16 05:59:26 -05004422 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004423 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004424 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004425 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004426
4427 return 0;
4428}
4429
Casey Schaufler491a0b02016-01-26 15:08:35 -08004430/*
4431 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004432 * No memory was allocated.
4433 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004434
4435#endif /* CONFIG_AUDIT */
4436
Randy Dunlap251a2a92009-02-18 11:42:33 -08004437/**
David Quigley746df9b2013-05-22 12:50:35 -04004438 * smack_ismaclabel - check if xattr @name references a smack MAC label
4439 * @name: Full xattr name to check.
4440 */
4441static int smack_ismaclabel(const char *name)
4442{
4443 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4444}
4445
4446
4447/**
Casey Schauflere114e472008-02-04 22:29:50 -08004448 * smack_secid_to_secctx - return the smack label for a secid
4449 * @secid: incoming integer
4450 * @secdata: destination
4451 * @seclen: how long it is
4452 *
4453 * Exists for networking code.
4454 */
4455static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4456{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004457 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004458
Eric Parisd5630b92010-10-13 16:24:48 -04004459 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004460 *secdata = skp->smk_known;
4461 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004462 return 0;
4463}
4464
Randy Dunlap251a2a92009-02-18 11:42:33 -08004465/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004466 * smack_secctx_to_secid - return the secid for a smack label
4467 * @secdata: smack label
4468 * @seclen: how long result is
4469 * @secid: outgoing integer
4470 *
4471 * Exists for audit and networking code.
4472 */
David Howellse52c17642008-04-29 20:52:51 +01004473static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004474{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004475 struct smack_known *skp = smk_find_entry(secdata);
4476
4477 if (skp)
4478 *secid = skp->smk_secid;
4479 else
4480 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004481 return 0;
4482}
4483
Casey Schaufler491a0b02016-01-26 15:08:35 -08004484/*
4485 * There used to be a smack_release_secctx hook
4486 * that did nothing back when hooks were in a vector.
4487 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004488 */
Casey Schauflere114e472008-02-04 22:29:50 -08004489
David P. Quigley1ee65e32009-09-03 14:25:57 -04004490static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4491{
4492 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4493}
4494
4495static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4496{
4497 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4498}
4499
4500static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4501{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004502 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004503
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004504 *ctx = skp->smk_known;
4505 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004506 return 0;
4507}
4508
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004509static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4510{
4511
4512 struct task_smack *tsp;
4513 struct smack_known *skp;
4514 struct inode_smack *isp;
4515 struct cred *new_creds = *new;
4516
4517 if (new_creds == NULL) {
4518 new_creds = prepare_creds();
4519 if (new_creds == NULL)
4520 return -ENOMEM;
4521 }
4522
4523 tsp = new_creds->security;
4524
4525 /*
4526 * Get label from overlay inode and set it in create_sid
4527 */
4528 isp = d_inode(dentry->d_parent)->i_security;
4529 skp = isp->smk_inode;
4530 tsp->smk_task = skp;
4531 *new = new_creds;
4532 return 0;
4533}
4534
4535static int smack_inode_copy_up_xattr(const char *name)
4536{
4537 /*
4538 * Return 1 if this is the smack access Smack attribute.
4539 */
4540 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4541 return 1;
4542
4543 return -EOPNOTSUPP;
4544}
4545
4546static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4547 struct qstr *name,
4548 const struct cred *old,
4549 struct cred *new)
4550{
4551 struct task_smack *otsp = old->security;
4552 struct task_smack *ntsp = new->security;
4553 struct inode_smack *isp;
4554 int may;
4555
4556 /*
4557 * Use the process credential unless all of
4558 * the transmuting criteria are met
4559 */
4560 ntsp->smk_task = otsp->smk_task;
4561
4562 /*
4563 * the attribute of the containing directory
4564 */
4565 isp = d_inode(dentry->d_parent)->i_security;
4566
4567 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4568 rcu_read_lock();
4569 may = smk_access_entry(otsp->smk_task->smk_known,
4570 isp->smk_inode->smk_known,
4571 &otsp->smk_task->smk_rules);
4572 rcu_read_unlock();
4573
4574 /*
4575 * If the directory is transmuting and the rule
4576 * providing access is transmuting use the containing
4577 * directory label instead of the process label.
4578 */
4579 if (may > 0 && (may & MAY_TRANSMUTE))
4580 ntsp->smk_task = isp->smk_inode;
4581 }
4582 return 0;
4583}
4584
James Morrisca97d932017-02-15 00:18:51 +11004585static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004586 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4587 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4588 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004589
Casey Schauflere20b0432015-05-02 15:11:36 -07004590 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4591 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004592 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004593 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004594 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304595 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004596
Casey Schauflere20b0432015-05-02 15:11:36 -07004597 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004598
Casey Schauflere20b0432015-05-02 15:11:36 -07004599 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4600 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4601 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4602 LSM_HOOK_INIT(inode_link, smack_inode_link),
4603 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4604 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4605 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4606 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4607 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4608 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4609 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4610 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4611 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4612 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4613 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4614 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4615 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4616 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004617
Casey Schauflere20b0432015-05-02 15:11:36 -07004618 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4619 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4620 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4621 LSM_HOOK_INIT(file_lock, smack_file_lock),
4622 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4623 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4624 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4625 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4626 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4627 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004628
Casey Schauflere20b0432015-05-02 15:11:36 -07004629 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004630
Casey Schauflere20b0432015-05-02 15:11:36 -07004631 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4632 LSM_HOOK_INIT(cred_free, smack_cred_free),
4633 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4634 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004635 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004636 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4637 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4638 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4639 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4640 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4641 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4642 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4643 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4644 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4645 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4646 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4647 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4648 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004649 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004650
Casey Schauflere20b0432015-05-02 15:11:36 -07004651 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4652 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004653
Casey Schauflere20b0432015-05-02 15:11:36 -07004654 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4655 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004656
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004657 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4658 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004659 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4660 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4661 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4662 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004663
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004664 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4665 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004666 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4667 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4668 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004669
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004670 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4671 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004672 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4673 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4674 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004675
Casey Schauflere20b0432015-05-02 15:11:36 -07004676 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004677
Casey Schauflere20b0432015-05-02 15:11:36 -07004678 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4679 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004680
Casey Schauflere20b0432015-05-02 15:11:36 -07004681 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4682 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004683
Casey Schauflere20b0432015-05-02 15:11:36 -07004684 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004685 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004686#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004687 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004688#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004689 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4690 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4691 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4692 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4693 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4694 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4695 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4696 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4697 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4698 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004699
Casey Schauflere114e472008-02-04 22:29:50 -08004700 /* key management security hooks */
4701#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004702 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4703 LSM_HOOK_INIT(key_free, smack_key_free),
4704 LSM_HOOK_INIT(key_permission, smack_key_permission),
4705 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004706#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004707
4708 /* Audit hooks */
4709#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004710 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4711 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4712 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004713#endif /* CONFIG_AUDIT */
4714
Casey Schauflere20b0432015-05-02 15:11:36 -07004715 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4716 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4717 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004718 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4719 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4720 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004721 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4722 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4723 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004724};
4725
Etienne Basset7198e2e2009-03-24 20:53:24 +01004726
Casey Schaufler86812bb2012-04-17 18:55:46 -07004727static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004728{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004729 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004730 * Initialize rule list locks
4731 */
4732 mutex_init(&smack_known_huh.smk_rules_lock);
4733 mutex_init(&smack_known_hat.smk_rules_lock);
4734 mutex_init(&smack_known_floor.smk_rules_lock);
4735 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004736 mutex_init(&smack_known_web.smk_rules_lock);
4737 /*
4738 * Initialize rule lists
4739 */
4740 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4741 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4742 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4743 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004744 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4745 /*
4746 * Create the known labels list
4747 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004748 smk_insert_entry(&smack_known_huh);
4749 smk_insert_entry(&smack_known_hat);
4750 smk_insert_entry(&smack_known_star);
4751 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004752 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004753}
4754
Casey Schauflere114e472008-02-04 22:29:50 -08004755/**
4756 * smack_init - initialize the smack system
4757 *
4758 * Returns 0
4759 */
4760static __init int smack_init(void)
4761{
David Howellsd84f4f92008-11-14 10:39:23 +11004762 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004763 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004764
Rohit1a5b4722014-10-15 17:40:41 +05304765 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4766 if (!smack_inode_cache)
4767 return -ENOMEM;
4768
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004769 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4770 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304771 if (tsp == NULL) {
4772 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004773 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304774 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004775
José Bollod21b7b02015-10-02 15:15:56 +02004776 smack_enabled = 1;
4777
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004778 pr_info("Smack: Initializing.\n");
4779#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4780 pr_info("Smack: Netfilter enabled.\n");
4781#endif
4782#ifdef SMACK_IPV6_PORT_LABELING
4783 pr_info("Smack: IPv6 port labeling enabled.\n");
4784#endif
4785#ifdef SMACK_IPV6_SECMARK_LABELING
4786 pr_info("Smack: IPv6 Netfilter enabled.\n");
4787#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004788
4789 /*
4790 * Set the security state for the initial task.
4791 */
David Howellsd84f4f92008-11-14 10:39:23 +11004792 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004793 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004794
Casey Schaufler86812bb2012-04-17 18:55:46 -07004795 /* initialize the smack_known_list */
4796 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004797
4798 /*
4799 * Register with LSM
4800 */
Casey Schauflerd69dece52017-01-18 17:09:05 -08004801 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
Casey Schauflere114e472008-02-04 22:29:50 -08004802
4803 return 0;
4804}
4805
4806/*
4807 * Smack requires early initialization in order to label
4808 * all processes and objects when they are created.
4809 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004810DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004811 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004812 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004813 .init = smack_init,
4814};