blob: 2717cdd7872c30329773c0ffc8879a36dcb840aa [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);
170 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %s) %s\n",
171 sskp->smk_known, (char *)file->f_security, acc,
172 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
173 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);
193 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %s) %s\n",
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200194 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700195 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
196 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.
205 * @ip: a pointer to the inode
206 * @dp: a pointer to the dentry
207 *
208 * Returns a pointer to the master list entry for the Smack label
209 * or NULL if there was no label to fetch.
210 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700211static struct smack_known *smk_fetch(const char *name, struct inode *ip,
212 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800213{
214 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700215 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700216 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800217
218 if (ip->i_op->getxattr == NULL)
219 return NULL;
220
Casey Schauflerf7112e62012-05-06 15:22:02 -0700221 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
222 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800223 return NULL;
224
Casey Schauflerf7112e62012-05-06 15:22:02 -0700225 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
226 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700227 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700228
229 kfree(buffer);
230
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700231 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800232}
233
234/**
235 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200236 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800237 *
238 * Returns the new blob or NULL if there's no memory available
239 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200240struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800241{
242 struct inode_smack *isp;
243
Rohit1a5b4722014-10-15 17:40:41 +0530244 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800245 if (isp == NULL)
246 return NULL;
247
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200248 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800249 isp->smk_flags = 0;
250 mutex_init(&isp->smk_lock);
251
252 return isp;
253}
254
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800255/**
256 * new_task_smack - allocate a task security blob
257 * @smack: a pointer to the Smack label to use in the blob
258 *
259 * Returns the new blob or NULL if there's no memory available
260 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700261static struct task_smack *new_task_smack(struct smack_known *task,
262 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800263{
264 struct task_smack *tsp;
265
266 tsp = kzalloc(sizeof(struct task_smack), gfp);
267 if (tsp == NULL)
268 return NULL;
269
270 tsp->smk_task = task;
271 tsp->smk_forked = forked;
272 INIT_LIST_HEAD(&tsp->smk_rules);
273 mutex_init(&tsp->smk_rules_lock);
274
275 return tsp;
276}
277
278/**
279 * smk_copy_rules - copy a rule set
280 * @nhead - new rules header pointer
281 * @ohead - old rules header pointer
282 *
283 * Returns 0 on success, -ENOMEM on error
284 */
285static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
286 gfp_t gfp)
287{
288 struct smack_rule *nrp;
289 struct smack_rule *orp;
290 int rc = 0;
291
292 INIT_LIST_HEAD(nhead);
293
294 list_for_each_entry_rcu(orp, ohead, list) {
295 nrp = kzalloc(sizeof(struct smack_rule), gfp);
296 if (nrp == NULL) {
297 rc = -ENOMEM;
298 break;
299 }
300 *nrp = *orp;
301 list_add_rcu(&nrp->list, nhead);
302 }
303 return rc;
304}
305
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100306/**
307 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
308 * @mode - input mode in form of PTRACE_MODE_*
309 *
310 * Returns a converted MAY_* mode usable by smack rules
311 */
312static inline unsigned int smk_ptrace_mode(unsigned int mode)
313{
314 switch (mode) {
315 case PTRACE_MODE_READ:
316 return MAY_READ;
317 case PTRACE_MODE_ATTACH:
318 return MAY_READWRITE;
319 }
320
321 return 0;
322}
323
324/**
325 * smk_ptrace_rule_check - helper for ptrace access
326 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200327 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100328 * @mode: ptrace attachment mode (PTRACE_MODE_*)
329 * @func: name of the function that called us, used for audit
330 *
331 * Returns 0 on access granted, -error on error
332 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200333static int smk_ptrace_rule_check(struct task_struct *tracer,
334 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100335 unsigned int mode, const char *func)
336{
337 int rc;
338 struct smk_audit_info ad, *saip = NULL;
339 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200340 struct smack_known *tracer_known;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100341
342 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
343 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
344 smk_ad_setfield_u_tsk(&ad, tracer);
345 saip = &ad;
346 }
347
348 tsp = task_security(tracer);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200349 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100350
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100351 if ((mode & PTRACE_MODE_ATTACH) &&
352 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
353 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200354 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100355 rc = 0;
356 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
357 rc = -EACCES;
358 else if (capable(CAP_SYS_PTRACE))
359 rc = 0;
360 else
361 rc = -EACCES;
362
363 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200364 smack_log(tracer_known->smk_known,
365 tracee_known->smk_known,
366 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100367
368 return rc;
369 }
370
371 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200372 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100373 return rc;
374}
375
Casey Schauflere114e472008-02-04 22:29:50 -0800376/*
377 * LSM hooks.
378 * We he, that is fun!
379 */
380
381/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000382 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800383 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100384 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800385 *
386 * Returns 0 if access is OK, an error code otherwise
387 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100388 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800389 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000390static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800391{
392 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700393 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800394
Ingo Molnar9e488582009-05-07 19:26:19 +1000395 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800396 if (rc != 0)
397 return rc;
398
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700399 skp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200400
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200401 rc = smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100402 return rc;
403}
Casey Schauflere114e472008-02-04 22:29:50 -0800404
David Howells5cd9c582008-08-14 11:37:28 +0100405/**
406 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
407 * @ptp: parent task pointer
408 *
409 * Returns 0 if access is OK, an error code otherwise
410 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100411 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100412 */
413static int smack_ptrace_traceme(struct task_struct *ptp)
414{
415 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700416 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100417
418 rc = cap_ptrace_traceme(ptp);
419 if (rc != 0)
420 return rc;
421
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100422 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200423
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200424 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800425 return rc;
426}
427
428/**
429 * smack_syslog - Smack approval on syslog
430 * @type: message type
431 *
Casey Schauflere114e472008-02-04 22:29:50 -0800432 * Returns 0 on success, error code otherwise.
433 */
Eric Paris12b30522010-11-15 18:36:29 -0500434static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800435{
Eric Paris12b30522010-11-15 18:36:29 -0500436 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700437 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800438
Casey Schaufler1880eff2012-06-05 15:28:30 -0700439 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800440 return 0;
441
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800442 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800443 rc = -EACCES;
444
445 return rc;
446}
447
448
449/*
450 * Superblock Hooks.
451 */
452
453/**
454 * smack_sb_alloc_security - allocate a superblock blob
455 * @sb: the superblock getting the blob
456 *
457 * Returns 0 on success or -ENOMEM on error.
458 */
459static int smack_sb_alloc_security(struct super_block *sb)
460{
461 struct superblock_smack *sbsp;
462
463 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
464
465 if (sbsp == NULL)
466 return -ENOMEM;
467
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200468 sbsp->smk_root = &smack_known_floor;
469 sbsp->smk_default = &smack_known_floor;
470 sbsp->smk_floor = &smack_known_floor;
471 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700472 /*
473 * smk_initialized will be zero from kzalloc.
474 */
Casey Schauflere114e472008-02-04 22:29:50 -0800475 sb->s_security = sbsp;
476
477 return 0;
478}
479
480/**
481 * smack_sb_free_security - free a superblock blob
482 * @sb: the superblock getting the blob
483 *
484 */
485static void smack_sb_free_security(struct super_block *sb)
486{
487 kfree(sb->s_security);
488 sb->s_security = NULL;
489}
490
491/**
492 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800493 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800494 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800495 *
496 * Returns 0 on success or -ENOMEM on error.
497 *
498 * Copy the Smack specific mount options out of the mount
499 * options list.
500 */
Eric Parise0007522008-03-05 10:31:54 -0500501static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800502{
503 char *cp, *commap, *otheropts, *dp;
504
Casey Schauflere114e472008-02-04 22:29:50 -0800505 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
506 if (otheropts == NULL)
507 return -ENOMEM;
508
509 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
510 if (strstr(cp, SMK_FSDEFAULT) == cp)
511 dp = smackopts;
512 else if (strstr(cp, SMK_FSFLOOR) == cp)
513 dp = smackopts;
514 else if (strstr(cp, SMK_FSHAT) == cp)
515 dp = smackopts;
516 else if (strstr(cp, SMK_FSROOT) == cp)
517 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700518 else if (strstr(cp, SMK_FSTRANS) == cp)
519 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800520 else
521 dp = otheropts;
522
523 commap = strchr(cp, ',');
524 if (commap != NULL)
525 *commap = '\0';
526
527 if (*dp != '\0')
528 strcat(dp, ",");
529 strcat(dp, cp);
530 }
531
532 strcpy(orig, otheropts);
533 free_page((unsigned long)otheropts);
534
535 return 0;
536}
537
538/**
539 * smack_sb_kern_mount - Smack specific mount processing
540 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100541 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800542 * @data: the smack mount options
543 *
544 * Returns 0 on success, an error code on failure
545 */
James Morris12204e22008-12-19 10:44:42 +1100546static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800547{
548 struct dentry *root = sb->s_root;
549 struct inode *inode = root->d_inode;
550 struct superblock_smack *sp = sb->s_security;
551 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800552 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800553 char *op;
554 char *commap;
Casey Schauflere830b392013-05-22 18:43:07 -0700555 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800556 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800557
Casey Schauflere830b392013-05-22 18:43:07 -0700558 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800559 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700560
Casey Schauflere114e472008-02-04 22:29:50 -0800561 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800562
563 for (op = data; op != NULL; op = commap) {
564 commap = strchr(op, ',');
565 if (commap != NULL)
566 *commap++ = '\0';
567
568 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
569 op += strlen(SMK_FSHAT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200570 skp = smk_import_entry(op, 0);
571 if (skp != NULL) {
572 sp->smk_hat = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800573 specified = 1;
574 }
Casey Schauflere114e472008-02-04 22:29:50 -0800575 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
576 op += strlen(SMK_FSFLOOR);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200577 skp = smk_import_entry(op, 0);
578 if (skp != NULL) {
579 sp->smk_floor = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800580 specified = 1;
581 }
Casey Schauflere114e472008-02-04 22:29:50 -0800582 } else if (strncmp(op, SMK_FSDEFAULT,
583 strlen(SMK_FSDEFAULT)) == 0) {
584 op += strlen(SMK_FSDEFAULT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200585 skp = smk_import_entry(op, 0);
586 if (skp != NULL) {
587 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800588 specified = 1;
589 }
Casey Schauflere114e472008-02-04 22:29:50 -0800590 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
591 op += strlen(SMK_FSROOT);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200592 skp = smk_import_entry(op, 0);
593 if (skp != NULL) {
594 sp->smk_root = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800595 specified = 1;
596 }
Casey Schauflere830b392013-05-22 18:43:07 -0700597 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
598 op += strlen(SMK_FSTRANS);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200599 skp = smk_import_entry(op, 0);
600 if (skp != NULL) {
601 sp->smk_root = skp;
Casey Schauflere830b392013-05-22 18:43:07 -0700602 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800603 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700604 }
Casey Schauflere114e472008-02-04 22:29:50 -0800605 }
606 }
607
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800608 if (!smack_privileged(CAP_MAC_ADMIN)) {
609 /*
610 * Unprivileged mounts don't get to specify Smack values.
611 */
612 if (specified)
613 return -EPERM;
614 /*
615 * Unprivileged mounts get root and default from the caller.
616 */
617 skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200618 sp->smk_root = skp;
619 sp->smk_default = skp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800620 }
Casey Schauflere114e472008-02-04 22:29:50 -0800621 /*
622 * Initialize the root inode.
623 */
624 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100625 if (isp == NULL) {
626 isp = new_inode_smack(sp->smk_root);
627 if (isp == NULL)
628 return -ENOMEM;
629 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700630 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800631 isp->smk_inode = sp->smk_root;
632
Casey Schauflere830b392013-05-22 18:43:07 -0700633 if (transmute)
634 isp->smk_flags |= SMK_INODE_TRANSMUTE;
635
Casey Schauflere114e472008-02-04 22:29:50 -0800636 return 0;
637}
638
639/**
640 * smack_sb_statfs - Smack check on statfs
641 * @dentry: identifies the file system in question
642 *
643 * Returns 0 if current can read the floor of the filesystem,
644 * and error code otherwise
645 */
646static int smack_sb_statfs(struct dentry *dentry)
647{
648 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200649 int rc;
650 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800651
Eric Parisa2694342011-04-25 13:10:27 -0400652 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200653 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
654
655 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700656 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200657 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800658}
659
Casey Schauflere114e472008-02-04 22:29:50 -0800660/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800661 * BPRM hooks
662 */
663
Casey Schauflerce8a4322011-09-29 18:21:01 -0700664/**
665 * smack_bprm_set_creds - set creds for exec
666 * @bprm: the exec information
667 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100668 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700669 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800670static int smack_bprm_set_creds(struct linux_binprm *bprm)
671{
Al Viro496ad9a2013-01-23 17:07:38 -0500672 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300673 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800674 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800675 int rc;
676
677 rc = cap_bprm_set_creds(bprm);
678 if (rc != 0)
679 return rc;
680
681 if (bprm->cred_prepared)
682 return 0;
683
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300684 isp = inode->i_security;
685 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800686 return 0;
687
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100688 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
689 struct task_struct *tracer;
690 rc = 0;
691
692 rcu_read_lock();
693 tracer = ptrace_parent(current);
694 if (likely(tracer != NULL))
695 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200696 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100697 PTRACE_MODE_ATTACH,
698 __func__);
699 rcu_read_unlock();
700
701 if (rc != 0)
702 return rc;
703 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300704 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800705
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300706 bsp->smk_task = isp->smk_task;
707 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800708
709 return 0;
710}
711
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300712/**
713 * smack_bprm_committing_creds - Prepare to install the new credentials
714 * from bprm.
715 *
716 * @bprm: binprm for exec
717 */
718static void smack_bprm_committing_creds(struct linux_binprm *bprm)
719{
720 struct task_smack *bsp = bprm->cred->security;
721
722 if (bsp->smk_task != bsp->smk_forked)
723 current->pdeath_signal = 0;
724}
725
726/**
727 * smack_bprm_secureexec - Return the decision to use secureexec.
728 * @bprm: binprm for exec
729 *
730 * Returns 0 on success.
731 */
732static int smack_bprm_secureexec(struct linux_binprm *bprm)
733{
734 struct task_smack *tsp = current_security();
735 int ret = cap_bprm_secureexec(bprm);
736
737 if (!ret && (tsp->smk_task != tsp->smk_forked))
738 ret = 1;
739
740 return ret;
741}
742
Casey Schaufler676dac42010-12-02 06:43:39 -0800743/*
Casey Schauflere114e472008-02-04 22:29:50 -0800744 * Inode hooks
745 */
746
747/**
748 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800749 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800750 *
751 * Returns 0 if it gets a blob, -ENOMEM otherwise
752 */
753static int smack_inode_alloc_security(struct inode *inode)
754{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700755 struct smack_known *skp = smk_of_current();
756
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200757 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800758 if (inode->i_security == NULL)
759 return -ENOMEM;
760 return 0;
761}
762
763/**
764 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800765 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800766 *
767 * Clears the blob pointer in inode
768 */
769static void smack_inode_free_security(struct inode *inode)
770{
Rohit1a5b4722014-10-15 17:40:41 +0530771 kmem_cache_free(smack_inode_cache, inode->i_security);
Casey Schauflere114e472008-02-04 22:29:50 -0800772 inode->i_security = NULL;
773}
774
775/**
776 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200777 * @inode: the newly created inode
778 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500779 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800780 * @name: where to put the attribute name
781 * @value: where to put the attribute value
782 * @len: where to put the length of the attribute
783 *
784 * Returns 0 if it all works out, -ENOMEM if there's no memory
785 */
786static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900787 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500788 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800789{
Casey Schaufler2267b132012-03-13 19:14:19 -0700790 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700791 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200792 struct smack_known *isp = smk_of_inode(inode);
793 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800794 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800795
Tetsuo Handa95489062013-07-25 05:44:02 +0900796 if (name)
797 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800798
799 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800800 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200801 may = smk_access_entry(skp->smk_known, dsp->smk_known,
802 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800803 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200804
805 /*
806 * If the access rule allows transmutation and
807 * the directory requests transmutation then
808 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700809 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200810 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800811 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700812 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200813 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700814 issp->smk_flags |= SMK_INODE_CHANGED;
815 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200816
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200817 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800818 if (*value == NULL)
819 return -ENOMEM;
820 }
821
822 if (len)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200823 *len = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -0800824
825 return 0;
826}
827
828/**
829 * smack_inode_link - Smack check on link
830 * @old_dentry: the existing object
831 * @dir: unused
832 * @new_dentry: the new object
833 *
834 * Returns 0 if access is permitted, an error code otherwise
835 */
836static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
837 struct dentry *new_dentry)
838{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200839 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200840 struct smk_audit_info ad;
841 int rc;
842
Eric Parisa2694342011-04-25 13:10:27 -0400843 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200844 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800845
846 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200847 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700848 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800849
850 if (rc == 0 && new_dentry->d_inode != NULL) {
851 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200852 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
853 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700854 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800855 }
856
857 return rc;
858}
859
860/**
861 * smack_inode_unlink - Smack check on inode deletion
862 * @dir: containing directory object
863 * @dentry: file to unlink
864 *
865 * Returns 0 if current can write the containing directory
866 * and the object, error code otherwise
867 */
868static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
869{
870 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200871 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800872 int rc;
873
Eric Parisa2694342011-04-25 13:10:27 -0400874 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200875 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
876
Casey Schauflere114e472008-02-04 22:29:50 -0800877 /*
878 * You need write access to the thing you're unlinking
879 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200880 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700881 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200882 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800883 /*
884 * You also need write access to the containing directory
885 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400886 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200887 smk_ad_setfield_u_fs_inode(&ad, dir);
888 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700889 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200890 }
Casey Schauflere114e472008-02-04 22:29:50 -0800891 return rc;
892}
893
894/**
895 * smack_inode_rmdir - Smack check on directory deletion
896 * @dir: containing directory object
897 * @dentry: directory to unlink
898 *
899 * Returns 0 if current can write the containing directory
900 * and the directory, error code otherwise
901 */
902static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
903{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200904 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800905 int rc;
906
Eric Parisa2694342011-04-25 13:10:27 -0400907 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200908 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
909
Casey Schauflere114e472008-02-04 22:29:50 -0800910 /*
911 * You need write access to the thing you're removing
912 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200913 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700914 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200915 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800916 /*
917 * You also need write access to the containing directory
918 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400919 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200920 smk_ad_setfield_u_fs_inode(&ad, dir);
921 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700922 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200923 }
Casey Schauflere114e472008-02-04 22:29:50 -0800924
925 return rc;
926}
927
928/**
929 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200930 * @old_inode: unused
931 * @old_dentry: the old object
932 * @new_inode: unused
933 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -0800934 *
935 * Read and write access is required on both the old and
936 * new directories.
937 *
938 * Returns 0 if access is permitted, an error code otherwise
939 */
940static int smack_inode_rename(struct inode *old_inode,
941 struct dentry *old_dentry,
942 struct inode *new_inode,
943 struct dentry *new_dentry)
944{
945 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200946 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200947 struct smk_audit_info ad;
948
Eric Parisa2694342011-04-25 13:10:27 -0400949 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200950 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800951
952 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200953 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700954 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800955
956 if (rc == 0 && new_dentry->d_inode != NULL) {
957 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200958 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
959 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700960 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800961 }
Casey Schauflere114e472008-02-04 22:29:50 -0800962 return rc;
963}
964
965/**
966 * smack_inode_permission - Smack version of permission()
967 * @inode: the inode in question
968 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800969 *
970 * This is the important Smack hook.
971 *
972 * Returns 0 if access is permitted, -EACCES otherwise
973 */
Al Viroe74f71e2011-06-20 19:38:15 -0400974static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800975{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200976 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400977 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -0700978 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -0400979
980 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800981 /*
982 * No permission to check. Existence test. Yup, it's there.
983 */
984 if (mask == 0)
985 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700986
987 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400988 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700989 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400990 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200991 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700992 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
993 rc = smk_bu_inode(inode, mask, rc);
994 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800995}
996
997/**
998 * smack_inode_setattr - Smack check for setting attributes
999 * @dentry: the object
1000 * @iattr: for the force flag
1001 *
1002 * Returns 0 if access is permitted, an error code otherwise
1003 */
1004static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1005{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001006 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001007 int rc;
1008
Casey Schauflere114e472008-02-04 22:29:50 -08001009 /*
1010 * Need to allow for clearing the setuid bit.
1011 */
1012 if (iattr->ia_valid & ATTR_FORCE)
1013 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001014 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001015 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001016
Casey Schauflerd166c802014-08-27 14:51:27 -07001017 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1018 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1019 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001020}
1021
1022/**
1023 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001024 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001025 * @dentry: the object
1026 *
1027 * Returns 0 if access is permitted, an error code otherwise
1028 */
1029static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1030{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001031 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -04001032 struct path path;
Casey Schauflerd166c802014-08-27 14:51:27 -07001033 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001034
Eric Parisa2694342011-04-25 13:10:27 -04001035 path.dentry = dentry;
1036 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001037
Eric Parisf48b7392011-04-25 12:54:27 -04001038 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -04001039 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001040 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1041 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1042 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001043}
1044
1045/**
1046 * smack_inode_setxattr - Smack check for setting xattrs
1047 * @dentry: the object
1048 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001049 * @value: value of the attribute
1050 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001051 * @flags: unused
1052 *
1053 * This protects the Smack attribute explicitly.
1054 *
1055 * Returns 0 if access is permitted, an error code otherwise
1056 */
David Howells8f0cfa52008-04-29 00:59:41 -07001057static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1058 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001059{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001060 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001061 struct smack_known *skp;
1062 int check_priv = 0;
1063 int check_import = 0;
1064 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001065 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001066
Casey Schaufler19760ad2013-12-16 16:27:26 -08001067 /*
1068 * Check label validity here so import won't fail in post_setxattr
1069 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001070 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1071 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001072 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1073 check_priv = 1;
1074 check_import = 1;
1075 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1076 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1077 check_priv = 1;
1078 check_import = 1;
1079 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001080 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001081 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001082 if (size != TRANS_TRUE_SIZE ||
1083 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1084 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001085 } else
1086 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1087
Casey Schaufler19760ad2013-12-16 16:27:26 -08001088 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1089 rc = -EPERM;
1090
1091 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001092 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001093 if (skp == NULL || (check_star &&
1094 (skp == &smack_known_star || skp == &smack_known_web)))
1095 rc = -EINVAL;
1096 }
1097
Eric Parisa2694342011-04-25 13:10:27 -04001098 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001099 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1100
Casey Schauflerd166c802014-08-27 14:51:27 -07001101 if (rc == 0) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001103 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1104 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001105
1106 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001107}
1108
1109/**
1110 * smack_inode_post_setxattr - Apply the Smack update approved above
1111 * @dentry: object
1112 * @name: attribute name
1113 * @value: attribute value
1114 * @size: attribute size
1115 * @flags: unused
1116 *
1117 * Set the pointer in the inode blob to the entry found
1118 * in the master label list.
1119 */
David Howells8f0cfa52008-04-29 00:59:41 -07001120static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1121 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001122{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001123 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001124 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001125
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001126 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1127 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1128 return;
1129 }
1130
Casey Schaufler676dac42010-12-02 06:43:39 -08001131 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001132 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001133 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001134 isp->smk_inode = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001135 else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001136 isp->smk_inode = &smack_known_invalid;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001137 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001138 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001139 if (skp != NULL)
1140 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001141 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001142 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001143 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001144 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001145 if (skp != NULL)
1146 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001147 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001148 isp->smk_mmap = &smack_known_invalid;
1149 }
Casey Schauflere114e472008-02-04 22:29:50 -08001150
1151 return;
1152}
1153
Casey Schauflerce8a4322011-09-29 18:21:01 -07001154/**
Casey Schauflere114e472008-02-04 22:29:50 -08001155 * smack_inode_getxattr - Smack check on getxattr
1156 * @dentry: the object
1157 * @name: unused
1158 *
1159 * Returns 0 if access is permitted, an error code otherwise
1160 */
David Howells8f0cfa52008-04-29 00:59:41 -07001161static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001162{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001163 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001164 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001165
Eric Parisa2694342011-04-25 13:10:27 -04001166 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001167 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1168
Casey Schauflerd166c802014-08-27 14:51:27 -07001169 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1170 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1171 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001172}
1173
Casey Schauflerce8a4322011-09-29 18:21:01 -07001174/**
Casey Schauflere114e472008-02-04 22:29:50 -08001175 * smack_inode_removexattr - Smack check on removexattr
1176 * @dentry: the object
1177 * @name: name of the attribute
1178 *
1179 * Removing the Smack attribute requires CAP_MAC_ADMIN
1180 *
1181 * Returns 0 if access is permitted, an error code otherwise
1182 */
David Howells8f0cfa52008-04-29 00:59:41 -07001183static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001184{
Casey Schaufler676dac42010-12-02 06:43:39 -08001185 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001186 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001187 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001188
Casey Schauflerbcdca222008-02-23 15:24:04 -08001189 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1190 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001191 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001192 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001193 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301194 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001195 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001196 rc = -EPERM;
1197 } else
1198 rc = cap_inode_removexattr(dentry, name);
1199
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001200 if (rc != 0)
1201 return rc;
1202
Eric Parisa2694342011-04-25 13:10:27 -04001203 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001204 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001205
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001206 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001207 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001208 if (rc != 0)
1209 return rc;
1210
1211 isp = dentry->d_inode->i_security;
1212 /*
1213 * Don't do anything special for these.
1214 * XATTR_NAME_SMACKIPIN
1215 * XATTR_NAME_SMACKIPOUT
1216 * XATTR_NAME_SMACKEXEC
1217 */
1218 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001219 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001220 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001221 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001222 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1223 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001224
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001225 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001226}
1227
1228/**
1229 * smack_inode_getsecurity - get smack xattrs
1230 * @inode: the object
1231 * @name: attribute name
1232 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001233 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001234 *
1235 * Returns the size of the attribute or an error code
1236 */
1237static int smack_inode_getsecurity(const struct inode *inode,
1238 const char *name, void **buffer,
1239 bool alloc)
1240{
1241 struct socket_smack *ssp;
1242 struct socket *sock;
1243 struct super_block *sbp;
1244 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001245 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001246 int ilen;
1247 int rc = 0;
1248
1249 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1250 isp = smk_of_inode(inode);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001251 ilen = strlen(isp->smk_known);
1252 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001253 return ilen;
1254 }
1255
1256 /*
1257 * The rest of the Smack xattrs are only on sockets.
1258 */
1259 sbp = ip->i_sb;
1260 if (sbp->s_magic != SOCKFS_MAGIC)
1261 return -EOPNOTSUPP;
1262
1263 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001264 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001265 return -EOPNOTSUPP;
1266
1267 ssp = sock->sk->sk_security;
1268
1269 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001270 isp = ssp->smk_in;
Casey Schauflere114e472008-02-04 22:29:50 -08001271 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001272 isp = ssp->smk_out;
Casey Schauflere114e472008-02-04 22:29:50 -08001273 else
1274 return -EOPNOTSUPP;
1275
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001276 ilen = strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001277 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001278 *buffer = isp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001279 rc = ilen;
1280 }
1281
1282 return rc;
1283}
1284
1285
1286/**
1287 * smack_inode_listsecurity - list the Smack attributes
1288 * @inode: the object
1289 * @buffer: where they go
1290 * @buffer_size: size of buffer
1291 *
1292 * Returns 0 on success, -EINVAL otherwise
1293 */
1294static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1295 size_t buffer_size)
1296{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001297 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001298
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001299 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001300 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001301
1302 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001303}
1304
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001305/**
1306 * smack_inode_getsecid - Extract inode's security id
1307 * @inode: inode to extract the info from
1308 * @secid: where result will be saved
1309 */
1310static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1311{
1312 struct inode_smack *isp = inode->i_security;
1313
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001314 *secid = isp->smk_inode->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001315}
1316
Casey Schauflere114e472008-02-04 22:29:50 -08001317/*
1318 * File Hooks
1319 */
1320
1321/**
1322 * smack_file_permission - Smack check on file operations
1323 * @file: unused
1324 * @mask: unused
1325 *
1326 * Returns 0
1327 *
1328 * Should access checks be done on each read or write?
1329 * UNICOS and SELinux say yes.
1330 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1331 *
1332 * I'll say no for now. Smack does not do the frequent
1333 * label changing that SELinux does.
1334 */
1335static int smack_file_permission(struct file *file, int mask)
1336{
1337 return 0;
1338}
1339
1340/**
1341 * smack_file_alloc_security - assign a file security blob
1342 * @file: the object
1343 *
1344 * The security blob for a file is a pointer to the master
1345 * label list, so no allocation is done.
1346 *
1347 * Returns 0
1348 */
1349static int smack_file_alloc_security(struct file *file)
1350{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001351 struct smack_known *skp = smk_of_current();
1352
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001353 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001354 return 0;
1355}
1356
1357/**
1358 * smack_file_free_security - clear a file security blob
1359 * @file: the object
1360 *
1361 * The security blob for a file is a pointer to the master
1362 * label list, so no memory is freed.
1363 */
1364static void smack_file_free_security(struct file *file)
1365{
1366 file->f_security = NULL;
1367}
1368
1369/**
1370 * smack_file_ioctl - Smack check on ioctls
1371 * @file: the object
1372 * @cmd: what to do
1373 * @arg: unused
1374 *
1375 * Relies heavily on the correct use of the ioctl command conventions.
1376 *
1377 * Returns 0 if allowed, error code otherwise
1378 */
1379static int smack_file_ioctl(struct file *file, unsigned int cmd,
1380 unsigned long arg)
1381{
1382 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001383 struct smk_audit_info ad;
1384
Eric Parisf48b7392011-04-25 12:54:27 -04001385 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001386 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001387
Casey Schauflerd166c802014-08-27 14:51:27 -07001388 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001389 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001390 rc = smk_bu_file(file, MAY_WRITE, rc);
1391 }
Casey Schauflere114e472008-02-04 22:29:50 -08001392
Casey Schauflerd166c802014-08-27 14:51:27 -07001393 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001394 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001395 rc = smk_bu_file(file, MAY_READ, rc);
1396 }
Casey Schauflere114e472008-02-04 22:29:50 -08001397
1398 return rc;
1399}
1400
1401/**
1402 * smack_file_lock - Smack check on file locking
1403 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001404 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001405 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001406 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001407 */
1408static int smack_file_lock(struct file *file, unsigned int cmd)
1409{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001410 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001411 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001412
Eric Paris92f42502011-04-25 13:15:55 -04001413 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1414 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001415 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1416 rc = smk_bu_file(file, MAY_LOCK, rc);
1417 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001418}
1419
1420/**
1421 * smack_file_fcntl - Smack check on fcntl
1422 * @file: the object
1423 * @cmd: what action to check
1424 * @arg: unused
1425 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001426 * Generally these operations are harmless.
1427 * File locking operations present an obvious mechanism
1428 * for passing information, so they require write access.
1429 *
Casey Schauflere114e472008-02-04 22:29:50 -08001430 * Returns 0 if current has access, error code otherwise
1431 */
1432static int smack_file_fcntl(struct file *file, unsigned int cmd,
1433 unsigned long arg)
1434{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001435 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001436 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001437
Etienne Bassetecfcc532009-04-08 20:40:06 +02001438
Casey Schauflere114e472008-02-04 22:29:50 -08001439 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001440 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001441 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001442 case F_SETLK:
1443 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001444 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1445 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1446 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001447 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001448 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001449 case F_SETOWN:
1450 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001451 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1452 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001453 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001454 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001455 break;
1456 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001457 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001458 }
1459
1460 return rc;
1461}
1462
1463/**
Al Viroe5467852012-05-30 13:30:51 -04001464 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001465 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1466 * if mapping anonymous memory.
1467 * @file contains the file structure for file to map (may be NULL).
1468 * @reqprot contains the protection requested by the application.
1469 * @prot contains the protection that will be applied by the kernel.
1470 * @flags contains the operational flags.
1471 * Return 0 if permission is granted.
1472 */
Al Viroe5467852012-05-30 13:30:51 -04001473static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001474 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001475 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001476{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001477 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001478 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001479 struct smack_rule *srp;
1480 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001481 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001482 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001483 int may;
1484 int mmay;
1485 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001486 int rc;
1487
Al Viro496ad9a2013-01-23 17:07:38 -05001488 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001489 return 0;
1490
Al Viro496ad9a2013-01-23 17:07:38 -05001491 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001492 if (isp->smk_mmap == NULL)
1493 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001494 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001495
1496 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001497 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001498 rc = 0;
1499
1500 rcu_read_lock();
1501 /*
1502 * For each Smack rule associated with the subject
1503 * label verify that the SMACK64MMAP also has access
1504 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001505 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001506 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001507 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001508 /*
1509 * Matching labels always allows access.
1510 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001511 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001512 continue;
1513 /*
1514 * If there is a matching local rule take
1515 * that into account as well.
1516 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001517 may = smk_access_entry(srp->smk_subject->smk_known,
1518 okp->smk_known,
1519 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001520 if (may == -ENOENT)
1521 may = srp->smk_access;
1522 else
1523 may &= srp->smk_access;
1524 /*
1525 * If may is zero the SMACK64MMAP subject can't
1526 * possibly have less access.
1527 */
1528 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001529 continue;
1530
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001531 /*
1532 * Fetch the global list entry.
1533 * If there isn't one a SMACK64MMAP subject
1534 * can't have as much access as current.
1535 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001536 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1537 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001538 if (mmay == -ENOENT) {
1539 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001540 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001541 }
1542 /*
1543 * If there is a local entry it modifies the
1544 * potential access, too.
1545 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001546 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1547 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001548 if (tmay != -ENOENT)
1549 mmay &= tmay;
1550
1551 /*
1552 * If there is any access available to current that is
1553 * not available to a SMACK64MMAP subject
1554 * deny access.
1555 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001556 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001557 rc = -EACCES;
1558 break;
1559 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001560 }
1561
1562 rcu_read_unlock();
1563
1564 return rc;
1565}
1566
1567/**
Casey Schauflere114e472008-02-04 22:29:50 -08001568 * smack_file_set_fowner - set the file security blob value
1569 * @file: object in question
1570 *
1571 * Returns 0
1572 * Further research may be required on this one.
1573 */
1574static int smack_file_set_fowner(struct file *file)
1575{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001576 struct smack_known *skp = smk_of_current();
1577
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001578 file->f_security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08001579 return 0;
1580}
1581
1582/**
1583 * smack_file_send_sigiotask - Smack on sigio
1584 * @tsk: The target task
1585 * @fown: the object the signal come from
1586 * @signum: unused
1587 *
1588 * Allow a privileged task to get signals even if it shouldn't
1589 *
1590 * Returns 0 if a subject with the object's smack could
1591 * write to the task, an error code otherwise.
1592 */
1593static int smack_file_send_sigiotask(struct task_struct *tsk,
1594 struct fown_struct *fown, int signum)
1595{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001596 struct smack_known *skp;
1597 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001598 struct file *file;
1599 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001600 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001601
1602 /*
1603 * struct fown_struct is never outside the context of a struct file
1604 */
1605 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001606
Etienne Bassetecfcc532009-04-08 20:40:06 +02001607 /* we don't log here as rc can be overriden */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001608 skp = file->f_security;
1609 rc = smk_access(skp, tkp, MAY_WRITE, NULL);
1610 rc = smk_bu_note("sigiotask", skp, tkp, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001611 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001612 rc = 0;
1613
1614 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1615 smk_ad_setfield_u_tsk(&ad, tsk);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001616 smack_log(skp->smk_known, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001617 return rc;
1618}
1619
1620/**
1621 * smack_file_receive - Smack file receive check
1622 * @file: the object
1623 *
1624 * Returns 0 if current has access, error code otherwise
1625 */
1626static int smack_file_receive(struct file *file)
1627{
Casey Schauflerd166c802014-08-27 14:51:27 -07001628 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001629 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001630 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001631
Casey Schaufler4482a442013-12-30 17:37:45 -08001632 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001633 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001634 /*
1635 * This code relies on bitmasks.
1636 */
1637 if (file->f_mode & FMODE_READ)
1638 may = MAY_READ;
1639 if (file->f_mode & FMODE_WRITE)
1640 may |= MAY_WRITE;
1641
Casey Schauflerd166c802014-08-27 14:51:27 -07001642 rc = smk_curacc(file->f_security, may, &ad);
1643 rc = smk_bu_file(file, may, rc);
1644 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001645}
1646
Casey Schaufler531f1d42011-09-19 12:41:42 -07001647/**
Eric Paris83d49852012-04-04 13:45:40 -04001648 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001649 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001650 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001651 *
1652 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001653 * Allow the open only if the task has read access. There are
1654 * many read operations (e.g. fstat) that you can do with an
1655 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001656 *
1657 * Returns 0
1658 */
Eric Paris83d49852012-04-04 13:45:40 -04001659static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001660{
Casey Schauflera6834c02014-04-21 11:10:26 -07001661 struct task_smack *tsp = cred->security;
Al Viro496ad9a2013-01-23 17:07:38 -05001662 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schauflera6834c02014-04-21 11:10:26 -07001663 struct smk_audit_info ad;
1664 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001665
Marcin Niesluchowskid83d2c22014-08-19 14:26:32 +02001666 if (smack_privileged(CAP_MAC_OVERRIDE)) {
1667 file->f_security = isp->smk_inode;
Casey Schauflera6834c02014-04-21 11:10:26 -07001668 return 0;
Marcin Niesluchowskid83d2c22014-08-19 14:26:32 +02001669 }
Casey Schaufler531f1d42011-09-19 12:41:42 -07001670
Casey Schauflera6834c02014-04-21 11:10:26 -07001671 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1672 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1673 rc = smk_access(tsp->smk_task, isp->smk_inode, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001674 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001675 if (rc == 0)
1676 file->f_security = isp->smk_inode;
1677
1678 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001679}
1680
Casey Schauflere114e472008-02-04 22:29:50 -08001681/*
1682 * Task hooks
1683 */
1684
1685/**
David Howellsee18d642009-09-02 09:14:21 +01001686 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1687 * @new: the new credentials
1688 * @gfp: the atomicity of any memory allocations
1689 *
1690 * Prepare a blank set of credentials for modification. This must allocate all
1691 * the memory the LSM module might require such that cred_transfer() can
1692 * complete without error.
1693 */
1694static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1695{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001696 struct task_smack *tsp;
1697
1698 tsp = new_task_smack(NULL, NULL, gfp);
1699 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001700 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001701
1702 cred->security = tsp;
1703
David Howellsee18d642009-09-02 09:14:21 +01001704 return 0;
1705}
1706
1707
1708/**
David Howellsf1752ee2008-11-14 10:39:17 +11001709 * smack_cred_free - "free" task-level security credentials
1710 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001711 *
Casey Schauflere114e472008-02-04 22:29:50 -08001712 */
David Howellsf1752ee2008-11-14 10:39:17 +11001713static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001714{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001715 struct task_smack *tsp = cred->security;
1716 struct smack_rule *rp;
1717 struct list_head *l;
1718 struct list_head *n;
1719
1720 if (tsp == NULL)
1721 return;
1722 cred->security = NULL;
1723
1724 list_for_each_safe(l, n, &tsp->smk_rules) {
1725 rp = list_entry(l, struct smack_rule, list);
1726 list_del(&rp->list);
1727 kfree(rp);
1728 }
1729 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001730}
1731
1732/**
David Howellsd84f4f92008-11-14 10:39:23 +11001733 * smack_cred_prepare - prepare new set of credentials for modification
1734 * @new: the new credentials
1735 * @old: the original credentials
1736 * @gfp: the atomicity of any memory allocations
1737 *
1738 * Prepare a new set of credentials for modification.
1739 */
1740static int smack_cred_prepare(struct cred *new, const struct cred *old,
1741 gfp_t gfp)
1742{
Casey Schaufler676dac42010-12-02 06:43:39 -08001743 struct task_smack *old_tsp = old->security;
1744 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001745 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001746
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001747 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001748 if (new_tsp == NULL)
1749 return -ENOMEM;
1750
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001751 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1752 if (rc != 0)
1753 return rc;
1754
Casey Schaufler676dac42010-12-02 06:43:39 -08001755 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001756 return 0;
1757}
1758
Randy Dunlap251a2a92009-02-18 11:42:33 -08001759/**
David Howellsee18d642009-09-02 09:14:21 +01001760 * smack_cred_transfer - Transfer the old credentials to the new credentials
1761 * @new: the new credentials
1762 * @old: the original credentials
1763 *
1764 * Fill in a set of blank credentials from another set of credentials.
1765 */
1766static void smack_cred_transfer(struct cred *new, const struct cred *old)
1767{
Casey Schaufler676dac42010-12-02 06:43:39 -08001768 struct task_smack *old_tsp = old->security;
1769 struct task_smack *new_tsp = new->security;
1770
1771 new_tsp->smk_task = old_tsp->smk_task;
1772 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001773 mutex_init(&new_tsp->smk_rules_lock);
1774 INIT_LIST_HEAD(&new_tsp->smk_rules);
1775
1776
1777 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001778}
1779
1780/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001781 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001782 * @new: points to the set of credentials to be modified.
1783 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001784 *
1785 * Set the security data for a kernel service.
1786 */
1787static int smack_kernel_act_as(struct cred *new, u32 secid)
1788{
Casey Schaufler676dac42010-12-02 06:43:39 -08001789 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001790 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001791
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001792 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001793 return -EINVAL;
1794
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001795 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001796 return 0;
1797}
1798
1799/**
1800 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001801 * @new: points to the set of credentials to be modified
1802 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001803 *
1804 * Set the file creation context in a set of credentials to the same
1805 * as the objective context of the specified inode
1806 */
1807static int smack_kernel_create_files_as(struct cred *new,
1808 struct inode *inode)
1809{
1810 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001811 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001812
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001813 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001814 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001815 return 0;
1816}
1817
1818/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001819 * smk_curacc_on_task - helper to log task related access
1820 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001821 * @access: the access requested
1822 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001823 *
1824 * Return 0 if access is permitted
1825 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001826static int smk_curacc_on_task(struct task_struct *p, int access,
1827 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001828{
1829 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001830 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflerd166c802014-08-27 14:51:27 -07001831 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001832
Casey Schaufler531f1d42011-09-19 12:41:42 -07001833 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001834 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001835 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001836 rc = smk_bu_task(p, access, rc);
1837 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001838}
1839
1840/**
Casey Schauflere114e472008-02-04 22:29:50 -08001841 * smack_task_setpgid - Smack check on setting pgid
1842 * @p: the task object
1843 * @pgid: unused
1844 *
1845 * Return 0 if write access is permitted
1846 */
1847static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1848{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001849 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001850}
1851
1852/**
1853 * smack_task_getpgid - Smack access check for getpgid
1854 * @p: the object task
1855 *
1856 * Returns 0 if current can read the object task, error code otherwise
1857 */
1858static int smack_task_getpgid(struct task_struct *p)
1859{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001860 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001861}
1862
1863/**
1864 * smack_task_getsid - Smack access check for getsid
1865 * @p: the object task
1866 *
1867 * Returns 0 if current can read the object task, error code otherwise
1868 */
1869static int smack_task_getsid(struct task_struct *p)
1870{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001871 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001872}
1873
1874/**
1875 * smack_task_getsecid - get the secid of the task
1876 * @p: the object task
1877 * @secid: where to put the result
1878 *
1879 * Sets the secid to contain a u32 version of the smack label.
1880 */
1881static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1882{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001883 struct smack_known *skp = smk_of_task(task_security(p));
1884
1885 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001886}
1887
1888/**
1889 * smack_task_setnice - Smack check on setting nice
1890 * @p: the task object
1891 * @nice: unused
1892 *
1893 * Return 0 if write access is permitted
1894 */
1895static int smack_task_setnice(struct task_struct *p, int nice)
1896{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001897 int rc;
1898
1899 rc = cap_task_setnice(p, nice);
1900 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001901 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001902 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001903}
1904
1905/**
1906 * smack_task_setioprio - Smack check on setting ioprio
1907 * @p: the task object
1908 * @ioprio: unused
1909 *
1910 * Return 0 if write access is permitted
1911 */
1912static int smack_task_setioprio(struct task_struct *p, int ioprio)
1913{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001914 int rc;
1915
1916 rc = cap_task_setioprio(p, ioprio);
1917 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001918 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001919 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001920}
1921
1922/**
1923 * smack_task_getioprio - Smack check on reading ioprio
1924 * @p: the task object
1925 *
1926 * Return 0 if read access is permitted
1927 */
1928static int smack_task_getioprio(struct task_struct *p)
1929{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001930 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001931}
1932
1933/**
1934 * smack_task_setscheduler - Smack check on setting scheduler
1935 * @p: the task object
1936 * @policy: unused
1937 * @lp: unused
1938 *
1939 * Return 0 if read access is permitted
1940 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001941static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001942{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001943 int rc;
1944
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001945 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001946 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001947 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001948 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001949}
1950
1951/**
1952 * smack_task_getscheduler - Smack check on reading scheduler
1953 * @p: the task object
1954 *
1955 * Return 0 if read access is permitted
1956 */
1957static int smack_task_getscheduler(struct task_struct *p)
1958{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001959 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001960}
1961
1962/**
1963 * smack_task_movememory - Smack check on moving memory
1964 * @p: the task object
1965 *
1966 * Return 0 if write access is permitted
1967 */
1968static int smack_task_movememory(struct task_struct *p)
1969{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001970 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001971}
1972
1973/**
1974 * smack_task_kill - Smack check on signal delivery
1975 * @p: the task object
1976 * @info: unused
1977 * @sig: unused
1978 * @secid: identifies the smack to use in lieu of current's
1979 *
1980 * Return 0 if write access is permitted
1981 *
1982 * The secid behavior is an artifact of an SELinux hack
1983 * in the USB code. Someday it may go away.
1984 */
1985static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1986 int sig, u32 secid)
1987{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001988 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001989 struct smack_known *skp;
1990 struct smack_known *tkp = smk_of_task(task_security(p));
Casey Schauflerd166c802014-08-27 14:51:27 -07001991 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001992
1993 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1994 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001995 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001996 * Sending a signal requires that the sender
1997 * can write the receiver.
1998 */
Casey Schauflerd166c802014-08-27 14:51:27 -07001999 if (secid == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002000 rc = smk_curacc(tkp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002001 rc = smk_bu_task(p, MAY_WRITE, rc);
2002 return rc;
2003 }
Casey Schauflere114e472008-02-04 22:29:50 -08002004 /*
2005 * If the secid isn't 0 we're dealing with some USB IO
2006 * specific behavior. This is not clean. For one thing
2007 * we can't take privilege into account.
2008 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002009 skp = smack_from_secid(secid);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002010 rc = smk_access(skp, tkp, MAY_WRITE, &ad);
2011 rc = smk_bu_note("USB signal", skp, tkp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002012 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002013}
2014
2015/**
2016 * smack_task_wait - Smack access check for waiting
2017 * @p: task to wait for
2018 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002019 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002020 */
2021static int smack_task_wait(struct task_struct *p)
2022{
Casey Schauflere114e472008-02-04 22:29:50 -08002023 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002024 * Allow the operation to succeed.
2025 * Zombies are bad.
2026 * In userless environments (e.g. phones) programs
2027 * get marked with SMACK64EXEC and even if the parent
2028 * and child shouldn't be talking the parent still
2029 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002030 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002031 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002032}
2033
2034/**
2035 * smack_task_to_inode - copy task smack into the inode blob
2036 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002037 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002038 *
2039 * Sets the smack pointer in the inode security blob
2040 */
2041static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2042{
2043 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002044 struct smack_known *skp = smk_of_task(task_security(p));
2045
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002046 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002047}
2048
2049/*
2050 * Socket hooks.
2051 */
2052
2053/**
2054 * smack_sk_alloc_security - Allocate a socket blob
2055 * @sk: the socket
2056 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002057 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002058 *
2059 * Assign Smack pointers to current
2060 *
2061 * Returns 0 on success, -ENOMEM is there's no memory
2062 */
2063static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2064{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002065 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002066 struct socket_smack *ssp;
2067
2068 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2069 if (ssp == NULL)
2070 return -ENOMEM;
2071
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002072 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002073 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002074 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002075
2076 sk->sk_security = ssp;
2077
2078 return 0;
2079}
2080
2081/**
2082 * smack_sk_free_security - Free a socket blob
2083 * @sk: the socket
2084 *
2085 * Clears the blob pointer
2086 */
2087static void smack_sk_free_security(struct sock *sk)
2088{
2089 kfree(sk->sk_security);
2090}
2091
2092/**
Paul Moore07feee82009-03-27 17:10:54 -04002093* smack_host_label - check host based restrictions
2094* @sip: the object end
2095*
2096* looks for host based access restrictions
2097*
2098* This version will only be appropriate for really small sets of single label
2099* hosts. The caller is responsible for ensuring that the RCU read lock is
2100* taken before calling this function.
2101*
2102* Returns the label of the far end or NULL if it's not special.
2103*/
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002104static struct smack_known *smack_host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002105{
2106 struct smk_netlbladdr *snp;
2107 struct in_addr *siap = &sip->sin_addr;
2108
2109 if (siap->s_addr == 0)
2110 return NULL;
2111
2112 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2113 /*
2114 * we break after finding the first match because
2115 * the list is sorted from longest to shortest mask
2116 * so we have found the most specific match
2117 */
2118 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002119 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2120 /* we have found the special CIPSO option */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002121 if (snp->smk_label == &smack_cipso_option)
Etienne Basset43031542009-03-27 17:11:01 -04002122 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002123 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002124 }
Paul Moore07feee82009-03-27 17:10:54 -04002125
2126 return NULL;
2127}
2128
2129/**
Casey Schauflere114e472008-02-04 22:29:50 -08002130 * smack_netlabel - Set the secattr on a socket
2131 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002132 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002133 *
2134 * Convert the outbound smack value (smk_out) to a
2135 * secattr and attach it to the socket.
2136 *
2137 * Returns 0 on success or an error code
2138 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002139static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002140{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002141 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002142 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002143 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002144
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002145 /*
2146 * Usually the netlabel code will handle changing the
2147 * packet labeling based on the label.
2148 * The case of a single label host is different, because
2149 * a single label host should never get a labeled packet
2150 * even though the label is usually associated with a packet
2151 * label.
2152 */
2153 local_bh_disable();
2154 bh_lock_sock_nested(sk);
2155
2156 if (ssp->smk_out == smack_net_ambient ||
2157 labeled == SMACK_UNLABELED_SOCKET)
2158 netlbl_sock_delattr(sk);
2159 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002160 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002161 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002162 }
2163
2164 bh_unlock_sock(sk);
2165 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002166
Casey Schauflere114e472008-02-04 22:29:50 -08002167 return rc;
2168}
2169
2170/**
Paul Moore07feee82009-03-27 17:10:54 -04002171 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2172 * @sk: the socket
2173 * @sap: the destination address
2174 *
2175 * Set the correct secattr for the given socket based on the destination
2176 * address and perform any outbound access checks needed.
2177 *
2178 * Returns 0 on success or an error code.
2179 *
2180 */
2181static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2182{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002183 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002184 int rc;
2185 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002186 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002187 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002188 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002189
2190 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002191 hkp = smack_host_label(sap);
2192 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002193#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002194 struct lsm_network_audit net;
2195
Eric Paris48c62af2012-04-02 13:15:44 -04002196 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2197 ad.a.u.net->family = sap->sin_family;
2198 ad.a.u.net->dport = sap->sin_port;
2199 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002200#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002201 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002202 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002203 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2204 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002205 } else {
2206 sk_lbl = SMACK_CIPSO_SOCKET;
2207 rc = 0;
2208 }
2209 rcu_read_unlock();
2210 if (rc != 0)
2211 return rc;
2212
2213 return smack_netlabel(sk, sk_lbl);
2214}
2215
2216/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002217 * smk_ipv6_port_label - Smack port access table management
2218 * @sock: socket
2219 * @address: address
2220 *
2221 * Create or update the port list entry
2222 */
2223static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2224{
2225 struct sock *sk = sock->sk;
2226 struct sockaddr_in6 *addr6;
2227 struct socket_smack *ssp = sock->sk->sk_security;
2228 struct smk_port_label *spp;
2229 unsigned short port = 0;
2230
2231 if (address == NULL) {
2232 /*
2233 * This operation is changing the Smack information
2234 * on the bound socket. Take the changes to the port
2235 * as well.
2236 */
2237 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2238 if (sk != spp->smk_sock)
2239 continue;
2240 spp->smk_in = ssp->smk_in;
2241 spp->smk_out = ssp->smk_out;
2242 return;
2243 }
2244 /*
2245 * A NULL address is only used for updating existing
2246 * bound entries. If there isn't one, it's OK.
2247 */
2248 return;
2249 }
2250
2251 addr6 = (struct sockaddr_in6 *)address;
2252 port = ntohs(addr6->sin6_port);
2253 /*
2254 * This is a special case that is safely ignored.
2255 */
2256 if (port == 0)
2257 return;
2258
2259 /*
2260 * Look for an existing port list entry.
2261 * This is an indication that a port is getting reused.
2262 */
2263 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2264 if (spp->smk_port != port)
2265 continue;
2266 spp->smk_port = port;
2267 spp->smk_sock = sk;
2268 spp->smk_in = ssp->smk_in;
2269 spp->smk_out = ssp->smk_out;
2270 return;
2271 }
2272
2273 /*
2274 * A new port entry is required.
2275 */
2276 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2277 if (spp == NULL)
2278 return;
2279
2280 spp->smk_port = port;
2281 spp->smk_sock = sk;
2282 spp->smk_in = ssp->smk_in;
2283 spp->smk_out = ssp->smk_out;
2284
2285 list_add(&spp->list, &smk_ipv6_port_list);
2286 return;
2287}
2288
2289/**
2290 * smk_ipv6_port_check - check Smack port access
2291 * @sock: socket
2292 * @address: address
2293 *
2294 * Create or update the port list entry
2295 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002296static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002297 int act)
2298{
2299 __be16 *bep;
2300 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002301 struct smk_port_label *spp;
2302 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002303 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002304 unsigned short port = 0;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002305 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002306 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002307 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002308#ifdef CONFIG_AUDIT
2309 struct lsm_network_audit net;
2310#endif
2311
2312 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002313 skp = smack_net_ambient;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002314 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002315 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002316 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002317 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002318 }
2319
2320 /*
2321 * Get the IP address and port from the address.
2322 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002323 port = ntohs(address->sin6_port);
2324 bep = (__be16 *)(&address->sin6_addr);
2325 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002326
2327 /*
2328 * It's remote, so port lookup does no good.
2329 */
2330 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2331 goto auditout;
2332
2333 /*
2334 * It's local so the send check has to have passed.
2335 */
2336 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002337 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002338 goto auditout;
2339 }
2340
2341 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2342 if (spp->smk_port != port)
2343 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002344 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002345 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002346 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002347 break;
2348 }
2349
2350auditout:
2351
2352#ifdef CONFIG_AUDIT
2353 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2354 ad.a.u.net->family = sk->sk_family;
2355 ad.a.u.net->dport = port;
2356 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002357 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002358 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002359 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002360#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002361 rc = smk_access(skp, object, MAY_WRITE, &ad);
2362 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2363 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002364}
2365
2366/**
Casey Schauflere114e472008-02-04 22:29:50 -08002367 * smack_inode_setsecurity - set smack xattrs
2368 * @inode: the object
2369 * @name: attribute name
2370 * @value: attribute value
2371 * @size: size of the attribute
2372 * @flags: unused
2373 *
2374 * Sets the named attribute in the appropriate blob
2375 *
2376 * Returns 0 on success, or an error code
2377 */
2378static int smack_inode_setsecurity(struct inode *inode, const char *name,
2379 const void *value, size_t size, int flags)
2380{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002381 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002382 struct inode_smack *nsp = inode->i_security;
2383 struct socket_smack *ssp;
2384 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002385 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002386
Casey Schauflerf7112e62012-05-06 15:22:02 -07002387 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302388 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002389
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002390 skp = smk_import_entry(value, size);
2391 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002392 return -EINVAL;
2393
2394 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002395 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002396 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002397 return 0;
2398 }
2399 /*
2400 * The rest of the Smack xattrs are only on sockets.
2401 */
2402 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2403 return -EOPNOTSUPP;
2404
2405 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002406 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002407 return -EOPNOTSUPP;
2408
2409 ssp = sock->sk->sk_security;
2410
2411 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002412 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002413 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002414 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002415 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002416 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2417 if (rc != 0)
2418 printk(KERN_WARNING
2419 "Smack: \"%s\" netlbl error %d.\n",
2420 __func__, -rc);
2421 }
Casey Schauflere114e472008-02-04 22:29:50 -08002422 } else
2423 return -EOPNOTSUPP;
2424
Casey Schauflerc6739442013-05-22 18:42:56 -07002425 if (sock->sk->sk_family == PF_INET6)
2426 smk_ipv6_port_label(sock, NULL);
2427
Casey Schauflere114e472008-02-04 22:29:50 -08002428 return 0;
2429}
2430
2431/**
2432 * smack_socket_post_create - finish socket setup
2433 * @sock: the socket
2434 * @family: protocol family
2435 * @type: unused
2436 * @protocol: unused
2437 * @kern: unused
2438 *
2439 * Sets the netlabel information on the socket
2440 *
2441 * Returns 0 on success, and error code otherwise
2442 */
2443static int smack_socket_post_create(struct socket *sock, int family,
2444 int type, int protocol, int kern)
2445{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002446 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002447 return 0;
2448 /*
2449 * Set the outbound netlbl.
2450 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002451 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2452}
2453
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002454/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002455 * smack_socket_bind - record port binding information.
2456 * @sock: the socket
2457 * @address: the port address
2458 * @addrlen: size of the address
2459 *
2460 * Records the label bound to a port.
2461 *
2462 * Returns 0
2463 */
2464static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2465 int addrlen)
2466{
2467 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2468 smk_ipv6_port_label(sock, address);
2469
2470 return 0;
2471}
2472
2473/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002474 * smack_socket_connect - connect access check
2475 * @sock: the socket
2476 * @sap: the other end
2477 * @addrlen: size of sap
2478 *
2479 * Verifies that a connection may be possible
2480 *
2481 * Returns 0 on success, and error code otherwise
2482 */
2483static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2484 int addrlen)
2485{
Casey Schauflerc6739442013-05-22 18:42:56 -07002486 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002487
Casey Schauflerc6739442013-05-22 18:42:56 -07002488 if (sock->sk == NULL)
2489 return 0;
2490
2491 switch (sock->sk->sk_family) {
2492 case PF_INET:
2493 if (addrlen < sizeof(struct sockaddr_in))
2494 return -EINVAL;
2495 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2496 break;
2497 case PF_INET6:
2498 if (addrlen < sizeof(struct sockaddr_in6))
2499 return -EINVAL;
Casey Schaufler6ea06242013-08-05 13:21:22 -07002500 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2501 SMK_CONNECTING);
Casey Schauflerc6739442013-05-22 18:42:56 -07002502 break;
2503 }
2504 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002505}
2506
2507/**
2508 * smack_flags_to_may - convert S_ to MAY_ values
2509 * @flags: the S_ value
2510 *
2511 * Returns the equivalent MAY_ value
2512 */
2513static int smack_flags_to_may(int flags)
2514{
2515 int may = 0;
2516
2517 if (flags & S_IRUGO)
2518 may |= MAY_READ;
2519 if (flags & S_IWUGO)
2520 may |= MAY_WRITE;
2521 if (flags & S_IXUGO)
2522 may |= MAY_EXEC;
2523
2524 return may;
2525}
2526
2527/**
2528 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2529 * @msg: the object
2530 *
2531 * Returns 0
2532 */
2533static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2534{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002535 struct smack_known *skp = smk_of_current();
2536
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002537 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002538 return 0;
2539}
2540
2541/**
2542 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2543 * @msg: the object
2544 *
2545 * Clears the blob pointer
2546 */
2547static void smack_msg_msg_free_security(struct msg_msg *msg)
2548{
2549 msg->security = NULL;
2550}
2551
2552/**
2553 * smack_of_shm - the smack pointer for the shm
2554 * @shp: the object
2555 *
2556 * Returns a pointer to the smack value
2557 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002558static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
Casey Schauflere114e472008-02-04 22:29:50 -08002559{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002560 return (struct smack_known *)shp->shm_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002561}
2562
2563/**
2564 * smack_shm_alloc_security - Set the security blob for shm
2565 * @shp: the object
2566 *
2567 * Returns 0
2568 */
2569static int smack_shm_alloc_security(struct shmid_kernel *shp)
2570{
2571 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002572 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002573
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002574 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002575 return 0;
2576}
2577
2578/**
2579 * smack_shm_free_security - Clear the security blob for shm
2580 * @shp: the object
2581 *
2582 * Clears the blob pointer
2583 */
2584static void smack_shm_free_security(struct shmid_kernel *shp)
2585{
2586 struct kern_ipc_perm *isp = &shp->shm_perm;
2587
2588 isp->security = NULL;
2589}
2590
2591/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002592 * smk_curacc_shm : check if current has access on shm
2593 * @shp : the object
2594 * @access : access requested
2595 *
2596 * Returns 0 if current has the requested access, error code otherwise
2597 */
2598static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2599{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002600 struct smack_known *ssp = smack_of_shm(shp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002601 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002602 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002603
2604#ifdef CONFIG_AUDIT
2605 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2606 ad.a.u.ipc_id = shp->shm_perm.id;
2607#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002608 rc = smk_curacc(ssp, access, &ad);
2609 rc = smk_bu_current("shm", ssp, access, rc);
2610 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002611}
2612
2613/**
Casey Schauflere114e472008-02-04 22:29:50 -08002614 * smack_shm_associate - Smack access check for shm
2615 * @shp: the object
2616 * @shmflg: access requested
2617 *
2618 * Returns 0 if current has the requested access, error code otherwise
2619 */
2620static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2621{
Casey Schauflere114e472008-02-04 22:29:50 -08002622 int may;
2623
2624 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002625 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002626}
2627
2628/**
2629 * smack_shm_shmctl - Smack access check for shm
2630 * @shp: the object
2631 * @cmd: what it wants to do
2632 *
2633 * Returns 0 if current has the requested access, error code otherwise
2634 */
2635static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2636{
Casey Schauflere114e472008-02-04 22:29:50 -08002637 int may;
2638
2639 switch (cmd) {
2640 case IPC_STAT:
2641 case SHM_STAT:
2642 may = MAY_READ;
2643 break;
2644 case IPC_SET:
2645 case SHM_LOCK:
2646 case SHM_UNLOCK:
2647 case IPC_RMID:
2648 may = MAY_READWRITE;
2649 break;
2650 case IPC_INFO:
2651 case SHM_INFO:
2652 /*
2653 * System level information.
2654 */
2655 return 0;
2656 default:
2657 return -EINVAL;
2658 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002659 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002660}
2661
2662/**
2663 * smack_shm_shmat - Smack access for shmat
2664 * @shp: the object
2665 * @shmaddr: unused
2666 * @shmflg: access requested
2667 *
2668 * Returns 0 if current has the requested access, error code otherwise
2669 */
2670static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2671 int shmflg)
2672{
Casey Schauflere114e472008-02-04 22:29:50 -08002673 int may;
2674
2675 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002676 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002677}
2678
2679/**
2680 * smack_of_sem - the smack pointer for the sem
2681 * @sma: the object
2682 *
2683 * Returns a pointer to the smack value
2684 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002685static struct smack_known *smack_of_sem(struct sem_array *sma)
Casey Schauflere114e472008-02-04 22:29:50 -08002686{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002687 return (struct smack_known *)sma->sem_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002688}
2689
2690/**
2691 * smack_sem_alloc_security - Set the security blob for sem
2692 * @sma: the object
2693 *
2694 * Returns 0
2695 */
2696static int smack_sem_alloc_security(struct sem_array *sma)
2697{
2698 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002699 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002700
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002701 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002702 return 0;
2703}
2704
2705/**
2706 * smack_sem_free_security - Clear the security blob for sem
2707 * @sma: the object
2708 *
2709 * Clears the blob pointer
2710 */
2711static void smack_sem_free_security(struct sem_array *sma)
2712{
2713 struct kern_ipc_perm *isp = &sma->sem_perm;
2714
2715 isp->security = NULL;
2716}
2717
2718/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002719 * smk_curacc_sem : check if current has access on sem
2720 * @sma : the object
2721 * @access : access requested
2722 *
2723 * Returns 0 if current has the requested access, error code otherwise
2724 */
2725static int smk_curacc_sem(struct sem_array *sma, int access)
2726{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002727 struct smack_known *ssp = smack_of_sem(sma);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002728 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002729 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002730
2731#ifdef CONFIG_AUDIT
2732 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2733 ad.a.u.ipc_id = sma->sem_perm.id;
2734#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002735 rc = smk_curacc(ssp, access, &ad);
2736 rc = smk_bu_current("sem", ssp, access, rc);
2737 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002738}
2739
2740/**
Casey Schauflere114e472008-02-04 22:29:50 -08002741 * smack_sem_associate - Smack access check for sem
2742 * @sma: the object
2743 * @semflg: access requested
2744 *
2745 * Returns 0 if current has the requested access, error code otherwise
2746 */
2747static int smack_sem_associate(struct sem_array *sma, int semflg)
2748{
Casey Schauflere114e472008-02-04 22:29:50 -08002749 int may;
2750
2751 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002752 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002753}
2754
2755/**
2756 * smack_sem_shmctl - Smack access check for sem
2757 * @sma: the object
2758 * @cmd: what it wants to do
2759 *
2760 * Returns 0 if current has the requested access, error code otherwise
2761 */
2762static int smack_sem_semctl(struct sem_array *sma, int cmd)
2763{
Casey Schauflere114e472008-02-04 22:29:50 -08002764 int may;
2765
2766 switch (cmd) {
2767 case GETPID:
2768 case GETNCNT:
2769 case GETZCNT:
2770 case GETVAL:
2771 case GETALL:
2772 case IPC_STAT:
2773 case SEM_STAT:
2774 may = MAY_READ;
2775 break;
2776 case SETVAL:
2777 case SETALL:
2778 case IPC_RMID:
2779 case IPC_SET:
2780 may = MAY_READWRITE;
2781 break;
2782 case IPC_INFO:
2783 case SEM_INFO:
2784 /*
2785 * System level information
2786 */
2787 return 0;
2788 default:
2789 return -EINVAL;
2790 }
2791
Etienne Bassetecfcc532009-04-08 20:40:06 +02002792 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002793}
2794
2795/**
2796 * smack_sem_semop - Smack checks of semaphore operations
2797 * @sma: the object
2798 * @sops: unused
2799 * @nsops: unused
2800 * @alter: unused
2801 *
2802 * Treated as read and write in all cases.
2803 *
2804 * Returns 0 if access is allowed, error code otherwise
2805 */
2806static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2807 unsigned nsops, int alter)
2808{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002809 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002810}
2811
2812/**
2813 * smack_msg_alloc_security - Set the security blob for msg
2814 * @msq: the object
2815 *
2816 * Returns 0
2817 */
2818static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2819{
2820 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002821 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002822
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002823 kisp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002824 return 0;
2825}
2826
2827/**
2828 * smack_msg_free_security - Clear the security blob for msg
2829 * @msq: the object
2830 *
2831 * Clears the blob pointer
2832 */
2833static void smack_msg_queue_free_security(struct msg_queue *msq)
2834{
2835 struct kern_ipc_perm *kisp = &msq->q_perm;
2836
2837 kisp->security = NULL;
2838}
2839
2840/**
2841 * smack_of_msq - the smack pointer for the msq
2842 * @msq: the object
2843 *
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002844 * Returns a pointer to the smack label entry
Casey Schauflere114e472008-02-04 22:29:50 -08002845 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002846static struct smack_known *smack_of_msq(struct msg_queue *msq)
Casey Schauflere114e472008-02-04 22:29:50 -08002847{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002848 return (struct smack_known *)msq->q_perm.security;
Casey Schauflere114e472008-02-04 22:29:50 -08002849}
2850
2851/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002852 * smk_curacc_msq : helper to check if current has access on msq
2853 * @msq : the msq
2854 * @access : access requested
2855 *
2856 * return 0 if current has access, error otherwise
2857 */
2858static int smk_curacc_msq(struct msg_queue *msq, int access)
2859{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002860 struct smack_known *msp = smack_of_msq(msq);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002861 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002862 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002863
2864#ifdef CONFIG_AUDIT
2865 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2866 ad.a.u.ipc_id = msq->q_perm.id;
2867#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002868 rc = smk_curacc(msp, access, &ad);
2869 rc = smk_bu_current("msq", msp, access, rc);
2870 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002871}
2872
2873/**
Casey Schauflere114e472008-02-04 22:29:50 -08002874 * smack_msg_queue_associate - Smack access check for msg_queue
2875 * @msq: the object
2876 * @msqflg: access requested
2877 *
2878 * Returns 0 if current has the requested access, error code otherwise
2879 */
2880static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2881{
Casey Schauflere114e472008-02-04 22:29:50 -08002882 int may;
2883
2884 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002885 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002886}
2887
2888/**
2889 * smack_msg_queue_msgctl - Smack access check for msg_queue
2890 * @msq: the object
2891 * @cmd: what it wants to do
2892 *
2893 * Returns 0 if current has the requested access, error code otherwise
2894 */
2895static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2896{
Casey Schauflere114e472008-02-04 22:29:50 -08002897 int may;
2898
2899 switch (cmd) {
2900 case IPC_STAT:
2901 case MSG_STAT:
2902 may = MAY_READ;
2903 break;
2904 case IPC_SET:
2905 case IPC_RMID:
2906 may = MAY_READWRITE;
2907 break;
2908 case IPC_INFO:
2909 case MSG_INFO:
2910 /*
2911 * System level information
2912 */
2913 return 0;
2914 default:
2915 return -EINVAL;
2916 }
2917
Etienne Bassetecfcc532009-04-08 20:40:06 +02002918 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002919}
2920
2921/**
2922 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2923 * @msq: the object
2924 * @msg: unused
2925 * @msqflg: access requested
2926 *
2927 * Returns 0 if current has the requested access, error code otherwise
2928 */
2929static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2930 int msqflg)
2931{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002932 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002933
Etienne Bassetecfcc532009-04-08 20:40:06 +02002934 may = smack_flags_to_may(msqflg);
2935 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002936}
2937
2938/**
2939 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2940 * @msq: the object
2941 * @msg: unused
2942 * @target: unused
2943 * @type: unused
2944 * @mode: unused
2945 *
2946 * Returns 0 if current has read and write access, error code otherwise
2947 */
2948static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2949 struct task_struct *target, long type, int mode)
2950{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002951 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002952}
2953
2954/**
2955 * smack_ipc_permission - Smack access for ipc_permission()
2956 * @ipp: the object permissions
2957 * @flag: access requested
2958 *
2959 * Returns 0 if current has read and write access, error code otherwise
2960 */
2961static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2962{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002963 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002964 int may = smack_flags_to_may(flag);
2965 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002966 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002967
Etienne Bassetecfcc532009-04-08 20:40:06 +02002968#ifdef CONFIG_AUDIT
2969 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2970 ad.a.u.ipc_id = ipp->id;
2971#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002972 rc = smk_curacc(iskp, may, &ad);
2973 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002974 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002975}
2976
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002977/**
2978 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002979 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002980 * @secid: where result will be saved
2981 */
2982static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2983{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002984 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002985
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002986 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002987}
2988
Casey Schauflere114e472008-02-04 22:29:50 -08002989/**
2990 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002991 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002992 * @inode: the object
2993 *
2994 * Set the inode's security blob if it hasn't been done already.
2995 */
2996static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2997{
2998 struct super_block *sbp;
2999 struct superblock_smack *sbsp;
3000 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003001 struct smack_known *skp;
3002 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003003 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003004 char trattr[TRANS_TRUE_SIZE];
3005 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003006 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003007 struct dentry *dp;
3008
3009 if (inode == NULL)
3010 return;
3011
3012 isp = inode->i_security;
3013
3014 mutex_lock(&isp->smk_lock);
3015 /*
3016 * If the inode is already instantiated
3017 * take the quick way out
3018 */
3019 if (isp->smk_flags & SMK_INODE_INSTANT)
3020 goto unlockandout;
3021
3022 sbp = inode->i_sb;
3023 sbsp = sbp->s_security;
3024 /*
3025 * We're going to use the superblock default label
3026 * if there's no label on the file.
3027 */
3028 final = sbsp->smk_default;
3029
3030 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003031 * If this is the root inode the superblock
3032 * may be in the process of initialization.
3033 * If that is the case use the root value out
3034 * of the superblock.
3035 */
3036 if (opt_dentry->d_parent == opt_dentry) {
Casey Schaufler36ea7352014-04-28 15:23:01 -07003037 if (sbp->s_magic == CGROUP_SUPER_MAGIC) {
3038 /*
3039 * The cgroup filesystem is never mounted,
3040 * so there's no opportunity to set the mount
3041 * options.
3042 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003043 sbsp->smk_root = &smack_known_star;
3044 sbsp->smk_default = &smack_known_star;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003045 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003046 isp->smk_inode = sbsp->smk_root;
3047 isp->smk_flags |= SMK_INODE_INSTANT;
3048 goto unlockandout;
3049 }
3050
3051 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003052 * This is pretty hackish.
3053 * Casey says that we shouldn't have to do
3054 * file system specific code, but it does help
3055 * with keeping it simple.
3056 */
3057 switch (sbp->s_magic) {
3058 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003059 case PIPEFS_MAGIC:
3060 case SOCKFS_MAGIC:
3061 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003062 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003063 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003064 * that the smack file system doesn't do
3065 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003066 *
Casey Schauflere114e472008-02-04 22:29:50 -08003067 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003068 *
3069 * Socket access is controlled by the socket
3070 * structures associated with the task involved.
3071 *
3072 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003073 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003074 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003075 break;
3076 case DEVPTS_SUPER_MAGIC:
3077 /*
3078 * devpts seems content with the label of the task.
3079 * Programs that change smack have to treat the
3080 * pty with respect.
3081 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003082 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003083 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003084 case PROC_SUPER_MAGIC:
3085 /*
3086 * Casey says procfs appears not to care.
3087 * The superblock default suffices.
3088 */
3089 break;
3090 case TMPFS_MAGIC:
3091 /*
3092 * Device labels should come from the filesystem,
3093 * but watch out, because they're volitile,
3094 * getting recreated on every reboot.
3095 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003096 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003097 /*
3098 * No break.
3099 *
3100 * If a smack value has been set we want to use it,
3101 * but since tmpfs isn't giving us the opportunity
3102 * to set mount options simulate setting the
3103 * superblock default.
3104 */
3105 default:
3106 /*
3107 * This isn't an understood special case.
3108 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003109 */
3110
3111 /*
3112 * UNIX domain sockets use lower level socket data.
3113 */
3114 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003115 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003116 break;
3117 }
3118 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003119 * No xattr support means, alas, no SMACK label.
3120 * Use the aforeapplied default.
3121 * It would be curious if the label of the task
3122 * does not match that assigned.
3123 */
3124 if (inode->i_op->getxattr == NULL)
3125 break;
3126 /*
3127 * Get the dentry for xattr.
3128 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003129 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003130 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3131 if (skp != NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003132 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003133
3134 /*
3135 * Transmuting directory
3136 */
3137 if (S_ISDIR(inode->i_mode)) {
3138 /*
3139 * If this is a new directory and the label was
3140 * transmuted when the inode was initialized
3141 * set the transmute attribute on the directory
3142 * and mark the inode.
3143 *
3144 * If there is a transmute attribute on the
3145 * directory mark the inode.
3146 */
3147 if (isp->smk_flags & SMK_INODE_CHANGED) {
3148 isp->smk_flags &= ~SMK_INODE_CHANGED;
3149 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003150 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003151 TRANS_TRUE, TRANS_TRUE_SIZE,
3152 0);
3153 } else {
3154 rc = inode->i_op->getxattr(dp,
3155 XATTR_NAME_SMACKTRANSMUTE, trattr,
3156 TRANS_TRUE_SIZE);
3157 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3158 TRANS_TRUE_SIZE) != 0)
3159 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003160 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003161 if (rc >= 0)
3162 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003163 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003164 /*
3165 * Don't let the exec or mmap label be "*" or "@".
3166 */
3167 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3168 if (skp == &smack_known_star || skp == &smack_known_web)
3169 skp = NULL;
3170 isp->smk_task = skp;
3171 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3172 if (skp == &smack_known_star || skp == &smack_known_web)
3173 skp = NULL;
3174 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003175
Casey Schauflere114e472008-02-04 22:29:50 -08003176 dput(dp);
3177 break;
3178 }
3179
3180 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003181 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003182 else
3183 isp->smk_inode = final;
3184
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003185 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003186
3187unlockandout:
3188 mutex_unlock(&isp->smk_lock);
3189 return;
3190}
3191
3192/**
3193 * smack_getprocattr - Smack process attribute access
3194 * @p: the object task
3195 * @name: the name of the attribute in /proc/.../attr
3196 * @value: where to put the result
3197 *
3198 * Places a copy of the task Smack into value
3199 *
3200 * Returns the length of the smack label or an error code
3201 */
3202static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3203{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003204 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08003205 char *cp;
3206 int slen;
3207
3208 if (strcmp(name, "current") != 0)
3209 return -EINVAL;
3210
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003211 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003212 if (cp == NULL)
3213 return -ENOMEM;
3214
3215 slen = strlen(cp);
3216 *value = cp;
3217 return slen;
3218}
3219
3220/**
3221 * smack_setprocattr - Smack process attribute setting
3222 * @p: the object task
3223 * @name: the name of the attribute in /proc/.../attr
3224 * @value: the value to set
3225 * @size: the size of the value
3226 *
3227 * Sets the Smack value of the task. Only setting self
3228 * is permitted and only with privilege
3229 *
3230 * Returns the length of the smack label or an error code
3231 */
3232static int smack_setprocattr(struct task_struct *p, char *name,
3233 void *value, size_t size)
3234{
Casey Schaufler676dac42010-12-02 06:43:39 -08003235 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003236 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003237 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003238
Casey Schauflere114e472008-02-04 22:29:50 -08003239 /*
3240 * Changing another process' Smack value is too dangerous
3241 * and supports no sane use case.
3242 */
3243 if (p != current)
3244 return -EPERM;
3245
Casey Schaufler1880eff2012-06-05 15:28:30 -07003246 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003247 return -EPERM;
3248
Casey Schauflerf7112e62012-05-06 15:22:02 -07003249 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003250 return -EINVAL;
3251
3252 if (strcmp(name, "current") != 0)
3253 return -EINVAL;
3254
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003255 skp = smk_import_entry(value, size);
3256 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003257 return -EINVAL;
3258
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003259 /*
3260 * No process is ever allowed the web ("@") label.
3261 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003262 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003263 return -EPERM;
3264
David Howellsd84f4f92008-11-14 10:39:23 +11003265 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003266 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003267 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003268
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003269 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003270 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003271
David Howellsd84f4f92008-11-14 10:39:23 +11003272 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003273 return size;
3274}
3275
3276/**
3277 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003278 * @sock: one sock
3279 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003280 * @newsk: unused
3281 *
3282 * Return 0 if a subject with the smack of sock could access
3283 * an object with the smack of other, otherwise an error code
3284 */
David S. Miller3610cda2011-01-05 15:38:53 -08003285static int smack_unix_stream_connect(struct sock *sock,
3286 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003287{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003288 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003289 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003290 struct socket_smack *ssp = sock->sk_security;
3291 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003292 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003293 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003294 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003295#ifdef CONFIG_AUDIT
3296 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003297#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003298
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003299 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3300 skp = ssp->smk_out;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003301 okp = osp->smk_out;
3302#ifdef CONFIG_AUDIT
3303 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3304 smk_ad_setfield_u_net_sk(&ad, other);
3305#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003306 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3307 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003308 if (rc == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003309 rc = smk_access(okp, skp, MAY_WRITE, NULL);
3310 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003311 MAY_WRITE, rc);
3312 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003313 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003314
Casey Schaufler975d5e52011-09-26 14:43:39 -07003315 /*
3316 * Cross reference the peer labels for SO_PEERSEC.
3317 */
3318 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003319 nsp->smk_packet = ssp->smk_out;
3320 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003321 }
3322
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003323 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003324}
3325
3326/**
3327 * smack_unix_may_send - Smack access on UDS
3328 * @sock: one socket
3329 * @other: the other socket
3330 *
3331 * Return 0 if a subject with the smack of sock could access
3332 * an object with the smack of other, otherwise an error code
3333 */
3334static int smack_unix_may_send(struct socket *sock, struct socket *other)
3335{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003336 struct socket_smack *ssp = sock->sk->sk_security;
3337 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003338 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003339 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003340
Kees Cook923e9a12012-04-10 13:26:44 -07003341#ifdef CONFIG_AUDIT
3342 struct lsm_network_audit net;
3343
Eric Paris48c62af2012-04-02 13:15:44 -04003344 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003345 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003346#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003347
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003348 if (smack_privileged(CAP_MAC_OVERRIDE))
3349 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003350
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003351 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3352 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003353 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003354}
3355
3356/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003357 * smack_socket_sendmsg - Smack check based on destination host
3358 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003359 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003360 * @size: the size of the message
3361 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003362 * Return 0 if the current subject can write to the destination host.
3363 * For IPv4 this is only a question if the destination is a single label host.
3364 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003365 */
3366static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3367 int size)
3368{
3369 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003370 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003371 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003372
3373 /*
3374 * Perfectly reasonable for this to be NULL
3375 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003376 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003377 return 0;
3378
Casey Schauflerc6739442013-05-22 18:42:56 -07003379 switch (sip->sin_family) {
3380 case AF_INET:
3381 rc = smack_netlabel_send(sock->sk, sip);
3382 break;
3383 case AF_INET6:
3384 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3385 break;
3386 }
3387 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003388}
3389
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003390/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003391 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003392 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003393 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003394 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003395 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003396 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003397static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3398 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003399{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003400 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003401 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003402 int acat;
3403 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003404
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003405 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003406 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003407 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003408 * If there are flags but no level netlabel isn't
3409 * behaving the way we expect it to.
3410 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003411 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003412 * Without guidance regarding the smack value
3413 * for the packet fall back on the network
3414 * ambient value.
3415 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003416 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003417 list_for_each_entry(skp, &smack_known_list, list) {
3418 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003419 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003420 /*
3421 * Compare the catsets. Use the netlbl APIs.
3422 */
3423 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3424 if ((skp->smk_netlabel.flags &
3425 NETLBL_SECATTR_MLS_CAT) == 0)
3426 found = 1;
3427 break;
3428 }
3429 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003430 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3431 acat + 1);
3432 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003433 skp->smk_netlabel.attr.mls.cat,
3434 kcat + 1);
3435 if (acat < 0 || kcat < 0)
3436 break;
3437 }
3438 if (acat == kcat) {
3439 found = 1;
3440 break;
3441 }
Casey Schauflere114e472008-02-04 22:29:50 -08003442 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003443 rcu_read_unlock();
3444
3445 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003446 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003447
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003448 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003449 return &smack_known_web;
3450 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003451 }
3452 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3453 /*
3454 * Looks like a fallback, which gives us a secid.
3455 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003456 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003457 /*
3458 * This has got to be a bug because it is
3459 * impossible to specify a fallback without
3460 * specifying the label, which will ensure
3461 * it has a secid, and the only way to get a
3462 * secid is from a fallback.
3463 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003464 BUG_ON(skp == NULL);
3465 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003466 }
3467 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003468 * Without guidance regarding the smack value
3469 * for the packet fall back on the network
3470 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003471 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003472 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003473}
3474
Casey Schaufler6ea06242013-08-05 13:21:22 -07003475static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003476{
Casey Schauflerc6739442013-05-22 18:42:56 -07003477 u8 nexthdr;
3478 int offset;
3479 int proto = -EINVAL;
3480 struct ipv6hdr _ipv6h;
3481 struct ipv6hdr *ip6;
3482 __be16 frag_off;
3483 struct tcphdr _tcph, *th;
3484 struct udphdr _udph, *uh;
3485 struct dccp_hdr _dccph, *dh;
3486
3487 sip->sin6_port = 0;
3488
3489 offset = skb_network_offset(skb);
3490 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3491 if (ip6 == NULL)
3492 return -EINVAL;
3493 sip->sin6_addr = ip6->saddr;
3494
3495 nexthdr = ip6->nexthdr;
3496 offset += sizeof(_ipv6h);
3497 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3498 if (offset < 0)
3499 return -EINVAL;
3500
3501 proto = nexthdr;
3502 switch (proto) {
3503 case IPPROTO_TCP:
3504 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3505 if (th != NULL)
3506 sip->sin6_port = th->source;
3507 break;
3508 case IPPROTO_UDP:
3509 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3510 if (uh != NULL)
3511 sip->sin6_port = uh->source;
3512 break;
3513 case IPPROTO_DCCP:
3514 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3515 if (dh != NULL)
3516 sip->sin6_port = dh->dccph_sport;
3517 break;
3518 }
3519 return proto;
3520}
3521
Casey Schauflere114e472008-02-04 22:29:50 -08003522/**
3523 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3524 * @sk: socket
3525 * @skb: packet
3526 *
3527 * Returns 0 if the packet should be delivered, an error code otherwise
3528 */
3529static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3530{
3531 struct netlbl_lsm_secattr secattr;
3532 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003533 struct smack_known *skp;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003534 struct sockaddr_in6 sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003535 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003536 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003537#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003538 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003539#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003540 switch (sk->sk_family) {
3541 case PF_INET:
3542 /*
3543 * Translate what netlabel gave us.
3544 */
3545 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003546
Casey Schauflerc6739442013-05-22 18:42:56 -07003547 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3548 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003549 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003550 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003551 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003552
Casey Schauflerc6739442013-05-22 18:42:56 -07003553 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003554
Etienne Bassetecfcc532009-04-08 20:40:06 +02003555#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003556 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3557 ad.a.u.net->family = sk->sk_family;
3558 ad.a.u.net->netif = skb->skb_iif;
3559 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003560#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003561 /*
3562 * Receiving a packet requires that the other end
3563 * be able to write here. Read access is not required.
3564 * This is the simplist possible security model
3565 * for networking.
3566 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003567 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3568 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003569 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003570 if (rc != 0)
3571 netlbl_skbuff_err(skb, rc, 0);
3572 break;
3573 case PF_INET6:
3574 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3575 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3576 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3577 else
3578 rc = 0;
3579 break;
3580 }
Paul Moorea8134292008-10-10 10:16:31 -04003581 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003582}
3583
3584/**
3585 * smack_socket_getpeersec_stream - pull in packet label
3586 * @sock: the socket
3587 * @optval: user's destination
3588 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003589 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003590 *
3591 * returns zero on success, an error code otherwise
3592 */
3593static int smack_socket_getpeersec_stream(struct socket *sock,
3594 char __user *optval,
3595 int __user *optlen, unsigned len)
3596{
3597 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003598 char *rcp = "";
3599 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003600 int rc = 0;
3601
3602 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003603 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003604 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003605 slen = strlen(rcp) + 1;
3606 }
Casey Schauflere114e472008-02-04 22:29:50 -08003607
3608 if (slen > len)
3609 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003610 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003611 rc = -EFAULT;
3612
3613 if (put_user(slen, optlen) != 0)
3614 rc = -EFAULT;
3615
3616 return rc;
3617}
3618
3619
3620/**
3621 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003622 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003623 * @skb: packet data
3624 * @secid: pointer to where to put the secid of the packet
3625 *
3626 * Sets the netlabel socket state on sk from parent
3627 */
3628static int smack_socket_getpeersec_dgram(struct socket *sock,
3629 struct sk_buff *skb, u32 *secid)
3630
3631{
3632 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003633 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003634 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003635 int family = PF_UNSPEC;
3636 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003637 int rc;
3638
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003639 if (skb != NULL) {
3640 if (skb->protocol == htons(ETH_P_IP))
3641 family = PF_INET;
3642 else if (skb->protocol == htons(ETH_P_IPV6))
3643 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003644 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003645 if (family == PF_UNSPEC && sock != NULL)
3646 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003647
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003648 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003649 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003650 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003651 } else if (family == PF_INET || family == PF_INET6) {
3652 /*
3653 * Translate what netlabel gave us.
3654 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003655 if (sock != NULL && sock->sk != NULL)
3656 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003657 netlbl_secattr_init(&secattr);
3658 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3659 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003660 skp = smack_from_secattr(&secattr, ssp);
3661 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003662 }
3663 netlbl_secattr_destroy(&secattr);
3664 }
3665 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003666 if (s == 0)
3667 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003668 return 0;
3669}
3670
3671/**
Paul Moore07feee82009-03-27 17:10:54 -04003672 * smack_sock_graft - Initialize a newly created socket with an existing sock
3673 * @sk: child sock
3674 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003675 *
Paul Moore07feee82009-03-27 17:10:54 -04003676 * Set the smk_{in,out} state of an existing sock based on the process that
3677 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003678 */
3679static void smack_sock_graft(struct sock *sk, struct socket *parent)
3680{
3681 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003682 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003683
Paul Moore07feee82009-03-27 17:10:54 -04003684 if (sk == NULL ||
3685 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003686 return;
3687
3688 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003689 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003690 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003691 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003692}
3693
3694/**
3695 * smack_inet_conn_request - Smack access check on connect
3696 * @sk: socket involved
3697 * @skb: packet
3698 * @req: unused
3699 *
3700 * Returns 0 if a task with the packet label could write to
3701 * the socket, otherwise an error code
3702 */
3703static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3704 struct request_sock *req)
3705{
Paul Moore07feee82009-03-27 17:10:54 -04003706 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003707 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003708 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003709 struct netlbl_lsm_secattr secattr;
3710 struct sockaddr_in addr;
3711 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003712 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003713 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003714 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003715#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003716 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003717#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003718
Casey Schauflerc6739442013-05-22 18:42:56 -07003719 if (family == PF_INET6) {
3720 /*
3721 * Handle mapped IPv4 packets arriving
3722 * via IPv6 sockets. Don't set up netlabel
3723 * processing on IPv6.
3724 */
3725 if (skb->protocol == htons(ETH_P_IP))
3726 family = PF_INET;
3727 else
3728 return 0;
3729 }
Casey Schauflere114e472008-02-04 22:29:50 -08003730
Paul Moore07feee82009-03-27 17:10:54 -04003731 netlbl_secattr_init(&secattr);
3732 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003733 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003734 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003735 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003736 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003737 netlbl_secattr_destroy(&secattr);
3738
Etienne Bassetecfcc532009-04-08 20:40:06 +02003739#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003740 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3741 ad.a.u.net->family = family;
3742 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003743 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3744#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003745 /*
Paul Moore07feee82009-03-27 17:10:54 -04003746 * Receiving a packet requires that the other end be able to write
3747 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003748 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003749 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3750 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04003751 if (rc != 0)
3752 return rc;
3753
3754 /*
3755 * Save the peer's label in the request_sock so we can later setup
3756 * smk_packet in the child socket so that SO_PEERCRED can report it.
3757 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003758 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04003759
3760 /*
3761 * We need to decide if we want to label the incoming connection here
3762 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003763 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04003764 */
3765 hdr = ip_hdr(skb);
3766 addr.sin_addr.s_addr = hdr->saddr;
3767 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003768 hskp = smack_host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07003769 rcu_read_unlock();
3770
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003771 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003772 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003773 else
Paul Moore07feee82009-03-27 17:10:54 -04003774 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08003775
3776 return rc;
3777}
3778
Paul Moore07feee82009-03-27 17:10:54 -04003779/**
3780 * smack_inet_csk_clone - Copy the connection information to the new socket
3781 * @sk: the new socket
3782 * @req: the connection's request_sock
3783 *
3784 * Transfer the connection's peer label to the newly created socket.
3785 */
3786static void smack_inet_csk_clone(struct sock *sk,
3787 const struct request_sock *req)
3788{
3789 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003790 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04003791
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003792 if (req->peer_secid != 0) {
3793 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003794 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003795 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003796 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04003797}
3798
Casey Schauflere114e472008-02-04 22:29:50 -08003799/*
3800 * Key management security hooks
3801 *
3802 * Casey has not tested key support very heavily.
3803 * The permission check is most likely too restrictive.
3804 * If you care about keys please have a look.
3805 */
3806#ifdef CONFIG_KEYS
3807
3808/**
3809 * smack_key_alloc - Set the key security blob
3810 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11003811 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003812 * @flags: unused
3813 *
3814 * No allocation required
3815 *
3816 * Returns 0
3817 */
David Howellsd84f4f92008-11-14 10:39:23 +11003818static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08003819 unsigned long flags)
3820{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003821 struct smack_known *skp = smk_of_task(cred->security);
3822
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003823 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003824 return 0;
3825}
3826
3827/**
3828 * smack_key_free - Clear the key security blob
3829 * @key: the object
3830 *
3831 * Clear the blob pointer
3832 */
3833static void smack_key_free(struct key *key)
3834{
3835 key->security = NULL;
3836}
3837
3838/*
3839 * smack_key_permission - Smack access on a key
3840 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11003841 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08003842 * @perm: unused
3843 *
3844 * Return 0 if the task has read and write to the object,
3845 * an error code otherwise
3846 */
3847static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00003848 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08003849{
3850 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003851 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003852 struct smack_known *tkp = smk_of_task(cred->security);
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003853 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07003854 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003855
3856 keyp = key_ref_to_ptr(key_ref);
3857 if (keyp == NULL)
3858 return -EINVAL;
3859 /*
3860 * If the key hasn't been initialized give it access so that
3861 * it may do so.
3862 */
3863 if (keyp->security == NULL)
3864 return 0;
3865 /*
3866 * This should not occur
3867 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003868 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003869 return -EACCES;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003870#ifdef CONFIG_AUDIT
3871 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
3872 ad.a.u.key_struct.key = keyp->serial;
3873 ad.a.u.key_struct.key_desc = keyp->description;
3874#endif
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00003875 if (perm & KEY_NEED_READ)
3876 request = MAY_READ;
3877 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
3878 request = MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07003879 rc = smk_access(tkp, keyp->security, request, &ad);
3880 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
3881 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003882}
3883#endif /* CONFIG_KEYS */
3884
3885/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003886 * Smack Audit hooks
3887 *
3888 * Audit requires a unique representation of each Smack specific
3889 * rule. This unique representation is used to distinguish the
3890 * object to be audited from remaining kernel objects and also
3891 * works as a glue between the audit hooks.
3892 *
3893 * Since repository entries are added but never deleted, we'll use
3894 * the smack_known label address related to the given audit rule as
3895 * the needed unique representation. This also better fits the smack
3896 * model where nearly everything is a label.
3897 */
3898#ifdef CONFIG_AUDIT
3899
3900/**
3901 * smack_audit_rule_init - Initialize a smack audit rule
3902 * @field: audit rule fields given from user-space (audit.h)
3903 * @op: required testing operator (=, !=, >, <, ...)
3904 * @rulestr: smack label to be audited
3905 * @vrule: pointer to save our own audit rule representation
3906 *
3907 * Prepare to audit cases where (@field @op @rulestr) is true.
3908 * The label to be audited is created if necessay.
3909 */
3910static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3911{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003912 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003913 char **rule = (char **)vrule;
3914 *rule = NULL;
3915
3916 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3917 return -EINVAL;
3918
Al Viro5af75d82008-12-16 05:59:26 -05003919 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003920 return -EINVAL;
3921
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003922 skp = smk_import_entry(rulestr, 0);
3923 if (skp)
3924 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003925
3926 return 0;
3927}
3928
3929/**
3930 * smack_audit_rule_known - Distinguish Smack audit rules
3931 * @krule: rule of interest, in Audit kernel representation format
3932 *
3933 * This is used to filter Smack rules from remaining Audit ones.
3934 * If it's proved that this rule belongs to us, the
3935 * audit_rule_match hook will be called to do the final judgement.
3936 */
3937static int smack_audit_rule_known(struct audit_krule *krule)
3938{
3939 struct audit_field *f;
3940 int i;
3941
3942 for (i = 0; i < krule->field_count; i++) {
3943 f = &krule->fields[i];
3944
3945 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3946 return 1;
3947 }
3948
3949 return 0;
3950}
3951
3952/**
3953 * smack_audit_rule_match - Audit given object ?
3954 * @secid: security id for identifying the object to test
3955 * @field: audit rule flags given from user-space
3956 * @op: required testing operator
3957 * @vrule: smack internal rule presentation
3958 * @actx: audit context associated with the check
3959 *
3960 * The core Audit hook. It's used to take the decision of
3961 * whether to audit or not to audit a given object.
3962 */
3963static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3964 struct audit_context *actx)
3965{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003966 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003967 char *rule = vrule;
3968
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05003969 if (unlikely(!rule)) {
3970 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003971 return -ENOENT;
3972 }
3973
3974 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3975 return 0;
3976
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003977 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003978
3979 /*
3980 * No need to do string comparisons. If a match occurs,
3981 * both pointers will point to the same smack_known
3982 * label.
3983 */
Al Viro5af75d82008-12-16 05:59:26 -05003984 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003985 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003986 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003987 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003988
3989 return 0;
3990}
3991
3992/**
3993 * smack_audit_rule_free - free smack rule representation
3994 * @vrule: rule to be freed.
3995 *
3996 * No memory was allocated.
3997 */
3998static void smack_audit_rule_free(void *vrule)
3999{
4000 /* No-op */
4001}
4002
4003#endif /* CONFIG_AUDIT */
4004
Randy Dunlap251a2a92009-02-18 11:42:33 -08004005/**
David Quigley746df9b2013-05-22 12:50:35 -04004006 * smack_ismaclabel - check if xattr @name references a smack MAC label
4007 * @name: Full xattr name to check.
4008 */
4009static int smack_ismaclabel(const char *name)
4010{
4011 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4012}
4013
4014
4015/**
Casey Schauflere114e472008-02-04 22:29:50 -08004016 * smack_secid_to_secctx - return the smack label for a secid
4017 * @secid: incoming integer
4018 * @secdata: destination
4019 * @seclen: how long it is
4020 *
4021 * Exists for networking code.
4022 */
4023static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4024{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004025 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004026
Eric Parisd5630b92010-10-13 16:24:48 -04004027 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004028 *secdata = skp->smk_known;
4029 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004030 return 0;
4031}
4032
Randy Dunlap251a2a92009-02-18 11:42:33 -08004033/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004034 * smack_secctx_to_secid - return the secid for a smack label
4035 * @secdata: smack label
4036 * @seclen: how long result is
4037 * @secid: outgoing integer
4038 *
4039 * Exists for audit and networking code.
4040 */
David Howellse52c17642008-04-29 20:52:51 +01004041static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004042{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004043 struct smack_known *skp = smk_find_entry(secdata);
4044
4045 if (skp)
4046 *secid = skp->smk_secid;
4047 else
4048 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004049 return 0;
4050}
4051
Randy Dunlap251a2a92009-02-18 11:42:33 -08004052/**
Casey Schauflere114e472008-02-04 22:29:50 -08004053 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004054 * @secdata: unused
4055 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004056 *
4057 * Exists to make sure nothing gets done, and properly
4058 */
4059static void smack_release_secctx(char *secdata, u32 seclen)
4060{
4061}
4062
David P. Quigley1ee65e32009-09-03 14:25:57 -04004063static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4064{
4065 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4066}
4067
4068static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4069{
4070 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4071}
4072
4073static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4074{
4075 int len = 0;
4076 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4077
4078 if (len < 0)
4079 return len;
4080 *ctxlen = len;
4081 return 0;
4082}
4083
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02004084struct security_operations smack_ops = {
4085 .name = "smack",
4086
Ingo Molnar9e488582009-05-07 19:26:19 +10004087 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01004088 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08004089 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08004090
4091 .sb_alloc_security = smack_sb_alloc_security,
4092 .sb_free_security = smack_sb_free_security,
4093 .sb_copy_data = smack_sb_copy_data,
4094 .sb_kern_mount = smack_sb_kern_mount,
4095 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08004096
Casey Schaufler676dac42010-12-02 06:43:39 -08004097 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03004098 .bprm_committing_creds = smack_bprm_committing_creds,
4099 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08004100
Casey Schauflere114e472008-02-04 22:29:50 -08004101 .inode_alloc_security = smack_inode_alloc_security,
4102 .inode_free_security = smack_inode_free_security,
4103 .inode_init_security = smack_inode_init_security,
4104 .inode_link = smack_inode_link,
4105 .inode_unlink = smack_inode_unlink,
4106 .inode_rmdir = smack_inode_rmdir,
4107 .inode_rename = smack_inode_rename,
4108 .inode_permission = smack_inode_permission,
4109 .inode_setattr = smack_inode_setattr,
4110 .inode_getattr = smack_inode_getattr,
4111 .inode_setxattr = smack_inode_setxattr,
4112 .inode_post_setxattr = smack_inode_post_setxattr,
4113 .inode_getxattr = smack_inode_getxattr,
4114 .inode_removexattr = smack_inode_removexattr,
4115 .inode_getsecurity = smack_inode_getsecurity,
4116 .inode_setsecurity = smack_inode_setsecurity,
4117 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004118 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004119
4120 .file_permission = smack_file_permission,
4121 .file_alloc_security = smack_file_alloc_security,
4122 .file_free_security = smack_file_free_security,
4123 .file_ioctl = smack_file_ioctl,
4124 .file_lock = smack_file_lock,
4125 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04004126 .mmap_file = smack_mmap_file,
4127 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08004128 .file_set_fowner = smack_file_set_fowner,
4129 .file_send_sigiotask = smack_file_send_sigiotask,
4130 .file_receive = smack_file_receive,
4131
Eric Paris83d49852012-04-04 13:45:40 -04004132 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07004133
David Howellsee18d642009-09-02 09:14:21 +01004134 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11004135 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11004136 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01004137 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11004138 .kernel_act_as = smack_kernel_act_as,
4139 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08004140 .task_setpgid = smack_task_setpgid,
4141 .task_getpgid = smack_task_getpgid,
4142 .task_getsid = smack_task_getsid,
4143 .task_getsecid = smack_task_getsecid,
4144 .task_setnice = smack_task_setnice,
4145 .task_setioprio = smack_task_setioprio,
4146 .task_getioprio = smack_task_getioprio,
4147 .task_setscheduler = smack_task_setscheduler,
4148 .task_getscheduler = smack_task_getscheduler,
4149 .task_movememory = smack_task_movememory,
4150 .task_kill = smack_task_kill,
4151 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08004152 .task_to_inode = smack_task_to_inode,
4153
4154 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004155 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004156
4157 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4158 .msg_msg_free_security = smack_msg_msg_free_security,
4159
4160 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4161 .msg_queue_free_security = smack_msg_queue_free_security,
4162 .msg_queue_associate = smack_msg_queue_associate,
4163 .msg_queue_msgctl = smack_msg_queue_msgctl,
4164 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4165 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4166
4167 .shm_alloc_security = smack_shm_alloc_security,
4168 .shm_free_security = smack_shm_free_security,
4169 .shm_associate = smack_shm_associate,
4170 .shm_shmctl = smack_shm_shmctl,
4171 .shm_shmat = smack_shm_shmat,
4172
4173 .sem_alloc_security = smack_sem_alloc_security,
4174 .sem_free_security = smack_sem_free_security,
4175 .sem_associate = smack_sem_associate,
4176 .sem_semctl = smack_sem_semctl,
4177 .sem_semop = smack_sem_semop,
4178
Casey Schauflere114e472008-02-04 22:29:50 -08004179 .d_instantiate = smack_d_instantiate,
4180
4181 .getprocattr = smack_getprocattr,
4182 .setprocattr = smack_setprocattr,
4183
4184 .unix_stream_connect = smack_unix_stream_connect,
4185 .unix_may_send = smack_unix_may_send,
4186
4187 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07004188 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004189 .socket_connect = smack_socket_connect,
4190 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08004191 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4192 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4193 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4194 .sk_alloc_security = smack_sk_alloc_security,
4195 .sk_free_security = smack_sk_free_security,
4196 .sock_graft = smack_sock_graft,
4197 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04004198 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004199
Casey Schauflere114e472008-02-04 22:29:50 -08004200 /* key management security hooks */
4201#ifdef CONFIG_KEYS
4202 .key_alloc = smack_key_alloc,
4203 .key_free = smack_key_free,
4204 .key_permission = smack_key_permission,
4205#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004206
4207 /* Audit hooks */
4208#ifdef CONFIG_AUDIT
4209 .audit_rule_init = smack_audit_rule_init,
4210 .audit_rule_known = smack_audit_rule_known,
4211 .audit_rule_match = smack_audit_rule_match,
4212 .audit_rule_free = smack_audit_rule_free,
4213#endif /* CONFIG_AUDIT */
4214
David Quigley746df9b2013-05-22 12:50:35 -04004215 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08004216 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004217 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08004218 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04004219 .inode_notifysecctx = smack_inode_notifysecctx,
4220 .inode_setsecctx = smack_inode_setsecctx,
4221 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08004222};
4223
Etienne Basset7198e2e2009-03-24 20:53:24 +01004224
Casey Schaufler86812bb2012-04-17 18:55:46 -07004225static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004226{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004227 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004228 * Initialize rule list locks
4229 */
4230 mutex_init(&smack_known_huh.smk_rules_lock);
4231 mutex_init(&smack_known_hat.smk_rules_lock);
4232 mutex_init(&smack_known_floor.smk_rules_lock);
4233 mutex_init(&smack_known_star.smk_rules_lock);
4234 mutex_init(&smack_known_invalid.smk_rules_lock);
4235 mutex_init(&smack_known_web.smk_rules_lock);
4236 /*
4237 * Initialize rule lists
4238 */
4239 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4240 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4241 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4242 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4243 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4244 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4245 /*
4246 * Create the known labels list
4247 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004248 smk_insert_entry(&smack_known_huh);
4249 smk_insert_entry(&smack_known_hat);
4250 smk_insert_entry(&smack_known_star);
4251 smk_insert_entry(&smack_known_floor);
4252 smk_insert_entry(&smack_known_invalid);
4253 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004254}
4255
Casey Schauflere114e472008-02-04 22:29:50 -08004256/**
4257 * smack_init - initialize the smack system
4258 *
4259 * Returns 0
4260 */
4261static __init int smack_init(void)
4262{
David Howellsd84f4f92008-11-14 10:39:23 +11004263 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004264 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004265
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004266 if (!security_module_enable(&smack_ops))
4267 return 0;
4268
Rohit1a5b4722014-10-15 17:40:41 +05304269 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4270 if (!smack_inode_cache)
4271 return -ENOMEM;
4272
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004273 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4274 GFP_KERNEL);
Rohit1a5b4722014-10-15 17:40:41 +05304275 if (tsp == NULL) {
4276 kmem_cache_destroy(smack_inode_cache);
Casey Schaufler676dac42010-12-02 06:43:39 -08004277 return -ENOMEM;
Rohit1a5b4722014-10-15 17:40:41 +05304278 }
Casey Schaufler676dac42010-12-02 06:43:39 -08004279
Casey Schauflere114e472008-02-04 22:29:50 -08004280 printk(KERN_INFO "Smack: Initializing.\n");
4281
4282 /*
4283 * Set the security state for the initial task.
4284 */
David Howellsd84f4f92008-11-14 10:39:23 +11004285 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004286 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004287
Casey Schaufler86812bb2012-04-17 18:55:46 -07004288 /* initialize the smack_known_list */
4289 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004290
4291 /*
4292 * Register with LSM
4293 */
4294 if (register_security(&smack_ops))
4295 panic("smack: Unable to register with kernel.\n");
4296
4297 return 0;
4298}
4299
4300/*
4301 * Smack requires early initialization in order to label
4302 * all processes and objects when they are created.
4303 */
4304security_initcall(smack_init);