blob: 1511965549b8232fdd4d3469166023c2d140f908 [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>
Casey Schauflere114e472008-02-04 22:29:50 -080044#include "smack.h"
45
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020046#define TRANS_TRUE "TRUE"
47#define TRANS_TRUE_SIZE 4
48
Casey Schauflerc6739442013-05-22 18:42:56 -070049#define SMK_CONNECTING 0
50#define SMK_RECEIVING 1
51#define SMK_SENDING 2
52
Casey Schaufler69f287a2014-12-12 17:08:40 -080053#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -070054LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler69f287a2014-12-12 17:08:40 -080055#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Rohit1a5b4722014-10-15 17:40:41 +053056static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080057int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070058
Casey Schauflerd166c802014-08-27 14:51:27 -070059#ifdef CONFIG_SECURITY_SMACK_BRINGUP
60static void smk_bu_mode(int mode, char *s)
61{
62 int i = 0;
63
64 if (mode & MAY_READ)
65 s[i++] = 'r';
66 if (mode & MAY_WRITE)
67 s[i++] = 'w';
68 if (mode & MAY_EXEC)
69 s[i++] = 'x';
70 if (mode & MAY_APPEND)
71 s[i++] = 'a';
72 if (mode & MAY_TRANSMUTE)
73 s[i++] = 't';
74 if (mode & MAY_LOCK)
75 s[i++] = 'l';
76 if (i == 0)
77 s[i++] = '-';
78 s[i] = '\0';
79}
80#endif
81
82#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020083static int smk_bu_note(char *note, struct smack_known *sskp,
84 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -070085{
86 char acc[SMK_NUM_ACCESS_TYPE + 1];
87
88 if (rc <= 0)
89 return rc;
90
91 smk_bu_mode(mode, acc);
92 pr_info("Smack Bringup: (%s %s %s) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020093 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -070094 return 0;
95}
96#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020097#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -070098#endif
99
100#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200101static int smk_bu_current(char *note, struct smack_known *oskp,
102 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700103{
104 struct task_smack *tsp = current_security();
105 char acc[SMK_NUM_ACCESS_TYPE + 1];
106
107 if (rc <= 0)
108 return rc;
109
110 smk_bu_mode(mode, acc);
111 pr_info("Smack Bringup: (%s %s %s) %s %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200112 tsp->smk_task->smk_known, oskp->smk_known,
113 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700114 return 0;
115}
116#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200117#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700118#endif
119
120#ifdef CONFIG_SECURITY_SMACK_BRINGUP
121static int smk_bu_task(struct task_struct *otp, int mode, int rc)
122{
123 struct task_smack *tsp = current_security();
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300124 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700125 char acc[SMK_NUM_ACCESS_TYPE + 1];
126
127 if (rc <= 0)
128 return rc;
129
130 smk_bu_mode(mode, acc);
131 pr_info("Smack Bringup: (%s %s %s) %s to %s\n",
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300132 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700133 current->comm, otp->comm);
134 return 0;
135}
136#else
137#define smk_bu_task(otp, mode, RC) (RC)
138#endif
139
140#ifdef CONFIG_SECURITY_SMACK_BRINGUP
141static int smk_bu_inode(struct inode *inode, int mode, int rc)
142{
143 struct task_smack *tsp = current_security();
144 char acc[SMK_NUM_ACCESS_TYPE + 1];
145
146 if (rc <= 0)
147 return rc;
148
149 smk_bu_mode(mode, acc);
150 pr_info("Smack Bringup: (%s %s %s) inode=(%s %ld) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200151 tsp->smk_task->smk_known, smk_of_inode(inode)->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700152 inode->i_sb->s_id, inode->i_ino, current->comm);
153 return 0;
154}
155#else
156#define smk_bu_inode(inode, mode, RC) (RC)
157#endif
158
159#ifdef CONFIG_SECURITY_SMACK_BRINGUP
160static int smk_bu_file(struct file *file, int mode, int rc)
161{
162 struct task_smack *tsp = current_security();
163 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800164 struct inode *inode = file_inode(file);
Casey Schauflerd166c802014-08-27 14:51:27 -0700165 char acc[SMK_NUM_ACCESS_TYPE + 1];
166
167 if (rc <= 0)
168 return rc;
169
170 smk_bu_mode(mode, acc);
Al Viroa4555892014-10-21 20:11:25 -0400171 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800172 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400173 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700174 current->comm);
175 return 0;
176}
177#else
178#define smk_bu_file(file, mode, RC) (RC)
179#endif
180
181#ifdef CONFIG_SECURITY_SMACK_BRINGUP
182static int smk_bu_credfile(const struct cred *cred, struct file *file,
183 int mode, int rc)
184{
185 struct task_smack *tsp = cred->security;
186 struct smack_known *sskp = tsp->smk_task;
187 struct inode *inode = file->f_inode;
188 char acc[SMK_NUM_ACCESS_TYPE + 1];
189
190 if (rc <= 0)
191 return rc;
192
193 smk_bu_mode(mode, acc);
Al Viroa4555892014-10-21 20:11:25 -0400194 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200195 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400196 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700197 current->comm);
198 return 0;
199}
200#else
201#define smk_bu_credfile(cred, file, mode, RC) (RC)
202#endif
203
Casey Schauflere114e472008-02-04 22:29:50 -0800204/**
205 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100206 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800207 * @ip: a pointer to the inode
208 * @dp: a pointer to the dentry
209 *
210 * Returns a pointer to the master list entry for the Smack label
211 * or NULL if there was no label to fetch.
212 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700213static struct smack_known *smk_fetch(const char *name, struct inode *ip,
214 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800215{
216 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700217 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700218 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800219
220 if (ip->i_op->getxattr == NULL)
221 return NULL;
222
Casey Schauflerf7112e62012-05-06 15:22:02 -0700223 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
224 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800225 return NULL;
226
Casey Schauflerf7112e62012-05-06 15:22:02 -0700227 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
228 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700229 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700230
231 kfree(buffer);
232
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700233 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800234}
235
236/**
237 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200238 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800239 *
240 * Returns the new blob or NULL if there's no memory available
241 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200242struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800243{
244 struct inode_smack *isp;
245
Rohit1a5b4722014-10-15 17:40:41 +0530246 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800247 if (isp == NULL)
248 return NULL;
249
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200250 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800251 isp->smk_flags = 0;
252 mutex_init(&isp->smk_lock);
253
254 return isp;
255}
256
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800257/**
258 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100259 * @task: a pointer to the Smack label for the running task
260 * @forked: a pointer to the Smack label for the forked task
261 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800262 *
263 * Returns the new blob or NULL if there's no memory available
264 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700265static struct task_smack *new_task_smack(struct smack_known *task,
266 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800267{
268 struct task_smack *tsp;
269
270 tsp = kzalloc(sizeof(struct task_smack), gfp);
271 if (tsp == NULL)
272 return NULL;
273
274 tsp->smk_task = task;
275 tsp->smk_forked = forked;
276 INIT_LIST_HEAD(&tsp->smk_rules);
277 mutex_init(&tsp->smk_rules_lock);
278
279 return tsp;
280}
281
282/**
283 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100284 * @nhead: new rules header pointer
285 * @ohead: old rules header pointer
286 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800287 *
288 * Returns 0 on success, -ENOMEM on error
289 */
290static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
291 gfp_t gfp)
292{
293 struct smack_rule *nrp;
294 struct smack_rule *orp;
295 int rc = 0;
296
297 INIT_LIST_HEAD(nhead);
298
299 list_for_each_entry_rcu(orp, ohead, list) {
300 nrp = kzalloc(sizeof(struct smack_rule), gfp);
301 if (nrp == NULL) {
302 rc = -ENOMEM;
303 break;
304 }
305 *nrp = *orp;
306 list_add_rcu(&nrp->list, nhead);
307 }
308 return rc;
309}
310
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100311/**
312 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
313 * @mode - input mode in form of PTRACE_MODE_*
314 *
315 * Returns a converted MAY_* mode usable by smack rules
316 */
317static inline unsigned int smk_ptrace_mode(unsigned int mode)
318{
319 switch (mode) {
320 case PTRACE_MODE_READ:
321 return MAY_READ;
322 case PTRACE_MODE_ATTACH:
323 return MAY_READWRITE;
324 }
325
326 return 0;
327}
328
329/**
330 * smk_ptrace_rule_check - helper for ptrace access
331 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200332 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100333 * @mode: ptrace attachment mode (PTRACE_MODE_*)
334 * @func: name of the function that called us, used for audit
335 *
336 * Returns 0 on access granted, -error on error
337 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200338static int smk_ptrace_rule_check(struct task_struct *tracer,
339 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100340 unsigned int mode, const char *func)
341{
342 int rc;
343 struct smk_audit_info ad, *saip = NULL;
344 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200345 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100346
347 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
348 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
349 smk_ad_setfield_u_tsk(&ad, tracer);
350 saip = &ad;
351 }
352
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300353 rcu_read_lock();
354 tsp = __task_cred(tracer)->security;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200355 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100356
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100357 if ((mode & PTRACE_MODE_ATTACH) &&
358 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
359 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200360 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100361 rc = 0;
362 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
363 rc = -EACCES;
364 else if (capable(CAP_SYS_PTRACE))
365 rc = 0;
366 else
367 rc = -EACCES;
368
369 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200370 smack_log(tracer_known->smk_known,
371 tracee_known->smk_known,
372 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100373
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300374 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100375 return rc;
376 }
377
378 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200379 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300380
381 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100382 return rc;
383}
384
Casey Schauflere114e472008-02-04 22:29:50 -0800385/*
386 * LSM hooks.
387 * We he, that is fun!
388 */
389
390/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000391 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800392 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100393 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800394 *
395 * Returns 0 if access is OK, an error code otherwise
396 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100397 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800398 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000399static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800400{
401 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700402 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800403
Ingo Molnar9e488582009-05-07 19:26:19 +1000404 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800405 if (rc != 0)
406 return rc;
407
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300408 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200409
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200410 rc = smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100411 return rc;
412}
Casey Schauflere114e472008-02-04 22:29:50 -0800413
David Howells5cd9c582008-08-14 11:37:28 +0100414/**
415 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
416 * @ptp: parent task pointer
417 *
418 * Returns 0 if access is OK, an error code otherwise
419 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100420 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100421 */
422static int smack_ptrace_traceme(struct task_struct *ptp)
423{
424 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700425 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100426
427 rc = cap_ptrace_traceme(ptp);
428 if (rc != 0)
429 return rc;
430
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100431 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200432
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200433 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800434 return rc;
435}
436
437/**
438 * smack_syslog - Smack approval on syslog
439 * @type: message type
440 *
Casey Schauflere114e472008-02-04 22:29:50 -0800441 * Returns 0 on success, error code otherwise.
442 */
Eric Paris12b30522010-11-15 18:36:29 -0500443static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800444{
Eric Paris12b30522010-11-15 18:36:29 -0500445 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700446 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800447
Casey Schaufler1880eff2012-06-05 15:28:30 -0700448 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800449 return 0;
450
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800451 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800452 rc = -EACCES;
453
454 return rc;
455}
456
457
458/*
459 * Superblock Hooks.
460 */
461
462/**
463 * smack_sb_alloc_security - allocate a superblock blob
464 * @sb: the superblock getting the blob
465 *
466 * Returns 0 on success or -ENOMEM on error.
467 */
468static int smack_sb_alloc_security(struct super_block *sb)
469{
470 struct superblock_smack *sbsp;
471
472 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
473
474 if (sbsp == NULL)
475 return -ENOMEM;
476
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200477 sbsp->smk_root = &smack_known_floor;
478 sbsp->smk_default = &smack_known_floor;
479 sbsp->smk_floor = &smack_known_floor;
480 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700481 /*
482 * smk_initialized will be zero from kzalloc.
483 */
Casey Schauflere114e472008-02-04 22:29:50 -0800484 sb->s_security = sbsp;
485
486 return 0;
487}
488
489/**
490 * smack_sb_free_security - free a superblock blob
491 * @sb: the superblock getting the blob
492 *
493 */
494static void smack_sb_free_security(struct super_block *sb)
495{
496 kfree(sb->s_security);
497 sb->s_security = NULL;
498}
499
500/**
501 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800502 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800503 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800504 *
505 * Returns 0 on success or -ENOMEM on error.
506 *
507 * Copy the Smack specific mount options out of the mount
508 * options list.
509 */
Eric Parise0007522008-03-05 10:31:54 -0500510static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800511{
512 char *cp, *commap, *otheropts, *dp;
513
Casey Schauflere114e472008-02-04 22:29:50 -0800514 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
515 if (otheropts == NULL)
516 return -ENOMEM;
517
518 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
519 if (strstr(cp, SMK_FSDEFAULT) == cp)
520 dp = smackopts;
521 else if (strstr(cp, SMK_FSFLOOR) == cp)
522 dp = smackopts;
523 else if (strstr(cp, SMK_FSHAT) == cp)
524 dp = smackopts;
525 else if (strstr(cp, SMK_FSROOT) == cp)
526 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700527 else if (strstr(cp, SMK_FSTRANS) == cp)
528 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800529 else
530 dp = otheropts;
531
532 commap = strchr(cp, ',');
533 if (commap != NULL)
534 *commap = '\0';
535
536 if (*dp != '\0')
537 strcat(dp, ",");
538 strcat(dp, cp);
539 }
540
541 strcpy(orig, otheropts);
542 free_page((unsigned long)otheropts);
543
544 return 0;
545}
546
547/**
548 * smack_sb_kern_mount - Smack specific mount processing
549 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100550 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800551 * @data: the smack mount options
552 *
553 * Returns 0 on success, an error code on failure
554 */
James Morris12204e22008-12-19 10:44:42 +1100555static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800556{
557 struct dentry *root = sb->s_root;
558 struct inode *inode = root->d_inode;
559 struct superblock_smack *sp = sb->s_security;
560 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800561 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800562 char *op;
563 char *commap;
Casey Schauflere830b392013-05-22 18:43:07 -0700564 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800565 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800566
Casey Schauflere830b392013-05-22 18:43:07 -0700567 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800568 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700569
Casey Schauflere114e472008-02-04 22:29:50 -0800570 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800571
572 for (op = data; op != NULL; op = commap) {
573 commap = strchr(op, ',');
574 if (commap != NULL)
575 *commap++ = '\0';
576
577 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
578 op += strlen(SMK_FSHAT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200579 skp = smk_import_entry(op, 0);
580 if (skp != NULL) {
581 sp->smk_hat = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800582 specified = 1;
583 }
Casey Schauflere114e472008-02-04 22:29:50 -0800584 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
585 op += strlen(SMK_FSFLOOR);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200586 skp = smk_import_entry(op, 0);
587 if (skp != NULL) {
588 sp->smk_floor = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800589 specified = 1;
590 }
Casey Schauflere114e472008-02-04 22:29:50 -0800591 } else if (strncmp(op, SMK_FSDEFAULT,
592 strlen(SMK_FSDEFAULT)) == 0) {
593 op += strlen(SMK_FSDEFAULT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200594 skp = smk_import_entry(op, 0);
595 if (skp != NULL) {
596 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800597 specified = 1;
598 }
Casey Schauflere114e472008-02-04 22:29:50 -0800599 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
600 op += strlen(SMK_FSROOT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200601 skp = smk_import_entry(op, 0);
602 if (skp != NULL) {
603 sp->smk_root = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800604 specified = 1;
605 }
Casey Schauflere830b392013-05-22 18:43:07 -0700606 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
607 op += strlen(SMK_FSTRANS);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200608 skp = smk_import_entry(op, 0);
609 if (skp != NULL) {
610 sp->smk_root = skp;
Casey Schauflere830b392013-05-22 18:43:07 -0700611 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800612 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700613 }
Casey Schauflere114e472008-02-04 22:29:50 -0800614 }
615 }
616
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800617 if (!smack_privileged(CAP_MAC_ADMIN)) {
618 /*
619 * Unprivileged mounts don't get to specify Smack values.
620 */
621 if (specified)
622 return -EPERM;
623 /*
624 * Unprivileged mounts get root and default from the caller.
625 */
626 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200627 sp->smk_root = skp;
628 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800629 }
Casey Schauflere114e472008-02-04 22:29:50 -0800630 /*
631 * Initialize the root inode.
632 */
633 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100634 if (isp == NULL) {
635 isp = new_inode_smack(sp->smk_root);
636 if (isp == NULL)
637 return -ENOMEM;
638 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700639 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800640 isp->smk_inode = sp->smk_root;
641
Casey Schauflere830b392013-05-22 18:43:07 -0700642 if (transmute)
643 isp->smk_flags |= SMK_INODE_TRANSMUTE;
644
Casey Schauflere114e472008-02-04 22:29:50 -0800645 return 0;
646}
647
648/**
649 * smack_sb_statfs - Smack check on statfs
650 * @dentry: identifies the file system in question
651 *
652 * Returns 0 if current can read the floor of the filesystem,
653 * and error code otherwise
654 */
655static int smack_sb_statfs(struct dentry *dentry)
656{
657 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200658 int rc;
659 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800660
Eric Parisa2694342011-04-25 13:10:27 -0400661 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200662 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
663
664 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700665 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200666 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800667}
668
Casey Schauflere114e472008-02-04 22:29:50 -0800669/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800670 * BPRM hooks
671 */
672
Casey Schauflerce8a4322011-09-29 18:21:01 -0700673/**
674 * smack_bprm_set_creds - set creds for exec
675 * @bprm: the exec information
676 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100677 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700678 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800679static int smack_bprm_set_creds(struct linux_binprm *bprm)
680{
Al Viro496ad9a2013-01-23 17:07:38 -0500681 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300682 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800683 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800684 int rc;
685
686 rc = cap_bprm_set_creds(bprm);
687 if (rc != 0)
688 return rc;
689
690 if (bprm->cred_prepared)
691 return 0;
692
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300693 isp = inode->i_security;
694 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800695 return 0;
696
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100697 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
698 struct task_struct *tracer;
699 rc = 0;
700
701 rcu_read_lock();
702 tracer = ptrace_parent(current);
703 if (likely(tracer != NULL))
704 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200705 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100706 PTRACE_MODE_ATTACH,
707 __func__);
708 rcu_read_unlock();
709
710 if (rc != 0)
711 return rc;
712 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300713 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800714
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300715 bsp->smk_task = isp->smk_task;
716 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800717
718 return 0;
719}
720
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300721/**
722 * smack_bprm_committing_creds - Prepare to install the new credentials
723 * from bprm.
724 *
725 * @bprm: binprm for exec
726 */
727static void smack_bprm_committing_creds(struct linux_binprm *bprm)
728{
729 struct task_smack *bsp = bprm->cred->security;
730
731 if (bsp->smk_task != bsp->smk_forked)
732 current->pdeath_signal = 0;
733}
734
735/**
736 * smack_bprm_secureexec - Return the decision to use secureexec.
737 * @bprm: binprm for exec
738 *
739 * Returns 0 on success.
740 */
741static int smack_bprm_secureexec(struct linux_binprm *bprm)
742{
743 struct task_smack *tsp = current_security();
744 int ret = cap_bprm_secureexec(bprm);
745
746 if (!ret && (tsp->smk_task != tsp->smk_forked))
747 ret = 1;
748
749 return ret;
750}
751
Casey Schaufler676dac42010-12-02 06:43:39 -0800752/*
Casey Schauflere114e472008-02-04 22:29:50 -0800753 * Inode hooks
754 */
755
756/**
757 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800758 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800759 *
760 * Returns 0 if it gets a blob, -ENOMEM otherwise
761 */
762static int smack_inode_alloc_security(struct inode *inode)
763{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700764 struct smack_known *skp = smk_of_current();
765
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200766 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800767 if (inode->i_security == NULL)
768 return -ENOMEM;
769 return 0;
770}
771
772/**
773 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800774 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800775 *
776 * Clears the blob pointer in inode
777 */
778static void smack_inode_free_security(struct inode *inode)
779{
Rohit1a5b4722014-10-15 17:40:41 +0530780 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800781 inode->i_security = NULL;
782}
783
784/**
785 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200786 * @inode: the newly created inode
787 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500788 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800789 * @name: where to put the attribute name
790 * @value: where to put the attribute value
791 * @len: where to put the length of the attribute
792 *
793 * Returns 0 if it all works out, -ENOMEM if there's no memory
794 */
795static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900796 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500797 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800798{
Casey Schaufler2267b132012-03-13 19:14:19 -0700799 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700800 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200801 struct smack_known *isp = smk_of_inode(inode);
802 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800803 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800804
Tetsuo Handa95489062013-07-25 05:44:02 +0900805 if (name)
806 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800807
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100808 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800809 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200810 may = smk_access_entry(skp->smk_known, dsp->smk_known,
811 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800812 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200813
814 /*
815 * If the access rule allows transmutation and
816 * the directory requests transmutation then
817 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700818 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200819 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800820 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700821 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200822 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700823 issp->smk_flags |= SMK_INODE_CHANGED;
824 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200825
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200826 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800827 if (*value == NULL)
828 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800829
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200830 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100831 }
Casey Schauflere114e472008-02-04 22:29:50 -0800832
833 return 0;
834}
835
836/**
837 * smack_inode_link - Smack check on link
838 * @old_dentry: the existing object
839 * @dir: unused
840 * @new_dentry: the new object
841 *
842 * Returns 0 if access is permitted, an error code otherwise
843 */
844static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
845 struct dentry *new_dentry)
846{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200847 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200848 struct smk_audit_info ad;
849 int rc;
850
Eric Parisa2694342011-04-25 13:10:27 -0400851 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200852 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800853
854 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200855 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700856 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800857
David Howells88025652015-01-29 12:02:32 +0000858 if (rc == 0 && d_is_positive(new_dentry)) {
Casey Schauflere114e472008-02-04 22:29:50 -0800859 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200860 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
861 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700862 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800863 }
864
865 return rc;
866}
867
868/**
869 * smack_inode_unlink - Smack check on inode deletion
870 * @dir: containing directory object
871 * @dentry: file to unlink
872 *
873 * Returns 0 if current can write the containing directory
874 * and the object, error code otherwise
875 */
876static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
877{
878 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200879 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800880 int rc;
881
Eric Parisa2694342011-04-25 13:10:27 -0400882 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200883 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
884
Casey Schauflere114e472008-02-04 22:29:50 -0800885 /*
886 * You need write access to the thing you're unlinking
887 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200888 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700889 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200890 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800891 /*
892 * You also need write access to the containing directory
893 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400894 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200895 smk_ad_setfield_u_fs_inode(&ad, dir);
896 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700897 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200898 }
Casey Schauflere114e472008-02-04 22:29:50 -0800899 return rc;
900}
901
902/**
903 * smack_inode_rmdir - Smack check on directory deletion
904 * @dir: containing directory object
905 * @dentry: directory to unlink
906 *
907 * Returns 0 if current can write the containing directory
908 * and the directory, error code otherwise
909 */
910static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
911{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200912 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800913 int rc;
914
Eric Parisa2694342011-04-25 13:10:27 -0400915 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200916 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
917
Casey Schauflere114e472008-02-04 22:29:50 -0800918 /*
919 * You need write access to the thing you're removing
920 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200921 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700922 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200923 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800924 /*
925 * You also need write access to the containing directory
926 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400927 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200928 smk_ad_setfield_u_fs_inode(&ad, dir);
929 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700930 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200931 }
Casey Schauflere114e472008-02-04 22:29:50 -0800932
933 return rc;
934}
935
936/**
937 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200938 * @old_inode: unused
939 * @old_dentry: the old object
940 * @new_inode: unused
941 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -0800942 *
943 * Read and write access is required on both the old and
944 * new directories.
945 *
946 * Returns 0 if access is permitted, an error code otherwise
947 */
948static int smack_inode_rename(struct inode *old_inode,
949 struct dentry *old_dentry,
950 struct inode *new_inode,
951 struct dentry *new_dentry)
952{
953 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200954 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200955 struct smk_audit_info ad;
956
Eric Parisa2694342011-04-25 13:10:27 -0400957 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200958 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800959
960 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200961 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700962 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800963
David Howells88025652015-01-29 12:02:32 +0000964 if (rc == 0 && d_is_positive(new_dentry)) {
Casey Schauflere114e472008-02-04 22:29:50 -0800965 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200966 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
967 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700968 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800969 }
Casey Schauflere114e472008-02-04 22:29:50 -0800970 return rc;
971}
972
973/**
974 * smack_inode_permission - Smack version of permission()
975 * @inode: the inode in question
976 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800977 *
978 * This is the important Smack hook.
979 *
980 * Returns 0 if access is permitted, -EACCES otherwise
981 */
Al Viroe74f71e2011-06-20 19:38:15 -0400982static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800983{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200984 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400985 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -0700986 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -0400987
988 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800989 /*
990 * No permission to check. Existence test. Yup, it's there.
991 */
992 if (mask == 0)
993 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700994
995 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400996 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700997 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400998 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200999 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001000 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1001 rc = smk_bu_inode(inode, mask, rc);
1002 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001003}
1004
1005/**
1006 * smack_inode_setattr - Smack check for setting attributes
1007 * @dentry: the object
1008 * @iattr: for the force flag
1009 *
1010 * Returns 0 if access is permitted, an error code otherwise
1011 */
1012static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1013{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001014 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001015 int rc;
1016
Casey Schauflere114e472008-02-04 22:29:50 -08001017 /*
1018 * Need to allow for clearing the setuid bit.
1019 */
1020 if (iattr->ia_valid & ATTR_FORCE)
1021 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001022 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001023 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001024
Casey Schauflerd166c802014-08-27 14:51:27 -07001025 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1026 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1027 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001028}
1029
1030/**
1031 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001032 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001033 * @dentry: the object
1034 *
1035 * Returns 0 if access is permitted, an error code otherwise
1036 */
Al Viro3f7036a2015-03-08 19:28:30 -04001037static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001038{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001039 struct smk_audit_info ad;
Al Viro3f7036a2015-03-08 19:28:30 -04001040 struct inode *inode = path->dentry->d_inode;
Casey Schauflerd166c802014-08-27 14:51:27 -07001041 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042
Eric Parisf48b7392011-04-25 12:54:27 -04001043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001044 smk_ad_setfield_u_fs_path(&ad, *path);
1045 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1046 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001047 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001048}
1049
1050/**
1051 * smack_inode_setxattr - Smack check for setting xattrs
1052 * @dentry: the object
1053 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001054 * @value: value of the attribute
1055 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001056 * @flags: unused
1057 *
1058 * This protects the Smack attribute explicitly.
1059 *
1060 * Returns 0 if access is permitted, an error code otherwise
1061 */
David Howells8f0cfa52008-04-29 00:59:41 -07001062static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1063 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001064{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001065 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001066 struct smack_known *skp;
1067 int check_priv = 0;
1068 int check_import = 0;
1069 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001070 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001071
Casey Schaufler19760ad2013-12-16 16:27:26 -08001072 /*
1073 * Check label validity here so import won't fail in post_setxattr
1074 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001075 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1076 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001077 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1078 check_priv = 1;
1079 check_import = 1;
1080 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1081 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1082 check_priv = 1;
1083 check_import = 1;
1084 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001085 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001086 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001087 if (size != TRANS_TRUE_SIZE ||
1088 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1089 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001090 } else
1091 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1092
Casey Schaufler19760ad2013-12-16 16:27:26 -08001093 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1094 rc = -EPERM;
1095
1096 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001097 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001098 if (skp == NULL || (check_star &&
1099 (skp == &smack_known_star || skp == &smack_known_web)))
1100 rc = -EINVAL;
1101 }
1102
Eric Parisa2694342011-04-25 13:10:27 -04001103 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001104 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1105
Casey Schauflerd166c802014-08-27 14:51:27 -07001106 if (rc == 0) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001107 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001108 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1109 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001110
1111 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001112}
1113
1114/**
1115 * smack_inode_post_setxattr - Apply the Smack update approved above
1116 * @dentry: object
1117 * @name: attribute name
1118 * @value: attribute value
1119 * @size: attribute size
1120 * @flags: unused
1121 *
1122 * Set the pointer in the inode blob to the entry found
1123 * in the master label list.
1124 */
David Howells8f0cfa52008-04-29 00:59:41 -07001125static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1126 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001127{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001128 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001129 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001130
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001131 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1132 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1133 return;
1134 }
1135
Casey Schaufler676dac42010-12-02 06:43:39 -08001136 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001137 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001138 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001139 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001140 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001141 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001142 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001143 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001144 if (skp != NULL)
1145 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001146 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001147 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001148 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001149 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001150 if (skp != NULL)
1151 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001152 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001153 isp->smk_mmap = &smack_known_invalid;
1154 }
Casey Schauflere114e472008-02-04 22:29:50 -08001155
1156 return;
1157}
1158
Casey Schauflerce8a4322011-09-29 18:21:01 -07001159/**
Casey Schauflere114e472008-02-04 22:29:50 -08001160 * smack_inode_getxattr - Smack check on getxattr
1161 * @dentry: the object
1162 * @name: unused
1163 *
1164 * Returns 0 if access is permitted, an error code otherwise
1165 */
David Howells8f0cfa52008-04-29 00:59:41 -07001166static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001167{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001168 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001169 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001170
Eric Parisa2694342011-04-25 13:10:27 -04001171 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001172 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1173
Casey Schauflerd166c802014-08-27 14:51:27 -07001174 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1175 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1176 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001177}
1178
Casey Schauflerce8a4322011-09-29 18:21:01 -07001179/**
Casey Schauflere114e472008-02-04 22:29:50 -08001180 * smack_inode_removexattr - Smack check on removexattr
1181 * @dentry: the object
1182 * @name: name of the attribute
1183 *
1184 * Removing the Smack attribute requires CAP_MAC_ADMIN
1185 *
1186 * Returns 0 if access is permitted, an error code otherwise
1187 */
David Howells8f0cfa52008-04-29 00:59:41 -07001188static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001189{
Casey Schaufler676dac42010-12-02 06:43:39 -08001190 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001191 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001192 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001193
Casey Schauflerbcdca222008-02-23 15:24:04 -08001194 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1195 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001196 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001197 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001198 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301199 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001200 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001201 rc = -EPERM;
1202 } else
1203 rc = cap_inode_removexattr(dentry, name);
1204
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001205 if (rc != 0)
1206 return rc;
1207
Eric Parisa2694342011-04-25 13:10:27 -04001208 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001209 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001210
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001211 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001212 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001213 if (rc != 0)
1214 return rc;
1215
1216 isp = dentry->d_inode->i_security;
1217 /*
1218 * Don't do anything special for these.
1219 * XATTR_NAME_SMACKIPIN
1220 * XATTR_NAME_SMACKIPOUT
1221 * XATTR_NAME_SMACKEXEC
1222 */
1223 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001224 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001225 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001226 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001227 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1228 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001229
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001230 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001231}
1232
1233/**
1234 * smack_inode_getsecurity - get smack xattrs
1235 * @inode: the object
1236 * @name: attribute name
1237 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001238 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001239 *
1240 * Returns the size of the attribute or an error code
1241 */
1242static int smack_inode_getsecurity(const struct inode *inode,
1243 const char *name, void **buffer,
1244 bool alloc)
1245{
1246 struct socket_smack *ssp;
1247 struct socket *sock;
1248 struct super_block *sbp;
1249 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001250 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001251 int ilen;
1252 int rc = 0;
1253
1254 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1255 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001256 ilen = strlen(isp->smk_known);
1257 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001258 return ilen;
1259 }
1260
1261 /*
1262 * The rest of the Smack xattrs are only on sockets.
1263 */
1264 sbp = ip->i_sb;
1265 if (sbp->s_magic != SOCKFS_MAGIC)
1266 return -EOPNOTSUPP;
1267
1268 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001269 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001270 return -EOPNOTSUPP;
1271
1272 ssp = sock->sk->sk_security;
1273
1274 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001275 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001276 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001277 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001278 else
1279 return -EOPNOTSUPP;
1280
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001281 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001282 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001283 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001284 rc = ilen;
1285 }
1286
1287 return rc;
1288}
1289
1290
1291/**
1292 * smack_inode_listsecurity - list the Smack attributes
1293 * @inode: the object
1294 * @buffer: where they go
1295 * @buffer_size: size of buffer
1296 *
1297 * Returns 0 on success, -EINVAL otherwise
1298 */
1299static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1300 size_t buffer_size)
1301{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001302 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001303
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001304 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001305 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001306
1307 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001308}
1309
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001310/**
1311 * smack_inode_getsecid - Extract inode's security id
1312 * @inode: inode to extract the info from
1313 * @secid: where result will be saved
1314 */
1315static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1316{
1317 struct inode_smack *isp = inode->i_security;
1318
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001319 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001320}
1321
Casey Schauflere114e472008-02-04 22:29:50 -08001322/*
1323 * File Hooks
1324 */
1325
1326/**
1327 * smack_file_permission - Smack check on file operations
1328 * @file: unused
1329 * @mask: unused
1330 *
1331 * Returns 0
1332 *
1333 * Should access checks be done on each read or write?
1334 * UNICOS and SELinux say yes.
1335 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1336 *
1337 * I'll say no for now. Smack does not do the frequent
1338 * label changing that SELinux does.
1339 */
1340static int smack_file_permission(struct file *file, int mask)
1341{
1342 return 0;
1343}
1344
1345/**
1346 * smack_file_alloc_security - assign a file security blob
1347 * @file: the object
1348 *
1349 * The security blob for a file is a pointer to the master
1350 * label list, so no allocation is done.
1351 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001352 * f_security is the owner security information. It
1353 * isn't used on file access checks, it's for send_sigio.
1354 *
Casey Schauflere114e472008-02-04 22:29:50 -08001355 * Returns 0
1356 */
1357static int smack_file_alloc_security(struct file *file)
1358{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001359 struct smack_known *skp = smk_of_current();
1360
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001361 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001362 return 0;
1363}
1364
1365/**
1366 * smack_file_free_security - clear a file security blob
1367 * @file: the object
1368 *
1369 * The security blob for a file is a pointer to the master
1370 * label list, so no memory is freed.
1371 */
1372static void smack_file_free_security(struct file *file)
1373{
1374 file->f_security = NULL;
1375}
1376
1377/**
1378 * smack_file_ioctl - Smack check on ioctls
1379 * @file: the object
1380 * @cmd: what to do
1381 * @arg: unused
1382 *
1383 * Relies heavily on the correct use of the ioctl command conventions.
1384 *
1385 * Returns 0 if allowed, error code otherwise
1386 */
1387static int smack_file_ioctl(struct file *file, unsigned int cmd,
1388 unsigned long arg)
1389{
1390 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001391 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001392 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001393
Eric Parisf48b7392011-04-25 12:54:27 -04001394 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001395 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001396
Casey Schauflerd166c802014-08-27 14:51:27 -07001397 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001398 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001399 rc = smk_bu_file(file, MAY_WRITE, rc);
1400 }
Casey Schauflere114e472008-02-04 22:29:50 -08001401
Casey Schauflerd166c802014-08-27 14:51:27 -07001402 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001403 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001404 rc = smk_bu_file(file, MAY_READ, rc);
1405 }
Casey Schauflere114e472008-02-04 22:29:50 -08001406
1407 return rc;
1408}
1409
1410/**
1411 * smack_file_lock - Smack check on file locking
1412 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001413 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001414 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001415 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001416 */
1417static int smack_file_lock(struct file *file, unsigned int cmd)
1418{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001419 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001420 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001421 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001422
Eric Paris92f42502011-04-25 13:15:55 -04001423 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1424 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001425 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001426 rc = smk_bu_file(file, MAY_LOCK, rc);
1427 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001428}
1429
1430/**
1431 * smack_file_fcntl - Smack check on fcntl
1432 * @file: the object
1433 * @cmd: what action to check
1434 * @arg: unused
1435 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001436 * Generally these operations are harmless.
1437 * File locking operations present an obvious mechanism
1438 * for passing information, so they require write access.
1439 *
Casey Schauflere114e472008-02-04 22:29:50 -08001440 * Returns 0 if current has access, error code otherwise
1441 */
1442static int smack_file_fcntl(struct file *file, unsigned int cmd,
1443 unsigned long arg)
1444{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001445 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001446 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001447 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001448
Casey Schauflere114e472008-02-04 22:29:50 -08001449 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001450 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001451 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001452 case F_SETLK:
1453 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001454 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1455 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001456 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001457 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001458 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001459 case F_SETOWN:
1460 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001461 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1462 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001463 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001464 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001465 break;
1466 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001467 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001468 }
1469
1470 return rc;
1471}
1472
1473/**
Al Viroe5467852012-05-30 13:30:51 -04001474 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001475 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1476 * if mapping anonymous memory.
1477 * @file contains the file structure for file to map (may be NULL).
1478 * @reqprot contains the protection requested by the application.
1479 * @prot contains the protection that will be applied by the kernel.
1480 * @flags contains the operational flags.
1481 * Return 0 if permission is granted.
1482 */
Al Viroe5467852012-05-30 13:30:51 -04001483static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001484 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001485 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001486{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001487 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001488 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001489 struct smack_rule *srp;
1490 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001491 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001492 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001493 int may;
1494 int mmay;
1495 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001496 int rc;
1497
Al Viro496ad9a2013-01-23 17:07:38 -05001498 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001499 return 0;
1500
Al Viro496ad9a2013-01-23 17:07:38 -05001501 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001502 if (isp->smk_mmap == NULL)
1503 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001504 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001505
1506 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001507 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001508 rc = 0;
1509
1510 rcu_read_lock();
1511 /*
1512 * For each Smack rule associated with the subject
1513 * label verify that the SMACK64MMAP also has access
1514 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001515 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001516 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001517 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001518 /*
1519 * Matching labels always allows access.
1520 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001521 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001522 continue;
1523 /*
1524 * If there is a matching local rule take
1525 * that into account as well.
1526 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001527 may = smk_access_entry(srp->smk_subject->smk_known,
1528 okp->smk_known,
1529 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001530 if (may == -ENOENT)
1531 may = srp->smk_access;
1532 else
1533 may &= srp->smk_access;
1534 /*
1535 * If may is zero the SMACK64MMAP subject can't
1536 * possibly have less access.
1537 */
1538 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001539 continue;
1540
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001541 /*
1542 * Fetch the global list entry.
1543 * If there isn't one a SMACK64MMAP subject
1544 * can't have as much access as current.
1545 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001546 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1547 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001548 if (mmay == -ENOENT) {
1549 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001550 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001551 }
1552 /*
1553 * If there is a local entry it modifies the
1554 * potential access, too.
1555 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001556 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1557 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001558 if (tmay != -ENOENT)
1559 mmay &= tmay;
1560
1561 /*
1562 * If there is any access available to current that is
1563 * not available to a SMACK64MMAP subject
1564 * deny access.
1565 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001566 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001567 rc = -EACCES;
1568 break;
1569 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001570 }
1571
1572 rcu_read_unlock();
1573
1574 return rc;
1575}
1576
1577/**
Casey Schauflere114e472008-02-04 22:29:50 -08001578 * smack_file_set_fowner - set the file security blob value
1579 * @file: object in question
1580 *
Casey Schauflere114e472008-02-04 22:29:50 -08001581 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001582static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001583{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001584 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001585}
1586
1587/**
1588 * smack_file_send_sigiotask - Smack on sigio
1589 * @tsk: The target task
1590 * @fown: the object the signal come from
1591 * @signum: unused
1592 *
1593 * Allow a privileged task to get signals even if it shouldn't
1594 *
1595 * Returns 0 if a subject with the object's smack could
1596 * write to the task, an error code otherwise.
1597 */
1598static int smack_file_send_sigiotask(struct task_struct *tsk,
1599 struct fown_struct *fown, int signum)
1600{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001601 struct smack_known *skp;
1602 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001603 struct file *file;
1604 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001605 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001606
1607 /*
1608 * struct fown_struct is never outside the context of a struct file
1609 */
1610 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001611
Etienne Bassetecfcc532009-04-08 20:40:06 +02001612 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001613 skp = file->f_security;
1614 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1615 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001616 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001617 rc = 0;
1618
1619 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1620 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001621 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001622 return rc;
1623}
1624
1625/**
1626 * smack_file_receive - Smack file receive check
1627 * @file: the object
1628 *
1629 * Returns 0 if current has access, error code otherwise
1630 */
1631static int smack_file_receive(struct file *file)
1632{
Casey Schauflerd166c802014-08-27 14:51:27 -07001633 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001634 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001635 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001636 struct inode *inode = file_inode(file);
Casey Schauflere114e472008-02-04 22:29:50 -08001637
Casey Schaufler4482a442013-12-30 17:37:45 -08001638 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001639 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001640 /*
1641 * This code relies on bitmasks.
1642 */
1643 if (file->f_mode & FMODE_READ)
1644 may = MAY_READ;
1645 if (file->f_mode & FMODE_WRITE)
1646 may |= MAY_WRITE;
1647
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001648 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001649 rc = smk_bu_file(file, may, rc);
1650 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001651}
1652
Casey Schaufler531f1d42011-09-19 12:41:42 -07001653/**
Eric Paris83d49852012-04-04 13:45:40 -04001654 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001655 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001656 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001657 *
1658 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001659 * Allow the open only if the task has read access. There are
1660 * many read operations (e.g. fstat) that you can do with an
1661 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001662 *
1663 * Returns 0
1664 */
Eric Paris83d49852012-04-04 13:45:40 -04001665static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001666{
Casey Schauflera6834c02014-04-21 11:10:26 -07001667 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001668 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001669 struct smk_audit_info ad;
1670 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001671
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001672 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001673 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001674
Casey Schauflera6834c02014-04-21 11:10:26 -07001675 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1676 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001677 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001678 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001679
1680 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001681}
1682
Casey Schauflere114e472008-02-04 22:29:50 -08001683/*
1684 * Task hooks
1685 */
1686
1687/**
David Howellsee18d642009-09-02 09:14:21 +01001688 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1689 * @new: the new credentials
1690 * @gfp: the atomicity of any memory allocations
1691 *
1692 * Prepare a blank set of credentials for modification. This must allocate all
1693 * the memory the LSM module might require such that cred_transfer() can
1694 * complete without error.
1695 */
1696static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1697{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001698 struct task_smack *tsp;
1699
1700 tsp = new_task_smack(NULL, NULL, gfp);
1701 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001702 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001703
1704 cred->security = tsp;
1705
David Howellsee18d642009-09-02 09:14:21 +01001706 return 0;
1707}
1708
1709
1710/**
David Howellsf1752ee2008-11-14 10:39:17 +11001711 * smack_cred_free - "free" task-level security credentials
1712 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001713 *
Casey Schauflere114e472008-02-04 22:29:50 -08001714 */
David Howellsf1752ee2008-11-14 10:39:17 +11001715static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001716{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001717 struct task_smack *tsp = cred->security;
1718 struct smack_rule *rp;
1719 struct list_head *l;
1720 struct list_head *n;
1721
1722 if (tsp == NULL)
1723 return;
1724 cred->security = NULL;
1725
1726 list_for_each_safe(l, n, &tsp->smk_rules) {
1727 rp = list_entry(l, struct smack_rule, list);
1728 list_del(&rp->list);
1729 kfree(rp);
1730 }
1731 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001732}
1733
1734/**
David Howellsd84f4f92008-11-14 10:39:23 +11001735 * smack_cred_prepare - prepare new set of credentials for modification
1736 * @new: the new credentials
1737 * @old: the original credentials
1738 * @gfp: the atomicity of any memory allocations
1739 *
1740 * Prepare a new set of credentials for modification.
1741 */
1742static int smack_cred_prepare(struct cred *new, const struct cred *old,
1743 gfp_t gfp)
1744{
Casey Schaufler676dac42010-12-02 06:43:39 -08001745 struct task_smack *old_tsp = old->security;
1746 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001747 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001748
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001749 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001750 if (new_tsp == NULL)
1751 return -ENOMEM;
1752
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001753 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1754 if (rc != 0)
1755 return rc;
1756
Casey Schaufler676dac42010-12-02 06:43:39 -08001757 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001758 return 0;
1759}
1760
Randy Dunlap251a2a92009-02-18 11:42:33 -08001761/**
David Howellsee18d642009-09-02 09:14:21 +01001762 * smack_cred_transfer - Transfer the old credentials to the new credentials
1763 * @new: the new credentials
1764 * @old: the original credentials
1765 *
1766 * Fill in a set of blank credentials from another set of credentials.
1767 */
1768static void smack_cred_transfer(struct cred *new, const struct cred *old)
1769{
Casey Schaufler676dac42010-12-02 06:43:39 -08001770 struct task_smack *old_tsp = old->security;
1771 struct task_smack *new_tsp = new->security;
1772
1773 new_tsp->smk_task = old_tsp->smk_task;
1774 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001775 mutex_init(&new_tsp->smk_rules_lock);
1776 INIT_LIST_HEAD(&new_tsp->smk_rules);
1777
1778
1779 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001780}
1781
1782/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001783 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001784 * @new: points to the set of credentials to be modified.
1785 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001786 *
1787 * Set the security data for a kernel service.
1788 */
1789static int smack_kernel_act_as(struct cred *new, u32 secid)
1790{
Casey Schaufler676dac42010-12-02 06:43:39 -08001791 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001792 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001793
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001794 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001795 return -EINVAL;
1796
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001797 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001798 return 0;
1799}
1800
1801/**
1802 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001803 * @new: points to the set of credentials to be modified
1804 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001805 *
1806 * Set the file creation context in a set of credentials to the same
1807 * as the objective context of the specified inode
1808 */
1809static int smack_kernel_create_files_as(struct cred *new,
1810 struct inode *inode)
1811{
1812 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001813 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001814
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001815 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001816 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001817 return 0;
1818}
1819
1820/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001821 * smk_curacc_on_task - helper to log task related access
1822 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001823 * @access: the access requested
1824 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001825 *
1826 * Return 0 if access is permitted
1827 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001828static int smk_curacc_on_task(struct task_struct *p, int access,
1829 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001830{
1831 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001832 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001833 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001834
Casey Schaufler531f1d42011-09-19 12:41:42 -07001835 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001836 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001837 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001838 rc = smk_bu_task(p, access, rc);
1839 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001840}
1841
1842/**
Casey Schauflere114e472008-02-04 22:29:50 -08001843 * smack_task_setpgid - Smack check on setting pgid
1844 * @p: the task object
1845 * @pgid: unused
1846 *
1847 * Return 0 if write access is permitted
1848 */
1849static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1850{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001851 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001852}
1853
1854/**
1855 * smack_task_getpgid - Smack access check for getpgid
1856 * @p: the object task
1857 *
1858 * Returns 0 if current can read the object task, error code otherwise
1859 */
1860static int smack_task_getpgid(struct task_struct *p)
1861{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001862 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001863}
1864
1865/**
1866 * smack_task_getsid - Smack access check for getsid
1867 * @p: the object task
1868 *
1869 * Returns 0 if current can read the object task, error code otherwise
1870 */
1871static int smack_task_getsid(struct task_struct *p)
1872{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001873 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001874}
1875
1876/**
1877 * smack_task_getsecid - get the secid of the task
1878 * @p: the object task
1879 * @secid: where to put the result
1880 *
1881 * Sets the secid to contain a u32 version of the smack label.
1882 */
1883static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1884{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001885 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001886
1887 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001888}
1889
1890/**
1891 * smack_task_setnice - Smack check on setting nice
1892 * @p: the task object
1893 * @nice: unused
1894 *
1895 * Return 0 if write access is permitted
1896 */
1897static int smack_task_setnice(struct task_struct *p, int nice)
1898{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001899 int rc;
1900
1901 rc = cap_task_setnice(p, nice);
1902 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001903 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001904 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001905}
1906
1907/**
1908 * smack_task_setioprio - Smack check on setting ioprio
1909 * @p: the task object
1910 * @ioprio: unused
1911 *
1912 * Return 0 if write access is permitted
1913 */
1914static int smack_task_setioprio(struct task_struct *p, int ioprio)
1915{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001916 int rc;
1917
1918 rc = cap_task_setioprio(p, ioprio);
1919 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001920 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001921 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001922}
1923
1924/**
1925 * smack_task_getioprio - Smack check on reading ioprio
1926 * @p: the task object
1927 *
1928 * Return 0 if read access is permitted
1929 */
1930static int smack_task_getioprio(struct task_struct *p)
1931{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001932 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001933}
1934
1935/**
1936 * smack_task_setscheduler - Smack check on setting scheduler
1937 * @p: the task object
1938 * @policy: unused
1939 * @lp: unused
1940 *
1941 * Return 0 if read access is permitted
1942 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001943static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001944{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001945 int rc;
1946
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001947 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001948 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001949 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001950 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001951}
1952
1953/**
1954 * smack_task_getscheduler - Smack check on reading scheduler
1955 * @p: the task object
1956 *
1957 * Return 0 if read access is permitted
1958 */
1959static int smack_task_getscheduler(struct task_struct *p)
1960{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001961 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001962}
1963
1964/**
1965 * smack_task_movememory - Smack check on moving memory
1966 * @p: the task object
1967 *
1968 * Return 0 if write access is permitted
1969 */
1970static int smack_task_movememory(struct task_struct *p)
1971{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001972 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001973}
1974
1975/**
1976 * smack_task_kill - Smack check on signal delivery
1977 * @p: the task object
1978 * @info: unused
1979 * @sig: unused
1980 * @secid: identifies the smack to use in lieu of current's
1981 *
1982 * Return 0 if write access is permitted
1983 *
1984 * The secid behavior is an artifact of an SELinux hack
1985 * in the USB code. Someday it may go away.
1986 */
1987static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1988 int sig, u32 secid)
1989{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001990 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001991 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001992 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001993 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001994
1995 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1996 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001997 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001998 * Sending a signal requires that the sender
1999 * can write the receiver.
2000 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002001 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002002 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002003 rc = smk_bu_task(p, MAY_WRITE, rc);
2004 return rc;
2005 }
Casey Schauflere114e472008-02-04 22:29:50 -08002006 /*
2007 * If the secid isn't 0 we're dealing with some USB IO
2008 * specific behavior. This is not clean. For one thing
2009 * we can't take privilege into account.
2010 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002011 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002012 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2013 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002014 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002015}
2016
2017/**
2018 * smack_task_wait - Smack access check for waiting
2019 * @p: task to wait for
2020 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002021 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002022 */
2023static int smack_task_wait(struct task_struct *p)
2024{
Casey Schauflere114e472008-02-04 22:29:50 -08002025 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002026 * Allow the operation to succeed.
2027 * Zombies are bad.
2028 * In userless environments (e.g. phones) programs
2029 * get marked with SMACK64EXEC and even if the parent
2030 * and child shouldn't be talking the parent still
2031 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002032 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002033 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002034}
2035
2036/**
2037 * smack_task_to_inode - copy task smack into the inode blob
2038 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002039 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002040 *
2041 * Sets the smack pointer in the inode security blob
2042 */
2043static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2044{
2045 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002046 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002047
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002048 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002049}
2050
2051/*
2052 * Socket hooks.
2053 */
2054
2055/**
2056 * smack_sk_alloc_security - Allocate a socket blob
2057 * @sk: the socket
2058 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002059 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002060 *
2061 * Assign Smack pointers to current
2062 *
2063 * Returns 0 on success, -ENOMEM is there's no memory
2064 */
2065static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2066{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002067 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002068 struct socket_smack *ssp;
2069
2070 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2071 if (ssp == NULL)
2072 return -ENOMEM;
2073
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002074 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002075 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002076 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002077
2078 sk->sk_security = ssp;
2079
2080 return 0;
2081}
2082
2083/**
2084 * smack_sk_free_security - Free a socket blob
2085 * @sk: the socket
2086 *
2087 * Clears the blob pointer
2088 */
2089static void smack_sk_free_security(struct sock *sk)
2090{
2091 kfree(sk->sk_security);
2092}
2093
2094/**
Paul Moore07feee82009-03-27 17:10:54 -04002095* smack_host_label - check host based restrictions
2096* @sip: the object end
2097*
2098* looks for host based access restrictions
2099*
2100* This version will only be appropriate for really small sets of single label
2101* hosts. The caller is responsible for ensuring that the RCU read lock is
2102* taken before calling this function.
2103*
2104* Returns the label of the far end or NULL if it's not special.
2105*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002106static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002107{
2108 struct smk_netlbladdr *snp;
2109 struct in_addr *siap = &sip->sin_addr;
2110
2111 if (siap->s_addr == 0)
2112 return NULL;
2113
2114 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2115 /*
2116 * we break after finding the first match because
2117 * the list is sorted from longest to shortest mask
2118 * so we have found the most specific match
2119 */
2120 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002121 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2122 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002123 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002124 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002125 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002126 }
Paul Moore07feee82009-03-27 17:10:54 -04002127
2128 return NULL;
2129}
2130
2131/**
Casey Schauflere114e472008-02-04 22:29:50 -08002132 * smack_netlabel - Set the secattr on a socket
2133 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002134 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002135 *
2136 * Convert the outbound smack value (smk_out) to a
2137 * secattr and attach it to the socket.
2138 *
2139 * Returns 0 on success or an error code
2140 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002141static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002142{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002143 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002144 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002145 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002146
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002147 /*
2148 * Usually the netlabel code will handle changing the
2149 * packet labeling based on the label.
2150 * The case of a single label host is different, because
2151 * a single label host should never get a labeled packet
2152 * even though the label is usually associated with a packet
2153 * label.
2154 */
2155 local_bh_disable();
2156 bh_lock_sock_nested(sk);
2157
2158 if (ssp->smk_out == smack_net_ambient ||
2159 labeled == SMACK_UNLABELED_SOCKET)
2160 netlbl_sock_delattr(sk);
2161 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002162 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002163 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002164 }
2165
2166 bh_unlock_sock(sk);
2167 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002168
Casey Schauflere114e472008-02-04 22:29:50 -08002169 return rc;
2170}
2171
2172/**
Paul Moore07feee82009-03-27 17:10:54 -04002173 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2174 * @sk: the socket
2175 * @sap: the destination address
2176 *
2177 * Set the correct secattr for the given socket based on the destination
2178 * address and perform any outbound access checks needed.
2179 *
2180 * Returns 0 on success or an error code.
2181 *
2182 */
2183static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2184{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002185 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002186 int rc;
2187 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002188 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002189 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002190 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002191
2192 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002193 hkp = smack_host_label(sap);
2194 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002195#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002196 struct lsm_network_audit net;
2197
Eric Paris48c62af2012-04-02 13:15:44 -04002198 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2199 ad.a.u.net->family = sap->sin_family;
2200 ad.a.u.net->dport = sap->sin_port;
2201 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002202#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002203 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002204 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002205 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2206 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002207 } else {
2208 sk_lbl = SMACK_CIPSO_SOCKET;
2209 rc = 0;
2210 }
2211 rcu_read_unlock();
2212 if (rc != 0)
2213 return rc;
2214
2215 return smack_netlabel(sk, sk_lbl);
2216}
2217
Casey Schaufler69f287a2014-12-12 17:08:40 -08002218#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Paul Moore07feee82009-03-27 17:10:54 -04002219/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002220 * smk_ipv6_port_label - Smack port access table management
2221 * @sock: socket
2222 * @address: address
2223 *
2224 * Create or update the port list entry
2225 */
2226static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2227{
2228 struct sock *sk = sock->sk;
2229 struct sockaddr_in6 *addr6;
2230 struct socket_smack *ssp = sock->sk->sk_security;
2231 struct smk_port_label *spp;
2232 unsigned short port = 0;
2233
2234 if (address == NULL) {
2235 /*
2236 * This operation is changing the Smack information
2237 * on the bound socket. Take the changes to the port
2238 * as well.
2239 */
2240 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2241 if (sk != spp->smk_sock)
2242 continue;
2243 spp->smk_in = ssp->smk_in;
2244 spp->smk_out = ssp->smk_out;
2245 return;
2246 }
2247 /*
2248 * A NULL address is only used for updating existing
2249 * bound entries. If there isn't one, it's OK.
2250 */
2251 return;
2252 }
2253
2254 addr6 = (struct sockaddr_in6 *)address;
2255 port = ntohs(addr6->sin6_port);
2256 /*
2257 * This is a special case that is safely ignored.
2258 */
2259 if (port == 0)
2260 return;
2261
2262 /*
2263 * Look for an existing port list entry.
2264 * This is an indication that a port is getting reused.
2265 */
2266 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2267 if (spp->smk_port != port)
2268 continue;
2269 spp->smk_port = port;
2270 spp->smk_sock = sk;
2271 spp->smk_in = ssp->smk_in;
2272 spp->smk_out = ssp->smk_out;
2273 return;
2274 }
2275
2276 /*
2277 * A new port entry is required.
2278 */
2279 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2280 if (spp == NULL)
2281 return;
2282
2283 spp->smk_port = port;
2284 spp->smk_sock = sk;
2285 spp->smk_in = ssp->smk_in;
2286 spp->smk_out = ssp->smk_out;
2287
2288 list_add(&spp->list, &smk_ipv6_port_list);
2289 return;
2290}
2291
2292/**
2293 * smk_ipv6_port_check - check Smack port access
2294 * @sock: socket
2295 * @address: address
2296 *
2297 * Create or update the port list entry
2298 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002299static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002300 int act)
2301{
2302 __be16 *bep;
2303 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002304 struct smk_port_label *spp;
2305 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002306 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002307 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002308 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002309 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002310 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002311#ifdef CONFIG_AUDIT
2312 struct lsm_network_audit net;
2313#endif
2314
2315 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002316 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002317 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002318 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002319 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002320 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002321 }
2322
2323 /*
2324 * Get the IP address and port from the address.
2325 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002326 port = ntohs(address->sin6_port);
2327 bep = (__be16 *)(&address->sin6_addr);
2328 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002329
2330 /*
2331 * It's remote, so port lookup does no good.
2332 */
2333 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2334 goto auditout;
2335
2336 /*
2337 * It's local so the send check has to have passed.
2338 */
2339 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002340 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002341 goto auditout;
2342 }
2343
2344 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2345 if (spp->smk_port != port)
2346 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002347 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002348 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002349 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002350 break;
2351 }
2352
2353auditout:
2354
2355#ifdef CONFIG_AUDIT
2356 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2357 ad.a.u.net->family = sk->sk_family;
2358 ad.a.u.net->dport = port;
2359 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002360 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002361 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002362 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002363#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002364 rc = smk_access(skp, object, MAY_WRITE, &ad);
2365 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2366 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002367}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002368#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002369
2370/**
Casey Schauflere114e472008-02-04 22:29:50 -08002371 * smack_inode_setsecurity - set smack xattrs
2372 * @inode: the object
2373 * @name: attribute name
2374 * @value: attribute value
2375 * @size: size of the attribute
2376 * @flags: unused
2377 *
2378 * Sets the named attribute in the appropriate blob
2379 *
2380 * Returns 0 on success, or an error code
2381 */
2382static int smack_inode_setsecurity(struct inode *inode, const char *name,
2383 const void *value, size_t size, int flags)
2384{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002385 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002386 struct inode_smack *nsp = inode->i_security;
2387 struct socket_smack *ssp;
2388 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002389 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002390
Casey Schauflerf7112e62012-05-06 15:22:02 -07002391 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302392 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002393
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002394 skp = smk_import_entry(value, size);
2395 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002396 return -EINVAL;
2397
2398 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002399 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002400 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002401 return 0;
2402 }
2403 /*
2404 * The rest of the Smack xattrs are only on sockets.
2405 */
2406 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2407 return -EOPNOTSUPP;
2408
2409 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002410 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002411 return -EOPNOTSUPP;
2412
2413 ssp = sock->sk->sk_security;
2414
2415 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002416 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002417 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002418 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002419 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002420 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2421 if (rc != 0)
2422 printk(KERN_WARNING
2423 "Smack: \"%s\" netlbl error %d.\n",
2424 __func__, -rc);
2425 }
Casey Schauflere114e472008-02-04 22:29:50 -08002426 } else
2427 return -EOPNOTSUPP;
2428
Casey Schaufler69f287a2014-12-12 17:08:40 -08002429#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07002430 if (sock->sk->sk_family == PF_INET6)
2431 smk_ipv6_port_label(sock, NULL);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002432#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002433
Casey Schauflere114e472008-02-04 22:29:50 -08002434 return 0;
2435}
2436
2437/**
2438 * smack_socket_post_create - finish socket setup
2439 * @sock: the socket
2440 * @family: protocol family
2441 * @type: unused
2442 * @protocol: unused
2443 * @kern: unused
2444 *
2445 * Sets the netlabel information on the socket
2446 *
2447 * Returns 0 on success, and error code otherwise
2448 */
2449static int smack_socket_post_create(struct socket *sock, int family,
2450 int type, int protocol, int kern)
2451{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002452 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002453 return 0;
2454 /*
2455 * Set the outbound netlbl.
2456 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002457 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2458}
2459
Casey Schaufler69f287a2014-12-12 17:08:40 -08002460#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002461/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002462 * smack_socket_bind - record port binding information.
2463 * @sock: the socket
2464 * @address: the port address
2465 * @addrlen: size of the address
2466 *
2467 * Records the label bound to a port.
2468 *
2469 * Returns 0
2470 */
2471static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2472 int addrlen)
2473{
Casey Schaufler69f287a2014-12-12 17:08:40 -08002474#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002475 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2476 smk_ipv6_port_label(sock, address);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002477#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002478
2479 return 0;
2480}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002481#endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002482
2483/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002484 * smack_socket_connect - connect access check
2485 * @sock: the socket
2486 * @sap: the other end
2487 * @addrlen: size of sap
2488 *
2489 * Verifies that a connection may be possible
2490 *
2491 * Returns 0 on success, and error code otherwise
2492 */
2493static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2494 int addrlen)
2495{
Casey Schauflerc6739442013-05-22 18:42:56 -07002496 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002497
Casey Schauflerc6739442013-05-22 18:42:56 -07002498 if (sock->sk == NULL)
2499 return 0;
2500
2501 switch (sock->sk->sk_family) {
2502 case PF_INET:
2503 if (addrlen < sizeof(struct sockaddr_in))
2504 return -EINVAL;
2505 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2506 break;
2507 case PF_INET6:
2508 if (addrlen < sizeof(struct sockaddr_in6))
2509 return -EINVAL;
Casey Schaufler69f287a2014-12-12 17:08:40 -08002510#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002511 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2512 SMK_CONNECTING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002513#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002514 break;
2515 }
2516 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002517}
2518
2519/**
2520 * smack_flags_to_may - convert S_ to MAY_ values
2521 * @flags: the S_ value
2522 *
2523 * Returns the equivalent MAY_ value
2524 */
2525static int smack_flags_to_may(int flags)
2526{
2527 int may = 0;
2528
2529 if (flags & S_IRUGO)
2530 may |= MAY_READ;
2531 if (flags & S_IWUGO)
2532 may |= MAY_WRITE;
2533 if (flags & S_IXUGO)
2534 may |= MAY_EXEC;
2535
2536 return may;
2537}
2538
2539/**
2540 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2541 * @msg: the object
2542 *
2543 * Returns 0
2544 */
2545static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2546{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002547 struct smack_known *skp = smk_of_current();
2548
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002549 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002550 return 0;
2551}
2552
2553/**
2554 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2555 * @msg: the object
2556 *
2557 * Clears the blob pointer
2558 */
2559static void smack_msg_msg_free_security(struct msg_msg *msg)
2560{
2561 msg->security = NULL;
2562}
2563
2564/**
2565 * smack_of_shm - the smack pointer for the shm
2566 * @shp: the object
2567 *
2568 * Returns a pointer to the smack value
2569 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002570static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002571{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002572 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002573}
2574
2575/**
2576 * smack_shm_alloc_security - Set the security blob for shm
2577 * @shp: the object
2578 *
2579 * Returns 0
2580 */
2581static int smack_shm_alloc_security(struct shmid_kernel *shp)
2582{
2583 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002584 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002585
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002586 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002587 return 0;
2588}
2589
2590/**
2591 * smack_shm_free_security - Clear the security blob for shm
2592 * @shp: the object
2593 *
2594 * Clears the blob pointer
2595 */
2596static void smack_shm_free_security(struct shmid_kernel *shp)
2597{
2598 struct kern_ipc_perm *isp = &shp->shm_perm;
2599
2600 isp->security = NULL;
2601}
2602
2603/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002604 * smk_curacc_shm : check if current has access on shm
2605 * @shp : the object
2606 * @access : access requested
2607 *
2608 * Returns 0 if current has the requested access, error code otherwise
2609 */
2610static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2611{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002612 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002613 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002614 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002615
2616#ifdef CONFIG_AUDIT
2617 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2618 ad.a.u.ipc_id = shp->shm_perm.id;
2619#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002620 rc = smk_curacc(ssp, access, &ad);
2621 rc = smk_bu_current("shm", ssp, access, rc);
2622 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002623}
2624
2625/**
Casey Schauflere114e472008-02-04 22:29:50 -08002626 * smack_shm_associate - Smack access check for shm
2627 * @shp: the object
2628 * @shmflg: access requested
2629 *
2630 * Returns 0 if current has the requested access, error code otherwise
2631 */
2632static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2633{
Casey Schauflere114e472008-02-04 22:29:50 -08002634 int may;
2635
2636 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002637 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002638}
2639
2640/**
2641 * smack_shm_shmctl - Smack access check for shm
2642 * @shp: the object
2643 * @cmd: what it wants to do
2644 *
2645 * Returns 0 if current has the requested access, error code otherwise
2646 */
2647static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2648{
Casey Schauflere114e472008-02-04 22:29:50 -08002649 int may;
2650
2651 switch (cmd) {
2652 case IPC_STAT:
2653 case SHM_STAT:
2654 may = MAY_READ;
2655 break;
2656 case IPC_SET:
2657 case SHM_LOCK:
2658 case SHM_UNLOCK:
2659 case IPC_RMID:
2660 may = MAY_READWRITE;
2661 break;
2662 case IPC_INFO:
2663 case SHM_INFO:
2664 /*
2665 * System level information.
2666 */
2667 return 0;
2668 default:
2669 return -EINVAL;
2670 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002671 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002672}
2673
2674/**
2675 * smack_shm_shmat - Smack access for shmat
2676 * @shp: the object
2677 * @shmaddr: unused
2678 * @shmflg: access requested
2679 *
2680 * Returns 0 if current has the requested access, error code otherwise
2681 */
2682static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2683 int shmflg)
2684{
Casey Schauflere114e472008-02-04 22:29:50 -08002685 int may;
2686
2687 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002688 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002689}
2690
2691/**
2692 * smack_of_sem - the smack pointer for the sem
2693 * @sma: the object
2694 *
2695 * Returns a pointer to the smack value
2696 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002697static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002698{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002699 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002700}
2701
2702/**
2703 * smack_sem_alloc_security - Set the security blob for sem
2704 * @sma: the object
2705 *
2706 * Returns 0
2707 */
2708static int smack_sem_alloc_security(struct sem_array *sma)
2709{
2710 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002711 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002712
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002713 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002714 return 0;
2715}
2716
2717/**
2718 * smack_sem_free_security - Clear the security blob for sem
2719 * @sma: the object
2720 *
2721 * Clears the blob pointer
2722 */
2723static void smack_sem_free_security(struct sem_array *sma)
2724{
2725 struct kern_ipc_perm *isp = &sma->sem_perm;
2726
2727 isp->security = NULL;
2728}
2729
2730/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002731 * smk_curacc_sem : check if current has access on sem
2732 * @sma : the object
2733 * @access : access requested
2734 *
2735 * Returns 0 if current has the requested access, error code otherwise
2736 */
2737static int smk_curacc_sem(struct sem_array *sma, int access)
2738{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002739 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002740 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002741 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002742
2743#ifdef CONFIG_AUDIT
2744 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2745 ad.a.u.ipc_id = sma->sem_perm.id;
2746#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002747 rc = smk_curacc(ssp, access, &ad);
2748 rc = smk_bu_current("sem", ssp, access, rc);
2749 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002750}
2751
2752/**
Casey Schauflere114e472008-02-04 22:29:50 -08002753 * smack_sem_associate - Smack access check for sem
2754 * @sma: the object
2755 * @semflg: access requested
2756 *
2757 * Returns 0 if current has the requested access, error code otherwise
2758 */
2759static int smack_sem_associate(struct sem_array *sma, int semflg)
2760{
Casey Schauflere114e472008-02-04 22:29:50 -08002761 int may;
2762
2763 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002764 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002765}
2766
2767/**
2768 * smack_sem_shmctl - Smack access check for sem
2769 * @sma: the object
2770 * @cmd: what it wants to do
2771 *
2772 * Returns 0 if current has the requested access, error code otherwise
2773 */
2774static int smack_sem_semctl(struct sem_array *sma, int cmd)
2775{
Casey Schauflere114e472008-02-04 22:29:50 -08002776 int may;
2777
2778 switch (cmd) {
2779 case GETPID:
2780 case GETNCNT:
2781 case GETZCNT:
2782 case GETVAL:
2783 case GETALL:
2784 case IPC_STAT:
2785 case SEM_STAT:
2786 may = MAY_READ;
2787 break;
2788 case SETVAL:
2789 case SETALL:
2790 case IPC_RMID:
2791 case IPC_SET:
2792 may = MAY_READWRITE;
2793 break;
2794 case IPC_INFO:
2795 case SEM_INFO:
2796 /*
2797 * System level information
2798 */
2799 return 0;
2800 default:
2801 return -EINVAL;
2802 }
2803
Etienne Bassetecfcc532009-04-08 20:40:06 +02002804 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002805}
2806
2807/**
2808 * smack_sem_semop - Smack checks of semaphore operations
2809 * @sma: the object
2810 * @sops: unused
2811 * @nsops: unused
2812 * @alter: unused
2813 *
2814 * Treated as read and write in all cases.
2815 *
2816 * Returns 0 if access is allowed, error code otherwise
2817 */
2818static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2819 unsigned nsops, int alter)
2820{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002821 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002822}
2823
2824/**
2825 * smack_msg_alloc_security - Set the security blob for msg
2826 * @msq: the object
2827 *
2828 * Returns 0
2829 */
2830static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2831{
2832 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002833 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002834
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002835 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002836 return 0;
2837}
2838
2839/**
2840 * smack_msg_free_security - Clear the security blob for msg
2841 * @msq: the object
2842 *
2843 * Clears the blob pointer
2844 */
2845static void smack_msg_queue_free_security(struct msg_queue *msq)
2846{
2847 struct kern_ipc_perm *kisp = &msq->q_perm;
2848
2849 kisp->security = NULL;
2850}
2851
2852/**
2853 * smack_of_msq - the smack pointer for the msq
2854 * @msq: the object
2855 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002856 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002857 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002858static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002859{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002860 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002861}
2862
2863/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002864 * smk_curacc_msq : helper to check if current has access on msq
2865 * @msq : the msq
2866 * @access : access requested
2867 *
2868 * return 0 if current has access, error otherwise
2869 */
2870static int smk_curacc_msq(struct msg_queue *msq, int access)
2871{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002872 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002873 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002874 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002875
2876#ifdef CONFIG_AUDIT
2877 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2878 ad.a.u.ipc_id = msq->q_perm.id;
2879#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002880 rc = smk_curacc(msp, access, &ad);
2881 rc = smk_bu_current("msq", msp, access, rc);
2882 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002883}
2884
2885/**
Casey Schauflere114e472008-02-04 22:29:50 -08002886 * smack_msg_queue_associate - Smack access check for msg_queue
2887 * @msq: the object
2888 * @msqflg: access requested
2889 *
2890 * Returns 0 if current has the requested access, error code otherwise
2891 */
2892static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2893{
Casey Schauflere114e472008-02-04 22:29:50 -08002894 int may;
2895
2896 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002897 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002898}
2899
2900/**
2901 * smack_msg_queue_msgctl - Smack access check for msg_queue
2902 * @msq: the object
2903 * @cmd: what it wants to do
2904 *
2905 * Returns 0 if current has the requested access, error code otherwise
2906 */
2907static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2908{
Casey Schauflere114e472008-02-04 22:29:50 -08002909 int may;
2910
2911 switch (cmd) {
2912 case IPC_STAT:
2913 case MSG_STAT:
2914 may = MAY_READ;
2915 break;
2916 case IPC_SET:
2917 case IPC_RMID:
2918 may = MAY_READWRITE;
2919 break;
2920 case IPC_INFO:
2921 case MSG_INFO:
2922 /*
2923 * System level information
2924 */
2925 return 0;
2926 default:
2927 return -EINVAL;
2928 }
2929
Etienne Bassetecfcc532009-04-08 20:40:06 +02002930 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002931}
2932
2933/**
2934 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2935 * @msq: the object
2936 * @msg: unused
2937 * @msqflg: access requested
2938 *
2939 * Returns 0 if current has the requested access, error code otherwise
2940 */
2941static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2942 int msqflg)
2943{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002944 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002945
Etienne Bassetecfcc532009-04-08 20:40:06 +02002946 may = smack_flags_to_may(msqflg);
2947 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002948}
2949
2950/**
2951 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2952 * @msq: the object
2953 * @msg: unused
2954 * @target: unused
2955 * @type: unused
2956 * @mode: unused
2957 *
2958 * Returns 0 if current has read and write access, error code otherwise
2959 */
2960static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2961 struct task_struct *target, long type, int mode)
2962{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002963 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002964}
2965
2966/**
2967 * smack_ipc_permission - Smack access for ipc_permission()
2968 * @ipp: the object permissions
2969 * @flag: access requested
2970 *
2971 * Returns 0 if current has read and write access, error code otherwise
2972 */
2973static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2974{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002975 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002976 int may = smack_flags_to_may(flag);
2977 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002978 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002979
Etienne Bassetecfcc532009-04-08 20:40:06 +02002980#ifdef CONFIG_AUDIT
2981 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2982 ad.a.u.ipc_id = ipp->id;
2983#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002984 rc = smk_curacc(iskp, may, &ad);
2985 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002986 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002987}
2988
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002989/**
2990 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002991 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002992 * @secid: where result will be saved
2993 */
2994static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2995{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002996 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002997
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002998 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002999}
3000
Casey Schauflere114e472008-02-04 22:29:50 -08003001/**
3002 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003003 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003004 * @inode: the object
3005 *
3006 * Set the inode's security blob if it hasn't been done already.
3007 */
3008static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3009{
3010 struct super_block *sbp;
3011 struct superblock_smack *sbsp;
3012 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003013 struct smack_known *skp;
3014 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003015 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003016 char trattr[TRANS_TRUE_SIZE];
3017 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003018 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003019 struct dentry *dp;
3020
3021 if (inode == NULL)
3022 return;
3023
3024 isp = inode->i_security;
3025
3026 mutex_lock(&isp->smk_lock);
3027 /*
3028 * If the inode is already instantiated
3029 * take the quick way out
3030 */
3031 if (isp->smk_flags & SMK_INODE_INSTANT)
3032 goto unlockandout;
3033
3034 sbp = inode->i_sb;
3035 sbsp = sbp->s_security;
3036 /*
3037 * We're going to use the superblock default label
3038 * if there's no label on the file.
3039 */
3040 final = sbsp->smk_default;
3041
3042 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003043 * If this is the root inode the superblock
3044 * may be in the process of initialization.
3045 * If that is the case use the root value out
3046 * of the superblock.
3047 */
3048 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003049 switch (sbp->s_magic) {
3050 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003051 /*
3052 * The cgroup filesystem is never mounted,
3053 * so there's no opportunity to set the mount
3054 * options.
3055 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003056 sbsp->smk_root = &smack_known_star;
3057 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003058 isp->smk_inode = sbsp->smk_root;
3059 break;
3060 case TMPFS_MAGIC:
3061 /*
3062 * What about shmem/tmpfs anonymous files with dentry
3063 * obtained from d_alloc_pseudo()?
3064 */
3065 isp->smk_inode = smk_of_current();
3066 break;
3067 default:
3068 isp->smk_inode = sbsp->smk_root;
3069 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003070 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003071 isp->smk_flags |= SMK_INODE_INSTANT;
3072 goto unlockandout;
3073 }
3074
3075 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003076 * This is pretty hackish.
3077 * Casey says that we shouldn't have to do
3078 * file system specific code, but it does help
3079 * with keeping it simple.
3080 */
3081 switch (sbp->s_magic) {
3082 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003083 case PIPEFS_MAGIC:
3084 case SOCKFS_MAGIC:
3085 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003086 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003087 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003088 * that the smack file system doesn't do
3089 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003090 *
Casey Schauflere114e472008-02-04 22:29:50 -08003091 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003092 *
3093 * Socket access is controlled by the socket
3094 * structures associated with the task involved.
3095 *
3096 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003097 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003098 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003099 break;
3100 case DEVPTS_SUPER_MAGIC:
3101 /*
3102 * devpts seems content with the label of the task.
3103 * Programs that change smack have to treat the
3104 * pty with respect.
3105 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003106 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003107 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003108 case PROC_SUPER_MAGIC:
3109 /*
3110 * Casey says procfs appears not to care.
3111 * The superblock default suffices.
3112 */
3113 break;
3114 case TMPFS_MAGIC:
3115 /*
3116 * Device labels should come from the filesystem,
3117 * but watch out, because they're volitile,
3118 * getting recreated on every reboot.
3119 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003120 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003121 /*
3122 * No break.
3123 *
3124 * If a smack value has been set we want to use it,
3125 * but since tmpfs isn't giving us the opportunity
3126 * to set mount options simulate setting the
3127 * superblock default.
3128 */
3129 default:
3130 /*
3131 * This isn't an understood special case.
3132 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003133 */
3134
3135 /*
3136 * UNIX domain sockets use lower level socket data.
3137 */
3138 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003139 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003140 break;
3141 }
3142 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003143 * No xattr support means, alas, no SMACK label.
3144 * Use the aforeapplied default.
3145 * It would be curious if the label of the task
3146 * does not match that assigned.
3147 */
3148 if (inode->i_op->getxattr == NULL)
3149 break;
3150 /*
3151 * Get the dentry for xattr.
3152 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003153 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003154 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3155 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003156 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003157
3158 /*
3159 * Transmuting directory
3160 */
3161 if (S_ISDIR(inode->i_mode)) {
3162 /*
3163 * If this is a new directory and the label was
3164 * transmuted when the inode was initialized
3165 * set the transmute attribute on the directory
3166 * and mark the inode.
3167 *
3168 * If there is a transmute attribute on the
3169 * directory mark the inode.
3170 */
3171 if (isp->smk_flags & SMK_INODE_CHANGED) {
3172 isp->smk_flags &= ~SMK_INODE_CHANGED;
3173 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003174 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003175 TRANS_TRUE, TRANS_TRUE_SIZE,
3176 0);
3177 } else {
3178 rc = inode->i_op->getxattr(dp,
3179 XATTR_NAME_SMACKTRANSMUTE, trattr,
3180 TRANS_TRUE_SIZE);
3181 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3182 TRANS_TRUE_SIZE) != 0)
3183 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003184 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003185 if (rc >= 0)
3186 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003187 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003188 /*
3189 * Don't let the exec or mmap label be "*" or "@".
3190 */
3191 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3192 if (skp == &smack_known_star || skp == &smack_known_web)
3193 skp = NULL;
3194 isp->smk_task = skp;
3195 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3196 if (skp == &smack_known_star || skp == &smack_known_web)
3197 skp = NULL;
3198 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003199
Casey Schauflere114e472008-02-04 22:29:50 -08003200 dput(dp);
3201 break;
3202 }
3203
3204 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003205 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003206 else
3207 isp->smk_inode = final;
3208
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003209 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003210
3211unlockandout:
3212 mutex_unlock(&isp->smk_lock);
3213 return;
3214}
3215
3216/**
3217 * smack_getprocattr - Smack process attribute access
3218 * @p: the object task
3219 * @name: the name of the attribute in /proc/.../attr
3220 * @value: where to put the result
3221 *
3222 * Places a copy of the task Smack into value
3223 *
3224 * Returns the length of the smack label or an error code
3225 */
3226static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3227{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003228 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003229 char *cp;
3230 int slen;
3231
3232 if (strcmp(name, "current") != 0)
3233 return -EINVAL;
3234
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003235 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003236 if (cp == NULL)
3237 return -ENOMEM;
3238
3239 slen = strlen(cp);
3240 *value = cp;
3241 return slen;
3242}
3243
3244/**
3245 * smack_setprocattr - Smack process attribute setting
3246 * @p: the object task
3247 * @name: the name of the attribute in /proc/.../attr
3248 * @value: the value to set
3249 * @size: the size of the value
3250 *
3251 * Sets the Smack value of the task. Only setting self
3252 * is permitted and only with privilege
3253 *
3254 * Returns the length of the smack label or an error code
3255 */
3256static int smack_setprocattr(struct task_struct *p, char *name,
3257 void *value, size_t size)
3258{
Casey Schaufler676dac42010-12-02 06:43:39 -08003259 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003260 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003261 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003262
Casey Schauflere114e472008-02-04 22:29:50 -08003263 /*
3264 * Changing another process' Smack value is too dangerous
3265 * and supports no sane use case.
3266 */
3267 if (p != current)
3268 return -EPERM;
3269
Casey Schaufler1880eff2012-06-05 15:28:30 -07003270 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003271 return -EPERM;
3272
Casey Schauflerf7112e62012-05-06 15:22:02 -07003273 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003274 return -EINVAL;
3275
3276 if (strcmp(name, "current") != 0)
3277 return -EINVAL;
3278
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003279 skp = smk_import_entry(value, size);
3280 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003281 return -EINVAL;
3282
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003283 /*
3284 * No process is ever allowed the web ("@") label.
3285 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003286 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003287 return -EPERM;
3288
David Howellsd84f4f92008-11-14 10:39:23 +11003289 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003290 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003291 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003292
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003293 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003294 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003295
David Howellsd84f4f92008-11-14 10:39:23 +11003296 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003297 return size;
3298}
3299
3300/**
3301 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003302 * @sock: one sock
3303 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003304 * @newsk: unused
3305 *
3306 * Return 0 if a subject with the smack of sock could access
3307 * an object with the smack of other, otherwise an error code
3308 */
David S. Miller3610cda2011-01-05 15:38:53 -08003309static int smack_unix_stream_connect(struct sock *sock,
3310 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003311{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003312 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003313 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003314 struct socket_smack *ssp = sock->sk_security;
3315 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003316 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003317 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003318 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003319#ifdef CONFIG_AUDIT
3320 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003321#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003322
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003323 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3324 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003325 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003326#ifdef CONFIG_AUDIT
3327 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3328 smk_ad_setfield_u_net_sk(&ad, other);
3329#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003330 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3331 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003332 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003333 okp = osp->smk_out;
3334 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003335 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003336 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003337 MAY_WRITE, rc);
3338 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003339 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003340
Casey Schaufler975d5e52011-09-26 14:43:39 -07003341 /*
3342 * Cross reference the peer labels for SO_PEERSEC.
3343 */
3344 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003345 nsp->smk_packet = ssp->smk_out;
3346 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003347 }
3348
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003349 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003350}
3351
3352/**
3353 * smack_unix_may_send - Smack access on UDS
3354 * @sock: one socket
3355 * @other: the other socket
3356 *
3357 * Return 0 if a subject with the smack of sock could access
3358 * an object with the smack of other, otherwise an error code
3359 */
3360static int smack_unix_may_send(struct socket *sock, struct socket *other)
3361{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003362 struct socket_smack *ssp = sock->sk->sk_security;
3363 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003364 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003365 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003366
Kees Cook923e9a12012-04-10 13:26:44 -07003367#ifdef CONFIG_AUDIT
3368 struct lsm_network_audit net;
3369
Eric Paris48c62af2012-04-02 13:15:44 -04003370 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003371 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003372#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003373
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003374 if (smack_privileged(CAP_MAC_OVERRIDE))
3375 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003376
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003377 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3378 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003379 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003380}
3381
3382/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003383 * smack_socket_sendmsg - Smack check based on destination host
3384 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003385 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003386 * @size: the size of the message
3387 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003388 * Return 0 if the current subject can write to the destination host.
3389 * For IPv4 this is only a question if the destination is a single label host.
3390 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003391 */
3392static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3393 int size)
3394{
3395 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003396#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003397 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003398#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003399 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003400
3401 /*
3402 * Perfectly reasonable for this to be NULL
3403 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003404 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003405 return 0;
3406
Casey Schauflerc6739442013-05-22 18:42:56 -07003407 switch (sip->sin_family) {
3408 case AF_INET:
3409 rc = smack_netlabel_send(sock->sk, sip);
3410 break;
3411 case AF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003412#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07003413 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003414#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003415 break;
3416 }
3417 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003418}
3419
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003420/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003421 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003422 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003423 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003424 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003425 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003426 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003427static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3428 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003429{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003430 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003431 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003432 int acat;
3433 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003434
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003435 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003436 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003437 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003438 * If there are flags but no level netlabel isn't
3439 * behaving the way we expect it to.
3440 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003441 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003442 * Without guidance regarding the smack value
3443 * for the packet fall back on the network
3444 * ambient value.
3445 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003446 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003447 list_for_each_entry(skp, &smack_known_list, list) {
3448 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003449 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003450 /*
3451 * Compare the catsets. Use the netlbl APIs.
3452 */
3453 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3454 if ((skp->smk_netlabel.flags &
3455 NETLBL_SECATTR_MLS_CAT) == 0)
3456 found = 1;
3457 break;
3458 }
3459 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003460 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3461 acat + 1);
3462 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003463 skp->smk_netlabel.attr.mls.cat,
3464 kcat + 1);
3465 if (acat < 0 || kcat < 0)
3466 break;
3467 }
3468 if (acat == kcat) {
3469 found = 1;
3470 break;
3471 }
Casey Schauflere114e472008-02-04 22:29:50 -08003472 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003473 rcu_read_unlock();
3474
3475 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003476 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003477
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003478 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003479 return &smack_known_web;
3480 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003481 }
3482 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3483 /*
3484 * Looks like a fallback, which gives us a secid.
3485 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003486 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003487 /*
3488 * This has got to be a bug because it is
3489 * impossible to specify a fallback without
3490 * specifying the label, which will ensure
3491 * it has a secid, and the only way to get a
3492 * secid is from a fallback.
3493 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003494 BUG_ON(skp == NULL);
3495 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003496 }
3497 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003498 * Without guidance regarding the smack value
3499 * for the packet fall back on the network
3500 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003501 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003502 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003503}
3504
Casey Schaufler69f287a2014-12-12 17:08:40 -08003505#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003506static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003507{
Casey Schauflerc6739442013-05-22 18:42:56 -07003508 u8 nexthdr;
3509 int offset;
3510 int proto = -EINVAL;
3511 struct ipv6hdr _ipv6h;
3512 struct ipv6hdr *ip6;
3513 __be16 frag_off;
3514 struct tcphdr _tcph, *th;
3515 struct udphdr _udph, *uh;
3516 struct dccp_hdr _dccph, *dh;
3517
3518 sip->sin6_port = 0;
3519
3520 offset = skb_network_offset(skb);
3521 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3522 if (ip6 == NULL)
3523 return -EINVAL;
3524 sip->sin6_addr = ip6->saddr;
3525
3526 nexthdr = ip6->nexthdr;
3527 offset += sizeof(_ipv6h);
3528 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3529 if (offset < 0)
3530 return -EINVAL;
3531
3532 proto = nexthdr;
3533 switch (proto) {
3534 case IPPROTO_TCP:
3535 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3536 if (th != NULL)
3537 sip->sin6_port = th->source;
3538 break;
3539 case IPPROTO_UDP:
3540 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3541 if (uh != NULL)
3542 sip->sin6_port = uh->source;
3543 break;
3544 case IPPROTO_DCCP:
3545 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3546 if (dh != NULL)
3547 sip->sin6_port = dh->dccph_sport;
3548 break;
3549 }
3550 return proto;
3551}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003552#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003553
Casey Schauflere114e472008-02-04 22:29:50 -08003554/**
3555 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3556 * @sk: socket
3557 * @skb: packet
3558 *
3559 * Returns 0 if the packet should be delivered, an error code otherwise
3560 */
3561static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3562{
3563 struct netlbl_lsm_secattr secattr;
3564 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003565 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003566 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003567 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003568#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003569 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003570#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003571#if IS_ENABLED(CONFIG_IPV6)
3572 struct sockaddr_in6 sadd;
3573 int proto;
3574#endif /* CONFIG_IPV6 */
3575
Casey Schauflerc6739442013-05-22 18:42:56 -07003576 switch (sk->sk_family) {
3577 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003578#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3579 /*
3580 * If there is a secmark use it rather than the CIPSO label.
3581 * If there is no secmark fall back to CIPSO.
3582 * The secmark is assumed to reflect policy better.
3583 */
3584 if (skb && skb->secmark != 0) {
3585 skp = smack_from_secid(skb->secmark);
3586 goto access_check;
3587 }
3588#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003589 /*
3590 * Translate what netlabel gave us.
3591 */
3592 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003593
Casey Schauflerc6739442013-05-22 18:42:56 -07003594 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3595 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003596 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003597 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003598 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003599
Casey Schauflerc6739442013-05-22 18:42:56 -07003600 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003601
Casey Schaufler69f287a2014-12-12 17:08:40 -08003602#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3603access_check:
3604#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003605#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003606 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3607 ad.a.u.net->family = sk->sk_family;
3608 ad.a.u.net->netif = skb->skb_iif;
3609 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003610#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003611 /*
3612 * Receiving a packet requires that the other end
3613 * be able to write here. Read access is not required.
3614 * This is the simplist possible security model
3615 * for networking.
3616 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003617 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3618 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003619 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003620 if (rc != 0)
3621 netlbl_skbuff_err(skb, rc, 0);
3622 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003623#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003624 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003625 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3626 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3627 break;
3628#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3629 if (skb && skb->secmark != 0)
3630 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003631 else
Casey Schaufler69f287a2014-12-12 17:08:40 -08003632 skp = smack_net_ambient;
3633#ifdef CONFIG_AUDIT
3634 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3635 ad.a.u.net->family = sk->sk_family;
3636 ad.a.u.net->netif = skb->skb_iif;
3637 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3638#endif /* CONFIG_AUDIT */
3639 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3640 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3641 MAY_WRITE, rc);
3642#else /* CONFIG_SECURITY_SMACK_NETFILTER */
3643 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3644#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003645 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003646#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003647 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003648
Paul Moorea8134292008-10-10 10:16:31 -04003649 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003650}
3651
3652/**
3653 * smack_socket_getpeersec_stream - pull in packet label
3654 * @sock: the socket
3655 * @optval: user's destination
3656 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003657 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003658 *
3659 * returns zero on success, an error code otherwise
3660 */
3661static int smack_socket_getpeersec_stream(struct socket *sock,
3662 char __user *optval,
3663 int __user *optlen, unsigned len)
3664{
3665 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003666 char *rcp = "";
3667 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003668 int rc = 0;
3669
3670 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003671 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003672 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003673 slen = strlen(rcp) + 1;
3674 }
Casey Schauflere114e472008-02-04 22:29:50 -08003675
3676 if (slen > len)
3677 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003678 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003679 rc = -EFAULT;
3680
3681 if (put_user(slen, optlen) != 0)
3682 rc = -EFAULT;
3683
3684 return rc;
3685}
3686
3687
3688/**
3689 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003690 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003691 * @skb: packet data
3692 * @secid: pointer to where to put the secid of the packet
3693 *
3694 * Sets the netlabel socket state on sk from parent
3695 */
3696static int smack_socket_getpeersec_dgram(struct socket *sock,
3697 struct sk_buff *skb, u32 *secid)
3698
3699{
3700 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003701 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003702 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003703 int family = PF_UNSPEC;
3704 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003705 int rc;
3706
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003707 if (skb != NULL) {
3708 if (skb->protocol == htons(ETH_P_IP))
3709 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003710#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003711 else if (skb->protocol == htons(ETH_P_IPV6))
3712 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003713#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003714 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003715 if (family == PF_UNSPEC && sock != NULL)
3716 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003717
Casey Schaufler69f287a2014-12-12 17:08:40 -08003718 switch (family) {
3719 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003720 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003721 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003722 break;
3723 case PF_INET:
3724#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3725 s = skb->secmark;
3726 if (s != 0)
3727 break;
3728#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003729 /*
3730 * Translate what netlabel gave us.
3731 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003732 if (sock != NULL && sock->sk != NULL)
3733 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003734 netlbl_secattr_init(&secattr);
3735 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3736 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003737 skp = smack_from_secattr(&secattr, ssp);
3738 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003739 }
3740 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003741 break;
3742#if IS_ENABLED(CONFIG_IPV6)
3743 case PF_INET6:
3744#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3745 s = skb->secmark;
3746#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3747 break;
3748#endif /* CONFIG_IPV6 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003749 }
3750 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003751 if (s == 0)
3752 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003753 return 0;
3754}
3755
3756/**
Paul Moore07feee82009-03-27 17:10:54 -04003757 * smack_sock_graft - Initialize a newly created socket with an existing sock
3758 * @sk: child sock
3759 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003760 *
Paul Moore07feee82009-03-27 17:10:54 -04003761 * Set the smk_{in,out} state of an existing sock based on the process that
3762 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003763 */
3764static void smack_sock_graft(struct sock *sk, struct socket *parent)
3765{
3766 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003767 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003768
Paul Moore07feee82009-03-27 17:10:54 -04003769 if (sk == NULL ||
3770 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003771 return;
3772
3773 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003774 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003775 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003776 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003777}
3778
3779/**
3780 * smack_inet_conn_request - Smack access check on connect
3781 * @sk: socket involved
3782 * @skb: packet
3783 * @req: unused
3784 *
3785 * Returns 0 if a task with the packet label could write to
3786 * the socket, otherwise an error code
3787 */
3788static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3789 struct request_sock *req)
3790{
Paul Moore07feee82009-03-27 17:10:54 -04003791 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003792 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003793 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003794 struct netlbl_lsm_secattr secattr;
3795 struct sockaddr_in addr;
3796 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003797 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003798 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003799 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003800#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003801 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003802#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003803
Casey Schaufler69f287a2014-12-12 17:08:40 -08003804#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003805 if (family == PF_INET6) {
3806 /*
3807 * Handle mapped IPv4 packets arriving
3808 * via IPv6 sockets. Don't set up netlabel
3809 * processing on IPv6.
3810 */
3811 if (skb->protocol == htons(ETH_P_IP))
3812 family = PF_INET;
3813 else
3814 return 0;
3815 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003816#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003817
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003818#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3819 /*
3820 * If there is a secmark use it rather than the CIPSO label.
3821 * If there is no secmark fall back to CIPSO.
3822 * The secmark is assumed to reflect policy better.
3823 */
3824 if (skb && skb->secmark != 0) {
3825 skp = smack_from_secid(skb->secmark);
3826 goto access_check;
3827 }
3828#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3829
Paul Moore07feee82009-03-27 17:10:54 -04003830 netlbl_secattr_init(&secattr);
3831 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003832 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003833 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003834 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003835 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003836 netlbl_secattr_destroy(&secattr);
3837
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003838#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3839access_check:
3840#endif
3841
Etienne Bassetecfcc532009-04-08 20:40:06 +02003842#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003843 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3844 ad.a.u.net->family = family;
3845 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003846 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3847#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003848 /*
Paul Moore07feee82009-03-27 17:10:54 -04003849 * Receiving a packet requires that the other end be able to write
3850 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003851 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003852 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3853 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003854 if (rc != 0)
3855 return rc;
3856
3857 /*
3858 * Save the peer's label in the request_sock so we can later setup
3859 * smk_packet in the child socket so that SO_PEERCRED can report it.
3860 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003861 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003862
3863 /*
3864 * We need to decide if we want to label the incoming connection here
3865 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003866 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003867 */
3868 hdr = ip_hdr(skb);
3869 addr.sin_addr.s_addr = hdr->saddr;
3870 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003871 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003872 rcu_read_unlock();
3873
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003874 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003875 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003876 else
Paul Moore07feee82009-03-27 17:10:54 -04003877 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003878
3879 return rc;
3880}
3881
Paul Moore07feee82009-03-27 17:10:54 -04003882/**
3883 * smack_inet_csk_clone - Copy the connection information to the new socket
3884 * @sk: the new socket
3885 * @req: the connection's request_sock
3886 *
3887 * Transfer the connection's peer label to the newly created socket.
3888 */
3889static void smack_inet_csk_clone(struct sock *sk,
3890 const struct request_sock *req)
3891{
3892 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003893 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003894
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003895 if (req->peer_secid != 0) {
3896 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003897 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003898 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003899 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003900}
3901
Casey Schauflere114e472008-02-04 22:29:50 -08003902/*
3903 * Key management security hooks
3904 *
3905 * Casey has not tested key support very heavily.
3906 * The permission check is most likely too restrictive.
3907 * If you care about keys please have a look.
3908 */
3909#ifdef CONFIG_KEYS
3910
3911/**
3912 * smack_key_alloc - Set the key security blob
3913 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003914 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003915 * @flags: unused
3916 *
3917 * No allocation required
3918 *
3919 * Returns 0
3920 */
David Howellsd84f4f92008-11-14 10:39:23 +11003921static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003922 unsigned long flags)
3923{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003924 struct smack_known *skp = smk_of_task(cred->security);
3925
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003926 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003927 return 0;
3928}
3929
3930/**
3931 * smack_key_free - Clear the key security blob
3932 * @key: the object
3933 *
3934 * Clear the blob pointer
3935 */
3936static void smack_key_free(struct key *key)
3937{
3938 key->security = NULL;
3939}
3940
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003941/**
Casey Schauflere114e472008-02-04 22:29:50 -08003942 * smack_key_permission - Smack access on a key
3943 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003944 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003945 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08003946 *
3947 * Return 0 if the task has read and write to the object,
3948 * an error code otherwise
3949 */
3950static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00003951 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003952{
3953 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003954 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003955 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003956 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07003957 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003958
3959 keyp = key_ref_to_ptr(key_ref);
3960 if (keyp == NULL)
3961 return -EINVAL;
3962 /*
3963 * If the key hasn't been initialized give it access so that
3964 * it may do so.
3965 */
3966 if (keyp->security == NULL)
3967 return 0;
3968 /*
3969 * This should not occur
3970 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003971 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003972 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003973#ifdef CONFIG_AUDIT
3974 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3975 ad.a.u.key_struct.key = keyp->serial;
3976 ad.a.u.key_struct.key_desc = keyp->description;
3977#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003978 if (perm & KEY_NEED_READ)
3979 request = MAY_READ;
3980 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3981 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07003982 rc = smk_access(tkp, keyp->security, request, &ad);
3983 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
3984 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003985}
3986#endif /* CONFIG_KEYS */
3987
3988/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003989 * Smack Audit hooks
3990 *
3991 * Audit requires a unique representation of each Smack specific
3992 * rule. This unique representation is used to distinguish the
3993 * object to be audited from remaining kernel objects and also
3994 * works as a glue between the audit hooks.
3995 *
3996 * Since repository entries are added but never deleted, we'll use
3997 * the smack_known label address related to the given audit rule as
3998 * the needed unique representation. This also better fits the smack
3999 * model where nearly everything is a label.
4000 */
4001#ifdef CONFIG_AUDIT
4002
4003/**
4004 * smack_audit_rule_init - Initialize a smack audit rule
4005 * @field: audit rule fields given from user-space (audit.h)
4006 * @op: required testing operator (=, !=, >, <, ...)
4007 * @rulestr: smack label to be audited
4008 * @vrule: pointer to save our own audit rule representation
4009 *
4010 * Prepare to audit cases where (@field @op @rulestr) is true.
4011 * The label to be audited is created if necessay.
4012 */
4013static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4014{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004015 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004016 char **rule = (char **)vrule;
4017 *rule = NULL;
4018
4019 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4020 return -EINVAL;
4021
Al Viro5af75d82008-12-16 05:59:26 -05004022 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004023 return -EINVAL;
4024
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004025 skp = smk_import_entry(rulestr, 0);
4026 if (skp)
4027 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004028
4029 return 0;
4030}
4031
4032/**
4033 * smack_audit_rule_known - Distinguish Smack audit rules
4034 * @krule: rule of interest, in Audit kernel representation format
4035 *
4036 * This is used to filter Smack rules from remaining Audit ones.
4037 * If it's proved that this rule belongs to us, the
4038 * audit_rule_match hook will be called to do the final judgement.
4039 */
4040static int smack_audit_rule_known(struct audit_krule *krule)
4041{
4042 struct audit_field *f;
4043 int i;
4044
4045 for (i = 0; i < krule->field_count; i++) {
4046 f = &krule->fields[i];
4047
4048 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4049 return 1;
4050 }
4051
4052 return 0;
4053}
4054
4055/**
4056 * smack_audit_rule_match - Audit given object ?
4057 * @secid: security id for identifying the object to test
4058 * @field: audit rule flags given from user-space
4059 * @op: required testing operator
4060 * @vrule: smack internal rule presentation
4061 * @actx: audit context associated with the check
4062 *
4063 * The core Audit hook. It's used to take the decision of
4064 * whether to audit or not to audit a given object.
4065 */
4066static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4067 struct audit_context *actx)
4068{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004069 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004070 char *rule = vrule;
4071
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004072 if (unlikely(!rule)) {
4073 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004074 return -ENOENT;
4075 }
4076
4077 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4078 return 0;
4079
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004080 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004081
4082 /*
4083 * No need to do string comparisons. If a match occurs,
4084 * both pointers will point to the same smack_known
4085 * label.
4086 */
Al Viro5af75d82008-12-16 05:59:26 -05004087 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004088 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004089 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004090 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004091
4092 return 0;
4093}
4094
4095/**
4096 * smack_audit_rule_free - free smack rule representation
4097 * @vrule: rule to be freed.
4098 *
4099 * No memory was allocated.
4100 */
4101static void smack_audit_rule_free(void *vrule)
4102{
4103 /* No-op */
4104}
4105
4106#endif /* CONFIG_AUDIT */
4107
Randy Dunlap251a2a92009-02-18 11:42:33 -08004108/**
David Quigley746df9b2013-05-22 12:50:35 -04004109 * smack_ismaclabel - check if xattr @name references a smack MAC label
4110 * @name: Full xattr name to check.
4111 */
4112static int smack_ismaclabel(const char *name)
4113{
4114 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4115}
4116
4117
4118/**
Casey Schauflere114e472008-02-04 22:29:50 -08004119 * smack_secid_to_secctx - return the smack label for a secid
4120 * @secid: incoming integer
4121 * @secdata: destination
4122 * @seclen: how long it is
4123 *
4124 * Exists for networking code.
4125 */
4126static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4127{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004128 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004129
Eric Parisd5630b92010-10-13 16:24:48 -04004130 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004131 *secdata = skp->smk_known;
4132 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004133 return 0;
4134}
4135
Randy Dunlap251a2a92009-02-18 11:42:33 -08004136/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004137 * smack_secctx_to_secid - return the secid for a smack label
4138 * @secdata: smack label
4139 * @seclen: how long result is
4140 * @secid: outgoing integer
4141 *
4142 * Exists for audit and networking code.
4143 */
David Howellse52c17642008-04-29 20:52:51 +01004144static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004145{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004146 struct smack_known *skp = smk_find_entry(secdata);
4147
4148 if (skp)
4149 *secid = skp->smk_secid;
4150 else
4151 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004152 return 0;
4153}
4154
Randy Dunlap251a2a92009-02-18 11:42:33 -08004155/**
Casey Schauflere114e472008-02-04 22:29:50 -08004156 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004157 * @secdata: unused
4158 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004159 *
4160 * Exists to make sure nothing gets done, and properly
4161 */
4162static void smack_release_secctx(char *secdata, u32 seclen)
4163{
4164}
4165
David P. Quigley1ee65e32009-09-03 14:25:57 -04004166static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4167{
4168 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4169}
4170
4171static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4172{
4173 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4174}
4175
4176static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4177{
4178 int len = 0;
4179 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4180
4181 if (len < 0)
4182 return len;
4183 *ctxlen = len;
4184 return 0;
4185}
4186
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02004187struct security_operations smack_ops = {
4188 .name = "smack",
4189
Ingo Molnar9e488582009-05-07 19:26:19 +10004190 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01004191 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08004192 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08004193
4194 .sb_alloc_security = smack_sb_alloc_security,
4195 .sb_free_security = smack_sb_free_security,
4196 .sb_copy_data = smack_sb_copy_data,
4197 .sb_kern_mount = smack_sb_kern_mount,
4198 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08004199
Casey Schaufler676dac42010-12-02 06:43:39 -08004200 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03004201 .bprm_committing_creds = smack_bprm_committing_creds,
4202 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08004203
Casey Schauflere114e472008-02-04 22:29:50 -08004204 .inode_alloc_security = smack_inode_alloc_security,
4205 .inode_free_security = smack_inode_free_security,
4206 .inode_init_security = smack_inode_init_security,
4207 .inode_link = smack_inode_link,
4208 .inode_unlink = smack_inode_unlink,
4209 .inode_rmdir = smack_inode_rmdir,
4210 .inode_rename = smack_inode_rename,
4211 .inode_permission = smack_inode_permission,
4212 .inode_setattr = smack_inode_setattr,
4213 .inode_getattr = smack_inode_getattr,
4214 .inode_setxattr = smack_inode_setxattr,
4215 .inode_post_setxattr = smack_inode_post_setxattr,
4216 .inode_getxattr = smack_inode_getxattr,
4217 .inode_removexattr = smack_inode_removexattr,
4218 .inode_getsecurity = smack_inode_getsecurity,
4219 .inode_setsecurity = smack_inode_setsecurity,
4220 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004221 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004222
4223 .file_permission = smack_file_permission,
4224 .file_alloc_security = smack_file_alloc_security,
4225 .file_free_security = smack_file_free_security,
4226 .file_ioctl = smack_file_ioctl,
4227 .file_lock = smack_file_lock,
4228 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04004229 .mmap_file = smack_mmap_file,
4230 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08004231 .file_set_fowner = smack_file_set_fowner,
4232 .file_send_sigiotask = smack_file_send_sigiotask,
4233 .file_receive = smack_file_receive,
4234
Eric Paris83d49852012-04-04 13:45:40 -04004235 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07004236
David Howellsee18d642009-09-02 09:14:21 +01004237 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11004238 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11004239 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01004240 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11004241 .kernel_act_as = smack_kernel_act_as,
4242 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08004243 .task_setpgid = smack_task_setpgid,
4244 .task_getpgid = smack_task_getpgid,
4245 .task_getsid = smack_task_getsid,
4246 .task_getsecid = smack_task_getsecid,
4247 .task_setnice = smack_task_setnice,
4248 .task_setioprio = smack_task_setioprio,
4249 .task_getioprio = smack_task_getioprio,
4250 .task_setscheduler = smack_task_setscheduler,
4251 .task_getscheduler = smack_task_getscheduler,
4252 .task_movememory = smack_task_movememory,
4253 .task_kill = smack_task_kill,
4254 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08004255 .task_to_inode = smack_task_to_inode,
4256
4257 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004258 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004259
4260 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4261 .msg_msg_free_security = smack_msg_msg_free_security,
4262
4263 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4264 .msg_queue_free_security = smack_msg_queue_free_security,
4265 .msg_queue_associate = smack_msg_queue_associate,
4266 .msg_queue_msgctl = smack_msg_queue_msgctl,
4267 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4268 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4269
4270 .shm_alloc_security = smack_shm_alloc_security,
4271 .shm_free_security = smack_shm_free_security,
4272 .shm_associate = smack_shm_associate,
4273 .shm_shmctl = smack_shm_shmctl,
4274 .shm_shmat = smack_shm_shmat,
4275
4276 .sem_alloc_security = smack_sem_alloc_security,
4277 .sem_free_security = smack_sem_free_security,
4278 .sem_associate = smack_sem_associate,
4279 .sem_semctl = smack_sem_semctl,
4280 .sem_semop = smack_sem_semop,
4281
Casey Schauflere114e472008-02-04 22:29:50 -08004282 .d_instantiate = smack_d_instantiate,
4283
4284 .getprocattr = smack_getprocattr,
4285 .setprocattr = smack_setprocattr,
4286
4287 .unix_stream_connect = smack_unix_stream_connect,
4288 .unix_may_send = smack_unix_may_send,
4289
4290 .socket_post_create = smack_socket_post_create,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004291#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schauflerc6739442013-05-22 18:42:56 -07004292 .socket_bind = smack_socket_bind,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004293#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004294 .socket_connect = smack_socket_connect,
4295 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08004296 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4297 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4298 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4299 .sk_alloc_security = smack_sk_alloc_security,
4300 .sk_free_security = smack_sk_free_security,
4301 .sock_graft = smack_sock_graft,
4302 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04004303 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004304
Casey Schauflere114e472008-02-04 22:29:50 -08004305 /* key management security hooks */
4306#ifdef CONFIG_KEYS
4307 .key_alloc = smack_key_alloc,
4308 .key_free = smack_key_free,
4309 .key_permission = smack_key_permission,
4310#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004311
4312 /* Audit hooks */
4313#ifdef CONFIG_AUDIT
4314 .audit_rule_init = smack_audit_rule_init,
4315 .audit_rule_known = smack_audit_rule_known,
4316 .audit_rule_match = smack_audit_rule_match,
4317 .audit_rule_free = smack_audit_rule_free,
4318#endif /* CONFIG_AUDIT */
4319
David Quigley746df9b2013-05-22 12:50:35 -04004320 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08004321 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004322 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08004323 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04004324 .inode_notifysecctx = smack_inode_notifysecctx,
4325 .inode_setsecctx = smack_inode_setsecctx,
4326 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08004327};
4328
Etienne Basset7198e2e2009-03-24 20:53:24 +01004329
Casey Schaufler86812bb2012-04-17 18:55:46 -07004330static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004331{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004332 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004333 * Initialize rule list locks
4334 */
4335 mutex_init(&smack_known_huh.smk_rules_lock);
4336 mutex_init(&smack_known_hat.smk_rules_lock);
4337 mutex_init(&smack_known_floor.smk_rules_lock);
4338 mutex_init(&smack_known_star.smk_rules_lock);
4339 mutex_init(&smack_known_invalid.smk_rules_lock);
4340 mutex_init(&smack_known_web.smk_rules_lock);
4341 /*
4342 * Initialize rule lists
4343 */
4344 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4345 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4346 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4347 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4348 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4349 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4350 /*
4351 * Create the known labels list
4352 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004353 smk_insert_entry(&smack_known_huh);
4354 smk_insert_entry(&smack_known_hat);
4355 smk_insert_entry(&smack_known_star);
4356 smk_insert_entry(&smack_known_floor);
4357 smk_insert_entry(&smack_known_invalid);
4358 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004359}
4360
Casey Schauflere114e472008-02-04 22:29:50 -08004361/**
4362 * smack_init - initialize the smack system
4363 *
4364 * Returns 0
4365 */
4366static __init int smack_init(void)
4367{
David Howellsd84f4f92008-11-14 10:39:23 +11004368 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004369 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004370
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004371 if (!security_module_enable(&smack_ops))
4372 return 0;
4373
Casey Schaufler69f287a2014-12-12 17:08:40 -08004374 smack_enabled = 1;
4375
Rohit1a5b4722014-10-15 17:40:41 +05304376 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4377 if (!smack_inode_cache)
4378 return -ENOMEM;
4379
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004380 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4381 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304382 if (tsp == NULL) {
4383 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004384 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304385 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004386
Casey Schauflere114e472008-02-04 22:29:50 -08004387 printk(KERN_INFO "Smack: Initializing.\n");
4388
4389 /*
4390 * Set the security state for the initial task.
4391 */
David Howellsd84f4f92008-11-14 10:39:23 +11004392 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004393 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004394
Casey Schaufler86812bb2012-04-17 18:55:46 -07004395 /* initialize the smack_known_list */
4396 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004397
4398 /*
4399 * Register with LSM
4400 */
4401 if (register_security(&smack_ops))
4402 panic("smack: Unable to register with kernel.\n");
4403
4404 return 0;
4405}
4406
4407/*
4408 * Smack requires early initialization in order to label
4409 * all processes and objects when they are created.
4410 */
4411security_initcall(smack_init);