blob: 5f93c4f843842baa0964286c2db3ae86b856fb0a [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
19 */
20
21#include <linux/xattr.h>
22#include <linux/pagemap.h>
23#include <linux/mount.h>
24#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070030#include <linux/dccp.h>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020031#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080033#include <linux/mutex.h>
34#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080035#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070036#include <net/ip.h>
37#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100038#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070039#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050040#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030041#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000042#include <linux/msg.h>
43#include <linux/shm.h>
44#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053045#include <linux/parser.h>
David Howells2febd252018-11-01 23:07:24 +000046#include <linux/fs_context.h>
47#include <linux/fs_parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080048#include "smack.h"
49
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020050#define TRANS_TRUE "TRUE"
51#define TRANS_TRUE_SIZE 4
52
Casey Schauflerc6739442013-05-22 18:42:56 -070053#define SMK_CONNECTING 0
54#define SMK_RECEIVING 1
55#define SMK_SENDING 2
56
Casey Schaufler21abb1e2015-07-22 14:25:31 -070057#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053058DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080059static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070060#endif
Rohit1a5b4722014-10-15 17:40:41 +053061static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080062int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070063
Al Viroc3300aa2018-12-16 01:52:24 -050064#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
65static struct {
66 const char *name;
67 int len;
68 int opt;
69} smk_mount_opts[] = {
70 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
Vivek Trivedi3bf27892015-06-22 15:36:06 +053071};
Al Viroc3300aa2018-12-16 01:52:24 -050072#undef A
73
74static int match_opt_prefix(char *s, int l, char **arg)
75{
76 int i;
77
78 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
79 size_t len = smk_mount_opts[i].len;
80 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
81 continue;
82 if (len == l || s[len] != '=')
83 continue;
84 *arg = s + len + 1;
85 return smk_mount_opts[i].opt;
86 }
87 return Opt_error;
88}
Vivek Trivedi3bf27892015-06-22 15:36:06 +053089
Casey Schaufler3d04c922015-08-12 11:56:02 -070090#ifdef CONFIG_SECURITY_SMACK_BRINGUP
91static char *smk_bu_mess[] = {
92 "Bringup Error", /* Unused */
93 "Bringup", /* SMACK_BRINGUP_ALLOW */
94 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
95 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
96};
97
Casey Schauflerd166c802014-08-27 14:51:27 -070098static void smk_bu_mode(int mode, char *s)
99{
100 int i = 0;
101
102 if (mode & MAY_READ)
103 s[i++] = 'r';
104 if (mode & MAY_WRITE)
105 s[i++] = 'w';
106 if (mode & MAY_EXEC)
107 s[i++] = 'x';
108 if (mode & MAY_APPEND)
109 s[i++] = 'a';
110 if (mode & MAY_TRANSMUTE)
111 s[i++] = 't';
112 if (mode & MAY_LOCK)
113 s[i++] = 'l';
114 if (i == 0)
115 s[i++] = '-';
116 s[i] = '\0';
117}
118#endif
119
120#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200121static int smk_bu_note(char *note, struct smack_known *sskp,
122 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700123{
124 char acc[SMK_NUM_ACCESS_TYPE + 1];
125
126 if (rc <= 0)
127 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700128 if (rc > SMACK_UNCONFINED_OBJECT)
129 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700130
131 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700132 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200133 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700134 return 0;
135}
136#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200137#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700138#endif
139
140#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200141static int smk_bu_current(char *note, struct smack_known *oskp,
142 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700143{
144 struct task_smack *tsp = current_security();
145 char acc[SMK_NUM_ACCESS_TYPE + 1];
146
147 if (rc <= 0)
148 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700149 if (rc > SMACK_UNCONFINED_OBJECT)
150 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700151
152 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700153 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200154 tsp->smk_task->smk_known, oskp->smk_known,
155 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700156 return 0;
157}
158#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200159#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700160#endif
161
162#ifdef CONFIG_SECURITY_SMACK_BRINGUP
163static int smk_bu_task(struct task_struct *otp, int mode, int rc)
164{
165 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300166 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700167 char acc[SMK_NUM_ACCESS_TYPE + 1];
168
169 if (rc <= 0)
170 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700171 if (rc > SMACK_UNCONFINED_OBJECT)
172 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700173
174 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700175 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300176 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700177 current->comm, otp->comm);
178 return 0;
179}
180#else
181#define smk_bu_task(otp, mode, RC) (RC)
182#endif
183
184#ifdef CONFIG_SECURITY_SMACK_BRINGUP
185static int smk_bu_inode(struct inode *inode, int mode, int rc)
186{
187 struct task_smack *tsp = current_security();
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700188 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700189 char acc[SMK_NUM_ACCESS_TYPE + 1];
190
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700191 if (isp->smk_flags & SMK_INODE_IMPURE)
192 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
193 inode->i_sb->s_id, inode->i_ino, current->comm);
194
Casey Schauflerd166c802014-08-27 14:51:27 -0700195 if (rc <= 0)
196 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700197 if (rc > SMACK_UNCONFINED_OBJECT)
198 rc = 0;
199 if (rc == SMACK_UNCONFINED_SUBJECT &&
200 (mode & (MAY_WRITE | MAY_APPEND)))
201 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700202
203 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700204
205 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
206 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700207 inode->i_sb->s_id, inode->i_ino, current->comm);
208 return 0;
209}
210#else
211#define smk_bu_inode(inode, mode, RC) (RC)
212#endif
213
214#ifdef CONFIG_SECURITY_SMACK_BRINGUP
215static int smk_bu_file(struct file *file, int mode, int rc)
216{
217 struct task_smack *tsp = current_security();
218 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800219 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700220 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700221 char acc[SMK_NUM_ACCESS_TYPE + 1];
222
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700223 if (isp->smk_flags & SMK_INODE_IMPURE)
224 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
225 inode->i_sb->s_id, inode->i_ino, current->comm);
226
Casey Schauflerd166c802014-08-27 14:51:27 -0700227 if (rc <= 0)
228 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700229 if (rc > SMACK_UNCONFINED_OBJECT)
230 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700231
232 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700233 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800234 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400235 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700236 current->comm);
237 return 0;
238}
239#else
240#define smk_bu_file(file, mode, RC) (RC)
241#endif
242
243#ifdef CONFIG_SECURITY_SMACK_BRINGUP
244static int smk_bu_credfile(const struct cred *cred, struct file *file,
245 int mode, int rc)
246{
247 struct task_smack *tsp = cred->security;
248 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500249 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700250 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700251 char acc[SMK_NUM_ACCESS_TYPE + 1];
252
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700253 if (isp->smk_flags & SMK_INODE_IMPURE)
254 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
255 inode->i_sb->s_id, inode->i_ino, current->comm);
256
Casey Schauflerd166c802014-08-27 14:51:27 -0700257 if (rc <= 0)
258 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700259 if (rc > SMACK_UNCONFINED_OBJECT)
260 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700261
262 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700263 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200264 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400265 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700266 current->comm);
267 return 0;
268}
269#else
270#define smk_bu_credfile(cred, file, mode, RC) (RC)
271#endif
272
Casey Schauflere114e472008-02-04 22:29:50 -0800273/**
274 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100275 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800276 * @ip: a pointer to the inode
277 * @dp: a pointer to the dentry
278 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200279 * Returns a pointer to the master list entry for the Smack label,
280 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800281 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700282static struct smack_known *smk_fetch(const char *name, struct inode *ip,
283 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800284{
285 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700286 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700287 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800288
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200289 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200290 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800291
Casey Schauflerf7112e62012-05-06 15:22:02 -0700292 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
293 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200294 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800295
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200296 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200297 if (rc < 0)
298 skp = ERR_PTR(rc);
299 else if (rc == 0)
300 skp = NULL;
301 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700302 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700303
304 kfree(buffer);
305
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700306 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800307}
308
309/**
310 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200311 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800312 *
313 * Returns the new blob or NULL if there's no memory available
314 */
Casey Schaufler1eddfe82015-07-30 14:35:14 -0700315static struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800316{
317 struct inode_smack *isp;
318
Rohit1a5b4722014-10-15 17:40:41 +0530319 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800320 if (isp == NULL)
321 return NULL;
322
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200323 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800324 isp->smk_flags = 0;
325 mutex_init(&isp->smk_lock);
326
327 return isp;
328}
329
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800330/**
331 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100332 * @task: a pointer to the Smack label for the running task
333 * @forked: a pointer to the Smack label for the forked task
334 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800335 *
336 * Returns the new blob or NULL if there's no memory available
337 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700338static struct task_smack *new_task_smack(struct smack_known *task,
339 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800340{
341 struct task_smack *tsp;
342
343 tsp = kzalloc(sizeof(struct task_smack), gfp);
344 if (tsp == NULL)
345 return NULL;
346
347 tsp->smk_task = task;
348 tsp->smk_forked = forked;
349 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200350 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800351 mutex_init(&tsp->smk_rules_lock);
352
353 return tsp;
354}
355
356/**
357 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100358 * @nhead: new rules header pointer
359 * @ohead: old rules header pointer
360 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800361 *
362 * Returns 0 on success, -ENOMEM on error
363 */
364static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
365 gfp_t gfp)
366{
367 struct smack_rule *nrp;
368 struct smack_rule *orp;
369 int rc = 0;
370
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800371 list_for_each_entry_rcu(orp, ohead, list) {
372 nrp = kzalloc(sizeof(struct smack_rule), gfp);
373 if (nrp == NULL) {
374 rc = -ENOMEM;
375 break;
376 }
377 *nrp = *orp;
378 list_add_rcu(&nrp->list, nhead);
379 }
380 return rc;
381}
382
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100383/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200384 * smk_copy_relabel - copy smk_relabel labels list
385 * @nhead: new rules header pointer
386 * @ohead: old rules header pointer
387 * @gfp: type of the memory for the allocation
388 *
389 * Returns 0 on success, -ENOMEM on error
390 */
391static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
392 gfp_t gfp)
393{
394 struct smack_known_list_elem *nklep;
395 struct smack_known_list_elem *oklep;
396
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200397 list_for_each_entry(oklep, ohead, list) {
398 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
399 if (nklep == NULL) {
400 smk_destroy_label_list(nhead);
401 return -ENOMEM;
402 }
403 nklep->smk_label = oklep->smk_label;
404 list_add(&nklep->list, nhead);
405 }
406
407 return 0;
408}
409
410/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100411 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
412 * @mode - input mode in form of PTRACE_MODE_*
413 *
414 * Returns a converted MAY_* mode usable by smack rules
415 */
416static inline unsigned int smk_ptrace_mode(unsigned int mode)
417{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800418 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100419 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800420 if (mode & PTRACE_MODE_READ)
421 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100422
423 return 0;
424}
425
426/**
427 * smk_ptrace_rule_check - helper for ptrace access
428 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200429 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100430 * @mode: ptrace attachment mode (PTRACE_MODE_*)
431 * @func: name of the function that called us, used for audit
432 *
433 * Returns 0 on access granted, -error on error
434 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200435static int smk_ptrace_rule_check(struct task_struct *tracer,
436 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100437 unsigned int mode, const char *func)
438{
439 int rc;
440 struct smk_audit_info ad, *saip = NULL;
441 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200442 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700443 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100444
445 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
446 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
447 smk_ad_setfield_u_tsk(&ad, tracer);
448 saip = &ad;
449 }
450
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300451 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700452 tracercred = __task_cred(tracer);
453 tsp = tracercred->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200454 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100455
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100456 if ((mode & PTRACE_MODE_ATTACH) &&
457 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
458 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200459 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100460 rc = 0;
461 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
462 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700463 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100464 rc = 0;
465 else
466 rc = -EACCES;
467
468 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200469 smack_log(tracer_known->smk_known,
470 tracee_known->smk_known,
471 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100472
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300473 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100474 return rc;
475 }
476
477 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200478 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300479
480 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100481 return rc;
482}
483
Casey Schauflere114e472008-02-04 22:29:50 -0800484/*
485 * LSM hooks.
486 * We he, that is fun!
487 */
488
489/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000490 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800491 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100492 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800493 *
494 * Returns 0 if access is OK, an error code otherwise
495 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100496 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800497 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000498static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800499{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700500 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800501
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300502 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200503
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700504 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100505}
Casey Schauflere114e472008-02-04 22:29:50 -0800506
David Howells5cd9c582008-08-14 11:37:28 +0100507/**
508 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
509 * @ptp: parent task pointer
510 *
511 * Returns 0 if access is OK, an error code otherwise
512 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100513 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100514 */
515static int smack_ptrace_traceme(struct task_struct *ptp)
516{
517 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700518 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100519
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100520 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200521
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200522 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800523 return rc;
524}
525
526/**
527 * smack_syslog - Smack approval on syslog
528 * @type: message type
529 *
Casey Schauflere114e472008-02-04 22:29:50 -0800530 * Returns 0 on success, error code otherwise.
531 */
Eric Paris12b30522010-11-15 18:36:29 -0500532static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800533{
Eric Paris12b30522010-11-15 18:36:29 -0500534 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700535 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800536
Casey Schaufler1880eff2012-06-05 15:28:30 -0700537 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800538 return 0;
539
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800540 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800541 rc = -EACCES;
542
543 return rc;
544}
545
Casey Schauflere114e472008-02-04 22:29:50 -0800546/*
547 * Superblock Hooks.
548 */
549
550/**
551 * smack_sb_alloc_security - allocate a superblock blob
552 * @sb: the superblock getting the blob
553 *
554 * Returns 0 on success or -ENOMEM on error.
555 */
556static int smack_sb_alloc_security(struct super_block *sb)
557{
558 struct superblock_smack *sbsp;
559
560 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
561
562 if (sbsp == NULL)
563 return -ENOMEM;
564
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200565 sbsp->smk_root = &smack_known_floor;
566 sbsp->smk_default = &smack_known_floor;
567 sbsp->smk_floor = &smack_known_floor;
568 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700569 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500570 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700571 */
Casey Schauflere114e472008-02-04 22:29:50 -0800572 sb->s_security = sbsp;
573
574 return 0;
575}
576
577/**
578 * smack_sb_free_security - free a superblock blob
579 * @sb: the superblock getting the blob
580 *
581 */
582static void smack_sb_free_security(struct super_block *sb)
583{
584 kfree(sb->s_security);
585 sb->s_security = NULL;
586}
587
Al Viro12085b12018-12-13 15:18:05 -0500588struct smack_mnt_opts {
589 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
590};
591
Al Viro204cc0c2018-12-13 13:41:47 -0500592static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800593{
Al Viro12085b12018-12-13 15:18:05 -0500594 struct smack_mnt_opts *opts = mnt_opts;
595 kfree(opts->fsdefault);
596 kfree(opts->fsfloor);
597 kfree(opts->fshat);
598 kfree(opts->fsroot);
599 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500600 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800601}
602
Al Viro55c0e5b2018-12-16 01:09:45 -0500603static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530604{
Al Viro55c0e5b2018-12-16 01:09:45 -0500605 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530606
Al Viro55c0e5b2018-12-16 01:09:45 -0500607 if (!opts) {
608 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
609 if (!opts)
610 return -ENOMEM;
611 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530612 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500613 if (!s)
614 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530615
Al Viro55c0e5b2018-12-16 01:09:45 -0500616 switch (token) {
617 case Opt_fsdefault:
618 if (opts->fsdefault)
619 goto out_opt_err;
620 opts->fsdefault = s;
621 break;
622 case Opt_fsfloor:
623 if (opts->fsfloor)
624 goto out_opt_err;
625 opts->fsfloor = s;
626 break;
627 case Opt_fshat:
628 if (opts->fshat)
629 goto out_opt_err;
630 opts->fshat = s;
631 break;
632 case Opt_fsroot:
633 if (opts->fsroot)
634 goto out_opt_err;
635 opts->fsroot = s;
636 break;
637 case Opt_fstransmute:
638 if (opts->fstransmute)
639 goto out_opt_err;
640 opts->fstransmute = s;
641 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530642 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530643 return 0;
644
645out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530646 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500647 return -EINVAL;
648}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530649
David Howells2febd252018-11-01 23:07:24 +0000650static const struct fs_parameter_spec smack_param_specs[] = {
651 fsparam_string("fsdefault", Opt_fsdefault),
652 fsparam_string("fsfloor", Opt_fsfloor),
653 fsparam_string("fshat", Opt_fshat),
654 fsparam_string("fsroot", Opt_fsroot),
655 fsparam_string("fstransmute", Opt_fstransmute),
656 {}
657};
658
659static const struct fs_parameter_description smack_fs_parameters = {
660 .name = "smack",
661 .specs = smack_param_specs,
662};
663
664/**
665 * smack_fs_context_parse_param - Parse a single mount parameter
666 * @fc: The new filesystem context being constructed.
667 * @param: The parameter.
668 *
669 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
670 * error.
671 */
672static int smack_fs_context_parse_param(struct fs_context *fc,
673 struct fs_parameter *param)
674{
675 struct fs_parse_result result;
676 int opt, rc;
677
678 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
679 if (opt < 0)
680 return opt;
681
682 rc = smack_add_opt(opt, param->string, &fc->security);
683 if (!rc)
684 param->string = NULL;
685 return rc;
686}
687
Al Virod2497e12018-12-16 01:37:06 -0500688static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530689{
Al Virod2497e12018-12-16 01:37:06 -0500690 char *from = options, *to = options;
691 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530692
Al Viroc3300aa2018-12-16 01:52:24 -0500693 while (1) {
694 char *next = strchr(from, ',');
695 int token, len, rc;
696 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530697
Al Viroc3300aa2018-12-16 01:52:24 -0500698 if (next)
699 len = next - from;
700 else
701 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530702
Al Viroc3300aa2018-12-16 01:52:24 -0500703 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500704 if (token != Opt_error) {
705 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
706 rc = smack_add_opt(token, arg, mnt_opts);
707 if (unlikely(rc)) {
708 kfree(arg);
709 if (*mnt_opts)
710 smack_free_mnt_opts(*mnt_opts);
711 *mnt_opts = NULL;
712 return rc;
713 }
714 } else {
715 if (!first) { // copy with preceding comma
716 from--;
717 len++;
718 }
719 if (to != from)
720 memmove(to, from, len);
721 to += len;
722 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530723 }
Al Viroc3300aa2018-12-16 01:52:24 -0500724 if (!from[len])
725 break;
726 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530727 }
Al Virod2497e12018-12-16 01:37:06 -0500728 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530729 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530730}
731
732/**
733 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800734 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530735 * @opts: Smack mount options
736 * @kern_flags: mount option from kernel space or user space
737 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800738 *
739 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530740 *
741 * Allow filesystems with binary mount data to explicitly set Smack mount
742 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800743 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530744static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500745 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530746 unsigned long kern_flags,
747 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800748{
749 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000750 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800751 struct superblock_smack *sp = sb->s_security;
752 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800753 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500754 struct smack_mnt_opts *opts = mnt_opts;
755 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800756
Seth Forshee9f50eda2015-09-23 15:16:06 -0500757 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800758 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700759
Himanshu Shukla2097f592016-11-10 16:19:52 +0530760 if (!smack_privileged(CAP_MAC_ADMIN)) {
761 /*
762 * Unprivileged mounts don't get to specify Smack values.
763 */
Al Viro12085b12018-12-13 15:18:05 -0500764 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530765 return -EPERM;
766 /*
767 * Unprivileged mounts get root and default from the caller.
768 */
769 skp = smk_of_current();
770 sp->smk_root = skp;
771 sp->smk_default = skp;
772 /*
773 * For a handful of fs types with no user-controlled
774 * backing store it's okay to trust security labels
775 * in the filesystem. The rest are untrusted.
776 */
777 if (sb->s_user_ns != &init_user_ns &&
778 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
779 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500780 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530781 sp->smk_flags |= SMK_SB_UNTRUSTED;
782 }
783 }
784
Seth Forshee9f50eda2015-09-23 15:16:06 -0500785 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800786
Al Viro12085b12018-12-13 15:18:05 -0500787 if (opts) {
788 if (opts->fsdefault) {
789 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200790 if (IS_ERR(skp))
791 return PTR_ERR(skp);
792 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500793 }
794 if (opts->fsfloor) {
795 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530796 if (IS_ERR(skp))
797 return PTR_ERR(skp);
798 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500799 }
800 if (opts->fshat) {
801 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530802 if (IS_ERR(skp))
803 return PTR_ERR(skp);
804 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500805 }
806 if (opts->fsroot) {
807 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200808 if (IS_ERR(skp))
809 return PTR_ERR(skp);
810 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500811 }
812 if (opts->fstransmute) {
813 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200814 if (IS_ERR(skp))
815 return PTR_ERR(skp);
816 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500817 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800818 }
819 }
820
821 /*
822 * Initialize the root inode.
823 */
824 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100825 if (isp == NULL) {
826 isp = new_inode_smack(sp->smk_root);
827 if (isp == NULL)
828 return -ENOMEM;
829 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700830 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800831 isp->smk_inode = sp->smk_root;
832
Casey Schauflere830b392013-05-22 18:43:07 -0700833 if (transmute)
834 isp->smk_flags |= SMK_INODE_TRANSMUTE;
835
Casey Schauflere114e472008-02-04 22:29:50 -0800836 return 0;
837}
838
839/**
840 * smack_sb_statfs - Smack check on statfs
841 * @dentry: identifies the file system in question
842 *
843 * Returns 0 if current can read the floor of the filesystem,
844 * and error code otherwise
845 */
846static int smack_sb_statfs(struct dentry *dentry)
847{
848 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200849 int rc;
850 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800851
Eric Parisa2694342011-04-25 13:10:27 -0400852 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200853 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
854
855 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700856 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200857 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800858}
859
Casey Schauflere114e472008-02-04 22:29:50 -0800860/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800861 * BPRM hooks
862 */
863
Casey Schauflerce8a4322011-09-29 18:21:01 -0700864/**
865 * smack_bprm_set_creds - set creds for exec
866 * @bprm: the exec information
867 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100868 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700869 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800870static int smack_bprm_set_creds(struct linux_binprm *bprm)
871{
Al Viro496ad9a2013-01-23 17:07:38 -0500872 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300873 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800874 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500875 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800876 int rc;
877
Kees Cookddb4a142017-07-18 15:25:23 -0700878 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800879 return 0;
880
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300881 isp = inode->i_security;
882 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800883 return 0;
884
Seth Forshee809c02e2016-04-26 14:36:22 -0500885 sbsp = inode->i_sb->s_security;
886 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
887 isp->smk_task != sbsp->smk_root)
888 return 0;
889
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300890 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100891 struct task_struct *tracer;
892 rc = 0;
893
894 rcu_read_lock();
895 tracer = ptrace_parent(current);
896 if (likely(tracer != NULL))
897 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200898 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100899 PTRACE_MODE_ATTACH,
900 __func__);
901 rcu_read_unlock();
902
903 if (rc != 0)
904 return rc;
905 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300906 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800907
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300908 bsp->smk_task = isp->smk_task;
909 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800910
Kees Cookccbb6e12017-07-18 15:25:26 -0700911 /* Decide if this is a secure exec. */
912 if (bsp->smk_task != bsp->smk_forked)
913 bprm->secureexec = 1;
914
Casey Schaufler676dac42010-12-02 06:43:39 -0800915 return 0;
916}
917
918/*
Casey Schauflere114e472008-02-04 22:29:50 -0800919 * Inode hooks
920 */
921
922/**
923 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800924 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800925 *
926 * Returns 0 if it gets a blob, -ENOMEM otherwise
927 */
928static int smack_inode_alloc_security(struct inode *inode)
929{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700930 struct smack_known *skp = smk_of_current();
931
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200932 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800933 if (inode->i_security == NULL)
934 return -ENOMEM;
935 return 0;
936}
937
938/**
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530939 * smack_inode_free_rcu - Free inode_smack blob from cache
940 * @head: the rcu_head for getting inode_smack pointer
941 *
942 * Call back function called from call_rcu() to free
943 * the i_security blob pointer in inode
944 */
945static void smack_inode_free_rcu(struct rcu_head *head)
946{
947 struct inode_smack *issp;
948
949 issp = container_of(head, struct inode_smack, smk_rcu);
950 kmem_cache_free(smack_inode_cache, issp);
951}
952
953/**
954 * smack_inode_free_security - free an inode blob using call_rcu()
Randy Dunlap251a2a92009-02-18 11:42:33 -0800955 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800956 *
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530957 * Clears the blob pointer in inode using RCU
Casey Schauflere114e472008-02-04 22:29:50 -0800958 */
959static void smack_inode_free_security(struct inode *inode)
960{
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530961 struct inode_smack *issp = inode->i_security;
962
963 /*
964 * The inode may still be referenced in a path walk and
965 * a call to smack_inode_permission() can be made
966 * after smack_inode_free_security() is called.
967 * To avoid race condition free the i_security via RCU
968 * and leave the current inode->i_security pointer intact.
969 * The inode will be freed after the RCU grace period too.
970 */
971 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
Casey Schauflere114e472008-02-04 22:29:50 -0800972}
973
974/**
975 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200976 * @inode: the newly created inode
977 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500978 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800979 * @name: where to put the attribute name
980 * @value: where to put the attribute value
981 * @len: where to put the length of the attribute
982 *
983 * Returns 0 if it all works out, -ENOMEM if there's no memory
984 */
985static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900986 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500987 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800988{
Casey Schaufler2267b132012-03-13 19:14:19 -0700989 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700990 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200991 struct smack_known *isp = smk_of_inode(inode);
992 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800993 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800994
Tetsuo Handa95489062013-07-25 05:44:02 +0900995 if (name)
996 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800997
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100998 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800999 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001000 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1001 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001002 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001003
1004 /*
1005 * If the access rule allows transmutation and
1006 * the directory requests transmutation then
1007 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001008 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001009 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001010 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001011 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001012 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001013 issp->smk_flags |= SMK_INODE_CHANGED;
1014 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001015
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001016 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001017 if (*value == NULL)
1018 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001019
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001020 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001021 }
Casey Schauflere114e472008-02-04 22:29:50 -08001022
1023 return 0;
1024}
1025
1026/**
1027 * smack_inode_link - Smack check on link
1028 * @old_dentry: the existing object
1029 * @dir: unused
1030 * @new_dentry: the new object
1031 *
1032 * Returns 0 if access is permitted, an error code otherwise
1033 */
1034static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1035 struct dentry *new_dentry)
1036{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001037 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001038 struct smk_audit_info ad;
1039 int rc;
1040
Eric Parisa2694342011-04-25 13:10:27 -04001041 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001043
David Howellsc6f493d2015-03-17 22:26:22 +00001044 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001045 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001046 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001047
David Howells88025652015-01-29 12:02:32 +00001048 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001049 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001050 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1051 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001052 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001053 }
1054
1055 return rc;
1056}
1057
1058/**
1059 * smack_inode_unlink - Smack check on inode deletion
1060 * @dir: containing directory object
1061 * @dentry: file to unlink
1062 *
1063 * Returns 0 if current can write the containing directory
1064 * and the object, error code otherwise
1065 */
1066static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1067{
David Howellsc6f493d2015-03-17 22:26:22 +00001068 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001069 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001070 int rc;
1071
Eric Parisa2694342011-04-25 13:10:27 -04001072 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001073 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1074
Casey Schauflere114e472008-02-04 22:29:50 -08001075 /*
1076 * You need write access to the thing you're unlinking
1077 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001078 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001079 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001080 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001081 /*
1082 * You also need write access to the containing directory
1083 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001084 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 smk_ad_setfield_u_fs_inode(&ad, dir);
1086 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001087 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001088 }
Casey Schauflere114e472008-02-04 22:29:50 -08001089 return rc;
1090}
1091
1092/**
1093 * smack_inode_rmdir - Smack check on directory deletion
1094 * @dir: containing directory object
1095 * @dentry: directory to unlink
1096 *
1097 * Returns 0 if current can write the containing directory
1098 * and the directory, error code otherwise
1099 */
1100static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1101{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001103 int rc;
1104
Eric Parisa2694342011-04-25 13:10:27 -04001105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1107
Casey Schauflere114e472008-02-04 22:29:50 -08001108 /*
1109 * You need write access to the thing you're removing
1110 */
David Howellsc6f493d2015-03-17 22:26:22 +00001111 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1112 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001113 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001114 /*
1115 * You also need write access to the containing directory
1116 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001117 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 smk_ad_setfield_u_fs_inode(&ad, dir);
1119 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001120 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001121 }
Casey Schauflere114e472008-02-04 22:29:50 -08001122
1123 return rc;
1124}
1125
1126/**
1127 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001128 * @old_inode: unused
1129 * @old_dentry: the old object
1130 * @new_inode: unused
1131 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001132 *
1133 * Read and write access is required on both the old and
1134 * new directories.
1135 *
1136 * Returns 0 if access is permitted, an error code otherwise
1137 */
1138static int smack_inode_rename(struct inode *old_inode,
1139 struct dentry *old_dentry,
1140 struct inode *new_inode,
1141 struct dentry *new_dentry)
1142{
1143 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001144 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 struct smk_audit_info ad;
1146
Eric Parisa2694342011-04-25 13:10:27 -04001147 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001149
David Howellsc6f493d2015-03-17 22:26:22 +00001150 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001151 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001152 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001153
David Howells88025652015-01-29 12:02:32 +00001154 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001155 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001156 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1157 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001158 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001159 }
Casey Schauflere114e472008-02-04 22:29:50 -08001160 return rc;
1161}
1162
1163/**
1164 * smack_inode_permission - Smack version of permission()
1165 * @inode: the inode in question
1166 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001167 *
1168 * This is the important Smack hook.
1169 *
1170 * Returns 0 if access is permitted, -EACCES otherwise
1171 */
Al Viroe74f71e2011-06-20 19:38:15 -04001172static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001173{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001174 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001175 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001176 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001177 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001178
1179 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001180 /*
1181 * No permission to check. Existence test. Yup, it's there.
1182 */
1183 if (mask == 0)
1184 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001185
Seth Forshee9f50eda2015-09-23 15:16:06 -05001186 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1187 if (smk_of_inode(inode) != sbsp->smk_root)
1188 return -EACCES;
1189 }
1190
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001191 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001192 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001193 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001194 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001195 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001196 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1197 rc = smk_bu_inode(inode, mask, rc);
1198 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001199}
1200
1201/**
1202 * smack_inode_setattr - Smack check for setting attributes
1203 * @dentry: the object
1204 * @iattr: for the force flag
1205 *
1206 * Returns 0 if access is permitted, an error code otherwise
1207 */
1208static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1209{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001210 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001211 int rc;
1212
Casey Schauflere114e472008-02-04 22:29:50 -08001213 /*
1214 * Need to allow for clearing the setuid bit.
1215 */
1216 if (iattr->ia_valid & ATTR_FORCE)
1217 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001218 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001219 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001220
David Howellsc6f493d2015-03-17 22:26:22 +00001221 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1222 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001223 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001224}
1225
1226/**
1227 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001228 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001229 * @dentry: the object
1230 *
1231 * Returns 0 if access is permitted, an error code otherwise
1232 */
Al Viro3f7036a2015-03-08 19:28:30 -04001233static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001234{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001235 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001236 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001237 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001238
Eric Parisf48b7392011-04-25 12:54:27 -04001239 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001240 smk_ad_setfield_u_fs_path(&ad, *path);
1241 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1242 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001243 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001244}
1245
1246/**
1247 * smack_inode_setxattr - Smack check for setting xattrs
1248 * @dentry: the object
1249 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001250 * @value: value of the attribute
1251 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001252 * @flags: unused
1253 *
1254 * This protects the Smack attribute explicitly.
1255 *
1256 * Returns 0 if access is permitted, an error code otherwise
1257 */
David Howells8f0cfa52008-04-29 00:59:41 -07001258static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1259 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001260{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001261 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001262 struct smack_known *skp;
1263 int check_priv = 0;
1264 int check_import = 0;
1265 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001266 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001267
Casey Schaufler19760ad2013-12-16 16:27:26 -08001268 /*
1269 * Check label validity here so import won't fail in post_setxattr
1270 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001271 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1272 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001273 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1274 check_priv = 1;
1275 check_import = 1;
1276 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1277 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1278 check_priv = 1;
1279 check_import = 1;
1280 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001281 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001282 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001283 if (size != TRANS_TRUE_SIZE ||
1284 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1285 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001286 } else
1287 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1288
Casey Schaufler19760ad2013-12-16 16:27:26 -08001289 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1290 rc = -EPERM;
1291
1292 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001293 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001294 if (IS_ERR(skp))
1295 rc = PTR_ERR(skp);
1296 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001297 (skp == &smack_known_star || skp == &smack_known_web)))
1298 rc = -EINVAL;
1299 }
1300
Eric Parisa2694342011-04-25 13:10:27 -04001301 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001302 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1303
Casey Schauflerd166c802014-08-27 14:51:27 -07001304 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001305 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1306 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001307 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001308
1309 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001310}
1311
1312/**
1313 * smack_inode_post_setxattr - Apply the Smack update approved above
1314 * @dentry: object
1315 * @name: attribute name
1316 * @value: attribute value
1317 * @size: attribute size
1318 * @flags: unused
1319 *
1320 * Set the pointer in the inode blob to the entry found
1321 * in the master label list.
1322 */
David Howells8f0cfa52008-04-29 00:59:41 -07001323static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1324 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001325{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001326 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001327 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001328
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001329 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1330 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1331 return;
1332 }
1333
Casey Schaufler676dac42010-12-02 06:43:39 -08001334 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001335 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001336 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001337 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001338 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001339 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001340 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001341 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001342 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001343 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001344 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001345 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001346 }
Casey Schauflere114e472008-02-04 22:29:50 -08001347
1348 return;
1349}
1350
Casey Schauflerce8a4322011-09-29 18:21:01 -07001351/**
Casey Schauflere114e472008-02-04 22:29:50 -08001352 * smack_inode_getxattr - Smack check on getxattr
1353 * @dentry: the object
1354 * @name: unused
1355 *
1356 * Returns 0 if access is permitted, an error code otherwise
1357 */
David Howells8f0cfa52008-04-29 00:59:41 -07001358static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001359{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001361 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001362
Eric Parisa2694342011-04-25 13:10:27 -04001363 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001364 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1365
David Howellsc6f493d2015-03-17 22:26:22 +00001366 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1367 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001368 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001369}
1370
Casey Schauflerce8a4322011-09-29 18:21:01 -07001371/**
Casey Schauflere114e472008-02-04 22:29:50 -08001372 * smack_inode_removexattr - Smack check on removexattr
1373 * @dentry: the object
1374 * @name: name of the attribute
1375 *
1376 * Removing the Smack attribute requires CAP_MAC_ADMIN
1377 *
1378 * Returns 0 if access is permitted, an error code otherwise
1379 */
David Howells8f0cfa52008-04-29 00:59:41 -07001380static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001381{
Casey Schaufler676dac42010-12-02 06:43:39 -08001382 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001383 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001384 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001385
Casey Schauflerbcdca222008-02-23 15:24:04 -08001386 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1387 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001388 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001389 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001390 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301391 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001392 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001393 rc = -EPERM;
1394 } else
1395 rc = cap_inode_removexattr(dentry, name);
1396
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001397 if (rc != 0)
1398 return rc;
1399
Eric Parisa2694342011-04-25 13:10:27 -04001400 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001401 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001402
David Howellsc6f493d2015-03-17 22:26:22 +00001403 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1404 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001405 if (rc != 0)
1406 return rc;
1407
David Howellsc6f493d2015-03-17 22:26:22 +00001408 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001409 /*
1410 * Don't do anything special for these.
1411 * XATTR_NAME_SMACKIPIN
1412 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001413 */
José Bollo80124952016-01-12 21:23:40 +01001414 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001415 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001416 struct superblock_smack *sbsp = sbp->s_security;
1417
1418 isp->smk_inode = sbsp->smk_default;
1419 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001420 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001421 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001422 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001423 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1424 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001425
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001426 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001427}
1428
1429/**
1430 * smack_inode_getsecurity - get smack xattrs
1431 * @inode: the object
1432 * @name: attribute name
1433 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001434 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001435 *
1436 * Returns the size of the attribute or an error code
1437 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001438static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001439 const char *name, void **buffer,
1440 bool alloc)
1441{
1442 struct socket_smack *ssp;
1443 struct socket *sock;
1444 struct super_block *sbp;
1445 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001446 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001447
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001448 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001449 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001450 else {
1451 /*
1452 * The rest of the Smack xattrs are only on sockets.
1453 */
1454 sbp = ip->i_sb;
1455 if (sbp->s_magic != SOCKFS_MAGIC)
1456 return -EOPNOTSUPP;
1457
1458 sock = SOCKET_I(ip);
1459 if (sock == NULL || sock->sk == NULL)
1460 return -EOPNOTSUPP;
1461
1462 ssp = sock->sk->sk_security;
1463
1464 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1465 isp = ssp->smk_in;
1466 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1467 isp = ssp->smk_out;
1468 else
1469 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001470 }
1471
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001472 if (alloc) {
1473 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1474 if (*buffer == NULL)
1475 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001476 }
1477
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001478 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001479}
1480
1481
1482/**
1483 * smack_inode_listsecurity - list the Smack attributes
1484 * @inode: the object
1485 * @buffer: where they go
1486 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001487 */
1488static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1489 size_t buffer_size)
1490{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001491 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001492
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001493 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001494 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001495
1496 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001497}
1498
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001499/**
1500 * smack_inode_getsecid - Extract inode's security id
1501 * @inode: inode to extract the info from
1502 * @secid: where result will be saved
1503 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001504static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001505{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001506 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001507
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001508 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001509}
1510
Casey Schauflere114e472008-02-04 22:29:50 -08001511/*
1512 * File Hooks
1513 */
1514
Casey Schaufler491a0b02016-01-26 15:08:35 -08001515/*
1516 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001517 *
1518 * Should access checks be done on each read or write?
1519 * UNICOS and SELinux say yes.
1520 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1521 *
1522 * I'll say no for now. Smack does not do the frequent
1523 * label changing that SELinux does.
1524 */
Casey Schauflere114e472008-02-04 22:29:50 -08001525
1526/**
1527 * smack_file_alloc_security - assign a file security blob
1528 * @file: the object
1529 *
1530 * The security blob for a file is a pointer to the master
1531 * label list, so no allocation is done.
1532 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001533 * f_security is the owner security information. It
1534 * isn't used on file access checks, it's for send_sigio.
1535 *
Casey Schauflere114e472008-02-04 22:29:50 -08001536 * Returns 0
1537 */
1538static int smack_file_alloc_security(struct file *file)
1539{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001540 struct smack_known *skp = smk_of_current();
1541
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001542 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001543 return 0;
1544}
1545
1546/**
1547 * smack_file_free_security - clear a file security blob
1548 * @file: the object
1549 *
1550 * The security blob for a file is a pointer to the master
1551 * label list, so no memory is freed.
1552 */
1553static void smack_file_free_security(struct file *file)
1554{
1555 file->f_security = NULL;
1556}
1557
1558/**
1559 * smack_file_ioctl - Smack check on ioctls
1560 * @file: the object
1561 * @cmd: what to do
1562 * @arg: unused
1563 *
1564 * Relies heavily on the correct use of the ioctl command conventions.
1565 *
1566 * Returns 0 if allowed, error code otherwise
1567 */
1568static int smack_file_ioctl(struct file *file, unsigned int cmd,
1569 unsigned long arg)
1570{
1571 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001572 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001573 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001574
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001575 if (unlikely(IS_PRIVATE(inode)))
1576 return 0;
1577
Eric Parisf48b7392011-04-25 12:54:27 -04001578 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001579 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001580
Casey Schauflerd166c802014-08-27 14:51:27 -07001581 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001582 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001583 rc = smk_bu_file(file, MAY_WRITE, rc);
1584 }
Casey Schauflere114e472008-02-04 22:29:50 -08001585
Casey Schauflerd166c802014-08-27 14:51:27 -07001586 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001587 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001588 rc = smk_bu_file(file, MAY_READ, rc);
1589 }
Casey Schauflere114e472008-02-04 22:29:50 -08001590
1591 return rc;
1592}
1593
1594/**
1595 * smack_file_lock - Smack check on file locking
1596 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001597 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001598 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001599 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001600 */
1601static int smack_file_lock(struct file *file, unsigned int cmd)
1602{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001603 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001604 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001605 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001606
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001607 if (unlikely(IS_PRIVATE(inode)))
1608 return 0;
1609
Eric Paris92f42502011-04-25 13:15:55 -04001610 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1611 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001612 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001613 rc = smk_bu_file(file, MAY_LOCK, rc);
1614 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001615}
1616
1617/**
1618 * smack_file_fcntl - Smack check on fcntl
1619 * @file: the object
1620 * @cmd: what action to check
1621 * @arg: unused
1622 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001623 * Generally these operations are harmless.
1624 * File locking operations present an obvious mechanism
1625 * for passing information, so they require write access.
1626 *
Casey Schauflere114e472008-02-04 22:29:50 -08001627 * Returns 0 if current has access, error code otherwise
1628 */
1629static int smack_file_fcntl(struct file *file, unsigned int cmd,
1630 unsigned long arg)
1631{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001632 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001633 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001634 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001635
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001636 if (unlikely(IS_PRIVATE(inode)))
1637 return 0;
1638
Casey Schauflere114e472008-02-04 22:29:50 -08001639 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001640 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001641 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001642 case F_SETLK:
1643 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001644 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1645 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001646 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001647 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001648 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001649 case F_SETOWN:
1650 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001651 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1652 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001653 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001654 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001655 break;
1656 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001657 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001658 }
1659
1660 return rc;
1661}
1662
1663/**
Al Viroe5467852012-05-30 13:30:51 -04001664 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001665 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1666 * if mapping anonymous memory.
1667 * @file contains the file structure for file to map (may be NULL).
1668 * @reqprot contains the protection requested by the application.
1669 * @prot contains the protection that will be applied by the kernel.
1670 * @flags contains the operational flags.
1671 * Return 0 if permission is granted.
1672 */
Al Viroe5467852012-05-30 13:30:51 -04001673static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001674 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001675 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001676{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001677 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001678 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001679 struct smack_rule *srp;
1680 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001681 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001682 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001683 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001684 int may;
1685 int mmay;
1686 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001687 int rc;
1688
Al Viro496ad9a2013-01-23 17:07:38 -05001689 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001690 return 0;
1691
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001692 if (unlikely(IS_PRIVATE(file_inode(file))))
1693 return 0;
1694
Al Viro496ad9a2013-01-23 17:07:38 -05001695 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001696 if (isp->smk_mmap == NULL)
1697 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001698 sbsp = file_inode(file)->i_sb->s_security;
1699 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1700 isp->smk_mmap != sbsp->smk_root)
1701 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001702 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001703
1704 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001705 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001706 rc = 0;
1707
1708 rcu_read_lock();
1709 /*
1710 * For each Smack rule associated with the subject
1711 * label verify that the SMACK64MMAP also has access
1712 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001713 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001714 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001715 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001716 /*
1717 * Matching labels always allows access.
1718 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001719 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001720 continue;
1721 /*
1722 * If there is a matching local rule take
1723 * that into account as well.
1724 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001725 may = smk_access_entry(srp->smk_subject->smk_known,
1726 okp->smk_known,
1727 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001728 if (may == -ENOENT)
1729 may = srp->smk_access;
1730 else
1731 may &= srp->smk_access;
1732 /*
1733 * If may is zero the SMACK64MMAP subject can't
1734 * possibly have less access.
1735 */
1736 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001737 continue;
1738
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001739 /*
1740 * Fetch the global list entry.
1741 * If there isn't one a SMACK64MMAP subject
1742 * can't have as much access as current.
1743 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001744 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1745 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001746 if (mmay == -ENOENT) {
1747 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001748 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001749 }
1750 /*
1751 * If there is a local entry it modifies the
1752 * potential access, too.
1753 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001754 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1755 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001756 if (tmay != -ENOENT)
1757 mmay &= tmay;
1758
1759 /*
1760 * If there is any access available to current that is
1761 * not available to a SMACK64MMAP subject
1762 * deny access.
1763 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001764 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001765 rc = -EACCES;
1766 break;
1767 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001768 }
1769
1770 rcu_read_unlock();
1771
1772 return rc;
1773}
1774
1775/**
Casey Schauflere114e472008-02-04 22:29:50 -08001776 * smack_file_set_fowner - set the file security blob value
1777 * @file: object in question
1778 *
Casey Schauflere114e472008-02-04 22:29:50 -08001779 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001780static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001781{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001782 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001783}
1784
1785/**
1786 * smack_file_send_sigiotask - Smack on sigio
1787 * @tsk: The target task
1788 * @fown: the object the signal come from
1789 * @signum: unused
1790 *
1791 * Allow a privileged task to get signals even if it shouldn't
1792 *
1793 * Returns 0 if a subject with the object's smack could
1794 * write to the task, an error code otherwise.
1795 */
1796static int smack_file_send_sigiotask(struct task_struct *tsk,
1797 struct fown_struct *fown, int signum)
1798{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001799 struct smack_known *skp;
1800 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001801 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001802 struct file *file;
1803 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001804 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001805
1806 /*
1807 * struct fown_struct is never outside the context of a struct file
1808 */
1809 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001810
Etienne Bassetecfcc532009-04-08 20:40:06 +02001811 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001812 skp = file->f_security;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001813 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1814 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001815
1816 rcu_read_lock();
1817 tcred = __task_cred(tsk);
1818 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001819 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001820 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001821
1822 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1823 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001824 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001825 return rc;
1826}
1827
1828/**
1829 * smack_file_receive - Smack file receive check
1830 * @file: the object
1831 *
1832 * Returns 0 if current has access, error code otherwise
1833 */
1834static int smack_file_receive(struct file *file)
1835{
Casey Schauflerd166c802014-08-27 14:51:27 -07001836 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001837 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001838 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001839 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001840 struct socket *sock;
1841 struct task_smack *tsp;
1842 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001843
Seung-Woo Kim97775822015-04-17 15:25:04 +09001844 if (unlikely(IS_PRIVATE(inode)))
1845 return 0;
1846
Casey Schaufler4482a442013-12-30 17:37:45 -08001847 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001848 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001849
Casey Schaufler51d59af2017-05-31 08:53:42 -07001850 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001851 sock = SOCKET_I(inode);
1852 ssp = sock->sk->sk_security;
1853 tsp = current_security();
1854 /*
1855 * If the receiving process can't write to the
1856 * passed socket or if the passed socket can't
1857 * write to the receiving process don't accept
1858 * the passed socket.
1859 */
1860 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1861 rc = smk_bu_file(file, may, rc);
1862 if (rc < 0)
1863 return rc;
1864 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1865 rc = smk_bu_file(file, may, rc);
1866 return rc;
1867 }
Casey Schauflere114e472008-02-04 22:29:50 -08001868 /*
1869 * This code relies on bitmasks.
1870 */
1871 if (file->f_mode & FMODE_READ)
1872 may = MAY_READ;
1873 if (file->f_mode & FMODE_WRITE)
1874 may |= MAY_WRITE;
1875
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001876 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001877 rc = smk_bu_file(file, may, rc);
1878 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001879}
1880
Casey Schaufler531f1d42011-09-19 12:41:42 -07001881/**
Eric Paris83d49852012-04-04 13:45:40 -04001882 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001883 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001884 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001885 *
1886 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001887 * Allow the open only if the task has read access. There are
1888 * many read operations (e.g. fstat) that you can do with an
1889 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001890 *
1891 * Returns 0
1892 */
Al Viro94817692018-07-10 14:13:18 -04001893static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001894{
Al Viro94817692018-07-10 14:13:18 -04001895 struct task_smack *tsp = file->f_cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001896 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001897 struct smk_audit_info ad;
1898 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001899
Casey Schauflera6834c02014-04-21 11:10:26 -07001900 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1901 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301902 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001903 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001904
1905 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001906}
1907
Casey Schauflere114e472008-02-04 22:29:50 -08001908/*
1909 * Task hooks
1910 */
1911
1912/**
David Howellsee18d642009-09-02 09:14:21 +01001913 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1914 * @new: the new credentials
1915 * @gfp: the atomicity of any memory allocations
1916 *
1917 * Prepare a blank set of credentials for modification. This must allocate all
1918 * the memory the LSM module might require such that cred_transfer() can
1919 * complete without error.
1920 */
1921static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1922{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001923 struct task_smack *tsp;
1924
1925 tsp = new_task_smack(NULL, NULL, gfp);
1926 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001927 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001928
1929 cred->security = tsp;
1930
David Howellsee18d642009-09-02 09:14:21 +01001931 return 0;
1932}
1933
1934
1935/**
David Howellsf1752ee2008-11-14 10:39:17 +11001936 * smack_cred_free - "free" task-level security credentials
1937 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001938 *
Casey Schauflere114e472008-02-04 22:29:50 -08001939 */
David Howellsf1752ee2008-11-14 10:39:17 +11001940static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001941{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001942 struct task_smack *tsp = cred->security;
1943 struct smack_rule *rp;
1944 struct list_head *l;
1945 struct list_head *n;
1946
1947 if (tsp == NULL)
1948 return;
1949 cred->security = NULL;
1950
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001951 smk_destroy_label_list(&tsp->smk_relabel);
1952
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001953 list_for_each_safe(l, n, &tsp->smk_rules) {
1954 rp = list_entry(l, struct smack_rule, list);
1955 list_del(&rp->list);
1956 kfree(rp);
1957 }
1958 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001959}
1960
1961/**
David Howellsd84f4f92008-11-14 10:39:23 +11001962 * smack_cred_prepare - prepare new set of credentials for modification
1963 * @new: the new credentials
1964 * @old: the original credentials
1965 * @gfp: the atomicity of any memory allocations
1966 *
1967 * Prepare a new set of credentials for modification.
1968 */
1969static int smack_cred_prepare(struct cred *new, const struct cred *old,
1970 gfp_t gfp)
1971{
Casey Schaufler676dac42010-12-02 06:43:39 -08001972 struct task_smack *old_tsp = old->security;
1973 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001974 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001975
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001976 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001977 if (new_tsp == NULL)
1978 return -ENOMEM;
1979
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301980 new->security = new_tsp;
1981
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001982 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1983 if (rc != 0)
1984 return rc;
1985
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001986 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1987 gfp);
1988 if (rc != 0)
1989 return rc;
1990
David Howellsd84f4f92008-11-14 10:39:23 +11001991 return 0;
1992}
1993
Randy Dunlap251a2a92009-02-18 11:42:33 -08001994/**
David Howellsee18d642009-09-02 09:14:21 +01001995 * smack_cred_transfer - Transfer the old credentials to the new credentials
1996 * @new: the new credentials
1997 * @old: the original credentials
1998 *
1999 * Fill in a set of blank credentials from another set of credentials.
2000 */
2001static void smack_cred_transfer(struct cred *new, const struct cred *old)
2002{
Casey Schaufler676dac42010-12-02 06:43:39 -08002003 struct task_smack *old_tsp = old->security;
2004 struct task_smack *new_tsp = new->security;
2005
2006 new_tsp->smk_task = old_tsp->smk_task;
2007 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002008 mutex_init(&new_tsp->smk_rules_lock);
2009 INIT_LIST_HEAD(&new_tsp->smk_rules);
2010
2011
2012 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01002013}
2014
2015/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08002016 * smack_cred_getsecid - get the secid corresponding to a creds structure
2017 * @c: the object creds
2018 * @secid: where to put the result
2019 *
2020 * Sets the secid to contain a u32 version of the smack label.
2021 */
2022static void smack_cred_getsecid(const struct cred *c, u32 *secid)
2023{
2024 struct smack_known *skp;
2025
2026 rcu_read_lock();
2027 skp = smk_of_task(c->security);
2028 *secid = skp->smk_secid;
2029 rcu_read_unlock();
2030}
2031
2032/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002033 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002034 * @new: points to the set of credentials to be modified.
2035 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002036 *
2037 * Set the security data for a kernel service.
2038 */
2039static int smack_kernel_act_as(struct cred *new, u32 secid)
2040{
Casey Schaufler676dac42010-12-02 06:43:39 -08002041 struct task_smack *new_tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002042
Casey Schaufler152f91d2016-11-14 09:38:15 -08002043 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002044 return 0;
2045}
2046
2047/**
2048 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002049 * @new: points to the set of credentials to be modified
2050 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002051 *
2052 * Set the file creation context in a set of credentials to the same
2053 * as the objective context of the specified inode
2054 */
2055static int smack_kernel_create_files_as(struct cred *new,
2056 struct inode *inode)
2057{
2058 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002059 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002060
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002061 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002062 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002063 return 0;
2064}
2065
2066/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002067 * smk_curacc_on_task - helper to log task related access
2068 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002069 * @access: the access requested
2070 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002071 *
2072 * Return 0 if access is permitted
2073 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002074static int smk_curacc_on_task(struct task_struct *p, int access,
2075 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002076{
2077 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002078 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002079 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002080
Casey Schaufler531f1d42011-09-19 12:41:42 -07002081 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002082 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002083 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002084 rc = smk_bu_task(p, access, rc);
2085 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002086}
2087
2088/**
Casey Schauflere114e472008-02-04 22:29:50 -08002089 * smack_task_setpgid - Smack check on setting pgid
2090 * @p: the task object
2091 * @pgid: unused
2092 *
2093 * Return 0 if write access is permitted
2094 */
2095static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2096{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002097 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002098}
2099
2100/**
2101 * smack_task_getpgid - Smack access check for getpgid
2102 * @p: the object task
2103 *
2104 * Returns 0 if current can read the object task, error code otherwise
2105 */
2106static int smack_task_getpgid(struct task_struct *p)
2107{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002108 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002109}
2110
2111/**
2112 * smack_task_getsid - Smack access check for getsid
2113 * @p: the object task
2114 *
2115 * Returns 0 if current can read the object task, error code otherwise
2116 */
2117static int smack_task_getsid(struct task_struct *p)
2118{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002119 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002120}
2121
2122/**
2123 * smack_task_getsecid - get the secid of the task
2124 * @p: the object task
2125 * @secid: where to put the result
2126 *
2127 * Sets the secid to contain a u32 version of the smack label.
2128 */
2129static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2130{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002131 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002132
2133 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002134}
2135
2136/**
2137 * smack_task_setnice - Smack check on setting nice
2138 * @p: the task object
2139 * @nice: unused
2140 *
2141 * Return 0 if write access is permitted
2142 */
2143static int smack_task_setnice(struct task_struct *p, int nice)
2144{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002145 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002146}
2147
2148/**
2149 * smack_task_setioprio - Smack check on setting ioprio
2150 * @p: the task object
2151 * @ioprio: unused
2152 *
2153 * Return 0 if write access is permitted
2154 */
2155static int smack_task_setioprio(struct task_struct *p, int ioprio)
2156{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002157 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002158}
2159
2160/**
2161 * smack_task_getioprio - Smack check on reading ioprio
2162 * @p: the task object
2163 *
2164 * Return 0 if read access is permitted
2165 */
2166static int smack_task_getioprio(struct task_struct *p)
2167{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002168 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002169}
2170
2171/**
2172 * smack_task_setscheduler - Smack check on setting scheduler
2173 * @p: the task object
2174 * @policy: unused
2175 * @lp: unused
2176 *
2177 * Return 0 if read access is permitted
2178 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002179static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002180{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002181 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002182}
2183
2184/**
2185 * smack_task_getscheduler - Smack check on reading scheduler
2186 * @p: the task object
2187 *
2188 * Return 0 if read access is permitted
2189 */
2190static int smack_task_getscheduler(struct task_struct *p)
2191{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002192 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002193}
2194
2195/**
2196 * smack_task_movememory - Smack check on moving memory
2197 * @p: the task object
2198 *
2199 * Return 0 if write access is permitted
2200 */
2201static int smack_task_movememory(struct task_struct *p)
2202{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002203 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002204}
2205
2206/**
2207 * smack_task_kill - Smack check on signal delivery
2208 * @p: the task object
2209 * @info: unused
2210 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002211 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002212 *
2213 * Return 0 if write access is permitted
2214 *
Casey Schauflere114e472008-02-04 22:29:50 -08002215 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002216static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002217 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002218{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002219 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002220 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002221 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002222 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002223
Rafal Krypa18d872f2016-04-04 11:14:53 +02002224 if (!sig)
2225 return 0; /* null signal; existence test */
2226
Etienne Bassetecfcc532009-04-08 20:40:06 +02002227 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2228 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002229 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002230 * Sending a signal requires that the sender
2231 * can write the receiver.
2232 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002233 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002234 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2235 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002236 return rc;
2237 }
Casey Schauflere114e472008-02-04 22:29:50 -08002238 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002239 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002240 * specific behavior. This is not clean. For one thing
2241 * we can't take privilege into account.
2242 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002243 skp = smk_of_task(cred->security);
Casey Schauflerc60b9062016-08-30 10:31:39 -07002244 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2245 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002246 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002247}
2248
2249/**
Casey Schauflere114e472008-02-04 22:29:50 -08002250 * smack_task_to_inode - copy task smack into the inode blob
2251 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002252 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002253 *
2254 * Sets the smack pointer in the inode security blob
2255 */
2256static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2257{
2258 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002259 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002260
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002261 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002262 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002263}
2264
2265/*
2266 * Socket hooks.
2267 */
2268
2269/**
2270 * smack_sk_alloc_security - Allocate a socket blob
2271 * @sk: the socket
2272 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002273 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002274 *
2275 * Assign Smack pointers to current
2276 *
2277 * Returns 0 on success, -ENOMEM is there's no memory
2278 */
2279static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2280{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002281 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002282 struct socket_smack *ssp;
2283
2284 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2285 if (ssp == NULL)
2286 return -ENOMEM;
2287
jooseong lee08382c92016-11-03 11:54:39 +01002288 /*
2289 * Sockets created by kernel threads receive web label.
2290 */
2291 if (unlikely(current->flags & PF_KTHREAD)) {
2292 ssp->smk_in = &smack_known_web;
2293 ssp->smk_out = &smack_known_web;
2294 } else {
2295 ssp->smk_in = skp;
2296 ssp->smk_out = skp;
2297 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002298 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002299
2300 sk->sk_security = ssp;
2301
2302 return 0;
2303}
2304
2305/**
2306 * smack_sk_free_security - Free a socket blob
2307 * @sk: the socket
2308 *
2309 * Clears the blob pointer
2310 */
2311static void smack_sk_free_security(struct sock *sk)
2312{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302313#ifdef SMACK_IPV6_PORT_LABELING
2314 struct smk_port_label *spp;
2315
2316 if (sk->sk_family == PF_INET6) {
2317 rcu_read_lock();
2318 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2319 if (spp->smk_sock != sk)
2320 continue;
2321 spp->smk_can_reuse = 1;
2322 break;
2323 }
2324 rcu_read_unlock();
2325 }
2326#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002327 kfree(sk->sk_security);
2328}
2329
2330/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002331* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002332* @sip: the object end
2333*
2334* looks for host based access restrictions
2335*
2336* This version will only be appropriate for really small sets of single label
2337* hosts. The caller is responsible for ensuring that the RCU read lock is
2338* taken before calling this function.
2339*
2340* Returns the label of the far end or NULL if it's not special.
2341*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002342static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002343{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002344 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002345 struct in_addr *siap = &sip->sin_addr;
2346
2347 if (siap->s_addr == 0)
2348 return NULL;
2349
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002350 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2351 /*
2352 * we break after finding the first match because
2353 * the list is sorted from longest to shortest mask
2354 * so we have found the most specific match
2355 */
2356 if (snp->smk_host.s_addr ==
2357 (siap->s_addr & snp->smk_mask.s_addr))
2358 return snp->smk_label;
2359
2360 return NULL;
2361}
2362
2363#if IS_ENABLED(CONFIG_IPV6)
2364/*
2365 * smk_ipv6_localhost - Check for local ipv6 host address
2366 * @sip: the address
2367 *
2368 * Returns boolean true if this is the localhost address
2369 */
2370static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2371{
2372 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2373 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2374
2375 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2376 ntohs(be16p[7]) == 1)
2377 return true;
2378 return false;
2379}
2380
2381/**
2382* smack_ipv6host_label - check host based restrictions
2383* @sip: the object end
2384*
2385* looks for host based access restrictions
2386*
2387* This version will only be appropriate for really small sets of single label
2388* hosts. The caller is responsible for ensuring that the RCU read lock is
2389* taken before calling this function.
2390*
2391* Returns the label of the far end or NULL if it's not special.
2392*/
2393static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2394{
2395 struct smk_net6addr *snp;
2396 struct in6_addr *sap = &sip->sin6_addr;
2397 int i;
2398 int found = 0;
2399
2400 /*
2401 * It's local. Don't look for a host label.
2402 */
2403 if (smk_ipv6_localhost(sip))
2404 return NULL;
2405
2406 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002407 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002408 * If the label is NULL the entry has
2409 * been renounced. Ignore it.
2410 */
2411 if (snp->smk_label == NULL)
2412 continue;
2413 /*
Paul Moore07feee82009-03-27 17:10:54 -04002414 * we break after finding the first match because
2415 * the list is sorted from longest to shortest mask
2416 * so we have found the most specific match
2417 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002418 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002419 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2420 snp->smk_host.s6_addr16[i]) {
2421 found = 0;
2422 break;
2423 }
Etienne Basset43031542009-03-27 17:11:01 -04002424 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002425 if (found)
2426 return snp->smk_label;
2427 }
Paul Moore07feee82009-03-27 17:10:54 -04002428
2429 return NULL;
2430}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002431#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002432
2433/**
Casey Schauflere114e472008-02-04 22:29:50 -08002434 * smack_netlabel - Set the secattr on a socket
2435 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002436 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002437 *
2438 * Convert the outbound smack value (smk_out) to a
2439 * secattr and attach it to the socket.
2440 *
2441 * Returns 0 on success or an error code
2442 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002443static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002444{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002445 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002446 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002447 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002448
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002449 /*
2450 * Usually the netlabel code will handle changing the
2451 * packet labeling based on the label.
2452 * The case of a single label host is different, because
2453 * a single label host should never get a labeled packet
2454 * even though the label is usually associated with a packet
2455 * label.
2456 */
2457 local_bh_disable();
2458 bh_lock_sock_nested(sk);
2459
2460 if (ssp->smk_out == smack_net_ambient ||
2461 labeled == SMACK_UNLABELED_SOCKET)
2462 netlbl_sock_delattr(sk);
2463 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002464 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002465 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002466 }
2467
2468 bh_unlock_sock(sk);
2469 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002470
Casey Schauflere114e472008-02-04 22:29:50 -08002471 return rc;
2472}
2473
2474/**
Paul Moore07feee82009-03-27 17:10:54 -04002475 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2476 * @sk: the socket
2477 * @sap: the destination address
2478 *
2479 * Set the correct secattr for the given socket based on the destination
2480 * address and perform any outbound access checks needed.
2481 *
2482 * Returns 0 on success or an error code.
2483 *
2484 */
2485static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2486{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002487 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002488 int rc;
2489 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002490 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002491 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002492 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002493
2494 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002495 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002496 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002497#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002498 struct lsm_network_audit net;
2499
Eric Paris48c62af2012-04-02 13:15:44 -04002500 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2501 ad.a.u.net->family = sap->sin_family;
2502 ad.a.u.net->dport = sap->sin_port;
2503 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002504#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002505 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002506 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002507 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2508 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002509 } else {
2510 sk_lbl = SMACK_CIPSO_SOCKET;
2511 rc = 0;
2512 }
2513 rcu_read_unlock();
2514 if (rc != 0)
2515 return rc;
2516
2517 return smack_netlabel(sk, sk_lbl);
2518}
2519
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002520#if IS_ENABLED(CONFIG_IPV6)
2521/**
2522 * smk_ipv6_check - check Smack access
2523 * @subject: subject Smack label
2524 * @object: object Smack label
2525 * @address: address
2526 * @act: the action being taken
2527 *
2528 * Check an IPv6 access
2529 */
2530static int smk_ipv6_check(struct smack_known *subject,
2531 struct smack_known *object,
2532 struct sockaddr_in6 *address, int act)
2533{
2534#ifdef CONFIG_AUDIT
2535 struct lsm_network_audit net;
2536#endif
2537 struct smk_audit_info ad;
2538 int rc;
2539
2540#ifdef CONFIG_AUDIT
2541 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2542 ad.a.u.net->family = PF_INET6;
2543 ad.a.u.net->dport = ntohs(address->sin6_port);
2544 if (act == SMK_RECEIVING)
2545 ad.a.u.net->v6info.saddr = address->sin6_addr;
2546 else
2547 ad.a.u.net->v6info.daddr = address->sin6_addr;
2548#endif
2549 rc = smk_access(subject, object, MAY_WRITE, &ad);
2550 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2551 return rc;
2552}
2553#endif /* CONFIG_IPV6 */
2554
2555#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002556/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002557 * smk_ipv6_port_label - Smack port access table management
2558 * @sock: socket
2559 * @address: address
2560 *
2561 * Create or update the port list entry
2562 */
2563static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2564{
2565 struct sock *sk = sock->sk;
2566 struct sockaddr_in6 *addr6;
2567 struct socket_smack *ssp = sock->sk->sk_security;
2568 struct smk_port_label *spp;
2569 unsigned short port = 0;
2570
2571 if (address == NULL) {
2572 /*
2573 * This operation is changing the Smack information
2574 * on the bound socket. Take the changes to the port
2575 * as well.
2576 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302577 rcu_read_lock();
2578 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002579 if (sk != spp->smk_sock)
2580 continue;
2581 spp->smk_in = ssp->smk_in;
2582 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302583 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002584 return;
2585 }
2586 /*
2587 * A NULL address is only used for updating existing
2588 * bound entries. If there isn't one, it's OK.
2589 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302590 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002591 return;
2592 }
2593
2594 addr6 = (struct sockaddr_in6 *)address;
2595 port = ntohs(addr6->sin6_port);
2596 /*
2597 * This is a special case that is safely ignored.
2598 */
2599 if (port == 0)
2600 return;
2601
2602 /*
2603 * Look for an existing port list entry.
2604 * This is an indication that a port is getting reused.
2605 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302606 rcu_read_lock();
2607 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302608 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002609 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302610 if (spp->smk_can_reuse != 1) {
2611 rcu_read_unlock();
2612 return;
2613 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002614 spp->smk_port = port;
2615 spp->smk_sock = sk;
2616 spp->smk_in = ssp->smk_in;
2617 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302618 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302619 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002620 return;
2621 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302622 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 /*
2624 * A new port entry is required.
2625 */
2626 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2627 if (spp == NULL)
2628 return;
2629
2630 spp->smk_port = port;
2631 spp->smk_sock = sk;
2632 spp->smk_in = ssp->smk_in;
2633 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302634 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302635 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002636
Vishal Goel3c7ce342016-11-23 10:31:08 +05302637 mutex_lock(&smack_ipv6_lock);
2638 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2639 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002640 return;
2641}
2642
2643/**
2644 * smk_ipv6_port_check - check Smack port access
2645 * @sock: socket
2646 * @address: address
2647 *
2648 * Create or update the port list entry
2649 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002650static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002651 int act)
2652{
Casey Schauflerc6739442013-05-22 18:42:56 -07002653 struct smk_port_label *spp;
2654 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002655 struct smack_known *skp = NULL;
2656 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002657 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002658
2659 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002660 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002661 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002662 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002663 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002664 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002665 }
2666
2667 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002668 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002669 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002670 if (skp != NULL && object != NULL)
2671 return smk_ipv6_check(skp, object, address, act);
2672 if (skp == NULL)
2673 skp = smack_net_ambient;
2674 if (object == NULL)
2675 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002676
2677 /*
2678 * It's remote, so port lookup does no good.
2679 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002680 if (!smk_ipv6_localhost(address))
2681 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002682
2683 /*
2684 * It's local so the send check has to have passed.
2685 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002686 if (act == SMK_RECEIVING)
2687 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002688
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002689 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302690 rcu_read_lock();
2691 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302692 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002693 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002694 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002695 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002696 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002697 break;
2698 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302699 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002700
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002701 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002702}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002703#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002704
2705/**
Casey Schauflere114e472008-02-04 22:29:50 -08002706 * smack_inode_setsecurity - set smack xattrs
2707 * @inode: the object
2708 * @name: attribute name
2709 * @value: attribute value
2710 * @size: size of the attribute
2711 * @flags: unused
2712 *
2713 * Sets the named attribute in the appropriate blob
2714 *
2715 * Returns 0 on success, or an error code
2716 */
2717static int smack_inode_setsecurity(struct inode *inode, const char *name,
2718 const void *value, size_t size, int flags)
2719{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002720 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002721 struct inode_smack *nsp = inode->i_security;
2722 struct socket_smack *ssp;
2723 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002724 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002725
Casey Schauflerf7112e62012-05-06 15:22:02 -07002726 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302727 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002728
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002729 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002730 if (IS_ERR(skp))
2731 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002732
2733 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002734 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002735 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002736 return 0;
2737 }
2738 /*
2739 * The rest of the Smack xattrs are only on sockets.
2740 */
2741 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2742 return -EOPNOTSUPP;
2743
2744 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002745 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002746 return -EOPNOTSUPP;
2747
2748 ssp = sock->sk->sk_security;
2749
2750 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002751 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002752 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002753 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002754 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002755 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2756 if (rc != 0)
2757 printk(KERN_WARNING
2758 "Smack: \"%s\" netlbl error %d.\n",
2759 __func__, -rc);
2760 }
Casey Schauflere114e472008-02-04 22:29:50 -08002761 } else
2762 return -EOPNOTSUPP;
2763
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002764#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002765 if (sock->sk->sk_family == PF_INET6)
2766 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002767#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002768
Casey Schauflere114e472008-02-04 22:29:50 -08002769 return 0;
2770}
2771
2772/**
2773 * smack_socket_post_create - finish socket setup
2774 * @sock: the socket
2775 * @family: protocol family
2776 * @type: unused
2777 * @protocol: unused
2778 * @kern: unused
2779 *
2780 * Sets the netlabel information on the socket
2781 *
2782 * Returns 0 on success, and error code otherwise
2783 */
2784static int smack_socket_post_create(struct socket *sock, int family,
2785 int type, int protocol, int kern)
2786{
Marcin Lis74123012015-01-22 15:40:33 +01002787 struct socket_smack *ssp;
2788
2789 if (sock->sk == NULL)
2790 return 0;
2791
2792 /*
2793 * Sockets created by kernel threads receive web label.
2794 */
2795 if (unlikely(current->flags & PF_KTHREAD)) {
2796 ssp = sock->sk->sk_security;
2797 ssp->smk_in = &smack_known_web;
2798 ssp->smk_out = &smack_known_web;
2799 }
2800
2801 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002802 return 0;
2803 /*
2804 * Set the outbound netlbl.
2805 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002806 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2807}
2808
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002809/**
2810 * smack_socket_socketpair - create socket pair
2811 * @socka: one socket
2812 * @sockb: another socket
2813 *
2814 * Cross reference the peer labels for SO_PEERSEC
2815 *
2816 * Returns 0 on success, and error code otherwise
2817 */
2818static int smack_socket_socketpair(struct socket *socka,
2819 struct socket *sockb)
2820{
2821 struct socket_smack *asp = socka->sk->sk_security;
2822 struct socket_smack *bsp = sockb->sk->sk_security;
2823
2824 asp->smk_packet = bsp->smk_out;
2825 bsp->smk_packet = asp->smk_out;
2826
2827 return 0;
2828}
2829
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002830#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002831/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002832 * smack_socket_bind - record port binding information.
2833 * @sock: the socket
2834 * @address: the port address
2835 * @addrlen: size of the address
2836 *
2837 * Records the label bound to a port.
2838 *
2839 * Returns 0
2840 */
2841static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2842 int addrlen)
2843{
2844 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2845 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002846 return 0;
2847}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002848#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002849
2850/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002851 * smack_socket_connect - connect access check
2852 * @sock: the socket
2853 * @sap: the other end
2854 * @addrlen: size of sap
2855 *
2856 * Verifies that a connection may be possible
2857 *
2858 * Returns 0 on success, and error code otherwise
2859 */
2860static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2861 int addrlen)
2862{
Casey Schauflerc6739442013-05-22 18:42:56 -07002863 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002864#if IS_ENABLED(CONFIG_IPV6)
2865 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2866#endif
2867#ifdef SMACK_IPV6_SECMARK_LABELING
2868 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002869 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002870#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002871
Casey Schauflerc6739442013-05-22 18:42:56 -07002872 if (sock->sk == NULL)
2873 return 0;
2874
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002875#ifdef SMACK_IPV6_SECMARK_LABELING
2876 ssp = sock->sk->sk_security;
2877#endif
2878
Casey Schauflerc6739442013-05-22 18:42:56 -07002879 switch (sock->sk->sk_family) {
2880 case PF_INET:
2881 if (addrlen < sizeof(struct sockaddr_in))
2882 return -EINVAL;
2883 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2884 break;
2885 case PF_INET6:
2886 if (addrlen < sizeof(struct sockaddr_in6))
2887 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002888#ifdef SMACK_IPV6_SECMARK_LABELING
2889 rsp = smack_ipv6host_label(sip);
2890 if (rsp != NULL)
2891 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002892 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002893#endif
2894#ifdef SMACK_IPV6_PORT_LABELING
2895 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2896#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002897 break;
2898 }
2899 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002900}
2901
2902/**
2903 * smack_flags_to_may - convert S_ to MAY_ values
2904 * @flags: the S_ value
2905 *
2906 * Returns the equivalent MAY_ value
2907 */
2908static int smack_flags_to_may(int flags)
2909{
2910 int may = 0;
2911
2912 if (flags & S_IRUGO)
2913 may |= MAY_READ;
2914 if (flags & S_IWUGO)
2915 may |= MAY_WRITE;
2916 if (flags & S_IXUGO)
2917 may |= MAY_EXEC;
2918
2919 return may;
2920}
2921
2922/**
2923 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2924 * @msg: the object
2925 *
2926 * Returns 0
2927 */
2928static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2929{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002930 struct smack_known *skp = smk_of_current();
2931
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002932 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002933 return 0;
2934}
2935
2936/**
2937 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2938 * @msg: the object
2939 *
2940 * Clears the blob pointer
2941 */
2942static void smack_msg_msg_free_security(struct msg_msg *msg)
2943{
2944 msg->security = NULL;
2945}
2946
2947/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002948 * smack_of_ipc - the smack pointer for the ipc
2949 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002950 *
2951 * Returns a pointer to the smack value
2952 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002953static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002954{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002955 return (struct smack_known *)isp->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002956}
2957
2958/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002959 * smack_ipc_alloc_security - Set the security blob for ipc
2960 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002961 *
2962 * Returns 0
2963 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002964static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002965{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002966 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002967
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002968 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002969 return 0;
2970}
2971
2972/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002973 * smack_ipc_free_security - Clear the security blob for ipc
2974 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002975 *
2976 * Clears the blob pointer
2977 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002978static void smack_ipc_free_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002979{
Casey Schauflere114e472008-02-04 22:29:50 -08002980 isp->security = NULL;
2981}
2982
2983/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002984 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002985 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002986 * @access : access requested
2987 *
2988 * Returns 0 if current has the requested access, error code otherwise
2989 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002990static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002991{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002992 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002993 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002994 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002995
2996#ifdef CONFIG_AUDIT
2997 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002998 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002999#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003000 rc = smk_curacc(ssp, access, &ad);
3001 rc = smk_bu_current("shm", ssp, access, rc);
3002 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003003}
3004
3005/**
Casey Schauflere114e472008-02-04 22:29:50 -08003006 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003007 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003008 * @shmflg: access requested
3009 *
3010 * Returns 0 if current has the requested access, error code otherwise
3011 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003012static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003013{
Casey Schauflere114e472008-02-04 22:29:50 -08003014 int may;
3015
3016 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003017 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003018}
3019
3020/**
3021 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003022 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003023 * @cmd: what it wants to do
3024 *
3025 * Returns 0 if current has the requested access, error code otherwise
3026 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003027static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003028{
Casey Schauflere114e472008-02-04 22:29:50 -08003029 int may;
3030
3031 switch (cmd) {
3032 case IPC_STAT:
3033 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07003034 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003035 may = MAY_READ;
3036 break;
3037 case IPC_SET:
3038 case SHM_LOCK:
3039 case SHM_UNLOCK:
3040 case IPC_RMID:
3041 may = MAY_READWRITE;
3042 break;
3043 case IPC_INFO:
3044 case SHM_INFO:
3045 /*
3046 * System level information.
3047 */
3048 return 0;
3049 default:
3050 return -EINVAL;
3051 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003052 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003053}
3054
3055/**
3056 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003057 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003058 * @shmaddr: unused
3059 * @shmflg: access requested
3060 *
3061 * Returns 0 if current has the requested access, error code otherwise
3062 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003063static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003064 int shmflg)
3065{
Casey Schauflere114e472008-02-04 22:29:50 -08003066 int may;
3067
3068 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003069 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003070}
3071
3072/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003073 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003074 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003075 * @access : access requested
3076 *
3077 * Returns 0 if current has the requested access, error code otherwise
3078 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003079static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003080{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003081 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003082 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003083 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003084
3085#ifdef CONFIG_AUDIT
3086 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003087 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003088#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003089 rc = smk_curacc(ssp, access, &ad);
3090 rc = smk_bu_current("sem", ssp, access, rc);
3091 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003092}
3093
3094/**
Casey Schauflere114e472008-02-04 22:29:50 -08003095 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003096 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003097 * @semflg: access requested
3098 *
3099 * Returns 0 if current has the requested access, error code otherwise
3100 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003101static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003102{
Casey Schauflere114e472008-02-04 22:29:50 -08003103 int may;
3104
3105 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003106 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003107}
3108
3109/**
3110 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003111 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003112 * @cmd: what it wants to do
3113 *
3114 * Returns 0 if current has the requested access, error code otherwise
3115 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003116static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003117{
Casey Schauflere114e472008-02-04 22:29:50 -08003118 int may;
3119
3120 switch (cmd) {
3121 case GETPID:
3122 case GETNCNT:
3123 case GETZCNT:
3124 case GETVAL:
3125 case GETALL:
3126 case IPC_STAT:
3127 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003128 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003129 may = MAY_READ;
3130 break;
3131 case SETVAL:
3132 case SETALL:
3133 case IPC_RMID:
3134 case IPC_SET:
3135 may = MAY_READWRITE;
3136 break;
3137 case IPC_INFO:
3138 case SEM_INFO:
3139 /*
3140 * System level information
3141 */
3142 return 0;
3143 default:
3144 return -EINVAL;
3145 }
3146
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003147 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003148}
3149
3150/**
3151 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003152 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003153 * @sops: unused
3154 * @nsops: unused
3155 * @alter: unused
3156 *
3157 * Treated as read and write in all cases.
3158 *
3159 * Returns 0 if access is allowed, error code otherwise
3160 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003161static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003162 unsigned nsops, int alter)
3163{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003164 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003165}
3166
3167/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003168 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003169 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003170 * @access : access requested
3171 *
3172 * return 0 if current has access, error otherwise
3173 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003174static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003175{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003176 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003177 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003178 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003179
3180#ifdef CONFIG_AUDIT
3181 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003182 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003183#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003184 rc = smk_curacc(msp, access, &ad);
3185 rc = smk_bu_current("msq", msp, access, rc);
3186 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003187}
3188
3189/**
Casey Schauflere114e472008-02-04 22:29:50 -08003190 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003191 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003192 * @msqflg: access requested
3193 *
3194 * Returns 0 if current has the requested access, error code otherwise
3195 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003196static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003197{
Casey Schauflere114e472008-02-04 22:29:50 -08003198 int may;
3199
3200 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003201 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003202}
3203
3204/**
3205 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003206 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003207 * @cmd: what it wants to do
3208 *
3209 * Returns 0 if current has the requested access, error code otherwise
3210 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003211static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003212{
Casey Schauflere114e472008-02-04 22:29:50 -08003213 int may;
3214
3215 switch (cmd) {
3216 case IPC_STAT:
3217 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003218 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003219 may = MAY_READ;
3220 break;
3221 case IPC_SET:
3222 case IPC_RMID:
3223 may = MAY_READWRITE;
3224 break;
3225 case IPC_INFO:
3226 case MSG_INFO:
3227 /*
3228 * System level information
3229 */
3230 return 0;
3231 default:
3232 return -EINVAL;
3233 }
3234
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003235 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003236}
3237
3238/**
3239 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003240 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003241 * @msg: unused
3242 * @msqflg: access requested
3243 *
3244 * Returns 0 if current has the requested access, error code otherwise
3245 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003246static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003247 int msqflg)
3248{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003249 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003250
Etienne Bassetecfcc532009-04-08 20:40:06 +02003251 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003252 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003253}
3254
3255/**
3256 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003257 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003258 * @msg: unused
3259 * @target: unused
3260 * @type: unused
3261 * @mode: unused
3262 *
3263 * Returns 0 if current has read and write access, error code otherwise
3264 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003265static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003266 struct task_struct *target, long type, int mode)
3267{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003268 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003269}
3270
3271/**
3272 * smack_ipc_permission - Smack access for ipc_permission()
3273 * @ipp: the object permissions
3274 * @flag: access requested
3275 *
3276 * Returns 0 if current has read and write access, error code otherwise
3277 */
3278static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3279{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003280 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003281 int may = smack_flags_to_may(flag);
3282 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003283 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003284
Etienne Bassetecfcc532009-04-08 20:40:06 +02003285#ifdef CONFIG_AUDIT
3286 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3287 ad.a.u.ipc_id = ipp->id;
3288#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003289 rc = smk_curacc(iskp, may, &ad);
3290 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003291 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003292}
3293
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003294/**
3295 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003296 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003297 * @secid: where result will be saved
3298 */
3299static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3300{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003301 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003302
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003303 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003304}
3305
Casey Schauflere114e472008-02-04 22:29:50 -08003306/**
3307 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003308 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003309 * @inode: the object
3310 *
3311 * Set the inode's security blob if it hasn't been done already.
3312 */
3313static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3314{
3315 struct super_block *sbp;
3316 struct superblock_smack *sbsp;
3317 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003318 struct smack_known *skp;
3319 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003320 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003321 char trattr[TRANS_TRUE_SIZE];
3322 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003323 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003324 struct dentry *dp;
3325
3326 if (inode == NULL)
3327 return;
3328
3329 isp = inode->i_security;
3330
3331 mutex_lock(&isp->smk_lock);
3332 /*
3333 * If the inode is already instantiated
3334 * take the quick way out
3335 */
3336 if (isp->smk_flags & SMK_INODE_INSTANT)
3337 goto unlockandout;
3338
3339 sbp = inode->i_sb;
3340 sbsp = sbp->s_security;
3341 /*
3342 * We're going to use the superblock default label
3343 * if there's no label on the file.
3344 */
3345 final = sbsp->smk_default;
3346
3347 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003348 * If this is the root inode the superblock
3349 * may be in the process of initialization.
3350 * If that is the case use the root value out
3351 * of the superblock.
3352 */
3353 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003354 switch (sbp->s_magic) {
3355 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003356 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003357 /*
3358 * The cgroup filesystem is never mounted,
3359 * so there's no opportunity to set the mount
3360 * options.
3361 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003362 sbsp->smk_root = &smack_known_star;
3363 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003364 isp->smk_inode = sbsp->smk_root;
3365 break;
3366 case TMPFS_MAGIC:
3367 /*
3368 * What about shmem/tmpfs anonymous files with dentry
3369 * obtained from d_alloc_pseudo()?
3370 */
3371 isp->smk_inode = smk_of_current();
3372 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003373 case PIPEFS_MAGIC:
3374 isp->smk_inode = smk_of_current();
3375 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003376 case SOCKFS_MAGIC:
3377 /*
3378 * Socket access is controlled by the socket
3379 * structures associated with the task involved.
3380 */
3381 isp->smk_inode = &smack_known_star;
3382 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003383 default:
3384 isp->smk_inode = sbsp->smk_root;
3385 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003386 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003387 isp->smk_flags |= SMK_INODE_INSTANT;
3388 goto unlockandout;
3389 }
3390
3391 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003392 * This is pretty hackish.
3393 * Casey says that we shouldn't have to do
3394 * file system specific code, but it does help
3395 * with keeping it simple.
3396 */
3397 switch (sbp->s_magic) {
3398 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003399 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003400 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003401 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003402 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003403 * that the smack file system doesn't do
3404 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003405 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003406 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003407 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003408 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003409 break;
3410 case DEVPTS_SUPER_MAGIC:
3411 /*
3412 * devpts seems content with the label of the task.
3413 * Programs that change smack have to treat the
3414 * pty with respect.
3415 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003416 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003417 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003418 case PROC_SUPER_MAGIC:
3419 /*
3420 * Casey says procfs appears not to care.
3421 * The superblock default suffices.
3422 */
3423 break;
3424 case TMPFS_MAGIC:
3425 /*
3426 * Device labels should come from the filesystem,
3427 * but watch out, because they're volitile,
3428 * getting recreated on every reboot.
3429 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003430 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003431 /*
Gustavo A. R. Silvab1fed3e2018-08-01 17:38:54 -05003432 * Fall through.
Casey Schauflere114e472008-02-04 22:29:50 -08003433 *
3434 * If a smack value has been set we want to use it,
3435 * but since tmpfs isn't giving us the opportunity
3436 * to set mount options simulate setting the
3437 * superblock default.
3438 */
3439 default:
3440 /*
3441 * This isn't an understood special case.
3442 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003443 */
3444
3445 /*
3446 * UNIX domain sockets use lower level socket data.
3447 */
3448 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003449 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003450 break;
3451 }
3452 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003453 * No xattr support means, alas, no SMACK label.
3454 * Use the aforeapplied default.
3455 * It would be curious if the label of the task
3456 * does not match that assigned.
3457 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003458 if (!(inode->i_opflags & IOP_XATTR))
3459 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003460 /*
3461 * Get the dentry for xattr.
3462 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003463 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003464 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003465 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003466 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003467
3468 /*
3469 * Transmuting directory
3470 */
3471 if (S_ISDIR(inode->i_mode)) {
3472 /*
3473 * If this is a new directory and the label was
3474 * transmuted when the inode was initialized
3475 * set the transmute attribute on the directory
3476 * and mark the inode.
3477 *
3478 * If there is a transmute attribute on the
3479 * directory mark the inode.
3480 */
3481 if (isp->smk_flags & SMK_INODE_CHANGED) {
3482 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003483 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003484 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003485 TRANS_TRUE, TRANS_TRUE_SIZE,
3486 0);
3487 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003488 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003489 XATTR_NAME_SMACKTRANSMUTE, trattr,
3490 TRANS_TRUE_SIZE);
3491 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3492 TRANS_TRUE_SIZE) != 0)
3493 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003494 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003495 if (rc >= 0)
3496 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003497 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003498 /*
3499 * Don't let the exec or mmap label be "*" or "@".
3500 */
3501 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3502 if (IS_ERR(skp) || skp == &smack_known_star ||
3503 skp == &smack_known_web)
3504 skp = NULL;
3505 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003506
Casey Schaufler19760ad2013-12-16 16:27:26 -08003507 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003508 if (IS_ERR(skp) || skp == &smack_known_star ||
3509 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003510 skp = NULL;
3511 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003512
Casey Schauflere114e472008-02-04 22:29:50 -08003513 dput(dp);
3514 break;
3515 }
3516
3517 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003518 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003519 else
3520 isp->smk_inode = final;
3521
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003522 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003523
3524unlockandout:
3525 mutex_unlock(&isp->smk_lock);
3526 return;
3527}
3528
3529/**
3530 * smack_getprocattr - Smack process attribute access
3531 * @p: the object task
3532 * @name: the name of the attribute in /proc/.../attr
3533 * @value: where to put the result
3534 *
3535 * Places a copy of the task Smack into value
3536 *
3537 * Returns the length of the smack label or an error code
3538 */
3539static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3540{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003541 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003542 char *cp;
3543 int slen;
3544
3545 if (strcmp(name, "current") != 0)
3546 return -EINVAL;
3547
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003548 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003549 if (cp == NULL)
3550 return -ENOMEM;
3551
3552 slen = strlen(cp);
3553 *value = cp;
3554 return slen;
3555}
3556
3557/**
3558 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003559 * @name: the name of the attribute in /proc/.../attr
3560 * @value: the value to set
3561 * @size: the size of the value
3562 *
3563 * Sets the Smack value of the task. Only setting self
3564 * is permitted and only with privilege
3565 *
3566 * Returns the length of the smack label or an error code
3567 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003568static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003569{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003570 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003571 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003572 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003573 struct smack_known_list_elem *sklep;
3574 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003575
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003576 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003577 return -EPERM;
3578
Casey Schauflerf7112e62012-05-06 15:22:02 -07003579 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003580 return -EINVAL;
3581
3582 if (strcmp(name, "current") != 0)
3583 return -EINVAL;
3584
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003585 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003586 if (IS_ERR(skp))
3587 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003588
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003589 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303590 * No process is ever allowed the web ("@") label
3591 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003592 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303593 if (skp == &smack_known_web || skp == &smack_known_star)
3594 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003595
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003596 if (!smack_privileged(CAP_MAC_ADMIN)) {
3597 rc = -EPERM;
3598 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3599 if (sklep->smk_label == skp) {
3600 rc = 0;
3601 break;
3602 }
3603 if (rc)
3604 return rc;
3605 }
3606
David Howellsd84f4f92008-11-14 10:39:23 +11003607 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003608 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003609 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003610
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003611 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003612 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003613 /*
3614 * process can change its label only once
3615 */
3616 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003617
David Howellsd84f4f92008-11-14 10:39:23 +11003618 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003619 return size;
3620}
3621
3622/**
3623 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003624 * @sock: one sock
3625 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003626 * @newsk: unused
3627 *
3628 * Return 0 if a subject with the smack of sock could access
3629 * an object with the smack of other, otherwise an error code
3630 */
David S. Miller3610cda2011-01-05 15:38:53 -08003631static int smack_unix_stream_connect(struct sock *sock,
3632 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003633{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003634 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003635 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003636 struct socket_smack *ssp = sock->sk_security;
3637 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003638 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003639 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003640 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003641#ifdef CONFIG_AUDIT
3642 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003643#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003644
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003645 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3646 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003647 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003648#ifdef CONFIG_AUDIT
3649 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3650 smk_ad_setfield_u_net_sk(&ad, other);
3651#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003652 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3653 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003654 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003655 okp = osp->smk_out;
3656 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003657 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003658 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003659 MAY_WRITE, rc);
3660 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003661 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003662
Casey Schaufler975d5e52011-09-26 14:43:39 -07003663 /*
3664 * Cross reference the peer labels for SO_PEERSEC.
3665 */
3666 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003667 nsp->smk_packet = ssp->smk_out;
3668 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003669 }
3670
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003671 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003672}
3673
3674/**
3675 * smack_unix_may_send - Smack access on UDS
3676 * @sock: one socket
3677 * @other: the other socket
3678 *
3679 * Return 0 if a subject with the smack of sock could access
3680 * an object with the smack of other, otherwise an error code
3681 */
3682static int smack_unix_may_send(struct socket *sock, struct socket *other)
3683{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003684 struct socket_smack *ssp = sock->sk->sk_security;
3685 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003686 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003687 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003688
Kees Cook923e9a12012-04-10 13:26:44 -07003689#ifdef CONFIG_AUDIT
3690 struct lsm_network_audit net;
3691
Eric Paris48c62af2012-04-02 13:15:44 -04003692 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003693 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003694#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003695
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003696 if (smack_privileged(CAP_MAC_OVERRIDE))
3697 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003698
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003699 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3700 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003701 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003702}
3703
3704/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003705 * smack_socket_sendmsg - Smack check based on destination host
3706 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003707 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003708 * @size: the size of the message
3709 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003710 * Return 0 if the current subject can write to the destination host.
3711 * For IPv4 this is only a question if the destination is a single label host.
3712 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003713 */
3714static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3715 int size)
3716{
3717 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003718#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003719 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003720#endif
3721#ifdef SMACK_IPV6_SECMARK_LABELING
3722 struct socket_smack *ssp = sock->sk->sk_security;
3723 struct smack_known *rsp;
3724#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003725 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003726
3727 /*
3728 * Perfectly reasonable for this to be NULL
3729 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003730 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003731 return 0;
3732
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003733 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003734 case AF_INET:
3735 rc = smack_netlabel_send(sock->sk, sip);
3736 break;
3737 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003738#ifdef SMACK_IPV6_SECMARK_LABELING
3739 rsp = smack_ipv6host_label(sap);
3740 if (rsp != NULL)
3741 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3742 SMK_CONNECTING);
3743#endif
3744#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003745 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003746#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003747 break;
3748 }
3749 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003750}
3751
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003752/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003753 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003754 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003755 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003756 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003757 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003758 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003759static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3760 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003761{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003762 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003763 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003764 int acat;
3765 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003766
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003767 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003768 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003769 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003770 * If there are flags but no level netlabel isn't
3771 * behaving the way we expect it to.
3772 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003773 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003774 * Without guidance regarding the smack value
3775 * for the packet fall back on the network
3776 * ambient value.
3777 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003778 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303779 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003780 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003781 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003782 /*
3783 * Compare the catsets. Use the netlbl APIs.
3784 */
3785 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3786 if ((skp->smk_netlabel.flags &
3787 NETLBL_SECATTR_MLS_CAT) == 0)
3788 found = 1;
3789 break;
3790 }
3791 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003792 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3793 acat + 1);
3794 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003795 skp->smk_netlabel.attr.mls.cat,
3796 kcat + 1);
3797 if (acat < 0 || kcat < 0)
3798 break;
3799 }
3800 if (acat == kcat) {
3801 found = 1;
3802 break;
3803 }
Casey Schauflere114e472008-02-04 22:29:50 -08003804 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003805 rcu_read_unlock();
3806
3807 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003808 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003809
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003810 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003811 return &smack_known_web;
3812 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003813 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003814 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003815 /*
3816 * Looks like a fallback, which gives us a secid.
3817 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003818 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003819 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003820 * Without guidance regarding the smack value
3821 * for the packet fall back on the network
3822 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003823 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003824 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003825}
3826
Casey Schaufler69f287a2014-12-12 17:08:40 -08003827#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003828static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003829{
Casey Schauflerc6739442013-05-22 18:42:56 -07003830 u8 nexthdr;
3831 int offset;
3832 int proto = -EINVAL;
3833 struct ipv6hdr _ipv6h;
3834 struct ipv6hdr *ip6;
3835 __be16 frag_off;
3836 struct tcphdr _tcph, *th;
3837 struct udphdr _udph, *uh;
3838 struct dccp_hdr _dccph, *dh;
3839
3840 sip->sin6_port = 0;
3841
3842 offset = skb_network_offset(skb);
3843 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3844 if (ip6 == NULL)
3845 return -EINVAL;
3846 sip->sin6_addr = ip6->saddr;
3847
3848 nexthdr = ip6->nexthdr;
3849 offset += sizeof(_ipv6h);
3850 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3851 if (offset < 0)
3852 return -EINVAL;
3853
3854 proto = nexthdr;
3855 switch (proto) {
3856 case IPPROTO_TCP:
3857 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3858 if (th != NULL)
3859 sip->sin6_port = th->source;
3860 break;
3861 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003862 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003863 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3864 if (uh != NULL)
3865 sip->sin6_port = uh->source;
3866 break;
3867 case IPPROTO_DCCP:
3868 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3869 if (dh != NULL)
3870 sip->sin6_port = dh->dccph_sport;
3871 break;
3872 }
3873 return proto;
3874}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003875#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003876
Casey Schauflere114e472008-02-04 22:29:50 -08003877/**
3878 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3879 * @sk: socket
3880 * @skb: packet
3881 *
3882 * Returns 0 if the packet should be delivered, an error code otherwise
3883 */
3884static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3885{
3886 struct netlbl_lsm_secattr secattr;
3887 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003888 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003889 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003890 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003891 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003892#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003893 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003894#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003895#if IS_ENABLED(CONFIG_IPV6)
3896 struct sockaddr_in6 sadd;
3897 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003898
3899 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3900 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003901#endif /* CONFIG_IPV6 */
3902
Piotr Sawicki129a9982018-07-19 11:42:58 +02003903 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003904 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003905#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3906 /*
3907 * If there is a secmark use it rather than the CIPSO label.
3908 * If there is no secmark fall back to CIPSO.
3909 * The secmark is assumed to reflect policy better.
3910 */
3911 if (skb && skb->secmark != 0) {
3912 skp = smack_from_secid(skb->secmark);
3913 goto access_check;
3914 }
3915#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003916 /*
3917 * Translate what netlabel gave us.
3918 */
3919 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003920
Piotr Sawicki129a9982018-07-19 11:42:58 +02003921 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003922 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003923 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003924 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003925 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003926
Casey Schauflerc6739442013-05-22 18:42:56 -07003927 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003928
Casey Schaufler69f287a2014-12-12 17:08:40 -08003929#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3930access_check:
3931#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003932#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003933 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003934 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003935 ad.a.u.net->netif = skb->skb_iif;
3936 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003937#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003938 /*
3939 * Receiving a packet requires that the other end
3940 * be able to write here. Read access is not required.
3941 * This is the simplist possible security model
3942 * for networking.
3943 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003944 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3945 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003946 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003947 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003948 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003949 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003950#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003951 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003952 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003953 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3954 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003955 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003956#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003957 if (skb && skb->secmark != 0)
3958 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003959 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003960 skp = smack_ipv6host_label(&sadd);
3961 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003962 skp = smack_net_ambient;
3963#ifdef CONFIG_AUDIT
3964 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003965 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003966 ad.a.u.net->netif = skb->skb_iif;
3967 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3968#endif /* CONFIG_AUDIT */
3969 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3970 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3971 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003972#endif /* SMACK_IPV6_SECMARK_LABELING */
3973#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003974 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003975#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003976 if (rc != 0)
3977 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3978 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003979 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003980#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003981 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003982
Paul Moorea8134292008-10-10 10:16:31 -04003983 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003984}
3985
3986/**
3987 * smack_socket_getpeersec_stream - pull in packet label
3988 * @sock: the socket
3989 * @optval: user's destination
3990 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003991 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003992 *
3993 * returns zero on success, an error code otherwise
3994 */
3995static int smack_socket_getpeersec_stream(struct socket *sock,
3996 char __user *optval,
3997 int __user *optlen, unsigned len)
3998{
3999 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004000 char *rcp = "";
4001 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004002 int rc = 0;
4003
4004 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004005 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004006 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004007 slen = strlen(rcp) + 1;
4008 }
Casey Schauflere114e472008-02-04 22:29:50 -08004009
4010 if (slen > len)
4011 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004012 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004013 rc = -EFAULT;
4014
4015 if (put_user(slen, optlen) != 0)
4016 rc = -EFAULT;
4017
4018 return rc;
4019}
4020
4021
4022/**
4023 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004024 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004025 * @skb: packet data
4026 * @secid: pointer to where to put the secid of the packet
4027 *
4028 * Sets the netlabel socket state on sk from parent
4029 */
4030static int smack_socket_getpeersec_dgram(struct socket *sock,
4031 struct sk_buff *skb, u32 *secid)
4032
4033{
4034 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004035 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004036 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004037 int family = PF_UNSPEC;
4038 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004039 int rc;
4040
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004041 if (skb != NULL) {
4042 if (skb->protocol == htons(ETH_P_IP))
4043 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004044#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004045 else if (skb->protocol == htons(ETH_P_IPV6))
4046 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004047#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004048 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004049 if (family == PF_UNSPEC && sock != NULL)
4050 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004051
Casey Schaufler69f287a2014-12-12 17:08:40 -08004052 switch (family) {
4053 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004054 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004055 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004056 break;
4057 case PF_INET:
4058#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4059 s = skb->secmark;
4060 if (s != 0)
4061 break;
4062#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004063 /*
4064 * Translate what netlabel gave us.
4065 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004066 if (sock != NULL && sock->sk != NULL)
4067 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004068 netlbl_secattr_init(&secattr);
4069 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4070 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004071 skp = smack_from_secattr(&secattr, ssp);
4072 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004073 }
4074 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004075 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004076 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004077#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004078 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004079#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004080 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004081 }
4082 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004083 if (s == 0)
4084 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004085 return 0;
4086}
4087
4088/**
Paul Moore07feee82009-03-27 17:10:54 -04004089 * smack_sock_graft - Initialize a newly created socket with an existing sock
4090 * @sk: child sock
4091 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004092 *
Paul Moore07feee82009-03-27 17:10:54 -04004093 * Set the smk_{in,out} state of an existing sock based on the process that
4094 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004095 */
4096static void smack_sock_graft(struct sock *sk, struct socket *parent)
4097{
4098 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004099 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004100
Paul Moore07feee82009-03-27 17:10:54 -04004101 if (sk == NULL ||
4102 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004103 return;
4104
4105 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004106 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004107 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004108 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004109}
4110
4111/**
4112 * smack_inet_conn_request - Smack access check on connect
4113 * @sk: socket involved
4114 * @skb: packet
4115 * @req: unused
4116 *
4117 * Returns 0 if a task with the packet label could write to
4118 * the socket, otherwise an error code
4119 */
4120static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4121 struct request_sock *req)
4122{
Paul Moore07feee82009-03-27 17:10:54 -04004123 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004124 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004125 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004126 struct netlbl_lsm_secattr secattr;
4127 struct sockaddr_in addr;
4128 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004129 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004130 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004131 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004132#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004133 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004134#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004135
Casey Schaufler69f287a2014-12-12 17:08:40 -08004136#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004137 if (family == PF_INET6) {
4138 /*
4139 * Handle mapped IPv4 packets arriving
4140 * via IPv6 sockets. Don't set up netlabel
4141 * processing on IPv6.
4142 */
4143 if (skb->protocol == htons(ETH_P_IP))
4144 family = PF_INET;
4145 else
4146 return 0;
4147 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004148#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004149
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004150#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4151 /*
4152 * If there is a secmark use it rather than the CIPSO label.
4153 * If there is no secmark fall back to CIPSO.
4154 * The secmark is assumed to reflect policy better.
4155 */
4156 if (skb && skb->secmark != 0) {
4157 skp = smack_from_secid(skb->secmark);
4158 goto access_check;
4159 }
4160#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4161
Paul Moore07feee82009-03-27 17:10:54 -04004162 netlbl_secattr_init(&secattr);
4163 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004164 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004165 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004166 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004167 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004168 netlbl_secattr_destroy(&secattr);
4169
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004170#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4171access_check:
4172#endif
4173
Etienne Bassetecfcc532009-04-08 20:40:06 +02004174#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004175 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4176 ad.a.u.net->family = family;
4177 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004178 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4179#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004180 /*
Paul Moore07feee82009-03-27 17:10:54 -04004181 * Receiving a packet requires that the other end be able to write
4182 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004183 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004184 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4185 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004186 if (rc != 0)
4187 return rc;
4188
4189 /*
4190 * Save the peer's label in the request_sock so we can later setup
4191 * smk_packet in the child socket so that SO_PEERCRED can report it.
4192 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004193 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004194
4195 /*
4196 * We need to decide if we want to label the incoming connection here
4197 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004198 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004199 */
4200 hdr = ip_hdr(skb);
4201 addr.sin_addr.s_addr = hdr->saddr;
4202 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004203 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004204 rcu_read_unlock();
4205
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004206 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004207 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004208 else
Paul Moore07feee82009-03-27 17:10:54 -04004209 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004210
4211 return rc;
4212}
4213
Paul Moore07feee82009-03-27 17:10:54 -04004214/**
4215 * smack_inet_csk_clone - Copy the connection information to the new socket
4216 * @sk: the new socket
4217 * @req: the connection's request_sock
4218 *
4219 * Transfer the connection's peer label to the newly created socket.
4220 */
4221static void smack_inet_csk_clone(struct sock *sk,
4222 const struct request_sock *req)
4223{
4224 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004225 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004226
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004227 if (req->peer_secid != 0) {
4228 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004229 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004230 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004231 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004232}
4233
Casey Schauflere114e472008-02-04 22:29:50 -08004234/*
4235 * Key management security hooks
4236 *
4237 * Casey has not tested key support very heavily.
4238 * The permission check is most likely too restrictive.
4239 * If you care about keys please have a look.
4240 */
4241#ifdef CONFIG_KEYS
4242
4243/**
4244 * smack_key_alloc - Set the key security blob
4245 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004246 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004247 * @flags: unused
4248 *
4249 * No allocation required
4250 *
4251 * Returns 0
4252 */
David Howellsd84f4f92008-11-14 10:39:23 +11004253static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004254 unsigned long flags)
4255{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004256 struct smack_known *skp = smk_of_task(cred->security);
4257
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004258 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004259 return 0;
4260}
4261
4262/**
4263 * smack_key_free - Clear the key security blob
4264 * @key: the object
4265 *
4266 * Clear the blob pointer
4267 */
4268static void smack_key_free(struct key *key)
4269{
4270 key->security = NULL;
4271}
4272
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004273/**
Casey Schauflere114e472008-02-04 22:29:50 -08004274 * smack_key_permission - Smack access on a key
4275 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004276 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004277 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004278 *
4279 * Return 0 if the task has read and write to the object,
4280 * an error code otherwise
4281 */
4282static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004283 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004284{
4285 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004286 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004287 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004288 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004289 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004290
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004291 /*
4292 * Validate requested permissions
4293 */
4294 if (perm & ~KEY_NEED_ALL)
4295 return -EINVAL;
4296
Casey Schauflere114e472008-02-04 22:29:50 -08004297 keyp = key_ref_to_ptr(key_ref);
4298 if (keyp == NULL)
4299 return -EINVAL;
4300 /*
4301 * If the key hasn't been initialized give it access so that
4302 * it may do so.
4303 */
4304 if (keyp->security == NULL)
4305 return 0;
4306 /*
4307 * This should not occur
4308 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004309 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004310 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004311
4312 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4313 return 0;
4314
Etienne Bassetecfcc532009-04-08 20:40:06 +02004315#ifdef CONFIG_AUDIT
4316 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4317 ad.a.u.key_struct.key = keyp->serial;
4318 ad.a.u.key_struct.key_desc = keyp->description;
4319#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004320 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4321 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004322 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004323 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004324 rc = smk_access(tkp, keyp->security, request, &ad);
4325 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4326 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004327}
José Bollo7fc5f362015-02-17 15:41:22 +01004328
4329/*
4330 * smack_key_getsecurity - Smack label tagging the key
4331 * @key points to the key to be queried
4332 * @_buffer points to a pointer that should be set to point to the
4333 * resulting string (if no label or an error occurs).
4334 * Return the length of the string (including terminating NUL) or -ve if
4335 * an error.
4336 * May also return 0 (and a NULL buffer pointer) if there is no label.
4337 */
4338static int smack_key_getsecurity(struct key *key, char **_buffer)
4339{
4340 struct smack_known *skp = key->security;
4341 size_t length;
4342 char *copy;
4343
4344 if (key->security == NULL) {
4345 *_buffer = NULL;
4346 return 0;
4347 }
4348
4349 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4350 if (copy == NULL)
4351 return -ENOMEM;
4352 length = strlen(copy) + 1;
4353
4354 *_buffer = copy;
4355 return length;
4356}
4357
Casey Schauflere114e472008-02-04 22:29:50 -08004358#endif /* CONFIG_KEYS */
4359
4360/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004361 * Smack Audit hooks
4362 *
4363 * Audit requires a unique representation of each Smack specific
4364 * rule. This unique representation is used to distinguish the
4365 * object to be audited from remaining kernel objects and also
4366 * works as a glue between the audit hooks.
4367 *
4368 * Since repository entries are added but never deleted, we'll use
4369 * the smack_known label address related to the given audit rule as
4370 * the needed unique representation. This also better fits the smack
4371 * model where nearly everything is a label.
4372 */
4373#ifdef CONFIG_AUDIT
4374
4375/**
4376 * smack_audit_rule_init - Initialize a smack audit rule
4377 * @field: audit rule fields given from user-space (audit.h)
4378 * @op: required testing operator (=, !=, >, <, ...)
4379 * @rulestr: smack label to be audited
4380 * @vrule: pointer to save our own audit rule representation
4381 *
4382 * Prepare to audit cases where (@field @op @rulestr) is true.
4383 * The label to be audited is created if necessay.
4384 */
4385static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4386{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004387 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004388 char **rule = (char **)vrule;
4389 *rule = NULL;
4390
4391 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4392 return -EINVAL;
4393
Al Viro5af75d82008-12-16 05:59:26 -05004394 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004395 return -EINVAL;
4396
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004397 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004398 if (IS_ERR(skp))
4399 return PTR_ERR(skp);
4400
4401 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004402
4403 return 0;
4404}
4405
4406/**
4407 * smack_audit_rule_known - Distinguish Smack audit rules
4408 * @krule: rule of interest, in Audit kernel representation format
4409 *
4410 * This is used to filter Smack rules from remaining Audit ones.
4411 * If it's proved that this rule belongs to us, the
4412 * audit_rule_match hook will be called to do the final judgement.
4413 */
4414static int smack_audit_rule_known(struct audit_krule *krule)
4415{
4416 struct audit_field *f;
4417 int i;
4418
4419 for (i = 0; i < krule->field_count; i++) {
4420 f = &krule->fields[i];
4421
4422 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4423 return 1;
4424 }
4425
4426 return 0;
4427}
4428
4429/**
4430 * smack_audit_rule_match - Audit given object ?
4431 * @secid: security id for identifying the object to test
4432 * @field: audit rule flags given from user-space
4433 * @op: required testing operator
4434 * @vrule: smack internal rule presentation
4435 * @actx: audit context associated with the check
4436 *
4437 * The core Audit hook. It's used to take the decision of
4438 * whether to audit or not to audit a given object.
4439 */
4440static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4441 struct audit_context *actx)
4442{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004443 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004444 char *rule = vrule;
4445
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004446 if (unlikely(!rule)) {
4447 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004448 return -ENOENT;
4449 }
4450
4451 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4452 return 0;
4453
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004454 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004455
4456 /*
4457 * No need to do string comparisons. If a match occurs,
4458 * both pointers will point to the same smack_known
4459 * label.
4460 */
Al Viro5af75d82008-12-16 05:59:26 -05004461 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004462 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004463 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004464 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004465
4466 return 0;
4467}
4468
Casey Schaufler491a0b02016-01-26 15:08:35 -08004469/*
4470 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004471 * No memory was allocated.
4472 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004473
4474#endif /* CONFIG_AUDIT */
4475
Randy Dunlap251a2a92009-02-18 11:42:33 -08004476/**
David Quigley746df9b2013-05-22 12:50:35 -04004477 * smack_ismaclabel - check if xattr @name references a smack MAC label
4478 * @name: Full xattr name to check.
4479 */
4480static int smack_ismaclabel(const char *name)
4481{
4482 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4483}
4484
4485
4486/**
Casey Schauflere114e472008-02-04 22:29:50 -08004487 * smack_secid_to_secctx - return the smack label for a secid
4488 * @secid: incoming integer
4489 * @secdata: destination
4490 * @seclen: how long it is
4491 *
4492 * Exists for networking code.
4493 */
4494static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4495{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004496 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004497
Eric Parisd5630b92010-10-13 16:24:48 -04004498 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004499 *secdata = skp->smk_known;
4500 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004501 return 0;
4502}
4503
Randy Dunlap251a2a92009-02-18 11:42:33 -08004504/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004505 * smack_secctx_to_secid - return the secid for a smack label
4506 * @secdata: smack label
4507 * @seclen: how long result is
4508 * @secid: outgoing integer
4509 *
4510 * Exists for audit and networking code.
4511 */
David Howellse52c17642008-04-29 20:52:51 +01004512static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004513{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004514 struct smack_known *skp = smk_find_entry(secdata);
4515
4516 if (skp)
4517 *secid = skp->smk_secid;
4518 else
4519 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004520 return 0;
4521}
4522
Casey Schaufler491a0b02016-01-26 15:08:35 -08004523/*
4524 * There used to be a smack_release_secctx hook
4525 * that did nothing back when hooks were in a vector.
4526 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004527 */
Casey Schauflere114e472008-02-04 22:29:50 -08004528
David P. Quigley1ee65e32009-09-03 14:25:57 -04004529static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4530{
4531 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4532}
4533
4534static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4535{
4536 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4537}
4538
4539static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4540{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004541 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004542
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004543 *ctx = skp->smk_known;
4544 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004545 return 0;
4546}
4547
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004548static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4549{
4550
4551 struct task_smack *tsp;
4552 struct smack_known *skp;
4553 struct inode_smack *isp;
4554 struct cred *new_creds = *new;
4555
4556 if (new_creds == NULL) {
4557 new_creds = prepare_creds();
4558 if (new_creds == NULL)
4559 return -ENOMEM;
4560 }
4561
4562 tsp = new_creds->security;
4563
4564 /*
4565 * Get label from overlay inode and set it in create_sid
4566 */
4567 isp = d_inode(dentry->d_parent)->i_security;
4568 skp = isp->smk_inode;
4569 tsp->smk_task = skp;
4570 *new = new_creds;
4571 return 0;
4572}
4573
4574static int smack_inode_copy_up_xattr(const char *name)
4575{
4576 /*
4577 * Return 1 if this is the smack access Smack attribute.
4578 */
4579 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4580 return 1;
4581
4582 return -EOPNOTSUPP;
4583}
4584
4585static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4586 struct qstr *name,
4587 const struct cred *old,
4588 struct cred *new)
4589{
4590 struct task_smack *otsp = old->security;
4591 struct task_smack *ntsp = new->security;
4592 struct inode_smack *isp;
4593 int may;
4594
4595 /*
4596 * Use the process credential unless all of
4597 * the transmuting criteria are met
4598 */
4599 ntsp->smk_task = otsp->smk_task;
4600
4601 /*
4602 * the attribute of the containing directory
4603 */
4604 isp = d_inode(dentry->d_parent)->i_security;
4605
4606 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4607 rcu_read_lock();
4608 may = smk_access_entry(otsp->smk_task->smk_known,
4609 isp->smk_inode->smk_known,
4610 &otsp->smk_task->smk_rules);
4611 rcu_read_unlock();
4612
4613 /*
4614 * If the directory is transmuting and the rule
4615 * providing access is transmuting use the containing
4616 * directory label instead of the process label.
4617 */
4618 if (may > 0 && (may & MAY_TRANSMUTE))
4619 ntsp->smk_task = isp->smk_inode;
4620 }
4621 return 0;
4622}
4623
James Morrisca97d932017-02-15 00:18:51 +11004624static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004625 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4626 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4627 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004628
David Howells2febd252018-11-01 23:07:24 +00004629 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4630
Casey Schauflere20b0432015-05-02 15:11:36 -07004631 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4632 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004633 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004634 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004635 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304636 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004637
Casey Schauflere20b0432015-05-02 15:11:36 -07004638 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004639
Casey Schauflere20b0432015-05-02 15:11:36 -07004640 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4641 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4642 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4643 LSM_HOOK_INIT(inode_link, smack_inode_link),
4644 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4645 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4646 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4647 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4648 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4649 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4650 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4651 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4652 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4653 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4654 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4655 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4656 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4657 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004658
Casey Schauflere20b0432015-05-02 15:11:36 -07004659 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4660 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4661 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4662 LSM_HOOK_INIT(file_lock, smack_file_lock),
4663 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4664 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4665 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4666 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4667 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4668 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004669
Casey Schauflere20b0432015-05-02 15:11:36 -07004670 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004671
Casey Schauflere20b0432015-05-02 15:11:36 -07004672 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4673 LSM_HOOK_INIT(cred_free, smack_cred_free),
4674 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4675 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004676 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004677 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4678 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4679 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4680 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4681 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4682 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4683 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4684 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4685 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4686 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4687 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4688 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4689 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004690 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004691
Casey Schauflere20b0432015-05-02 15:11:36 -07004692 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4693 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004694
Casey Schauflere20b0432015-05-02 15:11:36 -07004695 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4696 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004697
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004698 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4699 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004700 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4701 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4702 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4703 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004704
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004705 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4706 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004707 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4708 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4709 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004710
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004711 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4712 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004713 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4714 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4715 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004716
Casey Schauflere20b0432015-05-02 15:11:36 -07004717 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004718
Casey Schauflere20b0432015-05-02 15:11:36 -07004719 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4720 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004721
Casey Schauflere20b0432015-05-02 15:11:36 -07004722 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4723 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004724
Casey Schauflere20b0432015-05-02 15:11:36 -07004725 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004726 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004727#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004728 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004729#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004730 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4731 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4732 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4733 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4734 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4735 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4736 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4737 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4738 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4739 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004740
Casey Schauflere114e472008-02-04 22:29:50 -08004741 /* key management security hooks */
4742#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004743 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4744 LSM_HOOK_INIT(key_free, smack_key_free),
4745 LSM_HOOK_INIT(key_permission, smack_key_permission),
4746 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004747#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004748
4749 /* Audit hooks */
4750#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004751 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4752 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4753 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004754#endif /* CONFIG_AUDIT */
4755
Casey Schauflere20b0432015-05-02 15:11:36 -07004756 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4757 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4758 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004759 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4760 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4761 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004762 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4763 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4764 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004765};
4766
Etienne Basset7198e2e2009-03-24 20:53:24 +01004767
Casey Schaufler86812bb2012-04-17 18:55:46 -07004768static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004769{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004770 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004771 * Initialize rule list locks
4772 */
4773 mutex_init(&smack_known_huh.smk_rules_lock);
4774 mutex_init(&smack_known_hat.smk_rules_lock);
4775 mutex_init(&smack_known_floor.smk_rules_lock);
4776 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004777 mutex_init(&smack_known_web.smk_rules_lock);
4778 /*
4779 * Initialize rule lists
4780 */
4781 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4782 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4783 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4784 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004785 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4786 /*
4787 * Create the known labels list
4788 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004789 smk_insert_entry(&smack_known_huh);
4790 smk_insert_entry(&smack_known_hat);
4791 smk_insert_entry(&smack_known_star);
4792 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004793 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004794}
4795
Casey Schauflere114e472008-02-04 22:29:50 -08004796/**
4797 * smack_init - initialize the smack system
4798 *
4799 * Returns 0
4800 */
4801static __init int smack_init(void)
4802{
David Howellsd84f4f92008-11-14 10:39:23 +11004803 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004804 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004805
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004806 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004807 return 0;
4808
Rohit1a5b4722014-10-15 17:40:41 +05304809 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4810 if (!smack_inode_cache)
4811 return -ENOMEM;
4812
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004813 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4814 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304815 if (tsp == NULL) {
4816 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004817 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304818 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004819
José Bollod21b7b02015-10-02 15:15:56 +02004820 smack_enabled = 1;
4821
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004822 pr_info("Smack: Initializing.\n");
4823#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4824 pr_info("Smack: Netfilter enabled.\n");
4825#endif
4826#ifdef SMACK_IPV6_PORT_LABELING
4827 pr_info("Smack: IPv6 port labeling enabled.\n");
4828#endif
4829#ifdef SMACK_IPV6_SECMARK_LABELING
4830 pr_info("Smack: IPv6 Netfilter enabled.\n");
4831#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004832
4833 /*
4834 * Set the security state for the initial task.
4835 */
David Howellsd84f4f92008-11-14 10:39:23 +11004836 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004837 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004838
Casey Schaufler86812bb2012-04-17 18:55:46 -07004839 /* initialize the smack_known_list */
4840 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004841
4842 /*
4843 * Register with LSM
4844 */
Casey Schauflerd69dece52017-01-18 17:09:05 -08004845 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
Casey Schauflere114e472008-02-04 22:29:50 -08004846
4847 return 0;
4848}
4849
4850/*
4851 * Smack requires early initialization in order to label
4852 * all processes and objects when they are created.
4853 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004854DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004855 .name = "smack",
Kees Cook3d6e5f62018-10-10 17:18:23 -07004856 .init = smack_init,
4857};