blob: 996c889564383a4725e57a769b24abb53225cd50 [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
Casey Schauflerc6739442013-05-22 18:42:56 -070055LIST_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);
329 mutex_init(&tsp->smk_rules_lock);
330
331 return tsp;
332}
333
334/**
335 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100336 * @nhead: new rules header pointer
337 * @ohead: old rules header pointer
338 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800339 *
340 * Returns 0 on success, -ENOMEM on error
341 */
342static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
343 gfp_t gfp)
344{
345 struct smack_rule *nrp;
346 struct smack_rule *orp;
347 int rc = 0;
348
349 INIT_LIST_HEAD(nhead);
350
351 list_for_each_entry_rcu(orp, ohead, list) {
352 nrp = kzalloc(sizeof(struct smack_rule), gfp);
353 if (nrp == NULL) {
354 rc = -ENOMEM;
355 break;
356 }
357 *nrp = *orp;
358 list_add_rcu(&nrp->list, nhead);
359 }
360 return rc;
361}
362
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100363/**
364 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
365 * @mode - input mode in form of PTRACE_MODE_*
366 *
367 * Returns a converted MAY_* mode usable by smack rules
368 */
369static inline unsigned int smk_ptrace_mode(unsigned int mode)
370{
371 switch (mode) {
372 case PTRACE_MODE_READ:
373 return MAY_READ;
374 case PTRACE_MODE_ATTACH:
375 return MAY_READWRITE;
376 }
377
378 return 0;
379}
380
381/**
382 * smk_ptrace_rule_check - helper for ptrace access
383 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200384 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100385 * @mode: ptrace attachment mode (PTRACE_MODE_*)
386 * @func: name of the function that called us, used for audit
387 *
388 * Returns 0 on access granted, -error on error
389 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200390static int smk_ptrace_rule_check(struct task_struct *tracer,
391 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100392 unsigned int mode, const char *func)
393{
394 int rc;
395 struct smk_audit_info ad, *saip = NULL;
396 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200397 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100398
399 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
400 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
401 smk_ad_setfield_u_tsk(&ad, tracer);
402 saip = &ad;
403 }
404
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300405 rcu_read_lock();
406 tsp = __task_cred(tracer)->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200407 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100408
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100409 if ((mode & PTRACE_MODE_ATTACH) &&
410 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
411 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200412 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100413 rc = 0;
414 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
415 rc = -EACCES;
416 else if (capable(CAP_SYS_PTRACE))
417 rc = 0;
418 else
419 rc = -EACCES;
420
421 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200422 smack_log(tracer_known->smk_known,
423 tracee_known->smk_known,
424 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100425
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300426 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100427 return rc;
428 }
429
430 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200431 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300432
433 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100434 return rc;
435}
436
Casey Schauflere114e472008-02-04 22:29:50 -0800437/*
438 * LSM hooks.
439 * We he, that is fun!
440 */
441
442/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000443 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800444 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100445 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800446 *
447 * Returns 0 if access is OK, an error code otherwise
448 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100449 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800450 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000451static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800452{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700453 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800454
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300455 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200456
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700457 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100458}
Casey Schauflere114e472008-02-04 22:29:50 -0800459
David Howells5cd9c582008-08-14 11:37:28 +0100460/**
461 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
462 * @ptp: parent task pointer
463 *
464 * Returns 0 if access is OK, an error code otherwise
465 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100466 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100467 */
468static int smack_ptrace_traceme(struct task_struct *ptp)
469{
470 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700471 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100472
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100473 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200474
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200475 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800476 return rc;
477}
478
479/**
480 * smack_syslog - Smack approval on syslog
481 * @type: message type
482 *
Casey Schauflere114e472008-02-04 22:29:50 -0800483 * Returns 0 on success, error code otherwise.
484 */
Eric Paris12b30522010-11-15 18:36:29 -0500485static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800486{
Eric Paris12b30522010-11-15 18:36:29 -0500487 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700488 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800489
Casey Schaufler1880eff2012-06-05 15:28:30 -0700490 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800491 return 0;
492
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800493 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800494 rc = -EACCES;
495
496 return rc;
497}
498
499
500/*
501 * Superblock Hooks.
502 */
503
504/**
505 * smack_sb_alloc_security - allocate a superblock blob
506 * @sb: the superblock getting the blob
507 *
508 * Returns 0 on success or -ENOMEM on error.
509 */
510static int smack_sb_alloc_security(struct super_block *sb)
511{
512 struct superblock_smack *sbsp;
513
514 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
515
516 if (sbsp == NULL)
517 return -ENOMEM;
518
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200519 sbsp->smk_root = &smack_known_floor;
520 sbsp->smk_default = &smack_known_floor;
521 sbsp->smk_floor = &smack_known_floor;
522 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700523 /*
524 * smk_initialized will be zero from kzalloc.
525 */
Casey Schauflere114e472008-02-04 22:29:50 -0800526 sb->s_security = sbsp;
527
528 return 0;
529}
530
531/**
532 * smack_sb_free_security - free a superblock blob
533 * @sb: the superblock getting the blob
534 *
535 */
536static void smack_sb_free_security(struct super_block *sb)
537{
538 kfree(sb->s_security);
539 sb->s_security = NULL;
540}
541
542/**
543 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800544 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800545 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800546 *
547 * Returns 0 on success or -ENOMEM on error.
548 *
549 * Copy the Smack specific mount options out of the mount
550 * options list.
551 */
Eric Parise0007522008-03-05 10:31:54 -0500552static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800553{
554 char *cp, *commap, *otheropts, *dp;
555
Casey Schauflere114e472008-02-04 22:29:50 -0800556 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
557 if (otheropts == NULL)
558 return -ENOMEM;
559
560 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
561 if (strstr(cp, SMK_FSDEFAULT) == cp)
562 dp = smackopts;
563 else if (strstr(cp, SMK_FSFLOOR) == cp)
564 dp = smackopts;
565 else if (strstr(cp, SMK_FSHAT) == cp)
566 dp = smackopts;
567 else if (strstr(cp, SMK_FSROOT) == cp)
568 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700569 else if (strstr(cp, SMK_FSTRANS) == cp)
570 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800571 else
572 dp = otheropts;
573
574 commap = strchr(cp, ',');
575 if (commap != NULL)
576 *commap = '\0';
577
578 if (*dp != '\0')
579 strcat(dp, ",");
580 strcat(dp, cp);
581 }
582
583 strcpy(orig, otheropts);
584 free_page((unsigned long)otheropts);
585
586 return 0;
587}
588
589/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530590 * smack_parse_opts_str - parse Smack specific mount options
591 * @options: mount options string
592 * @opts: where to store converted mount opts
593 *
594 * Returns 0 on success or -ENOMEM on error.
595 *
596 * converts Smack specific mount options to generic security option format
597 */
598static int smack_parse_opts_str(char *options,
599 struct security_mnt_opts *opts)
600{
601 char *p;
Casey Schaufler3d04c922015-08-12 11:56:02 -0700602 char *fsdefault = NULL;
603 char *fsfloor = NULL;
604 char *fshat = NULL;
605 char *fsroot = NULL;
606 char *fstransmute = NULL;
607 int rc = -ENOMEM;
608 int num_mnt_opts = 0;
609 int token;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530610
611 opts->num_mnt_opts = 0;
612
613 if (!options)
614 return 0;
615
616 while ((p = strsep(&options, ",")) != NULL) {
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530617 substring_t args[MAX_OPT_ARGS];
618
619 if (!*p)
620 continue;
621
Casey Schaufler3d04c922015-08-12 11:56:02 -0700622 token = match_token(p, smk_mount_tokens, args);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530623
624 switch (token) {
625 case Opt_fsdefault:
626 if (fsdefault)
627 goto out_opt_err;
628 fsdefault = match_strdup(&args[0]);
629 if (!fsdefault)
630 goto out_err;
631 break;
632 case Opt_fsfloor:
633 if (fsfloor)
634 goto out_opt_err;
635 fsfloor = match_strdup(&args[0]);
636 if (!fsfloor)
637 goto out_err;
638 break;
639 case Opt_fshat:
640 if (fshat)
641 goto out_opt_err;
642 fshat = match_strdup(&args[0]);
643 if (!fshat)
644 goto out_err;
645 break;
646 case Opt_fsroot:
647 if (fsroot)
648 goto out_opt_err;
649 fsroot = match_strdup(&args[0]);
650 if (!fsroot)
651 goto out_err;
652 break;
653 case Opt_fstransmute:
654 if (fstransmute)
655 goto out_opt_err;
656 fstransmute = match_strdup(&args[0]);
657 if (!fstransmute)
658 goto out_err;
659 break;
660 default:
661 rc = -EINVAL;
662 pr_warn("Smack: unknown mount option\n");
663 goto out_err;
664 }
665 }
666
667 opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
668 if (!opts->mnt_opts)
669 goto out_err;
670
671 opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
672 GFP_ATOMIC);
673 if (!opts->mnt_opts_flags) {
674 kfree(opts->mnt_opts);
675 goto out_err;
676 }
677
678 if (fsdefault) {
679 opts->mnt_opts[num_mnt_opts] = fsdefault;
680 opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
681 }
682 if (fsfloor) {
683 opts->mnt_opts[num_mnt_opts] = fsfloor;
684 opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
685 }
686 if (fshat) {
687 opts->mnt_opts[num_mnt_opts] = fshat;
688 opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
689 }
690 if (fsroot) {
691 opts->mnt_opts[num_mnt_opts] = fsroot;
692 opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
693 }
694 if (fstransmute) {
695 opts->mnt_opts[num_mnt_opts] = fstransmute;
696 opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
697 }
698
699 opts->num_mnt_opts = num_mnt_opts;
700 return 0;
701
702out_opt_err:
703 rc = -EINVAL;
704 pr_warn("Smack: duplicate mount options\n");
705
706out_err:
707 kfree(fsdefault);
708 kfree(fsfloor);
709 kfree(fshat);
710 kfree(fsroot);
711 kfree(fstransmute);
712 return rc;
713}
714
715/**
716 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800717 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530718 * @opts: Smack mount options
719 * @kern_flags: mount option from kernel space or user space
720 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800721 *
722 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530723 *
724 * Allow filesystems with binary mount data to explicitly set Smack mount
725 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800726 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530727static int smack_set_mnt_opts(struct super_block *sb,
728 struct security_mnt_opts *opts,
729 unsigned long kern_flags,
730 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800731{
732 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000733 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800734 struct superblock_smack *sp = sb->s_security;
735 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800736 struct smack_known *skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530737 int i;
738 int num_opts = opts->num_mnt_opts;
Casey Schauflere830b392013-05-22 18:43:07 -0700739 int transmute = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800740
Casey Schauflere830b392013-05-22 18:43:07 -0700741 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800742 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700743
Casey Schauflere114e472008-02-04 22:29:50 -0800744 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800745
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530746 for (i = 0; i < num_opts; i++) {
747 switch (opts->mnt_opts_flags[i]) {
748 case FSDEFAULT_MNT:
749 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200750 if (IS_ERR(skp))
751 return PTR_ERR(skp);
752 sp->smk_default = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530753 break;
754 case FSFLOOR_MNT:
755 skp = smk_import_entry(opts->mnt_opts[i], 0);
756 if (IS_ERR(skp))
757 return PTR_ERR(skp);
758 sp->smk_floor = skp;
759 break;
760 case FSHAT_MNT:
761 skp = smk_import_entry(opts->mnt_opts[i], 0);
762 if (IS_ERR(skp))
763 return PTR_ERR(skp);
764 sp->smk_hat = skp;
765 break;
766 case FSROOT_MNT:
767 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200768 if (IS_ERR(skp))
769 return PTR_ERR(skp);
770 sp->smk_root = skp;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530771 break;
772 case FSTRANS_MNT:
773 skp = smk_import_entry(opts->mnt_opts[i], 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200774 if (IS_ERR(skp))
775 return PTR_ERR(skp);
776 sp->smk_root = skp;
777 transmute = 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530778 break;
779 default:
780 break;
Casey Schauflere114e472008-02-04 22:29:50 -0800781 }
782 }
783
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800784 if (!smack_privileged(CAP_MAC_ADMIN)) {
785 /*
786 * Unprivileged mounts don't get to specify Smack values.
787 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530788 if (num_opts)
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800789 return -EPERM;
790 /*
791 * Unprivileged mounts get root and default from the caller.
792 */
793 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200794 sp->smk_root = skp;
795 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800796 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530797
Casey Schauflere114e472008-02-04 22:29:50 -0800798 /*
799 * Initialize the root inode.
800 */
801 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100802 if (isp == NULL) {
803 isp = new_inode_smack(sp->smk_root);
804 if (isp == NULL)
805 return -ENOMEM;
806 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700807 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800808 isp->smk_inode = sp->smk_root;
809
Casey Schauflere830b392013-05-22 18:43:07 -0700810 if (transmute)
811 isp->smk_flags |= SMK_INODE_TRANSMUTE;
812
Casey Schauflere114e472008-02-04 22:29:50 -0800813 return 0;
814}
815
816/**
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530817 * smack_sb_kern_mount - Smack specific mount processing
818 * @sb: the file system superblock
819 * @flags: the mount flags
820 * @data: the smack mount options
821 *
822 * Returns 0 on success, an error code on failure
823 */
824static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
825{
826 int rc = 0;
827 char *options = data;
828 struct security_mnt_opts opts;
829
830 security_init_mnt_opts(&opts);
831
832 if (!options)
833 goto out;
834
835 rc = smack_parse_opts_str(options, &opts);
836 if (rc)
837 goto out_err;
838
839out:
840 rc = smack_set_mnt_opts(sb, &opts, 0, NULL);
841
842out_err:
843 security_free_mnt_opts(&opts);
844 return rc;
845}
846
847/**
Casey Schauflere114e472008-02-04 22:29:50 -0800848 * smack_sb_statfs - Smack check on statfs
849 * @dentry: identifies the file system in question
850 *
851 * Returns 0 if current can read the floor of the filesystem,
852 * and error code otherwise
853 */
854static int smack_sb_statfs(struct dentry *dentry)
855{
856 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200857 int rc;
858 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800859
Eric Parisa2694342011-04-25 13:10:27 -0400860 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200861 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
862
863 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700864 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200865 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800866}
867
Casey Schauflere114e472008-02-04 22:29:50 -0800868/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800869 * BPRM hooks
870 */
871
Casey Schauflerce8a4322011-09-29 18:21:01 -0700872/**
873 * smack_bprm_set_creds - set creds for exec
874 * @bprm: the exec information
875 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100876 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700877 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800878static int smack_bprm_set_creds(struct linux_binprm *bprm)
879{
Al Viro496ad9a2013-01-23 17:07:38 -0500880 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300881 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800882 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800883 int rc;
884
Casey Schaufler676dac42010-12-02 06:43:39 -0800885 if (bprm->cred_prepared)
886 return 0;
887
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300888 isp = inode->i_security;
889 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800890 return 0;
891
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100892 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
893 struct task_struct *tracer;
894 rc = 0;
895
896 rcu_read_lock();
897 tracer = ptrace_parent(current);
898 if (likely(tracer != NULL))
899 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200900 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100901 PTRACE_MODE_ATTACH,
902 __func__);
903 rcu_read_unlock();
904
905 if (rc != 0)
906 return rc;
907 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300908 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800909
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300910 bsp->smk_task = isp->smk_task;
911 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800912
913 return 0;
914}
915
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300916/**
917 * smack_bprm_committing_creds - Prepare to install the new credentials
918 * from bprm.
919 *
920 * @bprm: binprm for exec
921 */
922static void smack_bprm_committing_creds(struct linux_binprm *bprm)
923{
924 struct task_smack *bsp = bprm->cred->security;
925
926 if (bsp->smk_task != bsp->smk_forked)
927 current->pdeath_signal = 0;
928}
929
930/**
931 * smack_bprm_secureexec - Return the decision to use secureexec.
932 * @bprm: binprm for exec
933 *
934 * Returns 0 on success.
935 */
936static int smack_bprm_secureexec(struct linux_binprm *bprm)
937{
938 struct task_smack *tsp = current_security();
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300939
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700940 if (tsp->smk_task != tsp->smk_forked)
941 return 1;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300942
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700943 return 0;
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300944}
945
Casey Schaufler676dac42010-12-02 06:43:39 -0800946/*
Casey Schauflere114e472008-02-04 22:29:50 -0800947 * Inode hooks
948 */
949
950/**
951 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800952 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800953 *
954 * Returns 0 if it gets a blob, -ENOMEM otherwise
955 */
956static int smack_inode_alloc_security(struct inode *inode)
957{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700958 struct smack_known *skp = smk_of_current();
959
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200960 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800961 if (inode->i_security == NULL)
962 return -ENOMEM;
963 return 0;
964}
965
966/**
967 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800968 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800969 *
970 * Clears the blob pointer in inode
971 */
972static void smack_inode_free_security(struct inode *inode)
973{
Rohit1a5b4722014-10-15 17:40:41 +0530974 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800975 inode->i_security = NULL;
976}
977
978/**
979 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200980 * @inode: the newly created inode
981 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500982 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800983 * @name: where to put the attribute name
984 * @value: where to put the attribute value
985 * @len: where to put the length of the attribute
986 *
987 * Returns 0 if it all works out, -ENOMEM if there's no memory
988 */
989static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900990 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500991 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800992{
Casey Schaufler2267b132012-03-13 19:14:19 -0700993 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700994 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200995 struct smack_known *isp = smk_of_inode(inode);
996 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800997 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800998
Tetsuo Handa95489062013-07-25 05:44:02 +0900999 if (name)
1000 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -08001001
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001002 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001003 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001004 may = smk_access_entry(skp->smk_known, dsp->smk_known,
1005 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001006 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001007
1008 /*
1009 * If the access rule allows transmutation and
1010 * the directory requests transmutation then
1011 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001012 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001013 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001014 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001015 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001016 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001017 issp->smk_flags |= SMK_INODE_CHANGED;
1018 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001019
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001020 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001021 if (*value == NULL)
1022 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001023
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001024 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001025 }
Casey Schauflere114e472008-02-04 22:29:50 -08001026
1027 return 0;
1028}
1029
1030/**
1031 * smack_inode_link - Smack check on link
1032 * @old_dentry: the existing object
1033 * @dir: unused
1034 * @new_dentry: the new object
1035 *
1036 * Returns 0 if access is permitted, an error code otherwise
1037 */
1038static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1039 struct dentry *new_dentry)
1040{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001041 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042 struct smk_audit_info ad;
1043 int rc;
1044
Eric Parisa2694342011-04-25 13:10:27 -04001045 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001046 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001047
David Howellsc6f493d2015-03-17 22:26:22 +00001048 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001049 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001050 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001051
David Howells88025652015-01-29 12:02:32 +00001052 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001053 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001054 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1055 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001056 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001057 }
1058
1059 return rc;
1060}
1061
1062/**
1063 * smack_inode_unlink - Smack check on inode deletion
1064 * @dir: containing directory object
1065 * @dentry: file to unlink
1066 *
1067 * Returns 0 if current can write the containing directory
1068 * and the object, error code otherwise
1069 */
1070static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1071{
David Howellsc6f493d2015-03-17 22:26:22 +00001072 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001073 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001074 int rc;
1075
Eric Parisa2694342011-04-25 13:10:27 -04001076 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001077 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1078
Casey Schauflere114e472008-02-04 22:29:50 -08001079 /*
1080 * You need write access to the thing you're unlinking
1081 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001082 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001083 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001084 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001085 /*
1086 * You also need write access to the containing directory
1087 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001088 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001089 smk_ad_setfield_u_fs_inode(&ad, dir);
1090 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001091 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001092 }
Casey Schauflere114e472008-02-04 22:29:50 -08001093 return rc;
1094}
1095
1096/**
1097 * smack_inode_rmdir - Smack check on directory deletion
1098 * @dir: containing directory object
1099 * @dentry: directory to unlink
1100 *
1101 * Returns 0 if current can write the containing directory
1102 * and the directory, error code otherwise
1103 */
1104static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1105{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001107 int rc;
1108
Eric Parisa2694342011-04-25 13:10:27 -04001109 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001110 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1111
Casey Schauflere114e472008-02-04 22:29:50 -08001112 /*
1113 * You need write access to the thing you're removing
1114 */
David Howellsc6f493d2015-03-17 22:26:22 +00001115 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1116 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001117 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001118 /*
1119 * You also need write access to the containing directory
1120 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001121 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001122 smk_ad_setfield_u_fs_inode(&ad, dir);
1123 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001124 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001125 }
Casey Schauflere114e472008-02-04 22:29:50 -08001126
1127 return rc;
1128}
1129
1130/**
1131 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001132 * @old_inode: unused
1133 * @old_dentry: the old object
1134 * @new_inode: unused
1135 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001136 *
1137 * Read and write access is required on both the old and
1138 * new directories.
1139 *
1140 * Returns 0 if access is permitted, an error code otherwise
1141 */
1142static int smack_inode_rename(struct inode *old_inode,
1143 struct dentry *old_dentry,
1144 struct inode *new_inode,
1145 struct dentry *new_dentry)
1146{
1147 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001148 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001149 struct smk_audit_info ad;
1150
Eric Parisa2694342011-04-25 13:10:27 -04001151 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001152 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001153
David Howellsc6f493d2015-03-17 22:26:22 +00001154 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001155 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001156 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001157
David Howells88025652015-01-29 12:02:32 +00001158 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001159 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001160 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1161 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001162 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001163 }
Casey Schauflere114e472008-02-04 22:29:50 -08001164 return rc;
1165}
1166
1167/**
1168 * smack_inode_permission - Smack version of permission()
1169 * @inode: the inode in question
1170 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001171 *
1172 * This is the important Smack hook.
1173 *
1174 * Returns 0 if access is permitted, -EACCES otherwise
1175 */
Al Viroe74f71e2011-06-20 19:38:15 -04001176static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001177{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001178 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001179 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001180 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001181
1182 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001183 /*
1184 * No permission to check. Existence test. Yup, it's there.
1185 */
1186 if (mask == 0)
1187 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001188
1189 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001190 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001191 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001192 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001193 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001194 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1195 rc = smk_bu_inode(inode, mask, rc);
1196 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001197}
1198
1199/**
1200 * smack_inode_setattr - Smack check for setting attributes
1201 * @dentry: the object
1202 * @iattr: for the force flag
1203 *
1204 * Returns 0 if access is permitted, an error code otherwise
1205 */
1206static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1207{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001208 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001209 int rc;
1210
Casey Schauflere114e472008-02-04 22:29:50 -08001211 /*
1212 * Need to allow for clearing the setuid bit.
1213 */
1214 if (iattr->ia_valid & ATTR_FORCE)
1215 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001216 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001217 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001218
David Howellsc6f493d2015-03-17 22:26:22 +00001219 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1220 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001221 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001222}
1223
1224/**
1225 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001226 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001227 * @dentry: the object
1228 *
1229 * Returns 0 if access is permitted, an error code otherwise
1230 */
Al Viro3f7036a2015-03-08 19:28:30 -04001231static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001232{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001233 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001234 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001235 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001236
Eric Parisf48b7392011-04-25 12:54:27 -04001237 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001238 smk_ad_setfield_u_fs_path(&ad, *path);
1239 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1240 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001241 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001242}
1243
1244/**
1245 * smack_inode_setxattr - Smack check for setting xattrs
1246 * @dentry: the object
1247 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001248 * @value: value of the attribute
1249 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001250 * @flags: unused
1251 *
1252 * This protects the Smack attribute explicitly.
1253 *
1254 * Returns 0 if access is permitted, an error code otherwise
1255 */
David Howells8f0cfa52008-04-29 00:59:41 -07001256static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1257 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001258{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001259 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001260 struct smack_known *skp;
1261 int check_priv = 0;
1262 int check_import = 0;
1263 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001264 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001265
Casey Schaufler19760ad2013-12-16 16:27:26 -08001266 /*
1267 * Check label validity here so import won't fail in post_setxattr
1268 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001269 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1270 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001271 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1272 check_priv = 1;
1273 check_import = 1;
1274 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1275 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1276 check_priv = 1;
1277 check_import = 1;
1278 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001279 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001280 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001281 if (size != TRANS_TRUE_SIZE ||
1282 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1283 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001284 } else
1285 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1286
Casey Schaufler19760ad2013-12-16 16:27:26 -08001287 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1288 rc = -EPERM;
1289
1290 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001291 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001292 if (IS_ERR(skp))
1293 rc = PTR_ERR(skp);
1294 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001295 (skp == &smack_known_star || skp == &smack_known_web)))
1296 rc = -EINVAL;
1297 }
1298
Eric Parisa2694342011-04-25 13:10:27 -04001299 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001300 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1301
Casey Schauflerd166c802014-08-27 14:51:27 -07001302 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001303 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1304 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001305 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001306
1307 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001308}
1309
1310/**
1311 * smack_inode_post_setxattr - Apply the Smack update approved above
1312 * @dentry: object
1313 * @name: attribute name
1314 * @value: attribute value
1315 * @size: attribute size
1316 * @flags: unused
1317 *
1318 * Set the pointer in the inode blob to the entry found
1319 * in the master label list.
1320 */
David Howells8f0cfa52008-04-29 00:59:41 -07001321static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1322 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001323{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001324 struct smack_known *skp;
David Howellsc6f493d2015-03-17 22:26:22 +00001325 struct inode_smack *isp = d_backing_inode(dentry)->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001326
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001327 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1328 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1329 return;
1330 }
1331
Casey Schaufler676dac42010-12-02 06:43:39 -08001332 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001333 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001334 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001335 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001336 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001337 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001338 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001339 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001340 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001341 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001342 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001343 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001344 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001345 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001346 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001347 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001348 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001349 isp->smk_mmap = &smack_known_invalid;
1350 }
Casey Schauflere114e472008-02-04 22:29:50 -08001351
1352 return;
1353}
1354
Casey Schauflerce8a4322011-09-29 18:21:01 -07001355/**
Casey Schauflere114e472008-02-04 22:29:50 -08001356 * smack_inode_getxattr - Smack check on getxattr
1357 * @dentry: the object
1358 * @name: unused
1359 *
1360 * Returns 0 if access is permitted, an error code otherwise
1361 */
David Howells8f0cfa52008-04-29 00:59:41 -07001362static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001363{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001364 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001365 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001366
Eric Parisa2694342011-04-25 13:10:27 -04001367 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001368 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1369
David Howellsc6f493d2015-03-17 22:26:22 +00001370 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1371 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001372 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001373}
1374
Casey Schauflerce8a4322011-09-29 18:21:01 -07001375/**
Casey Schauflere114e472008-02-04 22:29:50 -08001376 * smack_inode_removexattr - Smack check on removexattr
1377 * @dentry: the object
1378 * @name: name of the attribute
1379 *
1380 * Removing the Smack attribute requires CAP_MAC_ADMIN
1381 *
1382 * Returns 0 if access is permitted, an error code otherwise
1383 */
David Howells8f0cfa52008-04-29 00:59:41 -07001384static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001385{
Casey Schaufler676dac42010-12-02 06:43:39 -08001386 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001387 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001388 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001389
Casey Schauflerbcdca222008-02-23 15:24:04 -08001390 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1391 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001392 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001393 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001394 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301395 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001396 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001397 rc = -EPERM;
1398 } else
1399 rc = cap_inode_removexattr(dentry, name);
1400
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001401 if (rc != 0)
1402 return rc;
1403
Eric Parisa2694342011-04-25 13:10:27 -04001404 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001405 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001406
David Howellsc6f493d2015-03-17 22:26:22 +00001407 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1408 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001409 if (rc != 0)
1410 return rc;
1411
David Howellsc6f493d2015-03-17 22:26:22 +00001412 isp = d_backing_inode(dentry)->i_security;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001413 /*
1414 * Don't do anything special for these.
1415 * XATTR_NAME_SMACKIPIN
1416 * XATTR_NAME_SMACKIPOUT
1417 * XATTR_NAME_SMACKEXEC
1418 */
1419 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001420 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001421 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001422 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001423 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1424 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001425
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001426 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001427}
1428
1429/**
1430 * smack_inode_getsecurity - get smack xattrs
1431 * @inode: the object
1432 * @name: attribute name
1433 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001434 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001435 *
1436 * Returns the size of the attribute or an error code
1437 */
1438static int smack_inode_getsecurity(const struct inode *inode,
1439 const char *name, void **buffer,
1440 bool alloc)
1441{
1442 struct socket_smack *ssp;
1443 struct socket *sock;
1444 struct super_block *sbp;
1445 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001446 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001447 int ilen;
1448 int rc = 0;
1449
1450 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1451 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001452 ilen = strlen(isp->smk_known);
1453 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001454 return ilen;
1455 }
1456
1457 /*
1458 * The rest of the Smack xattrs are only on sockets.
1459 */
1460 sbp = ip->i_sb;
1461 if (sbp->s_magic != SOCKFS_MAGIC)
1462 return -EOPNOTSUPP;
1463
1464 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001465 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001466 return -EOPNOTSUPP;
1467
1468 ssp = sock->sk->sk_security;
1469
1470 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001471 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001472 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001473 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001474 else
1475 return -EOPNOTSUPP;
1476
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001477 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001478 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001479 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001480 rc = ilen;
1481 }
1482
1483 return rc;
1484}
1485
1486
1487/**
1488 * smack_inode_listsecurity - list the Smack attributes
1489 * @inode: the object
1490 * @buffer: where they go
1491 * @buffer_size: size of buffer
1492 *
1493 * Returns 0 on success, -EINVAL otherwise
1494 */
1495static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1496 size_t buffer_size)
1497{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001498 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001499
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001500 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001501 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001502
1503 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001504}
1505
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001506/**
1507 * smack_inode_getsecid - Extract inode's security id
1508 * @inode: inode to extract the info from
1509 * @secid: where result will be saved
1510 */
1511static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1512{
1513 struct inode_smack *isp = inode->i_security;
1514
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001515 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001516}
1517
Casey Schauflere114e472008-02-04 22:29:50 -08001518/*
1519 * File Hooks
1520 */
1521
1522/**
1523 * smack_file_permission - Smack check on file operations
1524 * @file: unused
1525 * @mask: unused
1526 *
1527 * Returns 0
1528 *
1529 * Should access checks be done on each read or write?
1530 * UNICOS and SELinux say yes.
1531 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1532 *
1533 * I'll say no for now. Smack does not do the frequent
1534 * label changing that SELinux does.
1535 */
1536static int smack_file_permission(struct file *file, int mask)
1537{
1538 return 0;
1539}
1540
1541/**
1542 * smack_file_alloc_security - assign a file security blob
1543 * @file: the object
1544 *
1545 * The security blob for a file is a pointer to the master
1546 * label list, so no allocation is done.
1547 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001548 * f_security is the owner security information. It
1549 * isn't used on file access checks, it's for send_sigio.
1550 *
Casey Schauflere114e472008-02-04 22:29:50 -08001551 * Returns 0
1552 */
1553static int smack_file_alloc_security(struct file *file)
1554{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001555 struct smack_known *skp = smk_of_current();
1556
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001557 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001558 return 0;
1559}
1560
1561/**
1562 * smack_file_free_security - clear a file security blob
1563 * @file: the object
1564 *
1565 * The security blob for a file is a pointer to the master
1566 * label list, so no memory is freed.
1567 */
1568static void smack_file_free_security(struct file *file)
1569{
1570 file->f_security = NULL;
1571}
1572
1573/**
1574 * smack_file_ioctl - Smack check on ioctls
1575 * @file: the object
1576 * @cmd: what to do
1577 * @arg: unused
1578 *
1579 * Relies heavily on the correct use of the ioctl command conventions.
1580 *
1581 * Returns 0 if allowed, error code otherwise
1582 */
1583static int smack_file_ioctl(struct file *file, unsigned int cmd,
1584 unsigned long arg)
1585{
1586 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001587 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001588 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001589
Eric Parisf48b7392011-04-25 12:54:27 -04001590 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001591 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001592
Casey Schauflerd166c802014-08-27 14:51:27 -07001593 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001594 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001595 rc = smk_bu_file(file, MAY_WRITE, rc);
1596 }
Casey Schauflere114e472008-02-04 22:29:50 -08001597
Casey Schauflerd166c802014-08-27 14:51:27 -07001598 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001599 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001600 rc = smk_bu_file(file, MAY_READ, rc);
1601 }
Casey Schauflere114e472008-02-04 22:29:50 -08001602
1603 return rc;
1604}
1605
1606/**
1607 * smack_file_lock - Smack check on file locking
1608 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001609 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001610 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001611 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001612 */
1613static int smack_file_lock(struct file *file, unsigned int cmd)
1614{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001615 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001616 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001617 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001618
Eric Paris92f42502011-04-25 13:15:55 -04001619 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1620 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001621 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001622 rc = smk_bu_file(file, MAY_LOCK, rc);
1623 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001624}
1625
1626/**
1627 * smack_file_fcntl - Smack check on fcntl
1628 * @file: the object
1629 * @cmd: what action to check
1630 * @arg: unused
1631 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001632 * Generally these operations are harmless.
1633 * File locking operations present an obvious mechanism
1634 * for passing information, so they require write access.
1635 *
Casey Schauflere114e472008-02-04 22:29:50 -08001636 * Returns 0 if current has access, error code otherwise
1637 */
1638static int smack_file_fcntl(struct file *file, unsigned int cmd,
1639 unsigned long arg)
1640{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001641 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001642 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001643 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001644
Casey Schauflere114e472008-02-04 22:29:50 -08001645 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001646 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001647 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001648 case F_SETLK:
1649 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001650 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1651 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001652 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001653 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001654 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001655 case F_SETOWN:
1656 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001657 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1658 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001659 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001660 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001661 break;
1662 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001663 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001664 }
1665
1666 return rc;
1667}
1668
1669/**
Al Viroe5467852012-05-30 13:30:51 -04001670 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001671 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1672 * if mapping anonymous memory.
1673 * @file contains the file structure for file to map (may be NULL).
1674 * @reqprot contains the protection requested by the application.
1675 * @prot contains the protection that will be applied by the kernel.
1676 * @flags contains the operational flags.
1677 * Return 0 if permission is granted.
1678 */
Al Viroe5467852012-05-30 13:30:51 -04001679static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001680 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001681 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001682{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001683 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001684 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001685 struct smack_rule *srp;
1686 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001687 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001688 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001689 int may;
1690 int mmay;
1691 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001692 int rc;
1693
Al Viro496ad9a2013-01-23 17:07:38 -05001694 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001695 return 0;
1696
Al Viro496ad9a2013-01-23 17:07:38 -05001697 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001698 if (isp->smk_mmap == NULL)
1699 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001700 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001701
1702 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001703 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001704 rc = 0;
1705
1706 rcu_read_lock();
1707 /*
1708 * For each Smack rule associated with the subject
1709 * label verify that the SMACK64MMAP also has access
1710 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001711 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001712 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001713 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001714 /*
1715 * Matching labels always allows access.
1716 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001717 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001718 continue;
1719 /*
1720 * If there is a matching local rule take
1721 * that into account as well.
1722 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001723 may = smk_access_entry(srp->smk_subject->smk_known,
1724 okp->smk_known,
1725 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001726 if (may == -ENOENT)
1727 may = srp->smk_access;
1728 else
1729 may &= srp->smk_access;
1730 /*
1731 * If may is zero the SMACK64MMAP subject can't
1732 * possibly have less access.
1733 */
1734 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001735 continue;
1736
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001737 /*
1738 * Fetch the global list entry.
1739 * If there isn't one a SMACK64MMAP subject
1740 * can't have as much access as current.
1741 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001742 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1743 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001744 if (mmay == -ENOENT) {
1745 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001746 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001747 }
1748 /*
1749 * If there is a local entry it modifies the
1750 * potential access, too.
1751 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001752 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1753 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001754 if (tmay != -ENOENT)
1755 mmay &= tmay;
1756
1757 /*
1758 * If there is any access available to current that is
1759 * not available to a SMACK64MMAP subject
1760 * deny access.
1761 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001762 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001763 rc = -EACCES;
1764 break;
1765 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001766 }
1767
1768 rcu_read_unlock();
1769
1770 return rc;
1771}
1772
1773/**
Casey Schauflere114e472008-02-04 22:29:50 -08001774 * smack_file_set_fowner - set the file security blob value
1775 * @file: object in question
1776 *
Casey Schauflere114e472008-02-04 22:29:50 -08001777 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001778static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001779{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001780 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001781}
1782
1783/**
1784 * smack_file_send_sigiotask - Smack on sigio
1785 * @tsk: The target task
1786 * @fown: the object the signal come from
1787 * @signum: unused
1788 *
1789 * Allow a privileged task to get signals even if it shouldn't
1790 *
1791 * Returns 0 if a subject with the object's smack could
1792 * write to the task, an error code otherwise.
1793 */
1794static int smack_file_send_sigiotask(struct task_struct *tsk,
1795 struct fown_struct *fown, int signum)
1796{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001797 struct smack_known *skp;
1798 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001799 struct file *file;
1800 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001801 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001802
1803 /*
1804 * struct fown_struct is never outside the context of a struct file
1805 */
1806 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001807
Etienne Bassetecfcc532009-04-08 20:40:06 +02001808 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001809 skp = file->f_security;
1810 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1811 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001812 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001813 rc = 0;
1814
1815 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1816 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001817 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001818 return rc;
1819}
1820
1821/**
1822 * smack_file_receive - Smack file receive check
1823 * @file: the object
1824 *
1825 * Returns 0 if current has access, error code otherwise
1826 */
1827static int smack_file_receive(struct file *file)
1828{
Casey Schauflerd166c802014-08-27 14:51:27 -07001829 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001830 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001831 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001832 struct inode *inode = file_inode(file);
Casey Schauflere114e472008-02-04 22:29:50 -08001833
Seung-Woo Kim97775822015-04-17 15:25:04 +09001834 if (unlikely(IS_PRIVATE(inode)))
1835 return 0;
1836
Casey Schaufler4482a442013-12-30 17:37:45 -08001837 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001838 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001839 /*
1840 * This code relies on bitmasks.
1841 */
1842 if (file->f_mode & FMODE_READ)
1843 may = MAY_READ;
1844 if (file->f_mode & FMODE_WRITE)
1845 may |= MAY_WRITE;
1846
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001847 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001848 rc = smk_bu_file(file, may, rc);
1849 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001850}
1851
Casey Schaufler531f1d42011-09-19 12:41:42 -07001852/**
Eric Paris83d49852012-04-04 13:45:40 -04001853 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001854 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001855 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001856 *
1857 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001858 * Allow the open only if the task has read access. There are
1859 * many read operations (e.g. fstat) that you can do with an
1860 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001861 *
1862 * Returns 0
1863 */
Eric Paris83d49852012-04-04 13:45:40 -04001864static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001865{
Casey Schauflera6834c02014-04-21 11:10:26 -07001866 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001867 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001868 struct smk_audit_info ad;
1869 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001870
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001871 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001872 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001873
Casey Schauflera6834c02014-04-21 11:10:26 -07001874 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1875 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001876 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001877 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001878
1879 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001880}
1881
Casey Schauflere114e472008-02-04 22:29:50 -08001882/*
1883 * Task hooks
1884 */
1885
1886/**
David Howellsee18d642009-09-02 09:14:21 +01001887 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1888 * @new: the new credentials
1889 * @gfp: the atomicity of any memory allocations
1890 *
1891 * Prepare a blank set of credentials for modification. This must allocate all
1892 * the memory the LSM module might require such that cred_transfer() can
1893 * complete without error.
1894 */
1895static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1896{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001897 struct task_smack *tsp;
1898
1899 tsp = new_task_smack(NULL, NULL, gfp);
1900 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001901 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001902
1903 cred->security = tsp;
1904
David Howellsee18d642009-09-02 09:14:21 +01001905 return 0;
1906}
1907
1908
1909/**
David Howellsf1752ee2008-11-14 10:39:17 +11001910 * smack_cred_free - "free" task-level security credentials
1911 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001912 *
Casey Schauflere114e472008-02-04 22:29:50 -08001913 */
David Howellsf1752ee2008-11-14 10:39:17 +11001914static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001915{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001916 struct task_smack *tsp = cred->security;
1917 struct smack_rule *rp;
1918 struct list_head *l;
1919 struct list_head *n;
1920
1921 if (tsp == NULL)
1922 return;
1923 cred->security = NULL;
1924
1925 list_for_each_safe(l, n, &tsp->smk_rules) {
1926 rp = list_entry(l, struct smack_rule, list);
1927 list_del(&rp->list);
1928 kfree(rp);
1929 }
1930 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001931}
1932
1933/**
David Howellsd84f4f92008-11-14 10:39:23 +11001934 * smack_cred_prepare - prepare new set of credentials for modification
1935 * @new: the new credentials
1936 * @old: the original credentials
1937 * @gfp: the atomicity of any memory allocations
1938 *
1939 * Prepare a new set of credentials for modification.
1940 */
1941static int smack_cred_prepare(struct cred *new, const struct cred *old,
1942 gfp_t gfp)
1943{
Casey Schaufler676dac42010-12-02 06:43:39 -08001944 struct task_smack *old_tsp = old->security;
1945 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001946 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001947
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001948 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001949 if (new_tsp == NULL)
1950 return -ENOMEM;
1951
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001952 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1953 if (rc != 0)
1954 return rc;
1955
Casey Schaufler676dac42010-12-02 06:43:39 -08001956 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001957 return 0;
1958}
1959
Randy Dunlap251a2a92009-02-18 11:42:33 -08001960/**
David Howellsee18d642009-09-02 09:14:21 +01001961 * smack_cred_transfer - Transfer the old credentials to the new credentials
1962 * @new: the new credentials
1963 * @old: the original credentials
1964 *
1965 * Fill in a set of blank credentials from another set of credentials.
1966 */
1967static void smack_cred_transfer(struct cred *new, const struct cred *old)
1968{
Casey Schaufler676dac42010-12-02 06:43:39 -08001969 struct task_smack *old_tsp = old->security;
1970 struct task_smack *new_tsp = new->security;
1971
1972 new_tsp->smk_task = old_tsp->smk_task;
1973 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001974 mutex_init(&new_tsp->smk_rules_lock);
1975 INIT_LIST_HEAD(&new_tsp->smk_rules);
1976
1977
1978 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001979}
1980
1981/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001982 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001983 * @new: points to the set of credentials to be modified.
1984 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001985 *
1986 * Set the security data for a kernel service.
1987 */
1988static int smack_kernel_act_as(struct cred *new, u32 secid)
1989{
Casey Schaufler676dac42010-12-02 06:43:39 -08001990 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001991 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001992
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001993 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001994 return -EINVAL;
1995
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001996 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001997 return 0;
1998}
1999
2000/**
2001 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002002 * @new: points to the set of credentials to be modified
2003 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002004 *
2005 * Set the file creation context in a set of credentials to the same
2006 * as the objective context of the specified inode
2007 */
2008static int smack_kernel_create_files_as(struct cred *new,
2009 struct inode *inode)
2010{
2011 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08002012 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11002013
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002014 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002015 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002016 return 0;
2017}
2018
2019/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002020 * smk_curacc_on_task - helper to log task related access
2021 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002022 * @access: the access requested
2023 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002024 *
2025 * Return 0 if access is permitted
2026 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002027static int smk_curacc_on_task(struct task_struct *p, int access,
2028 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002029{
2030 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002031 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002032 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002033
Casey Schaufler531f1d42011-09-19 12:41:42 -07002034 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002035 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002036 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002037 rc = smk_bu_task(p, access, rc);
2038 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002039}
2040
2041/**
Casey Schauflere114e472008-02-04 22:29:50 -08002042 * smack_task_setpgid - Smack check on setting pgid
2043 * @p: the task object
2044 * @pgid: unused
2045 *
2046 * Return 0 if write access is permitted
2047 */
2048static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2049{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002050 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002051}
2052
2053/**
2054 * smack_task_getpgid - Smack access check for getpgid
2055 * @p: the object task
2056 *
2057 * Returns 0 if current can read the object task, error code otherwise
2058 */
2059static int smack_task_getpgid(struct task_struct *p)
2060{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002061 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002062}
2063
2064/**
2065 * smack_task_getsid - Smack access check for getsid
2066 * @p: the object task
2067 *
2068 * Returns 0 if current can read the object task, error code otherwise
2069 */
2070static int smack_task_getsid(struct task_struct *p)
2071{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002072 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002073}
2074
2075/**
2076 * smack_task_getsecid - get the secid of the task
2077 * @p: the object task
2078 * @secid: where to put the result
2079 *
2080 * Sets the secid to contain a u32 version of the smack label.
2081 */
2082static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2083{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002084 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002085
2086 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002087}
2088
2089/**
2090 * smack_task_setnice - Smack check on setting nice
2091 * @p: the task object
2092 * @nice: unused
2093 *
2094 * Return 0 if write access is permitted
2095 */
2096static int smack_task_setnice(struct task_struct *p, int nice)
2097{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002098 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002099}
2100
2101/**
2102 * smack_task_setioprio - Smack check on setting ioprio
2103 * @p: the task object
2104 * @ioprio: unused
2105 *
2106 * Return 0 if write access is permitted
2107 */
2108static int smack_task_setioprio(struct task_struct *p, int ioprio)
2109{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002110 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002111}
2112
2113/**
2114 * smack_task_getioprio - Smack check on reading ioprio
2115 * @p: the task object
2116 *
2117 * Return 0 if read access is permitted
2118 */
2119static int smack_task_getioprio(struct task_struct *p)
2120{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002121 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002122}
2123
2124/**
2125 * smack_task_setscheduler - Smack check on setting scheduler
2126 * @p: the task object
2127 * @policy: unused
2128 * @lp: unused
2129 *
2130 * Return 0 if read access is permitted
2131 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002132static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002133{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002134 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002135}
2136
2137/**
2138 * smack_task_getscheduler - Smack check on reading scheduler
2139 * @p: the task object
2140 *
2141 * Return 0 if read access is permitted
2142 */
2143static int smack_task_getscheduler(struct task_struct *p)
2144{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002145 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002146}
2147
2148/**
2149 * smack_task_movememory - Smack check on moving memory
2150 * @p: the task object
2151 *
2152 * Return 0 if write access is permitted
2153 */
2154static int smack_task_movememory(struct task_struct *p)
2155{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002156 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002157}
2158
2159/**
2160 * smack_task_kill - Smack check on signal delivery
2161 * @p: the task object
2162 * @info: unused
2163 * @sig: unused
2164 * @secid: identifies the smack to use in lieu of current's
2165 *
2166 * Return 0 if write access is permitted
2167 *
2168 * The secid behavior is an artifact of an SELinux hack
2169 * in the USB code. Someday it may go away.
2170 */
2171static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2172 int sig, u32 secid)
2173{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002174 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002175 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002176 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002177 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002178
2179 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2180 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002181 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002182 * Sending a signal requires that the sender
2183 * can write the receiver.
2184 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002185 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002186 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002187 rc = smk_bu_task(p, MAY_WRITE, rc);
2188 return rc;
2189 }
Casey Schauflere114e472008-02-04 22:29:50 -08002190 /*
2191 * If the secid isn't 0 we're dealing with some USB IO
2192 * specific behavior. This is not clean. For one thing
2193 * we can't take privilege into account.
2194 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002195 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002196 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2197 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002198 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002199}
2200
2201/**
2202 * smack_task_wait - Smack access check for waiting
2203 * @p: task to wait for
2204 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002205 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002206 */
2207static int smack_task_wait(struct task_struct *p)
2208{
Casey Schauflere114e472008-02-04 22:29:50 -08002209 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002210 * Allow the operation to succeed.
2211 * Zombies are bad.
2212 * In userless environments (e.g. phones) programs
2213 * get marked with SMACK64EXEC and even if the parent
2214 * and child shouldn't be talking the parent still
2215 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002216 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002217 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002218}
2219
2220/**
2221 * smack_task_to_inode - copy task smack into the inode blob
2222 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002223 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002224 *
2225 * Sets the smack pointer in the inode security blob
2226 */
2227static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2228{
2229 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002230 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002231
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002232 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002233}
2234
2235/*
2236 * Socket hooks.
2237 */
2238
2239/**
2240 * smack_sk_alloc_security - Allocate a socket blob
2241 * @sk: the socket
2242 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002243 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002244 *
2245 * Assign Smack pointers to current
2246 *
2247 * Returns 0 on success, -ENOMEM is there's no memory
2248 */
2249static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2250{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002251 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002252 struct socket_smack *ssp;
2253
2254 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2255 if (ssp == NULL)
2256 return -ENOMEM;
2257
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002258 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002259 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002260 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002261
2262 sk->sk_security = ssp;
2263
2264 return 0;
2265}
2266
2267/**
2268 * smack_sk_free_security - Free a socket blob
2269 * @sk: the socket
2270 *
2271 * Clears the blob pointer
2272 */
2273static void smack_sk_free_security(struct sock *sk)
2274{
2275 kfree(sk->sk_security);
2276}
2277
2278/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002279* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002280* @sip: the object end
2281*
2282* looks for host based access restrictions
2283*
2284* This version will only be appropriate for really small sets of single label
2285* hosts. The caller is responsible for ensuring that the RCU read lock is
2286* taken before calling this function.
2287*
2288* Returns the label of the far end or NULL if it's not special.
2289*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002290static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002291{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002292 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002293 struct in_addr *siap = &sip->sin_addr;
2294
2295 if (siap->s_addr == 0)
2296 return NULL;
2297
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002298 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2299 /*
2300 * we break after finding the first match because
2301 * the list is sorted from longest to shortest mask
2302 * so we have found the most specific match
2303 */
2304 if (snp->smk_host.s_addr ==
2305 (siap->s_addr & snp->smk_mask.s_addr))
2306 return snp->smk_label;
2307
2308 return NULL;
2309}
2310
2311#if IS_ENABLED(CONFIG_IPV6)
2312/*
2313 * smk_ipv6_localhost - Check for local ipv6 host address
2314 * @sip: the address
2315 *
2316 * Returns boolean true if this is the localhost address
2317 */
2318static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2319{
2320 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2321 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2322
2323 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2324 ntohs(be16p[7]) == 1)
2325 return true;
2326 return false;
2327}
2328
2329/**
2330* smack_ipv6host_label - check host based restrictions
2331* @sip: the object end
2332*
2333* looks for host based access restrictions
2334*
2335* This version will only be appropriate for really small sets of single label
2336* hosts. The caller is responsible for ensuring that the RCU read lock is
2337* taken before calling this function.
2338*
2339* Returns the label of the far end or NULL if it's not special.
2340*/
2341static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2342{
2343 struct smk_net6addr *snp;
2344 struct in6_addr *sap = &sip->sin6_addr;
2345 int i;
2346 int found = 0;
2347
2348 /*
2349 * It's local. Don't look for a host label.
2350 */
2351 if (smk_ipv6_localhost(sip))
2352 return NULL;
2353
2354 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002355 /*
2356 * we break after finding the first match because
2357 * the list is sorted from longest to shortest mask
2358 * so we have found the most specific match
2359 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002360 for (found = 1, i = 0; i < 8; i++) {
2361 /*
2362 * If the label is NULL the entry has
2363 * been renounced. Ignore it.
2364 */
2365 if (snp->smk_label == NULL)
2366 continue;
2367 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2368 snp->smk_host.s6_addr16[i]) {
2369 found = 0;
2370 break;
2371 }
Etienne Basset43031542009-03-27 17:11:01 -04002372 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002373 if (found)
2374 return snp->smk_label;
2375 }
Paul Moore07feee82009-03-27 17:10:54 -04002376
2377 return NULL;
2378}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002379#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002380
2381/**
Casey Schauflere114e472008-02-04 22:29:50 -08002382 * smack_netlabel - Set the secattr on a socket
2383 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002384 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002385 *
2386 * Convert the outbound smack value (smk_out) to a
2387 * secattr and attach it to the socket.
2388 *
2389 * Returns 0 on success or an error code
2390 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002391static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002392{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002393 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002394 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002395 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002396
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002397 /*
2398 * Usually the netlabel code will handle changing the
2399 * packet labeling based on the label.
2400 * The case of a single label host is different, because
2401 * a single label host should never get a labeled packet
2402 * even though the label is usually associated with a packet
2403 * label.
2404 */
2405 local_bh_disable();
2406 bh_lock_sock_nested(sk);
2407
2408 if (ssp->smk_out == smack_net_ambient ||
2409 labeled == SMACK_UNLABELED_SOCKET)
2410 netlbl_sock_delattr(sk);
2411 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002412 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002413 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002414 }
2415
2416 bh_unlock_sock(sk);
2417 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002418
Casey Schauflere114e472008-02-04 22:29:50 -08002419 return rc;
2420}
2421
2422/**
Paul Moore07feee82009-03-27 17:10:54 -04002423 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2424 * @sk: the socket
2425 * @sap: the destination address
2426 *
2427 * Set the correct secattr for the given socket based on the destination
2428 * address and perform any outbound access checks needed.
2429 *
2430 * Returns 0 on success or an error code.
2431 *
2432 */
2433static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2434{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002435 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002436 int rc;
2437 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002438 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002439 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002440 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002441
2442 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002443 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002444 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002445#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002446 struct lsm_network_audit net;
2447
Eric Paris48c62af2012-04-02 13:15:44 -04002448 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2449 ad.a.u.net->family = sap->sin_family;
2450 ad.a.u.net->dport = sap->sin_port;
2451 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002452#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002453 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002454 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002455 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2456 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002457 } else {
2458 sk_lbl = SMACK_CIPSO_SOCKET;
2459 rc = 0;
2460 }
2461 rcu_read_unlock();
2462 if (rc != 0)
2463 return rc;
2464
2465 return smack_netlabel(sk, sk_lbl);
2466}
2467
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002468#if IS_ENABLED(CONFIG_IPV6)
2469/**
2470 * smk_ipv6_check - check Smack access
2471 * @subject: subject Smack label
2472 * @object: object Smack label
2473 * @address: address
2474 * @act: the action being taken
2475 *
2476 * Check an IPv6 access
2477 */
2478static int smk_ipv6_check(struct smack_known *subject,
2479 struct smack_known *object,
2480 struct sockaddr_in6 *address, int act)
2481{
2482#ifdef CONFIG_AUDIT
2483 struct lsm_network_audit net;
2484#endif
2485 struct smk_audit_info ad;
2486 int rc;
2487
2488#ifdef CONFIG_AUDIT
2489 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2490 ad.a.u.net->family = PF_INET6;
2491 ad.a.u.net->dport = ntohs(address->sin6_port);
2492 if (act == SMK_RECEIVING)
2493 ad.a.u.net->v6info.saddr = address->sin6_addr;
2494 else
2495 ad.a.u.net->v6info.daddr = address->sin6_addr;
2496#endif
2497 rc = smk_access(subject, object, MAY_WRITE, &ad);
2498 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2499 return rc;
2500}
2501#endif /* CONFIG_IPV6 */
2502
2503#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002504/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002505 * smk_ipv6_port_label - Smack port access table management
2506 * @sock: socket
2507 * @address: address
2508 *
2509 * Create or update the port list entry
2510 */
2511static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2512{
2513 struct sock *sk = sock->sk;
2514 struct sockaddr_in6 *addr6;
2515 struct socket_smack *ssp = sock->sk->sk_security;
2516 struct smk_port_label *spp;
2517 unsigned short port = 0;
2518
2519 if (address == NULL) {
2520 /*
2521 * This operation is changing the Smack information
2522 * on the bound socket. Take the changes to the port
2523 * as well.
2524 */
2525 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2526 if (sk != spp->smk_sock)
2527 continue;
2528 spp->smk_in = ssp->smk_in;
2529 spp->smk_out = ssp->smk_out;
2530 return;
2531 }
2532 /*
2533 * A NULL address is only used for updating existing
2534 * bound entries. If there isn't one, it's OK.
2535 */
2536 return;
2537 }
2538
2539 addr6 = (struct sockaddr_in6 *)address;
2540 port = ntohs(addr6->sin6_port);
2541 /*
2542 * This is a special case that is safely ignored.
2543 */
2544 if (port == 0)
2545 return;
2546
2547 /*
2548 * Look for an existing port list entry.
2549 * This is an indication that a port is getting reused.
2550 */
2551 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2552 if (spp->smk_port != port)
2553 continue;
2554 spp->smk_port = port;
2555 spp->smk_sock = sk;
2556 spp->smk_in = ssp->smk_in;
2557 spp->smk_out = ssp->smk_out;
2558 return;
2559 }
2560
2561 /*
2562 * A new port entry is required.
2563 */
2564 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2565 if (spp == NULL)
2566 return;
2567
2568 spp->smk_port = port;
2569 spp->smk_sock = sk;
2570 spp->smk_in = ssp->smk_in;
2571 spp->smk_out = ssp->smk_out;
2572
2573 list_add(&spp->list, &smk_ipv6_port_list);
2574 return;
2575}
2576
2577/**
2578 * smk_ipv6_port_check - check Smack port access
2579 * @sock: socket
2580 * @address: address
2581 *
2582 * Create or update the port list entry
2583 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002584static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002585 int act)
2586{
Casey Schauflerc6739442013-05-22 18:42:56 -07002587 struct smk_port_label *spp;
2588 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002589 struct smack_known *skp = NULL;
2590 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002591 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002592
2593 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002594 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002595 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002596 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002597 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002598 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002599 }
2600
2601 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002602 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002603 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002604 if (skp != NULL && object != NULL)
2605 return smk_ipv6_check(skp, object, address, act);
2606 if (skp == NULL)
2607 skp = smack_net_ambient;
2608 if (object == NULL)
2609 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002610
2611 /*
2612 * It's remote, so port lookup does no good.
2613 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002614 if (!smk_ipv6_localhost(address))
2615 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002616
2617 /*
2618 * It's local so the send check has to have passed.
2619 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002620 if (act == SMK_RECEIVING)
2621 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002622
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002623 port = ntohs(address->sin6_port);
Casey Schauflerc6739442013-05-22 18:42:56 -07002624 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2625 if (spp->smk_port != port)
2626 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002627 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002628 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002629 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002630 break;
2631 }
2632
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002633 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002634}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002635#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002636
2637/**
Casey Schauflere114e472008-02-04 22:29:50 -08002638 * smack_inode_setsecurity - set smack xattrs
2639 * @inode: the object
2640 * @name: attribute name
2641 * @value: attribute value
2642 * @size: size of the attribute
2643 * @flags: unused
2644 *
2645 * Sets the named attribute in the appropriate blob
2646 *
2647 * Returns 0 on success, or an error code
2648 */
2649static int smack_inode_setsecurity(struct inode *inode, const char *name,
2650 const void *value, size_t size, int flags)
2651{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002652 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002653 struct inode_smack *nsp = inode->i_security;
2654 struct socket_smack *ssp;
2655 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002656 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002657
Casey Schauflerf7112e62012-05-06 15:22:02 -07002658 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302659 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002660
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002661 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002662 if (IS_ERR(skp))
2663 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002664
2665 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002666 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002667 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002668 return 0;
2669 }
2670 /*
2671 * The rest of the Smack xattrs are only on sockets.
2672 */
2673 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2674 return -EOPNOTSUPP;
2675
2676 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002677 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002678 return -EOPNOTSUPP;
2679
2680 ssp = sock->sk->sk_security;
2681
2682 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002683 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002684 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002685 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002686 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002687 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2688 if (rc != 0)
2689 printk(KERN_WARNING
2690 "Smack: \"%s\" netlbl error %d.\n",
2691 __func__, -rc);
2692 }
Casey Schauflere114e472008-02-04 22:29:50 -08002693 } else
2694 return -EOPNOTSUPP;
2695
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002696#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002697 if (sock->sk->sk_family == PF_INET6)
2698 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002699#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002700
Casey Schauflere114e472008-02-04 22:29:50 -08002701 return 0;
2702}
2703
2704/**
2705 * smack_socket_post_create - finish socket setup
2706 * @sock: the socket
2707 * @family: protocol family
2708 * @type: unused
2709 * @protocol: unused
2710 * @kern: unused
2711 *
2712 * Sets the netlabel information on the socket
2713 *
2714 * Returns 0 on success, and error code otherwise
2715 */
2716static int smack_socket_post_create(struct socket *sock, int family,
2717 int type, int protocol, int kern)
2718{
Marcin Lis74123012015-01-22 15:40:33 +01002719 struct socket_smack *ssp;
2720
2721 if (sock->sk == NULL)
2722 return 0;
2723
2724 /*
2725 * Sockets created by kernel threads receive web label.
2726 */
2727 if (unlikely(current->flags & PF_KTHREAD)) {
2728 ssp = sock->sk->sk_security;
2729 ssp->smk_in = &smack_known_web;
2730 ssp->smk_out = &smack_known_web;
2731 }
2732
2733 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002734 return 0;
2735 /*
2736 * Set the outbound netlbl.
2737 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002738 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2739}
2740
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002741#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002742/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002743 * smack_socket_bind - record port binding information.
2744 * @sock: the socket
2745 * @address: the port address
2746 * @addrlen: size of the address
2747 *
2748 * Records the label bound to a port.
2749 *
2750 * Returns 0
2751 */
2752static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2753 int addrlen)
2754{
2755 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2756 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002757 return 0;
2758}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002759#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002760
2761/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002762 * smack_socket_connect - connect access check
2763 * @sock: the socket
2764 * @sap: the other end
2765 * @addrlen: size of sap
2766 *
2767 * Verifies that a connection may be possible
2768 *
2769 * Returns 0 on success, and error code otherwise
2770 */
2771static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2772 int addrlen)
2773{
Casey Schauflerc6739442013-05-22 18:42:56 -07002774 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002775#if IS_ENABLED(CONFIG_IPV6)
2776 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2777#endif
2778#ifdef SMACK_IPV6_SECMARK_LABELING
2779 struct smack_known *rsp;
2780 struct socket_smack *ssp = sock->sk->sk_security;
2781#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002782
Casey Schauflerc6739442013-05-22 18:42:56 -07002783 if (sock->sk == NULL)
2784 return 0;
2785
2786 switch (sock->sk->sk_family) {
2787 case PF_INET:
2788 if (addrlen < sizeof(struct sockaddr_in))
2789 return -EINVAL;
2790 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2791 break;
2792 case PF_INET6:
2793 if (addrlen < sizeof(struct sockaddr_in6))
2794 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002795#ifdef SMACK_IPV6_SECMARK_LABELING
2796 rsp = smack_ipv6host_label(sip);
2797 if (rsp != NULL)
2798 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002799 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002800#endif
2801#ifdef SMACK_IPV6_PORT_LABELING
2802 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2803#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002804 break;
2805 }
2806 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002807}
2808
2809/**
2810 * smack_flags_to_may - convert S_ to MAY_ values
2811 * @flags: the S_ value
2812 *
2813 * Returns the equivalent MAY_ value
2814 */
2815static int smack_flags_to_may(int flags)
2816{
2817 int may = 0;
2818
2819 if (flags & S_IRUGO)
2820 may |= MAY_READ;
2821 if (flags & S_IWUGO)
2822 may |= MAY_WRITE;
2823 if (flags & S_IXUGO)
2824 may |= MAY_EXEC;
2825
2826 return may;
2827}
2828
2829/**
2830 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2831 * @msg: the object
2832 *
2833 * Returns 0
2834 */
2835static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2836{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002837 struct smack_known *skp = smk_of_current();
2838
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002839 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002840 return 0;
2841}
2842
2843/**
2844 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2845 * @msg: the object
2846 *
2847 * Clears the blob pointer
2848 */
2849static void smack_msg_msg_free_security(struct msg_msg *msg)
2850{
2851 msg->security = NULL;
2852}
2853
2854/**
2855 * smack_of_shm - the smack pointer for the shm
2856 * @shp: the object
2857 *
2858 * Returns a pointer to the smack value
2859 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002860static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002861{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002862 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002863}
2864
2865/**
2866 * smack_shm_alloc_security - Set the security blob for shm
2867 * @shp: the object
2868 *
2869 * Returns 0
2870 */
2871static int smack_shm_alloc_security(struct shmid_kernel *shp)
2872{
2873 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002874 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002875
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002876 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002877 return 0;
2878}
2879
2880/**
2881 * smack_shm_free_security - Clear the security blob for shm
2882 * @shp: the object
2883 *
2884 * Clears the blob pointer
2885 */
2886static void smack_shm_free_security(struct shmid_kernel *shp)
2887{
2888 struct kern_ipc_perm *isp = &shp->shm_perm;
2889
2890 isp->security = NULL;
2891}
2892
2893/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002894 * smk_curacc_shm : check if current has access on shm
2895 * @shp : the object
2896 * @access : access requested
2897 *
2898 * Returns 0 if current has the requested access, error code otherwise
2899 */
2900static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2901{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002902 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002903 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002904 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002905
2906#ifdef CONFIG_AUDIT
2907 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2908 ad.a.u.ipc_id = shp->shm_perm.id;
2909#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002910 rc = smk_curacc(ssp, access, &ad);
2911 rc = smk_bu_current("shm", ssp, access, rc);
2912 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002913}
2914
2915/**
Casey Schauflere114e472008-02-04 22:29:50 -08002916 * smack_shm_associate - Smack access check for shm
2917 * @shp: the object
2918 * @shmflg: access requested
2919 *
2920 * Returns 0 if current has the requested access, error code otherwise
2921 */
2922static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2923{
Casey Schauflere114e472008-02-04 22:29:50 -08002924 int may;
2925
2926 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002927 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002928}
2929
2930/**
2931 * smack_shm_shmctl - Smack access check for shm
2932 * @shp: the object
2933 * @cmd: what it wants to do
2934 *
2935 * Returns 0 if current has the requested access, error code otherwise
2936 */
2937static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2938{
Casey Schauflere114e472008-02-04 22:29:50 -08002939 int may;
2940
2941 switch (cmd) {
2942 case IPC_STAT:
2943 case SHM_STAT:
2944 may = MAY_READ;
2945 break;
2946 case IPC_SET:
2947 case SHM_LOCK:
2948 case SHM_UNLOCK:
2949 case IPC_RMID:
2950 may = MAY_READWRITE;
2951 break;
2952 case IPC_INFO:
2953 case SHM_INFO:
2954 /*
2955 * System level information.
2956 */
2957 return 0;
2958 default:
2959 return -EINVAL;
2960 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002961 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002962}
2963
2964/**
2965 * smack_shm_shmat - Smack access for shmat
2966 * @shp: the object
2967 * @shmaddr: unused
2968 * @shmflg: access requested
2969 *
2970 * Returns 0 if current has the requested access, error code otherwise
2971 */
2972static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2973 int shmflg)
2974{
Casey Schauflere114e472008-02-04 22:29:50 -08002975 int may;
2976
2977 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002978 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002979}
2980
2981/**
2982 * smack_of_sem - the smack pointer for the sem
2983 * @sma: the object
2984 *
2985 * Returns a pointer to the smack value
2986 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002987static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002988{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002989 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002990}
2991
2992/**
2993 * smack_sem_alloc_security - Set the security blob for sem
2994 * @sma: the object
2995 *
2996 * Returns 0
2997 */
2998static int smack_sem_alloc_security(struct sem_array *sma)
2999{
3000 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003001 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003002
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003003 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003004 return 0;
3005}
3006
3007/**
3008 * smack_sem_free_security - Clear the security blob for sem
3009 * @sma: the object
3010 *
3011 * Clears the blob pointer
3012 */
3013static void smack_sem_free_security(struct sem_array *sma)
3014{
3015 struct kern_ipc_perm *isp = &sma->sem_perm;
3016
3017 isp->security = NULL;
3018}
3019
3020/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003021 * smk_curacc_sem : check if current has access on sem
3022 * @sma : the object
3023 * @access : access requested
3024 *
3025 * Returns 0 if current has the requested access, error code otherwise
3026 */
3027static int smk_curacc_sem(struct sem_array *sma, int access)
3028{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003029 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003030 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003031 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032
3033#ifdef CONFIG_AUDIT
3034 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3035 ad.a.u.ipc_id = sma->sem_perm.id;
3036#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003037 rc = smk_curacc(ssp, access, &ad);
3038 rc = smk_bu_current("sem", ssp, access, rc);
3039 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003040}
3041
3042/**
Casey Schauflere114e472008-02-04 22:29:50 -08003043 * smack_sem_associate - Smack access check for sem
3044 * @sma: the object
3045 * @semflg: access requested
3046 *
3047 * Returns 0 if current has the requested access, error code otherwise
3048 */
3049static int smack_sem_associate(struct sem_array *sma, int semflg)
3050{
Casey Schauflere114e472008-02-04 22:29:50 -08003051 int may;
3052
3053 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003054 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003055}
3056
3057/**
3058 * smack_sem_shmctl - Smack access check for sem
3059 * @sma: the object
3060 * @cmd: what it wants to do
3061 *
3062 * Returns 0 if current has the requested access, error code otherwise
3063 */
3064static int smack_sem_semctl(struct sem_array *sma, int cmd)
3065{
Casey Schauflere114e472008-02-04 22:29:50 -08003066 int may;
3067
3068 switch (cmd) {
3069 case GETPID:
3070 case GETNCNT:
3071 case GETZCNT:
3072 case GETVAL:
3073 case GETALL:
3074 case IPC_STAT:
3075 case SEM_STAT:
3076 may = MAY_READ;
3077 break;
3078 case SETVAL:
3079 case SETALL:
3080 case IPC_RMID:
3081 case IPC_SET:
3082 may = MAY_READWRITE;
3083 break;
3084 case IPC_INFO:
3085 case SEM_INFO:
3086 /*
3087 * System level information
3088 */
3089 return 0;
3090 default:
3091 return -EINVAL;
3092 }
3093
Etienne Bassetecfcc532009-04-08 20:40:06 +02003094 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003095}
3096
3097/**
3098 * smack_sem_semop - Smack checks of semaphore operations
3099 * @sma: the object
3100 * @sops: unused
3101 * @nsops: unused
3102 * @alter: unused
3103 *
3104 * Treated as read and write in all cases.
3105 *
3106 * Returns 0 if access is allowed, error code otherwise
3107 */
3108static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
3109 unsigned nsops, int alter)
3110{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003111 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003112}
3113
3114/**
3115 * smack_msg_alloc_security - Set the security blob for msg
3116 * @msq: the object
3117 *
3118 * Returns 0
3119 */
3120static int smack_msg_queue_alloc_security(struct msg_queue *msq)
3121{
3122 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003123 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003124
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003125 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003126 return 0;
3127}
3128
3129/**
3130 * smack_msg_free_security - Clear the security blob for msg
3131 * @msq: the object
3132 *
3133 * Clears the blob pointer
3134 */
3135static void smack_msg_queue_free_security(struct msg_queue *msq)
3136{
3137 struct kern_ipc_perm *kisp = &msq->q_perm;
3138
3139 kisp->security = NULL;
3140}
3141
3142/**
3143 * smack_of_msq - the smack pointer for the msq
3144 * @msq: the object
3145 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003146 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08003147 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003148static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08003149{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003150 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08003151}
3152
3153/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003154 * smk_curacc_msq : helper to check if current has access on msq
3155 * @msq : the msq
3156 * @access : access requested
3157 *
3158 * return 0 if current has access, error otherwise
3159 */
3160static int smk_curacc_msq(struct msg_queue *msq, int access)
3161{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003162 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003163 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003164 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003165
3166#ifdef CONFIG_AUDIT
3167 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3168 ad.a.u.ipc_id = msq->q_perm.id;
3169#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003170 rc = smk_curacc(msp, access, &ad);
3171 rc = smk_bu_current("msq", msp, access, rc);
3172 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003173}
3174
3175/**
Casey Schauflere114e472008-02-04 22:29:50 -08003176 * smack_msg_queue_associate - Smack access check for msg_queue
3177 * @msq: the object
3178 * @msqflg: access requested
3179 *
3180 * Returns 0 if current has the requested access, error code otherwise
3181 */
3182static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
3183{
Casey Schauflere114e472008-02-04 22:29:50 -08003184 int may;
3185
3186 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003187 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003188}
3189
3190/**
3191 * smack_msg_queue_msgctl - Smack access check for msg_queue
3192 * @msq: the object
3193 * @cmd: what it wants to do
3194 *
3195 * Returns 0 if current has the requested access, error code otherwise
3196 */
3197static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
3198{
Casey Schauflere114e472008-02-04 22:29:50 -08003199 int may;
3200
3201 switch (cmd) {
3202 case IPC_STAT:
3203 case MSG_STAT:
3204 may = MAY_READ;
3205 break;
3206 case IPC_SET:
3207 case IPC_RMID:
3208 may = MAY_READWRITE;
3209 break;
3210 case IPC_INFO:
3211 case MSG_INFO:
3212 /*
3213 * System level information
3214 */
3215 return 0;
3216 default:
3217 return -EINVAL;
3218 }
3219
Etienne Bassetecfcc532009-04-08 20:40:06 +02003220 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003221}
3222
3223/**
3224 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3225 * @msq: the object
3226 * @msg: unused
3227 * @msqflg: access requested
3228 *
3229 * Returns 0 if current has the requested access, error code otherwise
3230 */
3231static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
3232 int msqflg)
3233{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003234 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003235
Etienne Bassetecfcc532009-04-08 20:40:06 +02003236 may = smack_flags_to_may(msqflg);
3237 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003238}
3239
3240/**
3241 * smack_msg_queue_msgsnd - Smack access check for msg_queue
3242 * @msq: the object
3243 * @msg: unused
3244 * @target: unused
3245 * @type: unused
3246 * @mode: unused
3247 *
3248 * Returns 0 if current has read and write access, error code otherwise
3249 */
3250static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3251 struct task_struct *target, long type, int mode)
3252{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003253 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003254}
3255
3256/**
3257 * smack_ipc_permission - Smack access for ipc_permission()
3258 * @ipp: the object permissions
3259 * @flag: access requested
3260 *
3261 * Returns 0 if current has read and write access, error code otherwise
3262 */
3263static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3264{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003265 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003266 int may = smack_flags_to_may(flag);
3267 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003268 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003269
Etienne Bassetecfcc532009-04-08 20:40:06 +02003270#ifdef CONFIG_AUDIT
3271 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3272 ad.a.u.ipc_id = ipp->id;
3273#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003274 rc = smk_curacc(iskp, may, &ad);
3275 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003276 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003277}
3278
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003279/**
3280 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003281 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003282 * @secid: where result will be saved
3283 */
3284static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3285{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003286 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003287
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003288 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003289}
3290
Casey Schauflere114e472008-02-04 22:29:50 -08003291/**
3292 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003293 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003294 * @inode: the object
3295 *
3296 * Set the inode's security blob if it hasn't been done already.
3297 */
3298static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3299{
3300 struct super_block *sbp;
3301 struct superblock_smack *sbsp;
3302 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003303 struct smack_known *skp;
3304 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003305 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003306 char trattr[TRANS_TRUE_SIZE];
3307 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003308 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003309 struct dentry *dp;
3310
3311 if (inode == NULL)
3312 return;
3313
3314 isp = inode->i_security;
3315
3316 mutex_lock(&isp->smk_lock);
3317 /*
3318 * If the inode is already instantiated
3319 * take the quick way out
3320 */
3321 if (isp->smk_flags & SMK_INODE_INSTANT)
3322 goto unlockandout;
3323
3324 sbp = inode->i_sb;
3325 sbsp = sbp->s_security;
3326 /*
3327 * We're going to use the superblock default label
3328 * if there's no label on the file.
3329 */
3330 final = sbsp->smk_default;
3331
3332 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003333 * If this is the root inode the superblock
3334 * may be in the process of initialization.
3335 * If that is the case use the root value out
3336 * of the superblock.
3337 */
3338 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003339 switch (sbp->s_magic) {
3340 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003341 /*
3342 * The cgroup filesystem is never mounted,
3343 * so there's no opportunity to set the mount
3344 * options.
3345 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003346 sbsp->smk_root = &smack_known_star;
3347 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003348 isp->smk_inode = sbsp->smk_root;
3349 break;
3350 case TMPFS_MAGIC:
3351 /*
3352 * What about shmem/tmpfs anonymous files with dentry
3353 * obtained from d_alloc_pseudo()?
3354 */
3355 isp->smk_inode = smk_of_current();
3356 break;
3357 default:
3358 isp->smk_inode = sbsp->smk_root;
3359 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003360 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003361 isp->smk_flags |= SMK_INODE_INSTANT;
3362 goto unlockandout;
3363 }
3364
3365 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003366 * This is pretty hackish.
3367 * Casey says that we shouldn't have to do
3368 * file system specific code, but it does help
3369 * with keeping it simple.
3370 */
3371 switch (sbp->s_magic) {
3372 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003373 case PIPEFS_MAGIC:
3374 case SOCKFS_MAGIC:
3375 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003376 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003377 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003378 * that the smack file system doesn't do
3379 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003380 *
Casey Schauflere114e472008-02-04 22:29:50 -08003381 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003382 *
3383 * Socket access is controlled by the socket
3384 * structures associated with the task involved.
3385 *
3386 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003387 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003388 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003389 break;
3390 case DEVPTS_SUPER_MAGIC:
3391 /*
3392 * devpts seems content with the label of the task.
3393 * Programs that change smack have to treat the
3394 * pty with respect.
3395 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003396 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003397 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003398 case PROC_SUPER_MAGIC:
3399 /*
3400 * Casey says procfs appears not to care.
3401 * The superblock default suffices.
3402 */
3403 break;
3404 case TMPFS_MAGIC:
3405 /*
3406 * Device labels should come from the filesystem,
3407 * but watch out, because they're volitile,
3408 * getting recreated on every reboot.
3409 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003410 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003411 /*
3412 * No break.
3413 *
3414 * If a smack value has been set we want to use it,
3415 * but since tmpfs isn't giving us the opportunity
3416 * to set mount options simulate setting the
3417 * superblock default.
3418 */
3419 default:
3420 /*
3421 * This isn't an understood special case.
3422 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003423 */
3424
3425 /*
3426 * UNIX domain sockets use lower level socket data.
3427 */
3428 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003429 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003430 break;
3431 }
3432 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003433 * No xattr support means, alas, no SMACK label.
3434 * Use the aforeapplied default.
3435 * It would be curious if the label of the task
3436 * does not match that assigned.
3437 */
3438 if (inode->i_op->getxattr == NULL)
3439 break;
3440 /*
3441 * Get the dentry for xattr.
3442 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003443 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003444 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003445 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003446 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003447
3448 /*
3449 * Transmuting directory
3450 */
3451 if (S_ISDIR(inode->i_mode)) {
3452 /*
3453 * If this is a new directory and the label was
3454 * transmuted when the inode was initialized
3455 * set the transmute attribute on the directory
3456 * and mark the inode.
3457 *
3458 * If there is a transmute attribute on the
3459 * directory mark the inode.
3460 */
3461 if (isp->smk_flags & SMK_INODE_CHANGED) {
3462 isp->smk_flags &= ~SMK_INODE_CHANGED;
3463 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003464 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003465 TRANS_TRUE, TRANS_TRUE_SIZE,
3466 0);
3467 } else {
3468 rc = inode->i_op->getxattr(dp,
3469 XATTR_NAME_SMACKTRANSMUTE, trattr,
3470 TRANS_TRUE_SIZE);
3471 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3472 TRANS_TRUE_SIZE) != 0)
3473 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003474 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003475 if (rc >= 0)
3476 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003477 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003478 /*
3479 * Don't let the exec or mmap label be "*" or "@".
3480 */
3481 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003482 if (IS_ERR(skp) || skp == &smack_known_star ||
3483 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003484 skp = NULL;
3485 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003486
Casey Schaufler19760ad2013-12-16 16:27:26 -08003487 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003488 if (IS_ERR(skp) || skp == &smack_known_star ||
3489 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003490 skp = NULL;
3491 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003492
Casey Schauflere114e472008-02-04 22:29:50 -08003493 dput(dp);
3494 break;
3495 }
3496
3497 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003498 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003499 else
3500 isp->smk_inode = final;
3501
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003502 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003503
3504unlockandout:
3505 mutex_unlock(&isp->smk_lock);
3506 return;
3507}
3508
3509/**
3510 * smack_getprocattr - Smack process attribute access
3511 * @p: the object task
3512 * @name: the name of the attribute in /proc/.../attr
3513 * @value: where to put the result
3514 *
3515 * Places a copy of the task Smack into value
3516 *
3517 * Returns the length of the smack label or an error code
3518 */
3519static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3520{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003521 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003522 char *cp;
3523 int slen;
3524
3525 if (strcmp(name, "current") != 0)
3526 return -EINVAL;
3527
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003528 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003529 if (cp == NULL)
3530 return -ENOMEM;
3531
3532 slen = strlen(cp);
3533 *value = cp;
3534 return slen;
3535}
3536
3537/**
3538 * smack_setprocattr - Smack process attribute setting
3539 * @p: the object task
3540 * @name: the name of the attribute in /proc/.../attr
3541 * @value: the value to set
3542 * @size: the size of the value
3543 *
3544 * Sets the Smack value of the task. Only setting self
3545 * is permitted and only with privilege
3546 *
3547 * Returns the length of the smack label or an error code
3548 */
3549static int smack_setprocattr(struct task_struct *p, char *name,
3550 void *value, size_t size)
3551{
Casey Schaufler676dac42010-12-02 06:43:39 -08003552 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003553 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003554 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003555
Casey Schauflere114e472008-02-04 22:29:50 -08003556 /*
3557 * Changing another process' Smack value is too dangerous
3558 * and supports no sane use case.
3559 */
3560 if (p != current)
3561 return -EPERM;
3562
Casey Schaufler1880eff2012-06-05 15:28:30 -07003563 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003564 return -EPERM;
3565
Casey Schauflerf7112e62012-05-06 15:22:02 -07003566 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003567 return -EINVAL;
3568
3569 if (strcmp(name, "current") != 0)
3570 return -EINVAL;
3571
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003572 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003573 if (IS_ERR(skp))
3574 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003575
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003576 /*
3577 * No process is ever allowed the web ("@") label.
3578 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003579 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003580 return -EPERM;
3581
David Howellsd84f4f92008-11-14 10:39:23 +11003582 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003583 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003584 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003585
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003586 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003587 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003588
David Howellsd84f4f92008-11-14 10:39:23 +11003589 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003590 return size;
3591}
3592
3593/**
3594 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003595 * @sock: one sock
3596 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003597 * @newsk: unused
3598 *
3599 * Return 0 if a subject with the smack of sock could access
3600 * an object with the smack of other, otherwise an error code
3601 */
David S. Miller3610cda2011-01-05 15:38:53 -08003602static int smack_unix_stream_connect(struct sock *sock,
3603 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003604{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003605 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003606 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003607 struct socket_smack *ssp = sock->sk_security;
3608 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003609 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003610 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003611 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003612#ifdef CONFIG_AUDIT
3613 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003614#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003615
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003616 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3617 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003618 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003619#ifdef CONFIG_AUDIT
3620 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3621 smk_ad_setfield_u_net_sk(&ad, other);
3622#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003623 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3624 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003625 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003626 okp = osp->smk_out;
3627 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003628 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003629 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003630 MAY_WRITE, rc);
3631 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003632 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003633
Casey Schaufler975d5e52011-09-26 14:43:39 -07003634 /*
3635 * Cross reference the peer labels for SO_PEERSEC.
3636 */
3637 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003638 nsp->smk_packet = ssp->smk_out;
3639 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003640 }
3641
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003642 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003643}
3644
3645/**
3646 * smack_unix_may_send - Smack access on UDS
3647 * @sock: one socket
3648 * @other: the other socket
3649 *
3650 * Return 0 if a subject with the smack of sock could access
3651 * an object with the smack of other, otherwise an error code
3652 */
3653static int smack_unix_may_send(struct socket *sock, struct socket *other)
3654{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003655 struct socket_smack *ssp = sock->sk->sk_security;
3656 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003657 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003658 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003659
Kees Cook923e9a12012-04-10 13:26:44 -07003660#ifdef CONFIG_AUDIT
3661 struct lsm_network_audit net;
3662
Eric Paris48c62af2012-04-02 13:15:44 -04003663 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003664 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003665#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003666
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003667 if (smack_privileged(CAP_MAC_OVERRIDE))
3668 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003669
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003670 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3671 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003672 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003673}
3674
3675/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003676 * smack_socket_sendmsg - Smack check based on destination host
3677 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003678 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003679 * @size: the size of the message
3680 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003681 * Return 0 if the current subject can write to the destination host.
3682 * For IPv4 this is only a question if the destination is a single label host.
3683 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003684 */
3685static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3686 int size)
3687{
3688 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003689#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003690 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003691#endif
3692#ifdef SMACK_IPV6_SECMARK_LABELING
3693 struct socket_smack *ssp = sock->sk->sk_security;
3694 struct smack_known *rsp;
3695#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003696 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003697
3698 /*
3699 * Perfectly reasonable for this to be NULL
3700 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003701 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003702 return 0;
3703
Casey Schauflerc6739442013-05-22 18:42:56 -07003704 switch (sip->sin_family) {
3705 case AF_INET:
3706 rc = smack_netlabel_send(sock->sk, sip);
3707 break;
3708 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003709#ifdef SMACK_IPV6_SECMARK_LABELING
3710 rsp = smack_ipv6host_label(sap);
3711 if (rsp != NULL)
3712 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3713 SMK_CONNECTING);
3714#endif
3715#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003716 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003717#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003718 break;
3719 }
3720 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003721}
3722
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003723/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003724 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003725 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003726 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003727 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003728 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003729 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003730static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3731 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003732{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003733 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003734 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003735 int acat;
3736 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003737
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003738 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003739 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003740 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003741 * If there are flags but no level netlabel isn't
3742 * behaving the way we expect it to.
3743 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003744 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003745 * Without guidance regarding the smack value
3746 * for the packet fall back on the network
3747 * ambient value.
3748 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003749 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003750 list_for_each_entry(skp, &smack_known_list, list) {
3751 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003752 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003753 /*
3754 * Compare the catsets. Use the netlbl APIs.
3755 */
3756 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3757 if ((skp->smk_netlabel.flags &
3758 NETLBL_SECATTR_MLS_CAT) == 0)
3759 found = 1;
3760 break;
3761 }
3762 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003763 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3764 acat + 1);
3765 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003766 skp->smk_netlabel.attr.mls.cat,
3767 kcat + 1);
3768 if (acat < 0 || kcat < 0)
3769 break;
3770 }
3771 if (acat == kcat) {
3772 found = 1;
3773 break;
3774 }
Casey Schauflere114e472008-02-04 22:29:50 -08003775 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003776 rcu_read_unlock();
3777
3778 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003779 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003780
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003781 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003782 return &smack_known_web;
3783 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003784 }
3785 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3786 /*
3787 * Looks like a fallback, which gives us a secid.
3788 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003789 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003790 /*
3791 * This has got to be a bug because it is
3792 * impossible to specify a fallback without
3793 * specifying the label, which will ensure
3794 * it has a secid, and the only way to get a
3795 * secid is from a fallback.
3796 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003797 BUG_ON(skp == NULL);
3798 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003799 }
3800 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003801 * Without guidance regarding the smack value
3802 * for the packet fall back on the network
3803 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003804 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003805 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003806}
3807
Casey Schaufler69f287a2014-12-12 17:08:40 -08003808#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003809static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003810{
Casey Schauflerc6739442013-05-22 18:42:56 -07003811 u8 nexthdr;
3812 int offset;
3813 int proto = -EINVAL;
3814 struct ipv6hdr _ipv6h;
3815 struct ipv6hdr *ip6;
3816 __be16 frag_off;
3817 struct tcphdr _tcph, *th;
3818 struct udphdr _udph, *uh;
3819 struct dccp_hdr _dccph, *dh;
3820
3821 sip->sin6_port = 0;
3822
3823 offset = skb_network_offset(skb);
3824 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3825 if (ip6 == NULL)
3826 return -EINVAL;
3827 sip->sin6_addr = ip6->saddr;
3828
3829 nexthdr = ip6->nexthdr;
3830 offset += sizeof(_ipv6h);
3831 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3832 if (offset < 0)
3833 return -EINVAL;
3834
3835 proto = nexthdr;
3836 switch (proto) {
3837 case IPPROTO_TCP:
3838 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3839 if (th != NULL)
3840 sip->sin6_port = th->source;
3841 break;
3842 case IPPROTO_UDP:
3843 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3844 if (uh != NULL)
3845 sip->sin6_port = uh->source;
3846 break;
3847 case IPPROTO_DCCP:
3848 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3849 if (dh != NULL)
3850 sip->sin6_port = dh->dccph_sport;
3851 break;
3852 }
3853 return proto;
3854}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003855#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003856
Casey Schauflere114e472008-02-04 22:29:50 -08003857/**
3858 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3859 * @sk: socket
3860 * @skb: packet
3861 *
3862 * Returns 0 if the packet should be delivered, an error code otherwise
3863 */
3864static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3865{
3866 struct netlbl_lsm_secattr secattr;
3867 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003868 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003869 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003870 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003871#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003872 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003873#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003874#if IS_ENABLED(CONFIG_IPV6)
3875 struct sockaddr_in6 sadd;
3876 int proto;
3877#endif /* CONFIG_IPV6 */
3878
Casey Schauflerc6739442013-05-22 18:42:56 -07003879 switch (sk->sk_family) {
3880 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003881#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3882 /*
3883 * If there is a secmark use it rather than the CIPSO label.
3884 * If there is no secmark fall back to CIPSO.
3885 * The secmark is assumed to reflect policy better.
3886 */
3887 if (skb && skb->secmark != 0) {
3888 skp = smack_from_secid(skb->secmark);
3889 goto access_check;
3890 }
3891#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003892 /*
3893 * Translate what netlabel gave us.
3894 */
3895 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003896
Casey Schauflerc6739442013-05-22 18:42:56 -07003897 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3898 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003899 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003900 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003901 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003902
Casey Schauflerc6739442013-05-22 18:42:56 -07003903 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003904
Casey Schaufler69f287a2014-12-12 17:08:40 -08003905#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3906access_check:
3907#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003908#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003909 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3910 ad.a.u.net->family = sk->sk_family;
3911 ad.a.u.net->netif = skb->skb_iif;
3912 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003913#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003914 /*
3915 * Receiving a packet requires that the other end
3916 * be able to write here. Read access is not required.
3917 * This is the simplist possible security model
3918 * for networking.
3919 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003920 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3921 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003922 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003923 if (rc != 0)
3924 netlbl_skbuff_err(skb, rc, 0);
3925 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003926#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003927 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003928 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3929 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3930 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003931#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003932 if (skb && skb->secmark != 0)
3933 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003934 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003935 skp = smack_ipv6host_label(&sadd);
3936 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003937 skp = smack_net_ambient;
3938#ifdef CONFIG_AUDIT
3939 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3940 ad.a.u.net->family = sk->sk_family;
3941 ad.a.u.net->netif = skb->skb_iif;
3942 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3943#endif /* CONFIG_AUDIT */
3944 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3945 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3946 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003947#endif /* SMACK_IPV6_SECMARK_LABELING */
3948#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003949 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003950#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07003951 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003952#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003953 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003954
Paul Moorea8134292008-10-10 10:16:31 -04003955 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003956}
3957
3958/**
3959 * smack_socket_getpeersec_stream - pull in packet label
3960 * @sock: the socket
3961 * @optval: user's destination
3962 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003963 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003964 *
3965 * returns zero on success, an error code otherwise
3966 */
3967static int smack_socket_getpeersec_stream(struct socket *sock,
3968 char __user *optval,
3969 int __user *optlen, unsigned len)
3970{
3971 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003972 char *rcp = "";
3973 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003974 int rc = 0;
3975
3976 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003977 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003978 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003979 slen = strlen(rcp) + 1;
3980 }
Casey Schauflere114e472008-02-04 22:29:50 -08003981
3982 if (slen > len)
3983 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003984 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003985 rc = -EFAULT;
3986
3987 if (put_user(slen, optlen) != 0)
3988 rc = -EFAULT;
3989
3990 return rc;
3991}
3992
3993
3994/**
3995 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003996 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003997 * @skb: packet data
3998 * @secid: pointer to where to put the secid of the packet
3999 *
4000 * Sets the netlabel socket state on sk from parent
4001 */
4002static int smack_socket_getpeersec_dgram(struct socket *sock,
4003 struct sk_buff *skb, u32 *secid)
4004
4005{
4006 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004007 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004008 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004009 int family = PF_UNSPEC;
4010 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004011 int rc;
4012
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004013 if (skb != NULL) {
4014 if (skb->protocol == htons(ETH_P_IP))
4015 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004016#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004017 else if (skb->protocol == htons(ETH_P_IPV6))
4018 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004019#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004020 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004021 if (family == PF_UNSPEC && sock != NULL)
4022 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004023
Casey Schaufler69f287a2014-12-12 17:08:40 -08004024 switch (family) {
4025 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004026 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004027 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004028 break;
4029 case PF_INET:
4030#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4031 s = skb->secmark;
4032 if (s != 0)
4033 break;
4034#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004035 /*
4036 * Translate what netlabel gave us.
4037 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004038 if (sock != NULL && sock->sk != NULL)
4039 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004040 netlbl_secattr_init(&secattr);
4041 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4042 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004043 skp = smack_from_secattr(&secattr, ssp);
4044 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004045 }
4046 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004047 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004048 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004049#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004050 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004051#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004052 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004053 }
4054 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004055 if (s == 0)
4056 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004057 return 0;
4058}
4059
4060/**
Paul Moore07feee82009-03-27 17:10:54 -04004061 * smack_sock_graft - Initialize a newly created socket with an existing sock
4062 * @sk: child sock
4063 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004064 *
Paul Moore07feee82009-03-27 17:10:54 -04004065 * Set the smk_{in,out} state of an existing sock based on the process that
4066 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004067 */
4068static void smack_sock_graft(struct sock *sk, struct socket *parent)
4069{
4070 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004071 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004072
Paul Moore07feee82009-03-27 17:10:54 -04004073 if (sk == NULL ||
4074 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004075 return;
4076
4077 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004078 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004079 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004080 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004081}
4082
4083/**
4084 * smack_inet_conn_request - Smack access check on connect
4085 * @sk: socket involved
4086 * @skb: packet
4087 * @req: unused
4088 *
4089 * Returns 0 if a task with the packet label could write to
4090 * the socket, otherwise an error code
4091 */
4092static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4093 struct request_sock *req)
4094{
Paul Moore07feee82009-03-27 17:10:54 -04004095 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004096 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004097 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004098 struct netlbl_lsm_secattr secattr;
4099 struct sockaddr_in addr;
4100 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004101 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004102 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004103 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004104#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004105 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004106#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004107
Casey Schaufler69f287a2014-12-12 17:08:40 -08004108#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004109 if (family == PF_INET6) {
4110 /*
4111 * Handle mapped IPv4 packets arriving
4112 * via IPv6 sockets. Don't set up netlabel
4113 * processing on IPv6.
4114 */
4115 if (skb->protocol == htons(ETH_P_IP))
4116 family = PF_INET;
4117 else
4118 return 0;
4119 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004120#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004121
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004122#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4123 /*
4124 * If there is a secmark use it rather than the CIPSO label.
4125 * If there is no secmark fall back to CIPSO.
4126 * The secmark is assumed to reflect policy better.
4127 */
4128 if (skb && skb->secmark != 0) {
4129 skp = smack_from_secid(skb->secmark);
4130 goto access_check;
4131 }
4132#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4133
Paul Moore07feee82009-03-27 17:10:54 -04004134 netlbl_secattr_init(&secattr);
4135 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004136 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004137 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004138 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004139 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004140 netlbl_secattr_destroy(&secattr);
4141
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004142#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4143access_check:
4144#endif
4145
Etienne Bassetecfcc532009-04-08 20:40:06 +02004146#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004147 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4148 ad.a.u.net->family = family;
4149 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004150 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4151#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004152 /*
Paul Moore07feee82009-03-27 17:10:54 -04004153 * Receiving a packet requires that the other end be able to write
4154 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004155 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004156 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4157 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004158 if (rc != 0)
4159 return rc;
4160
4161 /*
4162 * Save the peer's label in the request_sock so we can later setup
4163 * smk_packet in the child socket so that SO_PEERCRED can report it.
4164 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004165 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004166
4167 /*
4168 * We need to decide if we want to label the incoming connection here
4169 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004170 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004171 */
4172 hdr = ip_hdr(skb);
4173 addr.sin_addr.s_addr = hdr->saddr;
4174 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004175 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004176 rcu_read_unlock();
4177
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004178 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004179 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004180 else
Paul Moore07feee82009-03-27 17:10:54 -04004181 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004182
4183 return rc;
4184}
4185
Paul Moore07feee82009-03-27 17:10:54 -04004186/**
4187 * smack_inet_csk_clone - Copy the connection information to the new socket
4188 * @sk: the new socket
4189 * @req: the connection's request_sock
4190 *
4191 * Transfer the connection's peer label to the newly created socket.
4192 */
4193static void smack_inet_csk_clone(struct sock *sk,
4194 const struct request_sock *req)
4195{
4196 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004197 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004198
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004199 if (req->peer_secid != 0) {
4200 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004201 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004202 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004203 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004204}
4205
Casey Schauflere114e472008-02-04 22:29:50 -08004206/*
4207 * Key management security hooks
4208 *
4209 * Casey has not tested key support very heavily.
4210 * The permission check is most likely too restrictive.
4211 * If you care about keys please have a look.
4212 */
4213#ifdef CONFIG_KEYS
4214
4215/**
4216 * smack_key_alloc - Set the key security blob
4217 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004218 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004219 * @flags: unused
4220 *
4221 * No allocation required
4222 *
4223 * Returns 0
4224 */
David Howellsd84f4f92008-11-14 10:39:23 +11004225static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004226 unsigned long flags)
4227{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004228 struct smack_known *skp = smk_of_task(cred->security);
4229
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004230 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004231 return 0;
4232}
4233
4234/**
4235 * smack_key_free - Clear the key security blob
4236 * @key: the object
4237 *
4238 * Clear the blob pointer
4239 */
4240static void smack_key_free(struct key *key)
4241{
4242 key->security = NULL;
4243}
4244
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004245/**
Casey Schauflere114e472008-02-04 22:29:50 -08004246 * smack_key_permission - Smack access on a key
4247 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004248 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004249 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004250 *
4251 * Return 0 if the task has read and write to the object,
4252 * an error code otherwise
4253 */
4254static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004255 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004256{
4257 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004258 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004259 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004260 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004261 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004262
4263 keyp = key_ref_to_ptr(key_ref);
4264 if (keyp == NULL)
4265 return -EINVAL;
4266 /*
4267 * If the key hasn't been initialized give it access so that
4268 * it may do so.
4269 */
4270 if (keyp->security == NULL)
4271 return 0;
4272 /*
4273 * This should not occur
4274 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004275 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004276 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004277#ifdef CONFIG_AUDIT
4278 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4279 ad.a.u.key_struct.key = keyp->serial;
4280 ad.a.u.key_struct.key_desc = keyp->description;
4281#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004282 if (perm & KEY_NEED_READ)
4283 request = MAY_READ;
4284 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
4285 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004286 rc = smk_access(tkp, keyp->security, request, &ad);
4287 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4288 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004289}
José Bollo7fc5f362015-02-17 15:41:22 +01004290
4291/*
4292 * smack_key_getsecurity - Smack label tagging the key
4293 * @key points to the key to be queried
4294 * @_buffer points to a pointer that should be set to point to the
4295 * resulting string (if no label or an error occurs).
4296 * Return the length of the string (including terminating NUL) or -ve if
4297 * an error.
4298 * May also return 0 (and a NULL buffer pointer) if there is no label.
4299 */
4300static int smack_key_getsecurity(struct key *key, char **_buffer)
4301{
4302 struct smack_known *skp = key->security;
4303 size_t length;
4304 char *copy;
4305
4306 if (key->security == NULL) {
4307 *_buffer = NULL;
4308 return 0;
4309 }
4310
4311 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4312 if (copy == NULL)
4313 return -ENOMEM;
4314 length = strlen(copy) + 1;
4315
4316 *_buffer = copy;
4317 return length;
4318}
4319
Casey Schauflere114e472008-02-04 22:29:50 -08004320#endif /* CONFIG_KEYS */
4321
4322/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004323 * Smack Audit hooks
4324 *
4325 * Audit requires a unique representation of each Smack specific
4326 * rule. This unique representation is used to distinguish the
4327 * object to be audited from remaining kernel objects and also
4328 * works as a glue between the audit hooks.
4329 *
4330 * Since repository entries are added but never deleted, we'll use
4331 * the smack_known label address related to the given audit rule as
4332 * the needed unique representation. This also better fits the smack
4333 * model where nearly everything is a label.
4334 */
4335#ifdef CONFIG_AUDIT
4336
4337/**
4338 * smack_audit_rule_init - Initialize a smack audit rule
4339 * @field: audit rule fields given from user-space (audit.h)
4340 * @op: required testing operator (=, !=, >, <, ...)
4341 * @rulestr: smack label to be audited
4342 * @vrule: pointer to save our own audit rule representation
4343 *
4344 * Prepare to audit cases where (@field @op @rulestr) is true.
4345 * The label to be audited is created if necessay.
4346 */
4347static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4348{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004349 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004350 char **rule = (char **)vrule;
4351 *rule = NULL;
4352
4353 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4354 return -EINVAL;
4355
Al Viro5af75d82008-12-16 05:59:26 -05004356 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004357 return -EINVAL;
4358
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004359 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004360 if (IS_ERR(skp))
4361 return PTR_ERR(skp);
4362
4363 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004364
4365 return 0;
4366}
4367
4368/**
4369 * smack_audit_rule_known - Distinguish Smack audit rules
4370 * @krule: rule of interest, in Audit kernel representation format
4371 *
4372 * This is used to filter Smack rules from remaining Audit ones.
4373 * If it's proved that this rule belongs to us, the
4374 * audit_rule_match hook will be called to do the final judgement.
4375 */
4376static int smack_audit_rule_known(struct audit_krule *krule)
4377{
4378 struct audit_field *f;
4379 int i;
4380
4381 for (i = 0; i < krule->field_count; i++) {
4382 f = &krule->fields[i];
4383
4384 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4385 return 1;
4386 }
4387
4388 return 0;
4389}
4390
4391/**
4392 * smack_audit_rule_match - Audit given object ?
4393 * @secid: security id for identifying the object to test
4394 * @field: audit rule flags given from user-space
4395 * @op: required testing operator
4396 * @vrule: smack internal rule presentation
4397 * @actx: audit context associated with the check
4398 *
4399 * The core Audit hook. It's used to take the decision of
4400 * whether to audit or not to audit a given object.
4401 */
4402static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4403 struct audit_context *actx)
4404{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004405 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004406 char *rule = vrule;
4407
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004408 if (unlikely(!rule)) {
4409 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004410 return -ENOENT;
4411 }
4412
4413 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4414 return 0;
4415
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004416 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004417
4418 /*
4419 * No need to do string comparisons. If a match occurs,
4420 * both pointers will point to the same smack_known
4421 * label.
4422 */
Al Viro5af75d82008-12-16 05:59:26 -05004423 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004424 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004425 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004426 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004427
4428 return 0;
4429}
4430
4431/**
4432 * smack_audit_rule_free - free smack rule representation
4433 * @vrule: rule to be freed.
4434 *
4435 * No memory was allocated.
4436 */
4437static void smack_audit_rule_free(void *vrule)
4438{
4439 /* No-op */
4440}
4441
4442#endif /* CONFIG_AUDIT */
4443
Randy Dunlap251a2a92009-02-18 11:42:33 -08004444/**
David Quigley746df9b2013-05-22 12:50:35 -04004445 * smack_ismaclabel - check if xattr @name references a smack MAC label
4446 * @name: Full xattr name to check.
4447 */
4448static int smack_ismaclabel(const char *name)
4449{
4450 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4451}
4452
4453
4454/**
Casey Schauflere114e472008-02-04 22:29:50 -08004455 * smack_secid_to_secctx - return the smack label for a secid
4456 * @secid: incoming integer
4457 * @secdata: destination
4458 * @seclen: how long it is
4459 *
4460 * Exists for networking code.
4461 */
4462static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4463{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004464 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004465
Eric Parisd5630b92010-10-13 16:24:48 -04004466 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004467 *secdata = skp->smk_known;
4468 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004469 return 0;
4470}
4471
Randy Dunlap251a2a92009-02-18 11:42:33 -08004472/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004473 * smack_secctx_to_secid - return the secid for a smack label
4474 * @secdata: smack label
4475 * @seclen: how long result is
4476 * @secid: outgoing integer
4477 *
4478 * Exists for audit and networking code.
4479 */
David Howellse52c17642008-04-29 20:52:51 +01004480static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004481{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004482 struct smack_known *skp = smk_find_entry(secdata);
4483
4484 if (skp)
4485 *secid = skp->smk_secid;
4486 else
4487 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004488 return 0;
4489}
4490
Randy Dunlap251a2a92009-02-18 11:42:33 -08004491/**
Casey Schauflere114e472008-02-04 22:29:50 -08004492 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004493 * @secdata: unused
4494 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004495 *
4496 * Exists to make sure nothing gets done, and properly
4497 */
4498static void smack_release_secctx(char *secdata, u32 seclen)
4499{
4500}
4501
David P. Quigley1ee65e32009-09-03 14:25:57 -04004502static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4503{
4504 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4505}
4506
4507static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4508{
4509 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4510}
4511
4512static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4513{
4514 int len = 0;
4515 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4516
4517 if (len < 0)
4518 return len;
4519 *ctxlen = len;
4520 return 0;
4521}
4522
Casey Schaufler1eddfe82015-07-30 14:35:14 -07004523static struct security_hook_list smack_hooks[] = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004524 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4525 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4526 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004527
Casey Schauflere20b0432015-05-02 15:11:36 -07004528 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4529 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
4530 LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
4531 LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
4532 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304533 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
4534 LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
Casey Schauflere114e472008-02-04 22:29:50 -08004535
Casey Schauflere20b0432015-05-02 15:11:36 -07004536 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
4537 LSM_HOOK_INIT(bprm_committing_creds, smack_bprm_committing_creds),
4538 LSM_HOOK_INIT(bprm_secureexec, smack_bprm_secureexec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004539
Casey Schauflere20b0432015-05-02 15:11:36 -07004540 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4541 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4542 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4543 LSM_HOOK_INIT(inode_link, smack_inode_link),
4544 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4545 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4546 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4547 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4548 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4549 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4550 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4551 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4552 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4553 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4554 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4555 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4556 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4557 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004558
Casey Schauflere20b0432015-05-02 15:11:36 -07004559 LSM_HOOK_INIT(file_permission, smack_file_permission),
4560 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
4561 LSM_HOOK_INIT(file_free_security, smack_file_free_security),
4562 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4563 LSM_HOOK_INIT(file_lock, smack_file_lock),
4564 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4565 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4566 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4567 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4568 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4569 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004570
Casey Schauflere20b0432015-05-02 15:11:36 -07004571 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004572
Casey Schauflere20b0432015-05-02 15:11:36 -07004573 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4574 LSM_HOOK_INIT(cred_free, smack_cred_free),
4575 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4576 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
4577 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4578 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4579 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4580 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4581 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4582 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4583 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4584 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4585 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4586 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4587 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4588 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4589 LSM_HOOK_INIT(task_kill, smack_task_kill),
4590 LSM_HOOK_INIT(task_wait, smack_task_wait),
4591 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004592
Casey Schauflere20b0432015-05-02 15:11:36 -07004593 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4594 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004595
Casey Schauflere20b0432015-05-02 15:11:36 -07004596 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4597 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004598
Casey Schauflere20b0432015-05-02 15:11:36 -07004599 LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
4600 LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
4601 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4602 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4603 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4604 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004605
Casey Schauflere20b0432015-05-02 15:11:36 -07004606 LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
4607 LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
4608 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4609 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4610 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004611
Casey Schauflere20b0432015-05-02 15:11:36 -07004612 LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
4613 LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
4614 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4615 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4616 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004617
Casey Schauflere20b0432015-05-02 15:11:36 -07004618 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004619
Casey Schauflere20b0432015-05-02 15:11:36 -07004620 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4621 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004622
Casey Schauflere20b0432015-05-02 15:11:36 -07004623 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4624 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004625
Casey Schauflere20b0432015-05-02 15:11:36 -07004626 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004627#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004628 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004629#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004630 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4631 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4632 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4633 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4634 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4635 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4636 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4637 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4638 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4639 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004640
Casey Schauflere114e472008-02-04 22:29:50 -08004641 /* key management security hooks */
4642#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004643 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4644 LSM_HOOK_INIT(key_free, smack_key_free),
4645 LSM_HOOK_INIT(key_permission, smack_key_permission),
4646 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004647#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004648
4649 /* Audit hooks */
4650#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004651 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4652 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4653 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4654 LSM_HOOK_INIT(audit_rule_free, smack_audit_rule_free),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004655#endif /* CONFIG_AUDIT */
4656
Casey Schauflere20b0432015-05-02 15:11:36 -07004657 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4658 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4659 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
4660 LSM_HOOK_INIT(release_secctx, smack_release_secctx),
4661 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4662 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4663 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflere114e472008-02-04 22:29:50 -08004664};
4665
Etienne Basset7198e2e2009-03-24 20:53:24 +01004666
Casey Schaufler86812bb2012-04-17 18:55:46 -07004667static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004668{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004669 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004670 * Initialize rule list locks
4671 */
4672 mutex_init(&smack_known_huh.smk_rules_lock);
4673 mutex_init(&smack_known_hat.smk_rules_lock);
4674 mutex_init(&smack_known_floor.smk_rules_lock);
4675 mutex_init(&smack_known_star.smk_rules_lock);
4676 mutex_init(&smack_known_invalid.smk_rules_lock);
4677 mutex_init(&smack_known_web.smk_rules_lock);
4678 /*
4679 * Initialize rule lists
4680 */
4681 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4682 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4683 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4684 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4685 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4686 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4687 /*
4688 * Create the known labels list
4689 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004690 smk_insert_entry(&smack_known_huh);
4691 smk_insert_entry(&smack_known_hat);
4692 smk_insert_entry(&smack_known_star);
4693 smk_insert_entry(&smack_known_floor);
4694 smk_insert_entry(&smack_known_invalid);
4695 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004696}
4697
Casey Schauflere114e472008-02-04 22:29:50 -08004698/**
4699 * smack_init - initialize the smack system
4700 *
4701 * Returns 0
4702 */
4703static __init int smack_init(void)
4704{
David Howellsd84f4f92008-11-14 10:39:23 +11004705 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004706 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004707
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004708 if (!security_module_enable("smack"))
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004709 return 0;
4710
Casey Schaufler69f287a2014-12-12 17:08:40 -08004711 smack_enabled = 1;
4712
Rohit1a5b4722014-10-15 17:40:41 +05304713 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4714 if (!smack_inode_cache)
4715 return -ENOMEM;
4716
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004717 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4718 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304719 if (tsp == NULL) {
4720 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004721 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304722 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004723
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004724 pr_info("Smack: Initializing.\n");
4725#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4726 pr_info("Smack: Netfilter enabled.\n");
4727#endif
4728#ifdef SMACK_IPV6_PORT_LABELING
4729 pr_info("Smack: IPv6 port labeling enabled.\n");
4730#endif
4731#ifdef SMACK_IPV6_SECMARK_LABELING
4732 pr_info("Smack: IPv6 Netfilter enabled.\n");
4733#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004734
4735 /*
4736 * Set the security state for the initial task.
4737 */
David Howellsd84f4f92008-11-14 10:39:23 +11004738 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004739 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004740
Casey Schaufler86812bb2012-04-17 18:55:46 -07004741 /* initialize the smack_known_list */
4742 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004743
4744 /*
4745 * Register with LSM
4746 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07004747 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks));
Casey Schauflere114e472008-02-04 22:29:50 -08004748
4749 return 0;
4750}
4751
4752/*
4753 * Smack requires early initialization in order to label
4754 * all processes and objects when they are created.
4755 */
4756security_initcall(smack_init);