blob: dcfaddd955d1e77224b78439631b1d01e30f24eb [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
55LIST_HEAD(smk_ipv6_port_list);
Rohit1a5b4722014-10-15 17:40:41 +053056static struct kmem_cache *smack_inode_cache;
Casey Schauflerc6739442013-05-22 18:42:56 -070057
Casey Schauflerd166c802014-08-27 14:51:27 -070058#ifdef CONFIG_SECURITY_SMACK_BRINGUP
59static void smk_bu_mode(int mode, char *s)
60{
61 int i = 0;
62
63 if (mode & MAY_READ)
64 s[i++] = 'r';
65 if (mode & MAY_WRITE)
66 s[i++] = 'w';
67 if (mode & MAY_EXEC)
68 s[i++] = 'x';
69 if (mode & MAY_APPEND)
70 s[i++] = 'a';
71 if (mode & MAY_TRANSMUTE)
72 s[i++] = 't';
73 if (mode & MAY_LOCK)
74 s[i++] = 'l';
75 if (i == 0)
76 s[i++] = '-';
77 s[i] = '\0';
78}
79#endif
80
81#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020082static int smk_bu_note(char *note, struct smack_known *sskp,
83 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -070084{
85 char acc[SMK_NUM_ACCESS_TYPE + 1];
86
87 if (rc <= 0)
88 return rc;
89
90 smk_bu_mode(mode, acc);
91 pr_info("Smack Bringup: (%s %s %s) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020092 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -070093 return 0;
94}
95#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020096#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -070097#endif
98
99#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200100static int smk_bu_current(char *note, struct smack_known *oskp,
101 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700102{
103 struct task_smack *tsp = current_security();
104 char acc[SMK_NUM_ACCESS_TYPE + 1];
105
106 if (rc <= 0)
107 return rc;
108
109 smk_bu_mode(mode, acc);
110 pr_info("Smack Bringup: (%s %s %s) %s %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200111 tsp->smk_task->smk_known, oskp->smk_known,
112 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700113 return 0;
114}
115#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200116#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700117#endif
118
119#ifdef CONFIG_SECURITY_SMACK_BRINGUP
120static int smk_bu_task(struct task_struct *otp, int mode, int rc)
121{
122 struct task_smack *tsp = current_security();
123 struct task_smack *otsp = task_security(otp);
124 char acc[SMK_NUM_ACCESS_TYPE + 1];
125
126 if (rc <= 0)
127 return rc;
128
129 smk_bu_mode(mode, acc);
130 pr_info("Smack Bringup: (%s %s %s) %s to %s\n",
131 tsp->smk_task->smk_known, otsp->smk_task->smk_known, acc,
132 current->comm, otp->comm);
133 return 0;
134}
135#else
136#define smk_bu_task(otp, mode, RC) (RC)
137#endif
138
139#ifdef CONFIG_SECURITY_SMACK_BRINGUP
140static int smk_bu_inode(struct inode *inode, int mode, int rc)
141{
142 struct task_smack *tsp = current_security();
143 char acc[SMK_NUM_ACCESS_TYPE + 1];
144
145 if (rc <= 0)
146 return rc;
147
148 smk_bu_mode(mode, acc);
149 pr_info("Smack Bringup: (%s %s %s) inode=(%s %ld) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200150 tsp->smk_task->smk_known, smk_of_inode(inode)->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700151 inode->i_sb->s_id, inode->i_ino, current->comm);
152 return 0;
153}
154#else
155#define smk_bu_inode(inode, mode, RC) (RC)
156#endif
157
158#ifdef CONFIG_SECURITY_SMACK_BRINGUP
159static int smk_bu_file(struct file *file, int mode, int rc)
160{
161 struct task_smack *tsp = current_security();
162 struct smack_known *sskp = tsp->smk_task;
163 struct inode *inode = file->f_inode;
164 char acc[SMK_NUM_ACCESS_TYPE + 1];
165
166 if (rc <= 0)
167 return rc;
168
169 smk_bu_mode(mode, acc);
Al Viroa4555892014-10-21 20:11:25 -0400170 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
Casey Schauflerd166c802014-08-27 14:51:27 -0700171 sskp->smk_known, (char *)file->f_security, acc,
Al Viroa4555892014-10-21 20:11:25 -0400172 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700173 current->comm);
174 return 0;
175}
176#else
177#define smk_bu_file(file, mode, RC) (RC)
178#endif
179
180#ifdef CONFIG_SECURITY_SMACK_BRINGUP
181static int smk_bu_credfile(const struct cred *cred, struct file *file,
182 int mode, int rc)
183{
184 struct task_smack *tsp = cred->security;
185 struct smack_known *sskp = tsp->smk_task;
186 struct inode *inode = file->f_inode;
187 char acc[SMK_NUM_ACCESS_TYPE + 1];
188
189 if (rc <= 0)
190 return rc;
191
192 smk_bu_mode(mode, acc);
Al Viroa4555892014-10-21 20:11:25 -0400193 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %pD) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200194 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400195 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700196 current->comm);
197 return 0;
198}
199#else
200#define smk_bu_credfile(cred, file, mode, RC) (RC)
201#endif
202
Casey Schauflere114e472008-02-04 22:29:50 -0800203/**
204 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100205 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800206 * @ip: a pointer to the inode
207 * @dp: a pointer to the dentry
208 *
209 * Returns a pointer to the master list entry for the Smack label
210 * or NULL if there was no label to fetch.
211 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700212static struct smack_known *smk_fetch(const char *name, struct inode *ip,
213 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800214{
215 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700216 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700217 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800218
219 if (ip->i_op->getxattr == NULL)
220 return NULL;
221
Casey Schauflerf7112e62012-05-06 15:22:02 -0700222 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
223 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800224 return NULL;
225
Casey Schauflerf7112e62012-05-06 15:22:02 -0700226 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
227 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700228 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700229
230 kfree(buffer);
231
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700232 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800233}
234
235/**
236 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200237 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800238 *
239 * Returns the new blob or NULL if there's no memory available
240 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200241struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800242{
243 struct inode_smack *isp;
244
Rohit1a5b4722014-10-15 17:40:41 +0530245 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800246 if (isp == NULL)
247 return NULL;
248
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200249 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800250 isp->smk_flags = 0;
251 mutex_init(&isp->smk_lock);
252
253 return isp;
254}
255
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800256/**
257 * new_task_smack - allocate a task security blob
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100258 * @task: a pointer to the Smack label for the running task
259 * @forked: a pointer to the Smack label for the forked task
260 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800261 *
262 * Returns the new blob or NULL if there's no memory available
263 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700264static struct task_smack *new_task_smack(struct smack_known *task,
265 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800266{
267 struct task_smack *tsp;
268
269 tsp = kzalloc(sizeof(struct task_smack), gfp);
270 if (tsp == NULL)
271 return NULL;
272
273 tsp->smk_task = task;
274 tsp->smk_forked = forked;
275 INIT_LIST_HEAD(&tsp->smk_rules);
276 mutex_init(&tsp->smk_rules_lock);
277
278 return tsp;
279}
280
281/**
282 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100283 * @nhead: new rules header pointer
284 * @ohead: old rules header pointer
285 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800286 *
287 * Returns 0 on success, -ENOMEM on error
288 */
289static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
290 gfp_t gfp)
291{
292 struct smack_rule *nrp;
293 struct smack_rule *orp;
294 int rc = 0;
295
296 INIT_LIST_HEAD(nhead);
297
298 list_for_each_entry_rcu(orp, ohead, list) {
299 nrp = kzalloc(sizeof(struct smack_rule), gfp);
300 if (nrp == NULL) {
301 rc = -ENOMEM;
302 break;
303 }
304 *nrp = *orp;
305 list_add_rcu(&nrp->list, nhead);
306 }
307 return rc;
308}
309
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100310/**
311 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
312 * @mode - input mode in form of PTRACE_MODE_*
313 *
314 * Returns a converted MAY_* mode usable by smack rules
315 */
316static inline unsigned int smk_ptrace_mode(unsigned int mode)
317{
318 switch (mode) {
319 case PTRACE_MODE_READ:
320 return MAY_READ;
321 case PTRACE_MODE_ATTACH:
322 return MAY_READWRITE;
323 }
324
325 return 0;
326}
327
328/**
329 * smk_ptrace_rule_check - helper for ptrace access
330 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200331 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100332 * @mode: ptrace attachment mode (PTRACE_MODE_*)
333 * @func: name of the function that called us, used for audit
334 *
335 * Returns 0 on access granted, -error on error
336 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200337static int smk_ptrace_rule_check(struct task_struct *tracer,
338 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100339 unsigned int mode, const char *func)
340{
341 int rc;
342 struct smk_audit_info ad, *saip = NULL;
343 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200344 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100345
346 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
347 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
348 smk_ad_setfield_u_tsk(&ad, tracer);
349 saip = &ad;
350 }
351
352 tsp = task_security(tracer);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200353 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100354
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100355 if ((mode & PTRACE_MODE_ATTACH) &&
356 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
357 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200358 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100359 rc = 0;
360 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
361 rc = -EACCES;
362 else if (capable(CAP_SYS_PTRACE))
363 rc = 0;
364 else
365 rc = -EACCES;
366
367 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200368 smack_log(tracer_known->smk_known,
369 tracee_known->smk_known,
370 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100371
372 return rc;
373 }
374
375 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200376 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100377 return rc;
378}
379
Casey Schauflere114e472008-02-04 22:29:50 -0800380/*
381 * LSM hooks.
382 * We he, that is fun!
383 */
384
385/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000386 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800387 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100388 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800389 *
390 * Returns 0 if access is OK, an error code otherwise
391 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100392 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800393 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000394static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800395{
396 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700397 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800398
Ingo Molnar9e488582009-05-07 19:26:19 +1000399 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800400 if (rc != 0)
401 return rc;
402
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700403 skp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200404
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200405 rc = smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100406 return rc;
407}
Casey Schauflere114e472008-02-04 22:29:50 -0800408
David Howells5cd9c582008-08-14 11:37:28 +0100409/**
410 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
411 * @ptp: parent task pointer
412 *
413 * Returns 0 if access is OK, an error code otherwise
414 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100415 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100416 */
417static int smack_ptrace_traceme(struct task_struct *ptp)
418{
419 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700420 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100421
422 rc = cap_ptrace_traceme(ptp);
423 if (rc != 0)
424 return rc;
425
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100426 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200427
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200428 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800429 return rc;
430}
431
432/**
433 * smack_syslog - Smack approval on syslog
434 * @type: message type
435 *
Casey Schauflere114e472008-02-04 22:29:50 -0800436 * Returns 0 on success, error code otherwise.
437 */
Eric Paris12b30522010-11-15 18:36:29 -0500438static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800439{
Eric Paris12b30522010-11-15 18:36:29 -0500440 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700441 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800442
Casey Schaufler1880eff2012-06-05 15:28:30 -0700443 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800444 return 0;
445
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800446 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800447 rc = -EACCES;
448
449 return rc;
450}
451
452
453/*
454 * Superblock Hooks.
455 */
456
457/**
458 * smack_sb_alloc_security - allocate a superblock blob
459 * @sb: the superblock getting the blob
460 *
461 * Returns 0 on success or -ENOMEM on error.
462 */
463static int smack_sb_alloc_security(struct super_block *sb)
464{
465 struct superblock_smack *sbsp;
466
467 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
468
469 if (sbsp == NULL)
470 return -ENOMEM;
471
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200472 sbsp->smk_root = &smack_known_floor;
473 sbsp->smk_default = &smack_known_floor;
474 sbsp->smk_floor = &smack_known_floor;
475 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700476 /*
477 * smk_initialized will be zero from kzalloc.
478 */
Casey Schauflere114e472008-02-04 22:29:50 -0800479 sb->s_security = sbsp;
480
481 return 0;
482}
483
484/**
485 * smack_sb_free_security - free a superblock blob
486 * @sb: the superblock getting the blob
487 *
488 */
489static void smack_sb_free_security(struct super_block *sb)
490{
491 kfree(sb->s_security);
492 sb->s_security = NULL;
493}
494
495/**
496 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800497 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800498 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800499 *
500 * Returns 0 on success or -ENOMEM on error.
501 *
502 * Copy the Smack specific mount options out of the mount
503 * options list.
504 */
Eric Parise0007522008-03-05 10:31:54 -0500505static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800506{
507 char *cp, *commap, *otheropts, *dp;
508
Casey Schauflere114e472008-02-04 22:29:50 -0800509 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
510 if (otheropts == NULL)
511 return -ENOMEM;
512
513 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
514 if (strstr(cp, SMK_FSDEFAULT) == cp)
515 dp = smackopts;
516 else if (strstr(cp, SMK_FSFLOOR) == cp)
517 dp = smackopts;
518 else if (strstr(cp, SMK_FSHAT) == cp)
519 dp = smackopts;
520 else if (strstr(cp, SMK_FSROOT) == cp)
521 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700522 else if (strstr(cp, SMK_FSTRANS) == cp)
523 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800524 else
525 dp = otheropts;
526
527 commap = strchr(cp, ',');
528 if (commap != NULL)
529 *commap = '\0';
530
531 if (*dp != '\0')
532 strcat(dp, ",");
533 strcat(dp, cp);
534 }
535
536 strcpy(orig, otheropts);
537 free_page((unsigned long)otheropts);
538
539 return 0;
540}
541
542/**
543 * smack_sb_kern_mount - Smack specific mount processing
544 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100545 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800546 * @data: the smack mount options
547 *
548 * Returns 0 on success, an error code on failure
549 */
James Morris12204e22008-12-19 10:44:42 +1100550static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800551{
552 struct dentry *root = sb->s_root;
553 struct inode *inode = root->d_inode;
554 struct superblock_smack *sp = sb->s_security;
555 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800556 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800557 char *op;
558 char *commap;
Casey Schauflere830b392013-05-22 18:43:07 -0700559 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800560 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800561
Casey Schauflere830b392013-05-22 18:43:07 -0700562 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800563 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700564
Casey Schauflere114e472008-02-04 22:29:50 -0800565 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800566
567 for (op = data; op != NULL; op = commap) {
568 commap = strchr(op, ',');
569 if (commap != NULL)
570 *commap++ = '\0';
571
572 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
573 op += strlen(SMK_FSHAT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200574 skp = smk_import_entry(op, 0);
575 if (skp != NULL) {
576 sp->smk_hat = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800577 specified = 1;
578 }
Casey Schauflere114e472008-02-04 22:29:50 -0800579 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
580 op += strlen(SMK_FSFLOOR);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200581 skp = smk_import_entry(op, 0);
582 if (skp != NULL) {
583 sp->smk_floor = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800584 specified = 1;
585 }
Casey Schauflere114e472008-02-04 22:29:50 -0800586 } else if (strncmp(op, SMK_FSDEFAULT,
587 strlen(SMK_FSDEFAULT)) == 0) {
588 op += strlen(SMK_FSDEFAULT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200589 skp = smk_import_entry(op, 0);
590 if (skp != NULL) {
591 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800592 specified = 1;
593 }
Casey Schauflere114e472008-02-04 22:29:50 -0800594 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
595 op += strlen(SMK_FSROOT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200596 skp = smk_import_entry(op, 0);
597 if (skp != NULL) {
598 sp->smk_root = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800599 specified = 1;
600 }
Casey Schauflere830b392013-05-22 18:43:07 -0700601 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
602 op += strlen(SMK_FSTRANS);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200603 skp = smk_import_entry(op, 0);
604 if (skp != NULL) {
605 sp->smk_root = skp;
Casey Schauflere830b392013-05-22 18:43:07 -0700606 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800607 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700608 }
Casey Schauflere114e472008-02-04 22:29:50 -0800609 }
610 }
611
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800612 if (!smack_privileged(CAP_MAC_ADMIN)) {
613 /*
614 * Unprivileged mounts don't get to specify Smack values.
615 */
616 if (specified)
617 return -EPERM;
618 /*
619 * Unprivileged mounts get root and default from the caller.
620 */
621 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200622 sp->smk_root = skp;
623 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800624 }
Casey Schauflere114e472008-02-04 22:29:50 -0800625 /*
626 * Initialize the root inode.
627 */
628 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100629 if (isp == NULL) {
630 isp = new_inode_smack(sp->smk_root);
631 if (isp == NULL)
632 return -ENOMEM;
633 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700634 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800635 isp->smk_inode = sp->smk_root;
636
Casey Schauflere830b392013-05-22 18:43:07 -0700637 if (transmute)
638 isp->smk_flags |= SMK_INODE_TRANSMUTE;
639
Casey Schauflere114e472008-02-04 22:29:50 -0800640 return 0;
641}
642
643/**
644 * smack_sb_statfs - Smack check on statfs
645 * @dentry: identifies the file system in question
646 *
647 * Returns 0 if current can read the floor of the filesystem,
648 * and error code otherwise
649 */
650static int smack_sb_statfs(struct dentry *dentry)
651{
652 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200653 int rc;
654 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800655
Eric Parisa2694342011-04-25 13:10:27 -0400656 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200657 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
658
659 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700660 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200661 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800662}
663
Casey Schauflere114e472008-02-04 22:29:50 -0800664/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800665 * BPRM hooks
666 */
667
Casey Schauflerce8a4322011-09-29 18:21:01 -0700668/**
669 * smack_bprm_set_creds - set creds for exec
670 * @bprm: the exec information
671 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100672 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700673 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800674static int smack_bprm_set_creds(struct linux_binprm *bprm)
675{
Al Viro496ad9a2013-01-23 17:07:38 -0500676 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300677 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800678 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800679 int rc;
680
681 rc = cap_bprm_set_creds(bprm);
682 if (rc != 0)
683 return rc;
684
685 if (bprm->cred_prepared)
686 return 0;
687
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300688 isp = inode->i_security;
689 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800690 return 0;
691
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100692 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
693 struct task_struct *tracer;
694 rc = 0;
695
696 rcu_read_lock();
697 tracer = ptrace_parent(current);
698 if (likely(tracer != NULL))
699 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200700 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100701 PTRACE_MODE_ATTACH,
702 __func__);
703 rcu_read_unlock();
704
705 if (rc != 0)
706 return rc;
707 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300708 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800709
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300710 bsp->smk_task = isp->smk_task;
711 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800712
713 return 0;
714}
715
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300716/**
717 * smack_bprm_committing_creds - Prepare to install the new credentials
718 * from bprm.
719 *
720 * @bprm: binprm for exec
721 */
722static void smack_bprm_committing_creds(struct linux_binprm *bprm)
723{
724 struct task_smack *bsp = bprm->cred->security;
725
726 if (bsp->smk_task != bsp->smk_forked)
727 current->pdeath_signal = 0;
728}
729
730/**
731 * smack_bprm_secureexec - Return the decision to use secureexec.
732 * @bprm: binprm for exec
733 *
734 * Returns 0 on success.
735 */
736static int smack_bprm_secureexec(struct linux_binprm *bprm)
737{
738 struct task_smack *tsp = current_security();
739 int ret = cap_bprm_secureexec(bprm);
740
741 if (!ret && (tsp->smk_task != tsp->smk_forked))
742 ret = 1;
743
744 return ret;
745}
746
Casey Schaufler676dac42010-12-02 06:43:39 -0800747/*
Casey Schauflere114e472008-02-04 22:29:50 -0800748 * Inode hooks
749 */
750
751/**
752 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800753 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800754 *
755 * Returns 0 if it gets a blob, -ENOMEM otherwise
756 */
757static int smack_inode_alloc_security(struct inode *inode)
758{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700759 struct smack_known *skp = smk_of_current();
760
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200761 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800762 if (inode->i_security == NULL)
763 return -ENOMEM;
764 return 0;
765}
766
767/**
768 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800769 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800770 *
771 * Clears the blob pointer in inode
772 */
773static void smack_inode_free_security(struct inode *inode)
774{
Rohit1a5b4722014-10-15 17:40:41 +0530775 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800776 inode->i_security = NULL;
777}
778
779/**
780 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200781 * @inode: the newly created inode
782 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500783 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800784 * @name: where to put the attribute name
785 * @value: where to put the attribute value
786 * @len: where to put the length of the attribute
787 *
788 * Returns 0 if it all works out, -ENOMEM if there's no memory
789 */
790static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900791 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500792 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800793{
Casey Schaufler2267b132012-03-13 19:14:19 -0700794 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700795 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200796 struct smack_known *isp = smk_of_inode(inode);
797 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800798 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800799
Tetsuo Handa95489062013-07-25 05:44:02 +0900800 if (name)
801 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800802
803 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800804 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200805 may = smk_access_entry(skp->smk_known, dsp->smk_known,
806 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800807 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200808
809 /*
810 * If the access rule allows transmutation and
811 * the directory requests transmutation then
812 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700813 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200814 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800815 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700816 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200817 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700818 issp->smk_flags |= SMK_INODE_CHANGED;
819 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200820
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200821 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800822 if (*value == NULL)
823 return -ENOMEM;
824 }
825
826 if (len)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200827 *len = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -0800828
829 return 0;
830}
831
832/**
833 * smack_inode_link - Smack check on link
834 * @old_dentry: the existing object
835 * @dir: unused
836 * @new_dentry: the new object
837 *
838 * Returns 0 if access is permitted, an error code otherwise
839 */
840static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
841 struct dentry *new_dentry)
842{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200843 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200844 struct smk_audit_info ad;
845 int rc;
846
Eric Parisa2694342011-04-25 13:10:27 -0400847 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200848 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800849
850 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200851 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700852 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800853
854 if (rc == 0 && new_dentry->d_inode != NULL) {
855 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200856 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
857 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700858 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800859 }
860
861 return rc;
862}
863
864/**
865 * smack_inode_unlink - Smack check on inode deletion
866 * @dir: containing directory object
867 * @dentry: file to unlink
868 *
869 * Returns 0 if current can write the containing directory
870 * and the object, error code otherwise
871 */
872static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
873{
874 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200875 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800876 int rc;
877
Eric Parisa2694342011-04-25 13:10:27 -0400878 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200879 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
880
Casey Schauflere114e472008-02-04 22:29:50 -0800881 /*
882 * You need write access to the thing you're unlinking
883 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200884 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700885 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200886 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800887 /*
888 * You also need write access to the containing directory
889 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400890 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200891 smk_ad_setfield_u_fs_inode(&ad, dir);
892 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700893 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200894 }
Casey Schauflere114e472008-02-04 22:29:50 -0800895 return rc;
896}
897
898/**
899 * smack_inode_rmdir - Smack check on directory deletion
900 * @dir: containing directory object
901 * @dentry: directory to unlink
902 *
903 * Returns 0 if current can write the containing directory
904 * and the directory, error code otherwise
905 */
906static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
907{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200908 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800909 int rc;
910
Eric Parisa2694342011-04-25 13:10:27 -0400911 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200912 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
913
Casey Schauflere114e472008-02-04 22:29:50 -0800914 /*
915 * You need write access to the thing you're removing
916 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200917 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700918 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200919 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800920 /*
921 * You also need write access to the containing directory
922 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400923 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200924 smk_ad_setfield_u_fs_inode(&ad, dir);
925 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700926 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200927 }
Casey Schauflere114e472008-02-04 22:29:50 -0800928
929 return rc;
930}
931
932/**
933 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200934 * @old_inode: unused
935 * @old_dentry: the old object
936 * @new_inode: unused
937 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -0800938 *
939 * Read and write access is required on both the old and
940 * new directories.
941 *
942 * Returns 0 if access is permitted, an error code otherwise
943 */
944static int smack_inode_rename(struct inode *old_inode,
945 struct dentry *old_dentry,
946 struct inode *new_inode,
947 struct dentry *new_dentry)
948{
949 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200950 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200951 struct smk_audit_info ad;
952
Eric Parisa2694342011-04-25 13:10:27 -0400953 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200954 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800955
956 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200957 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700958 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800959
960 if (rc == 0 && new_dentry->d_inode != NULL) {
961 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200962 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
963 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700964 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800965 }
Casey Schauflere114e472008-02-04 22:29:50 -0800966 return rc;
967}
968
969/**
970 * smack_inode_permission - Smack version of permission()
971 * @inode: the inode in question
972 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800973 *
974 * This is the important Smack hook.
975 *
976 * Returns 0 if access is permitted, -EACCES otherwise
977 */
Al Viroe74f71e2011-06-20 19:38:15 -0400978static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800979{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200980 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400981 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -0700982 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -0400983
984 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800985 /*
986 * No permission to check. Existence test. Yup, it's there.
987 */
988 if (mask == 0)
989 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700990
991 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400992 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700993 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400994 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200995 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700996 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
997 rc = smk_bu_inode(inode, mask, rc);
998 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800999}
1000
1001/**
1002 * smack_inode_setattr - Smack check for setting attributes
1003 * @dentry: the object
1004 * @iattr: for the force flag
1005 *
1006 * Returns 0 if access is permitted, an error code otherwise
1007 */
1008static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1009{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001010 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001011 int rc;
1012
Casey Schauflere114e472008-02-04 22:29:50 -08001013 /*
1014 * Need to allow for clearing the setuid bit.
1015 */
1016 if (iattr->ia_valid & ATTR_FORCE)
1017 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001018 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001019 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001020
Casey Schauflerd166c802014-08-27 14:51:27 -07001021 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1022 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1023 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001024}
1025
1026/**
1027 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001028 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001029 * @dentry: the object
1030 *
1031 * Returns 0 if access is permitted, an error code otherwise
1032 */
1033static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1034{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001035 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -04001036 struct path path;
Casey Schauflerd166c802014-08-27 14:51:27 -07001037 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001038
Eric Parisa2694342011-04-25 13:10:27 -04001039 path.dentry = dentry;
1040 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001041
Eric Parisf48b7392011-04-25 12:54:27 -04001042 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -04001043 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001044 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1045 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1046 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001047}
1048
1049/**
1050 * smack_inode_setxattr - Smack check for setting xattrs
1051 * @dentry: the object
1052 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001053 * @value: value of the attribute
1054 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001055 * @flags: unused
1056 *
1057 * This protects the Smack attribute explicitly.
1058 *
1059 * Returns 0 if access is permitted, an error code otherwise
1060 */
David Howells8f0cfa52008-04-29 00:59:41 -07001061static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1062 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001063{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001064 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001065 struct smack_known *skp;
1066 int check_priv = 0;
1067 int check_import = 0;
1068 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001069 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001070
Casey Schaufler19760ad2013-12-16 16:27:26 -08001071 /*
1072 * Check label validity here so import won't fail in post_setxattr
1073 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001074 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1075 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001076 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1077 check_priv = 1;
1078 check_import = 1;
1079 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1080 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1081 check_priv = 1;
1082 check_import = 1;
1083 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001084 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001085 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001086 if (size != TRANS_TRUE_SIZE ||
1087 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1088 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001089 } else
1090 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1091
Casey Schaufler19760ad2013-12-16 16:27:26 -08001092 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1093 rc = -EPERM;
1094
1095 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001096 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001097 if (skp == NULL || (check_star &&
1098 (skp == &smack_known_star || skp == &smack_known_web)))
1099 rc = -EINVAL;
1100 }
1101
Eric Parisa2694342011-04-25 13:10:27 -04001102 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1104
Casey Schauflerd166c802014-08-27 14:51:27 -07001105 if (rc == 0) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001106 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001107 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1108 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001109
1110 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001111}
1112
1113/**
1114 * smack_inode_post_setxattr - Apply the Smack update approved above
1115 * @dentry: object
1116 * @name: attribute name
1117 * @value: attribute value
1118 * @size: attribute size
1119 * @flags: unused
1120 *
1121 * Set the pointer in the inode blob to the entry found
1122 * in the master label list.
1123 */
David Howells8f0cfa52008-04-29 00:59:41 -07001124static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1125 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001126{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001127 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001128 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001129
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001130 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1131 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1132 return;
1133 }
1134
Casey Schaufler676dac42010-12-02 06:43:39 -08001135 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001136 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001137 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001138 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001139 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001140 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001141 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001142 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001143 if (skp != NULL)
1144 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001145 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001146 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001147 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001148 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001149 if (skp != NULL)
1150 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001151 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001152 isp->smk_mmap = &smack_known_invalid;
1153 }
Casey Schauflere114e472008-02-04 22:29:50 -08001154
1155 return;
1156}
1157
Casey Schauflerce8a4322011-09-29 18:21:01 -07001158/**
Casey Schauflere114e472008-02-04 22:29:50 -08001159 * smack_inode_getxattr - Smack check on getxattr
1160 * @dentry: the object
1161 * @name: unused
1162 *
1163 * Returns 0 if access is permitted, an error code otherwise
1164 */
David Howells8f0cfa52008-04-29 00:59:41 -07001165static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001166{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001167 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001168 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001169
Eric Parisa2694342011-04-25 13:10:27 -04001170 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1172
Casey Schauflerd166c802014-08-27 14:51:27 -07001173 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1174 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1175 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001176}
1177
Casey Schauflerce8a4322011-09-29 18:21:01 -07001178/**
Casey Schauflere114e472008-02-04 22:29:50 -08001179 * smack_inode_removexattr - Smack check on removexattr
1180 * @dentry: the object
1181 * @name: name of the attribute
1182 *
1183 * Removing the Smack attribute requires CAP_MAC_ADMIN
1184 *
1185 * Returns 0 if access is permitted, an error code otherwise
1186 */
David Howells8f0cfa52008-04-29 00:59:41 -07001187static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001188{
Casey Schaufler676dac42010-12-02 06:43:39 -08001189 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001190 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001191 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001192
Casey Schauflerbcdca222008-02-23 15:24:04 -08001193 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1194 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001195 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001196 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001197 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301198 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001199 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001200 rc = -EPERM;
1201 } else
1202 rc = cap_inode_removexattr(dentry, name);
1203
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001204 if (rc != 0)
1205 return rc;
1206
Eric Parisa2694342011-04-25 13:10:27 -04001207 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001208 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001209
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001210 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001211 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001212 if (rc != 0)
1213 return rc;
1214
1215 isp = dentry->d_inode->i_security;
1216 /*
1217 * Don't do anything special for these.
1218 * XATTR_NAME_SMACKIPIN
1219 * XATTR_NAME_SMACKIPOUT
1220 * XATTR_NAME_SMACKEXEC
1221 */
1222 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001223 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001224 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001225 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001226 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1227 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001228
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001229 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001230}
1231
1232/**
1233 * smack_inode_getsecurity - get smack xattrs
1234 * @inode: the object
1235 * @name: attribute name
1236 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001237 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001238 *
1239 * Returns the size of the attribute or an error code
1240 */
1241static int smack_inode_getsecurity(const struct inode *inode,
1242 const char *name, void **buffer,
1243 bool alloc)
1244{
1245 struct socket_smack *ssp;
1246 struct socket *sock;
1247 struct super_block *sbp;
1248 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001249 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001250 int ilen;
1251 int rc = 0;
1252
1253 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1254 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001255 ilen = strlen(isp->smk_known);
1256 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001257 return ilen;
1258 }
1259
1260 /*
1261 * The rest of the Smack xattrs are only on sockets.
1262 */
1263 sbp = ip->i_sb;
1264 if (sbp->s_magic != SOCKFS_MAGIC)
1265 return -EOPNOTSUPP;
1266
1267 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001268 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001269 return -EOPNOTSUPP;
1270
1271 ssp = sock->sk->sk_security;
1272
1273 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001274 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001275 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001276 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001277 else
1278 return -EOPNOTSUPP;
1279
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001280 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001281 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001282 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001283 rc = ilen;
1284 }
1285
1286 return rc;
1287}
1288
1289
1290/**
1291 * smack_inode_listsecurity - list the Smack attributes
1292 * @inode: the object
1293 * @buffer: where they go
1294 * @buffer_size: size of buffer
1295 *
1296 * Returns 0 on success, -EINVAL otherwise
1297 */
1298static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1299 size_t buffer_size)
1300{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001301 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001302
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001303 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001304 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001305
1306 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001307}
1308
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001309/**
1310 * smack_inode_getsecid - Extract inode's security id
1311 * @inode: inode to extract the info from
1312 * @secid: where result will be saved
1313 */
1314static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1315{
1316 struct inode_smack *isp = inode->i_security;
1317
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001318 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001319}
1320
Casey Schauflere114e472008-02-04 22:29:50 -08001321/*
1322 * File Hooks
1323 */
1324
1325/**
1326 * smack_file_permission - Smack check on file operations
1327 * @file: unused
1328 * @mask: unused
1329 *
1330 * Returns 0
1331 *
1332 * Should access checks be done on each read or write?
1333 * UNICOS and SELinux say yes.
1334 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1335 *
1336 * I'll say no for now. Smack does not do the frequent
1337 * label changing that SELinux does.
1338 */
1339static int smack_file_permission(struct file *file, int mask)
1340{
1341 return 0;
1342}
1343
1344/**
1345 * smack_file_alloc_security - assign a file security blob
1346 * @file: the object
1347 *
1348 * The security blob for a file is a pointer to the master
1349 * label list, so no allocation is done.
1350 *
1351 * Returns 0
1352 */
1353static int smack_file_alloc_security(struct file *file)
1354{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001355 struct smack_known *skp = smk_of_current();
1356
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001357 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001358 return 0;
1359}
1360
1361/**
1362 * smack_file_free_security - clear a file security blob
1363 * @file: the object
1364 *
1365 * The security blob for a file is a pointer to the master
1366 * label list, so no memory is freed.
1367 */
1368static void smack_file_free_security(struct file *file)
1369{
1370 file->f_security = NULL;
1371}
1372
1373/**
1374 * smack_file_ioctl - Smack check on ioctls
1375 * @file: the object
1376 * @cmd: what to do
1377 * @arg: unused
1378 *
1379 * Relies heavily on the correct use of the ioctl command conventions.
1380 *
1381 * Returns 0 if allowed, error code otherwise
1382 */
1383static int smack_file_ioctl(struct file *file, unsigned int cmd,
1384 unsigned long arg)
1385{
1386 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001387 struct smk_audit_info ad;
1388
Eric Parisf48b7392011-04-25 12:54:27 -04001389 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001390 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001391
Casey Schauflerd166c802014-08-27 14:51:27 -07001392 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001393 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001394 rc = smk_bu_file(file, MAY_WRITE, rc);
1395 }
Casey Schauflere114e472008-02-04 22:29:50 -08001396
Casey Schauflerd166c802014-08-27 14:51:27 -07001397 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001398 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001399 rc = smk_bu_file(file, MAY_READ, rc);
1400 }
Casey Schauflere114e472008-02-04 22:29:50 -08001401
1402 return rc;
1403}
1404
1405/**
1406 * smack_file_lock - Smack check on file locking
1407 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001408 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001409 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001410 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001411 */
1412static int smack_file_lock(struct file *file, unsigned int cmd)
1413{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001414 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001415 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001416
Eric Paris92f42502011-04-25 13:15:55 -04001417 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1418 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001419 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1420 rc = smk_bu_file(file, MAY_LOCK, rc);
1421 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001422}
1423
1424/**
1425 * smack_file_fcntl - Smack check on fcntl
1426 * @file: the object
1427 * @cmd: what action to check
1428 * @arg: unused
1429 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001430 * Generally these operations are harmless.
1431 * File locking operations present an obvious mechanism
1432 * for passing information, so they require write access.
1433 *
Casey Schauflere114e472008-02-04 22:29:50 -08001434 * Returns 0 if current has access, error code otherwise
1435 */
1436static int smack_file_fcntl(struct file *file, unsigned int cmd,
1437 unsigned long arg)
1438{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001439 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001440 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001441
Etienne Bassetecfcc532009-04-08 20:40:06 +02001442
Casey Schauflere114e472008-02-04 22:29:50 -08001443 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001444 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001445 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001446 case F_SETLK:
1447 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001448 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1449 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1450 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001451 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001452 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001453 case F_SETOWN:
1454 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001455 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1456 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001457 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001458 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001459 break;
1460 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001461 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001462 }
1463
1464 return rc;
1465}
1466
1467/**
Al Viroe5467852012-05-30 13:30:51 -04001468 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001469 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1470 * if mapping anonymous memory.
1471 * @file contains the file structure for file to map (may be NULL).
1472 * @reqprot contains the protection requested by the application.
1473 * @prot contains the protection that will be applied by the kernel.
1474 * @flags contains the operational flags.
1475 * Return 0 if permission is granted.
1476 */
Al Viroe5467852012-05-30 13:30:51 -04001477static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001478 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001479 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001480{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001481 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001482 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001483 struct smack_rule *srp;
1484 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001485 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001486 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001487 int may;
1488 int mmay;
1489 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001490 int rc;
1491
Al Viro496ad9a2013-01-23 17:07:38 -05001492 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001493 return 0;
1494
Al Viro496ad9a2013-01-23 17:07:38 -05001495 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001496 if (isp->smk_mmap == NULL)
1497 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001498 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001499
1500 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001501 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001502 rc = 0;
1503
1504 rcu_read_lock();
1505 /*
1506 * For each Smack rule associated with the subject
1507 * label verify that the SMACK64MMAP also has access
1508 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001509 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001510 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001511 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001512 /*
1513 * Matching labels always allows access.
1514 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001515 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001516 continue;
1517 /*
1518 * If there is a matching local rule take
1519 * that into account as well.
1520 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001521 may = smk_access_entry(srp->smk_subject->smk_known,
1522 okp->smk_known,
1523 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001524 if (may == -ENOENT)
1525 may = srp->smk_access;
1526 else
1527 may &= srp->smk_access;
1528 /*
1529 * If may is zero the SMACK64MMAP subject can't
1530 * possibly have less access.
1531 */
1532 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001533 continue;
1534
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001535 /*
1536 * Fetch the global list entry.
1537 * If there isn't one a SMACK64MMAP subject
1538 * can't have as much access as current.
1539 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001540 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1541 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001542 if (mmay == -ENOENT) {
1543 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001544 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001545 }
1546 /*
1547 * If there is a local entry it modifies the
1548 * potential access, too.
1549 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001550 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1551 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001552 if (tmay != -ENOENT)
1553 mmay &= tmay;
1554
1555 /*
1556 * If there is any access available to current that is
1557 * not available to a SMACK64MMAP subject
1558 * deny access.
1559 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001560 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001561 rc = -EACCES;
1562 break;
1563 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001564 }
1565
1566 rcu_read_unlock();
1567
1568 return rc;
1569}
1570
1571/**
Casey Schauflere114e472008-02-04 22:29:50 -08001572 * smack_file_set_fowner - set the file security blob value
1573 * @file: object in question
1574 *
1575 * Returns 0
1576 * Further research may be required on this one.
1577 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001578static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001579{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001580 struct smack_known *skp = smk_of_current();
1581
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001582 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001583}
1584
1585/**
1586 * smack_file_send_sigiotask - Smack on sigio
1587 * @tsk: The target task
1588 * @fown: the object the signal come from
1589 * @signum: unused
1590 *
1591 * Allow a privileged task to get signals even if it shouldn't
1592 *
1593 * Returns 0 if a subject with the object's smack could
1594 * write to the task, an error code otherwise.
1595 */
1596static int smack_file_send_sigiotask(struct task_struct *tsk,
1597 struct fown_struct *fown, int signum)
1598{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001599 struct smack_known *skp;
1600 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001601 struct file *file;
1602 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001603 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001604
1605 /*
1606 * struct fown_struct is never outside the context of a struct file
1607 */
1608 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001609
Etienne Bassetecfcc532009-04-08 20:40:06 +02001610 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001611 skp = file->f_security;
1612 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1613 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001614 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001615 rc = 0;
1616
1617 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1618 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001619 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001620 return rc;
1621}
1622
1623/**
1624 * smack_file_receive - Smack file receive check
1625 * @file: the object
1626 *
1627 * Returns 0 if current has access, error code otherwise
1628 */
1629static int smack_file_receive(struct file *file)
1630{
Casey Schauflerd166c802014-08-27 14:51:27 -07001631 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001632 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001633 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001634
Casey Schaufler4482a442013-12-30 17:37:45 -08001635 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001636 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001637 /*
1638 * This code relies on bitmasks.
1639 */
1640 if (file->f_mode & FMODE_READ)
1641 may = MAY_READ;
1642 if (file->f_mode & FMODE_WRITE)
1643 may |= MAY_WRITE;
1644
Casey Schauflerd166c802014-08-27 14:51:27 -07001645 rc = smk_curacc(file->f_security, may, &ad);
1646 rc = smk_bu_file(file, may, rc);
1647 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001648}
1649
Casey Schaufler531f1d42011-09-19 12:41:42 -07001650/**
Eric Paris83d49852012-04-04 13:45:40 -04001651 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001652 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001653 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001654 *
1655 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001656 * Allow the open only if the task has read access. There are
1657 * many read operations (e.g. fstat) that you can do with an
1658 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001659 *
1660 * Returns 0
1661 */
Eric Paris83d49852012-04-04 13:45:40 -04001662static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001663{
Casey Schauflera6834c02014-04-21 11:10:26 -07001664 struct task_smack *tsp = cred->security;
Al Viro496ad9a2013-01-23 17:07:38 -05001665 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schauflera6834c02014-04-21 11:10:26 -07001666 struct smk_audit_info ad;
1667 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001668
Marcin Niesluchowskid83d2c22014-08-19 14:26:32 +02001669 if (smack_privileged(CAP_MAC_OVERRIDE)) {
1670 file->f_security = isp->smk_inode;
Casey Schauflera6834c02014-04-21 11:10:26 -07001671 return 0;
Marcin Niesluchowskid83d2c22014-08-19 14:26:32 +02001672 }
Casey Schaufler531f1d42011-09-19 12:41:42 -07001673
Casey Schauflera6834c02014-04-21 11:10:26 -07001674 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1675 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1676 rc = smk_access(tsp->smk_task, isp->smk_inode, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001677 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001678 if (rc == 0)
1679 file->f_security = isp->smk_inode;
1680
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
2219/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002220 * smk_ipv6_port_label - Smack port access table management
2221 * @sock: socket
2222 * @address: address
2223 *
2224 * Create or update the port list entry
2225 */
2226static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2227{
2228 struct sock *sk = sock->sk;
2229 struct sockaddr_in6 *addr6;
2230 struct socket_smack *ssp = sock->sk->sk_security;
2231 struct smk_port_label *spp;
2232 unsigned short port = 0;
2233
2234 if (address == NULL) {
2235 /*
2236 * This operation is changing the Smack information
2237 * on the bound socket. Take the changes to the port
2238 * as well.
2239 */
2240 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2241 if (sk != spp->smk_sock)
2242 continue;
2243 spp->smk_in = ssp->smk_in;
2244 spp->smk_out = ssp->smk_out;
2245 return;
2246 }
2247 /*
2248 * A NULL address is only used for updating existing
2249 * bound entries. If there isn't one, it's OK.
2250 */
2251 return;
2252 }
2253
2254 addr6 = (struct sockaddr_in6 *)address;
2255 port = ntohs(addr6->sin6_port);
2256 /*
2257 * This is a special case that is safely ignored.
2258 */
2259 if (port == 0)
2260 return;
2261
2262 /*
2263 * Look for an existing port list entry.
2264 * This is an indication that a port is getting reused.
2265 */
2266 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2267 if (spp->smk_port != port)
2268 continue;
2269 spp->smk_port = port;
2270 spp->smk_sock = sk;
2271 spp->smk_in = ssp->smk_in;
2272 spp->smk_out = ssp->smk_out;
2273 return;
2274 }
2275
2276 /*
2277 * A new port entry is required.
2278 */
2279 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2280 if (spp == NULL)
2281 return;
2282
2283 spp->smk_port = port;
2284 spp->smk_sock = sk;
2285 spp->smk_in = ssp->smk_in;
2286 spp->smk_out = ssp->smk_out;
2287
2288 list_add(&spp->list, &smk_ipv6_port_list);
2289 return;
2290}
2291
2292/**
2293 * smk_ipv6_port_check - check Smack port access
2294 * @sock: socket
2295 * @address: address
2296 *
2297 * Create or update the port list entry
2298 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002299static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002300 int act)
2301{
2302 __be16 *bep;
2303 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002304 struct smk_port_label *spp;
2305 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002306 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002307 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002308 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002309 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002310 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002311#ifdef CONFIG_AUDIT
2312 struct lsm_network_audit net;
2313#endif
2314
2315 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002316 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002317 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002318 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002319 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002320 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002321 }
2322
2323 /*
2324 * Get the IP address and port from the address.
2325 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002326 port = ntohs(address->sin6_port);
2327 bep = (__be16 *)(&address->sin6_addr);
2328 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002329
2330 /*
2331 * It's remote, so port lookup does no good.
2332 */
2333 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2334 goto auditout;
2335
2336 /*
2337 * It's local so the send check has to have passed.
2338 */
2339 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002340 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002341 goto auditout;
2342 }
2343
2344 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2345 if (spp->smk_port != port)
2346 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002347 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002348 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002349 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002350 break;
2351 }
2352
2353auditout:
2354
2355#ifdef CONFIG_AUDIT
2356 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2357 ad.a.u.net->family = sk->sk_family;
2358 ad.a.u.net->dport = port;
2359 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002360 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002361 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002362 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002363#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002364 rc = smk_access(skp, object, MAY_WRITE, &ad);
2365 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2366 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002367}
2368
2369/**
Casey Schauflere114e472008-02-04 22:29:50 -08002370 * smack_inode_setsecurity - set smack xattrs
2371 * @inode: the object
2372 * @name: attribute name
2373 * @value: attribute value
2374 * @size: size of the attribute
2375 * @flags: unused
2376 *
2377 * Sets the named attribute in the appropriate blob
2378 *
2379 * Returns 0 on success, or an error code
2380 */
2381static int smack_inode_setsecurity(struct inode *inode, const char *name,
2382 const void *value, size_t size, int flags)
2383{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002384 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002385 struct inode_smack *nsp = inode->i_security;
2386 struct socket_smack *ssp;
2387 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002388 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002389
Casey Schauflerf7112e62012-05-06 15:22:02 -07002390 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302391 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002392
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002393 skp = smk_import_entry(value, size);
2394 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002395 return -EINVAL;
2396
2397 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002398 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002399 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002400 return 0;
2401 }
2402 /*
2403 * The rest of the Smack xattrs are only on sockets.
2404 */
2405 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2406 return -EOPNOTSUPP;
2407
2408 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002409 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002410 return -EOPNOTSUPP;
2411
2412 ssp = sock->sk->sk_security;
2413
2414 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002415 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002416 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002417 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002418 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002419 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2420 if (rc != 0)
2421 printk(KERN_WARNING
2422 "Smack: \"%s\" netlbl error %d.\n",
2423 __func__, -rc);
2424 }
Casey Schauflere114e472008-02-04 22:29:50 -08002425 } else
2426 return -EOPNOTSUPP;
2427
Casey Schauflerc6739442013-05-22 18:42:56 -07002428 if (sock->sk->sk_family == PF_INET6)
2429 smk_ipv6_port_label(sock, NULL);
2430
Casey Schauflere114e472008-02-04 22:29:50 -08002431 return 0;
2432}
2433
2434/**
2435 * smack_socket_post_create - finish socket setup
2436 * @sock: the socket
2437 * @family: protocol family
2438 * @type: unused
2439 * @protocol: unused
2440 * @kern: unused
2441 *
2442 * Sets the netlabel information on the socket
2443 *
2444 * Returns 0 on success, and error code otherwise
2445 */
2446static int smack_socket_post_create(struct socket *sock, int family,
2447 int type, int protocol, int kern)
2448{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002449 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002450 return 0;
2451 /*
2452 * Set the outbound netlbl.
2453 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002454 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2455}
2456
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002457/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002458 * smack_socket_bind - record port binding information.
2459 * @sock: the socket
2460 * @address: the port address
2461 * @addrlen: size of the address
2462 *
2463 * Records the label bound to a port.
2464 *
2465 * Returns 0
2466 */
2467static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2468 int addrlen)
2469{
2470 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2471 smk_ipv6_port_label(sock, address);
2472
2473 return 0;
2474}
2475
2476/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002477 * smack_socket_connect - connect access check
2478 * @sock: the socket
2479 * @sap: the other end
2480 * @addrlen: size of sap
2481 *
2482 * Verifies that a connection may be possible
2483 *
2484 * Returns 0 on success, and error code otherwise
2485 */
2486static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2487 int addrlen)
2488{
Casey Schauflerc6739442013-05-22 18:42:56 -07002489 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002490
Casey Schauflerc6739442013-05-22 18:42:56 -07002491 if (sock->sk == NULL)
2492 return 0;
2493
2494 switch (sock->sk->sk_family) {
2495 case PF_INET:
2496 if (addrlen < sizeof(struct sockaddr_in))
2497 return -EINVAL;
2498 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2499 break;
2500 case PF_INET6:
2501 if (addrlen < sizeof(struct sockaddr_in6))
2502 return -EINVAL;
Casey Schaufler6ea06242013-08-05 13:21:22 -07002503 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2504 SMK_CONNECTING);
Casey Schauflerc6739442013-05-22 18:42:56 -07002505 break;
2506 }
2507 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002508}
2509
2510/**
2511 * smack_flags_to_may - convert S_ to MAY_ values
2512 * @flags: the S_ value
2513 *
2514 * Returns the equivalent MAY_ value
2515 */
2516static int smack_flags_to_may(int flags)
2517{
2518 int may = 0;
2519
2520 if (flags & S_IRUGO)
2521 may |= MAY_READ;
2522 if (flags & S_IWUGO)
2523 may |= MAY_WRITE;
2524 if (flags & S_IXUGO)
2525 may |= MAY_EXEC;
2526
2527 return may;
2528}
2529
2530/**
2531 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2532 * @msg: the object
2533 *
2534 * Returns 0
2535 */
2536static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2537{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002538 struct smack_known *skp = smk_of_current();
2539
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002540 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002541 return 0;
2542}
2543
2544/**
2545 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2546 * @msg: the object
2547 *
2548 * Clears the blob pointer
2549 */
2550static void smack_msg_msg_free_security(struct msg_msg *msg)
2551{
2552 msg->security = NULL;
2553}
2554
2555/**
2556 * smack_of_shm - the smack pointer for the shm
2557 * @shp: the object
2558 *
2559 * Returns a pointer to the smack value
2560 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002561static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002562{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002563 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002564}
2565
2566/**
2567 * smack_shm_alloc_security - Set the security blob for shm
2568 * @shp: the object
2569 *
2570 * Returns 0
2571 */
2572static int smack_shm_alloc_security(struct shmid_kernel *shp)
2573{
2574 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002575 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002576
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002577 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002578 return 0;
2579}
2580
2581/**
2582 * smack_shm_free_security - Clear the security blob for shm
2583 * @shp: the object
2584 *
2585 * Clears the blob pointer
2586 */
2587static void smack_shm_free_security(struct shmid_kernel *shp)
2588{
2589 struct kern_ipc_perm *isp = &shp->shm_perm;
2590
2591 isp->security = NULL;
2592}
2593
2594/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002595 * smk_curacc_shm : check if current has access on shm
2596 * @shp : the object
2597 * @access : access requested
2598 *
2599 * Returns 0 if current has the requested access, error code otherwise
2600 */
2601static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2602{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002603 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002604 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002605 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002606
2607#ifdef CONFIG_AUDIT
2608 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2609 ad.a.u.ipc_id = shp->shm_perm.id;
2610#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002611 rc = smk_curacc(ssp, access, &ad);
2612 rc = smk_bu_current("shm", ssp, access, rc);
2613 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002614}
2615
2616/**
Casey Schauflere114e472008-02-04 22:29:50 -08002617 * smack_shm_associate - Smack access check for shm
2618 * @shp: the object
2619 * @shmflg: access requested
2620 *
2621 * Returns 0 if current has the requested access, error code otherwise
2622 */
2623static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2624{
Casey Schauflere114e472008-02-04 22:29:50 -08002625 int may;
2626
2627 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002628 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002629}
2630
2631/**
2632 * smack_shm_shmctl - Smack access check for shm
2633 * @shp: the object
2634 * @cmd: what it wants to do
2635 *
2636 * Returns 0 if current has the requested access, error code otherwise
2637 */
2638static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2639{
Casey Schauflere114e472008-02-04 22:29:50 -08002640 int may;
2641
2642 switch (cmd) {
2643 case IPC_STAT:
2644 case SHM_STAT:
2645 may = MAY_READ;
2646 break;
2647 case IPC_SET:
2648 case SHM_LOCK:
2649 case SHM_UNLOCK:
2650 case IPC_RMID:
2651 may = MAY_READWRITE;
2652 break;
2653 case IPC_INFO:
2654 case SHM_INFO:
2655 /*
2656 * System level information.
2657 */
2658 return 0;
2659 default:
2660 return -EINVAL;
2661 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002662 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002663}
2664
2665/**
2666 * smack_shm_shmat - Smack access for shmat
2667 * @shp: the object
2668 * @shmaddr: unused
2669 * @shmflg: access requested
2670 *
2671 * Returns 0 if current has the requested access, error code otherwise
2672 */
2673static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2674 int shmflg)
2675{
Casey Schauflere114e472008-02-04 22:29:50 -08002676 int may;
2677
2678 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002679 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002680}
2681
2682/**
2683 * smack_of_sem - the smack pointer for the sem
2684 * @sma: the object
2685 *
2686 * Returns a pointer to the smack value
2687 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002688static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002689{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002690 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002691}
2692
2693/**
2694 * smack_sem_alloc_security - Set the security blob for sem
2695 * @sma: the object
2696 *
2697 * Returns 0
2698 */
2699static int smack_sem_alloc_security(struct sem_array *sma)
2700{
2701 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002702 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002703
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002704 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002705 return 0;
2706}
2707
2708/**
2709 * smack_sem_free_security - Clear the security blob for sem
2710 * @sma: the object
2711 *
2712 * Clears the blob pointer
2713 */
2714static void smack_sem_free_security(struct sem_array *sma)
2715{
2716 struct kern_ipc_perm *isp = &sma->sem_perm;
2717
2718 isp->security = NULL;
2719}
2720
2721/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002722 * smk_curacc_sem : check if current has access on sem
2723 * @sma : the object
2724 * @access : access requested
2725 *
2726 * Returns 0 if current has the requested access, error code otherwise
2727 */
2728static int smk_curacc_sem(struct sem_array *sma, int access)
2729{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002730 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002731 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002732 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002733
2734#ifdef CONFIG_AUDIT
2735 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2736 ad.a.u.ipc_id = sma->sem_perm.id;
2737#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002738 rc = smk_curacc(ssp, access, &ad);
2739 rc = smk_bu_current("sem", ssp, access, rc);
2740 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002741}
2742
2743/**
Casey Schauflere114e472008-02-04 22:29:50 -08002744 * smack_sem_associate - Smack access check for sem
2745 * @sma: the object
2746 * @semflg: access requested
2747 *
2748 * Returns 0 if current has the requested access, error code otherwise
2749 */
2750static int smack_sem_associate(struct sem_array *sma, int semflg)
2751{
Casey Schauflere114e472008-02-04 22:29:50 -08002752 int may;
2753
2754 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002755 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002756}
2757
2758/**
2759 * smack_sem_shmctl - Smack access check for sem
2760 * @sma: the object
2761 * @cmd: what it wants to do
2762 *
2763 * Returns 0 if current has the requested access, error code otherwise
2764 */
2765static int smack_sem_semctl(struct sem_array *sma, int cmd)
2766{
Casey Schauflere114e472008-02-04 22:29:50 -08002767 int may;
2768
2769 switch (cmd) {
2770 case GETPID:
2771 case GETNCNT:
2772 case GETZCNT:
2773 case GETVAL:
2774 case GETALL:
2775 case IPC_STAT:
2776 case SEM_STAT:
2777 may = MAY_READ;
2778 break;
2779 case SETVAL:
2780 case SETALL:
2781 case IPC_RMID:
2782 case IPC_SET:
2783 may = MAY_READWRITE;
2784 break;
2785 case IPC_INFO:
2786 case SEM_INFO:
2787 /*
2788 * System level information
2789 */
2790 return 0;
2791 default:
2792 return -EINVAL;
2793 }
2794
Etienne Bassetecfcc532009-04-08 20:40:06 +02002795 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002796}
2797
2798/**
2799 * smack_sem_semop - Smack checks of semaphore operations
2800 * @sma: the object
2801 * @sops: unused
2802 * @nsops: unused
2803 * @alter: unused
2804 *
2805 * Treated as read and write in all cases.
2806 *
2807 * Returns 0 if access is allowed, error code otherwise
2808 */
2809static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2810 unsigned nsops, int alter)
2811{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002812 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002813}
2814
2815/**
2816 * smack_msg_alloc_security - Set the security blob for msg
2817 * @msq: the object
2818 *
2819 * Returns 0
2820 */
2821static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2822{
2823 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002824 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002825
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002826 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002827 return 0;
2828}
2829
2830/**
2831 * smack_msg_free_security - Clear the security blob for msg
2832 * @msq: the object
2833 *
2834 * Clears the blob pointer
2835 */
2836static void smack_msg_queue_free_security(struct msg_queue *msq)
2837{
2838 struct kern_ipc_perm *kisp = &msq->q_perm;
2839
2840 kisp->security = NULL;
2841}
2842
2843/**
2844 * smack_of_msq - the smack pointer for the msq
2845 * @msq: the object
2846 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002847 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002848 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002849static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002850{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002851 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002852}
2853
2854/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002855 * smk_curacc_msq : helper to check if current has access on msq
2856 * @msq : the msq
2857 * @access : access requested
2858 *
2859 * return 0 if current has access, error otherwise
2860 */
2861static int smk_curacc_msq(struct msg_queue *msq, int access)
2862{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002863 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002864 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002865 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002866
2867#ifdef CONFIG_AUDIT
2868 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2869 ad.a.u.ipc_id = msq->q_perm.id;
2870#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002871 rc = smk_curacc(msp, access, &ad);
2872 rc = smk_bu_current("msq", msp, access, rc);
2873 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002874}
2875
2876/**
Casey Schauflere114e472008-02-04 22:29:50 -08002877 * smack_msg_queue_associate - Smack access check for msg_queue
2878 * @msq: the object
2879 * @msqflg: access requested
2880 *
2881 * Returns 0 if current has the requested access, error code otherwise
2882 */
2883static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2884{
Casey Schauflere114e472008-02-04 22:29:50 -08002885 int may;
2886
2887 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002888 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002889}
2890
2891/**
2892 * smack_msg_queue_msgctl - Smack access check for msg_queue
2893 * @msq: the object
2894 * @cmd: what it wants to do
2895 *
2896 * Returns 0 if current has the requested access, error code otherwise
2897 */
2898static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2899{
Casey Schauflere114e472008-02-04 22:29:50 -08002900 int may;
2901
2902 switch (cmd) {
2903 case IPC_STAT:
2904 case MSG_STAT:
2905 may = MAY_READ;
2906 break;
2907 case IPC_SET:
2908 case IPC_RMID:
2909 may = MAY_READWRITE;
2910 break;
2911 case IPC_INFO:
2912 case MSG_INFO:
2913 /*
2914 * System level information
2915 */
2916 return 0;
2917 default:
2918 return -EINVAL;
2919 }
2920
Etienne Bassetecfcc532009-04-08 20:40:06 +02002921 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002922}
2923
2924/**
2925 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2926 * @msq: the object
2927 * @msg: unused
2928 * @msqflg: access requested
2929 *
2930 * Returns 0 if current has the requested access, error code otherwise
2931 */
2932static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2933 int msqflg)
2934{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002935 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002936
Etienne Bassetecfcc532009-04-08 20:40:06 +02002937 may = smack_flags_to_may(msqflg);
2938 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002939}
2940
2941/**
2942 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2943 * @msq: the object
2944 * @msg: unused
2945 * @target: unused
2946 * @type: unused
2947 * @mode: unused
2948 *
2949 * Returns 0 if current has read and write access, error code otherwise
2950 */
2951static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2952 struct task_struct *target, long type, int mode)
2953{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002954 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002955}
2956
2957/**
2958 * smack_ipc_permission - Smack access for ipc_permission()
2959 * @ipp: the object permissions
2960 * @flag: access requested
2961 *
2962 * Returns 0 if current has read and write access, error code otherwise
2963 */
2964static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2965{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002966 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002967 int may = smack_flags_to_may(flag);
2968 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002969 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002970
Etienne Bassetecfcc532009-04-08 20:40:06 +02002971#ifdef CONFIG_AUDIT
2972 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2973 ad.a.u.ipc_id = ipp->id;
2974#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002975 rc = smk_curacc(iskp, may, &ad);
2976 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002977 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002978}
2979
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002980/**
2981 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002982 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002983 * @secid: where result will be saved
2984 */
2985static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2986{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002987 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002988
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002989 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002990}
2991
Casey Schauflere114e472008-02-04 22:29:50 -08002992/**
2993 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002994 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002995 * @inode: the object
2996 *
2997 * Set the inode's security blob if it hasn't been done already.
2998 */
2999static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3000{
3001 struct super_block *sbp;
3002 struct superblock_smack *sbsp;
3003 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003004 struct smack_known *skp;
3005 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003006 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003007 char trattr[TRANS_TRUE_SIZE];
3008 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003009 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003010 struct dentry *dp;
3011
3012 if (inode == NULL)
3013 return;
3014
3015 isp = inode->i_security;
3016
3017 mutex_lock(&isp->smk_lock);
3018 /*
3019 * If the inode is already instantiated
3020 * take the quick way out
3021 */
3022 if (isp->smk_flags & SMK_INODE_INSTANT)
3023 goto unlockandout;
3024
3025 sbp = inode->i_sb;
3026 sbsp = sbp->s_security;
3027 /*
3028 * We're going to use the superblock default label
3029 * if there's no label on the file.
3030 */
3031 final = sbsp->smk_default;
3032
3033 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003034 * If this is the root inode the superblock
3035 * may be in the process of initialization.
3036 * If that is the case use the root value out
3037 * of the superblock.
3038 */
3039 if (opt_dentry->d_parent == opt_dentry) {
Casey Schaufler36ea7352014-04-28 15:23:01 -07003040 if (sbp->s_magic == CGROUP_SUPER_MAGIC) {
3041 /*
3042 * The cgroup filesystem is never mounted,
3043 * so there's no opportunity to set the mount
3044 * options.
3045 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003046 sbsp->smk_root = &smack_known_star;
3047 sbsp->smk_default = &smack_known_star;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003048 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003049 isp->smk_inode = sbsp->smk_root;
3050 isp->smk_flags |= SMK_INODE_INSTANT;
3051 goto unlockandout;
3052 }
3053
3054 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003055 * This is pretty hackish.
3056 * Casey says that we shouldn't have to do
3057 * file system specific code, but it does help
3058 * with keeping it simple.
3059 */
3060 switch (sbp->s_magic) {
3061 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003062 case PIPEFS_MAGIC:
3063 case SOCKFS_MAGIC:
3064 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003065 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003066 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003067 * that the smack file system doesn't do
3068 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003069 *
Casey Schauflere114e472008-02-04 22:29:50 -08003070 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003071 *
3072 * Socket access is controlled by the socket
3073 * structures associated with the task involved.
3074 *
3075 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003076 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003077 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003078 break;
3079 case DEVPTS_SUPER_MAGIC:
3080 /*
3081 * devpts seems content with the label of the task.
3082 * Programs that change smack have to treat the
3083 * pty with respect.
3084 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003085 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003086 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003087 case PROC_SUPER_MAGIC:
3088 /*
3089 * Casey says procfs appears not to care.
3090 * The superblock default suffices.
3091 */
3092 break;
3093 case TMPFS_MAGIC:
3094 /*
3095 * Device labels should come from the filesystem,
3096 * but watch out, because they're volitile,
3097 * getting recreated on every reboot.
3098 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003099 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003100 /*
3101 * No break.
3102 *
3103 * If a smack value has been set we want to use it,
3104 * but since tmpfs isn't giving us the opportunity
3105 * to set mount options simulate setting the
3106 * superblock default.
3107 */
3108 default:
3109 /*
3110 * This isn't an understood special case.
3111 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003112 */
3113
3114 /*
3115 * UNIX domain sockets use lower level socket data.
3116 */
3117 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003118 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003119 break;
3120 }
3121 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003122 * No xattr support means, alas, no SMACK label.
3123 * Use the aforeapplied default.
3124 * It would be curious if the label of the task
3125 * does not match that assigned.
3126 */
3127 if (inode->i_op->getxattr == NULL)
3128 break;
3129 /*
3130 * Get the dentry for xattr.
3131 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003132 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003133 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3134 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003135 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003136
3137 /*
3138 * Transmuting directory
3139 */
3140 if (S_ISDIR(inode->i_mode)) {
3141 /*
3142 * If this is a new directory and the label was
3143 * transmuted when the inode was initialized
3144 * set the transmute attribute on the directory
3145 * and mark the inode.
3146 *
3147 * If there is a transmute attribute on the
3148 * directory mark the inode.
3149 */
3150 if (isp->smk_flags & SMK_INODE_CHANGED) {
3151 isp->smk_flags &= ~SMK_INODE_CHANGED;
3152 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003153 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003154 TRANS_TRUE, TRANS_TRUE_SIZE,
3155 0);
3156 } else {
3157 rc = inode->i_op->getxattr(dp,
3158 XATTR_NAME_SMACKTRANSMUTE, trattr,
3159 TRANS_TRUE_SIZE);
3160 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3161 TRANS_TRUE_SIZE) != 0)
3162 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003163 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003164 if (rc >= 0)
3165 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003166 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003167 /*
3168 * Don't let the exec or mmap label be "*" or "@".
3169 */
3170 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3171 if (skp == &smack_known_star || skp == &smack_known_web)
3172 skp = NULL;
3173 isp->smk_task = skp;
3174 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3175 if (skp == &smack_known_star || skp == &smack_known_web)
3176 skp = NULL;
3177 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003178
Casey Schauflere114e472008-02-04 22:29:50 -08003179 dput(dp);
3180 break;
3181 }
3182
3183 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003184 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003185 else
3186 isp->smk_inode = final;
3187
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003188 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003189
3190unlockandout:
3191 mutex_unlock(&isp->smk_lock);
3192 return;
3193}
3194
3195/**
3196 * smack_getprocattr - Smack process attribute access
3197 * @p: the object task
3198 * @name: the name of the attribute in /proc/.../attr
3199 * @value: where to put the result
3200 *
3201 * Places a copy of the task Smack into value
3202 *
3203 * Returns the length of the smack label or an error code
3204 */
3205static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3206{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003207 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08003208 char *cp;
3209 int slen;
3210
3211 if (strcmp(name, "current") != 0)
3212 return -EINVAL;
3213
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003214 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003215 if (cp == NULL)
3216 return -ENOMEM;
3217
3218 slen = strlen(cp);
3219 *value = cp;
3220 return slen;
3221}
3222
3223/**
3224 * smack_setprocattr - Smack process attribute setting
3225 * @p: the object task
3226 * @name: the name of the attribute in /proc/.../attr
3227 * @value: the value to set
3228 * @size: the size of the value
3229 *
3230 * Sets the Smack value of the task. Only setting self
3231 * is permitted and only with privilege
3232 *
3233 * Returns the length of the smack label or an error code
3234 */
3235static int smack_setprocattr(struct task_struct *p, char *name,
3236 void *value, size_t size)
3237{
Casey Schaufler676dac42010-12-02 06:43:39 -08003238 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003239 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003240 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003241
Casey Schauflere114e472008-02-04 22:29:50 -08003242 /*
3243 * Changing another process' Smack value is too dangerous
3244 * and supports no sane use case.
3245 */
3246 if (p != current)
3247 return -EPERM;
3248
Casey Schaufler1880eff2012-06-05 15:28:30 -07003249 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003250 return -EPERM;
3251
Casey Schauflerf7112e62012-05-06 15:22:02 -07003252 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003253 return -EINVAL;
3254
3255 if (strcmp(name, "current") != 0)
3256 return -EINVAL;
3257
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003258 skp = smk_import_entry(value, size);
3259 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003260 return -EINVAL;
3261
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003262 /*
3263 * No process is ever allowed the web ("@") label.
3264 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003265 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003266 return -EPERM;
3267
David Howellsd84f4f92008-11-14 10:39:23 +11003268 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003269 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003270 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003271
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003272 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003273 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003274
David Howellsd84f4f92008-11-14 10:39:23 +11003275 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003276 return size;
3277}
3278
3279/**
3280 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003281 * @sock: one sock
3282 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003283 * @newsk: unused
3284 *
3285 * Return 0 if a subject with the smack of sock could access
3286 * an object with the smack of other, otherwise an error code
3287 */
David S. Miller3610cda2011-01-05 15:38:53 -08003288static int smack_unix_stream_connect(struct sock *sock,
3289 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003290{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003291 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003292 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003293 struct socket_smack *ssp = sock->sk_security;
3294 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003295 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003296 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003297 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003298#ifdef CONFIG_AUDIT
3299 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003300#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003301
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003302 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3303 skp = ssp->smk_out;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003304 okp = osp->smk_out;
3305#ifdef CONFIG_AUDIT
3306 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3307 smk_ad_setfield_u_net_sk(&ad, other);
3308#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003309 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3310 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003311 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003312 rc = smk_access(okp, skp, MAY_WRITE, NULL);
3313 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003314 MAY_WRITE, rc);
3315 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003316 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003317
Casey Schaufler975d5e52011-09-26 14:43:39 -07003318 /*
3319 * Cross reference the peer labels for SO_PEERSEC.
3320 */
3321 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003322 nsp->smk_packet = ssp->smk_out;
3323 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003324 }
3325
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003326 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003327}
3328
3329/**
3330 * smack_unix_may_send - Smack access on UDS
3331 * @sock: one socket
3332 * @other: the other socket
3333 *
3334 * Return 0 if a subject with the smack of sock could access
3335 * an object with the smack of other, otherwise an error code
3336 */
3337static int smack_unix_may_send(struct socket *sock, struct socket *other)
3338{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003339 struct socket_smack *ssp = sock->sk->sk_security;
3340 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003341 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003342 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003343
Kees Cook923e9a12012-04-10 13:26:44 -07003344#ifdef CONFIG_AUDIT
3345 struct lsm_network_audit net;
3346
Eric Paris48c62af2012-04-02 13:15:44 -04003347 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003348 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003349#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003350
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003351 if (smack_privileged(CAP_MAC_OVERRIDE))
3352 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003353
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003354 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3355 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003356 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003357}
3358
3359/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003360 * smack_socket_sendmsg - Smack check based on destination host
3361 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003362 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003363 * @size: the size of the message
3364 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003365 * Return 0 if the current subject can write to the destination host.
3366 * For IPv4 this is only a question if the destination is a single label host.
3367 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003368 */
3369static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3370 int size)
3371{
3372 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003373 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003374 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003375
3376 /*
3377 * Perfectly reasonable for this to be NULL
3378 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003379 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003380 return 0;
3381
Casey Schauflerc6739442013-05-22 18:42:56 -07003382 switch (sip->sin_family) {
3383 case AF_INET:
3384 rc = smack_netlabel_send(sock->sk, sip);
3385 break;
3386 case AF_INET6:
3387 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3388 break;
3389 }
3390 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003391}
3392
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003393/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003394 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003395 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003396 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003397 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003398 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003399 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003400static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3401 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003402{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003403 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003404 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003405 int acat;
3406 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003407
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003408 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003409 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003410 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003411 * If there are flags but no level netlabel isn't
3412 * behaving the way we expect it to.
3413 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003414 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003415 * Without guidance regarding the smack value
3416 * for the packet fall back on the network
3417 * ambient value.
3418 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003419 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003420 list_for_each_entry(skp, &smack_known_list, list) {
3421 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003422 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003423 /*
3424 * Compare the catsets. Use the netlbl APIs.
3425 */
3426 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3427 if ((skp->smk_netlabel.flags &
3428 NETLBL_SECATTR_MLS_CAT) == 0)
3429 found = 1;
3430 break;
3431 }
3432 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003433 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3434 acat + 1);
3435 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003436 skp->smk_netlabel.attr.mls.cat,
3437 kcat + 1);
3438 if (acat < 0 || kcat < 0)
3439 break;
3440 }
3441 if (acat == kcat) {
3442 found = 1;
3443 break;
3444 }
Casey Schauflere114e472008-02-04 22:29:50 -08003445 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003446 rcu_read_unlock();
3447
3448 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003449 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003450
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003451 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003452 return &smack_known_web;
3453 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003454 }
3455 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3456 /*
3457 * Looks like a fallback, which gives us a secid.
3458 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003459 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003460 /*
3461 * This has got to be a bug because it is
3462 * impossible to specify a fallback without
3463 * specifying the label, which will ensure
3464 * it has a secid, and the only way to get a
3465 * secid is from a fallback.
3466 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003467 BUG_ON(skp == NULL);
3468 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003469 }
3470 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003471 * Without guidance regarding the smack value
3472 * for the packet fall back on the network
3473 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003474 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003475 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003476}
3477
Casey Schaufler6ea06242013-08-05 13:21:22 -07003478static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003479{
Casey Schauflerc6739442013-05-22 18:42:56 -07003480 u8 nexthdr;
3481 int offset;
3482 int proto = -EINVAL;
3483 struct ipv6hdr _ipv6h;
3484 struct ipv6hdr *ip6;
3485 __be16 frag_off;
3486 struct tcphdr _tcph, *th;
3487 struct udphdr _udph, *uh;
3488 struct dccp_hdr _dccph, *dh;
3489
3490 sip->sin6_port = 0;
3491
3492 offset = skb_network_offset(skb);
3493 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3494 if (ip6 == NULL)
3495 return -EINVAL;
3496 sip->sin6_addr = ip6->saddr;
3497
3498 nexthdr = ip6->nexthdr;
3499 offset += sizeof(_ipv6h);
3500 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3501 if (offset < 0)
3502 return -EINVAL;
3503
3504 proto = nexthdr;
3505 switch (proto) {
3506 case IPPROTO_TCP:
3507 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3508 if (th != NULL)
3509 sip->sin6_port = th->source;
3510 break;
3511 case IPPROTO_UDP:
3512 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3513 if (uh != NULL)
3514 sip->sin6_port = uh->source;
3515 break;
3516 case IPPROTO_DCCP:
3517 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3518 if (dh != NULL)
3519 sip->sin6_port = dh->dccph_sport;
3520 break;
3521 }
3522 return proto;
3523}
3524
Casey Schauflere114e472008-02-04 22:29:50 -08003525/**
3526 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3527 * @sk: socket
3528 * @skb: packet
3529 *
3530 * Returns 0 if the packet should be delivered, an error code otherwise
3531 */
3532static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3533{
3534 struct netlbl_lsm_secattr secattr;
3535 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003536 struct smack_known *skp;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003537 struct sockaddr_in6 sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003538 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003539 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003540#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003541 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003542#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003543 switch (sk->sk_family) {
3544 case PF_INET:
3545 /*
3546 * Translate what netlabel gave us.
3547 */
3548 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003549
Casey Schauflerc6739442013-05-22 18:42:56 -07003550 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3551 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003552 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003553 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003554 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003555
Casey Schauflerc6739442013-05-22 18:42:56 -07003556 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003557
Etienne Bassetecfcc532009-04-08 20:40:06 +02003558#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003559 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3560 ad.a.u.net->family = sk->sk_family;
3561 ad.a.u.net->netif = skb->skb_iif;
3562 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003563#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003564 /*
3565 * Receiving a packet requires that the other end
3566 * be able to write here. Read access is not required.
3567 * This is the simplist possible security model
3568 * for networking.
3569 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003570 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3571 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003572 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003573 if (rc != 0)
3574 netlbl_skbuff_err(skb, rc, 0);
3575 break;
3576 case PF_INET6:
3577 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3578 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3579 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3580 else
3581 rc = 0;
3582 break;
3583 }
Paul Moorea8134292008-10-10 10:16:31 -04003584 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003585}
3586
3587/**
3588 * smack_socket_getpeersec_stream - pull in packet label
3589 * @sock: the socket
3590 * @optval: user's destination
3591 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003592 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003593 *
3594 * returns zero on success, an error code otherwise
3595 */
3596static int smack_socket_getpeersec_stream(struct socket *sock,
3597 char __user *optval,
3598 int __user *optlen, unsigned len)
3599{
3600 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003601 char *rcp = "";
3602 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003603 int rc = 0;
3604
3605 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003606 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003607 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003608 slen = strlen(rcp) + 1;
3609 }
Casey Schauflere114e472008-02-04 22:29:50 -08003610
3611 if (slen > len)
3612 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003613 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003614 rc = -EFAULT;
3615
3616 if (put_user(slen, optlen) != 0)
3617 rc = -EFAULT;
3618
3619 return rc;
3620}
3621
3622
3623/**
3624 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003625 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003626 * @skb: packet data
3627 * @secid: pointer to where to put the secid of the packet
3628 *
3629 * Sets the netlabel socket state on sk from parent
3630 */
3631static int smack_socket_getpeersec_dgram(struct socket *sock,
3632 struct sk_buff *skb, u32 *secid)
3633
3634{
3635 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003636 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003637 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003638 int family = PF_UNSPEC;
3639 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003640 int rc;
3641
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003642 if (skb != NULL) {
3643 if (skb->protocol == htons(ETH_P_IP))
3644 family = PF_INET;
3645 else if (skb->protocol == htons(ETH_P_IPV6))
3646 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003647 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003648 if (family == PF_UNSPEC && sock != NULL)
3649 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003650
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003651 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003652 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003653 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003654 } else if (family == PF_INET || family == PF_INET6) {
3655 /*
3656 * Translate what netlabel gave us.
3657 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003658 if (sock != NULL && sock->sk != NULL)
3659 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003660 netlbl_secattr_init(&secattr);
3661 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3662 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003663 skp = smack_from_secattr(&secattr, ssp);
3664 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003665 }
3666 netlbl_secattr_destroy(&secattr);
3667 }
3668 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003669 if (s == 0)
3670 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003671 return 0;
3672}
3673
3674/**
Paul Moore07feee82009-03-27 17:10:54 -04003675 * smack_sock_graft - Initialize a newly created socket with an existing sock
3676 * @sk: child sock
3677 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003678 *
Paul Moore07feee82009-03-27 17:10:54 -04003679 * Set the smk_{in,out} state of an existing sock based on the process that
3680 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003681 */
3682static void smack_sock_graft(struct sock *sk, struct socket *parent)
3683{
3684 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003685 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003686
Paul Moore07feee82009-03-27 17:10:54 -04003687 if (sk == NULL ||
3688 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003689 return;
3690
3691 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003692 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003693 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003694 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003695}
3696
3697/**
3698 * smack_inet_conn_request - Smack access check on connect
3699 * @sk: socket involved
3700 * @skb: packet
3701 * @req: unused
3702 *
3703 * Returns 0 if a task with the packet label could write to
3704 * the socket, otherwise an error code
3705 */
3706static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3707 struct request_sock *req)
3708{
Paul Moore07feee82009-03-27 17:10:54 -04003709 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003710 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003711 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003712 struct netlbl_lsm_secattr secattr;
3713 struct sockaddr_in addr;
3714 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003715 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003716 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003717 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003718#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003719 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003720#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003721
Casey Schauflerc6739442013-05-22 18:42:56 -07003722 if (family == PF_INET6) {
3723 /*
3724 * Handle mapped IPv4 packets arriving
3725 * via IPv6 sockets. Don't set up netlabel
3726 * processing on IPv6.
3727 */
3728 if (skb->protocol == htons(ETH_P_IP))
3729 family = PF_INET;
3730 else
3731 return 0;
3732 }
Casey Schauflere114e472008-02-04 22:29:50 -08003733
Paul Moore07feee82009-03-27 17:10:54 -04003734 netlbl_secattr_init(&secattr);
3735 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003736 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003737 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003738 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003739 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003740 netlbl_secattr_destroy(&secattr);
3741
Etienne Bassetecfcc532009-04-08 20:40:06 +02003742#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003743 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3744 ad.a.u.net->family = family;
3745 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003746 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3747#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003748 /*
Paul Moore07feee82009-03-27 17:10:54 -04003749 * Receiving a packet requires that the other end be able to write
3750 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003751 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003752 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3753 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003754 if (rc != 0)
3755 return rc;
3756
3757 /*
3758 * Save the peer's label in the request_sock so we can later setup
3759 * smk_packet in the child socket so that SO_PEERCRED can report it.
3760 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003761 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003762
3763 /*
3764 * We need to decide if we want to label the incoming connection here
3765 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003766 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003767 */
3768 hdr = ip_hdr(skb);
3769 addr.sin_addr.s_addr = hdr->saddr;
3770 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003771 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003772 rcu_read_unlock();
3773
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003774 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003775 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003776 else
Paul Moore07feee82009-03-27 17:10:54 -04003777 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003778
3779 return rc;
3780}
3781
Paul Moore07feee82009-03-27 17:10:54 -04003782/**
3783 * smack_inet_csk_clone - Copy the connection information to the new socket
3784 * @sk: the new socket
3785 * @req: the connection's request_sock
3786 *
3787 * Transfer the connection's peer label to the newly created socket.
3788 */
3789static void smack_inet_csk_clone(struct sock *sk,
3790 const struct request_sock *req)
3791{
3792 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003793 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003794
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003795 if (req->peer_secid != 0) {
3796 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003797 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003798 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003799 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003800}
3801
Casey Schauflere114e472008-02-04 22:29:50 -08003802/*
3803 * Key management security hooks
3804 *
3805 * Casey has not tested key support very heavily.
3806 * The permission check is most likely too restrictive.
3807 * If you care about keys please have a look.
3808 */
3809#ifdef CONFIG_KEYS
3810
3811/**
3812 * smack_key_alloc - Set the key security blob
3813 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003814 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003815 * @flags: unused
3816 *
3817 * No allocation required
3818 *
3819 * Returns 0
3820 */
David Howellsd84f4f92008-11-14 10:39:23 +11003821static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003822 unsigned long flags)
3823{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003824 struct smack_known *skp = smk_of_task(cred->security);
3825
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003826 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003827 return 0;
3828}
3829
3830/**
3831 * smack_key_free - Clear the key security blob
3832 * @key: the object
3833 *
3834 * Clear the blob pointer
3835 */
3836static void smack_key_free(struct key *key)
3837{
3838 key->security = NULL;
3839}
3840
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003841/**
Casey Schauflere114e472008-02-04 22:29:50 -08003842 * smack_key_permission - Smack access on a key
3843 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003844 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01003845 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08003846 *
3847 * Return 0 if the task has read and write to the object,
3848 * an error code otherwise
3849 */
3850static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00003851 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003852{
3853 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003854 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003855 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003856 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07003857 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003858
3859 keyp = key_ref_to_ptr(key_ref);
3860 if (keyp == NULL)
3861 return -EINVAL;
3862 /*
3863 * If the key hasn't been initialized give it access so that
3864 * it may do so.
3865 */
3866 if (keyp->security == NULL)
3867 return 0;
3868 /*
3869 * This should not occur
3870 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003871 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003872 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003873#ifdef CONFIG_AUDIT
3874 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3875 ad.a.u.key_struct.key = keyp->serial;
3876 ad.a.u.key_struct.key_desc = keyp->description;
3877#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003878 if (perm & KEY_NEED_READ)
3879 request = MAY_READ;
3880 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3881 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07003882 rc = smk_access(tkp, keyp->security, request, &ad);
3883 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
3884 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003885}
3886#endif /* CONFIG_KEYS */
3887
3888/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003889 * Smack Audit hooks
3890 *
3891 * Audit requires a unique representation of each Smack specific
3892 * rule. This unique representation is used to distinguish the
3893 * object to be audited from remaining kernel objects and also
3894 * works as a glue between the audit hooks.
3895 *
3896 * Since repository entries are added but never deleted, we'll use
3897 * the smack_known label address related to the given audit rule as
3898 * the needed unique representation. This also better fits the smack
3899 * model where nearly everything is a label.
3900 */
3901#ifdef CONFIG_AUDIT
3902
3903/**
3904 * smack_audit_rule_init - Initialize a smack audit rule
3905 * @field: audit rule fields given from user-space (audit.h)
3906 * @op: required testing operator (=, !=, >, <, ...)
3907 * @rulestr: smack label to be audited
3908 * @vrule: pointer to save our own audit rule representation
3909 *
3910 * Prepare to audit cases where (@field @op @rulestr) is true.
3911 * The label to be audited is created if necessay.
3912 */
3913static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3914{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003915 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003916 char **rule = (char **)vrule;
3917 *rule = NULL;
3918
3919 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3920 return -EINVAL;
3921
Al Viro5af75d82008-12-16 05:59:26 -05003922 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003923 return -EINVAL;
3924
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003925 skp = smk_import_entry(rulestr, 0);
3926 if (skp)
3927 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003928
3929 return 0;
3930}
3931
3932/**
3933 * smack_audit_rule_known - Distinguish Smack audit rules
3934 * @krule: rule of interest, in Audit kernel representation format
3935 *
3936 * This is used to filter Smack rules from remaining Audit ones.
3937 * If it's proved that this rule belongs to us, the
3938 * audit_rule_match hook will be called to do the final judgement.
3939 */
3940static int smack_audit_rule_known(struct audit_krule *krule)
3941{
3942 struct audit_field *f;
3943 int i;
3944
3945 for (i = 0; i < krule->field_count; i++) {
3946 f = &krule->fields[i];
3947
3948 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3949 return 1;
3950 }
3951
3952 return 0;
3953}
3954
3955/**
3956 * smack_audit_rule_match - Audit given object ?
3957 * @secid: security id for identifying the object to test
3958 * @field: audit rule flags given from user-space
3959 * @op: required testing operator
3960 * @vrule: smack internal rule presentation
3961 * @actx: audit context associated with the check
3962 *
3963 * The core Audit hook. It's used to take the decision of
3964 * whether to audit or not to audit a given object.
3965 */
3966static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3967 struct audit_context *actx)
3968{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003969 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003970 char *rule = vrule;
3971
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05003972 if (unlikely(!rule)) {
3973 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003974 return -ENOENT;
3975 }
3976
3977 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3978 return 0;
3979
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003980 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003981
3982 /*
3983 * No need to do string comparisons. If a match occurs,
3984 * both pointers will point to the same smack_known
3985 * label.
3986 */
Al Viro5af75d82008-12-16 05:59:26 -05003987 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003988 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003989 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003990 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003991
3992 return 0;
3993}
3994
3995/**
3996 * smack_audit_rule_free - free smack rule representation
3997 * @vrule: rule to be freed.
3998 *
3999 * No memory was allocated.
4000 */
4001static void smack_audit_rule_free(void *vrule)
4002{
4003 /* No-op */
4004}
4005
4006#endif /* CONFIG_AUDIT */
4007
Randy Dunlap251a2a92009-02-18 11:42:33 -08004008/**
David Quigley746df9b2013-05-22 12:50:35 -04004009 * smack_ismaclabel - check if xattr @name references a smack MAC label
4010 * @name: Full xattr name to check.
4011 */
4012static int smack_ismaclabel(const char *name)
4013{
4014 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4015}
4016
4017
4018/**
Casey Schauflere114e472008-02-04 22:29:50 -08004019 * smack_secid_to_secctx - return the smack label for a secid
4020 * @secid: incoming integer
4021 * @secdata: destination
4022 * @seclen: how long it is
4023 *
4024 * Exists for networking code.
4025 */
4026static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4027{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004028 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004029
Eric Parisd5630b92010-10-13 16:24:48 -04004030 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004031 *secdata = skp->smk_known;
4032 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004033 return 0;
4034}
4035
Randy Dunlap251a2a92009-02-18 11:42:33 -08004036/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004037 * smack_secctx_to_secid - return the secid for a smack label
4038 * @secdata: smack label
4039 * @seclen: how long result is
4040 * @secid: outgoing integer
4041 *
4042 * Exists for audit and networking code.
4043 */
David Howellse52c17642008-04-29 20:52:51 +01004044static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004045{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004046 struct smack_known *skp = smk_find_entry(secdata);
4047
4048 if (skp)
4049 *secid = skp->smk_secid;
4050 else
4051 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004052 return 0;
4053}
4054
Randy Dunlap251a2a92009-02-18 11:42:33 -08004055/**
Casey Schauflere114e472008-02-04 22:29:50 -08004056 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004057 * @secdata: unused
4058 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004059 *
4060 * Exists to make sure nothing gets done, and properly
4061 */
4062static void smack_release_secctx(char *secdata, u32 seclen)
4063{
4064}
4065
David P. Quigley1ee65e32009-09-03 14:25:57 -04004066static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4067{
4068 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4069}
4070
4071static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4072{
4073 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4074}
4075
4076static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4077{
4078 int len = 0;
4079 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4080
4081 if (len < 0)
4082 return len;
4083 *ctxlen = len;
4084 return 0;
4085}
4086
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02004087struct security_operations smack_ops = {
4088 .name = "smack",
4089
Ingo Molnar9e488582009-05-07 19:26:19 +10004090 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01004091 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08004092 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08004093
4094 .sb_alloc_security = smack_sb_alloc_security,
4095 .sb_free_security = smack_sb_free_security,
4096 .sb_copy_data = smack_sb_copy_data,
4097 .sb_kern_mount = smack_sb_kern_mount,
4098 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08004099
Casey Schaufler676dac42010-12-02 06:43:39 -08004100 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03004101 .bprm_committing_creds = smack_bprm_committing_creds,
4102 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08004103
Casey Schauflere114e472008-02-04 22:29:50 -08004104 .inode_alloc_security = smack_inode_alloc_security,
4105 .inode_free_security = smack_inode_free_security,
4106 .inode_init_security = smack_inode_init_security,
4107 .inode_link = smack_inode_link,
4108 .inode_unlink = smack_inode_unlink,
4109 .inode_rmdir = smack_inode_rmdir,
4110 .inode_rename = smack_inode_rename,
4111 .inode_permission = smack_inode_permission,
4112 .inode_setattr = smack_inode_setattr,
4113 .inode_getattr = smack_inode_getattr,
4114 .inode_setxattr = smack_inode_setxattr,
4115 .inode_post_setxattr = smack_inode_post_setxattr,
4116 .inode_getxattr = smack_inode_getxattr,
4117 .inode_removexattr = smack_inode_removexattr,
4118 .inode_getsecurity = smack_inode_getsecurity,
4119 .inode_setsecurity = smack_inode_setsecurity,
4120 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004121 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004122
4123 .file_permission = smack_file_permission,
4124 .file_alloc_security = smack_file_alloc_security,
4125 .file_free_security = smack_file_free_security,
4126 .file_ioctl = smack_file_ioctl,
4127 .file_lock = smack_file_lock,
4128 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04004129 .mmap_file = smack_mmap_file,
4130 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08004131 .file_set_fowner = smack_file_set_fowner,
4132 .file_send_sigiotask = smack_file_send_sigiotask,
4133 .file_receive = smack_file_receive,
4134
Eric Paris83d49852012-04-04 13:45:40 -04004135 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07004136
David Howellsee18d642009-09-02 09:14:21 +01004137 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11004138 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11004139 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01004140 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11004141 .kernel_act_as = smack_kernel_act_as,
4142 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08004143 .task_setpgid = smack_task_setpgid,
4144 .task_getpgid = smack_task_getpgid,
4145 .task_getsid = smack_task_getsid,
4146 .task_getsecid = smack_task_getsecid,
4147 .task_setnice = smack_task_setnice,
4148 .task_setioprio = smack_task_setioprio,
4149 .task_getioprio = smack_task_getioprio,
4150 .task_setscheduler = smack_task_setscheduler,
4151 .task_getscheduler = smack_task_getscheduler,
4152 .task_movememory = smack_task_movememory,
4153 .task_kill = smack_task_kill,
4154 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08004155 .task_to_inode = smack_task_to_inode,
4156
4157 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004158 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004159
4160 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4161 .msg_msg_free_security = smack_msg_msg_free_security,
4162
4163 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4164 .msg_queue_free_security = smack_msg_queue_free_security,
4165 .msg_queue_associate = smack_msg_queue_associate,
4166 .msg_queue_msgctl = smack_msg_queue_msgctl,
4167 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4168 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4169
4170 .shm_alloc_security = smack_shm_alloc_security,
4171 .shm_free_security = smack_shm_free_security,
4172 .shm_associate = smack_shm_associate,
4173 .shm_shmctl = smack_shm_shmctl,
4174 .shm_shmat = smack_shm_shmat,
4175
4176 .sem_alloc_security = smack_sem_alloc_security,
4177 .sem_free_security = smack_sem_free_security,
4178 .sem_associate = smack_sem_associate,
4179 .sem_semctl = smack_sem_semctl,
4180 .sem_semop = smack_sem_semop,
4181
Casey Schauflere114e472008-02-04 22:29:50 -08004182 .d_instantiate = smack_d_instantiate,
4183
4184 .getprocattr = smack_getprocattr,
4185 .setprocattr = smack_setprocattr,
4186
4187 .unix_stream_connect = smack_unix_stream_connect,
4188 .unix_may_send = smack_unix_may_send,
4189
4190 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07004191 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004192 .socket_connect = smack_socket_connect,
4193 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08004194 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4195 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4196 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4197 .sk_alloc_security = smack_sk_alloc_security,
4198 .sk_free_security = smack_sk_free_security,
4199 .sock_graft = smack_sock_graft,
4200 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04004201 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004202
Casey Schauflere114e472008-02-04 22:29:50 -08004203 /* key management security hooks */
4204#ifdef CONFIG_KEYS
4205 .key_alloc = smack_key_alloc,
4206 .key_free = smack_key_free,
4207 .key_permission = smack_key_permission,
4208#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004209
4210 /* Audit hooks */
4211#ifdef CONFIG_AUDIT
4212 .audit_rule_init = smack_audit_rule_init,
4213 .audit_rule_known = smack_audit_rule_known,
4214 .audit_rule_match = smack_audit_rule_match,
4215 .audit_rule_free = smack_audit_rule_free,
4216#endif /* CONFIG_AUDIT */
4217
David Quigley746df9b2013-05-22 12:50:35 -04004218 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08004219 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004220 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08004221 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04004222 .inode_notifysecctx = smack_inode_notifysecctx,
4223 .inode_setsecctx = smack_inode_setsecctx,
4224 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08004225};
4226
Etienne Basset7198e2e2009-03-24 20:53:24 +01004227
Casey Schaufler86812bb2012-04-17 18:55:46 -07004228static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004229{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004230 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004231 * Initialize rule list locks
4232 */
4233 mutex_init(&smack_known_huh.smk_rules_lock);
4234 mutex_init(&smack_known_hat.smk_rules_lock);
4235 mutex_init(&smack_known_floor.smk_rules_lock);
4236 mutex_init(&smack_known_star.smk_rules_lock);
4237 mutex_init(&smack_known_invalid.smk_rules_lock);
4238 mutex_init(&smack_known_web.smk_rules_lock);
4239 /*
4240 * Initialize rule lists
4241 */
4242 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4243 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4244 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4245 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4246 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4247 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4248 /*
4249 * Create the known labels list
4250 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004251 smk_insert_entry(&smack_known_huh);
4252 smk_insert_entry(&smack_known_hat);
4253 smk_insert_entry(&smack_known_star);
4254 smk_insert_entry(&smack_known_floor);
4255 smk_insert_entry(&smack_known_invalid);
4256 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004257}
4258
Casey Schauflere114e472008-02-04 22:29:50 -08004259/**
4260 * smack_init - initialize the smack system
4261 *
4262 * Returns 0
4263 */
4264static __init int smack_init(void)
4265{
David Howellsd84f4f92008-11-14 10:39:23 +11004266 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004267 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004268
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004269 if (!security_module_enable(&smack_ops))
4270 return 0;
4271
Rohit1a5b4722014-10-15 17:40:41 +05304272 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4273 if (!smack_inode_cache)
4274 return -ENOMEM;
4275
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004276 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4277 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304278 if (tsp == NULL) {
4279 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004280 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304281 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004282
Casey Schauflere114e472008-02-04 22:29:50 -08004283 printk(KERN_INFO "Smack: Initializing.\n");
4284
4285 /*
4286 * Set the security state for the initial task.
4287 */
David Howellsd84f4f92008-11-14 10:39:23 +11004288 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004289 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004290
Casey Schaufler86812bb2012-04-17 18:55:46 -07004291 /* initialize the smack_known_list */
4292 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004293
4294 /*
4295 * Register with LSM
4296 */
4297 if (register_security(&smack_ops))
4298 panic("smack: Unable to register with kernel.\n");
4299
4300 return 0;
4301}
4302
4303/*
4304 * Smack requires early initialization in order to label
4305 * all processes and objects when they are created.
4306 */
4307security_initcall(smack_init);