blob: 0e7703773a97806373c784ac1dbda3d6193e6aec [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080032#include <linux/mutex.h>
33#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080034#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070035#include <net/ip.h>
36#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100037#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070038#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050039#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030040#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000041#include <linux/msg.h>
42#include <linux/shm.h>
43#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053044#include <linux/parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080045#include "smack.h"
46
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020047#define TRANS_TRUE "TRUE"
48#define TRANS_TRUE_SIZE 4
49
Casey Schauflerc6739442013-05-22 18:42:56 -070050#define SMK_CONNECTING 0
51#define SMK_RECEIVING 1
52#define SMK_SENDING 2
53
Casey Schaufler21abb1e2015-07-22 14:25:31 -070054#ifdef SMACK_IPV6_PORT_LABELING
Geliang Tang8b549ef2015-09-27 23:10:25 +080055static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070056#endif
Rohit1a5b4722014-10-15 17:40:41 +053057static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080058int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070059
Casey Schaufler3d04c922015-08-12 11:56:02 -070060static const match_table_t smk_mount_tokens = {
Vivek Trivedi3bf27892015-06-22 15:36:06 +053061 {Opt_fsdefault, SMK_FSDEFAULT "%s"},
62 {Opt_fsfloor, SMK_FSFLOOR "%s"},
63 {Opt_fshat, SMK_FSHAT "%s"},
64 {Opt_fsroot, SMK_FSROOT "%s"},
65 {Opt_fstransmute, SMK_FSTRANS "%s"},
66 {Opt_error, NULL},
67};
68
Casey Schaufler3d04c922015-08-12 11:56:02 -070069#ifdef CONFIG_SECURITY_SMACK_BRINGUP
70static char *smk_bu_mess[] = {
71 "Bringup Error", /* Unused */
72 "Bringup", /* SMACK_BRINGUP_ALLOW */
73 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
74 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
75};
76
Casey Schauflerd166c802014-08-27 14:51:27 -070077static void smk_bu_mode(int mode, char *s)
78{
79 int i = 0;
80
81 if (mode & MAY_READ)
82 s[i++] = 'r';
83 if (mode & MAY_WRITE)
84 s[i++] = 'w';
85 if (mode & MAY_EXEC)
86 s[i++] = 'x';
87 if (mode & MAY_APPEND)
88 s[i++] = 'a';
89 if (mode & MAY_TRANSMUTE)
90 s[i++] = 't';
91 if (mode & MAY_LOCK)
92 s[i++] = 'l';
93 if (i == 0)
94 s[i++] = '-';
95 s[i] = '\0';
96}
97#endif
98
99#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200100static int smk_bu_note(char *note, struct smack_known *sskp,
101 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700102{
103 char acc[SMK_NUM_ACCESS_TYPE + 1];
104
105 if (rc <= 0)
106 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700107 if (rc > SMACK_UNCONFINED_OBJECT)
108 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700109
110 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700111 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200112 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700113 return 0;
114}
115#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200116#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700117#endif
118
119#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200120static int smk_bu_current(char *note, struct smack_known *oskp,
121 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700122{
123 struct task_smack *tsp = current_security();
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 %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200133 tsp->smk_task->smk_known, oskp->smk_known,
134 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700135 return 0;
136}
137#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200138#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700139#endif
140
141#ifdef CONFIG_SECURITY_SMACK_BRINGUP
142static int smk_bu_task(struct task_struct *otp, int mode, int rc)
143{
144 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300145 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700146 char acc[SMK_NUM_ACCESS_TYPE + 1];
147
148 if (rc <= 0)
149 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700150 if (rc > SMACK_UNCONFINED_OBJECT)
151 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700152
153 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700154 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300155 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700156 current->comm, otp->comm);
157 return 0;
158}
159#else
160#define smk_bu_task(otp, mode, RC) (RC)
161#endif
162
163#ifdef CONFIG_SECURITY_SMACK_BRINGUP
164static int smk_bu_inode(struct inode *inode, int mode, int rc)
165{
166 struct task_smack *tsp = current_security();
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700167 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700168 char acc[SMK_NUM_ACCESS_TYPE + 1];
169
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700170 if (isp->smk_flags & SMK_INODE_IMPURE)
171 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
172 inode->i_sb->s_id, inode->i_ino, current->comm);
173
Casey Schauflerd166c802014-08-27 14:51:27 -0700174 if (rc <= 0)
175 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700176 if (rc > SMACK_UNCONFINED_OBJECT)
177 rc = 0;
178 if (rc == SMACK_UNCONFINED_SUBJECT &&
179 (mode & (MAY_WRITE | MAY_APPEND)))
180 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700181
182 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700183
184 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
185 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700186 inode->i_sb->s_id, inode->i_ino, current->comm);
187 return 0;
188}
189#else
190#define smk_bu_inode(inode, mode, RC) (RC)
191#endif
192
193#ifdef CONFIG_SECURITY_SMACK_BRINGUP
194static int smk_bu_file(struct file *file, int mode, int rc)
195{
196 struct task_smack *tsp = current_security();
197 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800198 struct inode *inode = file_inode(file);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700199 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700200 char acc[SMK_NUM_ACCESS_TYPE + 1];
201
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700202 if (isp->smk_flags & SMK_INODE_IMPURE)
203 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
204 inode->i_sb->s_id, inode->i_ino, current->comm);
205
Casey Schauflerd166c802014-08-27 14:51:27 -0700206 if (rc <= 0)
207 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700208 if (rc > SMACK_UNCONFINED_OBJECT)
209 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700210
211 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700212 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800213 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400214 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700215 current->comm);
216 return 0;
217}
218#else
219#define smk_bu_file(file, mode, RC) (RC)
220#endif
221
222#ifdef CONFIG_SECURITY_SMACK_BRINGUP
223static int smk_bu_credfile(const struct cred *cred, struct file *file,
224 int mode, int rc)
225{
226 struct task_smack *tsp = cred->security;
227 struct smack_known *sskp = tsp->smk_task;
228 struct inode *inode = file->f_inode;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700229 struct inode_smack *isp = inode->i_security;
Casey Schauflerd166c802014-08-27 14:51:27 -0700230 char acc[SMK_NUM_ACCESS_TYPE + 1];
231
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700232 if (isp->smk_flags & SMK_INODE_IMPURE)
233 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
234 inode->i_sb->s_id, inode->i_ino, current->comm);
235
Casey Schauflerd166c802014-08-27 14:51:27 -0700236 if (rc <= 0)
237 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700238 if (rc > SMACK_UNCONFINED_OBJECT)
239 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700240
241 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700242 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200243 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400244 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700245 current->comm);
246 return 0;
247}
248#else
249#define smk_bu_credfile(cred, file, mode, RC) (RC)
250#endif
251
Casey Schauflere114e472008-02-04 22:29:50 -0800252/**
253 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100254 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800255 * @ip: a pointer to the inode
256 * @dp: a pointer to the dentry
257 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200258 * Returns a pointer to the master list entry for the Smack label,
259 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800260 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700261static struct smack_known *smk_fetch(const char *name, struct inode *ip,
262 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800263{
264 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700265 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700266 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800267
268 if (ip->i_op->getxattr == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200269 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800270
Casey Schauflerf7112e62012-05-06 15:22:02 -0700271 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
272 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200273 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800274
Casey Schauflerf7112e62012-05-06 15:22:02 -0700275 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200276 if (rc < 0)
277 skp = ERR_PTR(rc);
278 else if (rc == 0)
279 skp = NULL;
280 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700281 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700282
283 kfree(buffer);
284
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700285 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800286}
287
288/**
289 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200290 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800291 *
292 * Returns the new blob or NULL if there's no memory available
293 */
Casey Schaufler1eddfe82015-07-30 14:35:14 -0700294static struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800295{
296 struct inode_smack *isp;
297
Rohit1a5b4722014-10-15 17:40:41 +0530298 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800299 if (isp == NULL)
300 return NULL;
301
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200302 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800303 isp->smk_flags = 0;
304 mutex_init(&isp->smk_lock);
305
306 return isp;
307}
308
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800309/**
310 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100311 * @task: a pointer to the Smack label for the running task
312 * @forked: a pointer to the Smack label for the forked task
313 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800314 *
315 * Returns the new blob or NULL if there's no memory available
316 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700317static struct task_smack *new_task_smack(struct smack_known *task,
318 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800319{
320 struct task_smack *tsp;
321
322 tsp = kzalloc(sizeof(struct task_smack), gfp);
323 if (tsp == NULL)
324 return NULL;
325
326 tsp->smk_task = task;
327 tsp->smk_forked = forked;
328 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200329 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800330 mutex_init(&tsp->smk_rules_lock);
331
332 return tsp;
333}
334
335/**
336 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100337 * @nhead: new rules header pointer
338 * @ohead: old rules header pointer
339 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800340 *
341 * Returns 0 on success, -ENOMEM on error
342 */
343static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
344 gfp_t gfp)
345{
346 struct smack_rule *nrp;
347 struct smack_rule *orp;
348 int rc = 0;
349
350 INIT_LIST_HEAD(nhead);
351
352 list_for_each_entry_rcu(orp, ohead, list) {
353 nrp = kzalloc(sizeof(struct smack_rule), gfp);
354 if (nrp == NULL) {
355 rc = -ENOMEM;
356 break;
357 }
358 *nrp = *orp;
359 list_add_rcu(&nrp->list, nhead);
360 }
361 return rc;
362}
363
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100364/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200365 * smk_copy_relabel - copy smk_relabel labels list
366 * @nhead: new rules header pointer
367 * @ohead: old rules header pointer
368 * @gfp: type of the memory for the allocation
369 *
370 * Returns 0 on success, -ENOMEM on error
371 */
372static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
373 gfp_t gfp)
374{
375 struct smack_known_list_elem *nklep;
376 struct smack_known_list_elem *oklep;
377
378 INIT_LIST_HEAD(nhead);
379
380 list_for_each_entry(oklep, ohead, list) {
381 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
382 if (nklep == NULL) {
383 smk_destroy_label_list(nhead);
384 return -ENOMEM;
385 }
386 nklep->smk_label = oklep->smk_label;
387 list_add(&nklep->list, nhead);
388 }
389
390 return 0;
391}
392
393/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100394 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
395 * @mode - input mode in form of PTRACE_MODE_*
396 *
397 * Returns a converted MAY_* mode usable by smack rules
398 */
399static inline unsigned int smk_ptrace_mode(unsigned int mode)
400{
401 switch (mode) {
402 case PTRACE_MODE_READ:
403 return MAY_READ;
404 case PTRACE_MODE_ATTACH:
405 return MAY_READWRITE;
406 }
407
408 return 0;
409}
410
411/**
412 * smk_ptrace_rule_check - helper for ptrace access
413 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200414 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100415 * @mode: ptrace attachment mode (PTRACE_MODE_*)
416 * @func: name of the function that called us, used for audit
417 *
418 * Returns 0 on access granted, -error on error
419 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200420static int smk_ptrace_rule_check(struct task_struct *tracer,
421 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100422 unsigned int mode, const char *func)
423{
424 int rc;
425 struct smk_audit_info ad, *saip = NULL;
426 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200427 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100428
429 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
430 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
431 smk_ad_setfield_u_tsk(&ad, tracer);
432 saip = &ad;
433 }
434
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300435 rcu_read_lock();
436 tsp = __task_cred(tracer)->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200437 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100438
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100439 if ((mode & PTRACE_MODE_ATTACH) &&
440 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
441 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200442 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100443 rc = 0;
444 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
445 rc = -EACCES;
446 else if (capable(CAP_SYS_PTRACE))
447 rc = 0;
448 else
449 rc = -EACCES;
450
451 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200452 smack_log(tracer_known->smk_known,
453 tracee_known->smk_known,
454 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100455
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300456 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100457 return rc;
458 }
459
460 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200461 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300462
463 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100464 return rc;
465}
466
Casey Schauflere114e472008-02-04 22:29:50 -0800467/*
468 * LSM hooks.
469 * We he, that is fun!
470 */
471
472/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000473 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800474 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100475 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800476 *
477 * Returns 0 if access is OK, an error code otherwise
478 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100479 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800480 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000481static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800482{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700483 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800484
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300485 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200486
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700487 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100488}
Casey Schauflere114e472008-02-04 22:29:50 -0800489
David Howells5cd9c582008-08-14 11:37:28 +0100490/**
491 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
492 * @ptp: parent task pointer
493 *
494 * Returns 0 if access is OK, an error code otherwise
495 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100496 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100497 */
498static int smack_ptrace_traceme(struct task_struct *ptp)
499{
500 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700501 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100502
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100503 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200504
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200505 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800506 return rc;
507}
508
509/**
510 * smack_syslog - Smack approval on syslog
511 * @type: message type
512 *
Casey Schauflere114e472008-02-04 22:29:50 -0800513 * Returns 0 on success, error code otherwise.
514 */
Eric Paris12b30522010-11-15 18:36:29 -0500515static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800516{
Eric Paris12b30522010-11-15 18:36:29 -0500517 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700518 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800519
Casey Schaufler1880eff2012-06-05 15:28:30 -0700520 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800521 return 0;
522
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800523 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800524 rc = -EACCES;
525
526 return rc;
527}
528
529
530/*
531 * Superblock Hooks.
532 */
533
534/**
535 * smack_sb_alloc_security - allocate a superblock blob
536 * @sb: the superblock getting the blob
537 *
538 * Returns 0 on success or -ENOMEM on error.
539 */
540static int smack_sb_alloc_security(struct super_block *sb)
541{
542 struct superblock_smack *sbsp;
543
544 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
545
546 if (sbsp == NULL)
547 return -ENOMEM;
548
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200549 sbsp->smk_root = &smack_known_floor;
550 sbsp->smk_default = &smack_known_floor;
551 sbsp->smk_floor = &smack_known_floor;
552 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700553 /*
554 * smk_initialized will be zero from kzalloc.
555 */
Casey Schauflere114e472008-02-04 22:29:50 -0800556 sb->s_security = sbsp;
557
558 return 0;
559}
560
561/**
562 * smack_sb_free_security - free a superblock blob
563 * @sb: the superblock getting the blob
564 *
565 */
566static void smack_sb_free_security(struct super_block *sb)
567{
568 kfree(sb->s_security);
569 sb->s_security = NULL;
570}
571
572/**
573 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800574 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800575 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800576 *
577 * Returns 0 on success or -ENOMEM on error.
578 *
579 * Copy the Smack specific mount options out of the mount
580 * options list.
581 */
Eric Parise0007522008-03-05 10:31:54 -0500582static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800583{
584 char *cp, *commap, *otheropts, *dp;
585
Casey Schauflere114e472008-02-04 22:29:50 -0800586 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
587 if (otheropts == NULL)
588 return -ENOMEM;
589
590 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
591 if (strstr(cp, SMK_FSDEFAULT) == cp)
592 dp = smackopts;
593 else if (strstr(cp, SMK_FSFLOOR) == cp)
594 dp = smackopts;
595 else if (strstr(cp, SMK_FSHAT) == cp)
596 dp = smackopts;
597 else if (strstr(cp, SMK_FSROOT) == cp)
598 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700599 else if (strstr(cp, SMK_FSTRANS) == cp)
600 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800601 else
602 dp = otheropts;
603
604 commap = strchr(cp, ',');
605 if (commap != NULL)
606 *commap = '\0';
607
608 if (*dp != '\0')
609 strcat(dp, ",");
610 strcat(dp, cp);
611 }
612
613 strcpy(orig, otheropts);
614 free_page((unsigned long)otheropts);
615
616 return 0;
617}
618
619/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530620 * smack_parse_opts_str - parse Smack specific mount options
621 * @options: mount options string
622 * @opts: where to store converted mount opts
623 *
624 * Returns 0 on success or -ENOMEM on error.
625 *
626 * converts Smack specific mount options to generic security option format
627 */
628static int smack_parse_opts_str(char *options,
629 struct security_mnt_opts *opts)
630{
631 char *p;
Casey Schaufler3d04c922015-08-12 11:56:02 -0700632 char *fsdefault = NULL;
633 char *fsfloor = NULL;
634 char *fshat = NULL;
635 char *fsroot = NULL;
636 char *fstransmute = NULL;
637 int rc = -ENOMEM;
638 int num_mnt_opts = 0;
639 int token;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530640
641 opts->num_mnt_opts = 0;
642
643 if (!options)
644 return 0;
645
646 while ((p = strsep(&options, ",")) != NULL) {
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530647 substring_t args[MAX_OPT_ARGS];
648
649 if (!*p)
650 continue;
651
Casey Schaufler3d04c922015-08-12 11:56:02 -0700652 token = match_token(p, smk_mount_tokens, args);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530653
654 switch (token) {
655 case Opt_fsdefault:
656 if (fsdefault)
657 goto out_opt_err;
658 fsdefault = match_strdup(&args[0]);
659 if (!fsdefault)
660 goto out_err;
661 break;
662 case Opt_fsfloor:
663 if (fsfloor)
664 goto out_opt_err;
665 fsfloor = match_strdup(&args[0]);
666 if (!fsfloor)
667 goto out_err;
668 break;
669 case Opt_fshat:
670 if (fshat)
671 goto out_opt_err;
672 fshat = match_strdup(&args[0]);
673 if (!fshat)
674 goto out_err;
675 break;
676 case Opt_fsroot:
677 if (fsroot)
678 goto out_opt_err;
679 fsroot = match_strdup(&args[0]);
680 if (!fsroot)
681 goto out_err;
682 break;
683 case Opt_fstransmute:
684 if (fstransmute)
685 goto out_opt_err;
686 fstransmute = match_strdup(&args[0]);
687 if (!fstransmute)
688 goto out_err;
689 break;
690 default:
691 rc = -EINVAL;
692 pr_warn("Smack: unknown mount option\n");
693 goto out_err;
694 }
695 }
696
697 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
698 if (!opts->mnt_opts)
699 goto out_err;
700
701 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
702 GFP_ATOMIC);
703 if (!opts->mnt_opts_flags) {
704 kfree(opts->mnt_opts);
705 goto out_err;
706 }
707
708 if (fsdefault) {
709 opts->mnt_opts[num_mnt_opts] = fsdefault;
710 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
711 }
712 if (fsfloor) {
713 opts->mnt_opts[num_mnt_opts] = fsfloor;
714 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
715 }
716 if (fshat) {
717 opts->mnt_opts[num_mnt_opts] = fshat;
718 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
719 }
720 if (fsroot) {
721 opts->mnt_opts[num_mnt_opts] = fsroot;
722 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
723 }
724 if (fstransmute) {
725 opts->mnt_opts[num_mnt_opts] = fstransmute;
726 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
727 }
728
729 opts->num_mnt_opts = num_mnt_opts;
730 return 0;
731
732out_opt_err:
733 rc = -EINVAL;
734 pr_warn("Smack: duplicate mount options\n");
735
736out_err:
737 kfree(fsdefault);
738 kfree(fsfloor);
739 kfree(fshat);
740 kfree(fsroot);
741 kfree(fstransmute);
742 return rc;
743}
744
745/**
746 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800747 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530748 * @opts: Smack mount options
749 * @kern_flags: mount option from kernel space or user space
750 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800751 *
752 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530753 *
754 * Allow filesystems with binary mount data to explicitly set Smack mount
755 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800756 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530757static int smack_set_mnt_opts(struct super_block *sb,
758 struct security_mnt_opts *opts,
759 unsigned long kern_flags,
760 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800761{
762 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000763 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800764 struct superblock_smack *sp = sb->s_security;
765 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800766 struct smack_known *skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530767 int i;
768 int num_opts = opts->num_mnt_opts;
Casey Schauflere830b392013-05-22 18:43:07 -0700769 int transmute = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800770
Casey Schauflere830b392013-05-22 18:43:07 -0700771 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800772 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700773
Casey Schauflere114e472008-02-04 22:29:50 -0800774 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800775
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530776 for (i = 0; i < num_opts; i++) {
777 switch (opts->mnt_opts_flags[i]) {
778 case FSDEFAULT_MNT:
779 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200780 if (IS_ERR(skp))
781 return PTR_ERR(skp);
782 sp->smk_default = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530783 break;
784 case FSFLOOR_MNT:
785 skp = smk_import_entry(opts->mnt_opts[i], 0);
786 if (IS_ERR(skp))
787 return PTR_ERR(skp);
788 sp->smk_floor = skp;
789 break;
790 case FSHAT_MNT:
791 skp = smk_import_entry(opts->mnt_opts[i], 0);
792 if (IS_ERR(skp))
793 return PTR_ERR(skp);
794 sp->smk_hat = skp;
795 break;
796 case FSROOT_MNT:
797 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200798 if (IS_ERR(skp))
799 return PTR_ERR(skp);
800 sp->smk_root = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530801 break;
802 case FSTRANS_MNT:
803 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200804 if (IS_ERR(skp))
805 return PTR_ERR(skp);
806 sp->smk_root = skp;
807 transmute = 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530808 break;
809 default:
810 break;
Casey Schauflere114e472008-02-04 22:29:50 -0800811 }
812 }
813
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800814 if (!smack_privileged(CAP_MAC_ADMIN)) {
815 /*
816 * Unprivileged mounts don't get to specify Smack values.
817 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530818 if (num_opts)
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800819 return -EPERM;
820 /*
821 * Unprivileged mounts get root and default from the caller.
822 */
823 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200824 sp->smk_root = skp;
825 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800826 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530827
Casey Schauflere114e472008-02-04 22:29:50 -0800828 /*
829 * Initialize the root inode.
830 */
831 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100832 if (isp == NULL) {
833 isp = new_inode_smack(sp->smk_root);
834 if (isp == NULL)
835 return -ENOMEM;
836 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700837 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800838 isp->smk_inode = sp->smk_root;
839
Casey Schauflere830b392013-05-22 18:43:07 -0700840 if (transmute)
841 isp->smk_flags |= SMK_INODE_TRANSMUTE;
842
Casey Schauflere114e472008-02-04 22:29:50 -0800843 return 0;
844}
845
846/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530847 * smack_sb_kern_mount - Smack specific mount processing
848 * @sb: the file system superblock
849 * @flags: the mount flags
850 * @data: the smack mount options
851 *
852 * Returns 0 on success, an error code on failure
853 */
854static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
855{
856 int rc = 0;
857 char *options = data;
858 struct security_mnt_opts opts;
859
860 security_init_mnt_opts(&opts);
861
862 if (!options)
863 goto out;
864
865 rc = smack_parse_opts_str(options, &opts);
866 if (rc)
867 goto out_err;
868
869out:
870 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
871
872out_err:
873 security_free_mnt_opts(&opts);
874 return rc;
875}
876
877/**
Casey Schauflere114e472008-02-04 22:29:50 -0800878 * smack_sb_statfs - Smack check on statfs
879 * @dentry: identifies the file system in question
880 *
881 * Returns 0 if current can read the floor of the filesystem,
882 * and error code otherwise
883 */
884static int smack_sb_statfs(struct dentry *dentry)
885{
886 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200887 int rc;
888 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800889
Eric Parisa2694342011-04-25 13:10:27 -0400890 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200891 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
892
893 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700894 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200895 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800896}
897
Casey Schauflere114e472008-02-04 22:29:50 -0800898/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800899 * BPRM hooks
900 */
901
Casey Schauflerce8a4322011-09-29 18:21:01 -0700902/**
903 * smack_bprm_set_creds - set creds for exec
904 * @bprm: the exec information
905 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100906 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700907 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800908static int smack_bprm_set_creds(struct linux_binprm *bprm)
909{
Al Viro496ad9a2013-01-23 17:07:38 -0500910 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300911 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800912 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800913 int rc;
914
Casey Schaufler676dac42010-12-02 06:43:39 -0800915 if (bprm->cred_prepared)
916 return 0;
917
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300918 isp = inode->i_security;
919 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800920 return 0;
921
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100922 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
923 struct task_struct *tracer;
924 rc = 0;
925
926 rcu_read_lock();
927 tracer = ptrace_parent(current);
928 if (likely(tracer != NULL))
929 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200930 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100931 PTRACE_MODE_ATTACH,
932 __func__);
933 rcu_read_unlock();
934
935 if (rc != 0)
936 return rc;
937 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300938 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800939
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300940 bsp->smk_task = isp->smk_task;
941 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800942
943 return 0;
944}
945
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300946/**
947 * smack_bprm_committing_creds - Prepare to install the new credentials
948 * from bprm.
949 *
950 * @bprm: binprm for exec
951 */
952static void smack_bprm_committing_creds(struct linux_binprm *bprm)
953{
954 struct task_smack *bsp = bprm->cred->security;
955
956 if (bsp->smk_task != bsp->smk_forked)
957 current->pdeath_signal = 0;
958}
959
960/**
961 * smack_bprm_secureexec - Return the decision to use secureexec.
962 * @bprm: binprm for exec
963 *
964 * Returns 0 on success.
965 */
966static int smack_bprm_secureexec(struct linux_binprm *bprm)
967{
968 struct task_smack *tsp = current_security();
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300969
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700970 if (tsp->smk_task != tsp->smk_forked)
971 return 1;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300972
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700973 return 0;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300974}
975
Casey Schaufler676dac42010-12-02 06:43:39 -0800976/*
Casey Schauflere114e472008-02-04 22:29:50 -0800977 * Inode hooks
978 */
979
980/**
981 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800982 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800983 *
984 * Returns 0 if it gets a blob, -ENOMEM otherwise
985 */
986static int smack_inode_alloc_security(struct inode *inode)
987{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700988 struct smack_known *skp = smk_of_current();
989
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200990 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800991 if (inode->i_security == NULL)
992 return -ENOMEM;
993 return 0;
994}
995
996/**
997 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800998 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800999 *
1000 * Clears the blob pointer in inode
1001 */
1002static void smack_inode_free_security(struct inode *inode)
1003{
Rohit1a5b4722014-10-15 17:40:41 +05301004 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -08001005 inode->i_security = NULL;
1006}
1007
1008/**
1009 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001010 * @inode: the newly created inode
1011 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -05001012 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001013 * @name: where to put the attribute name
1014 * @value: where to put the attribute value
1015 * @len: where to put the length of the attribute
1016 *
1017 * Returns 0 if it all works out, -ENOMEM if there's no memory
1018 */
1019static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09001020 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05001021 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -08001022{
Casey Schaufler2267b132012-03-13 19:14:19 -07001023 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001024 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001025 struct smack_known *isp = smk_of_inode(inode);
1026 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001027 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08001028
Tetsuo Handa95489062013-07-25 05:44:02 +09001029 if (name)
1030 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -08001031
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001032 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001033 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001034 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1035 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001036 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001037
1038 /*
1039 * If the access rule allows transmutation and
1040 * the directory requests transmutation then
1041 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001042 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001043 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001044 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001045 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001046 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001047 issp->smk_flags |= SMK_INODE_CHANGED;
1048 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001049
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001050 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001051 if (*value == NULL)
1052 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001053
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001054 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001055 }
Casey Schauflere114e472008-02-04 22:29:50 -08001056
1057 return 0;
1058}
1059
1060/**
1061 * smack_inode_link - Smack check on link
1062 * @old_dentry: the existing object
1063 * @dir: unused
1064 * @new_dentry: the new object
1065 *
1066 * Returns 0 if access is permitted, an error code otherwise
1067 */
1068static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1069 struct dentry *new_dentry)
1070{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001071 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001072 struct smk_audit_info ad;
1073 int rc;
1074
Eric Parisa2694342011-04-25 13:10:27 -04001075 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001076 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001077
David Howellsc6f493d2015-03-17 22:26:22 +00001078 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001079 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001080 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001081
David Howells88025652015-01-29 12:02:32 +00001082 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001083 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001084 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1085 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001086 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001087 }
1088
1089 return rc;
1090}
1091
1092/**
1093 * smack_inode_unlink - Smack check on inode deletion
1094 * @dir: containing directory object
1095 * @dentry: file to unlink
1096 *
1097 * Returns 0 if current can write the containing directory
1098 * and the object, error code otherwise
1099 */
1100static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1101{
David Howellsc6f493d2015-03-17 22:26:22 +00001102 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001104 int rc;
1105
Eric Parisa2694342011-04-25 13:10:27 -04001106 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001107 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1108
Casey Schauflere114e472008-02-04 22:29:50 -08001109 /*
1110 * You need write access to the thing you're unlinking
1111 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001112 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001113 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001114 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001115 /*
1116 * You also need write access to the containing directory
1117 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001118 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001119 smk_ad_setfield_u_fs_inode(&ad, dir);
1120 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001121 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001122 }
Casey Schauflere114e472008-02-04 22:29:50 -08001123 return rc;
1124}
1125
1126/**
1127 * smack_inode_rmdir - Smack check on directory deletion
1128 * @dir: containing directory object
1129 * @dentry: directory to unlink
1130 *
1131 * Returns 0 if current can write the containing directory
1132 * and the directory, error code otherwise
1133 */
1134static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1135{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001136 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001137 int rc;
1138
Eric Parisa2694342011-04-25 13:10:27 -04001139 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001140 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1141
Casey Schauflere114e472008-02-04 22:29:50 -08001142 /*
1143 * You need write access to the thing you're removing
1144 */
David Howellsc6f493d2015-03-17 22:26:22 +00001145 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1146 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001147 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001148 /*
1149 * You also need write access to the containing directory
1150 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001151 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001152 smk_ad_setfield_u_fs_inode(&ad, dir);
1153 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001154 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001155 }
Casey Schauflere114e472008-02-04 22:29:50 -08001156
1157 return rc;
1158}
1159
1160/**
1161 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001162 * @old_inode: unused
1163 * @old_dentry: the old object
1164 * @new_inode: unused
1165 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001166 *
1167 * Read and write access is required on both the old and
1168 * new directories.
1169 *
1170 * Returns 0 if access is permitted, an error code otherwise
1171 */
1172static int smack_inode_rename(struct inode *old_inode,
1173 struct dentry *old_dentry,
1174 struct inode *new_inode,
1175 struct dentry *new_dentry)
1176{
1177 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001178 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001179 struct smk_audit_info ad;
1180
Eric Parisa2694342011-04-25 13:10:27 -04001181 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001182 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001183
David Howellsc6f493d2015-03-17 22:26:22 +00001184 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001185 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001186 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001187
David Howells88025652015-01-29 12:02:32 +00001188 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001189 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001190 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1191 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001192 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001193 }
Casey Schauflere114e472008-02-04 22:29:50 -08001194 return rc;
1195}
1196
1197/**
1198 * smack_inode_permission - Smack version of permission()
1199 * @inode: the inode in question
1200 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001201 *
1202 * This is the important Smack hook.
1203 *
1204 * Returns 0 if access is permitted, -EACCES otherwise
1205 */
Al Viroe74f71e2011-06-20 19:38:15 -04001206static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001207{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001208 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001209 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001210 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001211
1212 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001213 /*
1214 * No permission to check. Existence test. Yup, it's there.
1215 */
1216 if (mask == 0)
1217 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001218
1219 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001220 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001221 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001222 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001223 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001224 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1225 rc = smk_bu_inode(inode, mask, rc);
1226 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001227}
1228
1229/**
1230 * smack_inode_setattr - Smack check for setting attributes
1231 * @dentry: the object
1232 * @iattr: for the force flag
1233 *
1234 * Returns 0 if access is permitted, an error code otherwise
1235 */
1236static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1237{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001238 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001239 int rc;
1240
Casey Schauflere114e472008-02-04 22:29:50 -08001241 /*
1242 * Need to allow for clearing the setuid bit.
1243 */
1244 if (iattr->ia_valid & ATTR_FORCE)
1245 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001246 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001247 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001248
David Howellsc6f493d2015-03-17 22:26:22 +00001249 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1250 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001251 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001252}
1253
1254/**
1255 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001256 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001257 * @dentry: the object
1258 *
1259 * Returns 0 if access is permitted, an error code otherwise
1260 */
Al Viro3f7036a2015-03-08 19:28:30 -04001261static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001262{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001263 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001264 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001265 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001266
Eric Parisf48b7392011-04-25 12:54:27 -04001267 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001268 smk_ad_setfield_u_fs_path(&ad, *path);
1269 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1270 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001271 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001272}
1273
1274/**
1275 * smack_inode_setxattr - Smack check for setting xattrs
1276 * @dentry: the object
1277 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001278 * @value: value of the attribute
1279 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001280 * @flags: unused
1281 *
1282 * This protects the Smack attribute explicitly.
1283 *
1284 * Returns 0 if access is permitted, an error code otherwise
1285 */
David Howells8f0cfa52008-04-29 00:59:41 -07001286static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1287 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001288{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001289 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001290 struct smack_known *skp;
1291 int check_priv = 0;
1292 int check_import = 0;
1293 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001294 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001295
Casey Schaufler19760ad2013-12-16 16:27:26 -08001296 /*
1297 * Check label validity here so import won't fail in post_setxattr
1298 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001299 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1300 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001301 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1302 check_priv = 1;
1303 check_import = 1;
1304 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1305 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1306 check_priv = 1;
1307 check_import = 1;
1308 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001309 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001310 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001311 if (size != TRANS_TRUE_SIZE ||
1312 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1313 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001314 } else
1315 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1316
Casey Schaufler19760ad2013-12-16 16:27:26 -08001317 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1318 rc = -EPERM;
1319
1320 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001321 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001322 if (IS_ERR(skp))
1323 rc = PTR_ERR(skp);
1324 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001325 (skp == &smack_known_star || skp == &smack_known_web)))
1326 rc = -EINVAL;
1327 }
1328
Eric Parisa2694342011-04-25 13:10:27 -04001329 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001330 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1331
Casey Schauflerd166c802014-08-27 14:51:27 -07001332 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001333 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1334 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001335 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001336
1337 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001338}
1339
1340/**
1341 * smack_inode_post_setxattr - Apply the Smack update approved above
1342 * @dentry: object
1343 * @name: attribute name
1344 * @value: attribute value
1345 * @size: attribute size
1346 * @flags: unused
1347 *
1348 * Set the pointer in the inode blob to the entry found
1349 * in the master label list.
1350 */
David Howells8f0cfa52008-04-29 00:59:41 -07001351static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1352 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001353{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001354 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001355 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001356
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001357 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1358 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1359 return;
1360 }
1361
Casey Schaufler676dac42010-12-02 06:43:39 -08001362 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001363 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001364 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001365 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001366 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001367 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001368 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001369 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001370 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001371 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001372 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001373 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001374 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001375 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001376 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001377 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001378 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001379 isp->smk_mmap = &smack_known_invalid;
1380 }
Casey Schauflere114e472008-02-04 22:29:50 -08001381
1382 return;
1383}
1384
Casey Schauflerce8a4322011-09-29 18:21:01 -07001385/**
Casey Schauflere114e472008-02-04 22:29:50 -08001386 * smack_inode_getxattr - Smack check on getxattr
1387 * @dentry: the object
1388 * @name: unused
1389 *
1390 * Returns 0 if access is permitted, an error code otherwise
1391 */
David Howells8f0cfa52008-04-29 00:59:41 -07001392static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001393{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001394 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001395 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001396
Eric Parisa2694342011-04-25 13:10:27 -04001397 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001398 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1399
David Howellsc6f493d2015-03-17 22:26:22 +00001400 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1401 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001402 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001403}
1404
Casey Schauflerce8a4322011-09-29 18:21:01 -07001405/**
Casey Schauflere114e472008-02-04 22:29:50 -08001406 * smack_inode_removexattr - Smack check on removexattr
1407 * @dentry: the object
1408 * @name: name of the attribute
1409 *
1410 * Removing the Smack attribute requires CAP_MAC_ADMIN
1411 *
1412 * Returns 0 if access is permitted, an error code otherwise
1413 */
David Howells8f0cfa52008-04-29 00:59:41 -07001414static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001415{
Casey Schaufler676dac42010-12-02 06:43:39 -08001416 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001417 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001418 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001419
Casey Schauflerbcdca222008-02-23 15:24:04 -08001420 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1421 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001422 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001423 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001424 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301425 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001426 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001427 rc = -EPERM;
1428 } else
1429 rc = cap_inode_removexattr(dentry, name);
1430
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001431 if (rc != 0)
1432 return rc;
1433
Eric Parisa2694342011-04-25 13:10:27 -04001434 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001435 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001436
David Howellsc6f493d2015-03-17 22:26:22 +00001437 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1438 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001439 if (rc != 0)
1440 return rc;
1441
David Howellsc6f493d2015-03-17 22:26:22 +00001442 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001443 /*
1444 * Don't do anything special for these.
1445 * XATTR_NAME_SMACKIPIN
1446 * XATTR_NAME_SMACKIPOUT
1447 * XATTR_NAME_SMACKEXEC
1448 */
1449 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001450 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001451 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001452 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001453 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1454 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001455
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001456 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001457}
1458
1459/**
1460 * smack_inode_getsecurity - get smack xattrs
1461 * @inode: the object
1462 * @name: attribute name
1463 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001464 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001465 *
1466 * Returns the size of the attribute or an error code
1467 */
1468static int smack_inode_getsecurity(const struct inode *inode,
1469 const char *name, void **buffer,
1470 bool alloc)
1471{
1472 struct socket_smack *ssp;
1473 struct socket *sock;
1474 struct super_block *sbp;
1475 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001476 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001477 int ilen;
1478 int rc = 0;
1479
1480 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1481 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001482 ilen = strlen(isp->smk_known);
1483 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001484 return ilen;
1485 }
1486
1487 /*
1488 * The rest of the Smack xattrs are only on sockets.
1489 */
1490 sbp = ip->i_sb;
1491 if (sbp->s_magic != SOCKFS_MAGIC)
1492 return -EOPNOTSUPP;
1493
1494 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001495 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001496 return -EOPNOTSUPP;
1497
1498 ssp = sock->sk->sk_security;
1499
1500 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001501 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001502 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001503 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001504 else
1505 return -EOPNOTSUPP;
1506
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001507 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001508 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001509 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001510 rc = ilen;
1511 }
1512
1513 return rc;
1514}
1515
1516
1517/**
1518 * smack_inode_listsecurity - list the Smack attributes
1519 * @inode: the object
1520 * @buffer: where they go
1521 * @buffer_size: size of buffer
1522 *
1523 * Returns 0 on success, -EINVAL otherwise
1524 */
1525static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1526 size_t buffer_size)
1527{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001528 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001529
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001530 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001531 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001532
1533 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001534}
1535
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001536/**
1537 * smack_inode_getsecid - Extract inode's security id
1538 * @inode: inode to extract the info from
1539 * @secid: where result will be saved
1540 */
1541static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1542{
1543 struct inode_smack *isp = inode->i_security;
1544
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001545 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001546}
1547
Casey Schauflere114e472008-02-04 22:29:50 -08001548/*
1549 * File Hooks
1550 */
1551
1552/**
1553 * smack_file_permission - Smack check on file operations
1554 * @file: unused
1555 * @mask: unused
1556 *
1557 * Returns 0
1558 *
1559 * Should access checks be done on each read or write?
1560 * UNICOS and SELinux say yes.
1561 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1562 *
1563 * I'll say no for now. Smack does not do the frequent
1564 * label changing that SELinux does.
1565 */
1566static int smack_file_permission(struct file *file, int mask)
1567{
1568 return 0;
1569}
1570
1571/**
1572 * smack_file_alloc_security - assign a file security blob
1573 * @file: the object
1574 *
1575 * The security blob for a file is a pointer to the master
1576 * label list, so no allocation is done.
1577 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001578 * f_security is the owner security information. It
1579 * isn't used on file access checks, it's for send_sigio.
1580 *
Casey Schauflere114e472008-02-04 22:29:50 -08001581 * Returns 0
1582 */
1583static int smack_file_alloc_security(struct file *file)
1584{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001585 struct smack_known *skp = smk_of_current();
1586
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001587 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001588 return 0;
1589}
1590
1591/**
1592 * smack_file_free_security - clear a file security blob
1593 * @file: the object
1594 *
1595 * The security blob for a file is a pointer to the master
1596 * label list, so no memory is freed.
1597 */
1598static void smack_file_free_security(struct file *file)
1599{
1600 file->f_security = NULL;
1601}
1602
1603/**
1604 * smack_file_ioctl - Smack check on ioctls
1605 * @file: the object
1606 * @cmd: what to do
1607 * @arg: unused
1608 *
1609 * Relies heavily on the correct use of the ioctl command conventions.
1610 *
1611 * Returns 0 if allowed, error code otherwise
1612 */
1613static int smack_file_ioctl(struct file *file, unsigned int cmd,
1614 unsigned long arg)
1615{
1616 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001617 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001618 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001619
Eric Parisf48b7392011-04-25 12:54:27 -04001620 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001621 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001622
Casey Schauflerd166c802014-08-27 14:51:27 -07001623 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001624 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001625 rc = smk_bu_file(file, MAY_WRITE, rc);
1626 }
Casey Schauflere114e472008-02-04 22:29:50 -08001627
Casey Schauflerd166c802014-08-27 14:51:27 -07001628 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001629 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001630 rc = smk_bu_file(file, MAY_READ, rc);
1631 }
Casey Schauflere114e472008-02-04 22:29:50 -08001632
1633 return rc;
1634}
1635
1636/**
1637 * smack_file_lock - Smack check on file locking
1638 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001639 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001640 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001641 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001642 */
1643static int smack_file_lock(struct file *file, unsigned int cmd)
1644{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001645 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001646 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001647 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001648
Eric Paris92f42502011-04-25 13:15:55 -04001649 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1650 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001651 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001652 rc = smk_bu_file(file, MAY_LOCK, rc);
1653 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001654}
1655
1656/**
1657 * smack_file_fcntl - Smack check on fcntl
1658 * @file: the object
1659 * @cmd: what action to check
1660 * @arg: unused
1661 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001662 * Generally these operations are harmless.
1663 * File locking operations present an obvious mechanism
1664 * for passing information, so they require write access.
1665 *
Casey Schauflere114e472008-02-04 22:29:50 -08001666 * Returns 0 if current has access, error code otherwise
1667 */
1668static int smack_file_fcntl(struct file *file, unsigned int cmd,
1669 unsigned long arg)
1670{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001671 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001672 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001673 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001674
Casey Schauflere114e472008-02-04 22:29:50 -08001675 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001676 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001677 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001678 case F_SETLK:
1679 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001680 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1681 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001682 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001683 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001684 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001685 case F_SETOWN:
1686 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001687 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1688 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001689 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001690 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001691 break;
1692 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001693 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001694 }
1695
1696 return rc;
1697}
1698
1699/**
Al Viroe5467852012-05-30 13:30:51 -04001700 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001701 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1702 * if mapping anonymous memory.
1703 * @file contains the file structure for file to map (may be NULL).
1704 * @reqprot contains the protection requested by the application.
1705 * @prot contains the protection that will be applied by the kernel.
1706 * @flags contains the operational flags.
1707 * Return 0 if permission is granted.
1708 */
Al Viroe5467852012-05-30 13:30:51 -04001709static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001710 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001711 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001712{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001713 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001714 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001715 struct smack_rule *srp;
1716 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001717 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001718 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001719 int may;
1720 int mmay;
1721 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001722 int rc;
1723
Al Viro496ad9a2013-01-23 17:07:38 -05001724 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001725 return 0;
1726
Al Viro496ad9a2013-01-23 17:07:38 -05001727 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001728 if (isp->smk_mmap == NULL)
1729 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001730 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001731
1732 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001733 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001734 rc = 0;
1735
1736 rcu_read_lock();
1737 /*
1738 * For each Smack rule associated with the subject
1739 * label verify that the SMACK64MMAP also has access
1740 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001741 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001742 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001743 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001744 /*
1745 * Matching labels always allows access.
1746 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001747 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001748 continue;
1749 /*
1750 * If there is a matching local rule take
1751 * that into account as well.
1752 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001753 may = smk_access_entry(srp->smk_subject->smk_known,
1754 okp->smk_known,
1755 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001756 if (may == -ENOENT)
1757 may = srp->smk_access;
1758 else
1759 may &= srp->smk_access;
1760 /*
1761 * If may is zero the SMACK64MMAP subject can't
1762 * possibly have less access.
1763 */
1764 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001765 continue;
1766
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001767 /*
1768 * Fetch the global list entry.
1769 * If there isn't one a SMACK64MMAP subject
1770 * can't have as much access as current.
1771 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001772 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1773 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001774 if (mmay == -ENOENT) {
1775 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001776 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001777 }
1778 /*
1779 * If there is a local entry it modifies the
1780 * potential access, too.
1781 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001782 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1783 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001784 if (tmay != -ENOENT)
1785 mmay &= tmay;
1786
1787 /*
1788 * If there is any access available to current that is
1789 * not available to a SMACK64MMAP subject
1790 * deny access.
1791 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001792 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001793 rc = -EACCES;
1794 break;
1795 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001796 }
1797
1798 rcu_read_unlock();
1799
1800 return rc;
1801}
1802
1803/**
Casey Schauflere114e472008-02-04 22:29:50 -08001804 * smack_file_set_fowner - set the file security blob value
1805 * @file: object in question
1806 *
Casey Schauflere114e472008-02-04 22:29:50 -08001807 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001808static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001809{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001810 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001811}
1812
1813/**
1814 * smack_file_send_sigiotask - Smack on sigio
1815 * @tsk: The target task
1816 * @fown: the object the signal come from
1817 * @signum: unused
1818 *
1819 * Allow a privileged task to get signals even if it shouldn't
1820 *
1821 * Returns 0 if a subject with the object's smack could
1822 * write to the task, an error code otherwise.
1823 */
1824static int smack_file_send_sigiotask(struct task_struct *tsk,
1825 struct fown_struct *fown, int signum)
1826{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001827 struct smack_known *skp;
1828 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001829 struct file *file;
1830 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001831 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001832
1833 /*
1834 * struct fown_struct is never outside the context of a struct file
1835 */
1836 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001837
Etienne Bassetecfcc532009-04-08 20:40:06 +02001838 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001839 skp = file->f_security;
1840 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1841 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001842 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001843 rc = 0;
1844
1845 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1846 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001847 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001848 return rc;
1849}
1850
1851/**
1852 * smack_file_receive - Smack file receive check
1853 * @file: the object
1854 *
1855 * Returns 0 if current has access, error code otherwise
1856 */
1857static int smack_file_receive(struct file *file)
1858{
Casey Schauflerd166c802014-08-27 14:51:27 -07001859 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001860 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001861 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001862 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001863 struct socket *sock;
1864 struct task_smack *tsp;
1865 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001866
Seung-Woo Kim97775822015-04-17 15:25:04 +09001867 if (unlikely(IS_PRIVATE(inode)))
1868 return 0;
1869
Casey Schaufler4482a442013-12-30 17:37:45 -08001870 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001871 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001872
1873 if (S_ISSOCK(inode->i_mode)) {
1874 sock = SOCKET_I(inode);
1875 ssp = sock->sk->sk_security;
1876 tsp = current_security();
1877 /*
1878 * If the receiving process can't write to the
1879 * passed socket or if the passed socket can't
1880 * write to the receiving process don't accept
1881 * the passed socket.
1882 */
1883 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1884 rc = smk_bu_file(file, may, rc);
1885 if (rc < 0)
1886 return rc;
1887 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1888 rc = smk_bu_file(file, may, rc);
1889 return rc;
1890 }
Casey Schauflere114e472008-02-04 22:29:50 -08001891 /*
1892 * This code relies on bitmasks.
1893 */
1894 if (file->f_mode & FMODE_READ)
1895 may = MAY_READ;
1896 if (file->f_mode & FMODE_WRITE)
1897 may |= MAY_WRITE;
1898
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001899 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001900 rc = smk_bu_file(file, may, rc);
1901 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001902}
1903
Casey Schaufler531f1d42011-09-19 12:41:42 -07001904/**
Eric Paris83d49852012-04-04 13:45:40 -04001905 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001906 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001907 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001908 *
1909 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001910 * Allow the open only if the task has read access. There are
1911 * many read operations (e.g. fstat) that you can do with an
1912 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001913 *
1914 * Returns 0
1915 */
Eric Paris83d49852012-04-04 13:45:40 -04001916static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001917{
Casey Schauflera6834c02014-04-21 11:10:26 -07001918 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001919 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001920 struct smk_audit_info ad;
1921 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001922
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001923 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001924 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001925
Casey Schauflera6834c02014-04-21 11:10:26 -07001926 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1927 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001928 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001929 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001930
1931 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001932}
1933
Casey Schauflere114e472008-02-04 22:29:50 -08001934/*
1935 * Task hooks
1936 */
1937
1938/**
David Howellsee18d642009-09-02 09:14:21 +01001939 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1940 * @new: the new credentials
1941 * @gfp: the atomicity of any memory allocations
1942 *
1943 * Prepare a blank set of credentials for modification. This must allocate all
1944 * the memory the LSM module might require such that cred_transfer() can
1945 * complete without error.
1946 */
1947static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1948{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001949 struct task_smack *tsp;
1950
1951 tsp = new_task_smack(NULL, NULL, gfp);
1952 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001953 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001954
1955 cred->security = tsp;
1956
David Howellsee18d642009-09-02 09:14:21 +01001957 return 0;
1958}
1959
1960
1961/**
David Howellsf1752ee2008-11-14 10:39:17 +11001962 * smack_cred_free - "free" task-level security credentials
1963 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001964 *
Casey Schauflere114e472008-02-04 22:29:50 -08001965 */
David Howellsf1752ee2008-11-14 10:39:17 +11001966static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001967{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001968 struct task_smack *tsp = cred->security;
1969 struct smack_rule *rp;
1970 struct list_head *l;
1971 struct list_head *n;
1972
1973 if (tsp == NULL)
1974 return;
1975 cred->security = NULL;
1976
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001977 smk_destroy_label_list(&tsp->smk_relabel);
1978
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001979 list_for_each_safe(l, n, &tsp->smk_rules) {
1980 rp = list_entry(l, struct smack_rule, list);
1981 list_del(&rp->list);
1982 kfree(rp);
1983 }
1984 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001985}
1986
1987/**
David Howellsd84f4f92008-11-14 10:39:23 +11001988 * smack_cred_prepare - prepare new set of credentials for modification
1989 * @new: the new credentials
1990 * @old: the original credentials
1991 * @gfp: the atomicity of any memory allocations
1992 *
1993 * Prepare a new set of credentials for modification.
1994 */
1995static int smack_cred_prepare(struct cred *new, const struct cred *old,
1996 gfp_t gfp)
1997{
Casey Schaufler676dac42010-12-02 06:43:39 -08001998 struct task_smack *old_tsp = old->security;
1999 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002000 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08002001
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002002 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08002003 if (new_tsp == NULL)
2004 return -ENOMEM;
2005
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002006 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
2007 if (rc != 0)
2008 return rc;
2009
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02002010 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
2011 gfp);
2012 if (rc != 0)
2013 return rc;
2014
Casey Schaufler676dac42010-12-02 06:43:39 -08002015 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11002016 return 0;
2017}
2018
Randy Dunlap251a2a92009-02-18 11:42:33 -08002019/**
David Howellsee18d642009-09-02 09:14:21 +01002020 * smack_cred_transfer - Transfer the old credentials to the new credentials
2021 * @new: the new credentials
2022 * @old: the original credentials
2023 *
2024 * Fill in a set of blank credentials from another set of credentials.
2025 */
2026static void smack_cred_transfer(struct cred *new, const struct cred *old)
2027{
Casey Schaufler676dac42010-12-02 06:43:39 -08002028 struct task_smack *old_tsp = old->security;
2029 struct task_smack *new_tsp = new->security;
2030
2031 new_tsp->smk_task = old_tsp->smk_task;
2032 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08002033 mutex_init(&new_tsp->smk_rules_lock);
2034 INIT_LIST_HEAD(&new_tsp->smk_rules);
2035
2036
2037 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01002038}
2039
2040/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002041 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002042 * @new: points to the set of credentials to be modified.
2043 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002044 *
2045 * Set the security data for a kernel service.
2046 */
2047static int smack_kernel_act_as(struct cred *new, u32 secid)
2048{
Casey Schaufler676dac42010-12-02 06:43:39 -08002049 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002050 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002051
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002052 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11002053 return -EINVAL;
2054
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002055 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11002056 return 0;
2057}
2058
2059/**
2060 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002061 * @new: points to the set of credentials to be modified
2062 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002063 *
2064 * Set the file creation context in a set of credentials to the same
2065 * as the objective context of the specified inode
2066 */
2067static int smack_kernel_create_files_as(struct cred *new,
2068 struct inode *inode)
2069{
2070 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002071 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002072
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002073 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002074 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002075 return 0;
2076}
2077
2078/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002079 * smk_curacc_on_task - helper to log task related access
2080 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002081 * @access: the access requested
2082 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002083 *
2084 * Return 0 if access is permitted
2085 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002086static int smk_curacc_on_task(struct task_struct *p, int access,
2087 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002088{
2089 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002090 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002091 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002092
Casey Schaufler531f1d42011-09-19 12:41:42 -07002093 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002094 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002095 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002096 rc = smk_bu_task(p, access, rc);
2097 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002098}
2099
2100/**
Casey Schauflere114e472008-02-04 22:29:50 -08002101 * smack_task_setpgid - Smack check on setting pgid
2102 * @p: the task object
2103 * @pgid: unused
2104 *
2105 * Return 0 if write access is permitted
2106 */
2107static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2108{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002109 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002110}
2111
2112/**
2113 * smack_task_getpgid - Smack access check for getpgid
2114 * @p: the object task
2115 *
2116 * Returns 0 if current can read the object task, error code otherwise
2117 */
2118static int smack_task_getpgid(struct task_struct *p)
2119{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002120 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002121}
2122
2123/**
2124 * smack_task_getsid - Smack access check for getsid
2125 * @p: the object task
2126 *
2127 * Returns 0 if current can read the object task, error code otherwise
2128 */
2129static int smack_task_getsid(struct task_struct *p)
2130{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002131 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002132}
2133
2134/**
2135 * smack_task_getsecid - get the secid of the task
2136 * @p: the object task
2137 * @secid: where to put the result
2138 *
2139 * Sets the secid to contain a u32 version of the smack label.
2140 */
2141static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2142{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002143 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002144
2145 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002146}
2147
2148/**
2149 * smack_task_setnice - Smack check on setting nice
2150 * @p: the task object
2151 * @nice: unused
2152 *
2153 * Return 0 if write access is permitted
2154 */
2155static int smack_task_setnice(struct task_struct *p, int nice)
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_setioprio - Smack check on setting ioprio
2162 * @p: the task object
2163 * @ioprio: unused
2164 *
2165 * Return 0 if write access is permitted
2166 */
2167static int smack_task_setioprio(struct task_struct *p, int ioprio)
2168{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002169 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002170}
2171
2172/**
2173 * smack_task_getioprio - Smack check on reading ioprio
2174 * @p: the task object
2175 *
2176 * Return 0 if read access is permitted
2177 */
2178static int smack_task_getioprio(struct task_struct *p)
2179{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002180 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002181}
2182
2183/**
2184 * smack_task_setscheduler - Smack check on setting scheduler
2185 * @p: the task object
2186 * @policy: unused
2187 * @lp: unused
2188 *
2189 * Return 0 if read access is permitted
2190 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002191static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002192{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002193 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002194}
2195
2196/**
2197 * smack_task_getscheduler - Smack check on reading scheduler
2198 * @p: the task object
2199 *
2200 * Return 0 if read access is permitted
2201 */
2202static int smack_task_getscheduler(struct task_struct *p)
2203{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002204 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002205}
2206
2207/**
2208 * smack_task_movememory - Smack check on moving memory
2209 * @p: the task object
2210 *
2211 * Return 0 if write access is permitted
2212 */
2213static int smack_task_movememory(struct task_struct *p)
2214{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002215 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002216}
2217
2218/**
2219 * smack_task_kill - Smack check on signal delivery
2220 * @p: the task object
2221 * @info: unused
2222 * @sig: unused
2223 * @secid: identifies the smack to use in lieu of current's
2224 *
2225 * Return 0 if write access is permitted
2226 *
2227 * The secid behavior is an artifact of an SELinux hack
2228 * in the USB code. Someday it may go away.
2229 */
2230static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2231 int sig, u32 secid)
2232{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002233 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002234 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002235 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002236 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002237
2238 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2239 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002240 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002241 * Sending a signal requires that the sender
2242 * can write the receiver.
2243 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002244 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002245 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002246 rc = smk_bu_task(p, MAY_WRITE, rc);
2247 return rc;
2248 }
Casey Schauflere114e472008-02-04 22:29:50 -08002249 /*
2250 * If the secid isn't 0 we're dealing with some USB IO
2251 * specific behavior. This is not clean. For one thing
2252 * we can't take privilege into account.
2253 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002254 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002255 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2256 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002257 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002258}
2259
2260/**
2261 * smack_task_wait - Smack access check for waiting
2262 * @p: task to wait for
2263 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002264 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002265 */
2266static int smack_task_wait(struct task_struct *p)
2267{
Casey Schauflere114e472008-02-04 22:29:50 -08002268 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002269 * Allow the operation to succeed.
2270 * Zombies are bad.
2271 * In userless environments (e.g. phones) programs
2272 * get marked with SMACK64EXEC and even if the parent
2273 * and child shouldn't be talking the parent still
2274 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002275 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002276 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002277}
2278
2279/**
2280 * smack_task_to_inode - copy task smack into the inode blob
2281 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002282 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002283 *
2284 * Sets the smack pointer in the inode security blob
2285 */
2286static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2287{
2288 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002289 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002290
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002291 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002292}
2293
2294/*
2295 * Socket hooks.
2296 */
2297
2298/**
2299 * smack_sk_alloc_security - Allocate a socket blob
2300 * @sk: the socket
2301 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002302 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002303 *
2304 * Assign Smack pointers to current
2305 *
2306 * Returns 0 on success, -ENOMEM is there's no memory
2307 */
2308static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2309{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002310 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002311 struct socket_smack *ssp;
2312
2313 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2314 if (ssp == NULL)
2315 return -ENOMEM;
2316
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002317 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002318 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002319 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002320
2321 sk->sk_security = ssp;
2322
2323 return 0;
2324}
2325
2326/**
2327 * smack_sk_free_security - Free a socket blob
2328 * @sk: the socket
2329 *
2330 * Clears the blob pointer
2331 */
2332static void smack_sk_free_security(struct sock *sk)
2333{
2334 kfree(sk->sk_security);
2335}
2336
2337/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002338* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002339* @sip: the object end
2340*
2341* looks for host based access restrictions
2342*
2343* This version will only be appropriate for really small sets of single label
2344* hosts. The caller is responsible for ensuring that the RCU read lock is
2345* taken before calling this function.
2346*
2347* Returns the label of the far end or NULL if it's not special.
2348*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002349static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002350{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002351 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002352 struct in_addr *siap = &sip->sin_addr;
2353
2354 if (siap->s_addr == 0)
2355 return NULL;
2356
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002357 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2358 /*
2359 * we break after finding the first match because
2360 * the list is sorted from longest to shortest mask
2361 * so we have found the most specific match
2362 */
2363 if (snp->smk_host.s_addr ==
2364 (siap->s_addr & snp->smk_mask.s_addr))
2365 return snp->smk_label;
2366
2367 return NULL;
2368}
2369
2370#if IS_ENABLED(CONFIG_IPV6)
2371/*
2372 * smk_ipv6_localhost - Check for local ipv6 host address
2373 * @sip: the address
2374 *
2375 * Returns boolean true if this is the localhost address
2376 */
2377static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2378{
2379 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2380 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2381
2382 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2383 ntohs(be16p[7]) == 1)
2384 return true;
2385 return false;
2386}
2387
2388/**
2389* smack_ipv6host_label - check host based restrictions
2390* @sip: the object end
2391*
2392* looks for host based access restrictions
2393*
2394* This version will only be appropriate for really small sets of single label
2395* hosts. The caller is responsible for ensuring that the RCU read lock is
2396* taken before calling this function.
2397*
2398* Returns the label of the far end or NULL if it's not special.
2399*/
2400static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2401{
2402 struct smk_net6addr *snp;
2403 struct in6_addr *sap = &sip->sin6_addr;
2404 int i;
2405 int found = 0;
2406
2407 /*
2408 * It's local. Don't look for a host label.
2409 */
2410 if (smk_ipv6_localhost(sip))
2411 return NULL;
2412
2413 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002414 /*
2415 * we break after finding the first match because
2416 * the list is sorted from longest to shortest mask
2417 * so we have found the most specific match
2418 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002419 for (found = 1, i = 0; i < 8; i++) {
2420 /*
2421 * If the label is NULL the entry has
2422 * been renounced. Ignore it.
2423 */
2424 if (snp->smk_label == NULL)
2425 continue;
2426 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2427 snp->smk_host.s6_addr16[i]) {
2428 found = 0;
2429 break;
2430 }
Etienne Basset43031542009-03-27 17:11:01 -04002431 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002432 if (found)
2433 return snp->smk_label;
2434 }
Paul Moore07feee82009-03-27 17:10:54 -04002435
2436 return NULL;
2437}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002438#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002439
2440/**
Casey Schauflere114e472008-02-04 22:29:50 -08002441 * smack_netlabel - Set the secattr on a socket
2442 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002443 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002444 *
2445 * Convert the outbound smack value (smk_out) to a
2446 * secattr and attach it to the socket.
2447 *
2448 * Returns 0 on success or an error code
2449 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002450static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002451{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002452 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002453 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002454 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002455
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002456 /*
2457 * Usually the netlabel code will handle changing the
2458 * packet labeling based on the label.
2459 * The case of a single label host is different, because
2460 * a single label host should never get a labeled packet
2461 * even though the label is usually associated with a packet
2462 * label.
2463 */
2464 local_bh_disable();
2465 bh_lock_sock_nested(sk);
2466
2467 if (ssp->smk_out == smack_net_ambient ||
2468 labeled == SMACK_UNLABELED_SOCKET)
2469 netlbl_sock_delattr(sk);
2470 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002471 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002472 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002473 }
2474
2475 bh_unlock_sock(sk);
2476 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002477
Casey Schauflere114e472008-02-04 22:29:50 -08002478 return rc;
2479}
2480
2481/**
Paul Moore07feee82009-03-27 17:10:54 -04002482 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2483 * @sk: the socket
2484 * @sap: the destination address
2485 *
2486 * Set the correct secattr for the given socket based on the destination
2487 * address and perform any outbound access checks needed.
2488 *
2489 * Returns 0 on success or an error code.
2490 *
2491 */
2492static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2493{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002494 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002495 int rc;
2496 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002497 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002498 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002499 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002500
2501 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002502 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002503 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002504#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002505 struct lsm_network_audit net;
2506
Eric Paris48c62af2012-04-02 13:15:44 -04002507 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2508 ad.a.u.net->family = sap->sin_family;
2509 ad.a.u.net->dport = sap->sin_port;
2510 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002511#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002512 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002513 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002514 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2515 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002516 } else {
2517 sk_lbl = SMACK_CIPSO_SOCKET;
2518 rc = 0;
2519 }
2520 rcu_read_unlock();
2521 if (rc != 0)
2522 return rc;
2523
2524 return smack_netlabel(sk, sk_lbl);
2525}
2526
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002527#if IS_ENABLED(CONFIG_IPV6)
2528/**
2529 * smk_ipv6_check - check Smack access
2530 * @subject: subject Smack label
2531 * @object: object Smack label
2532 * @address: address
2533 * @act: the action being taken
2534 *
2535 * Check an IPv6 access
2536 */
2537static int smk_ipv6_check(struct smack_known *subject,
2538 struct smack_known *object,
2539 struct sockaddr_in6 *address, int act)
2540{
2541#ifdef CONFIG_AUDIT
2542 struct lsm_network_audit net;
2543#endif
2544 struct smk_audit_info ad;
2545 int rc;
2546
2547#ifdef CONFIG_AUDIT
2548 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2549 ad.a.u.net->family = PF_INET6;
2550 ad.a.u.net->dport = ntohs(address->sin6_port);
2551 if (act == SMK_RECEIVING)
2552 ad.a.u.net->v6info.saddr = address->sin6_addr;
2553 else
2554 ad.a.u.net->v6info.daddr = address->sin6_addr;
2555#endif
2556 rc = smk_access(subject, object, MAY_WRITE, &ad);
2557 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2558 return rc;
2559}
2560#endif /* CONFIG_IPV6 */
2561
2562#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002563/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002564 * smk_ipv6_port_label - Smack port access table management
2565 * @sock: socket
2566 * @address: address
2567 *
2568 * Create or update the port list entry
2569 */
2570static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2571{
2572 struct sock *sk = sock->sk;
2573 struct sockaddr_in6 *addr6;
2574 struct socket_smack *ssp = sock->sk->sk_security;
2575 struct smk_port_label *spp;
2576 unsigned short port = 0;
2577
2578 if (address == NULL) {
2579 /*
2580 * This operation is changing the Smack information
2581 * on the bound socket. Take the changes to the port
2582 * as well.
2583 */
2584 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2585 if (sk != spp->smk_sock)
2586 continue;
2587 spp->smk_in = ssp->smk_in;
2588 spp->smk_out = ssp->smk_out;
2589 return;
2590 }
2591 /*
2592 * A NULL address is only used for updating existing
2593 * bound entries. If there isn't one, it's OK.
2594 */
2595 return;
2596 }
2597
2598 addr6 = (struct sockaddr_in6 *)address;
2599 port = ntohs(addr6->sin6_port);
2600 /*
2601 * This is a special case that is safely ignored.
2602 */
2603 if (port == 0)
2604 return;
2605
2606 /*
2607 * Look for an existing port list entry.
2608 * This is an indication that a port is getting reused.
2609 */
2610 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2611 if (spp->smk_port != port)
2612 continue;
2613 spp->smk_port = port;
2614 spp->smk_sock = sk;
2615 spp->smk_in = ssp->smk_in;
2616 spp->smk_out = ssp->smk_out;
2617 return;
2618 }
2619
2620 /*
2621 * A new port entry is required.
2622 */
2623 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2624 if (spp == NULL)
2625 return;
2626
2627 spp->smk_port = port;
2628 spp->smk_sock = sk;
2629 spp->smk_in = ssp->smk_in;
2630 spp->smk_out = ssp->smk_out;
2631
2632 list_add(&spp->list, &smk_ipv6_port_list);
2633 return;
2634}
2635
2636/**
2637 * smk_ipv6_port_check - check Smack port access
2638 * @sock: socket
2639 * @address: address
2640 *
2641 * Create or update the port list entry
2642 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002643static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002644 int act)
2645{
Casey Schauflerc6739442013-05-22 18:42:56 -07002646 struct smk_port_label *spp;
2647 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002648 struct smack_known *skp = NULL;
2649 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002650 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002651
2652 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002653 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002654 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002655 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002656 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002657 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002658 }
2659
2660 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002661 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002662 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002663 if (skp != NULL && object != NULL)
2664 return smk_ipv6_check(skp, object, address, act);
2665 if (skp == NULL)
2666 skp = smack_net_ambient;
2667 if (object == NULL)
2668 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002669
2670 /*
2671 * It's remote, so port lookup does no good.
2672 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002673 if (!smk_ipv6_localhost(address))
2674 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002675
2676 /*
2677 * It's local so the send check has to have passed.
2678 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002679 if (act == SMK_RECEIVING)
2680 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002681
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002682 port = ntohs(address->sin6_port);
Casey Schauflerc6739442013-05-22 18:42:56 -07002683 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2684 if (spp->smk_port != port)
2685 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002686 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002687 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002688 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002689 break;
2690 }
2691
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002692 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002693}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002694#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002695
2696/**
Casey Schauflere114e472008-02-04 22:29:50 -08002697 * smack_inode_setsecurity - set smack xattrs
2698 * @inode: the object
2699 * @name: attribute name
2700 * @value: attribute value
2701 * @size: size of the attribute
2702 * @flags: unused
2703 *
2704 * Sets the named attribute in the appropriate blob
2705 *
2706 * Returns 0 on success, or an error code
2707 */
2708static int smack_inode_setsecurity(struct inode *inode, const char *name,
2709 const void *value, size_t size, int flags)
2710{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002711 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002712 struct inode_smack *nsp = inode->i_security;
2713 struct socket_smack *ssp;
2714 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002715 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002716
Casey Schauflerf7112e62012-05-06 15:22:02 -07002717 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302718 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002719
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002720 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002721 if (IS_ERR(skp))
2722 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002723
2724 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002725 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002726 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002727 return 0;
2728 }
2729 /*
2730 * The rest of the Smack xattrs are only on sockets.
2731 */
2732 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2733 return -EOPNOTSUPP;
2734
2735 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002736 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002737 return -EOPNOTSUPP;
2738
2739 ssp = sock->sk->sk_security;
2740
2741 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002742 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002743 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002744 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002745 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002746 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2747 if (rc != 0)
2748 printk(KERN_WARNING
2749 "Smack: \"%s\" netlbl error %d.\n",
2750 __func__, -rc);
2751 }
Casey Schauflere114e472008-02-04 22:29:50 -08002752 } else
2753 return -EOPNOTSUPP;
2754
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002755#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002756 if (sock->sk->sk_family == PF_INET6)
2757 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002758#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002759
Casey Schauflere114e472008-02-04 22:29:50 -08002760 return 0;
2761}
2762
2763/**
2764 * smack_socket_post_create - finish socket setup
2765 * @sock: the socket
2766 * @family: protocol family
2767 * @type: unused
2768 * @protocol: unused
2769 * @kern: unused
2770 *
2771 * Sets the netlabel information on the socket
2772 *
2773 * Returns 0 on success, and error code otherwise
2774 */
2775static int smack_socket_post_create(struct socket *sock, int family,
2776 int type, int protocol, int kern)
2777{
Marcin Lis74123012015-01-22 15:40:33 +01002778 struct socket_smack *ssp;
2779
2780 if (sock->sk == NULL)
2781 return 0;
2782
2783 /*
2784 * Sockets created by kernel threads receive web label.
2785 */
2786 if (unlikely(current->flags & PF_KTHREAD)) {
2787 ssp = sock->sk->sk_security;
2788 ssp->smk_in = &smack_known_web;
2789 ssp->smk_out = &smack_known_web;
2790 }
2791
2792 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002793 return 0;
2794 /*
2795 * Set the outbound netlbl.
2796 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002797 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2798}
2799
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002800#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002801/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002802 * smack_socket_bind - record port binding information.
2803 * @sock: the socket
2804 * @address: the port address
2805 * @addrlen: size of the address
2806 *
2807 * Records the label bound to a port.
2808 *
2809 * Returns 0
2810 */
2811static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2812 int addrlen)
2813{
2814 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2815 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002816 return 0;
2817}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002818#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002819
2820/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002821 * smack_socket_connect - connect access check
2822 * @sock: the socket
2823 * @sap: the other end
2824 * @addrlen: size of sap
2825 *
2826 * Verifies that a connection may be possible
2827 *
2828 * Returns 0 on success, and error code otherwise
2829 */
2830static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2831 int addrlen)
2832{
Casey Schauflerc6739442013-05-22 18:42:56 -07002833 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002834#if IS_ENABLED(CONFIG_IPV6)
2835 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2836#endif
2837#ifdef SMACK_IPV6_SECMARK_LABELING
2838 struct smack_known *rsp;
2839 struct socket_smack *ssp = sock->sk->sk_security;
2840#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002841
Casey Schauflerc6739442013-05-22 18:42:56 -07002842 if (sock->sk == NULL)
2843 return 0;
2844
2845 switch (sock->sk->sk_family) {
2846 case PF_INET:
2847 if (addrlen < sizeof(struct sockaddr_in))
2848 return -EINVAL;
2849 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2850 break;
2851 case PF_INET6:
2852 if (addrlen < sizeof(struct sockaddr_in6))
2853 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002854#ifdef SMACK_IPV6_SECMARK_LABELING
2855 rsp = smack_ipv6host_label(sip);
2856 if (rsp != NULL)
2857 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002858 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002859#endif
2860#ifdef SMACK_IPV6_PORT_LABELING
2861 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2862#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002863 break;
2864 }
2865 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002866}
2867
2868/**
2869 * smack_flags_to_may - convert S_ to MAY_ values
2870 * @flags: the S_ value
2871 *
2872 * Returns the equivalent MAY_ value
2873 */
2874static int smack_flags_to_may(int flags)
2875{
2876 int may = 0;
2877
2878 if (flags & S_IRUGO)
2879 may |= MAY_READ;
2880 if (flags & S_IWUGO)
2881 may |= MAY_WRITE;
2882 if (flags & S_IXUGO)
2883 may |= MAY_EXEC;
2884
2885 return may;
2886}
2887
2888/**
2889 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2890 * @msg: the object
2891 *
2892 * Returns 0
2893 */
2894static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2895{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002896 struct smack_known *skp = smk_of_current();
2897
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002898 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002899 return 0;
2900}
2901
2902/**
2903 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2904 * @msg: the object
2905 *
2906 * Clears the blob pointer
2907 */
2908static void smack_msg_msg_free_security(struct msg_msg *msg)
2909{
2910 msg->security = NULL;
2911}
2912
2913/**
2914 * smack_of_shm - the smack pointer for the shm
2915 * @shp: the object
2916 *
2917 * Returns a pointer to the smack value
2918 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002919static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002920{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002921 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002922}
2923
2924/**
2925 * smack_shm_alloc_security - Set the security blob for shm
2926 * @shp: the object
2927 *
2928 * Returns 0
2929 */
2930static int smack_shm_alloc_security(struct shmid_kernel *shp)
2931{
2932 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002933 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002934
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002935 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002936 return 0;
2937}
2938
2939/**
2940 * smack_shm_free_security - Clear the security blob for shm
2941 * @shp: the object
2942 *
2943 * Clears the blob pointer
2944 */
2945static void smack_shm_free_security(struct shmid_kernel *shp)
2946{
2947 struct kern_ipc_perm *isp = &shp->shm_perm;
2948
2949 isp->security = NULL;
2950}
2951
2952/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002953 * smk_curacc_shm : check if current has access on shm
2954 * @shp : the object
2955 * @access : access requested
2956 *
2957 * Returns 0 if current has the requested access, error code otherwise
2958 */
2959static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2960{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002961 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002962 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002963 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002964
2965#ifdef CONFIG_AUDIT
2966 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2967 ad.a.u.ipc_id = shp->shm_perm.id;
2968#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002969 rc = smk_curacc(ssp, access, &ad);
2970 rc = smk_bu_current("shm", ssp, access, rc);
2971 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002972}
2973
2974/**
Casey Schauflere114e472008-02-04 22:29:50 -08002975 * smack_shm_associate - Smack access check for shm
2976 * @shp: the object
2977 * @shmflg: access requested
2978 *
2979 * Returns 0 if current has the requested access, error code otherwise
2980 */
2981static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2982{
Casey Schauflere114e472008-02-04 22:29:50 -08002983 int may;
2984
2985 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002986 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002987}
2988
2989/**
2990 * smack_shm_shmctl - Smack access check for shm
2991 * @shp: the object
2992 * @cmd: what it wants to do
2993 *
2994 * Returns 0 if current has the requested access, error code otherwise
2995 */
2996static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2997{
Casey Schauflere114e472008-02-04 22:29:50 -08002998 int may;
2999
3000 switch (cmd) {
3001 case IPC_STAT:
3002 case SHM_STAT:
3003 may = MAY_READ;
3004 break;
3005 case IPC_SET:
3006 case SHM_LOCK:
3007 case SHM_UNLOCK:
3008 case IPC_RMID:
3009 may = MAY_READWRITE;
3010 break;
3011 case IPC_INFO:
3012 case SHM_INFO:
3013 /*
3014 * System level information.
3015 */
3016 return 0;
3017 default:
3018 return -EINVAL;
3019 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02003020 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003021}
3022
3023/**
3024 * smack_shm_shmat - Smack access for shmat
3025 * @shp: the object
3026 * @shmaddr: unused
3027 * @shmflg: access requested
3028 *
3029 * Returns 0 if current has the requested access, error code otherwise
3030 */
3031static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
3032 int shmflg)
3033{
Casey Schauflere114e472008-02-04 22:29:50 -08003034 int may;
3035
3036 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003037 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003038}
3039
3040/**
3041 * smack_of_sem - the smack pointer for the sem
3042 * @sma: the object
3043 *
3044 * Returns a pointer to the smack value
3045 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003046static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08003047{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003048 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08003049}
3050
3051/**
3052 * smack_sem_alloc_security - Set the security blob for sem
3053 * @sma: the object
3054 *
3055 * Returns 0
3056 */
3057static int smack_sem_alloc_security(struct sem_array *sma)
3058{
3059 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003060 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003061
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003062 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003063 return 0;
3064}
3065
3066/**
3067 * smack_sem_free_security - Clear the security blob for sem
3068 * @sma: the object
3069 *
3070 * Clears the blob pointer
3071 */
3072static void smack_sem_free_security(struct sem_array *sma)
3073{
3074 struct kern_ipc_perm *isp = &sma->sem_perm;
3075
3076 isp->security = NULL;
3077}
3078
3079/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003080 * smk_curacc_sem : check if current has access on sem
3081 * @sma : the object
3082 * @access : access requested
3083 *
3084 * Returns 0 if current has the requested access, error code otherwise
3085 */
3086static int smk_curacc_sem(struct sem_array *sma, int access)
3087{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003088 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003089 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003090 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003091
3092#ifdef CONFIG_AUDIT
3093 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3094 ad.a.u.ipc_id = sma->sem_perm.id;
3095#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003096 rc = smk_curacc(ssp, access, &ad);
3097 rc = smk_bu_current("sem", ssp, access, rc);
3098 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003099}
3100
3101/**
Casey Schauflere114e472008-02-04 22:29:50 -08003102 * smack_sem_associate - Smack access check for sem
3103 * @sma: the object
3104 * @semflg: access requested
3105 *
3106 * Returns 0 if current has the requested access, error code otherwise
3107 */
3108static int smack_sem_associate(struct sem_array *sma, int semflg)
3109{
Casey Schauflere114e472008-02-04 22:29:50 -08003110 int may;
3111
3112 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003113 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003114}
3115
3116/**
3117 * smack_sem_shmctl - Smack access check for sem
3118 * @sma: the object
3119 * @cmd: what it wants to do
3120 *
3121 * Returns 0 if current has the requested access, error code otherwise
3122 */
3123static int smack_sem_semctl(struct sem_array *sma, int cmd)
3124{
Casey Schauflere114e472008-02-04 22:29:50 -08003125 int may;
3126
3127 switch (cmd) {
3128 case GETPID:
3129 case GETNCNT:
3130 case GETZCNT:
3131 case GETVAL:
3132 case GETALL:
3133 case IPC_STAT:
3134 case SEM_STAT:
3135 may = MAY_READ;
3136 break;
3137 case SETVAL:
3138 case SETALL:
3139 case IPC_RMID:
3140 case IPC_SET:
3141 may = MAY_READWRITE;
3142 break;
3143 case IPC_INFO:
3144 case SEM_INFO:
3145 /*
3146 * System level information
3147 */
3148 return 0;
3149 default:
3150 return -EINVAL;
3151 }
3152
Etienne Bassetecfcc532009-04-08 20:40:06 +02003153 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003154}
3155
3156/**
3157 * smack_sem_semop - Smack checks of semaphore operations
3158 * @sma: the object
3159 * @sops: unused
3160 * @nsops: unused
3161 * @alter: unused
3162 *
3163 * Treated as read and write in all cases.
3164 *
3165 * Returns 0 if access is allowed, error code otherwise
3166 */
3167static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
3168 unsigned nsops, int alter)
3169{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003170 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003171}
3172
3173/**
3174 * smack_msg_alloc_security - Set the security blob for msg
3175 * @msq: the object
3176 *
3177 * Returns 0
3178 */
3179static int smack_msg_queue_alloc_security(struct msg_queue *msq)
3180{
3181 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003182 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003183
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003184 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003185 return 0;
3186}
3187
3188/**
3189 * smack_msg_free_security - Clear the security blob for msg
3190 * @msq: the object
3191 *
3192 * Clears the blob pointer
3193 */
3194static void smack_msg_queue_free_security(struct msg_queue *msq)
3195{
3196 struct kern_ipc_perm *kisp = &msq->q_perm;
3197
3198 kisp->security = NULL;
3199}
3200
3201/**
3202 * smack_of_msq - the smack pointer for the msq
3203 * @msq: the object
3204 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003205 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08003206 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003207static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08003208{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003209 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08003210}
3211
3212/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003213 * smk_curacc_msq : helper to check if current has access on msq
3214 * @msq : the msq
3215 * @access : access requested
3216 *
3217 * return 0 if current has access, error otherwise
3218 */
3219static int smk_curacc_msq(struct msg_queue *msq, int access)
3220{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003221 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003222 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003223 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003224
3225#ifdef CONFIG_AUDIT
3226 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3227 ad.a.u.ipc_id = msq->q_perm.id;
3228#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003229 rc = smk_curacc(msp, access, &ad);
3230 rc = smk_bu_current("msq", msp, access, rc);
3231 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003232}
3233
3234/**
Casey Schauflere114e472008-02-04 22:29:50 -08003235 * smack_msg_queue_associate - Smack access check for msg_queue
3236 * @msq: the object
3237 * @msqflg: access requested
3238 *
3239 * Returns 0 if current has the requested access, error code otherwise
3240 */
3241static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
3242{
Casey Schauflere114e472008-02-04 22:29:50 -08003243 int may;
3244
3245 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003246 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003247}
3248
3249/**
3250 * smack_msg_queue_msgctl - Smack access check for msg_queue
3251 * @msq: the object
3252 * @cmd: what it wants to do
3253 *
3254 * Returns 0 if current has the requested access, error code otherwise
3255 */
3256static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
3257{
Casey Schauflere114e472008-02-04 22:29:50 -08003258 int may;
3259
3260 switch (cmd) {
3261 case IPC_STAT:
3262 case MSG_STAT:
3263 may = MAY_READ;
3264 break;
3265 case IPC_SET:
3266 case IPC_RMID:
3267 may = MAY_READWRITE;
3268 break;
3269 case IPC_INFO:
3270 case MSG_INFO:
3271 /*
3272 * System level information
3273 */
3274 return 0;
3275 default:
3276 return -EINVAL;
3277 }
3278
Etienne Bassetecfcc532009-04-08 20:40:06 +02003279 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003280}
3281
3282/**
3283 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3284 * @msq: the object
3285 * @msg: unused
3286 * @msqflg: access requested
3287 *
3288 * Returns 0 if current has the requested access, error code otherwise
3289 */
3290static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3291 int msqflg)
3292{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003293 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003294
Etienne Bassetecfcc532009-04-08 20:40:06 +02003295 may = smack_flags_to_may(msqflg);
3296 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003297}
3298
3299/**
3300 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3301 * @msq: the object
3302 * @msg: unused
3303 * @target: unused
3304 * @type: unused
3305 * @mode: unused
3306 *
3307 * Returns 0 if current has read and write access, error code otherwise
3308 */
3309static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3310 struct task_struct *target, long type, int mode)
3311{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003312 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003313}
3314
3315/**
3316 * smack_ipc_permission - Smack access for ipc_permission()
3317 * @ipp: the object permissions
3318 * @flag: access requested
3319 *
3320 * Returns 0 if current has read and write access, error code otherwise
3321 */
3322static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3323{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003324 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003325 int may = smack_flags_to_may(flag);
3326 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003327 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003328
Etienne Bassetecfcc532009-04-08 20:40:06 +02003329#ifdef CONFIG_AUDIT
3330 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3331 ad.a.u.ipc_id = ipp->id;
3332#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003333 rc = smk_curacc(iskp, may, &ad);
3334 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003335 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003336}
3337
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003338/**
3339 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003340 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003341 * @secid: where result will be saved
3342 */
3343static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3344{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003345 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003346
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003347 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003348}
3349
Casey Schauflere114e472008-02-04 22:29:50 -08003350/**
3351 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003352 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003353 * @inode: the object
3354 *
3355 * Set the inode's security blob if it hasn't been done already.
3356 */
3357static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3358{
3359 struct super_block *sbp;
3360 struct superblock_smack *sbsp;
3361 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003362 struct smack_known *skp;
3363 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003364 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003365 char trattr[TRANS_TRUE_SIZE];
3366 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003367 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003368 struct dentry *dp;
3369
3370 if (inode == NULL)
3371 return;
3372
3373 isp = inode->i_security;
3374
3375 mutex_lock(&isp->smk_lock);
3376 /*
3377 * If the inode is already instantiated
3378 * take the quick way out
3379 */
3380 if (isp->smk_flags & SMK_INODE_INSTANT)
3381 goto unlockandout;
3382
3383 sbp = inode->i_sb;
3384 sbsp = sbp->s_security;
3385 /*
3386 * We're going to use the superblock default label
3387 * if there's no label on the file.
3388 */
3389 final = sbsp->smk_default;
3390
3391 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003392 * If this is the root inode the superblock
3393 * may be in the process of initialization.
3394 * If that is the case use the root value out
3395 * of the superblock.
3396 */
3397 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003398 switch (sbp->s_magic) {
3399 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003400 /*
3401 * The cgroup filesystem is never mounted,
3402 * so there's no opportunity to set the mount
3403 * options.
3404 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003405 sbsp->smk_root = &smack_known_star;
3406 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003407 isp->smk_inode = sbsp->smk_root;
3408 break;
3409 case TMPFS_MAGIC:
3410 /*
3411 * What about shmem/tmpfs anonymous files with dentry
3412 * obtained from d_alloc_pseudo()?
3413 */
3414 isp->smk_inode = smk_of_current();
3415 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003416 case PIPEFS_MAGIC:
3417 isp->smk_inode = smk_of_current();
3418 break;
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003419 default:
3420 isp->smk_inode = sbsp->smk_root;
3421 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003422 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003423 isp->smk_flags |= SMK_INODE_INSTANT;
3424 goto unlockandout;
3425 }
3426
3427 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003428 * This is pretty hackish.
3429 * Casey says that we shouldn't have to do
3430 * file system specific code, but it does help
3431 * with keeping it simple.
3432 */
3433 switch (sbp->s_magic) {
3434 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003435 case PIPEFS_MAGIC:
3436 case SOCKFS_MAGIC:
3437 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003438 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003439 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003440 * that the smack file system doesn't do
3441 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003442 *
Casey Schauflere114e472008-02-04 22:29:50 -08003443 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003444 *
3445 * Socket access is controlled by the socket
3446 * structures associated with the task involved.
3447 *
3448 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003449 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003450 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003451 break;
3452 case DEVPTS_SUPER_MAGIC:
3453 /*
3454 * devpts seems content with the label of the task.
3455 * Programs that change smack have to treat the
3456 * pty with respect.
3457 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003458 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003459 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003460 case PROC_SUPER_MAGIC:
3461 /*
3462 * Casey says procfs appears not to care.
3463 * The superblock default suffices.
3464 */
3465 break;
3466 case TMPFS_MAGIC:
3467 /*
3468 * Device labels should come from the filesystem,
3469 * but watch out, because they're volitile,
3470 * getting recreated on every reboot.
3471 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003472 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003473 /*
3474 * No break.
3475 *
3476 * If a smack value has been set we want to use it,
3477 * but since tmpfs isn't giving us the opportunity
3478 * to set mount options simulate setting the
3479 * superblock default.
3480 */
3481 default:
3482 /*
3483 * This isn't an understood special case.
3484 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003485 */
3486
3487 /*
3488 * UNIX domain sockets use lower level socket data.
3489 */
3490 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003491 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003492 break;
3493 }
3494 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003495 * No xattr support means, alas, no SMACK label.
3496 * Use the aforeapplied default.
3497 * It would be curious if the label of the task
3498 * does not match that assigned.
3499 */
3500 if (inode->i_op->getxattr == NULL)
3501 break;
3502 /*
3503 * Get the dentry for xattr.
3504 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003505 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003506 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003507 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003508 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003509
3510 /*
3511 * Transmuting directory
3512 */
3513 if (S_ISDIR(inode->i_mode)) {
3514 /*
3515 * If this is a new directory and the label was
3516 * transmuted when the inode was initialized
3517 * set the transmute attribute on the directory
3518 * and mark the inode.
3519 *
3520 * If there is a transmute attribute on the
3521 * directory mark the inode.
3522 */
3523 if (isp->smk_flags & SMK_INODE_CHANGED) {
3524 isp->smk_flags &= ~SMK_INODE_CHANGED;
3525 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003526 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003527 TRANS_TRUE, TRANS_TRUE_SIZE,
3528 0);
3529 } else {
3530 rc = inode->i_op->getxattr(dp,
3531 XATTR_NAME_SMACKTRANSMUTE, trattr,
3532 TRANS_TRUE_SIZE);
3533 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3534 TRANS_TRUE_SIZE) != 0)
3535 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003536 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003537 if (rc >= 0)
3538 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003539 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003540 /*
3541 * Don't let the exec or mmap label be "*" or "@".
3542 */
3543 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003544 if (IS_ERR(skp) || skp == &smack_known_star ||
3545 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003546 skp = NULL;
3547 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003548
Casey Schaufler19760ad2013-12-16 16:27:26 -08003549 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003550 if (IS_ERR(skp) || skp == &smack_known_star ||
3551 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003552 skp = NULL;
3553 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003554
Casey Schauflere114e472008-02-04 22:29:50 -08003555 dput(dp);
3556 break;
3557 }
3558
3559 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003560 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003561 else
3562 isp->smk_inode = final;
3563
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003564 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003565
3566unlockandout:
3567 mutex_unlock(&isp->smk_lock);
3568 return;
3569}
3570
3571/**
3572 * smack_getprocattr - Smack process attribute access
3573 * @p: the object task
3574 * @name: the name of the attribute in /proc/.../attr
3575 * @value: where to put the result
3576 *
3577 * Places a copy of the task Smack into value
3578 *
3579 * Returns the length of the smack label or an error code
3580 */
3581static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3582{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003583 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003584 char *cp;
3585 int slen;
3586
3587 if (strcmp(name, "current") != 0)
3588 return -EINVAL;
3589
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003590 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003591 if (cp == NULL)
3592 return -ENOMEM;
3593
3594 slen = strlen(cp);
3595 *value = cp;
3596 return slen;
3597}
3598
3599/**
3600 * smack_setprocattr - Smack process attribute setting
3601 * @p: the object task
3602 * @name: the name of the attribute in /proc/.../attr
3603 * @value: the value to set
3604 * @size: the size of the value
3605 *
3606 * Sets the Smack value of the task. Only setting self
3607 * is permitted and only with privilege
3608 *
3609 * Returns the length of the smack label or an error code
3610 */
3611static int smack_setprocattr(struct task_struct *p, char *name,
3612 void *value, size_t size)
3613{
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003614 struct task_smack *tsp = current_security();
David Howellsd84f4f92008-11-14 10:39:23 +11003615 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003616 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003617 struct smack_known_list_elem *sklep;
3618 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003619
Casey Schauflere114e472008-02-04 22:29:50 -08003620 /*
3621 * Changing another process' Smack value is too dangerous
3622 * and supports no sane use case.
3623 */
3624 if (p != current)
3625 return -EPERM;
3626
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003627 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003628 return -EPERM;
3629
Casey Schauflerf7112e62012-05-06 15:22:02 -07003630 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003631 return -EINVAL;
3632
3633 if (strcmp(name, "current") != 0)
3634 return -EINVAL;
3635
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003636 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003637 if (IS_ERR(skp))
3638 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003639
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003640 /*
3641 * No process is ever allowed the web ("@") label.
3642 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003643 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003644 return -EPERM;
3645
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003646 if (!smack_privileged(CAP_MAC_ADMIN)) {
3647 rc = -EPERM;
3648 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3649 if (sklep->smk_label == skp) {
3650 rc = 0;
3651 break;
3652 }
3653 if (rc)
3654 return rc;
3655 }
3656
David Howellsd84f4f92008-11-14 10:39:23 +11003657 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003658 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003659 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003660
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003661 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003662 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003663 /*
3664 * process can change its label only once
3665 */
3666 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003667
David Howellsd84f4f92008-11-14 10:39:23 +11003668 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003669 return size;
3670}
3671
3672/**
3673 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003674 * @sock: one sock
3675 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003676 * @newsk: unused
3677 *
3678 * Return 0 if a subject with the smack of sock could access
3679 * an object with the smack of other, otherwise an error code
3680 */
David S. Miller3610cda2011-01-05 15:38:53 -08003681static int smack_unix_stream_connect(struct sock *sock,
3682 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003683{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003684 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003685 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003686 struct socket_smack *ssp = sock->sk_security;
3687 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003688 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003689 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003690 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003691#ifdef CONFIG_AUDIT
3692 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003693#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003694
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003695 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3696 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003697 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003698#ifdef CONFIG_AUDIT
3699 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3700 smk_ad_setfield_u_net_sk(&ad, other);
3701#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003702 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3703 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003704 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003705 okp = osp->smk_out;
3706 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003707 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003708 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003709 MAY_WRITE, rc);
3710 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003711 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003712
Casey Schaufler975d5e52011-09-26 14:43:39 -07003713 /*
3714 * Cross reference the peer labels for SO_PEERSEC.
3715 */
3716 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003717 nsp->smk_packet = ssp->smk_out;
3718 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003719 }
3720
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003721 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003722}
3723
3724/**
3725 * smack_unix_may_send - Smack access on UDS
3726 * @sock: one socket
3727 * @other: the other socket
3728 *
3729 * Return 0 if a subject with the smack of sock could access
3730 * an object with the smack of other, otherwise an error code
3731 */
3732static int smack_unix_may_send(struct socket *sock, struct socket *other)
3733{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003734 struct socket_smack *ssp = sock->sk->sk_security;
3735 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003736 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003737 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003738
Kees Cook923e9a12012-04-10 13:26:44 -07003739#ifdef CONFIG_AUDIT
3740 struct lsm_network_audit net;
3741
Eric Paris48c62af2012-04-02 13:15:44 -04003742 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003743 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003744#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003745
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003746 if (smack_privileged(CAP_MAC_OVERRIDE))
3747 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003748
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003749 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3750 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003751 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003752}
3753
3754/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003755 * smack_socket_sendmsg - Smack check based on destination host
3756 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003757 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003758 * @size: the size of the message
3759 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003760 * Return 0 if the current subject can write to the destination host.
3761 * For IPv4 this is only a question if the destination is a single label host.
3762 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003763 */
3764static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3765 int size)
3766{
3767 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003768#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003769 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003770#endif
3771#ifdef SMACK_IPV6_SECMARK_LABELING
3772 struct socket_smack *ssp = sock->sk->sk_security;
3773 struct smack_known *rsp;
3774#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003775 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003776
3777 /*
3778 * Perfectly reasonable for this to be NULL
3779 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003780 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003781 return 0;
3782
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003783 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003784 case AF_INET:
3785 rc = smack_netlabel_send(sock->sk, sip);
3786 break;
3787 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003788#ifdef SMACK_IPV6_SECMARK_LABELING
3789 rsp = smack_ipv6host_label(sap);
3790 if (rsp != NULL)
3791 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3792 SMK_CONNECTING);
3793#endif
3794#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003795 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003796#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003797 break;
3798 }
3799 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003800}
3801
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003802/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003803 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003804 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003805 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003806 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003807 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003808 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003809static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3810 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003811{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003812 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003813 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003814 int acat;
3815 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003816
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003817 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003818 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003819 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003820 * If there are flags but no level netlabel isn't
3821 * behaving the way we expect it to.
3822 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003823 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003824 * Without guidance regarding the smack value
3825 * for the packet fall back on the network
3826 * ambient value.
3827 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003828 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003829 list_for_each_entry(skp, &smack_known_list, list) {
3830 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003831 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003832 /*
3833 * Compare the catsets. Use the netlbl APIs.
3834 */
3835 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3836 if ((skp->smk_netlabel.flags &
3837 NETLBL_SECATTR_MLS_CAT) == 0)
3838 found = 1;
3839 break;
3840 }
3841 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003842 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3843 acat + 1);
3844 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003845 skp->smk_netlabel.attr.mls.cat,
3846 kcat + 1);
3847 if (acat < 0 || kcat < 0)
3848 break;
3849 }
3850 if (acat == kcat) {
3851 found = 1;
3852 break;
3853 }
Casey Schauflere114e472008-02-04 22:29:50 -08003854 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003855 rcu_read_unlock();
3856
3857 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003858 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003859
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003860 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003861 return &smack_known_web;
3862 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003863 }
3864 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3865 /*
3866 * Looks like a fallback, which gives us a secid.
3867 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003868 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003869 /*
3870 * This has got to be a bug because it is
3871 * impossible to specify a fallback without
3872 * specifying the label, which will ensure
3873 * it has a secid, and the only way to get a
3874 * secid is from a fallback.
3875 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003876 BUG_ON(skp == NULL);
3877 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003878 }
3879 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003880 * Without guidance regarding the smack value
3881 * for the packet fall back on the network
3882 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003883 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003884 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003885}
3886
Casey Schaufler69f287a2014-12-12 17:08:40 -08003887#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003888static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003889{
Casey Schauflerc6739442013-05-22 18:42:56 -07003890 u8 nexthdr;
3891 int offset;
3892 int proto = -EINVAL;
3893 struct ipv6hdr _ipv6h;
3894 struct ipv6hdr *ip6;
3895 __be16 frag_off;
3896 struct tcphdr _tcph, *th;
3897 struct udphdr _udph, *uh;
3898 struct dccp_hdr _dccph, *dh;
3899
3900 sip->sin6_port = 0;
3901
3902 offset = skb_network_offset(skb);
3903 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3904 if (ip6 == NULL)
3905 return -EINVAL;
3906 sip->sin6_addr = ip6->saddr;
3907
3908 nexthdr = ip6->nexthdr;
3909 offset += sizeof(_ipv6h);
3910 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3911 if (offset < 0)
3912 return -EINVAL;
3913
3914 proto = nexthdr;
3915 switch (proto) {
3916 case IPPROTO_TCP:
3917 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3918 if (th != NULL)
3919 sip->sin6_port = th->source;
3920 break;
3921 case IPPROTO_UDP:
3922 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3923 if (uh != NULL)
3924 sip->sin6_port = uh->source;
3925 break;
3926 case IPPROTO_DCCP:
3927 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3928 if (dh != NULL)
3929 sip->sin6_port = dh->dccph_sport;
3930 break;
3931 }
3932 return proto;
3933}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003934#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003935
Casey Schauflere114e472008-02-04 22:29:50 -08003936/**
3937 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3938 * @sk: socket
3939 * @skb: packet
3940 *
3941 * Returns 0 if the packet should be delivered, an error code otherwise
3942 */
3943static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3944{
3945 struct netlbl_lsm_secattr secattr;
3946 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003947 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003948 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003949 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003950#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003951 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003952#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003953#if IS_ENABLED(CONFIG_IPV6)
3954 struct sockaddr_in6 sadd;
3955 int proto;
3956#endif /* CONFIG_IPV6 */
3957
Casey Schauflerc6739442013-05-22 18:42:56 -07003958 switch (sk->sk_family) {
3959 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003960#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3961 /*
3962 * If there is a secmark use it rather than the CIPSO label.
3963 * If there is no secmark fall back to CIPSO.
3964 * The secmark is assumed to reflect policy better.
3965 */
3966 if (skb && skb->secmark != 0) {
3967 skp = smack_from_secid(skb->secmark);
3968 goto access_check;
3969 }
3970#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003971 /*
3972 * Translate what netlabel gave us.
3973 */
3974 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003975
Casey Schauflerc6739442013-05-22 18:42:56 -07003976 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3977 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003978 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003979 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003980 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003981
Casey Schauflerc6739442013-05-22 18:42:56 -07003982 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003983
Casey Schaufler69f287a2014-12-12 17:08:40 -08003984#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3985access_check:
3986#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003987#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003988 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3989 ad.a.u.net->family = sk->sk_family;
3990 ad.a.u.net->netif = skb->skb_iif;
3991 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003992#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003993 /*
3994 * Receiving a packet requires that the other end
3995 * be able to write here. Read access is not required.
3996 * This is the simplist possible security model
3997 * for networking.
3998 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003999 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4000 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07004001 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07004002 if (rc != 0)
4003 netlbl_skbuff_err(skb, rc, 0);
4004 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004005#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004006 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08004007 proto = smk_skb_to_addr_ipv6(skb, &sadd);
4008 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
4009 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004010#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004011 if (skb && skb->secmark != 0)
4012 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07004013 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004014 skp = smack_ipv6host_label(&sadd);
4015 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08004016 skp = smack_net_ambient;
4017#ifdef CONFIG_AUDIT
4018 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4019 ad.a.u.net->family = sk->sk_family;
4020 ad.a.u.net->netif = skb->skb_iif;
4021 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
4022#endif /* CONFIG_AUDIT */
4023 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4024 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
4025 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004026#endif /* SMACK_IPV6_SECMARK_LABELING */
4027#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004028 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004029#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07004030 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004031#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07004032 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004033
Paul Moorea8134292008-10-10 10:16:31 -04004034 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004035}
4036
4037/**
4038 * smack_socket_getpeersec_stream - pull in packet label
4039 * @sock: the socket
4040 * @optval: user's destination
4041 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08004042 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08004043 *
4044 * returns zero on success, an error code otherwise
4045 */
4046static int smack_socket_getpeersec_stream(struct socket *sock,
4047 char __user *optval,
4048 int __user *optlen, unsigned len)
4049{
4050 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004051 char *rcp = "";
4052 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08004053 int rc = 0;
4054
4055 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004056 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004057 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004058 slen = strlen(rcp) + 1;
4059 }
Casey Schauflere114e472008-02-04 22:29:50 -08004060
4061 if (slen > len)
4062 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004063 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08004064 rc = -EFAULT;
4065
4066 if (put_user(slen, optlen) != 0)
4067 rc = -EFAULT;
4068
4069 return rc;
4070}
4071
4072
4073/**
4074 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004075 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08004076 * @skb: packet data
4077 * @secid: pointer to where to put the secid of the packet
4078 *
4079 * Sets the netlabel socket state on sk from parent
4080 */
4081static int smack_socket_getpeersec_dgram(struct socket *sock,
4082 struct sk_buff *skb, u32 *secid)
4083
4084{
4085 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004086 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004087 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004088 int family = PF_UNSPEC;
4089 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004090 int rc;
4091
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004092 if (skb != NULL) {
4093 if (skb->protocol == htons(ETH_P_IP))
4094 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004095#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004096 else if (skb->protocol == htons(ETH_P_IPV6))
4097 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004098#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004099 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004100 if (family == PF_UNSPEC && sock != NULL)
4101 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004102
Casey Schaufler69f287a2014-12-12 17:08:40 -08004103 switch (family) {
4104 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004105 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004106 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004107 break;
4108 case PF_INET:
4109#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4110 s = skb->secmark;
4111 if (s != 0)
4112 break;
4113#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004114 /*
4115 * Translate what netlabel gave us.
4116 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004117 if (sock != NULL && sock->sk != NULL)
4118 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004119 netlbl_secattr_init(&secattr);
4120 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4121 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004122 skp = smack_from_secattr(&secattr, ssp);
4123 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004124 }
4125 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004126 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004127 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004128#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004129 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004130#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004131 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004132 }
4133 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004134 if (s == 0)
4135 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004136 return 0;
4137}
4138
4139/**
Paul Moore07feee82009-03-27 17:10:54 -04004140 * smack_sock_graft - Initialize a newly created socket with an existing sock
4141 * @sk: child sock
4142 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004143 *
Paul Moore07feee82009-03-27 17:10:54 -04004144 * Set the smk_{in,out} state of an existing sock based on the process that
4145 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004146 */
4147static void smack_sock_graft(struct sock *sk, struct socket *parent)
4148{
4149 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004150 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004151
Paul Moore07feee82009-03-27 17:10:54 -04004152 if (sk == NULL ||
4153 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004154 return;
4155
4156 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004157 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004158 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004159 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004160}
4161
4162/**
4163 * smack_inet_conn_request - Smack access check on connect
4164 * @sk: socket involved
4165 * @skb: packet
4166 * @req: unused
4167 *
4168 * Returns 0 if a task with the packet label could write to
4169 * the socket, otherwise an error code
4170 */
4171static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4172 struct request_sock *req)
4173{
Paul Moore07feee82009-03-27 17:10:54 -04004174 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004175 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004176 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004177 struct netlbl_lsm_secattr secattr;
4178 struct sockaddr_in addr;
4179 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004180 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004181 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004182 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004183#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004184 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004185#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004186
Casey Schaufler69f287a2014-12-12 17:08:40 -08004187#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004188 if (family == PF_INET6) {
4189 /*
4190 * Handle mapped IPv4 packets arriving
4191 * via IPv6 sockets. Don't set up netlabel
4192 * processing on IPv6.
4193 */
4194 if (skb->protocol == htons(ETH_P_IP))
4195 family = PF_INET;
4196 else
4197 return 0;
4198 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004199#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004200
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004201#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4202 /*
4203 * If there is a secmark use it rather than the CIPSO label.
4204 * If there is no secmark fall back to CIPSO.
4205 * The secmark is assumed to reflect policy better.
4206 */
4207 if (skb && skb->secmark != 0) {
4208 skp = smack_from_secid(skb->secmark);
4209 goto access_check;
4210 }
4211#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4212
Paul Moore07feee82009-03-27 17:10:54 -04004213 netlbl_secattr_init(&secattr);
4214 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004215 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004216 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004217 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004218 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004219 netlbl_secattr_destroy(&secattr);
4220
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004221#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4222access_check:
4223#endif
4224
Etienne Bassetecfcc532009-04-08 20:40:06 +02004225#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004226 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4227 ad.a.u.net->family = family;
4228 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004229 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4230#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004231 /*
Paul Moore07feee82009-03-27 17:10:54 -04004232 * Receiving a packet requires that the other end be able to write
4233 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004234 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004235 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4236 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004237 if (rc != 0)
4238 return rc;
4239
4240 /*
4241 * Save the peer's label in the request_sock so we can later setup
4242 * smk_packet in the child socket so that SO_PEERCRED can report it.
4243 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004244 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004245
4246 /*
4247 * We need to decide if we want to label the incoming connection here
4248 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004249 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004250 */
4251 hdr = ip_hdr(skb);
4252 addr.sin_addr.s_addr = hdr->saddr;
4253 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004254 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004255 rcu_read_unlock();
4256
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004257 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004258 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004259 else
Paul Moore07feee82009-03-27 17:10:54 -04004260 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004261
4262 return rc;
4263}
4264
Paul Moore07feee82009-03-27 17:10:54 -04004265/**
4266 * smack_inet_csk_clone - Copy the connection information to the new socket
4267 * @sk: the new socket
4268 * @req: the connection's request_sock
4269 *
4270 * Transfer the connection's peer label to the newly created socket.
4271 */
4272static void smack_inet_csk_clone(struct sock *sk,
4273 const struct request_sock *req)
4274{
4275 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004276 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004277
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004278 if (req->peer_secid != 0) {
4279 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004280 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004281 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004282 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004283}
4284
Casey Schauflere114e472008-02-04 22:29:50 -08004285/*
4286 * Key management security hooks
4287 *
4288 * Casey has not tested key support very heavily.
4289 * The permission check is most likely too restrictive.
4290 * If you care about keys please have a look.
4291 */
4292#ifdef CONFIG_KEYS
4293
4294/**
4295 * smack_key_alloc - Set the key security blob
4296 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004297 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004298 * @flags: unused
4299 *
4300 * No allocation required
4301 *
4302 * Returns 0
4303 */
David Howellsd84f4f92008-11-14 10:39:23 +11004304static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004305 unsigned long flags)
4306{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004307 struct smack_known *skp = smk_of_task(cred->security);
4308
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004309 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004310 return 0;
4311}
4312
4313/**
4314 * smack_key_free - Clear the key security blob
4315 * @key: the object
4316 *
4317 * Clear the blob pointer
4318 */
4319static void smack_key_free(struct key *key)
4320{
4321 key->security = NULL;
4322}
4323
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004324/**
Casey Schauflere114e472008-02-04 22:29:50 -08004325 * smack_key_permission - Smack access on a key
4326 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004327 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004328 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004329 *
4330 * Return 0 if the task has read and write to the object,
4331 * an error code otherwise
4332 */
4333static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004334 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004335{
4336 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004337 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004338 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004339 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004340 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004341
4342 keyp = key_ref_to_ptr(key_ref);
4343 if (keyp == NULL)
4344 return -EINVAL;
4345 /*
4346 * If the key hasn't been initialized give it access so that
4347 * it may do so.
4348 */
4349 if (keyp->security == NULL)
4350 return 0;
4351 /*
4352 * This should not occur
4353 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004354 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004355 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004356#ifdef CONFIG_AUDIT
4357 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4358 ad.a.u.key_struct.key = keyp->serial;
4359 ad.a.u.key_struct.key_desc = keyp->description;
4360#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004361 if (perm & KEY_NEED_READ)
4362 request = MAY_READ;
4363 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4364 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004365 rc = smk_access(tkp, keyp->security, request, &ad);
4366 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4367 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004368}
José Bollo7fc5f362015-02-17 15:41:22 +01004369
4370/*
4371 * smack_key_getsecurity - Smack label tagging the key
4372 * @key points to the key to be queried
4373 * @_buffer points to a pointer that should be set to point to the
4374 * resulting string (if no label or an error occurs).
4375 * Return the length of the string (including terminating NUL) or -ve if
4376 * an error.
4377 * May also return 0 (and a NULL buffer pointer) if there is no label.
4378 */
4379static int smack_key_getsecurity(struct key *key, char **_buffer)
4380{
4381 struct smack_known *skp = key->security;
4382 size_t length;
4383 char *copy;
4384
4385 if (key->security == NULL) {
4386 *_buffer = NULL;
4387 return 0;
4388 }
4389
4390 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4391 if (copy == NULL)
4392 return -ENOMEM;
4393 length = strlen(copy) + 1;
4394
4395 *_buffer = copy;
4396 return length;
4397}
4398
Casey Schauflere114e472008-02-04 22:29:50 -08004399#endif /* CONFIG_KEYS */
4400
4401/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004402 * Smack Audit hooks
4403 *
4404 * Audit requires a unique representation of each Smack specific
4405 * rule. This unique representation is used to distinguish the
4406 * object to be audited from remaining kernel objects and also
4407 * works as a glue between the audit hooks.
4408 *
4409 * Since repository entries are added but never deleted, we'll use
4410 * the smack_known label address related to the given audit rule as
4411 * the needed unique representation. This also better fits the smack
4412 * model where nearly everything is a label.
4413 */
4414#ifdef CONFIG_AUDIT
4415
4416/**
4417 * smack_audit_rule_init - Initialize a smack audit rule
4418 * @field: audit rule fields given from user-space (audit.h)
4419 * @op: required testing operator (=, !=, >, <, ...)
4420 * @rulestr: smack label to be audited
4421 * @vrule: pointer to save our own audit rule representation
4422 *
4423 * Prepare to audit cases where (@field @op @rulestr) is true.
4424 * The label to be audited is created if necessay.
4425 */
4426static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4427{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004428 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004429 char **rule = (char **)vrule;
4430 *rule = NULL;
4431
4432 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4433 return -EINVAL;
4434
Al Viro5af75d82008-12-16 05:59:26 -05004435 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004436 return -EINVAL;
4437
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004438 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004439 if (IS_ERR(skp))
4440 return PTR_ERR(skp);
4441
4442 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004443
4444 return 0;
4445}
4446
4447/**
4448 * smack_audit_rule_known - Distinguish Smack audit rules
4449 * @krule: rule of interest, in Audit kernel representation format
4450 *
4451 * This is used to filter Smack rules from remaining Audit ones.
4452 * If it's proved that this rule belongs to us, the
4453 * audit_rule_match hook will be called to do the final judgement.
4454 */
4455static int smack_audit_rule_known(struct audit_krule *krule)
4456{
4457 struct audit_field *f;
4458 int i;
4459
4460 for (i = 0; i < krule->field_count; i++) {
4461 f = &krule->fields[i];
4462
4463 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4464 return 1;
4465 }
4466
4467 return 0;
4468}
4469
4470/**
4471 * smack_audit_rule_match - Audit given object ?
4472 * @secid: security id for identifying the object to test
4473 * @field: audit rule flags given from user-space
4474 * @op: required testing operator
4475 * @vrule: smack internal rule presentation
4476 * @actx: audit context associated with the check
4477 *
4478 * The core Audit hook. It's used to take the decision of
4479 * whether to audit or not to audit a given object.
4480 */
4481static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4482 struct audit_context *actx)
4483{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004484 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004485 char *rule = vrule;
4486
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004487 if (unlikely(!rule)) {
4488 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004489 return -ENOENT;
4490 }
4491
4492 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4493 return 0;
4494
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004495 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004496
4497 /*
4498 * No need to do string comparisons. If a match occurs,
4499 * both pointers will point to the same smack_known
4500 * label.
4501 */
Al Viro5af75d82008-12-16 05:59:26 -05004502 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004503 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004504 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004505 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004506
4507 return 0;
4508}
4509
4510/**
4511 * smack_audit_rule_free - free smack rule representation
4512 * @vrule: rule to be freed.
4513 *
4514 * No memory was allocated.
4515 */
4516static void smack_audit_rule_free(void *vrule)
4517{
4518 /* No-op */
4519}
4520
4521#endif /* CONFIG_AUDIT */
4522
Randy Dunlap251a2a92009-02-18 11:42:33 -08004523/**
David Quigley746df9b2013-05-22 12:50:35 -04004524 * smack_ismaclabel - check if xattr @name references a smack MAC label
4525 * @name: Full xattr name to check.
4526 */
4527static int smack_ismaclabel(const char *name)
4528{
4529 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4530}
4531
4532
4533/**
Casey Schauflere114e472008-02-04 22:29:50 -08004534 * smack_secid_to_secctx - return the smack label for a secid
4535 * @secid: incoming integer
4536 * @secdata: destination
4537 * @seclen: how long it is
4538 *
4539 * Exists for networking code.
4540 */
4541static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4542{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004543 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004544
Eric Parisd5630b92010-10-13 16:24:48 -04004545 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004546 *secdata = skp->smk_known;
4547 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004548 return 0;
4549}
4550
Randy Dunlap251a2a92009-02-18 11:42:33 -08004551/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004552 * smack_secctx_to_secid - return the secid for a smack label
4553 * @secdata: smack label
4554 * @seclen: how long result is
4555 * @secid: outgoing integer
4556 *
4557 * Exists for audit and networking code.
4558 */
David Howellse52c17642008-04-29 20:52:51 +01004559static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004560{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004561 struct smack_known *skp = smk_find_entry(secdata);
4562
4563 if (skp)
4564 *secid = skp->smk_secid;
4565 else
4566 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004567 return 0;
4568}
4569
Randy Dunlap251a2a92009-02-18 11:42:33 -08004570/**
Casey Schauflere114e472008-02-04 22:29:50 -08004571 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004572 * @secdata: unused
4573 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004574 *
4575 * Exists to make sure nothing gets done, and properly
4576 */
4577static void smack_release_secctx(char *secdata, u32 seclen)
4578{
4579}
4580
David P. Quigley1ee65e32009-09-03 14:25:57 -04004581static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4582{
4583 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4584}
4585
4586static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4587{
4588 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4589}
4590
4591static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4592{
4593 int len = 0;
4594 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4595
4596 if (len < 0)
4597 return len;
4598 *ctxlen = len;
4599 return 0;
4600}
4601
Casey Schaufler1eddfe82015-07-30 14:35:14 -07004602static struct security_hook_list smack_hooks[] = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004603 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4604 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4605 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004606
Casey Schauflere20b0432015-05-02 15:11:36 -07004607 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4608 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4609 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4610 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4611 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304612 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4613 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
Casey Schauflere114e472008-02-04 22:29:50 -08004614
Casey Schauflere20b0432015-05-02 15:11:36 -07004615 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4616 LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
4617 LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004618
Casey Schauflere20b0432015-05-02 15:11:36 -07004619 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4620 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4621 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4622 LSM_HOOK_INIT(inode_link, smack_inode_link),
4623 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4624 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4625 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4626 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4627 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4628 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4629 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4630 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4631 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4632 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4633 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4634 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4635 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4636 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004637
Casey Schauflere20b0432015-05-02 15:11:36 -07004638 LSM_HOOK_INIT(file_permission, smack_file_permission),
4639 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4640 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4641 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4642 LSM_HOOK_INIT(file_lock, smack_file_lock),
4643 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4644 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4645 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4646 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4647 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4648 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004649
Casey Schauflere20b0432015-05-02 15:11:36 -07004650 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004651
Casey Schauflere20b0432015-05-02 15:11:36 -07004652 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4653 LSM_HOOK_INIT(cred_free, smack_cred_free),
4654 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4655 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4656 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4657 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4658 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4659 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4660 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4661 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4662 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4663 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4664 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4665 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4666 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4667 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4668 LSM_HOOK_INIT(task_kill, smack_task_kill),
4669 LSM_HOOK_INIT(task_wait, smack_task_wait),
4670 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004671
Casey Schauflere20b0432015-05-02 15:11:36 -07004672 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4673 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004674
Casey Schauflere20b0432015-05-02 15:11:36 -07004675 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4676 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004677
Casey Schauflere20b0432015-05-02 15:11:36 -07004678 LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
4679 LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
4680 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4681 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4682 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4683 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004684
Casey Schauflere20b0432015-05-02 15:11:36 -07004685 LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
4686 LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
4687 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4688 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4689 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004690
Casey Schauflere20b0432015-05-02 15:11:36 -07004691 LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
4692 LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
4693 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4694 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4695 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004696
Casey Schauflere20b0432015-05-02 15:11:36 -07004697 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004698
Casey Schauflere20b0432015-05-02 15:11:36 -07004699 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4700 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004701
Casey Schauflere20b0432015-05-02 15:11:36 -07004702 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4703 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004704
Casey Schauflere20b0432015-05-02 15:11:36 -07004705 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004706#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004707 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004708#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004709 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4710 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4711 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4712 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4713 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4714 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4715 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4716 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4717 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4718 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004719
Casey Schauflere114e472008-02-04 22:29:50 -08004720 /* key management security hooks */
4721#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004722 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4723 LSM_HOOK_INIT(key_free, smack_key_free),
4724 LSM_HOOK_INIT(key_permission, smack_key_permission),
4725 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004726#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004727
4728 /* Audit hooks */
4729#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004730 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4731 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4732 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4733 LSM_HOOK_INIT(audit_rule_free, smack_audit_rule_free),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004734#endif /* CONFIG_AUDIT */
4735
Casey Schauflere20b0432015-05-02 15:11:36 -07004736 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4737 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4738 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4739 LSM_HOOK_INIT(release_secctx, smack_release_secctx),
4740 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4741 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4742 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflere114e472008-02-04 22:29:50 -08004743};
4744
Etienne Basset7198e2e2009-03-24 20:53:24 +01004745
Casey Schaufler86812bb2012-04-17 18:55:46 -07004746static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004747{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004748 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004749 * Initialize rule list locks
4750 */
4751 mutex_init(&smack_known_huh.smk_rules_lock);
4752 mutex_init(&smack_known_hat.smk_rules_lock);
4753 mutex_init(&smack_known_floor.smk_rules_lock);
4754 mutex_init(&smack_known_star.smk_rules_lock);
4755 mutex_init(&smack_known_invalid.smk_rules_lock);
4756 mutex_init(&smack_known_web.smk_rules_lock);
4757 /*
4758 * Initialize rule lists
4759 */
4760 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4761 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4762 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4763 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4764 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4765 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4766 /*
4767 * Create the known labels list
4768 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004769 smk_insert_entry(&smack_known_huh);
4770 smk_insert_entry(&smack_known_hat);
4771 smk_insert_entry(&smack_known_star);
4772 smk_insert_entry(&smack_known_floor);
4773 smk_insert_entry(&smack_known_invalid);
4774 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004775}
4776
Casey Schauflere114e472008-02-04 22:29:50 -08004777/**
4778 * smack_init - initialize the smack system
4779 *
4780 * Returns 0
4781 */
4782static __init int smack_init(void)
4783{
David Howellsd84f4f92008-11-14 10:39:23 +11004784 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004785 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004786
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004787 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004788 return 0;
4789
Rohit1a5b4722014-10-15 17:40:41 +05304790 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4791 if (!smack_inode_cache)
4792 return -ENOMEM;
4793
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004794 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4795 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304796 if (tsp == NULL) {
4797 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004798 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304799 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004800
José Bollod21b7b02015-10-02 15:15:56 +02004801 smack_enabled = 1;
4802
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004803 pr_info("Smack: Initializing.\n");
4804#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4805 pr_info("Smack: Netfilter enabled.\n");
4806#endif
4807#ifdef SMACK_IPV6_PORT_LABELING
4808 pr_info("Smack: IPv6 port labeling enabled.\n");
4809#endif
4810#ifdef SMACK_IPV6_SECMARK_LABELING
4811 pr_info("Smack: IPv6 Netfilter enabled.\n");
4812#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004813
4814 /*
4815 * Set the security state for the initial task.
4816 */
David Howellsd84f4f92008-11-14 10:39:23 +11004817 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004818 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004819
Casey Schaufler86812bb2012-04-17 18:55:46 -07004820 /* initialize the smack_known_list */
4821 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004822
4823 /*
4824 * Register with LSM
4825 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004826 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
Casey Schauflere114e472008-02-04 22:29:50 -08004827
4828 return 0;
4829}
4830
4831/*
4832 * Smack requires early initialization in order to label
4833 * all processes and objects when they are created.
4834 */
4835security_initcall(smack_init);