blob: a097dc7d4669236d12fb72aad43a46e2fbed6aba [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 */
1037static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1038{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001039 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -04001040 struct path path;
Casey Schauflerd166c802014-08-27 14:51:27 -07001041 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042
Eric Parisa2694342011-04-25 13:10:27 -04001043 path.dentry = dentry;
1044 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001045
Eric Parisf48b7392011-04-25 12:54:27 -04001046 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -04001047 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001048 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1049 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1050 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001051}
1052
1053/**
1054 * smack_inode_setxattr - Smack check for setting xattrs
1055 * @dentry: the object
1056 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001057 * @value: value of the attribute
1058 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001059 * @flags: unused
1060 *
1061 * This protects the Smack attribute explicitly.
1062 *
1063 * Returns 0 if access is permitted, an error code otherwise
1064 */
David Howells8f0cfa52008-04-29 00:59:41 -07001065static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1066 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001067{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001068 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001069 struct smack_known *skp;
1070 int check_priv = 0;
1071 int check_import = 0;
1072 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001073 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001074
Casey Schaufler19760ad2013-12-16 16:27:26 -08001075 /*
1076 * Check label validity here so import won't fail in post_setxattr
1077 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001078 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1079 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001080 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1081 check_priv = 1;
1082 check_import = 1;
1083 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1084 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1085 check_priv = 1;
1086 check_import = 1;
1087 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001088 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001089 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001090 if (size != TRANS_TRUE_SIZE ||
1091 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1092 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001093 } else
1094 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1095
Casey Schaufler19760ad2013-12-16 16:27:26 -08001096 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1097 rc = -EPERM;
1098
1099 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001100 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001101 if (skp == NULL || (check_star &&
1102 (skp == &smack_known_star || skp == &smack_known_web)))
1103 rc = -EINVAL;
1104 }
1105
Eric Parisa2694342011-04-25 13:10:27 -04001106 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001107 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1108
Casey Schauflerd166c802014-08-27 14:51:27 -07001109 if (rc == 0) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001110 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001111 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1112 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001113
1114 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001115}
1116
1117/**
1118 * smack_inode_post_setxattr - Apply the Smack update approved above
1119 * @dentry: object
1120 * @name: attribute name
1121 * @value: attribute value
1122 * @size: attribute size
1123 * @flags: unused
1124 *
1125 * Set the pointer in the inode blob to the entry found
1126 * in the master label list.
1127 */
David Howells8f0cfa52008-04-29 00:59:41 -07001128static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1129 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001130{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001131 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001132 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001133
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001134 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1135 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1136 return;
1137 }
1138
Casey Schaufler676dac42010-12-02 06:43:39 -08001139 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001140 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001141 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001142 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001143 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001144 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001145 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001146 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001147 if (skp != NULL)
1148 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001149 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001150 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001151 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001152 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001153 if (skp != NULL)
1154 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001155 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001156 isp->smk_mmap = &smack_known_invalid;
1157 }
Casey Schauflere114e472008-02-04 22:29:50 -08001158
1159 return;
1160}
1161
Casey Schauflerce8a4322011-09-29 18:21:01 -07001162/**
Casey Schauflere114e472008-02-04 22:29:50 -08001163 * smack_inode_getxattr - Smack check on getxattr
1164 * @dentry: the object
1165 * @name: unused
1166 *
1167 * Returns 0 if access is permitted, an error code otherwise
1168 */
David Howells8f0cfa52008-04-29 00:59:41 -07001169static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001170{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001172 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001173
Eric Parisa2694342011-04-25 13:10:27 -04001174 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001175 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1176
Casey Schauflerd166c802014-08-27 14:51:27 -07001177 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1178 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1179 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001180}
1181
Casey Schauflerce8a4322011-09-29 18:21:01 -07001182/**
Casey Schauflere114e472008-02-04 22:29:50 -08001183 * smack_inode_removexattr - Smack check on removexattr
1184 * @dentry: the object
1185 * @name: name of the attribute
1186 *
1187 * Removing the Smack attribute requires CAP_MAC_ADMIN
1188 *
1189 * Returns 0 if access is permitted, an error code otherwise
1190 */
David Howells8f0cfa52008-04-29 00:59:41 -07001191static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001192{
Casey Schaufler676dac42010-12-02 06:43:39 -08001193 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001194 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001195 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001196
Casey Schauflerbcdca222008-02-23 15:24:04 -08001197 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1198 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001199 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001200 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001201 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301202 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001203 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001204 rc = -EPERM;
1205 } else
1206 rc = cap_inode_removexattr(dentry, name);
1207
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001208 if (rc != 0)
1209 return rc;
1210
Eric Parisa2694342011-04-25 13:10:27 -04001211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001212 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001213
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001214 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001215 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001216 if (rc != 0)
1217 return rc;
1218
1219 isp = dentry->d_inode->i_security;
1220 /*
1221 * Don't do anything special for these.
1222 * XATTR_NAME_SMACKIPIN
1223 * XATTR_NAME_SMACKIPOUT
1224 * XATTR_NAME_SMACKEXEC
1225 */
1226 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001227 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001228 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001229 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001230 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1231 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001232
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001233 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001234}
1235
1236/**
1237 * smack_inode_getsecurity - get smack xattrs
1238 * @inode: the object
1239 * @name: attribute name
1240 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001241 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001242 *
1243 * Returns the size of the attribute or an error code
1244 */
1245static int smack_inode_getsecurity(const struct inode *inode,
1246 const char *name, void **buffer,
1247 bool alloc)
1248{
1249 struct socket_smack *ssp;
1250 struct socket *sock;
1251 struct super_block *sbp;
1252 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001253 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001254 int ilen;
1255 int rc = 0;
1256
1257 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1258 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001259 ilen = strlen(isp->smk_known);
1260 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001261 return ilen;
1262 }
1263
1264 /*
1265 * The rest of the Smack xattrs are only on sockets.
1266 */
1267 sbp = ip->i_sb;
1268 if (sbp->s_magic != SOCKFS_MAGIC)
1269 return -EOPNOTSUPP;
1270
1271 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001272 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001273 return -EOPNOTSUPP;
1274
1275 ssp = sock->sk->sk_security;
1276
1277 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001278 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001279 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001280 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001281 else
1282 return -EOPNOTSUPP;
1283
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001284 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001285 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001286 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001287 rc = ilen;
1288 }
1289
1290 return rc;
1291}
1292
1293
1294/**
1295 * smack_inode_listsecurity - list the Smack attributes
1296 * @inode: the object
1297 * @buffer: where they go
1298 * @buffer_size: size of buffer
1299 *
1300 * Returns 0 on success, -EINVAL otherwise
1301 */
1302static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1303 size_t buffer_size)
1304{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001305 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001306
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001307 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001308 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001309
1310 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001311}
1312
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001313/**
1314 * smack_inode_getsecid - Extract inode's security id
1315 * @inode: inode to extract the info from
1316 * @secid: where result will be saved
1317 */
1318static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1319{
1320 struct inode_smack *isp = inode->i_security;
1321
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001322 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001323}
1324
Casey Schauflere114e472008-02-04 22:29:50 -08001325/*
1326 * File Hooks
1327 */
1328
1329/**
1330 * smack_file_permission - Smack check on file operations
1331 * @file: unused
1332 * @mask: unused
1333 *
1334 * Returns 0
1335 *
1336 * Should access checks be done on each read or write?
1337 * UNICOS and SELinux say yes.
1338 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1339 *
1340 * I'll say no for now. Smack does not do the frequent
1341 * label changing that SELinux does.
1342 */
1343static int smack_file_permission(struct file *file, int mask)
1344{
1345 return 0;
1346}
1347
1348/**
1349 * smack_file_alloc_security - assign a file security blob
1350 * @file: the object
1351 *
1352 * The security blob for a file is a pointer to the master
1353 * label list, so no allocation is done.
1354 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001355 * f_security is the owner security information. It
1356 * isn't used on file access checks, it's for send_sigio.
1357 *
Casey Schauflere114e472008-02-04 22:29:50 -08001358 * Returns 0
1359 */
1360static int smack_file_alloc_security(struct file *file)
1361{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001362 struct smack_known *skp = smk_of_current();
1363
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001364 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001365 return 0;
1366}
1367
1368/**
1369 * smack_file_free_security - clear a file security blob
1370 * @file: the object
1371 *
1372 * The security blob for a file is a pointer to the master
1373 * label list, so no memory is freed.
1374 */
1375static void smack_file_free_security(struct file *file)
1376{
1377 file->f_security = NULL;
1378}
1379
1380/**
1381 * smack_file_ioctl - Smack check on ioctls
1382 * @file: the object
1383 * @cmd: what to do
1384 * @arg: unused
1385 *
1386 * Relies heavily on the correct use of the ioctl command conventions.
1387 *
1388 * Returns 0 if allowed, error code otherwise
1389 */
1390static int smack_file_ioctl(struct file *file, unsigned int cmd,
1391 unsigned long arg)
1392{
1393 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001394 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001395 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001396
Eric Parisf48b7392011-04-25 12:54:27 -04001397 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001398 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001399
Casey Schauflerd166c802014-08-27 14:51:27 -07001400 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001401 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001402 rc = smk_bu_file(file, MAY_WRITE, rc);
1403 }
Casey Schauflere114e472008-02-04 22:29:50 -08001404
Casey Schauflerd166c802014-08-27 14:51:27 -07001405 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001406 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001407 rc = smk_bu_file(file, MAY_READ, rc);
1408 }
Casey Schauflere114e472008-02-04 22:29:50 -08001409
1410 return rc;
1411}
1412
1413/**
1414 * smack_file_lock - Smack check on file locking
1415 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001416 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001417 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001418 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001419 */
1420static int smack_file_lock(struct file *file, unsigned int cmd)
1421{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001422 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001423 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001424 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001425
Eric Paris92f42502011-04-25 13:15:55 -04001426 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1427 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001428 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001429 rc = smk_bu_file(file, MAY_LOCK, rc);
1430 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001431}
1432
1433/**
1434 * smack_file_fcntl - Smack check on fcntl
1435 * @file: the object
1436 * @cmd: what action to check
1437 * @arg: unused
1438 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001439 * Generally these operations are harmless.
1440 * File locking operations present an obvious mechanism
1441 * for passing information, so they require write access.
1442 *
Casey Schauflere114e472008-02-04 22:29:50 -08001443 * Returns 0 if current has access, error code otherwise
1444 */
1445static int smack_file_fcntl(struct file *file, unsigned int cmd,
1446 unsigned long arg)
1447{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001448 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001449 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001450 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001451
Casey Schauflere114e472008-02-04 22:29:50 -08001452 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001453 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001454 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001455 case F_SETLK:
1456 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001457 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1458 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001459 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001460 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001461 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001462 case F_SETOWN:
1463 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001464 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1465 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001466 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001467 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001468 break;
1469 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001470 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001471 }
1472
1473 return rc;
1474}
1475
1476/**
Al Viroe5467852012-05-30 13:30:51 -04001477 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001478 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1479 * if mapping anonymous memory.
1480 * @file contains the file structure for file to map (may be NULL).
1481 * @reqprot contains the protection requested by the application.
1482 * @prot contains the protection that will be applied by the kernel.
1483 * @flags contains the operational flags.
1484 * Return 0 if permission is granted.
1485 */
Al Viroe5467852012-05-30 13:30:51 -04001486static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001487 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001488 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001489{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001490 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001491 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001492 struct smack_rule *srp;
1493 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001494 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001495 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001496 int may;
1497 int mmay;
1498 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001499 int rc;
1500
Al Viro496ad9a2013-01-23 17:07:38 -05001501 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001502 return 0;
1503
Al Viro496ad9a2013-01-23 17:07:38 -05001504 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001505 if (isp->smk_mmap == NULL)
1506 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001507 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001508
1509 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001510 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001511 rc = 0;
1512
1513 rcu_read_lock();
1514 /*
1515 * For each Smack rule associated with the subject
1516 * label verify that the SMACK64MMAP also has access
1517 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001518 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001519 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001520 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001521 /*
1522 * Matching labels always allows access.
1523 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001524 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001525 continue;
1526 /*
1527 * If there is a matching local rule take
1528 * that into account as well.
1529 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001530 may = smk_access_entry(srp->smk_subject->smk_known,
1531 okp->smk_known,
1532 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001533 if (may == -ENOENT)
1534 may = srp->smk_access;
1535 else
1536 may &= srp->smk_access;
1537 /*
1538 * If may is zero the SMACK64MMAP subject can't
1539 * possibly have less access.
1540 */
1541 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001542 continue;
1543
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001544 /*
1545 * Fetch the global list entry.
1546 * If there isn't one a SMACK64MMAP subject
1547 * can't have as much access as current.
1548 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001549 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1550 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001551 if (mmay == -ENOENT) {
1552 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001553 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001554 }
1555 /*
1556 * If there is a local entry it modifies the
1557 * potential access, too.
1558 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001559 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1560 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001561 if (tmay != -ENOENT)
1562 mmay &= tmay;
1563
1564 /*
1565 * If there is any access available to current that is
1566 * not available to a SMACK64MMAP subject
1567 * deny access.
1568 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001569 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001570 rc = -EACCES;
1571 break;
1572 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001573 }
1574
1575 rcu_read_unlock();
1576
1577 return rc;
1578}
1579
1580/**
Casey Schauflere114e472008-02-04 22:29:50 -08001581 * smack_file_set_fowner - set the file security blob value
1582 * @file: object in question
1583 *
Casey Schauflere114e472008-02-04 22:29:50 -08001584 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001585static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001586{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001587 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001588}
1589
1590/**
1591 * smack_file_send_sigiotask - Smack on sigio
1592 * @tsk: The target task
1593 * @fown: the object the signal come from
1594 * @signum: unused
1595 *
1596 * Allow a privileged task to get signals even if it shouldn't
1597 *
1598 * Returns 0 if a subject with the object's smack could
1599 * write to the task, an error code otherwise.
1600 */
1601static int smack_file_send_sigiotask(struct task_struct *tsk,
1602 struct fown_struct *fown, int signum)
1603{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001604 struct smack_known *skp;
1605 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001606 struct file *file;
1607 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001608 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001609
1610 /*
1611 * struct fown_struct is never outside the context of a struct file
1612 */
1613 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001614
Etienne Bassetecfcc532009-04-08 20:40:06 +02001615 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001616 skp = file->f_security;
1617 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1618 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001619 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001620 rc = 0;
1621
1622 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1623 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001624 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001625 return rc;
1626}
1627
1628/**
1629 * smack_file_receive - Smack file receive check
1630 * @file: the object
1631 *
1632 * Returns 0 if current has access, error code otherwise
1633 */
1634static int smack_file_receive(struct file *file)
1635{
Casey Schauflerd166c802014-08-27 14:51:27 -07001636 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001637 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001638 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001639 struct inode *inode = file_inode(file);
Casey Schauflere114e472008-02-04 22:29:50 -08001640
Casey Schaufler4482a442013-12-30 17:37:45 -08001641 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001642 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001643 /*
1644 * This code relies on bitmasks.
1645 */
1646 if (file->f_mode & FMODE_READ)
1647 may = MAY_READ;
1648 if (file->f_mode & FMODE_WRITE)
1649 may |= MAY_WRITE;
1650
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001651 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001652 rc = smk_bu_file(file, may, rc);
1653 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001654}
1655
Casey Schaufler531f1d42011-09-19 12:41:42 -07001656/**
Eric Paris83d49852012-04-04 13:45:40 -04001657 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001658 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001659 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001660 *
1661 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001662 * Allow the open only if the task has read access. There are
1663 * many read operations (e.g. fstat) that you can do with an
1664 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001665 *
1666 * Returns 0
1667 */
Eric Paris83d49852012-04-04 13:45:40 -04001668static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001669{
Casey Schauflera6834c02014-04-21 11:10:26 -07001670 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001671 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001672 struct smk_audit_info ad;
1673 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001674
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001675 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001676 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001677
Casey Schauflera6834c02014-04-21 11:10:26 -07001678 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1679 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001680 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001681 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001682
1683 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001684}
1685
Casey Schauflere114e472008-02-04 22:29:50 -08001686/*
1687 * Task hooks
1688 */
1689
1690/**
David Howellsee18d642009-09-02 09:14:21 +01001691 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1692 * @new: the new credentials
1693 * @gfp: the atomicity of any memory allocations
1694 *
1695 * Prepare a blank set of credentials for modification. This must allocate all
1696 * the memory the LSM module might require such that cred_transfer() can
1697 * complete without error.
1698 */
1699static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1700{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001701 struct task_smack *tsp;
1702
1703 tsp = new_task_smack(NULL, NULL, gfp);
1704 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001705 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001706
1707 cred->security = tsp;
1708
David Howellsee18d642009-09-02 09:14:21 +01001709 return 0;
1710}
1711
1712
1713/**
David Howellsf1752ee2008-11-14 10:39:17 +11001714 * smack_cred_free - "free" task-level security credentials
1715 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001716 *
Casey Schauflere114e472008-02-04 22:29:50 -08001717 */
David Howellsf1752ee2008-11-14 10:39:17 +11001718static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001719{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001720 struct task_smack *tsp = cred->security;
1721 struct smack_rule *rp;
1722 struct list_head *l;
1723 struct list_head *n;
1724
1725 if (tsp == NULL)
1726 return;
1727 cred->security = NULL;
1728
1729 list_for_each_safe(l, n, &tsp->smk_rules) {
1730 rp = list_entry(l, struct smack_rule, list);
1731 list_del(&rp->list);
1732 kfree(rp);
1733 }
1734 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001735}
1736
1737/**
David Howellsd84f4f92008-11-14 10:39:23 +11001738 * smack_cred_prepare - prepare new set of credentials for modification
1739 * @new: the new credentials
1740 * @old: the original credentials
1741 * @gfp: the atomicity of any memory allocations
1742 *
1743 * Prepare a new set of credentials for modification.
1744 */
1745static int smack_cred_prepare(struct cred *new, const struct cred *old,
1746 gfp_t gfp)
1747{
Casey Schaufler676dac42010-12-02 06:43:39 -08001748 struct task_smack *old_tsp = old->security;
1749 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001750 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001751
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001752 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001753 if (new_tsp == NULL)
1754 return -ENOMEM;
1755
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001756 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1757 if (rc != 0)
1758 return rc;
1759
Casey Schaufler676dac42010-12-02 06:43:39 -08001760 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001761 return 0;
1762}
1763
Randy Dunlap251a2a92009-02-18 11:42:33 -08001764/**
David Howellsee18d642009-09-02 09:14:21 +01001765 * smack_cred_transfer - Transfer the old credentials to the new credentials
1766 * @new: the new credentials
1767 * @old: the original credentials
1768 *
1769 * Fill in a set of blank credentials from another set of credentials.
1770 */
1771static void smack_cred_transfer(struct cred *new, const struct cred *old)
1772{
Casey Schaufler676dac42010-12-02 06:43:39 -08001773 struct task_smack *old_tsp = old->security;
1774 struct task_smack *new_tsp = new->security;
1775
1776 new_tsp->smk_task = old_tsp->smk_task;
1777 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001778 mutex_init(&new_tsp->smk_rules_lock);
1779 INIT_LIST_HEAD(&new_tsp->smk_rules);
1780
1781
1782 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001783}
1784
1785/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001786 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001787 * @new: points to the set of credentials to be modified.
1788 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001789 *
1790 * Set the security data for a kernel service.
1791 */
1792static int smack_kernel_act_as(struct cred *new, u32 secid)
1793{
Casey Schaufler676dac42010-12-02 06:43:39 -08001794 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001795 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001796
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001797 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001798 return -EINVAL;
1799
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001800 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001801 return 0;
1802}
1803
1804/**
1805 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001806 * @new: points to the set of credentials to be modified
1807 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001808 *
1809 * Set the file creation context in a set of credentials to the same
1810 * as the objective context of the specified inode
1811 */
1812static int smack_kernel_create_files_as(struct cred *new,
1813 struct inode *inode)
1814{
1815 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001816 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001817
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001818 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001819 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001820 return 0;
1821}
1822
1823/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001824 * smk_curacc_on_task - helper to log task related access
1825 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001826 * @access: the access requested
1827 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001828 *
1829 * Return 0 if access is permitted
1830 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001831static int smk_curacc_on_task(struct task_struct *p, int access,
1832 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001833{
1834 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001835 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001836 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001837
Casey Schaufler531f1d42011-09-19 12:41:42 -07001838 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001839 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001840 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001841 rc = smk_bu_task(p, access, rc);
1842 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001843}
1844
1845/**
Casey Schauflere114e472008-02-04 22:29:50 -08001846 * smack_task_setpgid - Smack check on setting pgid
1847 * @p: the task object
1848 * @pgid: unused
1849 *
1850 * Return 0 if write access is permitted
1851 */
1852static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1853{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001854 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001855}
1856
1857/**
1858 * smack_task_getpgid - Smack access check for getpgid
1859 * @p: the object task
1860 *
1861 * Returns 0 if current can read the object task, error code otherwise
1862 */
1863static int smack_task_getpgid(struct task_struct *p)
1864{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001865 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001866}
1867
1868/**
1869 * smack_task_getsid - Smack access check for getsid
1870 * @p: the object task
1871 *
1872 * Returns 0 if current can read the object task, error code otherwise
1873 */
1874static int smack_task_getsid(struct task_struct *p)
1875{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001876 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001877}
1878
1879/**
1880 * smack_task_getsecid - get the secid of the task
1881 * @p: the object task
1882 * @secid: where to put the result
1883 *
1884 * Sets the secid to contain a u32 version of the smack label.
1885 */
1886static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1887{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001888 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001889
1890 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001891}
1892
1893/**
1894 * smack_task_setnice - Smack check on setting nice
1895 * @p: the task object
1896 * @nice: unused
1897 *
1898 * Return 0 if write access is permitted
1899 */
1900static int smack_task_setnice(struct task_struct *p, int nice)
1901{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001902 int rc;
1903
1904 rc = cap_task_setnice(p, nice);
1905 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001906 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001907 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001908}
1909
1910/**
1911 * smack_task_setioprio - Smack check on setting ioprio
1912 * @p: the task object
1913 * @ioprio: unused
1914 *
1915 * Return 0 if write access is permitted
1916 */
1917static int smack_task_setioprio(struct task_struct *p, int ioprio)
1918{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001919 int rc;
1920
1921 rc = cap_task_setioprio(p, ioprio);
1922 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001923 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001924 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001925}
1926
1927/**
1928 * smack_task_getioprio - Smack check on reading ioprio
1929 * @p: the task object
1930 *
1931 * Return 0 if read access is permitted
1932 */
1933static int smack_task_getioprio(struct task_struct *p)
1934{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001935 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001936}
1937
1938/**
1939 * smack_task_setscheduler - Smack check on setting scheduler
1940 * @p: the task object
1941 * @policy: unused
1942 * @lp: unused
1943 *
1944 * Return 0 if read access is permitted
1945 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001946static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001947{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001948 int rc;
1949
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001950 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001951 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001952 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001953 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001954}
1955
1956/**
1957 * smack_task_getscheduler - Smack check on reading scheduler
1958 * @p: the task object
1959 *
1960 * Return 0 if read access is permitted
1961 */
1962static int smack_task_getscheduler(struct task_struct *p)
1963{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001964 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001965}
1966
1967/**
1968 * smack_task_movememory - Smack check on moving memory
1969 * @p: the task object
1970 *
1971 * Return 0 if write access is permitted
1972 */
1973static int smack_task_movememory(struct task_struct *p)
1974{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001975 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001976}
1977
1978/**
1979 * smack_task_kill - Smack check on signal delivery
1980 * @p: the task object
1981 * @info: unused
1982 * @sig: unused
1983 * @secid: identifies the smack to use in lieu of current's
1984 *
1985 * Return 0 if write access is permitted
1986 *
1987 * The secid behavior is an artifact of an SELinux hack
1988 * in the USB code. Someday it may go away.
1989 */
1990static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1991 int sig, u32 secid)
1992{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001993 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001994 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001995 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001996 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001997
1998 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1999 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002000 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002001 * Sending a signal requires that the sender
2002 * can write the receiver.
2003 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002004 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002005 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002006 rc = smk_bu_task(p, MAY_WRITE, rc);
2007 return rc;
2008 }
Casey Schauflere114e472008-02-04 22:29:50 -08002009 /*
2010 * If the secid isn't 0 we're dealing with some USB IO
2011 * specific behavior. This is not clean. For one thing
2012 * we can't take privilege into account.
2013 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002014 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002015 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2016 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002017 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002018}
2019
2020/**
2021 * smack_task_wait - Smack access check for waiting
2022 * @p: task to wait for
2023 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002024 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002025 */
2026static int smack_task_wait(struct task_struct *p)
2027{
Casey Schauflere114e472008-02-04 22:29:50 -08002028 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002029 * Allow the operation to succeed.
2030 * Zombies are bad.
2031 * In userless environments (e.g. phones) programs
2032 * get marked with SMACK64EXEC and even if the parent
2033 * and child shouldn't be talking the parent still
2034 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002035 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002036 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002037}
2038
2039/**
2040 * smack_task_to_inode - copy task smack into the inode blob
2041 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002042 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002043 *
2044 * Sets the smack pointer in the inode security blob
2045 */
2046static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2047{
2048 struct inode_smack *isp = inode->i_security;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002049 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002050
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002051 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002052}
2053
2054/*
2055 * Socket hooks.
2056 */
2057
2058/**
2059 * smack_sk_alloc_security - Allocate a socket blob
2060 * @sk: the socket
2061 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002062 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002063 *
2064 * Assign Smack pointers to current
2065 *
2066 * Returns 0 on success, -ENOMEM is there's no memory
2067 */
2068static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2069{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002070 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002071 struct socket_smack *ssp;
2072
2073 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2074 if (ssp == NULL)
2075 return -ENOMEM;
2076
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002077 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002078 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002079 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002080
2081 sk->sk_security = ssp;
2082
2083 return 0;
2084}
2085
2086/**
2087 * smack_sk_free_security - Free a socket blob
2088 * @sk: the socket
2089 *
2090 * Clears the blob pointer
2091 */
2092static void smack_sk_free_security(struct sock *sk)
2093{
2094 kfree(sk->sk_security);
2095}
2096
2097/**
Paul Moore07feee82009-03-27 17:10:54 -04002098* smack_host_label - check host based restrictions
2099* @sip: the object end
2100*
2101* looks for host based access restrictions
2102*
2103* This version will only be appropriate for really small sets of single label
2104* hosts. The caller is responsible for ensuring that the RCU read lock is
2105* taken before calling this function.
2106*
2107* Returns the label of the far end or NULL if it's not special.
2108*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002109static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002110{
2111 struct smk_netlbladdr *snp;
2112 struct in_addr *siap = &sip->sin_addr;
2113
2114 if (siap->s_addr == 0)
2115 return NULL;
2116
2117 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2118 /*
2119 * we break after finding the first match because
2120 * the list is sorted from longest to shortest mask
2121 * so we have found the most specific match
2122 */
2123 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002124 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2125 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002126 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002127 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002128 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002129 }
Paul Moore07feee82009-03-27 17:10:54 -04002130
2131 return NULL;
2132}
2133
2134/**
Casey Schauflere114e472008-02-04 22:29:50 -08002135 * smack_netlabel - Set the secattr on a socket
2136 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002137 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002138 *
2139 * Convert the outbound smack value (smk_out) to a
2140 * secattr and attach it to the socket.
2141 *
2142 * Returns 0 on success or an error code
2143 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002144static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002145{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002146 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002147 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002148 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002149
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002150 /*
2151 * Usually the netlabel code will handle changing the
2152 * packet labeling based on the label.
2153 * The case of a single label host is different, because
2154 * a single label host should never get a labeled packet
2155 * even though the label is usually associated with a packet
2156 * label.
2157 */
2158 local_bh_disable();
2159 bh_lock_sock_nested(sk);
2160
2161 if (ssp->smk_out == smack_net_ambient ||
2162 labeled == SMACK_UNLABELED_SOCKET)
2163 netlbl_sock_delattr(sk);
2164 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002165 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002166 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002167 }
2168
2169 bh_unlock_sock(sk);
2170 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002171
Casey Schauflere114e472008-02-04 22:29:50 -08002172 return rc;
2173}
2174
2175/**
Paul Moore07feee82009-03-27 17:10:54 -04002176 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2177 * @sk: the socket
2178 * @sap: the destination address
2179 *
2180 * Set the correct secattr for the given socket based on the destination
2181 * address and perform any outbound access checks needed.
2182 *
2183 * Returns 0 on success or an error code.
2184 *
2185 */
2186static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2187{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002188 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002189 int rc;
2190 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002191 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002192 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002193 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002194
2195 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002196 hkp = smack_host_label(sap);
2197 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002198#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002199 struct lsm_network_audit net;
2200
Eric Paris48c62af2012-04-02 13:15:44 -04002201 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2202 ad.a.u.net->family = sap->sin_family;
2203 ad.a.u.net->dport = sap->sin_port;
2204 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002205#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002206 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002207 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002208 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2209 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002210 } else {
2211 sk_lbl = SMACK_CIPSO_SOCKET;
2212 rc = 0;
2213 }
2214 rcu_read_unlock();
2215 if (rc != 0)
2216 return rc;
2217
2218 return smack_netlabel(sk, sk_lbl);
2219}
2220
Casey Schaufler69f287a2014-12-12 17:08:40 -08002221#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Paul Moore07feee82009-03-27 17:10:54 -04002222/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002223 * smk_ipv6_port_label - Smack port access table management
2224 * @sock: socket
2225 * @address: address
2226 *
2227 * Create or update the port list entry
2228 */
2229static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2230{
2231 struct sock *sk = sock->sk;
2232 struct sockaddr_in6 *addr6;
2233 struct socket_smack *ssp = sock->sk->sk_security;
2234 struct smk_port_label *spp;
2235 unsigned short port = 0;
2236
2237 if (address == NULL) {
2238 /*
2239 * This operation is changing the Smack information
2240 * on the bound socket. Take the changes to the port
2241 * as well.
2242 */
2243 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2244 if (sk != spp->smk_sock)
2245 continue;
2246 spp->smk_in = ssp->smk_in;
2247 spp->smk_out = ssp->smk_out;
2248 return;
2249 }
2250 /*
2251 * A NULL address is only used for updating existing
2252 * bound entries. If there isn't one, it's OK.
2253 */
2254 return;
2255 }
2256
2257 addr6 = (struct sockaddr_in6 *)address;
2258 port = ntohs(addr6->sin6_port);
2259 /*
2260 * This is a special case that is safely ignored.
2261 */
2262 if (port == 0)
2263 return;
2264
2265 /*
2266 * Look for an existing port list entry.
2267 * This is an indication that a port is getting reused.
2268 */
2269 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2270 if (spp->smk_port != port)
2271 continue;
2272 spp->smk_port = port;
2273 spp->smk_sock = sk;
2274 spp->smk_in = ssp->smk_in;
2275 spp->smk_out = ssp->smk_out;
2276 return;
2277 }
2278
2279 /*
2280 * A new port entry is required.
2281 */
2282 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2283 if (spp == NULL)
2284 return;
2285
2286 spp->smk_port = port;
2287 spp->smk_sock = sk;
2288 spp->smk_in = ssp->smk_in;
2289 spp->smk_out = ssp->smk_out;
2290
2291 list_add(&spp->list, &smk_ipv6_port_list);
2292 return;
2293}
2294
2295/**
2296 * smk_ipv6_port_check - check Smack port access
2297 * @sock: socket
2298 * @address: address
2299 *
2300 * Create or update the port list entry
2301 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002302static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002303 int act)
2304{
2305 __be16 *bep;
2306 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002307 struct smk_port_label *spp;
2308 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002309 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002310 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002311 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002312 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002313 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002314#ifdef CONFIG_AUDIT
2315 struct lsm_network_audit net;
2316#endif
2317
2318 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002319 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002320 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002321 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002322 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002323 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002324 }
2325
2326 /*
2327 * Get the IP address and port from the address.
2328 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002329 port = ntohs(address->sin6_port);
2330 bep = (__be16 *)(&address->sin6_addr);
2331 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002332
2333 /*
2334 * It's remote, so port lookup does no good.
2335 */
2336 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2337 goto auditout;
2338
2339 /*
2340 * It's local so the send check has to have passed.
2341 */
2342 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002343 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002344 goto auditout;
2345 }
2346
2347 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2348 if (spp->smk_port != port)
2349 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002350 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002351 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002352 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002353 break;
2354 }
2355
2356auditout:
2357
2358#ifdef CONFIG_AUDIT
2359 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2360 ad.a.u.net->family = sk->sk_family;
2361 ad.a.u.net->dport = port;
2362 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002363 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002364 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002365 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002366#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002367 rc = smk_access(skp, object, MAY_WRITE, &ad);
2368 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2369 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002370}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002371#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002372
2373/**
Casey Schauflere114e472008-02-04 22:29:50 -08002374 * smack_inode_setsecurity - set smack xattrs
2375 * @inode: the object
2376 * @name: attribute name
2377 * @value: attribute value
2378 * @size: size of the attribute
2379 * @flags: unused
2380 *
2381 * Sets the named attribute in the appropriate blob
2382 *
2383 * Returns 0 on success, or an error code
2384 */
2385static int smack_inode_setsecurity(struct inode *inode, const char *name,
2386 const void *value, size_t size, int flags)
2387{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002388 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002389 struct inode_smack *nsp = inode->i_security;
2390 struct socket_smack *ssp;
2391 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002392 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002393
Casey Schauflerf7112e62012-05-06 15:22:02 -07002394 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302395 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002396
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002397 skp = smk_import_entry(value, size);
2398 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002399 return -EINVAL;
2400
2401 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002402 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002403 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002404 return 0;
2405 }
2406 /*
2407 * The rest of the Smack xattrs are only on sockets.
2408 */
2409 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2410 return -EOPNOTSUPP;
2411
2412 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002413 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002414 return -EOPNOTSUPP;
2415
2416 ssp = sock->sk->sk_security;
2417
2418 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002419 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002420 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002421 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002422 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002423 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2424 if (rc != 0)
2425 printk(KERN_WARNING
2426 "Smack: \"%s\" netlbl error %d.\n",
2427 __func__, -rc);
2428 }
Casey Schauflere114e472008-02-04 22:29:50 -08002429 } else
2430 return -EOPNOTSUPP;
2431
Casey Schaufler69f287a2014-12-12 17:08:40 -08002432#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07002433 if (sock->sk->sk_family == PF_INET6)
2434 smk_ipv6_port_label(sock, NULL);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002435#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002436
Casey Schauflere114e472008-02-04 22:29:50 -08002437 return 0;
2438}
2439
2440/**
2441 * smack_socket_post_create - finish socket setup
2442 * @sock: the socket
2443 * @family: protocol family
2444 * @type: unused
2445 * @protocol: unused
2446 * @kern: unused
2447 *
2448 * Sets the netlabel information on the socket
2449 *
2450 * Returns 0 on success, and error code otherwise
2451 */
2452static int smack_socket_post_create(struct socket *sock, int family,
2453 int type, int protocol, int kern)
2454{
Marcin Lis74123012015-01-22 15:40:33 +01002455 struct socket_smack *ssp;
2456
2457 if (sock->sk == NULL)
2458 return 0;
2459
2460 /*
2461 * Sockets created by kernel threads receive web label.
2462 */
2463 if (unlikely(current->flags & PF_KTHREAD)) {
2464 ssp = sock->sk->sk_security;
2465 ssp->smk_in = &smack_known_web;
2466 ssp->smk_out = &smack_known_web;
2467 }
2468
2469 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002470 return 0;
2471 /*
2472 * Set the outbound netlbl.
2473 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002474 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2475}
2476
Casey Schaufler69f287a2014-12-12 17:08:40 -08002477#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002478/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002479 * smack_socket_bind - record port binding information.
2480 * @sock: the socket
2481 * @address: the port address
2482 * @addrlen: size of the address
2483 *
2484 * Records the label bound to a port.
2485 *
2486 * Returns 0
2487 */
2488static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2489 int addrlen)
2490{
Casey Schaufler69f287a2014-12-12 17:08:40 -08002491#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002492 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2493 smk_ipv6_port_label(sock, address);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002494#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002495
2496 return 0;
2497}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002498#endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002499
2500/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002501 * smack_socket_connect - connect access check
2502 * @sock: the socket
2503 * @sap: the other end
2504 * @addrlen: size of sap
2505 *
2506 * Verifies that a connection may be possible
2507 *
2508 * Returns 0 on success, and error code otherwise
2509 */
2510static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2511 int addrlen)
2512{
Casey Schauflerc6739442013-05-22 18:42:56 -07002513 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002514
Casey Schauflerc6739442013-05-22 18:42:56 -07002515 if (sock->sk == NULL)
2516 return 0;
2517
2518 switch (sock->sk->sk_family) {
2519 case PF_INET:
2520 if (addrlen < sizeof(struct sockaddr_in))
2521 return -EINVAL;
2522 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2523 break;
2524 case PF_INET6:
2525 if (addrlen < sizeof(struct sockaddr_in6))
2526 return -EINVAL;
Casey Schaufler69f287a2014-12-12 17:08:40 -08002527#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002528 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2529 SMK_CONNECTING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002530#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002531 break;
2532 }
2533 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002534}
2535
2536/**
2537 * smack_flags_to_may - convert S_ to MAY_ values
2538 * @flags: the S_ value
2539 *
2540 * Returns the equivalent MAY_ value
2541 */
2542static int smack_flags_to_may(int flags)
2543{
2544 int may = 0;
2545
2546 if (flags & S_IRUGO)
2547 may |= MAY_READ;
2548 if (flags & S_IWUGO)
2549 may |= MAY_WRITE;
2550 if (flags & S_IXUGO)
2551 may |= MAY_EXEC;
2552
2553 return may;
2554}
2555
2556/**
2557 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2558 * @msg: the object
2559 *
2560 * Returns 0
2561 */
2562static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2563{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002564 struct smack_known *skp = smk_of_current();
2565
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002566 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002567 return 0;
2568}
2569
2570/**
2571 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2572 * @msg: the object
2573 *
2574 * Clears the blob pointer
2575 */
2576static void smack_msg_msg_free_security(struct msg_msg *msg)
2577{
2578 msg->security = NULL;
2579}
2580
2581/**
2582 * smack_of_shm - the smack pointer for the shm
2583 * @shp: the object
2584 *
2585 * Returns a pointer to the smack value
2586 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002587static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002588{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002589 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002590}
2591
2592/**
2593 * smack_shm_alloc_security - Set the security blob for shm
2594 * @shp: the object
2595 *
2596 * Returns 0
2597 */
2598static int smack_shm_alloc_security(struct shmid_kernel *shp)
2599{
2600 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002601 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002602
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002603 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002604 return 0;
2605}
2606
2607/**
2608 * smack_shm_free_security - Clear the security blob for shm
2609 * @shp: the object
2610 *
2611 * Clears the blob pointer
2612 */
2613static void smack_shm_free_security(struct shmid_kernel *shp)
2614{
2615 struct kern_ipc_perm *isp = &shp->shm_perm;
2616
2617 isp->security = NULL;
2618}
2619
2620/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002621 * smk_curacc_shm : check if current has access on shm
2622 * @shp : the object
2623 * @access : access requested
2624 *
2625 * Returns 0 if current has the requested access, error code otherwise
2626 */
2627static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2628{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002629 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002630 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002631 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002632
2633#ifdef CONFIG_AUDIT
2634 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2635 ad.a.u.ipc_id = shp->shm_perm.id;
2636#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002637 rc = smk_curacc(ssp, access, &ad);
2638 rc = smk_bu_current("shm", ssp, access, rc);
2639 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002640}
2641
2642/**
Casey Schauflere114e472008-02-04 22:29:50 -08002643 * smack_shm_associate - Smack access check for shm
2644 * @shp: the object
2645 * @shmflg: access requested
2646 *
2647 * Returns 0 if current has the requested access, error code otherwise
2648 */
2649static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2650{
Casey Schauflere114e472008-02-04 22:29:50 -08002651 int may;
2652
2653 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002654 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002655}
2656
2657/**
2658 * smack_shm_shmctl - Smack access check for shm
2659 * @shp: the object
2660 * @cmd: what it wants to do
2661 *
2662 * Returns 0 if current has the requested access, error code otherwise
2663 */
2664static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2665{
Casey Schauflere114e472008-02-04 22:29:50 -08002666 int may;
2667
2668 switch (cmd) {
2669 case IPC_STAT:
2670 case SHM_STAT:
2671 may = MAY_READ;
2672 break;
2673 case IPC_SET:
2674 case SHM_LOCK:
2675 case SHM_UNLOCK:
2676 case IPC_RMID:
2677 may = MAY_READWRITE;
2678 break;
2679 case IPC_INFO:
2680 case SHM_INFO:
2681 /*
2682 * System level information.
2683 */
2684 return 0;
2685 default:
2686 return -EINVAL;
2687 }
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_shm_shmat - Smack access for shmat
2693 * @shp: the object
2694 * @shmaddr: unused
2695 * @shmflg: access requested
2696 *
2697 * Returns 0 if current has the requested access, error code otherwise
2698 */
2699static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2700 int shmflg)
2701{
Casey Schauflere114e472008-02-04 22:29:50 -08002702 int may;
2703
2704 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002705 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002706}
2707
2708/**
2709 * smack_of_sem - the smack pointer for the sem
2710 * @sma: the object
2711 *
2712 * Returns a pointer to the smack value
2713 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002714static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002715{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002716 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002717}
2718
2719/**
2720 * smack_sem_alloc_security - Set the security blob for sem
2721 * @sma: the object
2722 *
2723 * Returns 0
2724 */
2725static int smack_sem_alloc_security(struct sem_array *sma)
2726{
2727 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002728 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002729
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002730 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002731 return 0;
2732}
2733
2734/**
2735 * smack_sem_free_security - Clear the security blob for sem
2736 * @sma: the object
2737 *
2738 * Clears the blob pointer
2739 */
2740static void smack_sem_free_security(struct sem_array *sma)
2741{
2742 struct kern_ipc_perm *isp = &sma->sem_perm;
2743
2744 isp->security = NULL;
2745}
2746
2747/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002748 * smk_curacc_sem : check if current has access on sem
2749 * @sma : the object
2750 * @access : access requested
2751 *
2752 * Returns 0 if current has the requested access, error code otherwise
2753 */
2754static int smk_curacc_sem(struct sem_array *sma, int access)
2755{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002756 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002757 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002758 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002759
2760#ifdef CONFIG_AUDIT
2761 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2762 ad.a.u.ipc_id = sma->sem_perm.id;
2763#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002764 rc = smk_curacc(ssp, access, &ad);
2765 rc = smk_bu_current("sem", ssp, access, rc);
2766 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002767}
2768
2769/**
Casey Schauflere114e472008-02-04 22:29:50 -08002770 * smack_sem_associate - Smack access check for sem
2771 * @sma: the object
2772 * @semflg: access requested
2773 *
2774 * Returns 0 if current has the requested access, error code otherwise
2775 */
2776static int smack_sem_associate(struct sem_array *sma, int semflg)
2777{
Casey Schauflere114e472008-02-04 22:29:50 -08002778 int may;
2779
2780 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002781 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002782}
2783
2784/**
2785 * smack_sem_shmctl - Smack access check for sem
2786 * @sma: the object
2787 * @cmd: what it wants to do
2788 *
2789 * Returns 0 if current has the requested access, error code otherwise
2790 */
2791static int smack_sem_semctl(struct sem_array *sma, int cmd)
2792{
Casey Schauflere114e472008-02-04 22:29:50 -08002793 int may;
2794
2795 switch (cmd) {
2796 case GETPID:
2797 case GETNCNT:
2798 case GETZCNT:
2799 case GETVAL:
2800 case GETALL:
2801 case IPC_STAT:
2802 case SEM_STAT:
2803 may = MAY_READ;
2804 break;
2805 case SETVAL:
2806 case SETALL:
2807 case IPC_RMID:
2808 case IPC_SET:
2809 may = MAY_READWRITE;
2810 break;
2811 case IPC_INFO:
2812 case SEM_INFO:
2813 /*
2814 * System level information
2815 */
2816 return 0;
2817 default:
2818 return -EINVAL;
2819 }
2820
Etienne Bassetecfcc532009-04-08 20:40:06 +02002821 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002822}
2823
2824/**
2825 * smack_sem_semop - Smack checks of semaphore operations
2826 * @sma: the object
2827 * @sops: unused
2828 * @nsops: unused
2829 * @alter: unused
2830 *
2831 * Treated as read and write in all cases.
2832 *
2833 * Returns 0 if access is allowed, error code otherwise
2834 */
2835static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2836 unsigned nsops, int alter)
2837{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002838 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002839}
2840
2841/**
2842 * smack_msg_alloc_security - Set the security blob for msg
2843 * @msq: the object
2844 *
2845 * Returns 0
2846 */
2847static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2848{
2849 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002850 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002851
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002852 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002853 return 0;
2854}
2855
2856/**
2857 * smack_msg_free_security - Clear the security blob for msg
2858 * @msq: the object
2859 *
2860 * Clears the blob pointer
2861 */
2862static void smack_msg_queue_free_security(struct msg_queue *msq)
2863{
2864 struct kern_ipc_perm *kisp = &msq->q_perm;
2865
2866 kisp->security = NULL;
2867}
2868
2869/**
2870 * smack_of_msq - the smack pointer for the msq
2871 * @msq: the object
2872 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002873 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002874 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002875static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002876{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002877 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002878}
2879
2880/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002881 * smk_curacc_msq : helper to check if current has access on msq
2882 * @msq : the msq
2883 * @access : access requested
2884 *
2885 * return 0 if current has access, error otherwise
2886 */
2887static int smk_curacc_msq(struct msg_queue *msq, int access)
2888{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002889 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002890 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002891 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002892
2893#ifdef CONFIG_AUDIT
2894 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2895 ad.a.u.ipc_id = msq->q_perm.id;
2896#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002897 rc = smk_curacc(msp, access, &ad);
2898 rc = smk_bu_current("msq", msp, access, rc);
2899 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002900}
2901
2902/**
Casey Schauflere114e472008-02-04 22:29:50 -08002903 * smack_msg_queue_associate - Smack access check for msg_queue
2904 * @msq: the object
2905 * @msqflg: access requested
2906 *
2907 * Returns 0 if current has the requested access, error code otherwise
2908 */
2909static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2910{
Casey Schauflere114e472008-02-04 22:29:50 -08002911 int may;
2912
2913 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002914 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002915}
2916
2917/**
2918 * smack_msg_queue_msgctl - Smack access check for msg_queue
2919 * @msq: the object
2920 * @cmd: what it wants to do
2921 *
2922 * Returns 0 if current has the requested access, error code otherwise
2923 */
2924static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2925{
Casey Schauflere114e472008-02-04 22:29:50 -08002926 int may;
2927
2928 switch (cmd) {
2929 case IPC_STAT:
2930 case MSG_STAT:
2931 may = MAY_READ;
2932 break;
2933 case IPC_SET:
2934 case IPC_RMID:
2935 may = MAY_READWRITE;
2936 break;
2937 case IPC_INFO:
2938 case MSG_INFO:
2939 /*
2940 * System level information
2941 */
2942 return 0;
2943 default:
2944 return -EINVAL;
2945 }
2946
Etienne Bassetecfcc532009-04-08 20:40:06 +02002947 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 * @msqflg: access requested
2955 *
2956 * Returns 0 if current has the requested access, error code otherwise
2957 */
2958static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2959 int msqflg)
2960{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002961 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002962
Etienne Bassetecfcc532009-04-08 20:40:06 +02002963 may = smack_flags_to_may(msqflg);
2964 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002965}
2966
2967/**
2968 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2969 * @msq: the object
2970 * @msg: unused
2971 * @target: unused
2972 * @type: unused
2973 * @mode: unused
2974 *
2975 * Returns 0 if current has read and write access, error code otherwise
2976 */
2977static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2978 struct task_struct *target, long type, int mode)
2979{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002980 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002981}
2982
2983/**
2984 * smack_ipc_permission - Smack access for ipc_permission()
2985 * @ipp: the object permissions
2986 * @flag: access requested
2987 *
2988 * Returns 0 if current has read and write access, error code otherwise
2989 */
2990static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2991{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002992 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002993 int may = smack_flags_to_may(flag);
2994 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002995 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002996
Etienne Bassetecfcc532009-04-08 20:40:06 +02002997#ifdef CONFIG_AUDIT
2998 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2999 ad.a.u.ipc_id = ipp->id;
3000#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003001 rc = smk_curacc(iskp, may, &ad);
3002 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003003 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003004}
3005
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003006/**
3007 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003008 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003009 * @secid: where result will be saved
3010 */
3011static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3012{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003013 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003014
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003015 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003016}
3017
Casey Schauflere114e472008-02-04 22:29:50 -08003018/**
3019 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003020 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003021 * @inode: the object
3022 *
3023 * Set the inode's security blob if it hasn't been done already.
3024 */
3025static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3026{
3027 struct super_block *sbp;
3028 struct superblock_smack *sbsp;
3029 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003030 struct smack_known *skp;
3031 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003032 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003033 char trattr[TRANS_TRUE_SIZE];
3034 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003035 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003036 struct dentry *dp;
3037
3038 if (inode == NULL)
3039 return;
3040
3041 isp = inode->i_security;
3042
3043 mutex_lock(&isp->smk_lock);
3044 /*
3045 * If the inode is already instantiated
3046 * take the quick way out
3047 */
3048 if (isp->smk_flags & SMK_INODE_INSTANT)
3049 goto unlockandout;
3050
3051 sbp = inode->i_sb;
3052 sbsp = sbp->s_security;
3053 /*
3054 * We're going to use the superblock default label
3055 * if there's no label on the file.
3056 */
3057 final = sbsp->smk_default;
3058
3059 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003060 * If this is the root inode the superblock
3061 * may be in the process of initialization.
3062 * If that is the case use the root value out
3063 * of the superblock.
3064 */
3065 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003066 switch (sbp->s_magic) {
3067 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003068 /*
3069 * The cgroup filesystem is never mounted,
3070 * so there's no opportunity to set the mount
3071 * options.
3072 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003073 sbsp->smk_root = &smack_known_star;
3074 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003075 isp->smk_inode = sbsp->smk_root;
3076 break;
3077 case TMPFS_MAGIC:
3078 /*
3079 * What about shmem/tmpfs anonymous files with dentry
3080 * obtained from d_alloc_pseudo()?
3081 */
3082 isp->smk_inode = smk_of_current();
3083 break;
3084 default:
3085 isp->smk_inode = sbsp->smk_root;
3086 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003087 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003088 isp->smk_flags |= SMK_INODE_INSTANT;
3089 goto unlockandout;
3090 }
3091
3092 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003093 * This is pretty hackish.
3094 * Casey says that we shouldn't have to do
3095 * file system specific code, but it does help
3096 * with keeping it simple.
3097 */
3098 switch (sbp->s_magic) {
3099 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003100 case PIPEFS_MAGIC:
3101 case SOCKFS_MAGIC:
3102 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003103 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003104 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003105 * that the smack file system doesn't do
3106 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003107 *
Casey Schauflere114e472008-02-04 22:29:50 -08003108 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003109 *
3110 * Socket access is controlled by the socket
3111 * structures associated with the task involved.
3112 *
3113 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003114 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003115 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003116 break;
3117 case DEVPTS_SUPER_MAGIC:
3118 /*
3119 * devpts seems content with the label of the task.
3120 * Programs that change smack have to treat the
3121 * pty with respect.
3122 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003123 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003124 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003125 case PROC_SUPER_MAGIC:
3126 /*
3127 * Casey says procfs appears not to care.
3128 * The superblock default suffices.
3129 */
3130 break;
3131 case TMPFS_MAGIC:
3132 /*
3133 * Device labels should come from the filesystem,
3134 * but watch out, because they're volitile,
3135 * getting recreated on every reboot.
3136 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003137 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003138 /*
3139 * No break.
3140 *
3141 * If a smack value has been set we want to use it,
3142 * but since tmpfs isn't giving us the opportunity
3143 * to set mount options simulate setting the
3144 * superblock default.
3145 */
3146 default:
3147 /*
3148 * This isn't an understood special case.
3149 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003150 */
3151
3152 /*
3153 * UNIX domain sockets use lower level socket data.
3154 */
3155 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003156 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003157 break;
3158 }
3159 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003160 * No xattr support means, alas, no SMACK label.
3161 * Use the aforeapplied default.
3162 * It would be curious if the label of the task
3163 * does not match that assigned.
3164 */
3165 if (inode->i_op->getxattr == NULL)
3166 break;
3167 /*
3168 * Get the dentry for xattr.
3169 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003170 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003171 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3172 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003173 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003174
3175 /*
3176 * Transmuting directory
3177 */
3178 if (S_ISDIR(inode->i_mode)) {
3179 /*
3180 * If this is a new directory and the label was
3181 * transmuted when the inode was initialized
3182 * set the transmute attribute on the directory
3183 * and mark the inode.
3184 *
3185 * If there is a transmute attribute on the
3186 * directory mark the inode.
3187 */
3188 if (isp->smk_flags & SMK_INODE_CHANGED) {
3189 isp->smk_flags &= ~SMK_INODE_CHANGED;
3190 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003191 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003192 TRANS_TRUE, TRANS_TRUE_SIZE,
3193 0);
3194 } else {
3195 rc = inode->i_op->getxattr(dp,
3196 XATTR_NAME_SMACKTRANSMUTE, trattr,
3197 TRANS_TRUE_SIZE);
3198 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3199 TRANS_TRUE_SIZE) != 0)
3200 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003201 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003202 if (rc >= 0)
3203 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003204 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003205 /*
3206 * Don't let the exec or mmap label be "*" or "@".
3207 */
3208 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3209 if (skp == &smack_known_star || skp == &smack_known_web)
3210 skp = NULL;
3211 isp->smk_task = skp;
3212 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3213 if (skp == &smack_known_star || skp == &smack_known_web)
3214 skp = NULL;
3215 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003216
Casey Schauflere114e472008-02-04 22:29:50 -08003217 dput(dp);
3218 break;
3219 }
3220
3221 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003222 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003223 else
3224 isp->smk_inode = final;
3225
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003226 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003227
3228unlockandout:
3229 mutex_unlock(&isp->smk_lock);
3230 return;
3231}
3232
3233/**
3234 * smack_getprocattr - Smack process attribute access
3235 * @p: the object task
3236 * @name: the name of the attribute in /proc/.../attr
3237 * @value: where to put the result
3238 *
3239 * Places a copy of the task Smack into value
3240 *
3241 * Returns the length of the smack label or an error code
3242 */
3243static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3244{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003245 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003246 char *cp;
3247 int slen;
3248
3249 if (strcmp(name, "current") != 0)
3250 return -EINVAL;
3251
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003252 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003253 if (cp == NULL)
3254 return -ENOMEM;
3255
3256 slen = strlen(cp);
3257 *value = cp;
3258 return slen;
3259}
3260
3261/**
3262 * smack_setprocattr - Smack process attribute setting
3263 * @p: the object task
3264 * @name: the name of the attribute in /proc/.../attr
3265 * @value: the value to set
3266 * @size: the size of the value
3267 *
3268 * Sets the Smack value of the task. Only setting self
3269 * is permitted and only with privilege
3270 *
3271 * Returns the length of the smack label or an error code
3272 */
3273static int smack_setprocattr(struct task_struct *p, char *name,
3274 void *value, size_t size)
3275{
Casey Schaufler676dac42010-12-02 06:43:39 -08003276 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003277 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003278 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003279
Casey Schauflere114e472008-02-04 22:29:50 -08003280 /*
3281 * Changing another process' Smack value is too dangerous
3282 * and supports no sane use case.
3283 */
3284 if (p != current)
3285 return -EPERM;
3286
Casey Schaufler1880eff2012-06-05 15:28:30 -07003287 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003288 return -EPERM;
3289
Casey Schauflerf7112e62012-05-06 15:22:02 -07003290 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003291 return -EINVAL;
3292
3293 if (strcmp(name, "current") != 0)
3294 return -EINVAL;
3295
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003296 skp = smk_import_entry(value, size);
3297 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003298 return -EINVAL;
3299
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003300 /*
3301 * No process is ever allowed the web ("@") label.
3302 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003303 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003304 return -EPERM;
3305
David Howellsd84f4f92008-11-14 10:39:23 +11003306 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003307 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003308 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003309
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003310 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003311 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003312
David Howellsd84f4f92008-11-14 10:39:23 +11003313 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003314 return size;
3315}
3316
3317/**
3318 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003319 * @sock: one sock
3320 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003321 * @newsk: unused
3322 *
3323 * Return 0 if a subject with the smack of sock could access
3324 * an object with the smack of other, otherwise an error code
3325 */
David S. Miller3610cda2011-01-05 15:38:53 -08003326static int smack_unix_stream_connect(struct sock *sock,
3327 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003328{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003329 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003330 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003331 struct socket_smack *ssp = sock->sk_security;
3332 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003333 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003334 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003335 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003336#ifdef CONFIG_AUDIT
3337 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003338#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003339
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003340 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3341 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003342 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003343#ifdef CONFIG_AUDIT
3344 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3345 smk_ad_setfield_u_net_sk(&ad, other);
3346#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003347 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3348 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003349 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003350 okp = osp->smk_out;
3351 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003352 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003353 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003354 MAY_WRITE, rc);
3355 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003356 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003357
Casey Schaufler975d5e52011-09-26 14:43:39 -07003358 /*
3359 * Cross reference the peer labels for SO_PEERSEC.
3360 */
3361 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003362 nsp->smk_packet = ssp->smk_out;
3363 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003364 }
3365
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003366 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003367}
3368
3369/**
3370 * smack_unix_may_send - Smack access on UDS
3371 * @sock: one socket
3372 * @other: the other socket
3373 *
3374 * Return 0 if a subject with the smack of sock could access
3375 * an object with the smack of other, otherwise an error code
3376 */
3377static int smack_unix_may_send(struct socket *sock, struct socket *other)
3378{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003379 struct socket_smack *ssp = sock->sk->sk_security;
3380 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003381 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003382 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003383
Kees Cook923e9a12012-04-10 13:26:44 -07003384#ifdef CONFIG_AUDIT
3385 struct lsm_network_audit net;
3386
Eric Paris48c62af2012-04-02 13:15:44 -04003387 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003388 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003389#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003390
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003391 if (smack_privileged(CAP_MAC_OVERRIDE))
3392 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003393
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003394 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3395 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003396 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003397}
3398
3399/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003400 * smack_socket_sendmsg - Smack check based on destination host
3401 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003402 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003403 * @size: the size of the message
3404 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003405 * Return 0 if the current subject can write to the destination host.
3406 * For IPv4 this is only a question if the destination is a single label host.
3407 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003408 */
3409static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3410 int size)
3411{
3412 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003413#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003414 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003415#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003416 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003417
3418 /*
3419 * Perfectly reasonable for this to be NULL
3420 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003421 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003422 return 0;
3423
Casey Schauflerc6739442013-05-22 18:42:56 -07003424 switch (sip->sin_family) {
3425 case AF_INET:
3426 rc = smack_netlabel_send(sock->sk, sip);
3427 break;
3428 case AF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003429#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07003430 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003431#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003432 break;
3433 }
3434 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003435}
3436
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003437/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003438 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003439 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003440 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003441 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003442 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003443 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003444static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3445 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003446{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003447 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003448 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003449 int acat;
3450 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003451
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003452 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003453 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003454 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003455 * If there are flags but no level netlabel isn't
3456 * behaving the way we expect it to.
3457 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003458 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003459 * Without guidance regarding the smack value
3460 * for the packet fall back on the network
3461 * ambient value.
3462 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003463 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003464 list_for_each_entry(skp, &smack_known_list, list) {
3465 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003466 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003467 /*
3468 * Compare the catsets. Use the netlbl APIs.
3469 */
3470 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3471 if ((skp->smk_netlabel.flags &
3472 NETLBL_SECATTR_MLS_CAT) == 0)
3473 found = 1;
3474 break;
3475 }
3476 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003477 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3478 acat + 1);
3479 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003480 skp->smk_netlabel.attr.mls.cat,
3481 kcat + 1);
3482 if (acat < 0 || kcat < 0)
3483 break;
3484 }
3485 if (acat == kcat) {
3486 found = 1;
3487 break;
3488 }
Casey Schauflere114e472008-02-04 22:29:50 -08003489 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003490 rcu_read_unlock();
3491
3492 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003493 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003494
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003495 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003496 return &smack_known_web;
3497 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003498 }
3499 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3500 /*
3501 * Looks like a fallback, which gives us a secid.
3502 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003503 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003504 /*
3505 * This has got to be a bug because it is
3506 * impossible to specify a fallback without
3507 * specifying the label, which will ensure
3508 * it has a secid, and the only way to get a
3509 * secid is from a fallback.
3510 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003511 BUG_ON(skp == NULL);
3512 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003513 }
3514 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003515 * Without guidance regarding the smack value
3516 * for the packet fall back on the network
3517 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003518 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003519 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003520}
3521
Casey Schaufler69f287a2014-12-12 17:08:40 -08003522#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003523static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003524{
Casey Schauflerc6739442013-05-22 18:42:56 -07003525 u8 nexthdr;
3526 int offset;
3527 int proto = -EINVAL;
3528 struct ipv6hdr _ipv6h;
3529 struct ipv6hdr *ip6;
3530 __be16 frag_off;
3531 struct tcphdr _tcph, *th;
3532 struct udphdr _udph, *uh;
3533 struct dccp_hdr _dccph, *dh;
3534
3535 sip->sin6_port = 0;
3536
3537 offset = skb_network_offset(skb);
3538 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3539 if (ip6 == NULL)
3540 return -EINVAL;
3541 sip->sin6_addr = ip6->saddr;
3542
3543 nexthdr = ip6->nexthdr;
3544 offset += sizeof(_ipv6h);
3545 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3546 if (offset < 0)
3547 return -EINVAL;
3548
3549 proto = nexthdr;
3550 switch (proto) {
3551 case IPPROTO_TCP:
3552 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3553 if (th != NULL)
3554 sip->sin6_port = th->source;
3555 break;
3556 case IPPROTO_UDP:
3557 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3558 if (uh != NULL)
3559 sip->sin6_port = uh->source;
3560 break;
3561 case IPPROTO_DCCP:
3562 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3563 if (dh != NULL)
3564 sip->sin6_port = dh->dccph_sport;
3565 break;
3566 }
3567 return proto;
3568}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003569#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003570
Casey Schauflere114e472008-02-04 22:29:50 -08003571/**
3572 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3573 * @sk: socket
3574 * @skb: packet
3575 *
3576 * Returns 0 if the packet should be delivered, an error code otherwise
3577 */
3578static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3579{
3580 struct netlbl_lsm_secattr secattr;
3581 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003582 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003583 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003584 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003585#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003586 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003587#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003588#if IS_ENABLED(CONFIG_IPV6)
3589 struct sockaddr_in6 sadd;
3590 int proto;
3591#endif /* CONFIG_IPV6 */
3592
Casey Schauflerc6739442013-05-22 18:42:56 -07003593 switch (sk->sk_family) {
3594 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003595#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3596 /*
3597 * If there is a secmark use it rather than the CIPSO label.
3598 * If there is no secmark fall back to CIPSO.
3599 * The secmark is assumed to reflect policy better.
3600 */
3601 if (skb && skb->secmark != 0) {
3602 skp = smack_from_secid(skb->secmark);
3603 goto access_check;
3604 }
3605#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003606 /*
3607 * Translate what netlabel gave us.
3608 */
3609 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003610
Casey Schauflerc6739442013-05-22 18:42:56 -07003611 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3612 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003613 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003614 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003615 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003616
Casey Schauflerc6739442013-05-22 18:42:56 -07003617 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003618
Casey Schaufler69f287a2014-12-12 17:08:40 -08003619#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3620access_check:
3621#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003622#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003623 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3624 ad.a.u.net->family = sk->sk_family;
3625 ad.a.u.net->netif = skb->skb_iif;
3626 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003627#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003628 /*
3629 * Receiving a packet requires that the other end
3630 * be able to write here. Read access is not required.
3631 * This is the simplist possible security model
3632 * for networking.
3633 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003634 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3635 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003636 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003637 if (rc != 0)
3638 netlbl_skbuff_err(skb, rc, 0);
3639 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003640#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003641 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003642 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3643 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3644 break;
3645#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3646 if (skb && skb->secmark != 0)
3647 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003648 else
Casey Schaufler69f287a2014-12-12 17:08:40 -08003649 skp = smack_net_ambient;
3650#ifdef CONFIG_AUDIT
3651 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3652 ad.a.u.net->family = sk->sk_family;
3653 ad.a.u.net->netif = skb->skb_iif;
3654 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3655#endif /* CONFIG_AUDIT */
3656 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3657 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3658 MAY_WRITE, rc);
3659#else /* CONFIG_SECURITY_SMACK_NETFILTER */
3660 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3661#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003662 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003663#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003664 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003665
Paul Moorea8134292008-10-10 10:16:31 -04003666 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003667}
3668
3669/**
3670 * smack_socket_getpeersec_stream - pull in packet label
3671 * @sock: the socket
3672 * @optval: user's destination
3673 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003674 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003675 *
3676 * returns zero on success, an error code otherwise
3677 */
3678static int smack_socket_getpeersec_stream(struct socket *sock,
3679 char __user *optval,
3680 int __user *optlen, unsigned len)
3681{
3682 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003683 char *rcp = "";
3684 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003685 int rc = 0;
3686
3687 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003688 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003689 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003690 slen = strlen(rcp) + 1;
3691 }
Casey Schauflere114e472008-02-04 22:29:50 -08003692
3693 if (slen > len)
3694 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003695 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003696 rc = -EFAULT;
3697
3698 if (put_user(slen, optlen) != 0)
3699 rc = -EFAULT;
3700
3701 return rc;
3702}
3703
3704
3705/**
3706 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003707 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003708 * @skb: packet data
3709 * @secid: pointer to where to put the secid of the packet
3710 *
3711 * Sets the netlabel socket state on sk from parent
3712 */
3713static int smack_socket_getpeersec_dgram(struct socket *sock,
3714 struct sk_buff *skb, u32 *secid)
3715
3716{
3717 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003718 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003719 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003720 int family = PF_UNSPEC;
3721 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003722 int rc;
3723
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003724 if (skb != NULL) {
3725 if (skb->protocol == htons(ETH_P_IP))
3726 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003727#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003728 else if (skb->protocol == htons(ETH_P_IPV6))
3729 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003730#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003731 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003732 if (family == PF_UNSPEC && sock != NULL)
3733 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003734
Casey Schaufler69f287a2014-12-12 17:08:40 -08003735 switch (family) {
3736 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003737 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003738 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003739 break;
3740 case PF_INET:
3741#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3742 s = skb->secmark;
3743 if (s != 0)
3744 break;
3745#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003746 /*
3747 * Translate what netlabel gave us.
3748 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003749 if (sock != NULL && sock->sk != NULL)
3750 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003751 netlbl_secattr_init(&secattr);
3752 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3753 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003754 skp = smack_from_secattr(&secattr, ssp);
3755 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003756 }
3757 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003758 break;
3759#if IS_ENABLED(CONFIG_IPV6)
3760 case PF_INET6:
3761#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3762 s = skb->secmark;
3763#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3764 break;
3765#endif /* CONFIG_IPV6 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003766 }
3767 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003768 if (s == 0)
3769 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003770 return 0;
3771}
3772
3773/**
Paul Moore07feee82009-03-27 17:10:54 -04003774 * smack_sock_graft - Initialize a newly created socket with an existing sock
3775 * @sk: child sock
3776 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003777 *
Paul Moore07feee82009-03-27 17:10:54 -04003778 * Set the smk_{in,out} state of an existing sock based on the process that
3779 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003780 */
3781static void smack_sock_graft(struct sock *sk, struct socket *parent)
3782{
3783 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003784 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003785
Paul Moore07feee82009-03-27 17:10:54 -04003786 if (sk == NULL ||
3787 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003788 return;
3789
3790 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003791 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003792 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003793 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003794}
3795
3796/**
3797 * smack_inet_conn_request - Smack access check on connect
3798 * @sk: socket involved
3799 * @skb: packet
3800 * @req: unused
3801 *
3802 * Returns 0 if a task with the packet label could write to
3803 * the socket, otherwise an error code
3804 */
3805static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3806 struct request_sock *req)
3807{
Paul Moore07feee82009-03-27 17:10:54 -04003808 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003809 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003810 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003811 struct netlbl_lsm_secattr secattr;
3812 struct sockaddr_in addr;
3813 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003814 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003815 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003816 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003817#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003818 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003819#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003820
Casey Schaufler69f287a2014-12-12 17:08:40 -08003821#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003822 if (family == PF_INET6) {
3823 /*
3824 * Handle mapped IPv4 packets arriving
3825 * via IPv6 sockets. Don't set up netlabel
3826 * processing on IPv6.
3827 */
3828 if (skb->protocol == htons(ETH_P_IP))
3829 family = PF_INET;
3830 else
3831 return 0;
3832 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003833#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003834
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003835#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3836 /*
3837 * If there is a secmark use it rather than the CIPSO label.
3838 * If there is no secmark fall back to CIPSO.
3839 * The secmark is assumed to reflect policy better.
3840 */
3841 if (skb && skb->secmark != 0) {
3842 skp = smack_from_secid(skb->secmark);
3843 goto access_check;
3844 }
3845#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3846
Paul Moore07feee82009-03-27 17:10:54 -04003847 netlbl_secattr_init(&secattr);
3848 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003849 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003850 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003851 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003852 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003853 netlbl_secattr_destroy(&secattr);
3854
Casey Schaufler7f368ad2015-02-11 12:52:32 -08003855#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3856access_check:
3857#endif
3858
Etienne Bassetecfcc532009-04-08 20:40:06 +02003859#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003860 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3861 ad.a.u.net->family = family;
3862 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003863 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3864#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003865 /*
Paul Moore07feee82009-03-27 17:10:54 -04003866 * Receiving a packet requires that the other end be able to write
3867 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003868 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003869 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3870 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003871 if (rc != 0)
3872 return rc;
3873
3874 /*
3875 * Save the peer's label in the request_sock so we can later setup
3876 * smk_packet in the child socket so that SO_PEERCRED can report it.
3877 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003878 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003879
3880 /*
3881 * We need to decide if we want to label the incoming connection here
3882 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003883 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003884 */
3885 hdr = ip_hdr(skb);
3886 addr.sin_addr.s_addr = hdr->saddr;
3887 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003888 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003889 rcu_read_unlock();
3890
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003891 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003892 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003893 else
Paul Moore07feee82009-03-27 17:10:54 -04003894 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003895
3896 return rc;
3897}
3898
Paul Moore07feee82009-03-27 17:10:54 -04003899/**
3900 * smack_inet_csk_clone - Copy the connection information to the new socket
3901 * @sk: the new socket
3902 * @req: the connection's request_sock
3903 *
3904 * Transfer the connection's peer label to the newly created socket.
3905 */
3906static void smack_inet_csk_clone(struct sock *sk,
3907 const struct request_sock *req)
3908{
3909 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003910 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003911
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003912 if (req->peer_secid != 0) {
3913 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003914 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003915 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003916 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003917}
3918
Casey Schauflere114e472008-02-04 22:29:50 -08003919/*
3920 * Key management security hooks
3921 *
3922 * Casey has not tested key support very heavily.
3923 * The permission check is most likely too restrictive.
3924 * If you care about keys please have a look.
3925 */
3926#ifdef CONFIG_KEYS
3927
3928/**
3929 * smack_key_alloc - Set the key security blob
3930 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003931 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003932 * @flags: unused
3933 *
3934 * No allocation required
3935 *
3936 * Returns 0
3937 */
David Howellsd84f4f92008-11-14 10:39:23 +11003938static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003939 unsigned long flags)
3940{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003941 struct smack_known *skp = smk_of_task(cred->security);
3942
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003943 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003944 return 0;
3945}
3946
3947/**
3948 * smack_key_free - Clear the key security blob
3949 * @key: the object
3950 *
3951 * Clear the blob pointer
3952 */
3953static void smack_key_free(struct key *key)
3954{
3955 key->security = NULL;
3956}
3957
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003958/**
Casey Schauflere114e472008-02-04 22:29:50 -08003959 * smack_key_permission - Smack access on a key
3960 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003961 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003962 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08003963 *
3964 * Return 0 if the task has read and write to the object,
3965 * an error code otherwise
3966 */
3967static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00003968 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003969{
3970 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003971 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003972 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003973 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07003974 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003975
3976 keyp = key_ref_to_ptr(key_ref);
3977 if (keyp == NULL)
3978 return -EINVAL;
3979 /*
3980 * If the key hasn't been initialized give it access so that
3981 * it may do so.
3982 */
3983 if (keyp->security == NULL)
3984 return 0;
3985 /*
3986 * This should not occur
3987 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003988 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003989 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003990#ifdef CONFIG_AUDIT
3991 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3992 ad.a.u.key_struct.key = keyp->serial;
3993 ad.a.u.key_struct.key_desc = keyp->description;
3994#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003995 if (perm & KEY_NEED_READ)
3996 request = MAY_READ;
3997 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3998 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07003999 rc = smk_access(tkp, keyp->security, request, &ad);
4000 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4001 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004002}
4003#endif /* CONFIG_KEYS */
4004
4005/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004006 * Smack Audit hooks
4007 *
4008 * Audit requires a unique representation of each Smack specific
4009 * rule. This unique representation is used to distinguish the
4010 * object to be audited from remaining kernel objects and also
4011 * works as a glue between the audit hooks.
4012 *
4013 * Since repository entries are added but never deleted, we'll use
4014 * the smack_known label address related to the given audit rule as
4015 * the needed unique representation. This also better fits the smack
4016 * model where nearly everything is a label.
4017 */
4018#ifdef CONFIG_AUDIT
4019
4020/**
4021 * smack_audit_rule_init - Initialize a smack audit rule
4022 * @field: audit rule fields given from user-space (audit.h)
4023 * @op: required testing operator (=, !=, >, <, ...)
4024 * @rulestr: smack label to be audited
4025 * @vrule: pointer to save our own audit rule representation
4026 *
4027 * Prepare to audit cases where (@field @op @rulestr) is true.
4028 * The label to be audited is created if necessay.
4029 */
4030static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4031{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004032 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004033 char **rule = (char **)vrule;
4034 *rule = NULL;
4035
4036 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4037 return -EINVAL;
4038
Al Viro5af75d82008-12-16 05:59:26 -05004039 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004040 return -EINVAL;
4041
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004042 skp = smk_import_entry(rulestr, 0);
4043 if (skp)
4044 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004045
4046 return 0;
4047}
4048
4049/**
4050 * smack_audit_rule_known - Distinguish Smack audit rules
4051 * @krule: rule of interest, in Audit kernel representation format
4052 *
4053 * This is used to filter Smack rules from remaining Audit ones.
4054 * If it's proved that this rule belongs to us, the
4055 * audit_rule_match hook will be called to do the final judgement.
4056 */
4057static int smack_audit_rule_known(struct audit_krule *krule)
4058{
4059 struct audit_field *f;
4060 int i;
4061
4062 for (i = 0; i < krule->field_count; i++) {
4063 f = &krule->fields[i];
4064
4065 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4066 return 1;
4067 }
4068
4069 return 0;
4070}
4071
4072/**
4073 * smack_audit_rule_match - Audit given object ?
4074 * @secid: security id for identifying the object to test
4075 * @field: audit rule flags given from user-space
4076 * @op: required testing operator
4077 * @vrule: smack internal rule presentation
4078 * @actx: audit context associated with the check
4079 *
4080 * The core Audit hook. It's used to take the decision of
4081 * whether to audit or not to audit a given object.
4082 */
4083static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4084 struct audit_context *actx)
4085{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004086 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004087 char *rule = vrule;
4088
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004089 if (unlikely(!rule)) {
4090 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004091 return -ENOENT;
4092 }
4093
4094 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4095 return 0;
4096
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004097 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004098
4099 /*
4100 * No need to do string comparisons. If a match occurs,
4101 * both pointers will point to the same smack_known
4102 * label.
4103 */
Al Viro5af75d82008-12-16 05:59:26 -05004104 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004105 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004106 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004107 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004108
4109 return 0;
4110}
4111
4112/**
4113 * smack_audit_rule_free - free smack rule representation
4114 * @vrule: rule to be freed.
4115 *
4116 * No memory was allocated.
4117 */
4118static void smack_audit_rule_free(void *vrule)
4119{
4120 /* No-op */
4121}
4122
4123#endif /* CONFIG_AUDIT */
4124
Randy Dunlap251a2a92009-02-18 11:42:33 -08004125/**
David Quigley746df9b2013-05-22 12:50:35 -04004126 * smack_ismaclabel - check if xattr @name references a smack MAC label
4127 * @name: Full xattr name to check.
4128 */
4129static int smack_ismaclabel(const char *name)
4130{
4131 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4132}
4133
4134
4135/**
Casey Schauflere114e472008-02-04 22:29:50 -08004136 * smack_secid_to_secctx - return the smack label for a secid
4137 * @secid: incoming integer
4138 * @secdata: destination
4139 * @seclen: how long it is
4140 *
4141 * Exists for networking code.
4142 */
4143static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4144{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004145 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004146
Eric Parisd5630b92010-10-13 16:24:48 -04004147 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004148 *secdata = skp->smk_known;
4149 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004150 return 0;
4151}
4152
Randy Dunlap251a2a92009-02-18 11:42:33 -08004153/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004154 * smack_secctx_to_secid - return the secid for a smack label
4155 * @secdata: smack label
4156 * @seclen: how long result is
4157 * @secid: outgoing integer
4158 *
4159 * Exists for audit and networking code.
4160 */
David Howellse52c17642008-04-29 20:52:51 +01004161static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004162{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004163 struct smack_known *skp = smk_find_entry(secdata);
4164
4165 if (skp)
4166 *secid = skp->smk_secid;
4167 else
4168 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004169 return 0;
4170}
4171
Randy Dunlap251a2a92009-02-18 11:42:33 -08004172/**
Casey Schauflere114e472008-02-04 22:29:50 -08004173 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004174 * @secdata: unused
4175 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004176 *
4177 * Exists to make sure nothing gets done, and properly
4178 */
4179static void smack_release_secctx(char *secdata, u32 seclen)
4180{
4181}
4182
David P. Quigley1ee65e32009-09-03 14:25:57 -04004183static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4184{
4185 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4186}
4187
4188static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4189{
4190 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4191}
4192
4193static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4194{
4195 int len = 0;
4196 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4197
4198 if (len < 0)
4199 return len;
4200 *ctxlen = len;
4201 return 0;
4202}
4203
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02004204struct security_operations smack_ops = {
4205 .name = "smack",
4206
Ingo Molnar9e488582009-05-07 19:26:19 +10004207 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01004208 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08004209 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08004210
4211 .sb_alloc_security = smack_sb_alloc_security,
4212 .sb_free_security = smack_sb_free_security,
4213 .sb_copy_data = smack_sb_copy_data,
4214 .sb_kern_mount = smack_sb_kern_mount,
4215 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08004216
Casey Schaufler676dac42010-12-02 06:43:39 -08004217 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03004218 .bprm_committing_creds = smack_bprm_committing_creds,
4219 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08004220
Casey Schauflere114e472008-02-04 22:29:50 -08004221 .inode_alloc_security = smack_inode_alloc_security,
4222 .inode_free_security = smack_inode_free_security,
4223 .inode_init_security = smack_inode_init_security,
4224 .inode_link = smack_inode_link,
4225 .inode_unlink = smack_inode_unlink,
4226 .inode_rmdir = smack_inode_rmdir,
4227 .inode_rename = smack_inode_rename,
4228 .inode_permission = smack_inode_permission,
4229 .inode_setattr = smack_inode_setattr,
4230 .inode_getattr = smack_inode_getattr,
4231 .inode_setxattr = smack_inode_setxattr,
4232 .inode_post_setxattr = smack_inode_post_setxattr,
4233 .inode_getxattr = smack_inode_getxattr,
4234 .inode_removexattr = smack_inode_removexattr,
4235 .inode_getsecurity = smack_inode_getsecurity,
4236 .inode_setsecurity = smack_inode_setsecurity,
4237 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004238 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004239
4240 .file_permission = smack_file_permission,
4241 .file_alloc_security = smack_file_alloc_security,
4242 .file_free_security = smack_file_free_security,
4243 .file_ioctl = smack_file_ioctl,
4244 .file_lock = smack_file_lock,
4245 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04004246 .mmap_file = smack_mmap_file,
4247 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08004248 .file_set_fowner = smack_file_set_fowner,
4249 .file_send_sigiotask = smack_file_send_sigiotask,
4250 .file_receive = smack_file_receive,
4251
Eric Paris83d49852012-04-04 13:45:40 -04004252 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07004253
David Howellsee18d642009-09-02 09:14:21 +01004254 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11004255 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11004256 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01004257 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11004258 .kernel_act_as = smack_kernel_act_as,
4259 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08004260 .task_setpgid = smack_task_setpgid,
4261 .task_getpgid = smack_task_getpgid,
4262 .task_getsid = smack_task_getsid,
4263 .task_getsecid = smack_task_getsecid,
4264 .task_setnice = smack_task_setnice,
4265 .task_setioprio = smack_task_setioprio,
4266 .task_getioprio = smack_task_getioprio,
4267 .task_setscheduler = smack_task_setscheduler,
4268 .task_getscheduler = smack_task_getscheduler,
4269 .task_movememory = smack_task_movememory,
4270 .task_kill = smack_task_kill,
4271 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08004272 .task_to_inode = smack_task_to_inode,
4273
4274 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004275 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004276
4277 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4278 .msg_msg_free_security = smack_msg_msg_free_security,
4279
4280 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4281 .msg_queue_free_security = smack_msg_queue_free_security,
4282 .msg_queue_associate = smack_msg_queue_associate,
4283 .msg_queue_msgctl = smack_msg_queue_msgctl,
4284 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4285 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4286
4287 .shm_alloc_security = smack_shm_alloc_security,
4288 .shm_free_security = smack_shm_free_security,
4289 .shm_associate = smack_shm_associate,
4290 .shm_shmctl = smack_shm_shmctl,
4291 .shm_shmat = smack_shm_shmat,
4292
4293 .sem_alloc_security = smack_sem_alloc_security,
4294 .sem_free_security = smack_sem_free_security,
4295 .sem_associate = smack_sem_associate,
4296 .sem_semctl = smack_sem_semctl,
4297 .sem_semop = smack_sem_semop,
4298
Casey Schauflere114e472008-02-04 22:29:50 -08004299 .d_instantiate = smack_d_instantiate,
4300
4301 .getprocattr = smack_getprocattr,
4302 .setprocattr = smack_setprocattr,
4303
4304 .unix_stream_connect = smack_unix_stream_connect,
4305 .unix_may_send = smack_unix_may_send,
4306
4307 .socket_post_create = smack_socket_post_create,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004308#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schauflerc6739442013-05-22 18:42:56 -07004309 .socket_bind = smack_socket_bind,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004310#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004311 .socket_connect = smack_socket_connect,
4312 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08004313 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4314 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4315 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4316 .sk_alloc_security = smack_sk_alloc_security,
4317 .sk_free_security = smack_sk_free_security,
4318 .sock_graft = smack_sock_graft,
4319 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04004320 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004321
Casey Schauflere114e472008-02-04 22:29:50 -08004322 /* key management security hooks */
4323#ifdef CONFIG_KEYS
4324 .key_alloc = smack_key_alloc,
4325 .key_free = smack_key_free,
4326 .key_permission = smack_key_permission,
4327#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004328
4329 /* Audit hooks */
4330#ifdef CONFIG_AUDIT
4331 .audit_rule_init = smack_audit_rule_init,
4332 .audit_rule_known = smack_audit_rule_known,
4333 .audit_rule_match = smack_audit_rule_match,
4334 .audit_rule_free = smack_audit_rule_free,
4335#endif /* CONFIG_AUDIT */
4336
David Quigley746df9b2013-05-22 12:50:35 -04004337 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08004338 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004339 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08004340 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04004341 .inode_notifysecctx = smack_inode_notifysecctx,
4342 .inode_setsecctx = smack_inode_setsecctx,
4343 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08004344};
4345
Etienne Basset7198e2e2009-03-24 20:53:24 +01004346
Casey Schaufler86812bb2012-04-17 18:55:46 -07004347static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004348{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004349 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004350 * Initialize rule list locks
4351 */
4352 mutex_init(&smack_known_huh.smk_rules_lock);
4353 mutex_init(&smack_known_hat.smk_rules_lock);
4354 mutex_init(&smack_known_floor.smk_rules_lock);
4355 mutex_init(&smack_known_star.smk_rules_lock);
4356 mutex_init(&smack_known_invalid.smk_rules_lock);
4357 mutex_init(&smack_known_web.smk_rules_lock);
4358 /*
4359 * Initialize rule lists
4360 */
4361 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4362 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4363 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4364 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4365 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4366 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4367 /*
4368 * Create the known labels list
4369 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004370 smk_insert_entry(&smack_known_huh);
4371 smk_insert_entry(&smack_known_hat);
4372 smk_insert_entry(&smack_known_star);
4373 smk_insert_entry(&smack_known_floor);
4374 smk_insert_entry(&smack_known_invalid);
4375 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004376}
4377
Casey Schauflere114e472008-02-04 22:29:50 -08004378/**
4379 * smack_init - initialize the smack system
4380 *
4381 * Returns 0
4382 */
4383static __init int smack_init(void)
4384{
David Howellsd84f4f92008-11-14 10:39:23 +11004385 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004386 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004387
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004388 if (!security_module_enable(&smack_ops))
4389 return 0;
4390
Casey Schaufler69f287a2014-12-12 17:08:40 -08004391 smack_enabled = 1;
4392
Rohit1a5b4722014-10-15 17:40:41 +05304393 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4394 if (!smack_inode_cache)
4395 return -ENOMEM;
4396
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004397 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4398 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304399 if (tsp == NULL) {
4400 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004401 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304402 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004403
Casey Schauflere114e472008-02-04 22:29:50 -08004404 printk(KERN_INFO "Smack: Initializing.\n");
4405
4406 /*
4407 * Set the security state for the initial task.
4408 */
David Howellsd84f4f92008-11-14 10:39:23 +11004409 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004410 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004411
Casey Schaufler86812bb2012-04-17 18:55:46 -07004412 /* initialize the smack_known_list */
4413 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004414
4415 /*
4416 * Register with LSM
4417 */
4418 if (register_security(&smack_ops))
4419 panic("smack: Unable to register with kernel.\n");
4420
4421 return 0;
4422}
4423
4424/*
4425 * Smack requires early initialization in order to label
4426 * all processes and objects when they are created.
4427 */
4428security_initcall(smack_init);