blob: f60ded3a8da17861d0c88f80498829b88db02b47 [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
David Howellsc69e8d92008-11-14 10:39:19 +110046#define task_security(task) (task_cred_xxx((task), security))
47
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020048#define TRANS_TRUE "TRUE"
49#define TRANS_TRUE_SIZE 4
50
Casey Schauflerc6739442013-05-22 18:42:56 -070051#define SMK_CONNECTING 0
52#define SMK_RECEIVING 1
53#define SMK_SENDING 2
54
Casey Schaufler69f287a2014-12-12 17:08:40 -080055#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -070056LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler69f287a2014-12-12 17:08:40 -080057#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Rohit1a5b4722014-10-15 17:40:41 +053058static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080059int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070060
Casey Schauflerd166c802014-08-27 14:51:27 -070061#ifdef CONFIG_SECURITY_SMACK_BRINGUP
62static void smk_bu_mode(int mode, char *s)
63{
64 int i = 0;
65
66 if (mode & MAY_READ)
67 s[i++] = 'r';
68 if (mode & MAY_WRITE)
69 s[i++] = 'w';
70 if (mode & MAY_EXEC)
71 s[i++] = 'x';
72 if (mode & MAY_APPEND)
73 s[i++] = 'a';
74 if (mode & MAY_TRANSMUTE)
75 s[i++] = 't';
76 if (mode & MAY_LOCK)
77 s[i++] = 'l';
78 if (i == 0)
79 s[i++] = '-';
80 s[i] = '\0';
81}
82#endif
83
84#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020085static int smk_bu_note(char *note, struct smack_known *sskp,
86 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -070087{
88 char acc[SMK_NUM_ACCESS_TYPE + 1];
89
90 if (rc <= 0)
91 return rc;
92
93 smk_bu_mode(mode, acc);
94 pr_info("Smack Bringup: (%s %s %s) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020095 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -070096 return 0;
97}
98#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020099#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700100#endif
101
102#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200103static int smk_bu_current(char *note, struct smack_known *oskp,
104 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700105{
106 struct task_smack *tsp = current_security();
107 char acc[SMK_NUM_ACCESS_TYPE + 1];
108
109 if (rc <= 0)
110 return rc;
111
112 smk_bu_mode(mode, acc);
113 pr_info("Smack Bringup: (%s %s %s) %s %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200114 tsp->smk_task->smk_known, oskp->smk_known,
115 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700116 return 0;
117}
118#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200119#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700120#endif
121
122#ifdef CONFIG_SECURITY_SMACK_BRINGUP
123static int smk_bu_task(struct task_struct *otp, int mode, int rc)
124{
125 struct task_smack *tsp = current_security();
126 struct task_smack *otsp = task_security(otp);
127 char acc[SMK_NUM_ACCESS_TYPE + 1];
128
129 if (rc <= 0)
130 return rc;
131
132 smk_bu_mode(mode, acc);
133 pr_info("Smack Bringup: (%s %s %s) %s to %s\n",
134 tsp->smk_task->smk_known, otsp->smk_task->smk_known, acc,
135 current->comm, otp->comm);
136 return 0;
137}
138#else
139#define smk_bu_task(otp, mode, RC) (RC)
140#endif
141
142#ifdef CONFIG_SECURITY_SMACK_BRINGUP
143static int smk_bu_inode(struct inode *inode, int mode, int rc)
144{
145 struct task_smack *tsp = current_security();
146 char acc[SMK_NUM_ACCESS_TYPE + 1];
147
148 if (rc <= 0)
149 return rc;
150
151 smk_bu_mode(mode, acc);
152 pr_info("Smack Bringup: (%s %s %s) inode=(%s %ld) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200153 tsp->smk_task->smk_known, smk_of_inode(inode)->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700154 inode->i_sb->s_id, inode->i_ino, current->comm);
155 return 0;
156}
157#else
158#define smk_bu_inode(inode, mode, RC) (RC)
159#endif
160
161#ifdef CONFIG_SECURITY_SMACK_BRINGUP
162static int smk_bu_file(struct file *file, int mode, int rc)
163{
164 struct task_smack *tsp = current_security();
165 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800166 struct inode *inode = file_inode(file);
Casey Schauflerd166c802014-08-27 14:51:27 -0700167 char acc[SMK_NUM_ACCESS_TYPE + 1];
168
169 if (rc <= 0)
170 return rc;
171
172 smk_bu_mode(mode, acc);
Al Viroa4555892014-10-21 20:11:25 -0400173 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800174 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400175 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700176 current->comm);
177 return 0;
178}
179#else
180#define smk_bu_file(file, mode, RC) (RC)
181#endif
182
183#ifdef CONFIG_SECURITY_SMACK_BRINGUP
184static int smk_bu_credfile(const struct cred *cred, struct file *file,
185 int mode, int rc)
186{
187 struct task_smack *tsp = cred->security;
188 struct smack_known *sskp = tsp->smk_task;
189 struct inode *inode = file->f_inode;
190 char acc[SMK_NUM_ACCESS_TYPE + 1];
191
192 if (rc <= 0)
193 return rc;
194
195 smk_bu_mode(mode, acc);
Al Viroa4555892014-10-21 20:11:25 -0400196 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200197 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400198 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700199 current->comm);
200 return 0;
201}
202#else
203#define smk_bu_credfile(cred, file, mode, RC) (RC)
204#endif
205
Casey Schauflere114e472008-02-04 22:29:50 -0800206/**
207 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100208 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800209 * @ip: a pointer to the inode
210 * @dp: a pointer to the dentry
211 *
212 * Returns a pointer to the master list entry for the Smack label
213 * or NULL if there was no label to fetch.
214 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700215static struct smack_known *smk_fetch(const char *name, struct inode *ip,
216 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800217{
218 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700219 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700220 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800221
222 if (ip->i_op->getxattr == NULL)
223 return NULL;
224
Casey Schauflerf7112e62012-05-06 15:22:02 -0700225 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
226 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800227 return NULL;
228
Casey Schauflerf7112e62012-05-06 15:22:02 -0700229 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
230 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700231 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700232
233 kfree(buffer);
234
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700235 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800236}
237
238/**
239 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200240 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800241 *
242 * Returns the new blob or NULL if there's no memory available
243 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200244struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800245{
246 struct inode_smack *isp;
247
Rohit1a5b4722014-10-15 17:40:41 +0530248 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800249 if (isp == NULL)
250 return NULL;
251
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200252 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800253 isp->smk_flags = 0;
254 mutex_init(&isp->smk_lock);
255
256 return isp;
257}
258
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800259/**
260 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100261 * @task: a pointer to the Smack label for the running task
262 * @forked: a pointer to the Smack label for the forked task
263 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800264 *
265 * Returns the new blob or NULL if there's no memory available
266 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700267static struct task_smack *new_task_smack(struct smack_known *task,
268 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800269{
270 struct task_smack *tsp;
271
272 tsp = kzalloc(sizeof(struct task_smack), gfp);
273 if (tsp == NULL)
274 return NULL;
275
276 tsp->smk_task = task;
277 tsp->smk_forked = forked;
278 INIT_LIST_HEAD(&tsp->smk_rules);
279 mutex_init(&tsp->smk_rules_lock);
280
281 return tsp;
282}
283
284/**
285 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100286 * @nhead: new rules header pointer
287 * @ohead: old rules header pointer
288 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800289 *
290 * Returns 0 on success, -ENOMEM on error
291 */
292static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
293 gfp_t gfp)
294{
295 struct smack_rule *nrp;
296 struct smack_rule *orp;
297 int rc = 0;
298
299 INIT_LIST_HEAD(nhead);
300
301 list_for_each_entry_rcu(orp, ohead, list) {
302 nrp = kzalloc(sizeof(struct smack_rule), gfp);
303 if (nrp == NULL) {
304 rc = -ENOMEM;
305 break;
306 }
307 *nrp = *orp;
308 list_add_rcu(&nrp->list, nhead);
309 }
310 return rc;
311}
312
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100313/**
314 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
315 * @mode - input mode in form of PTRACE_MODE_*
316 *
317 * Returns a converted MAY_* mode usable by smack rules
318 */
319static inline unsigned int smk_ptrace_mode(unsigned int mode)
320{
321 switch (mode) {
322 case PTRACE_MODE_READ:
323 return MAY_READ;
324 case PTRACE_MODE_ATTACH:
325 return MAY_READWRITE;
326 }
327
328 return 0;
329}
330
331/**
332 * smk_ptrace_rule_check - helper for ptrace access
333 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200334 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100335 * @mode: ptrace attachment mode (PTRACE_MODE_*)
336 * @func: name of the function that called us, used for audit
337 *
338 * Returns 0 on access granted, -error on error
339 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200340static int smk_ptrace_rule_check(struct task_struct *tracer,
341 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100342 unsigned int mode, const char *func)
343{
344 int rc;
345 struct smk_audit_info ad, *saip = NULL;
346 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200347 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100348
349 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
350 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
351 smk_ad_setfield_u_tsk(&ad, tracer);
352 saip = &ad;
353 }
354
355 tsp = task_security(tracer);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200356 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100357
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100358 if ((mode & PTRACE_MODE_ATTACH) &&
359 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
360 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200361 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100362 rc = 0;
363 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
364 rc = -EACCES;
365 else if (capable(CAP_SYS_PTRACE))
366 rc = 0;
367 else
368 rc = -EACCES;
369
370 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200371 smack_log(tracer_known->smk_known,
372 tracee_known->smk_known,
373 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100374
375 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);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100380 return rc;
381}
382
Casey Schauflere114e472008-02-04 22:29:50 -0800383/*
384 * LSM hooks.
385 * We he, that is fun!
386 */
387
388/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000389 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800390 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100391 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800392 *
393 * Returns 0 if access is OK, an error code otherwise
394 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100395 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800396 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000397static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800398{
399 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700400 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800401
Ingo Molnar9e488582009-05-07 19:26:19 +1000402 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800403 if (rc != 0)
404 return rc;
405
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700406 skp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200407
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200408 rc = smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100409 return rc;
410}
Casey Schauflere114e472008-02-04 22:29:50 -0800411
David Howells5cd9c582008-08-14 11:37:28 +0100412/**
413 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
414 * @ptp: parent task pointer
415 *
416 * Returns 0 if access is OK, an error code otherwise
417 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100418 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100419 */
420static int smack_ptrace_traceme(struct task_struct *ptp)
421{
422 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700423 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100424
425 rc = cap_ptrace_traceme(ptp);
426 if (rc != 0)
427 return rc;
428
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100429 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200430
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200431 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800432 return rc;
433}
434
435/**
436 * smack_syslog - Smack approval on syslog
437 * @type: message type
438 *
Casey Schauflere114e472008-02-04 22:29:50 -0800439 * Returns 0 on success, error code otherwise.
440 */
Eric Paris12b30522010-11-15 18:36:29 -0500441static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800442{
Eric Paris12b30522010-11-15 18:36:29 -0500443 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700444 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800445
Casey Schaufler1880eff2012-06-05 15:28:30 -0700446 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800447 return 0;
448
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800449 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800450 rc = -EACCES;
451
452 return rc;
453}
454
455
456/*
457 * Superblock Hooks.
458 */
459
460/**
461 * smack_sb_alloc_security - allocate a superblock blob
462 * @sb: the superblock getting the blob
463 *
464 * Returns 0 on success or -ENOMEM on error.
465 */
466static int smack_sb_alloc_security(struct super_block *sb)
467{
468 struct superblock_smack *sbsp;
469
470 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
471
472 if (sbsp == NULL)
473 return -ENOMEM;
474
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200475 sbsp->smk_root = &smack_known_floor;
476 sbsp->smk_default = &smack_known_floor;
477 sbsp->smk_floor = &smack_known_floor;
478 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700479 /*
480 * smk_initialized will be zero from kzalloc.
481 */
Casey Schauflere114e472008-02-04 22:29:50 -0800482 sb->s_security = sbsp;
483
484 return 0;
485}
486
487/**
488 * smack_sb_free_security - free a superblock blob
489 * @sb: the superblock getting the blob
490 *
491 */
492static void smack_sb_free_security(struct super_block *sb)
493{
494 kfree(sb->s_security);
495 sb->s_security = NULL;
496}
497
498/**
499 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800500 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800501 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800502 *
503 * Returns 0 on success or -ENOMEM on error.
504 *
505 * Copy the Smack specific mount options out of the mount
506 * options list.
507 */
Eric Parise0007522008-03-05 10:31:54 -0500508static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800509{
510 char *cp, *commap, *otheropts, *dp;
511
Casey Schauflere114e472008-02-04 22:29:50 -0800512 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
513 if (otheropts == NULL)
514 return -ENOMEM;
515
516 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
517 if (strstr(cp, SMK_FSDEFAULT) == cp)
518 dp = smackopts;
519 else if (strstr(cp, SMK_FSFLOOR) == cp)
520 dp = smackopts;
521 else if (strstr(cp, SMK_FSHAT) == cp)
522 dp = smackopts;
523 else if (strstr(cp, SMK_FSROOT) == cp)
524 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700525 else if (strstr(cp, SMK_FSTRANS) == cp)
526 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800527 else
528 dp = otheropts;
529
530 commap = strchr(cp, ',');
531 if (commap != NULL)
532 *commap = '\0';
533
534 if (*dp != '\0')
535 strcat(dp, ",");
536 strcat(dp, cp);
537 }
538
539 strcpy(orig, otheropts);
540 free_page((unsigned long)otheropts);
541
542 return 0;
543}
544
545/**
546 * smack_sb_kern_mount - Smack specific mount processing
547 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100548 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800549 * @data: the smack mount options
550 *
551 * Returns 0 on success, an error code on failure
552 */
James Morris12204e22008-12-19 10:44:42 +1100553static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800554{
555 struct dentry *root = sb->s_root;
556 struct inode *inode = root->d_inode;
557 struct superblock_smack *sp = sb->s_security;
558 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800559 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800560 char *op;
561 char *commap;
Casey Schauflere830b392013-05-22 18:43:07 -0700562 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800563 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800564
Casey Schauflere830b392013-05-22 18:43:07 -0700565 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800566 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700567
Casey Schauflere114e472008-02-04 22:29:50 -0800568 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800569
570 for (op = data; op != NULL; op = commap) {
571 commap = strchr(op, ',');
572 if (commap != NULL)
573 *commap++ = '\0';
574
575 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
576 op += strlen(SMK_FSHAT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200577 skp = smk_import_entry(op, 0);
578 if (skp != NULL) {
579 sp->smk_hat = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800580 specified = 1;
581 }
Casey Schauflere114e472008-02-04 22:29:50 -0800582 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
583 op += strlen(SMK_FSFLOOR);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200584 skp = smk_import_entry(op, 0);
585 if (skp != NULL) {
586 sp->smk_floor = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800587 specified = 1;
588 }
Casey Schauflere114e472008-02-04 22:29:50 -0800589 } else if (strncmp(op, SMK_FSDEFAULT,
590 strlen(SMK_FSDEFAULT)) == 0) {
591 op += strlen(SMK_FSDEFAULT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200592 skp = smk_import_entry(op, 0);
593 if (skp != NULL) {
594 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800595 specified = 1;
596 }
Casey Schauflere114e472008-02-04 22:29:50 -0800597 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
598 op += strlen(SMK_FSROOT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200599 skp = smk_import_entry(op, 0);
600 if (skp != NULL) {
601 sp->smk_root = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800602 specified = 1;
603 }
Casey Schauflere830b392013-05-22 18:43:07 -0700604 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
605 op += strlen(SMK_FSTRANS);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200606 skp = smk_import_entry(op, 0);
607 if (skp != NULL) {
608 sp->smk_root = skp;
Casey Schauflere830b392013-05-22 18:43:07 -0700609 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800610 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700611 }
Casey Schauflere114e472008-02-04 22:29:50 -0800612 }
613 }
614
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800615 if (!smack_privileged(CAP_MAC_ADMIN)) {
616 /*
617 * Unprivileged mounts don't get to specify Smack values.
618 */
619 if (specified)
620 return -EPERM;
621 /*
622 * Unprivileged mounts get root and default from the caller.
623 */
624 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200625 sp->smk_root = skp;
626 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800627 }
Casey Schauflere114e472008-02-04 22:29:50 -0800628 /*
629 * Initialize the root inode.
630 */
631 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100632 if (isp == NULL) {
633 isp = new_inode_smack(sp->smk_root);
634 if (isp == NULL)
635 return -ENOMEM;
636 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700637 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800638 isp->smk_inode = sp->smk_root;
639
Casey Schauflere830b392013-05-22 18:43:07 -0700640 if (transmute)
641 isp->smk_flags |= SMK_INODE_TRANSMUTE;
642
Casey Schauflere114e472008-02-04 22:29:50 -0800643 return 0;
644}
645
646/**
647 * smack_sb_statfs - Smack check on statfs
648 * @dentry: identifies the file system in question
649 *
650 * Returns 0 if current can read the floor of the filesystem,
651 * and error code otherwise
652 */
653static int smack_sb_statfs(struct dentry *dentry)
654{
655 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200656 int rc;
657 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800658
Eric Parisa2694342011-04-25 13:10:27 -0400659 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200660 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
661
662 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700663 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200664 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800665}
666
Casey Schauflere114e472008-02-04 22:29:50 -0800667/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800668 * BPRM hooks
669 */
670
Casey Schauflerce8a4322011-09-29 18:21:01 -0700671/**
672 * smack_bprm_set_creds - set creds for exec
673 * @bprm: the exec information
674 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100675 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700676 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800677static int smack_bprm_set_creds(struct linux_binprm *bprm)
678{
Al Viro496ad9a2013-01-23 17:07:38 -0500679 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300680 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800681 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800682 int rc;
683
684 rc = cap_bprm_set_creds(bprm);
685 if (rc != 0)
686 return rc;
687
688 if (bprm->cred_prepared)
689 return 0;
690
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300691 isp = inode->i_security;
692 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800693 return 0;
694
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100695 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
696 struct task_struct *tracer;
697 rc = 0;
698
699 rcu_read_lock();
700 tracer = ptrace_parent(current);
701 if (likely(tracer != NULL))
702 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200703 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100704 PTRACE_MODE_ATTACH,
705 __func__);
706 rcu_read_unlock();
707
708 if (rc != 0)
709 return rc;
710 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300711 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800712
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300713 bsp->smk_task = isp->smk_task;
714 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800715
716 return 0;
717}
718
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300719/**
720 * smack_bprm_committing_creds - Prepare to install the new credentials
721 * from bprm.
722 *
723 * @bprm: binprm for exec
724 */
725static void smack_bprm_committing_creds(struct linux_binprm *bprm)
726{
727 struct task_smack *bsp = bprm->cred->security;
728
729 if (bsp->smk_task != bsp->smk_forked)
730 current->pdeath_signal = 0;
731}
732
733/**
734 * smack_bprm_secureexec - Return the decision to use secureexec.
735 * @bprm: binprm for exec
736 *
737 * Returns 0 on success.
738 */
739static int smack_bprm_secureexec(struct linux_binprm *bprm)
740{
741 struct task_smack *tsp = current_security();
742 int ret = cap_bprm_secureexec(bprm);
743
744 if (!ret && (tsp->smk_task != tsp->smk_forked))
745 ret = 1;
746
747 return ret;
748}
749
Casey Schaufler676dac42010-12-02 06:43:39 -0800750/*
Casey Schauflere114e472008-02-04 22:29:50 -0800751 * Inode hooks
752 */
753
754/**
755 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800756 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800757 *
758 * Returns 0 if it gets a blob, -ENOMEM otherwise
759 */
760static int smack_inode_alloc_security(struct inode *inode)
761{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700762 struct smack_known *skp = smk_of_current();
763
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200764 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800765 if (inode->i_security == NULL)
766 return -ENOMEM;
767 return 0;
768}
769
770/**
771 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800772 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800773 *
774 * Clears the blob pointer in inode
775 */
776static void smack_inode_free_security(struct inode *inode)
777{
Rohit1a5b4722014-10-15 17:40:41 +0530778 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800779 inode->i_security = NULL;
780}
781
782/**
783 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200784 * @inode: the newly created inode
785 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500786 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800787 * @name: where to put the attribute name
788 * @value: where to put the attribute value
789 * @len: where to put the length of the attribute
790 *
791 * Returns 0 if it all works out, -ENOMEM if there's no memory
792 */
793static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900794 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500795 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800796{
Casey Schaufler2267b132012-03-13 19:14:19 -0700797 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700798 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200799 struct smack_known *isp = smk_of_inode(inode);
800 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800801 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800802
Tetsuo Handa95489062013-07-25 05:44:02 +0900803 if (name)
804 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800805
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100806 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800807 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200808 may = smk_access_entry(skp->smk_known, dsp->smk_known,
809 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800810 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200811
812 /*
813 * If the access rule allows transmutation and
814 * the directory requests transmutation then
815 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700816 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200817 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800818 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700819 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200820 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700821 issp->smk_flags |= SMK_INODE_CHANGED;
822 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200823
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200824 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800825 if (*value == NULL)
826 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800827
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200828 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100829 }
Casey Schauflere114e472008-02-04 22:29:50 -0800830
831 return 0;
832}
833
834/**
835 * smack_inode_link - Smack check on link
836 * @old_dentry: the existing object
837 * @dir: unused
838 * @new_dentry: the new object
839 *
840 * Returns 0 if access is permitted, an error code otherwise
841 */
842static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
843 struct dentry *new_dentry)
844{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200845 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200846 struct smk_audit_info ad;
847 int rc;
848
Eric Parisa2694342011-04-25 13:10:27 -0400849 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200850 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800851
852 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200853 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700854 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800855
856 if (rc == 0 && new_dentry->d_inode != NULL) {
857 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200858 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
859 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700860 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800861 }
862
863 return rc;
864}
865
866/**
867 * smack_inode_unlink - Smack check on inode deletion
868 * @dir: containing directory object
869 * @dentry: file to unlink
870 *
871 * Returns 0 if current can write the containing directory
872 * and the object, error code otherwise
873 */
874static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
875{
876 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200877 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800878 int rc;
879
Eric Parisa2694342011-04-25 13:10:27 -0400880 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200881 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
882
Casey Schauflere114e472008-02-04 22:29:50 -0800883 /*
884 * You need write access to the thing you're unlinking
885 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200886 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700887 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200888 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800889 /*
890 * You also need write access to the containing directory
891 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400892 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200893 smk_ad_setfield_u_fs_inode(&ad, dir);
894 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700895 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200896 }
Casey Schauflere114e472008-02-04 22:29:50 -0800897 return rc;
898}
899
900/**
901 * smack_inode_rmdir - Smack check on directory deletion
902 * @dir: containing directory object
903 * @dentry: directory to unlink
904 *
905 * Returns 0 if current can write the containing directory
906 * and the directory, error code otherwise
907 */
908static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
909{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200910 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800911 int rc;
912
Eric Parisa2694342011-04-25 13:10:27 -0400913 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200914 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
915
Casey Schauflere114e472008-02-04 22:29:50 -0800916 /*
917 * You need write access to the thing you're removing
918 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200919 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700920 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200921 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800922 /*
923 * You also need write access to the containing directory
924 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400925 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200926 smk_ad_setfield_u_fs_inode(&ad, dir);
927 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700928 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200929 }
Casey Schauflere114e472008-02-04 22:29:50 -0800930
931 return rc;
932}
933
934/**
935 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200936 * @old_inode: unused
937 * @old_dentry: the old object
938 * @new_inode: unused
939 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -0800940 *
941 * Read and write access is required on both the old and
942 * new directories.
943 *
944 * Returns 0 if access is permitted, an error code otherwise
945 */
946static int smack_inode_rename(struct inode *old_inode,
947 struct dentry *old_dentry,
948 struct inode *new_inode,
949 struct dentry *new_dentry)
950{
951 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200952 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200953 struct smk_audit_info ad;
954
Eric Parisa2694342011-04-25 13:10:27 -0400955 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200956 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800957
958 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200959 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700960 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800961
962 if (rc == 0 && new_dentry->d_inode != NULL) {
963 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200964 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
965 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700966 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800967 }
Casey Schauflere114e472008-02-04 22:29:50 -0800968 return rc;
969}
970
971/**
972 * smack_inode_permission - Smack version of permission()
973 * @inode: the inode in question
974 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800975 *
976 * This is the important Smack hook.
977 *
978 * Returns 0 if access is permitted, -EACCES otherwise
979 */
Al Viroe74f71e2011-06-20 19:38:15 -0400980static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800981{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200982 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400983 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -0700984 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -0400985
986 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800987 /*
988 * No permission to check. Existence test. Yup, it's there.
989 */
990 if (mask == 0)
991 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700992
993 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400994 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700995 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400996 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200997 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700998 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
999 rc = smk_bu_inode(inode, mask, rc);
1000 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001001}
1002
1003/**
1004 * smack_inode_setattr - Smack check for setting attributes
1005 * @dentry: the object
1006 * @iattr: for the force flag
1007 *
1008 * Returns 0 if access is permitted, an error code otherwise
1009 */
1010static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1011{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001012 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001013 int rc;
1014
Casey Schauflere114e472008-02-04 22:29:50 -08001015 /*
1016 * Need to allow for clearing the setuid bit.
1017 */
1018 if (iattr->ia_valid & ATTR_FORCE)
1019 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001020 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001021 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001022
Casey Schauflerd166c802014-08-27 14:51:27 -07001023 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1024 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1025 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001026}
1027
1028/**
1029 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001030 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001031 * @dentry: the object
1032 *
1033 * Returns 0 if access is permitted, an error code otherwise
1034 */
1035static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1036{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001037 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -04001038 struct path path;
Casey Schauflerd166c802014-08-27 14:51:27 -07001039 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001040
Eric Parisa2694342011-04-25 13:10:27 -04001041 path.dentry = dentry;
1042 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001043
Eric Parisf48b7392011-04-25 12:54:27 -04001044 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -04001045 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001046 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1047 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1048 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001049}
1050
1051/**
1052 * smack_inode_setxattr - Smack check for setting xattrs
1053 * @dentry: the object
1054 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001055 * @value: value of the attribute
1056 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001057 * @flags: unused
1058 *
1059 * This protects the Smack attribute explicitly.
1060 *
1061 * Returns 0 if access is permitted, an error code otherwise
1062 */
David Howells8f0cfa52008-04-29 00:59:41 -07001063static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1064 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001065{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001066 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001067 struct smack_known *skp;
1068 int check_priv = 0;
1069 int check_import = 0;
1070 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001071 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001072
Casey Schaufler19760ad2013-12-16 16:27:26 -08001073 /*
1074 * Check label validity here so import won't fail in post_setxattr
1075 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001076 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1077 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001078 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1079 check_priv = 1;
1080 check_import = 1;
1081 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1082 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1083 check_priv = 1;
1084 check_import = 1;
1085 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001086 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001087 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001088 if (size != TRANS_TRUE_SIZE ||
1089 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1090 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001091 } else
1092 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1093
Casey Schaufler19760ad2013-12-16 16:27:26 -08001094 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1095 rc = -EPERM;
1096
1097 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001098 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001099 if (skp == NULL || (check_star &&
1100 (skp == &smack_known_star || skp == &smack_known_web)))
1101 rc = -EINVAL;
1102 }
1103
Eric Parisa2694342011-04-25 13:10:27 -04001104 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001105 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1106
Casey Schauflerd166c802014-08-27 14:51:27 -07001107 if (rc == 0) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001108 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001109 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1110 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001111
1112 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001113}
1114
1115/**
1116 * smack_inode_post_setxattr - Apply the Smack update approved above
1117 * @dentry: object
1118 * @name: attribute name
1119 * @value: attribute value
1120 * @size: attribute size
1121 * @flags: unused
1122 *
1123 * Set the pointer in the inode blob to the entry found
1124 * in the master label list.
1125 */
David Howells8f0cfa52008-04-29 00:59:41 -07001126static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1127 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001128{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001129 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001130 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001131
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001132 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1133 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1134 return;
1135 }
1136
Casey Schaufler676dac42010-12-02 06:43:39 -08001137 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001138 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001139 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001140 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001141 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001142 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001143 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001144 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001145 if (skp != NULL)
1146 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001147 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001148 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001149 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001150 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001151 if (skp != NULL)
1152 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001153 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001154 isp->smk_mmap = &smack_known_invalid;
1155 }
Casey Schauflere114e472008-02-04 22:29:50 -08001156
1157 return;
1158}
1159
Casey Schauflerce8a4322011-09-29 18:21:01 -07001160/**
Casey Schauflere114e472008-02-04 22:29:50 -08001161 * smack_inode_getxattr - Smack check on getxattr
1162 * @dentry: the object
1163 * @name: unused
1164 *
1165 * Returns 0 if access is permitted, an error code otherwise
1166 */
David Howells8f0cfa52008-04-29 00:59:41 -07001167static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001168{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001169 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001170 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171
Eric Parisa2694342011-04-25 13:10:27 -04001172 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001173 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1174
Casey Schauflerd166c802014-08-27 14:51:27 -07001175 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1176 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1177 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001178}
1179
Casey Schauflerce8a4322011-09-29 18:21:01 -07001180/**
Casey Schauflere114e472008-02-04 22:29:50 -08001181 * smack_inode_removexattr - Smack check on removexattr
1182 * @dentry: the object
1183 * @name: name of the attribute
1184 *
1185 * Removing the Smack attribute requires CAP_MAC_ADMIN
1186 *
1187 * Returns 0 if access is permitted, an error code otherwise
1188 */
David Howells8f0cfa52008-04-29 00:59:41 -07001189static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001190{
Casey Schaufler676dac42010-12-02 06:43:39 -08001191 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001192 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001193 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001194
Casey Schauflerbcdca222008-02-23 15:24:04 -08001195 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1196 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001197 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001198 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001199 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301200 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001201 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001202 rc = -EPERM;
1203 } else
1204 rc = cap_inode_removexattr(dentry, name);
1205
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001206 if (rc != 0)
1207 return rc;
1208
Eric Parisa2694342011-04-25 13:10:27 -04001209 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001210 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001211
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001212 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001213 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001214 if (rc != 0)
1215 return rc;
1216
1217 isp = dentry->d_inode->i_security;
1218 /*
1219 * Don't do anything special for these.
1220 * XATTR_NAME_SMACKIPIN
1221 * XATTR_NAME_SMACKIPOUT
1222 * XATTR_NAME_SMACKEXEC
1223 */
1224 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001225 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001226 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001227 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001228 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1229 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001230
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001231 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001232}
1233
1234/**
1235 * smack_inode_getsecurity - get smack xattrs
1236 * @inode: the object
1237 * @name: attribute name
1238 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001239 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001240 *
1241 * Returns the size of the attribute or an error code
1242 */
1243static int smack_inode_getsecurity(const struct inode *inode,
1244 const char *name, void **buffer,
1245 bool alloc)
1246{
1247 struct socket_smack *ssp;
1248 struct socket *sock;
1249 struct super_block *sbp;
1250 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001251 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001252 int ilen;
1253 int rc = 0;
1254
1255 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1256 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001257 ilen = strlen(isp->smk_known);
1258 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001259 return ilen;
1260 }
1261
1262 /*
1263 * The rest of the Smack xattrs are only on sockets.
1264 */
1265 sbp = ip->i_sb;
1266 if (sbp->s_magic != SOCKFS_MAGIC)
1267 return -EOPNOTSUPP;
1268
1269 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001270 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001271 return -EOPNOTSUPP;
1272
1273 ssp = sock->sk->sk_security;
1274
1275 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001276 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001277 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001278 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001279 else
1280 return -EOPNOTSUPP;
1281
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001282 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001283 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001284 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001285 rc = ilen;
1286 }
1287
1288 return rc;
1289}
1290
1291
1292/**
1293 * smack_inode_listsecurity - list the Smack attributes
1294 * @inode: the object
1295 * @buffer: where they go
1296 * @buffer_size: size of buffer
1297 *
1298 * Returns 0 on success, -EINVAL otherwise
1299 */
1300static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1301 size_t buffer_size)
1302{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001303 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001304
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001305 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001306 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001307
1308 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001309}
1310
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001311/**
1312 * smack_inode_getsecid - Extract inode's security id
1313 * @inode: inode to extract the info from
1314 * @secid: where result will be saved
1315 */
1316static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1317{
1318 struct inode_smack *isp = inode->i_security;
1319
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001320 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001321}
1322
Casey Schauflere114e472008-02-04 22:29:50 -08001323/*
1324 * File Hooks
1325 */
1326
1327/**
1328 * smack_file_permission - Smack check on file operations
1329 * @file: unused
1330 * @mask: unused
1331 *
1332 * Returns 0
1333 *
1334 * Should access checks be done on each read or write?
1335 * UNICOS and SELinux say yes.
1336 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1337 *
1338 * I'll say no for now. Smack does not do the frequent
1339 * label changing that SELinux does.
1340 */
1341static int smack_file_permission(struct file *file, int mask)
1342{
1343 return 0;
1344}
1345
1346/**
1347 * smack_file_alloc_security - assign a file security blob
1348 * @file: the object
1349 *
1350 * The security blob for a file is a pointer to the master
1351 * label list, so no allocation is done.
1352 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001353 * f_security is the owner security information. It
1354 * isn't used on file access checks, it's for send_sigio.
1355 *
Casey Schauflere114e472008-02-04 22:29:50 -08001356 * Returns 0
1357 */
1358static int smack_file_alloc_security(struct file *file)
1359{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001360 struct smack_known *skp = smk_of_current();
1361
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001362 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001363 return 0;
1364}
1365
1366/**
1367 * smack_file_free_security - clear a file security blob
1368 * @file: the object
1369 *
1370 * The security blob for a file is a pointer to the master
1371 * label list, so no memory is freed.
1372 */
1373static void smack_file_free_security(struct file *file)
1374{
1375 file->f_security = NULL;
1376}
1377
1378/**
1379 * smack_file_ioctl - Smack check on ioctls
1380 * @file: the object
1381 * @cmd: what to do
1382 * @arg: unused
1383 *
1384 * Relies heavily on the correct use of the ioctl command conventions.
1385 *
1386 * Returns 0 if allowed, error code otherwise
1387 */
1388static int smack_file_ioctl(struct file *file, unsigned int cmd,
1389 unsigned long arg)
1390{
1391 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001392 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001393 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001394
Eric Parisf48b7392011-04-25 12:54:27 -04001395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001396 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001397
Casey Schauflerd166c802014-08-27 14:51:27 -07001398 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001399 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001400 rc = smk_bu_file(file, MAY_WRITE, rc);
1401 }
Casey Schauflere114e472008-02-04 22:29:50 -08001402
Casey Schauflerd166c802014-08-27 14:51:27 -07001403 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001404 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001405 rc = smk_bu_file(file, MAY_READ, rc);
1406 }
Casey Schauflere114e472008-02-04 22:29:50 -08001407
1408 return rc;
1409}
1410
1411/**
1412 * smack_file_lock - Smack check on file locking
1413 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001414 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001415 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001416 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001417 */
1418static int smack_file_lock(struct file *file, unsigned int cmd)
1419{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001420 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001421 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001422 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001423
Eric Paris92f42502011-04-25 13:15:55 -04001424 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1425 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001426 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001427 rc = smk_bu_file(file, MAY_LOCK, rc);
1428 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001429}
1430
1431/**
1432 * smack_file_fcntl - Smack check on fcntl
1433 * @file: the object
1434 * @cmd: what action to check
1435 * @arg: unused
1436 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001437 * Generally these operations are harmless.
1438 * File locking operations present an obvious mechanism
1439 * for passing information, so they require write access.
1440 *
Casey Schauflere114e472008-02-04 22:29:50 -08001441 * Returns 0 if current has access, error code otherwise
1442 */
1443static int smack_file_fcntl(struct file *file, unsigned int cmd,
1444 unsigned long arg)
1445{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001446 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001447 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001448 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001449
Casey Schauflere114e472008-02-04 22:29:50 -08001450 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001451 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001452 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001453 case F_SETLK:
1454 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001455 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1456 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001457 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001458 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001459 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001460 case F_SETOWN:
1461 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001462 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1463 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001464 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001465 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001466 break;
1467 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001468 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001469 }
1470
1471 return rc;
1472}
1473
1474/**
Al Viroe5467852012-05-30 13:30:51 -04001475 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001476 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1477 * if mapping anonymous memory.
1478 * @file contains the file structure for file to map (may be NULL).
1479 * @reqprot contains the protection requested by the application.
1480 * @prot contains the protection that will be applied by the kernel.
1481 * @flags contains the operational flags.
1482 * Return 0 if permission is granted.
1483 */
Al Viroe5467852012-05-30 13:30:51 -04001484static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001485 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001486 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001487{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001488 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001489 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001490 struct smack_rule *srp;
1491 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001492 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001493 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001494 int may;
1495 int mmay;
1496 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001497 int rc;
1498
Al Viro496ad9a2013-01-23 17:07:38 -05001499 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001500 return 0;
1501
Al Viro496ad9a2013-01-23 17:07:38 -05001502 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001503 if (isp->smk_mmap == NULL)
1504 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001505 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001506
1507 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001508 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001509 rc = 0;
1510
1511 rcu_read_lock();
1512 /*
1513 * For each Smack rule associated with the subject
1514 * label verify that the SMACK64MMAP also has access
1515 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001516 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001517 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001518 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001519 /*
1520 * Matching labels always allows access.
1521 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001522 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001523 continue;
1524 /*
1525 * If there is a matching local rule take
1526 * that into account as well.
1527 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001528 may = smk_access_entry(srp->smk_subject->smk_known,
1529 okp->smk_known,
1530 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001531 if (may == -ENOENT)
1532 may = srp->smk_access;
1533 else
1534 may &= srp->smk_access;
1535 /*
1536 * If may is zero the SMACK64MMAP subject can't
1537 * possibly have less access.
1538 */
1539 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001540 continue;
1541
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001542 /*
1543 * Fetch the global list entry.
1544 * If there isn't one a SMACK64MMAP subject
1545 * can't have as much access as current.
1546 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001547 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1548 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001549 if (mmay == -ENOENT) {
1550 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001551 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001552 }
1553 /*
1554 * If there is a local entry it modifies the
1555 * potential access, too.
1556 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001557 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1558 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001559 if (tmay != -ENOENT)
1560 mmay &= tmay;
1561
1562 /*
1563 * If there is any access available to current that is
1564 * not available to a SMACK64MMAP subject
1565 * deny access.
1566 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001567 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001568 rc = -EACCES;
1569 break;
1570 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001571 }
1572
1573 rcu_read_unlock();
1574
1575 return rc;
1576}
1577
1578/**
Casey Schauflere114e472008-02-04 22:29:50 -08001579 * smack_file_set_fowner - set the file security blob value
1580 * @file: object in question
1581 *
Casey Schauflere114e472008-02-04 22:29:50 -08001582 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001583static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001584{
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001585 file->f_security = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001586}
1587
1588/**
1589 * smack_file_send_sigiotask - Smack on sigio
1590 * @tsk: The target task
1591 * @fown: the object the signal come from
1592 * @signum: unused
1593 *
1594 * Allow a privileged task to get signals even if it shouldn't
1595 *
1596 * Returns 0 if a subject with the object's smack could
1597 * write to the task, an error code otherwise.
1598 */
1599static int smack_file_send_sigiotask(struct task_struct *tsk,
1600 struct fown_struct *fown, int signum)
1601{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001602 struct smack_known *skp;
1603 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001604 struct file *file;
1605 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001606 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001607
1608 /*
1609 * struct fown_struct is never outside the context of a struct file
1610 */
1611 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001612
Etienne Bassetecfcc532009-04-08 20:40:06 +02001613 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001614 skp = file->f_security;
1615 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1616 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001617 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001618 rc = 0;
1619
1620 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1621 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001622 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001623 return rc;
1624}
1625
1626/**
1627 * smack_file_receive - Smack file receive check
1628 * @file: the object
1629 *
1630 * Returns 0 if current has access, error code otherwise
1631 */
1632static int smack_file_receive(struct file *file)
1633{
Casey Schauflerd166c802014-08-27 14:51:27 -07001634 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001635 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001636 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001637 struct inode *inode = file_inode(file);
Casey Schauflere114e472008-02-04 22:29:50 -08001638
Casey Schaufler4482a442013-12-30 17:37:45 -08001639 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001640 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001641 /*
1642 * This code relies on bitmasks.
1643 */
1644 if (file->f_mode & FMODE_READ)
1645 may = MAY_READ;
1646 if (file->f_mode & FMODE_WRITE)
1647 may |= MAY_WRITE;
1648
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001649 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001650 rc = smk_bu_file(file, may, rc);
1651 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001652}
1653
Casey Schaufler531f1d42011-09-19 12:41:42 -07001654/**
Eric Paris83d49852012-04-04 13:45:40 -04001655 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001656 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001657 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001658 *
1659 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001660 * Allow the open only if the task has read access. There are
1661 * many read operations (e.g. fstat) that you can do with an
1662 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001663 *
1664 * Returns 0
1665 */
Eric Paris83d49852012-04-04 13:45:40 -04001666static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001667{
Casey Schauflera6834c02014-04-21 11:10:26 -07001668 struct task_smack *tsp = cred->security;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001669 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001670 struct smk_audit_info ad;
1671 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001672
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001673 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflera6834c02014-04-21 11:10:26 -07001674 return 0;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001675
Casey Schauflera6834c02014-04-21 11:10:26 -07001676 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1677 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001678 rc = smk_access(tsp->smk_task, smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001679 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001680
1681 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001682}
1683
Casey Schauflere114e472008-02-04 22:29:50 -08001684/*
1685 * Task hooks
1686 */
1687
1688/**
David Howellsee18d642009-09-02 09:14:21 +01001689 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1690 * @new: the new credentials
1691 * @gfp: the atomicity of any memory allocations
1692 *
1693 * Prepare a blank set of credentials for modification. This must allocate all
1694 * the memory the LSM module might require such that cred_transfer() can
1695 * complete without error.
1696 */
1697static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1698{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001699 struct task_smack *tsp;
1700
1701 tsp = new_task_smack(NULL, NULL, gfp);
1702 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001703 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001704
1705 cred->security = tsp;
1706
David Howellsee18d642009-09-02 09:14:21 +01001707 return 0;
1708}
1709
1710
1711/**
David Howellsf1752ee2008-11-14 10:39:17 +11001712 * smack_cred_free - "free" task-level security credentials
1713 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001714 *
Casey Schauflere114e472008-02-04 22:29:50 -08001715 */
David Howellsf1752ee2008-11-14 10:39:17 +11001716static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001717{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001718 struct task_smack *tsp = cred->security;
1719 struct smack_rule *rp;
1720 struct list_head *l;
1721 struct list_head *n;
1722
1723 if (tsp == NULL)
1724 return;
1725 cred->security = NULL;
1726
1727 list_for_each_safe(l, n, &tsp->smk_rules) {
1728 rp = list_entry(l, struct smack_rule, list);
1729 list_del(&rp->list);
1730 kfree(rp);
1731 }
1732 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001733}
1734
1735/**
David Howellsd84f4f92008-11-14 10:39:23 +11001736 * smack_cred_prepare - prepare new set of credentials for modification
1737 * @new: the new credentials
1738 * @old: the original credentials
1739 * @gfp: the atomicity of any memory allocations
1740 *
1741 * Prepare a new set of credentials for modification.
1742 */
1743static int smack_cred_prepare(struct cred *new, const struct cred *old,
1744 gfp_t gfp)
1745{
Casey Schaufler676dac42010-12-02 06:43:39 -08001746 struct task_smack *old_tsp = old->security;
1747 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001748 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001749
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001750 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001751 if (new_tsp == NULL)
1752 return -ENOMEM;
1753
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001754 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1755 if (rc != 0)
1756 return rc;
1757
Casey Schaufler676dac42010-12-02 06:43:39 -08001758 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001759 return 0;
1760}
1761
Randy Dunlap251a2a92009-02-18 11:42:33 -08001762/**
David Howellsee18d642009-09-02 09:14:21 +01001763 * smack_cred_transfer - Transfer the old credentials to the new credentials
1764 * @new: the new credentials
1765 * @old: the original credentials
1766 *
1767 * Fill in a set of blank credentials from another set of credentials.
1768 */
1769static void smack_cred_transfer(struct cred *new, const struct cred *old)
1770{
Casey Schaufler676dac42010-12-02 06:43:39 -08001771 struct task_smack *old_tsp = old->security;
1772 struct task_smack *new_tsp = new->security;
1773
1774 new_tsp->smk_task = old_tsp->smk_task;
1775 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001776 mutex_init(&new_tsp->smk_rules_lock);
1777 INIT_LIST_HEAD(&new_tsp->smk_rules);
1778
1779
1780 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001781}
1782
1783/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001784 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001785 * @new: points to the set of credentials to be modified.
1786 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001787 *
1788 * Set the security data for a kernel service.
1789 */
1790static int smack_kernel_act_as(struct cred *new, u32 secid)
1791{
Casey Schaufler676dac42010-12-02 06:43:39 -08001792 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001793 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001794
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001795 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001796 return -EINVAL;
1797
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001798 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001799 return 0;
1800}
1801
1802/**
1803 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001804 * @new: points to the set of credentials to be modified
1805 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001806 *
1807 * Set the file creation context in a set of credentials to the same
1808 * as the objective context of the specified inode
1809 */
1810static int smack_kernel_create_files_as(struct cred *new,
1811 struct inode *inode)
1812{
1813 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001814 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001815
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001816 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001817 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001818 return 0;
1819}
1820
1821/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001822 * smk_curacc_on_task - helper to log task related access
1823 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001824 * @access: the access requested
1825 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001826 *
1827 * Return 0 if access is permitted
1828 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001829static int smk_curacc_on_task(struct task_struct *p, int access,
1830 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001831{
1832 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001833 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflerd166c802014-08-27 14:51:27 -07001834 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001835
Casey Schaufler531f1d42011-09-19 12:41:42 -07001836 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001837 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001838 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001839 rc = smk_bu_task(p, access, rc);
1840 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001841}
1842
1843/**
Casey Schauflere114e472008-02-04 22:29:50 -08001844 * smack_task_setpgid - Smack check on setting pgid
1845 * @p: the task object
1846 * @pgid: unused
1847 *
1848 * Return 0 if write access is permitted
1849 */
1850static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1851{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001852 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001853}
1854
1855/**
1856 * smack_task_getpgid - Smack access check for getpgid
1857 * @p: the object task
1858 *
1859 * Returns 0 if current can read the object task, error code otherwise
1860 */
1861static int smack_task_getpgid(struct task_struct *p)
1862{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001863 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001864}
1865
1866/**
1867 * smack_task_getsid - Smack access check for getsid
1868 * @p: the object task
1869 *
1870 * Returns 0 if current can read the object task, error code otherwise
1871 */
1872static int smack_task_getsid(struct task_struct *p)
1873{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001874 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001875}
1876
1877/**
1878 * smack_task_getsecid - get the secid of the task
1879 * @p: the object task
1880 * @secid: where to put the result
1881 *
1882 * Sets the secid to contain a u32 version of the smack label.
1883 */
1884static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1885{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001886 struct smack_known *skp = smk_of_task(task_security(p));
1887
1888 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001889}
1890
1891/**
1892 * smack_task_setnice - Smack check on setting nice
1893 * @p: the task object
1894 * @nice: unused
1895 *
1896 * Return 0 if write access is permitted
1897 */
1898static int smack_task_setnice(struct task_struct *p, int nice)
1899{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001900 int rc;
1901
1902 rc = cap_task_setnice(p, nice);
1903 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001904 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001905 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001906}
1907
1908/**
1909 * smack_task_setioprio - Smack check on setting ioprio
1910 * @p: the task object
1911 * @ioprio: unused
1912 *
1913 * Return 0 if write access is permitted
1914 */
1915static int smack_task_setioprio(struct task_struct *p, int ioprio)
1916{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001917 int rc;
1918
1919 rc = cap_task_setioprio(p, ioprio);
1920 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001921 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001922 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001923}
1924
1925/**
1926 * smack_task_getioprio - Smack check on reading ioprio
1927 * @p: the task object
1928 *
1929 * Return 0 if read access is permitted
1930 */
1931static int smack_task_getioprio(struct task_struct *p)
1932{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001933 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001934}
1935
1936/**
1937 * smack_task_setscheduler - Smack check on setting scheduler
1938 * @p: the task object
1939 * @policy: unused
1940 * @lp: unused
1941 *
1942 * Return 0 if read access is permitted
1943 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001944static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001945{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001946 int rc;
1947
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001948 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001949 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001950 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001951 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001952}
1953
1954/**
1955 * smack_task_getscheduler - Smack check on reading scheduler
1956 * @p: the task object
1957 *
1958 * Return 0 if read access is permitted
1959 */
1960static int smack_task_getscheduler(struct task_struct *p)
1961{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001962 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001963}
1964
1965/**
1966 * smack_task_movememory - Smack check on moving memory
1967 * @p: the task object
1968 *
1969 * Return 0 if write access is permitted
1970 */
1971static int smack_task_movememory(struct task_struct *p)
1972{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001973 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001974}
1975
1976/**
1977 * smack_task_kill - Smack check on signal delivery
1978 * @p: the task object
1979 * @info: unused
1980 * @sig: unused
1981 * @secid: identifies the smack to use in lieu of current's
1982 *
1983 * Return 0 if write access is permitted
1984 *
1985 * The secid behavior is an artifact of an SELinux hack
1986 * in the USB code. Someday it may go away.
1987 */
1988static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1989 int sig, u32 secid)
1990{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001991 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001992 struct smack_known *skp;
1993 struct smack_known *tkp = smk_of_task(task_security(p));
Casey Schauflerd166c802014-08-27 14:51:27 -07001994 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001995
1996 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1997 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001998 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001999 * Sending a signal requires that the sender
2000 * can write the receiver.
2001 */
Casey Schauflerd166c802014-08-27 14:51:27 -07002002 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002003 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002004 rc = smk_bu_task(p, MAY_WRITE, rc);
2005 return rc;
2006 }
Casey Schauflere114e472008-02-04 22:29:50 -08002007 /*
2008 * If the secid isn't 0 we're dealing with some USB IO
2009 * specific behavior. This is not clean. For one thing
2010 * we can't take privilege into account.
2011 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002012 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002013 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2014 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002015 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002016}
2017
2018/**
2019 * smack_task_wait - Smack access check for waiting
2020 * @p: task to wait for
2021 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002022 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002023 */
2024static int smack_task_wait(struct task_struct *p)
2025{
Casey Schauflere114e472008-02-04 22:29:50 -08002026 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002027 * Allow the operation to succeed.
2028 * Zombies are bad.
2029 * In userless environments (e.g. phones) programs
2030 * get marked with SMACK64EXEC and even if the parent
2031 * and child shouldn't be talking the parent still
2032 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002033 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002034 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002035}
2036
2037/**
2038 * smack_task_to_inode - copy task smack into the inode blob
2039 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002040 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002041 *
2042 * Sets the smack pointer in the inode security blob
2043 */
2044static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2045{
2046 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002047 struct smack_known *skp = smk_of_task(task_security(p));
2048
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002049 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002050}
2051
2052/*
2053 * Socket hooks.
2054 */
2055
2056/**
2057 * smack_sk_alloc_security - Allocate a socket blob
2058 * @sk: the socket
2059 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002060 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002061 *
2062 * Assign Smack pointers to current
2063 *
2064 * Returns 0 on success, -ENOMEM is there's no memory
2065 */
2066static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2067{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002068 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002069 struct socket_smack *ssp;
2070
2071 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2072 if (ssp == NULL)
2073 return -ENOMEM;
2074
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002075 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002076 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002077 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002078
2079 sk->sk_security = ssp;
2080
2081 return 0;
2082}
2083
2084/**
2085 * smack_sk_free_security - Free a socket blob
2086 * @sk: the socket
2087 *
2088 * Clears the blob pointer
2089 */
2090static void smack_sk_free_security(struct sock *sk)
2091{
2092 kfree(sk->sk_security);
2093}
2094
2095/**
Paul Moore07feee82009-03-27 17:10:54 -04002096* smack_host_label - check host based restrictions
2097* @sip: the object end
2098*
2099* looks for host based access restrictions
2100*
2101* This version will only be appropriate for really small sets of single label
2102* hosts. The caller is responsible for ensuring that the RCU read lock is
2103* taken before calling this function.
2104*
2105* Returns the label of the far end or NULL if it's not special.
2106*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002107static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002108{
2109 struct smk_netlbladdr *snp;
2110 struct in_addr *siap = &sip->sin_addr;
2111
2112 if (siap->s_addr == 0)
2113 return NULL;
2114
2115 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2116 /*
2117 * we break after finding the first match because
2118 * the list is sorted from longest to shortest mask
2119 * so we have found the most specific match
2120 */
2121 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002122 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2123 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002124 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002125 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002126 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002127 }
Paul Moore07feee82009-03-27 17:10:54 -04002128
2129 return NULL;
2130}
2131
2132/**
Casey Schauflere114e472008-02-04 22:29:50 -08002133 * smack_netlabel - Set the secattr on a socket
2134 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002135 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002136 *
2137 * Convert the outbound smack value (smk_out) to a
2138 * secattr and attach it to the socket.
2139 *
2140 * Returns 0 on success or an error code
2141 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002142static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002143{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002144 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002145 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002146 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002147
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002148 /*
2149 * Usually the netlabel code will handle changing the
2150 * packet labeling based on the label.
2151 * The case of a single label host is different, because
2152 * a single label host should never get a labeled packet
2153 * even though the label is usually associated with a packet
2154 * label.
2155 */
2156 local_bh_disable();
2157 bh_lock_sock_nested(sk);
2158
2159 if (ssp->smk_out == smack_net_ambient ||
2160 labeled == SMACK_UNLABELED_SOCKET)
2161 netlbl_sock_delattr(sk);
2162 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002163 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002164 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002165 }
2166
2167 bh_unlock_sock(sk);
2168 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002169
Casey Schauflere114e472008-02-04 22:29:50 -08002170 return rc;
2171}
2172
2173/**
Paul Moore07feee82009-03-27 17:10:54 -04002174 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2175 * @sk: the socket
2176 * @sap: the destination address
2177 *
2178 * Set the correct secattr for the given socket based on the destination
2179 * address and perform any outbound access checks needed.
2180 *
2181 * Returns 0 on success or an error code.
2182 *
2183 */
2184static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2185{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002186 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002187 int rc;
2188 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002189 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002190 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002191 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002192
2193 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002194 hkp = smack_host_label(sap);
2195 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002196#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002197 struct lsm_network_audit net;
2198
Eric Paris48c62af2012-04-02 13:15:44 -04002199 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2200 ad.a.u.net->family = sap->sin_family;
2201 ad.a.u.net->dport = sap->sin_port;
2202 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002203#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002204 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002205 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002206 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2207 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002208 } else {
2209 sk_lbl = SMACK_CIPSO_SOCKET;
2210 rc = 0;
2211 }
2212 rcu_read_unlock();
2213 if (rc != 0)
2214 return rc;
2215
2216 return smack_netlabel(sk, sk_lbl);
2217}
2218
Casey Schaufler69f287a2014-12-12 17:08:40 -08002219#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Paul Moore07feee82009-03-27 17:10:54 -04002220/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002221 * smk_ipv6_port_label - Smack port access table management
2222 * @sock: socket
2223 * @address: address
2224 *
2225 * Create or update the port list entry
2226 */
2227static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2228{
2229 struct sock *sk = sock->sk;
2230 struct sockaddr_in6 *addr6;
2231 struct socket_smack *ssp = sock->sk->sk_security;
2232 struct smk_port_label *spp;
2233 unsigned short port = 0;
2234
2235 if (address == NULL) {
2236 /*
2237 * This operation is changing the Smack information
2238 * on the bound socket. Take the changes to the port
2239 * as well.
2240 */
2241 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2242 if (sk != spp->smk_sock)
2243 continue;
2244 spp->smk_in = ssp->smk_in;
2245 spp->smk_out = ssp->smk_out;
2246 return;
2247 }
2248 /*
2249 * A NULL address is only used for updating existing
2250 * bound entries. If there isn't one, it's OK.
2251 */
2252 return;
2253 }
2254
2255 addr6 = (struct sockaddr_in6 *)address;
2256 port = ntohs(addr6->sin6_port);
2257 /*
2258 * This is a special case that is safely ignored.
2259 */
2260 if (port == 0)
2261 return;
2262
2263 /*
2264 * Look for an existing port list entry.
2265 * This is an indication that a port is getting reused.
2266 */
2267 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2268 if (spp->smk_port != port)
2269 continue;
2270 spp->smk_port = port;
2271 spp->smk_sock = sk;
2272 spp->smk_in = ssp->smk_in;
2273 spp->smk_out = ssp->smk_out;
2274 return;
2275 }
2276
2277 /*
2278 * A new port entry is required.
2279 */
2280 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2281 if (spp == NULL)
2282 return;
2283
2284 spp->smk_port = port;
2285 spp->smk_sock = sk;
2286 spp->smk_in = ssp->smk_in;
2287 spp->smk_out = ssp->smk_out;
2288
2289 list_add(&spp->list, &smk_ipv6_port_list);
2290 return;
2291}
2292
2293/**
2294 * smk_ipv6_port_check - check Smack port access
2295 * @sock: socket
2296 * @address: address
2297 *
2298 * Create or update the port list entry
2299 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002300static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002301 int act)
2302{
2303 __be16 *bep;
2304 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002305 struct smk_port_label *spp;
2306 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002307 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002308 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002309 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002310 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002311 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002312#ifdef CONFIG_AUDIT
2313 struct lsm_network_audit net;
2314#endif
2315
2316 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002317 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002318 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002319 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002320 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002321 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002322 }
2323
2324 /*
2325 * Get the IP address and port from the address.
2326 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002327 port = ntohs(address->sin6_port);
2328 bep = (__be16 *)(&address->sin6_addr);
2329 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002330
2331 /*
2332 * It's remote, so port lookup does no good.
2333 */
2334 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2335 goto auditout;
2336
2337 /*
2338 * It's local so the send check has to have passed.
2339 */
2340 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002341 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002342 goto auditout;
2343 }
2344
2345 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2346 if (spp->smk_port != port)
2347 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002348 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002349 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002350 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002351 break;
2352 }
2353
2354auditout:
2355
2356#ifdef CONFIG_AUDIT
2357 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2358 ad.a.u.net->family = sk->sk_family;
2359 ad.a.u.net->dport = port;
2360 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002361 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002362 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002363 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002364#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002365 rc = smk_access(skp, object, MAY_WRITE, &ad);
2366 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2367 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002368}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002369#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002370
2371/**
Casey Schauflere114e472008-02-04 22:29:50 -08002372 * smack_inode_setsecurity - set smack xattrs
2373 * @inode: the object
2374 * @name: attribute name
2375 * @value: attribute value
2376 * @size: size of the attribute
2377 * @flags: unused
2378 *
2379 * Sets the named attribute in the appropriate blob
2380 *
2381 * Returns 0 on success, or an error code
2382 */
2383static int smack_inode_setsecurity(struct inode *inode, const char *name,
2384 const void *value, size_t size, int flags)
2385{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002386 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002387 struct inode_smack *nsp = inode->i_security;
2388 struct socket_smack *ssp;
2389 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002390 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002391
Casey Schauflerf7112e62012-05-06 15:22:02 -07002392 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302393 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002394
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002395 skp = smk_import_entry(value, size);
2396 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002397 return -EINVAL;
2398
2399 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002400 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002401 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002402 return 0;
2403 }
2404 /*
2405 * The rest of the Smack xattrs are only on sockets.
2406 */
2407 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2408 return -EOPNOTSUPP;
2409
2410 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002411 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002412 return -EOPNOTSUPP;
2413
2414 ssp = sock->sk->sk_security;
2415
2416 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002417 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002418 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002419 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002420 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002421 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2422 if (rc != 0)
2423 printk(KERN_WARNING
2424 "Smack: \"%s\" netlbl error %d.\n",
2425 __func__, -rc);
2426 }
Casey Schauflere114e472008-02-04 22:29:50 -08002427 } else
2428 return -EOPNOTSUPP;
2429
Casey Schaufler69f287a2014-12-12 17:08:40 -08002430#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07002431 if (sock->sk->sk_family == PF_INET6)
2432 smk_ipv6_port_label(sock, NULL);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002433#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002434
Casey Schauflere114e472008-02-04 22:29:50 -08002435 return 0;
2436}
2437
2438/**
2439 * smack_socket_post_create - finish socket setup
2440 * @sock: the socket
2441 * @family: protocol family
2442 * @type: unused
2443 * @protocol: unused
2444 * @kern: unused
2445 *
2446 * Sets the netlabel information on the socket
2447 *
2448 * Returns 0 on success, and error code otherwise
2449 */
2450static int smack_socket_post_create(struct socket *sock, int family,
2451 int type, int protocol, int kern)
2452{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002453 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002454 return 0;
2455 /*
2456 * Set the outbound netlbl.
2457 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002458 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2459}
2460
Casey Schaufler69f287a2014-12-12 17:08:40 -08002461#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002462/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002463 * smack_socket_bind - record port binding information.
2464 * @sock: the socket
2465 * @address: the port address
2466 * @addrlen: size of the address
2467 *
2468 * Records the label bound to a port.
2469 *
2470 * Returns 0
2471 */
2472static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2473 int addrlen)
2474{
Casey Schaufler69f287a2014-12-12 17:08:40 -08002475#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002476 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2477 smk_ipv6_port_label(sock, address);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002478#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002479
2480 return 0;
2481}
Casey Schaufler69f287a2014-12-12 17:08:40 -08002482#endif /* !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002483
2484/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002485 * smack_socket_connect - connect access check
2486 * @sock: the socket
2487 * @sap: the other end
2488 * @addrlen: size of sap
2489 *
2490 * Verifies that a connection may be possible
2491 *
2492 * Returns 0 on success, and error code otherwise
2493 */
2494static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2495 int addrlen)
2496{
Casey Schauflerc6739442013-05-22 18:42:56 -07002497 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002498
Casey Schauflerc6739442013-05-22 18:42:56 -07002499 if (sock->sk == NULL)
2500 return 0;
2501
2502 switch (sock->sk->sk_family) {
2503 case PF_INET:
2504 if (addrlen < sizeof(struct sockaddr_in))
2505 return -EINVAL;
2506 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2507 break;
2508 case PF_INET6:
2509 if (addrlen < sizeof(struct sockaddr_in6))
2510 return -EINVAL;
Casey Schaufler69f287a2014-12-12 17:08:40 -08002511#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002512 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2513 SMK_CONNECTING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08002514#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07002515 break;
2516 }
2517 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002518}
2519
2520/**
2521 * smack_flags_to_may - convert S_ to MAY_ values
2522 * @flags: the S_ value
2523 *
2524 * Returns the equivalent MAY_ value
2525 */
2526static int smack_flags_to_may(int flags)
2527{
2528 int may = 0;
2529
2530 if (flags & S_IRUGO)
2531 may |= MAY_READ;
2532 if (flags & S_IWUGO)
2533 may |= MAY_WRITE;
2534 if (flags & S_IXUGO)
2535 may |= MAY_EXEC;
2536
2537 return may;
2538}
2539
2540/**
2541 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2542 * @msg: the object
2543 *
2544 * Returns 0
2545 */
2546static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2547{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002548 struct smack_known *skp = smk_of_current();
2549
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002550 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002551 return 0;
2552}
2553
2554/**
2555 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2556 * @msg: the object
2557 *
2558 * Clears the blob pointer
2559 */
2560static void smack_msg_msg_free_security(struct msg_msg *msg)
2561{
2562 msg->security = NULL;
2563}
2564
2565/**
2566 * smack_of_shm - the smack pointer for the shm
2567 * @shp: the object
2568 *
2569 * Returns a pointer to the smack value
2570 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002571static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002572{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002573 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002574}
2575
2576/**
2577 * smack_shm_alloc_security - Set the security blob for shm
2578 * @shp: the object
2579 *
2580 * Returns 0
2581 */
2582static int smack_shm_alloc_security(struct shmid_kernel *shp)
2583{
2584 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002585 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002586
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002587 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002588 return 0;
2589}
2590
2591/**
2592 * smack_shm_free_security - Clear the security blob for shm
2593 * @shp: the object
2594 *
2595 * Clears the blob pointer
2596 */
2597static void smack_shm_free_security(struct shmid_kernel *shp)
2598{
2599 struct kern_ipc_perm *isp = &shp->shm_perm;
2600
2601 isp->security = NULL;
2602}
2603
2604/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002605 * smk_curacc_shm : check if current has access on shm
2606 * @shp : the object
2607 * @access : access requested
2608 *
2609 * Returns 0 if current has the requested access, error code otherwise
2610 */
2611static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2612{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002613 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002614 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002615 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002616
2617#ifdef CONFIG_AUDIT
2618 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2619 ad.a.u.ipc_id = shp->shm_perm.id;
2620#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002621 rc = smk_curacc(ssp, access, &ad);
2622 rc = smk_bu_current("shm", ssp, access, rc);
2623 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002624}
2625
2626/**
Casey Schauflere114e472008-02-04 22:29:50 -08002627 * smack_shm_associate - Smack access check for shm
2628 * @shp: the object
2629 * @shmflg: access requested
2630 *
2631 * Returns 0 if current has the requested access, error code otherwise
2632 */
2633static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2634{
Casey Schauflere114e472008-02-04 22:29:50 -08002635 int may;
2636
2637 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002638 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002639}
2640
2641/**
2642 * smack_shm_shmctl - Smack access check for shm
2643 * @shp: the object
2644 * @cmd: what it wants to do
2645 *
2646 * Returns 0 if current has the requested access, error code otherwise
2647 */
2648static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2649{
Casey Schauflere114e472008-02-04 22:29:50 -08002650 int may;
2651
2652 switch (cmd) {
2653 case IPC_STAT:
2654 case SHM_STAT:
2655 may = MAY_READ;
2656 break;
2657 case IPC_SET:
2658 case SHM_LOCK:
2659 case SHM_UNLOCK:
2660 case IPC_RMID:
2661 may = MAY_READWRITE;
2662 break;
2663 case IPC_INFO:
2664 case SHM_INFO:
2665 /*
2666 * System level information.
2667 */
2668 return 0;
2669 default:
2670 return -EINVAL;
2671 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002672 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002673}
2674
2675/**
2676 * smack_shm_shmat - Smack access for shmat
2677 * @shp: the object
2678 * @shmaddr: unused
2679 * @shmflg: access requested
2680 *
2681 * Returns 0 if current has the requested access, error code otherwise
2682 */
2683static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2684 int shmflg)
2685{
Casey Schauflere114e472008-02-04 22:29:50 -08002686 int may;
2687
2688 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002689 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002690}
2691
2692/**
2693 * smack_of_sem - the smack pointer for the sem
2694 * @sma: the object
2695 *
2696 * Returns a pointer to the smack value
2697 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002698static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002699{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002700 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002701}
2702
2703/**
2704 * smack_sem_alloc_security - Set the security blob for sem
2705 * @sma: the object
2706 *
2707 * Returns 0
2708 */
2709static int smack_sem_alloc_security(struct sem_array *sma)
2710{
2711 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002712 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002713
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002714 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002715 return 0;
2716}
2717
2718/**
2719 * smack_sem_free_security - Clear the security blob for sem
2720 * @sma: the object
2721 *
2722 * Clears the blob pointer
2723 */
2724static void smack_sem_free_security(struct sem_array *sma)
2725{
2726 struct kern_ipc_perm *isp = &sma->sem_perm;
2727
2728 isp->security = NULL;
2729}
2730
2731/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002732 * smk_curacc_sem : check if current has access on sem
2733 * @sma : the object
2734 * @access : access requested
2735 *
2736 * Returns 0 if current has the requested access, error code otherwise
2737 */
2738static int smk_curacc_sem(struct sem_array *sma, int access)
2739{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002740 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002741 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002742 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002743
2744#ifdef CONFIG_AUDIT
2745 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2746 ad.a.u.ipc_id = sma->sem_perm.id;
2747#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002748 rc = smk_curacc(ssp, access, &ad);
2749 rc = smk_bu_current("sem", ssp, access, rc);
2750 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002751}
2752
2753/**
Casey Schauflere114e472008-02-04 22:29:50 -08002754 * smack_sem_associate - Smack access check for sem
2755 * @sma: the object
2756 * @semflg: access requested
2757 *
2758 * Returns 0 if current has the requested access, error code otherwise
2759 */
2760static int smack_sem_associate(struct sem_array *sma, int semflg)
2761{
Casey Schauflere114e472008-02-04 22:29:50 -08002762 int may;
2763
2764 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002765 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002766}
2767
2768/**
2769 * smack_sem_shmctl - Smack access check for sem
2770 * @sma: the object
2771 * @cmd: what it wants to do
2772 *
2773 * Returns 0 if current has the requested access, error code otherwise
2774 */
2775static int smack_sem_semctl(struct sem_array *sma, int cmd)
2776{
Casey Schauflere114e472008-02-04 22:29:50 -08002777 int may;
2778
2779 switch (cmd) {
2780 case GETPID:
2781 case GETNCNT:
2782 case GETZCNT:
2783 case GETVAL:
2784 case GETALL:
2785 case IPC_STAT:
2786 case SEM_STAT:
2787 may = MAY_READ;
2788 break;
2789 case SETVAL:
2790 case SETALL:
2791 case IPC_RMID:
2792 case IPC_SET:
2793 may = MAY_READWRITE;
2794 break;
2795 case IPC_INFO:
2796 case SEM_INFO:
2797 /*
2798 * System level information
2799 */
2800 return 0;
2801 default:
2802 return -EINVAL;
2803 }
2804
Etienne Bassetecfcc532009-04-08 20:40:06 +02002805 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002806}
2807
2808/**
2809 * smack_sem_semop - Smack checks of semaphore operations
2810 * @sma: the object
2811 * @sops: unused
2812 * @nsops: unused
2813 * @alter: unused
2814 *
2815 * Treated as read and write in all cases.
2816 *
2817 * Returns 0 if access is allowed, error code otherwise
2818 */
2819static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2820 unsigned nsops, int alter)
2821{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002822 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002823}
2824
2825/**
2826 * smack_msg_alloc_security - Set the security blob for msg
2827 * @msq: the object
2828 *
2829 * Returns 0
2830 */
2831static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2832{
2833 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002834 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002835
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002836 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002837 return 0;
2838}
2839
2840/**
2841 * smack_msg_free_security - Clear the security blob for msg
2842 * @msq: the object
2843 *
2844 * Clears the blob pointer
2845 */
2846static void smack_msg_queue_free_security(struct msg_queue *msq)
2847{
2848 struct kern_ipc_perm *kisp = &msq->q_perm;
2849
2850 kisp->security = NULL;
2851}
2852
2853/**
2854 * smack_of_msq - the smack pointer for the msq
2855 * @msq: the object
2856 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002857 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002858 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002859static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002860{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002861 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002862}
2863
2864/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002865 * smk_curacc_msq : helper to check if current has access on msq
2866 * @msq : the msq
2867 * @access : access requested
2868 *
2869 * return 0 if current has access, error otherwise
2870 */
2871static int smk_curacc_msq(struct msg_queue *msq, int access)
2872{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002873 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002874 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002875 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002876
2877#ifdef CONFIG_AUDIT
2878 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2879 ad.a.u.ipc_id = msq->q_perm.id;
2880#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002881 rc = smk_curacc(msp, access, &ad);
2882 rc = smk_bu_current("msq", msp, access, rc);
2883 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002884}
2885
2886/**
Casey Schauflere114e472008-02-04 22:29:50 -08002887 * smack_msg_queue_associate - Smack access check for msg_queue
2888 * @msq: the object
2889 * @msqflg: access requested
2890 *
2891 * Returns 0 if current has the requested access, error code otherwise
2892 */
2893static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2894{
Casey Schauflere114e472008-02-04 22:29:50 -08002895 int may;
2896
2897 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002898 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002899}
2900
2901/**
2902 * smack_msg_queue_msgctl - Smack access check for msg_queue
2903 * @msq: the object
2904 * @cmd: what it wants to do
2905 *
2906 * Returns 0 if current has the requested access, error code otherwise
2907 */
2908static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2909{
Casey Schauflere114e472008-02-04 22:29:50 -08002910 int may;
2911
2912 switch (cmd) {
2913 case IPC_STAT:
2914 case MSG_STAT:
2915 may = MAY_READ;
2916 break;
2917 case IPC_SET:
2918 case IPC_RMID:
2919 may = MAY_READWRITE;
2920 break;
2921 case IPC_INFO:
2922 case MSG_INFO:
2923 /*
2924 * System level information
2925 */
2926 return 0;
2927 default:
2928 return -EINVAL;
2929 }
2930
Etienne Bassetecfcc532009-04-08 20:40:06 +02002931 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002932}
2933
2934/**
2935 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2936 * @msq: the object
2937 * @msg: unused
2938 * @msqflg: access requested
2939 *
2940 * Returns 0 if current has the requested access, error code otherwise
2941 */
2942static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2943 int msqflg)
2944{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002945 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002946
Etienne Bassetecfcc532009-04-08 20:40:06 +02002947 may = smack_flags_to_may(msqflg);
2948 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002949}
2950
2951/**
2952 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2953 * @msq: the object
2954 * @msg: unused
2955 * @target: unused
2956 * @type: unused
2957 * @mode: unused
2958 *
2959 * Returns 0 if current has read and write access, error code otherwise
2960 */
2961static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2962 struct task_struct *target, long type, int mode)
2963{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002964 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002965}
2966
2967/**
2968 * smack_ipc_permission - Smack access for ipc_permission()
2969 * @ipp: the object permissions
2970 * @flag: access requested
2971 *
2972 * Returns 0 if current has read and write access, error code otherwise
2973 */
2974static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2975{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002976 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002977 int may = smack_flags_to_may(flag);
2978 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002979 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002980
Etienne Bassetecfcc532009-04-08 20:40:06 +02002981#ifdef CONFIG_AUDIT
2982 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2983 ad.a.u.ipc_id = ipp->id;
2984#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002985 rc = smk_curacc(iskp, may, &ad);
2986 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002987 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002988}
2989
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002990/**
2991 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002992 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002993 * @secid: where result will be saved
2994 */
2995static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2996{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002997 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002998
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002999 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003000}
3001
Casey Schauflere114e472008-02-04 22:29:50 -08003002/**
3003 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003004 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003005 * @inode: the object
3006 *
3007 * Set the inode's security blob if it hasn't been done already.
3008 */
3009static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3010{
3011 struct super_block *sbp;
3012 struct superblock_smack *sbsp;
3013 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003014 struct smack_known *skp;
3015 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003016 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003017 char trattr[TRANS_TRUE_SIZE];
3018 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003019 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003020 struct dentry *dp;
3021
3022 if (inode == NULL)
3023 return;
3024
3025 isp = inode->i_security;
3026
3027 mutex_lock(&isp->smk_lock);
3028 /*
3029 * If the inode is already instantiated
3030 * take the quick way out
3031 */
3032 if (isp->smk_flags & SMK_INODE_INSTANT)
3033 goto unlockandout;
3034
3035 sbp = inode->i_sb;
3036 sbsp = sbp->s_security;
3037 /*
3038 * We're going to use the superblock default label
3039 * if there's no label on the file.
3040 */
3041 final = sbsp->smk_default;
3042
3043 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003044 * If this is the root inode the superblock
3045 * may be in the process of initialization.
3046 * If that is the case use the root value out
3047 * of the superblock.
3048 */
3049 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003050 switch (sbp->s_magic) {
3051 case CGROUP_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003052 /*
3053 * The cgroup filesystem is never mounted,
3054 * so there's no opportunity to set the mount
3055 * options.
3056 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003057 sbsp->smk_root = &smack_known_star;
3058 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c23262014-12-16 16:53:08 +01003059 isp->smk_inode = sbsp->smk_root;
3060 break;
3061 case TMPFS_MAGIC:
3062 /*
3063 * What about shmem/tmpfs anonymous files with dentry
3064 * obtained from d_alloc_pseudo()?
3065 */
3066 isp->smk_inode = smk_of_current();
3067 break;
3068 default:
3069 isp->smk_inode = sbsp->smk_root;
3070 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003071 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003072 isp->smk_flags |= SMK_INODE_INSTANT;
3073 goto unlockandout;
3074 }
3075
3076 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003077 * This is pretty hackish.
3078 * Casey says that we shouldn't have to do
3079 * file system specific code, but it does help
3080 * with keeping it simple.
3081 */
3082 switch (sbp->s_magic) {
3083 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003084 case PIPEFS_MAGIC:
3085 case SOCKFS_MAGIC:
3086 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003087 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003088 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003089 * that the smack file system doesn't do
3090 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003091 *
Casey Schauflere114e472008-02-04 22:29:50 -08003092 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003093 *
3094 * Socket access is controlled by the socket
3095 * structures associated with the task involved.
3096 *
3097 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003098 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003099 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003100 break;
3101 case DEVPTS_SUPER_MAGIC:
3102 /*
3103 * devpts seems content with the label of the task.
3104 * Programs that change smack have to treat the
3105 * pty with respect.
3106 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003107 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003108 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003109 case PROC_SUPER_MAGIC:
3110 /*
3111 * Casey says procfs appears not to care.
3112 * The superblock default suffices.
3113 */
3114 break;
3115 case TMPFS_MAGIC:
3116 /*
3117 * Device labels should come from the filesystem,
3118 * but watch out, because they're volitile,
3119 * getting recreated on every reboot.
3120 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003121 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003122 /*
3123 * No break.
3124 *
3125 * If a smack value has been set we want to use it,
3126 * but since tmpfs isn't giving us the opportunity
3127 * to set mount options simulate setting the
3128 * superblock default.
3129 */
3130 default:
3131 /*
3132 * This isn't an understood special case.
3133 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003134 */
3135
3136 /*
3137 * UNIX domain sockets use lower level socket data.
3138 */
3139 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003140 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003141 break;
3142 }
3143 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003144 * No xattr support means, alas, no SMACK label.
3145 * Use the aforeapplied default.
3146 * It would be curious if the label of the task
3147 * does not match that assigned.
3148 */
3149 if (inode->i_op->getxattr == NULL)
3150 break;
3151 /*
3152 * Get the dentry for xattr.
3153 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003154 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003155 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3156 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003157 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003158
3159 /*
3160 * Transmuting directory
3161 */
3162 if (S_ISDIR(inode->i_mode)) {
3163 /*
3164 * If this is a new directory and the label was
3165 * transmuted when the inode was initialized
3166 * set the transmute attribute on the directory
3167 * and mark the inode.
3168 *
3169 * If there is a transmute attribute on the
3170 * directory mark the inode.
3171 */
3172 if (isp->smk_flags & SMK_INODE_CHANGED) {
3173 isp->smk_flags &= ~SMK_INODE_CHANGED;
3174 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003175 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003176 TRANS_TRUE, TRANS_TRUE_SIZE,
3177 0);
3178 } else {
3179 rc = inode->i_op->getxattr(dp,
3180 XATTR_NAME_SMACKTRANSMUTE, trattr,
3181 TRANS_TRUE_SIZE);
3182 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3183 TRANS_TRUE_SIZE) != 0)
3184 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003185 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003186 if (rc >= 0)
3187 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003188 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003189 /*
3190 * Don't let the exec or mmap label be "*" or "@".
3191 */
3192 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3193 if (skp == &smack_known_star || skp == &smack_known_web)
3194 skp = NULL;
3195 isp->smk_task = skp;
3196 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3197 if (skp == &smack_known_star || skp == &smack_known_web)
3198 skp = NULL;
3199 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003200
Casey Schauflere114e472008-02-04 22:29:50 -08003201 dput(dp);
3202 break;
3203 }
3204
3205 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003206 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003207 else
3208 isp->smk_inode = final;
3209
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003210 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003211
3212unlockandout:
3213 mutex_unlock(&isp->smk_lock);
3214 return;
3215}
3216
3217/**
3218 * smack_getprocattr - Smack process attribute access
3219 * @p: the object task
3220 * @name: the name of the attribute in /proc/.../attr
3221 * @value: where to put the result
3222 *
3223 * Places a copy of the task Smack into value
3224 *
3225 * Returns the length of the smack label or an error code
3226 */
3227static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3228{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003229 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08003230 char *cp;
3231 int slen;
3232
3233 if (strcmp(name, "current") != 0)
3234 return -EINVAL;
3235
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003236 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003237 if (cp == NULL)
3238 return -ENOMEM;
3239
3240 slen = strlen(cp);
3241 *value = cp;
3242 return slen;
3243}
3244
3245/**
3246 * smack_setprocattr - Smack process attribute setting
3247 * @p: the object task
3248 * @name: the name of the attribute in /proc/.../attr
3249 * @value: the value to set
3250 * @size: the size of the value
3251 *
3252 * Sets the Smack value of the task. Only setting self
3253 * is permitted and only with privilege
3254 *
3255 * Returns the length of the smack label or an error code
3256 */
3257static int smack_setprocattr(struct task_struct *p, char *name,
3258 void *value, size_t size)
3259{
Casey Schaufler676dac42010-12-02 06:43:39 -08003260 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003261 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003262 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003263
Casey Schauflere114e472008-02-04 22:29:50 -08003264 /*
3265 * Changing another process' Smack value is too dangerous
3266 * and supports no sane use case.
3267 */
3268 if (p != current)
3269 return -EPERM;
3270
Casey Schaufler1880eff2012-06-05 15:28:30 -07003271 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003272 return -EPERM;
3273
Casey Schauflerf7112e62012-05-06 15:22:02 -07003274 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003275 return -EINVAL;
3276
3277 if (strcmp(name, "current") != 0)
3278 return -EINVAL;
3279
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003280 skp = smk_import_entry(value, size);
3281 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003282 return -EINVAL;
3283
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003284 /*
3285 * No process is ever allowed the web ("@") label.
3286 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003287 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003288 return -EPERM;
3289
David Howellsd84f4f92008-11-14 10:39:23 +11003290 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003291 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003292 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003293
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003294 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003295 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003296
David Howellsd84f4f92008-11-14 10:39:23 +11003297 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003298 return size;
3299}
3300
3301/**
3302 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003303 * @sock: one sock
3304 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003305 * @newsk: unused
3306 *
3307 * Return 0 if a subject with the smack of sock could access
3308 * an object with the smack of other, otherwise an error code
3309 */
David S. Miller3610cda2011-01-05 15:38:53 -08003310static int smack_unix_stream_connect(struct sock *sock,
3311 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003312{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003313 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003314 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003315 struct socket_smack *ssp = sock->sk_security;
3316 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003317 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003318 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003319 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003320#ifdef CONFIG_AUDIT
3321 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003322#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003323
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003324 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3325 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003326 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003327#ifdef CONFIG_AUDIT
3328 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3329 smk_ad_setfield_u_net_sk(&ad, other);
3330#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003331 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3332 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003333 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003334 okp = osp->smk_out;
3335 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003336 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003337 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003338 MAY_WRITE, rc);
3339 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003340 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003341
Casey Schaufler975d5e52011-09-26 14:43:39 -07003342 /*
3343 * Cross reference the peer labels for SO_PEERSEC.
3344 */
3345 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003346 nsp->smk_packet = ssp->smk_out;
3347 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003348 }
3349
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003350 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003351}
3352
3353/**
3354 * smack_unix_may_send - Smack access on UDS
3355 * @sock: one socket
3356 * @other: the other socket
3357 *
3358 * Return 0 if a subject with the smack of sock could access
3359 * an object with the smack of other, otherwise an error code
3360 */
3361static int smack_unix_may_send(struct socket *sock, struct socket *other)
3362{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003363 struct socket_smack *ssp = sock->sk->sk_security;
3364 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003365 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003366 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003367
Kees Cook923e9a12012-04-10 13:26:44 -07003368#ifdef CONFIG_AUDIT
3369 struct lsm_network_audit net;
3370
Eric Paris48c62af2012-04-02 13:15:44 -04003371 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003372 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003373#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003374
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003375 if (smack_privileged(CAP_MAC_OVERRIDE))
3376 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003377
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003378 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3379 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003380 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003381}
3382
3383/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003384 * smack_socket_sendmsg - Smack check based on destination host
3385 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003386 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003387 * @size: the size of the message
3388 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003389 * Return 0 if the current subject can write to the destination host.
3390 * For IPv4 this is only a question if the destination is a single label host.
3391 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003392 */
3393static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3394 int size)
3395{
3396 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003397#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003398 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003399#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003400 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003401
3402 /*
3403 * Perfectly reasonable for this to be NULL
3404 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003405 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003406 return 0;
3407
Casey Schauflerc6739442013-05-22 18:42:56 -07003408 switch (sip->sin_family) {
3409 case AF_INET:
3410 rc = smack_netlabel_send(sock->sk, sip);
3411 break;
3412 case AF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003413#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
Casey Schauflerc6739442013-05-22 18:42:56 -07003414 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003415#endif /* CONFIG_IPV6 && !CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003416 break;
3417 }
3418 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003419}
3420
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003421/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003422 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003423 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003424 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003425 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003426 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003427 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003428static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3429 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003430{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003431 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003432 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003433 int acat;
3434 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003435
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003436 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003437 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003438 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003439 * If there are flags but no level netlabel isn't
3440 * behaving the way we expect it to.
3441 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003442 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003443 * Without guidance regarding the smack value
3444 * for the packet fall back on the network
3445 * ambient value.
3446 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003447 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003448 list_for_each_entry(skp, &smack_known_list, list) {
3449 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003450 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003451 /*
3452 * Compare the catsets. Use the netlbl APIs.
3453 */
3454 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3455 if ((skp->smk_netlabel.flags &
3456 NETLBL_SECATTR_MLS_CAT) == 0)
3457 found = 1;
3458 break;
3459 }
3460 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003461 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3462 acat + 1);
3463 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003464 skp->smk_netlabel.attr.mls.cat,
3465 kcat + 1);
3466 if (acat < 0 || kcat < 0)
3467 break;
3468 }
3469 if (acat == kcat) {
3470 found = 1;
3471 break;
3472 }
Casey Schauflere114e472008-02-04 22:29:50 -08003473 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003474 rcu_read_unlock();
3475
3476 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003477 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003478
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003479 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003480 return &smack_known_web;
3481 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003482 }
3483 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3484 /*
3485 * Looks like a fallback, which gives us a secid.
3486 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003487 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003488 /*
3489 * This has got to be a bug because it is
3490 * impossible to specify a fallback without
3491 * specifying the label, which will ensure
3492 * it has a secid, and the only way to get a
3493 * secid is from a fallback.
3494 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003495 BUG_ON(skp == NULL);
3496 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003497 }
3498 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003499 * Without guidance regarding the smack value
3500 * for the packet fall back on the network
3501 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003502 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003503 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003504}
3505
Casey Schaufler69f287a2014-12-12 17:08:40 -08003506#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003507static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003508{
Casey Schauflerc6739442013-05-22 18:42:56 -07003509 u8 nexthdr;
3510 int offset;
3511 int proto = -EINVAL;
3512 struct ipv6hdr _ipv6h;
3513 struct ipv6hdr *ip6;
3514 __be16 frag_off;
3515 struct tcphdr _tcph, *th;
3516 struct udphdr _udph, *uh;
3517 struct dccp_hdr _dccph, *dh;
3518
3519 sip->sin6_port = 0;
3520
3521 offset = skb_network_offset(skb);
3522 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3523 if (ip6 == NULL)
3524 return -EINVAL;
3525 sip->sin6_addr = ip6->saddr;
3526
3527 nexthdr = ip6->nexthdr;
3528 offset += sizeof(_ipv6h);
3529 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3530 if (offset < 0)
3531 return -EINVAL;
3532
3533 proto = nexthdr;
3534 switch (proto) {
3535 case IPPROTO_TCP:
3536 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3537 if (th != NULL)
3538 sip->sin6_port = th->source;
3539 break;
3540 case IPPROTO_UDP:
3541 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3542 if (uh != NULL)
3543 sip->sin6_port = uh->source;
3544 break;
3545 case IPPROTO_DCCP:
3546 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3547 if (dh != NULL)
3548 sip->sin6_port = dh->dccph_sport;
3549 break;
3550 }
3551 return proto;
3552}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003553#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003554
Casey Schauflere114e472008-02-04 22:29:50 -08003555/**
3556 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3557 * @sk: socket
3558 * @skb: packet
3559 *
3560 * Returns 0 if the packet should be delivered, an error code otherwise
3561 */
3562static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3563{
3564 struct netlbl_lsm_secattr secattr;
3565 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003566 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003567 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003568 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003569#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003570 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003571#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003572#if IS_ENABLED(CONFIG_IPV6)
3573 struct sockaddr_in6 sadd;
3574 int proto;
3575#endif /* CONFIG_IPV6 */
3576
Casey Schauflerc6739442013-05-22 18:42:56 -07003577 switch (sk->sk_family) {
3578 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003579#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3580 /*
3581 * If there is a secmark use it rather than the CIPSO label.
3582 * If there is no secmark fall back to CIPSO.
3583 * The secmark is assumed to reflect policy better.
3584 */
3585 if (skb && skb->secmark != 0) {
3586 skp = smack_from_secid(skb->secmark);
3587 goto access_check;
3588 }
3589#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003590 /*
3591 * Translate what netlabel gave us.
3592 */
3593 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003594
Casey Schauflerc6739442013-05-22 18:42:56 -07003595 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3596 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003597 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003598 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003599 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003600
Casey Schauflerc6739442013-05-22 18:42:56 -07003601 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003602
Casey Schaufler69f287a2014-12-12 17:08:40 -08003603#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3604access_check:
3605#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003606#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003607 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3608 ad.a.u.net->family = sk->sk_family;
3609 ad.a.u.net->netif = skb->skb_iif;
3610 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003611#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003612 /*
3613 * Receiving a packet requires that the other end
3614 * be able to write here. Read access is not required.
3615 * This is the simplist possible security model
3616 * for networking.
3617 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003618 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3619 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003620 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003621 if (rc != 0)
3622 netlbl_skbuff_err(skb, rc, 0);
3623 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003624#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003625 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003626 proto = smk_skb_to_addr_ipv6(skb, &sadd);
3627 if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
3628 break;
3629#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3630 if (skb && skb->secmark != 0)
3631 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003632 else
Casey Schaufler69f287a2014-12-12 17:08:40 -08003633 skp = smack_net_ambient;
3634#ifdef CONFIG_AUDIT
3635 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3636 ad.a.u.net->family = sk->sk_family;
3637 ad.a.u.net->netif = skb->skb_iif;
3638 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3639#endif /* CONFIG_AUDIT */
3640 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3641 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3642 MAY_WRITE, rc);
3643#else /* CONFIG_SECURITY_SMACK_NETFILTER */
3644 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3645#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003646 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003647#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003648 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003649
Paul Moorea8134292008-10-10 10:16:31 -04003650 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003651}
3652
3653/**
3654 * smack_socket_getpeersec_stream - pull in packet label
3655 * @sock: the socket
3656 * @optval: user's destination
3657 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003658 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003659 *
3660 * returns zero on success, an error code otherwise
3661 */
3662static int smack_socket_getpeersec_stream(struct socket *sock,
3663 char __user *optval,
3664 int __user *optlen, unsigned len)
3665{
3666 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003667 char *rcp = "";
3668 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003669 int rc = 0;
3670
3671 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003672 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003673 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003674 slen = strlen(rcp) + 1;
3675 }
Casey Schauflere114e472008-02-04 22:29:50 -08003676
3677 if (slen > len)
3678 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003679 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003680 rc = -EFAULT;
3681
3682 if (put_user(slen, optlen) != 0)
3683 rc = -EFAULT;
3684
3685 return rc;
3686}
3687
3688
3689/**
3690 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003691 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003692 * @skb: packet data
3693 * @secid: pointer to where to put the secid of the packet
3694 *
3695 * Sets the netlabel socket state on sk from parent
3696 */
3697static int smack_socket_getpeersec_dgram(struct socket *sock,
3698 struct sk_buff *skb, u32 *secid)
3699
3700{
3701 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003702 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003703 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003704 int family = PF_UNSPEC;
3705 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003706 int rc;
3707
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003708 if (skb != NULL) {
3709 if (skb->protocol == htons(ETH_P_IP))
3710 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003711#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003712 else if (skb->protocol == htons(ETH_P_IPV6))
3713 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003714#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003715 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003716 if (family == PF_UNSPEC && sock != NULL)
3717 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003718
Casey Schaufler69f287a2014-12-12 17:08:40 -08003719 switch (family) {
3720 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003721 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003722 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003723 break;
3724 case PF_INET:
3725#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3726 s = skb->secmark;
3727 if (s != 0)
3728 break;
3729#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003730 /*
3731 * Translate what netlabel gave us.
3732 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003733 if (sock != NULL && sock->sk != NULL)
3734 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003735 netlbl_secattr_init(&secattr);
3736 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3737 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003738 skp = smack_from_secattr(&secattr, ssp);
3739 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003740 }
3741 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003742 break;
3743#if IS_ENABLED(CONFIG_IPV6)
3744 case PF_INET6:
3745#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3746 s = skb->secmark;
3747#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3748 break;
3749#endif /* CONFIG_IPV6 */
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003750 }
3751 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003752 if (s == 0)
3753 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003754 return 0;
3755}
3756
3757/**
Paul Moore07feee82009-03-27 17:10:54 -04003758 * smack_sock_graft - Initialize a newly created socket with an existing sock
3759 * @sk: child sock
3760 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003761 *
Paul Moore07feee82009-03-27 17:10:54 -04003762 * Set the smk_{in,out} state of an existing sock based on the process that
3763 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003764 */
3765static void smack_sock_graft(struct sock *sk, struct socket *parent)
3766{
3767 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003768 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003769
Paul Moore07feee82009-03-27 17:10:54 -04003770 if (sk == NULL ||
3771 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003772 return;
3773
3774 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003775 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003776 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003777 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003778}
3779
3780/**
3781 * smack_inet_conn_request - Smack access check on connect
3782 * @sk: socket involved
3783 * @skb: packet
3784 * @req: unused
3785 *
3786 * Returns 0 if a task with the packet label could write to
3787 * the socket, otherwise an error code
3788 */
3789static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3790 struct request_sock *req)
3791{
Paul Moore07feee82009-03-27 17:10:54 -04003792 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003793 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003794 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003795 struct netlbl_lsm_secattr secattr;
3796 struct sockaddr_in addr;
3797 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003798 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003799 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003800 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003801#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003802 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003803#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003804
Casey Schaufler69f287a2014-12-12 17:08:40 -08003805#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003806 if (family == PF_INET6) {
3807 /*
3808 * Handle mapped IPv4 packets arriving
3809 * via IPv6 sockets. Don't set up netlabel
3810 * processing on IPv6.
3811 */
3812 if (skb->protocol == htons(ETH_P_IP))
3813 family = PF_INET;
3814 else
3815 return 0;
3816 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003817#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003818
Paul Moore07feee82009-03-27 17:10:54 -04003819 netlbl_secattr_init(&secattr);
3820 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003821 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003822 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003823 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003824 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003825 netlbl_secattr_destroy(&secattr);
3826
Etienne Bassetecfcc532009-04-08 20:40:06 +02003827#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003828 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3829 ad.a.u.net->family = family;
3830 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003831 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3832#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003833 /*
Paul Moore07feee82009-03-27 17:10:54 -04003834 * Receiving a packet requires that the other end be able to write
3835 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003836 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003837 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3838 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003839 if (rc != 0)
3840 return rc;
3841
3842 /*
3843 * Save the peer's label in the request_sock so we can later setup
3844 * smk_packet in the child socket so that SO_PEERCRED can report it.
3845 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003846 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003847
3848 /*
3849 * We need to decide if we want to label the incoming connection here
3850 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003851 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003852 */
3853 hdr = ip_hdr(skb);
3854 addr.sin_addr.s_addr = hdr->saddr;
3855 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003856 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003857 rcu_read_unlock();
3858
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003859 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003860 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003861 else
Paul Moore07feee82009-03-27 17:10:54 -04003862 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003863
3864 return rc;
3865}
3866
Paul Moore07feee82009-03-27 17:10:54 -04003867/**
3868 * smack_inet_csk_clone - Copy the connection information to the new socket
3869 * @sk: the new socket
3870 * @req: the connection's request_sock
3871 *
3872 * Transfer the connection's peer label to the newly created socket.
3873 */
3874static void smack_inet_csk_clone(struct sock *sk,
3875 const struct request_sock *req)
3876{
3877 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003878 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003879
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003880 if (req->peer_secid != 0) {
3881 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003882 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003883 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003884 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003885}
3886
Casey Schauflere114e472008-02-04 22:29:50 -08003887/*
3888 * Key management security hooks
3889 *
3890 * Casey has not tested key support very heavily.
3891 * The permission check is most likely too restrictive.
3892 * If you care about keys please have a look.
3893 */
3894#ifdef CONFIG_KEYS
3895
3896/**
3897 * smack_key_alloc - Set the key security blob
3898 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003899 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003900 * @flags: unused
3901 *
3902 * No allocation required
3903 *
3904 * Returns 0
3905 */
David Howellsd84f4f92008-11-14 10:39:23 +11003906static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003907 unsigned long flags)
3908{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003909 struct smack_known *skp = smk_of_task(cred->security);
3910
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003911 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003912 return 0;
3913}
3914
3915/**
3916 * smack_key_free - Clear the key security blob
3917 * @key: the object
3918 *
3919 * Clear the blob pointer
3920 */
3921static void smack_key_free(struct key *key)
3922{
3923 key->security = NULL;
3924}
3925
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003926/**
Casey Schauflere114e472008-02-04 22:29:50 -08003927 * smack_key_permission - Smack access on a key
3928 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003929 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003930 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08003931 *
3932 * Return 0 if the task has read and write to the object,
3933 * an error code otherwise
3934 */
3935static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00003936 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003937{
3938 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003939 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003940 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003941 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07003942 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003943
3944 keyp = key_ref_to_ptr(key_ref);
3945 if (keyp == NULL)
3946 return -EINVAL;
3947 /*
3948 * If the key hasn't been initialized give it access so that
3949 * it may do so.
3950 */
3951 if (keyp->security == NULL)
3952 return 0;
3953 /*
3954 * This should not occur
3955 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003956 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003957 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003958#ifdef CONFIG_AUDIT
3959 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3960 ad.a.u.key_struct.key = keyp->serial;
3961 ad.a.u.key_struct.key_desc = keyp->description;
3962#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003963 if (perm & KEY_NEED_READ)
3964 request = MAY_READ;
3965 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3966 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07003967 rc = smk_access(tkp, keyp->security, request, &ad);
3968 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
3969 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003970}
3971#endif /* CONFIG_KEYS */
3972
3973/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003974 * Smack Audit hooks
3975 *
3976 * Audit requires a unique representation of each Smack specific
3977 * rule. This unique representation is used to distinguish the
3978 * object to be audited from remaining kernel objects and also
3979 * works as a glue between the audit hooks.
3980 *
3981 * Since repository entries are added but never deleted, we'll use
3982 * the smack_known label address related to the given audit rule as
3983 * the needed unique representation. This also better fits the smack
3984 * model where nearly everything is a label.
3985 */
3986#ifdef CONFIG_AUDIT
3987
3988/**
3989 * smack_audit_rule_init - Initialize a smack audit rule
3990 * @field: audit rule fields given from user-space (audit.h)
3991 * @op: required testing operator (=, !=, >, <, ...)
3992 * @rulestr: smack label to be audited
3993 * @vrule: pointer to save our own audit rule representation
3994 *
3995 * Prepare to audit cases where (@field @op @rulestr) is true.
3996 * The label to be audited is created if necessay.
3997 */
3998static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3999{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004000 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004001 char **rule = (char **)vrule;
4002 *rule = NULL;
4003
4004 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4005 return -EINVAL;
4006
Al Viro5af75d82008-12-16 05:59:26 -05004007 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004008 return -EINVAL;
4009
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004010 skp = smk_import_entry(rulestr, 0);
4011 if (skp)
4012 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004013
4014 return 0;
4015}
4016
4017/**
4018 * smack_audit_rule_known - Distinguish Smack audit rules
4019 * @krule: rule of interest, in Audit kernel representation format
4020 *
4021 * This is used to filter Smack rules from remaining Audit ones.
4022 * If it's proved that this rule belongs to us, the
4023 * audit_rule_match hook will be called to do the final judgement.
4024 */
4025static int smack_audit_rule_known(struct audit_krule *krule)
4026{
4027 struct audit_field *f;
4028 int i;
4029
4030 for (i = 0; i < krule->field_count; i++) {
4031 f = &krule->fields[i];
4032
4033 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4034 return 1;
4035 }
4036
4037 return 0;
4038}
4039
4040/**
4041 * smack_audit_rule_match - Audit given object ?
4042 * @secid: security id for identifying the object to test
4043 * @field: audit rule flags given from user-space
4044 * @op: required testing operator
4045 * @vrule: smack internal rule presentation
4046 * @actx: audit context associated with the check
4047 *
4048 * The core Audit hook. It's used to take the decision of
4049 * whether to audit or not to audit a given object.
4050 */
4051static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4052 struct audit_context *actx)
4053{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004054 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004055 char *rule = vrule;
4056
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004057 if (unlikely(!rule)) {
4058 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004059 return -ENOENT;
4060 }
4061
4062 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4063 return 0;
4064
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004065 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004066
4067 /*
4068 * No need to do string comparisons. If a match occurs,
4069 * both pointers will point to the same smack_known
4070 * label.
4071 */
Al Viro5af75d82008-12-16 05:59:26 -05004072 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004073 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004074 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004075 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004076
4077 return 0;
4078}
4079
4080/**
4081 * smack_audit_rule_free - free smack rule representation
4082 * @vrule: rule to be freed.
4083 *
4084 * No memory was allocated.
4085 */
4086static void smack_audit_rule_free(void *vrule)
4087{
4088 /* No-op */
4089}
4090
4091#endif /* CONFIG_AUDIT */
4092
Randy Dunlap251a2a92009-02-18 11:42:33 -08004093/**
David Quigley746df9b2013-05-22 12:50:35 -04004094 * smack_ismaclabel - check if xattr @name references a smack MAC label
4095 * @name: Full xattr name to check.
4096 */
4097static int smack_ismaclabel(const char *name)
4098{
4099 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4100}
4101
4102
4103/**
Casey Schauflere114e472008-02-04 22:29:50 -08004104 * smack_secid_to_secctx - return the smack label for a secid
4105 * @secid: incoming integer
4106 * @secdata: destination
4107 * @seclen: how long it is
4108 *
4109 * Exists for networking code.
4110 */
4111static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4112{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004113 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004114
Eric Parisd5630b92010-10-13 16:24:48 -04004115 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004116 *secdata = skp->smk_known;
4117 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004118 return 0;
4119}
4120
Randy Dunlap251a2a92009-02-18 11:42:33 -08004121/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004122 * smack_secctx_to_secid - return the secid for a smack label
4123 * @secdata: smack label
4124 * @seclen: how long result is
4125 * @secid: outgoing integer
4126 *
4127 * Exists for audit and networking code.
4128 */
David Howellse52c17642008-04-29 20:52:51 +01004129static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004130{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004131 struct smack_known *skp = smk_find_entry(secdata);
4132
4133 if (skp)
4134 *secid = skp->smk_secid;
4135 else
4136 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004137 return 0;
4138}
4139
Randy Dunlap251a2a92009-02-18 11:42:33 -08004140/**
Casey Schauflere114e472008-02-04 22:29:50 -08004141 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004142 * @secdata: unused
4143 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004144 *
4145 * Exists to make sure nothing gets done, and properly
4146 */
4147static void smack_release_secctx(char *secdata, u32 seclen)
4148{
4149}
4150
David P. Quigley1ee65e32009-09-03 14:25:57 -04004151static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4152{
4153 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4154}
4155
4156static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4157{
4158 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4159}
4160
4161static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4162{
4163 int len = 0;
4164 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4165
4166 if (len < 0)
4167 return len;
4168 *ctxlen = len;
4169 return 0;
4170}
4171
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02004172struct security_operations smack_ops = {
4173 .name = "smack",
4174
Ingo Molnar9e488582009-05-07 19:26:19 +10004175 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01004176 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08004177 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08004178
4179 .sb_alloc_security = smack_sb_alloc_security,
4180 .sb_free_security = smack_sb_free_security,
4181 .sb_copy_data = smack_sb_copy_data,
4182 .sb_kern_mount = smack_sb_kern_mount,
4183 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08004184
Casey Schaufler676dac42010-12-02 06:43:39 -08004185 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03004186 .bprm_committing_creds = smack_bprm_committing_creds,
4187 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08004188
Casey Schauflere114e472008-02-04 22:29:50 -08004189 .inode_alloc_security = smack_inode_alloc_security,
4190 .inode_free_security = smack_inode_free_security,
4191 .inode_init_security = smack_inode_init_security,
4192 .inode_link = smack_inode_link,
4193 .inode_unlink = smack_inode_unlink,
4194 .inode_rmdir = smack_inode_rmdir,
4195 .inode_rename = smack_inode_rename,
4196 .inode_permission = smack_inode_permission,
4197 .inode_setattr = smack_inode_setattr,
4198 .inode_getattr = smack_inode_getattr,
4199 .inode_setxattr = smack_inode_setxattr,
4200 .inode_post_setxattr = smack_inode_post_setxattr,
4201 .inode_getxattr = smack_inode_getxattr,
4202 .inode_removexattr = smack_inode_removexattr,
4203 .inode_getsecurity = smack_inode_getsecurity,
4204 .inode_setsecurity = smack_inode_setsecurity,
4205 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004206 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004207
4208 .file_permission = smack_file_permission,
4209 .file_alloc_security = smack_file_alloc_security,
4210 .file_free_security = smack_file_free_security,
4211 .file_ioctl = smack_file_ioctl,
4212 .file_lock = smack_file_lock,
4213 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04004214 .mmap_file = smack_mmap_file,
4215 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08004216 .file_set_fowner = smack_file_set_fowner,
4217 .file_send_sigiotask = smack_file_send_sigiotask,
4218 .file_receive = smack_file_receive,
4219
Eric Paris83d49852012-04-04 13:45:40 -04004220 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07004221
David Howellsee18d642009-09-02 09:14:21 +01004222 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11004223 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11004224 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01004225 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11004226 .kernel_act_as = smack_kernel_act_as,
4227 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08004228 .task_setpgid = smack_task_setpgid,
4229 .task_getpgid = smack_task_getpgid,
4230 .task_getsid = smack_task_getsid,
4231 .task_getsecid = smack_task_getsecid,
4232 .task_setnice = smack_task_setnice,
4233 .task_setioprio = smack_task_setioprio,
4234 .task_getioprio = smack_task_getioprio,
4235 .task_setscheduler = smack_task_setscheduler,
4236 .task_getscheduler = smack_task_getscheduler,
4237 .task_movememory = smack_task_movememory,
4238 .task_kill = smack_task_kill,
4239 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08004240 .task_to_inode = smack_task_to_inode,
4241
4242 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004243 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004244
4245 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4246 .msg_msg_free_security = smack_msg_msg_free_security,
4247
4248 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4249 .msg_queue_free_security = smack_msg_queue_free_security,
4250 .msg_queue_associate = smack_msg_queue_associate,
4251 .msg_queue_msgctl = smack_msg_queue_msgctl,
4252 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4253 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4254
4255 .shm_alloc_security = smack_shm_alloc_security,
4256 .shm_free_security = smack_shm_free_security,
4257 .shm_associate = smack_shm_associate,
4258 .shm_shmctl = smack_shm_shmctl,
4259 .shm_shmat = smack_shm_shmat,
4260
4261 .sem_alloc_security = smack_sem_alloc_security,
4262 .sem_free_security = smack_sem_free_security,
4263 .sem_associate = smack_sem_associate,
4264 .sem_semctl = smack_sem_semctl,
4265 .sem_semop = smack_sem_semop,
4266
Casey Schauflere114e472008-02-04 22:29:50 -08004267 .d_instantiate = smack_d_instantiate,
4268
4269 .getprocattr = smack_getprocattr,
4270 .setprocattr = smack_setprocattr,
4271
4272 .unix_stream_connect = smack_unix_stream_connect,
4273 .unix_may_send = smack_unix_may_send,
4274
4275 .socket_post_create = smack_socket_post_create,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004276#ifndef CONFIG_SECURITY_SMACK_NETFILTER
Casey Schauflerc6739442013-05-22 18:42:56 -07004277 .socket_bind = smack_socket_bind,
Casey Schaufler69f287a2014-12-12 17:08:40 -08004278#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004279 .socket_connect = smack_socket_connect,
4280 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08004281 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4282 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4283 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4284 .sk_alloc_security = smack_sk_alloc_security,
4285 .sk_free_security = smack_sk_free_security,
4286 .sock_graft = smack_sock_graft,
4287 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04004288 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004289
Casey Schauflere114e472008-02-04 22:29:50 -08004290 /* key management security hooks */
4291#ifdef CONFIG_KEYS
4292 .key_alloc = smack_key_alloc,
4293 .key_free = smack_key_free,
4294 .key_permission = smack_key_permission,
4295#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004296
4297 /* Audit hooks */
4298#ifdef CONFIG_AUDIT
4299 .audit_rule_init = smack_audit_rule_init,
4300 .audit_rule_known = smack_audit_rule_known,
4301 .audit_rule_match = smack_audit_rule_match,
4302 .audit_rule_free = smack_audit_rule_free,
4303#endif /* CONFIG_AUDIT */
4304
David Quigley746df9b2013-05-22 12:50:35 -04004305 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08004306 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004307 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08004308 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04004309 .inode_notifysecctx = smack_inode_notifysecctx,
4310 .inode_setsecctx = smack_inode_setsecctx,
4311 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08004312};
4313
Etienne Basset7198e2e2009-03-24 20:53:24 +01004314
Casey Schaufler86812bb2012-04-17 18:55:46 -07004315static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004316{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004317 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004318 * Initialize rule list locks
4319 */
4320 mutex_init(&smack_known_huh.smk_rules_lock);
4321 mutex_init(&smack_known_hat.smk_rules_lock);
4322 mutex_init(&smack_known_floor.smk_rules_lock);
4323 mutex_init(&smack_known_star.smk_rules_lock);
4324 mutex_init(&smack_known_invalid.smk_rules_lock);
4325 mutex_init(&smack_known_web.smk_rules_lock);
4326 /*
4327 * Initialize rule lists
4328 */
4329 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4330 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4331 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4332 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4333 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4334 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4335 /*
4336 * Create the known labels list
4337 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004338 smk_insert_entry(&smack_known_huh);
4339 smk_insert_entry(&smack_known_hat);
4340 smk_insert_entry(&smack_known_star);
4341 smk_insert_entry(&smack_known_floor);
4342 smk_insert_entry(&smack_known_invalid);
4343 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004344}
4345
Casey Schauflere114e472008-02-04 22:29:50 -08004346/**
4347 * smack_init - initialize the smack system
4348 *
4349 * Returns 0
4350 */
4351static __init int smack_init(void)
4352{
David Howellsd84f4f92008-11-14 10:39:23 +11004353 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004354 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004355
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004356 if (!security_module_enable(&smack_ops))
4357 return 0;
4358
Casey Schaufler69f287a2014-12-12 17:08:40 -08004359 smack_enabled = 1;
4360
Rohit1a5b4722014-10-15 17:40:41 +05304361 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4362 if (!smack_inode_cache)
4363 return -ENOMEM;
4364
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004365 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4366 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304367 if (tsp == NULL) {
4368 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004369 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304370 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004371
Casey Schauflere114e472008-02-04 22:29:50 -08004372 printk(KERN_INFO "Smack: Initializing.\n");
4373
4374 /*
4375 * Set the security state for the initial task.
4376 */
David Howellsd84f4f92008-11-14 10:39:23 +11004377 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004378 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004379
Casey Schaufler86812bb2012-04-17 18:55:46 -07004380 /* initialize the smack_known_list */
4381 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004382
4383 /*
4384 * Register with LSM
4385 */
4386 if (register_security(&smack_ops))
4387 panic("smack: Unable to register with kernel.\n");
4388
4389 return 0;
4390}
4391
4392/*
4393 * Smack requires early initialization in order to label
4394 * all processes and objects when they are created.
4395 */
4396security_initcall(smack_init);