blob: 478d99e0a0ebb3d0544bc7b3b34a0787bd27b42e [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);
56
Casey Schauflerd166c802014-08-27 14:51:27 -070057#ifdef CONFIG_SECURITY_SMACK_BRINGUP
58static void smk_bu_mode(int mode, char *s)
59{
60 int i = 0;
61
62 if (mode & MAY_READ)
63 s[i++] = 'r';
64 if (mode & MAY_WRITE)
65 s[i++] = 'w';
66 if (mode & MAY_EXEC)
67 s[i++] = 'x';
68 if (mode & MAY_APPEND)
69 s[i++] = 'a';
70 if (mode & MAY_TRANSMUTE)
71 s[i++] = 't';
72 if (mode & MAY_LOCK)
73 s[i++] = 'l';
74 if (i == 0)
75 s[i++] = '-';
76 s[i] = '\0';
77}
78#endif
79
80#ifdef CONFIG_SECURITY_SMACK_BRINGUP
81static int smk_bu_note(char *note, struct smack_known *sskp, char *osp,
82 int mode, int rc)
83{
84 char acc[SMK_NUM_ACCESS_TYPE + 1];
85
86 if (rc <= 0)
87 return rc;
88
89 smk_bu_mode(mode, acc);
90 pr_info("Smack Bringup: (%s %s %s) %s\n",
91 sskp->smk_known, osp, acc, note);
92 return 0;
93}
94#else
95#define smk_bu_note(note, sskp, osp, mode, RC) (RC)
96#endif
97
98#ifdef CONFIG_SECURITY_SMACK_BRINGUP
99static int smk_bu_current(char *note, char *osp, int mode, int rc)
100{
101 struct task_smack *tsp = current_security();
102 char acc[SMK_NUM_ACCESS_TYPE + 1];
103
104 if (rc <= 0)
105 return rc;
106
107 smk_bu_mode(mode, acc);
108 pr_info("Smack Bringup: (%s %s %s) %s %s\n",
109 tsp->smk_task->smk_known, osp, acc, current->comm, note);
110 return 0;
111}
112#else
113#define smk_bu_current(note, osp, mode, RC) (RC)
114#endif
115
116#ifdef CONFIG_SECURITY_SMACK_BRINGUP
117static int smk_bu_task(struct task_struct *otp, int mode, int rc)
118{
119 struct task_smack *tsp = current_security();
120 struct task_smack *otsp = task_security(otp);
121 char acc[SMK_NUM_ACCESS_TYPE + 1];
122
123 if (rc <= 0)
124 return rc;
125
126 smk_bu_mode(mode, acc);
127 pr_info("Smack Bringup: (%s %s %s) %s to %s\n",
128 tsp->smk_task->smk_known, otsp->smk_task->smk_known, acc,
129 current->comm, otp->comm);
130 return 0;
131}
132#else
133#define smk_bu_task(otp, mode, RC) (RC)
134#endif
135
136#ifdef CONFIG_SECURITY_SMACK_BRINGUP
137static int smk_bu_inode(struct inode *inode, int mode, int rc)
138{
139 struct task_smack *tsp = current_security();
140 char acc[SMK_NUM_ACCESS_TYPE + 1];
141
142 if (rc <= 0)
143 return rc;
144
145 smk_bu_mode(mode, acc);
146 pr_info("Smack Bringup: (%s %s %s) inode=(%s %ld) %s\n",
147 tsp->smk_task->smk_known, smk_of_inode(inode), acc,
148 inode->i_sb->s_id, inode->i_ino, current->comm);
149 return 0;
150}
151#else
152#define smk_bu_inode(inode, mode, RC) (RC)
153#endif
154
155#ifdef CONFIG_SECURITY_SMACK_BRINGUP
156static int smk_bu_file(struct file *file, int mode, int rc)
157{
158 struct task_smack *tsp = current_security();
159 struct smack_known *sskp = tsp->smk_task;
160 struct inode *inode = file->f_inode;
161 char acc[SMK_NUM_ACCESS_TYPE + 1];
162
163 if (rc <= 0)
164 return rc;
165
166 smk_bu_mode(mode, acc);
167 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %s) %s\n",
168 sskp->smk_known, (char *)file->f_security, acc,
169 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
170 current->comm);
171 return 0;
172}
173#else
174#define smk_bu_file(file, mode, RC) (RC)
175#endif
176
177#ifdef CONFIG_SECURITY_SMACK_BRINGUP
178static int smk_bu_credfile(const struct cred *cred, struct file *file,
179 int mode, int rc)
180{
181 struct task_smack *tsp = cred->security;
182 struct smack_known *sskp = tsp->smk_task;
183 struct inode *inode = file->f_inode;
184 char acc[SMK_NUM_ACCESS_TYPE + 1];
185
186 if (rc <= 0)
187 return rc;
188
189 smk_bu_mode(mode, acc);
190 pr_info("Smack Bringup: (%s %s %s) file=(%s %ld %s) %s\n",
191 sskp->smk_known, smk_of_inode(inode), acc,
192 inode->i_sb->s_id, inode->i_ino, file->f_dentry->d_name.name,
193 current->comm);
194 return 0;
195}
196#else
197#define smk_bu_credfile(cred, file, mode, RC) (RC)
198#endif
199
Casey Schauflere114e472008-02-04 22:29:50 -0800200/**
201 * smk_fetch - Fetch the smack label from a file.
202 * @ip: a pointer to the inode
203 * @dp: a pointer to the dentry
204 *
205 * Returns a pointer to the master list entry for the Smack label
206 * or NULL if there was no label to fetch.
207 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700208static struct smack_known *smk_fetch(const char *name, struct inode *ip,
209 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800210{
211 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700212 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700213 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800214
215 if (ip->i_op->getxattr == NULL)
216 return NULL;
217
Casey Schauflerf7112e62012-05-06 15:22:02 -0700218 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
219 if (buffer == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -0800220 return NULL;
221
Casey Schauflerf7112e62012-05-06 15:22:02 -0700222 rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
223 if (rc > 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700224 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700225
226 kfree(buffer);
227
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700228 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800229}
230
231/**
232 * new_inode_smack - allocate an inode security blob
233 * @smack: a pointer to the Smack label to use in the blob
234 *
235 * Returns the new blob or NULL if there's no memory available
236 */
237struct inode_smack *new_inode_smack(char *smack)
238{
239 struct inode_smack *isp;
240
Tetsuo Handaceffec552012-03-29 16:19:05 +0900241 isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800242 if (isp == NULL)
243 return NULL;
244
245 isp->smk_inode = smack;
246 isp->smk_flags = 0;
247 mutex_init(&isp->smk_lock);
248
249 return isp;
250}
251
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800252/**
253 * new_task_smack - allocate a task security blob
254 * @smack: a pointer to the Smack label to use in the blob
255 *
256 * Returns the new blob or NULL if there's no memory available
257 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700258static struct task_smack *new_task_smack(struct smack_known *task,
259 struct smack_known *forked, gfp_t gfp)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800260{
261 struct task_smack *tsp;
262
263 tsp = kzalloc(sizeof(struct task_smack), gfp);
264 if (tsp == NULL)
265 return NULL;
266
267 tsp->smk_task = task;
268 tsp->smk_forked = forked;
269 INIT_LIST_HEAD(&tsp->smk_rules);
270 mutex_init(&tsp->smk_rules_lock);
271
272 return tsp;
273}
274
275/**
276 * smk_copy_rules - copy a rule set
277 * @nhead - new rules header pointer
278 * @ohead - old rules header pointer
279 *
280 * Returns 0 on success, -ENOMEM on error
281 */
282static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
283 gfp_t gfp)
284{
285 struct smack_rule *nrp;
286 struct smack_rule *orp;
287 int rc = 0;
288
289 INIT_LIST_HEAD(nhead);
290
291 list_for_each_entry_rcu(orp, ohead, list) {
292 nrp = kzalloc(sizeof(struct smack_rule), gfp);
293 if (nrp == NULL) {
294 rc = -ENOMEM;
295 break;
296 }
297 *nrp = *orp;
298 list_add_rcu(&nrp->list, nhead);
299 }
300 return rc;
301}
302
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100303/**
304 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
305 * @mode - input mode in form of PTRACE_MODE_*
306 *
307 * Returns a converted MAY_* mode usable by smack rules
308 */
309static inline unsigned int smk_ptrace_mode(unsigned int mode)
310{
311 switch (mode) {
312 case PTRACE_MODE_READ:
313 return MAY_READ;
314 case PTRACE_MODE_ATTACH:
315 return MAY_READWRITE;
316 }
317
318 return 0;
319}
320
321/**
322 * smk_ptrace_rule_check - helper for ptrace access
323 * @tracer: tracer process
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100324 * @tracee_label: label of the process that's about to be traced,
325 * the pointer must originate from smack structures
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100326 * @mode: ptrace attachment mode (PTRACE_MODE_*)
327 * @func: name of the function that called us, used for audit
328 *
329 * Returns 0 on access granted, -error on error
330 */
331static int smk_ptrace_rule_check(struct task_struct *tracer, char *tracee_label,
332 unsigned int mode, const char *func)
333{
334 int rc;
335 struct smk_audit_info ad, *saip = NULL;
336 struct task_smack *tsp;
337 struct smack_known *skp;
338
339 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
340 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
341 smk_ad_setfield_u_tsk(&ad, tracer);
342 saip = &ad;
343 }
344
345 tsp = task_security(tracer);
346 skp = smk_of_task(tsp);
347
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100348 if ((mode & PTRACE_MODE_ATTACH) &&
349 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
350 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
351 if (skp->smk_known == tracee_label)
352 rc = 0;
353 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
354 rc = -EACCES;
355 else if (capable(CAP_SYS_PTRACE))
356 rc = 0;
357 else
358 rc = -EACCES;
359
360 if (saip)
361 smack_log(skp->smk_known, tracee_label, 0, rc, saip);
362
363 return rc;
364 }
365
366 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100367 rc = smk_tskacc(tsp, tracee_label, smk_ptrace_mode(mode), saip);
368 return rc;
369}
370
Casey Schauflere114e472008-02-04 22:29:50 -0800371/*
372 * LSM hooks.
373 * We he, that is fun!
374 */
375
376/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000377 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800378 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100379 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800380 *
381 * Returns 0 if access is OK, an error code otherwise
382 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100383 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800384 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000385static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800386{
387 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700388 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800389
Ingo Molnar9e488582009-05-07 19:26:19 +1000390 rc = cap_ptrace_access_check(ctp, mode);
Casey Schauflere114e472008-02-04 22:29:50 -0800391 if (rc != 0)
392 return rc;
393
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700394 skp = smk_of_task(task_security(ctp));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200395
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100396 rc = smk_ptrace_rule_check(current, skp->smk_known, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100397 return rc;
398}
Casey Schauflere114e472008-02-04 22:29:50 -0800399
David Howells5cd9c582008-08-14 11:37:28 +0100400/**
401 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
402 * @ptp: parent task pointer
403 *
404 * Returns 0 if access is OK, an error code otherwise
405 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100406 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100407 */
408static int smack_ptrace_traceme(struct task_struct *ptp)
409{
410 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700411 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100412
413 rc = cap_ptrace_traceme(ptp);
414 if (rc != 0)
415 return rc;
416
Lukasz Pawelczyk959e6c72014-03-11 17:07:04 +0100417 skp = smk_of_task(current_security());
Etienne Bassetecfcc532009-04-08 20:40:06 +0200418
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100419 rc = smk_ptrace_rule_check(ptp, skp->smk_known,
420 PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800421 return rc;
422}
423
424/**
425 * smack_syslog - Smack approval on syslog
426 * @type: message type
427 *
Casey Schauflere114e472008-02-04 22:29:50 -0800428 * Returns 0 on success, error code otherwise.
429 */
Eric Paris12b30522010-11-15 18:36:29 -0500430static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800431{
Eric Paris12b30522010-11-15 18:36:29 -0500432 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700433 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800434
Casey Schaufler1880eff2012-06-05 15:28:30 -0700435 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800436 return 0;
437
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800438 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800439 rc = -EACCES;
440
441 return rc;
442}
443
444
445/*
446 * Superblock Hooks.
447 */
448
449/**
450 * smack_sb_alloc_security - allocate a superblock blob
451 * @sb: the superblock getting the blob
452 *
453 * Returns 0 on success or -ENOMEM on error.
454 */
455static int smack_sb_alloc_security(struct super_block *sb)
456{
457 struct superblock_smack *sbsp;
458
459 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
460
461 if (sbsp == NULL)
462 return -ENOMEM;
463
464 sbsp->smk_root = smack_known_floor.smk_known;
465 sbsp->smk_default = smack_known_floor.smk_known;
466 sbsp->smk_floor = smack_known_floor.smk_known;
467 sbsp->smk_hat = smack_known_hat.smk_known;
Casey Schauflere830b392013-05-22 18:43:07 -0700468 /*
469 * smk_initialized will be zero from kzalloc.
470 */
Casey Schauflere114e472008-02-04 22:29:50 -0800471 sb->s_security = sbsp;
472
473 return 0;
474}
475
476/**
477 * smack_sb_free_security - free a superblock blob
478 * @sb: the superblock getting the blob
479 *
480 */
481static void smack_sb_free_security(struct super_block *sb)
482{
483 kfree(sb->s_security);
484 sb->s_security = NULL;
485}
486
487/**
488 * smack_sb_copy_data - copy mount options data for processing
Casey Schauflere114e472008-02-04 22:29:50 -0800489 * @orig: where to start
Randy Dunlap251a2a92009-02-18 11:42:33 -0800490 * @smackopts: mount options string
Casey Schauflere114e472008-02-04 22:29:50 -0800491 *
492 * Returns 0 on success or -ENOMEM on error.
493 *
494 * Copy the Smack specific mount options out of the mount
495 * options list.
496 */
Eric Parise0007522008-03-05 10:31:54 -0500497static int smack_sb_copy_data(char *orig, char *smackopts)
Casey Schauflere114e472008-02-04 22:29:50 -0800498{
499 char *cp, *commap, *otheropts, *dp;
500
Casey Schauflere114e472008-02-04 22:29:50 -0800501 otheropts = (char *)get_zeroed_page(GFP_KERNEL);
502 if (otheropts == NULL)
503 return -ENOMEM;
504
505 for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
506 if (strstr(cp, SMK_FSDEFAULT) == cp)
507 dp = smackopts;
508 else if (strstr(cp, SMK_FSFLOOR) == cp)
509 dp = smackopts;
510 else if (strstr(cp, SMK_FSHAT) == cp)
511 dp = smackopts;
512 else if (strstr(cp, SMK_FSROOT) == cp)
513 dp = smackopts;
Casey Schauflere830b392013-05-22 18:43:07 -0700514 else if (strstr(cp, SMK_FSTRANS) == cp)
515 dp = smackopts;
Casey Schauflere114e472008-02-04 22:29:50 -0800516 else
517 dp = otheropts;
518
519 commap = strchr(cp, ',');
520 if (commap != NULL)
521 *commap = '\0';
522
523 if (*dp != '\0')
524 strcat(dp, ",");
525 strcat(dp, cp);
526 }
527
528 strcpy(orig, otheropts);
529 free_page((unsigned long)otheropts);
530
531 return 0;
532}
533
534/**
535 * smack_sb_kern_mount - Smack specific mount processing
536 * @sb: the file system superblock
James Morris12204e22008-12-19 10:44:42 +1100537 * @flags: the mount flags
Casey Schauflere114e472008-02-04 22:29:50 -0800538 * @data: the smack mount options
539 *
540 * Returns 0 on success, an error code on failure
541 */
James Morris12204e22008-12-19 10:44:42 +1100542static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
Casey Schauflere114e472008-02-04 22:29:50 -0800543{
544 struct dentry *root = sb->s_root;
545 struct inode *inode = root->d_inode;
546 struct superblock_smack *sp = sb->s_security;
547 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800548 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800549 char *op;
550 char *commap;
551 char *nsp;
Casey Schauflere830b392013-05-22 18:43:07 -0700552 int transmute = 0;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800553 int specified = 0;
Casey Schauflere114e472008-02-04 22:29:50 -0800554
Casey Schauflere830b392013-05-22 18:43:07 -0700555 if (sp->smk_initialized)
Casey Schauflere114e472008-02-04 22:29:50 -0800556 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700557
Casey Schauflere114e472008-02-04 22:29:50 -0800558 sp->smk_initialized = 1;
Casey Schauflere114e472008-02-04 22:29:50 -0800559
560 for (op = data; op != NULL; op = commap) {
561 commap = strchr(op, ',');
562 if (commap != NULL)
563 *commap++ = '\0';
564
565 if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
566 op += strlen(SMK_FSHAT);
567 nsp = smk_import(op, 0);
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800568 if (nsp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800569 sp->smk_hat = nsp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800570 specified = 1;
571 }
Casey Schauflere114e472008-02-04 22:29:50 -0800572 } else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
573 op += strlen(SMK_FSFLOOR);
574 nsp = smk_import(op, 0);
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800575 if (nsp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800576 sp->smk_floor = nsp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800577 specified = 1;
578 }
Casey Schauflere114e472008-02-04 22:29:50 -0800579 } else if (strncmp(op, SMK_FSDEFAULT,
580 strlen(SMK_FSDEFAULT)) == 0) {
581 op += strlen(SMK_FSDEFAULT);
582 nsp = smk_import(op, 0);
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800583 if (nsp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800584 sp->smk_default = nsp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800585 specified = 1;
586 }
Casey Schauflere114e472008-02-04 22:29:50 -0800587 } else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
588 op += strlen(SMK_FSROOT);
589 nsp = smk_import(op, 0);
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800590 if (nsp != NULL) {
Casey Schauflere114e472008-02-04 22:29:50 -0800591 sp->smk_root = nsp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800592 specified = 1;
593 }
Casey Schauflere830b392013-05-22 18:43:07 -0700594 } else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
595 op += strlen(SMK_FSTRANS);
596 nsp = smk_import(op, 0);
597 if (nsp != NULL) {
598 sp->smk_root = nsp;
599 transmute = 1;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800600 specified = 1;
Casey Schauflere830b392013-05-22 18:43:07 -0700601 }
Casey Schauflere114e472008-02-04 22:29:50 -0800602 }
603 }
604
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800605 if (!smack_privileged(CAP_MAC_ADMIN)) {
606 /*
607 * Unprivileged mounts don't get to specify Smack values.
608 */
609 if (specified)
610 return -EPERM;
611 /*
612 * Unprivileged mounts get root and default from the caller.
613 */
614 skp = smk_of_current();
615 sp->smk_root = skp->smk_known;
616 sp->smk_default = skp->smk_known;
617 }
Casey Schauflere114e472008-02-04 22:29:50 -0800618 /*
619 * Initialize the root inode.
620 */
621 isp = inode->i_security;
José Bollo55dfc5d2014-01-08 15:53:05 +0100622 if (isp == NULL) {
623 isp = new_inode_smack(sp->smk_root);
624 if (isp == NULL)
625 return -ENOMEM;
626 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700627 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800628 isp->smk_inode = sp->smk_root;
629
Casey Schauflere830b392013-05-22 18:43:07 -0700630 if (transmute)
631 isp->smk_flags |= SMK_INODE_TRANSMUTE;
632
Casey Schauflere114e472008-02-04 22:29:50 -0800633 return 0;
634}
635
636/**
637 * smack_sb_statfs - Smack check on statfs
638 * @dentry: identifies the file system in question
639 *
640 * Returns 0 if current can read the floor of the filesystem,
641 * and error code otherwise
642 */
643static int smack_sb_statfs(struct dentry *dentry)
644{
645 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200646 int rc;
647 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800648
Eric Parisa2694342011-04-25 13:10:27 -0400649 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200650 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
651
652 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700653 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200654 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800655}
656
Casey Schauflere114e472008-02-04 22:29:50 -0800657/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800658 * BPRM hooks
659 */
660
Casey Schauflerce8a4322011-09-29 18:21:01 -0700661/**
662 * smack_bprm_set_creds - set creds for exec
663 * @bprm: the exec information
664 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100665 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700666 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800667static int smack_bprm_set_creds(struct linux_binprm *bprm)
668{
Al Viro496ad9a2013-01-23 17:07:38 -0500669 struct inode *inode = file_inode(bprm->file);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300670 struct task_smack *bsp = bprm->cred->security;
Casey Schaufler676dac42010-12-02 06:43:39 -0800671 struct inode_smack *isp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800672 int rc;
673
674 rc = cap_bprm_set_creds(bprm);
675 if (rc != 0)
676 return rc;
677
678 if (bprm->cred_prepared)
679 return 0;
680
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300681 isp = inode->i_security;
682 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800683 return 0;
684
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100685 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
686 struct task_struct *tracer;
687 rc = 0;
688
689 rcu_read_lock();
690 tracer = ptrace_parent(current);
691 if (likely(tracer != NULL))
692 rc = smk_ptrace_rule_check(tracer,
693 isp->smk_task->smk_known,
694 PTRACE_MODE_ATTACH,
695 __func__);
696 rcu_read_unlock();
697
698 if (rc != 0)
699 return rc;
700 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300701 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800702
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300703 bsp->smk_task = isp->smk_task;
704 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800705
706 return 0;
707}
708
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300709/**
710 * smack_bprm_committing_creds - Prepare to install the new credentials
711 * from bprm.
712 *
713 * @bprm: binprm for exec
714 */
715static void smack_bprm_committing_creds(struct linux_binprm *bprm)
716{
717 struct task_smack *bsp = bprm->cred->security;
718
719 if (bsp->smk_task != bsp->smk_forked)
720 current->pdeath_signal = 0;
721}
722
723/**
724 * smack_bprm_secureexec - Return the decision to use secureexec.
725 * @bprm: binprm for exec
726 *
727 * Returns 0 on success.
728 */
729static int smack_bprm_secureexec(struct linux_binprm *bprm)
730{
731 struct task_smack *tsp = current_security();
732 int ret = cap_bprm_secureexec(bprm);
733
734 if (!ret && (tsp->smk_task != tsp->smk_forked))
735 ret = 1;
736
737 return ret;
738}
739
Casey Schaufler676dac42010-12-02 06:43:39 -0800740/*
Casey Schauflere114e472008-02-04 22:29:50 -0800741 * Inode hooks
742 */
743
744/**
745 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800746 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800747 *
748 * Returns 0 if it gets a blob, -ENOMEM otherwise
749 */
750static int smack_inode_alloc_security(struct inode *inode)
751{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700752 struct smack_known *skp = smk_of_current();
753
754 inode->i_security = new_inode_smack(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -0800755 if (inode->i_security == NULL)
756 return -ENOMEM;
757 return 0;
758}
759
760/**
761 * smack_inode_free_security - free an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800762 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800763 *
764 * Clears the blob pointer in inode
765 */
766static void smack_inode_free_security(struct inode *inode)
767{
768 kfree(inode->i_security);
769 inode->i_security = NULL;
770}
771
772/**
773 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200774 * @inode: the newly created inode
775 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500776 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800777 * @name: where to put the attribute name
778 * @value: where to put the attribute value
779 * @len: where to put the length of the attribute
780 *
781 * Returns 0 if it all works out, -ENOMEM if there's no memory
782 */
783static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900784 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500785 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800786{
Casey Schaufler2267b132012-03-13 19:14:19 -0700787 struct inode_smack *issp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700788 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800789 char *isp = smk_of_inode(inode);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200790 char *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800791 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800792
Tetsuo Handa95489062013-07-25 05:44:02 +0900793 if (name)
794 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800795
796 if (value) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800797 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700798 may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800799 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200800
801 /*
802 * If the access rule allows transmutation and
803 * the directory requests transmutation then
804 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700805 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200806 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800807 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700808 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200809 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700810 issp->smk_flags |= SMK_INODE_CHANGED;
811 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200812
Tetsuo Handaceffec552012-03-29 16:19:05 +0900813 *value = kstrdup(isp, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800814 if (*value == NULL)
815 return -ENOMEM;
816 }
817
818 if (len)
Konstantin Khlebnikovda1b6352014-08-07 20:52:49 +0400819 *len = strlen(isp);
Casey Schauflere114e472008-02-04 22:29:50 -0800820
821 return 0;
822}
823
824/**
825 * smack_inode_link - Smack check on link
826 * @old_dentry: the existing object
827 * @dir: unused
828 * @new_dentry: the new object
829 *
830 * Returns 0 if access is permitted, an error code otherwise
831 */
832static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
833 struct dentry *new_dentry)
834{
Casey Schauflere114e472008-02-04 22:29:50 -0800835 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200836 struct smk_audit_info ad;
837 int rc;
838
Eric Parisa2694342011-04-25 13:10:27 -0400839 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200840 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800841
842 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200843 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700844 rc = smk_bu_inode(old_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800845
846 if (rc == 0 && new_dentry->d_inode != NULL) {
847 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200848 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
849 rc = smk_curacc(isp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700850 rc = smk_bu_inode(new_dentry->d_inode, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800851 }
852
853 return rc;
854}
855
856/**
857 * smack_inode_unlink - Smack check on inode deletion
858 * @dir: containing directory object
859 * @dentry: file to unlink
860 *
861 * Returns 0 if current can write the containing directory
862 * and the object, error code otherwise
863 */
864static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
865{
866 struct inode *ip = dentry->d_inode;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200867 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800868 int rc;
869
Eric Parisa2694342011-04-25 13:10:27 -0400870 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200871 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
872
Casey Schauflere114e472008-02-04 22:29:50 -0800873 /*
874 * You need write access to the thing you're unlinking
875 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200876 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700877 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200878 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800879 /*
880 * You also need write access to the containing directory
881 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400882 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200883 smk_ad_setfield_u_fs_inode(&ad, dir);
884 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700885 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200886 }
Casey Schauflere114e472008-02-04 22:29:50 -0800887 return rc;
888}
889
890/**
891 * smack_inode_rmdir - Smack check on directory deletion
892 * @dir: containing directory object
893 * @dentry: directory to unlink
894 *
895 * Returns 0 if current can write the containing directory
896 * and the directory, error code otherwise
897 */
898static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
899{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200900 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800901 int rc;
902
Eric Parisa2694342011-04-25 13:10:27 -0400903 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200904 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
905
Casey Schauflere114e472008-02-04 22:29:50 -0800906 /*
907 * You need write access to the thing you're removing
908 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200909 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700910 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200911 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800912 /*
913 * You also need write access to the containing directory
914 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400915 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200916 smk_ad_setfield_u_fs_inode(&ad, dir);
917 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700918 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200919 }
Casey Schauflere114e472008-02-04 22:29:50 -0800920
921 return rc;
922}
923
924/**
925 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200926 * @old_inode: unused
927 * @old_dentry: the old object
928 * @new_inode: unused
929 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -0800930 *
931 * Read and write access is required on both the old and
932 * new directories.
933 *
934 * Returns 0 if access is permitted, an error code otherwise
935 */
936static int smack_inode_rename(struct inode *old_inode,
937 struct dentry *old_dentry,
938 struct inode *new_inode,
939 struct dentry *new_dentry)
940{
941 int rc;
942 char *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200943 struct smk_audit_info ad;
944
Eric Parisa2694342011-04-25 13:10:27 -0400945 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200946 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800947
948 isp = smk_of_inode(old_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200949 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700950 rc = smk_bu_inode(old_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800951
952 if (rc == 0 && new_dentry->d_inode != NULL) {
953 isp = smk_of_inode(new_dentry->d_inode);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200954 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
955 rc = smk_curacc(isp, MAY_READWRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700956 rc = smk_bu_inode(new_dentry->d_inode, MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800957 }
Casey Schauflere114e472008-02-04 22:29:50 -0800958 return rc;
959}
960
961/**
962 * smack_inode_permission - Smack version of permission()
963 * @inode: the inode in question
964 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -0800965 *
966 * This is the important Smack hook.
967 *
968 * Returns 0 if access is permitted, -EACCES otherwise
969 */
Al Viroe74f71e2011-06-20 19:38:15 -0400970static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -0800971{
Etienne Bassetecfcc532009-04-08 20:40:06 +0200972 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -0400973 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -0700974 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -0400975
976 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -0800977 /*
978 * No permission to check. Existence test. Yup, it's there.
979 */
980 if (mask == 0)
981 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700982
983 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -0400984 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -0700985 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -0400986 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200987 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700988 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
989 rc = smk_bu_inode(inode, mask, rc);
990 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800991}
992
993/**
994 * smack_inode_setattr - Smack check for setting attributes
995 * @dentry: the object
996 * @iattr: for the force flag
997 *
998 * Returns 0 if access is permitted, an error code otherwise
999 */
1000static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1001{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001002 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001003 int rc;
1004
Casey Schauflere114e472008-02-04 22:29:50 -08001005 /*
1006 * Need to allow for clearing the setuid bit.
1007 */
1008 if (iattr->ia_valid & ATTR_FORCE)
1009 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001010 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001011 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001012
Casey Schauflerd166c802014-08-27 14:51:27 -07001013 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
1014 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1015 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001016}
1017
1018/**
1019 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001020 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001021 * @dentry: the object
1022 *
1023 * Returns 0 if access is permitted, an error code otherwise
1024 */
1025static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
1026{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001027 struct smk_audit_info ad;
Eric Parisa2694342011-04-25 13:10:27 -04001028 struct path path;
Casey Schauflerd166c802014-08-27 14:51:27 -07001029 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001030
Eric Parisa2694342011-04-25 13:10:27 -04001031 path.dentry = dentry;
1032 path.mnt = mnt;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001033
Eric Parisf48b7392011-04-25 12:54:27 -04001034 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Eric Parisa2694342011-04-25 13:10:27 -04001035 smk_ad_setfield_u_fs_path(&ad, path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001036 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1037 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1038 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001039}
1040
1041/**
1042 * smack_inode_setxattr - Smack check for setting xattrs
1043 * @dentry: the object
1044 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001045 * @value: value of the attribute
1046 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001047 * @flags: unused
1048 *
1049 * This protects the Smack attribute explicitly.
1050 *
1051 * Returns 0 if access is permitted, an error code otherwise
1052 */
David Howells8f0cfa52008-04-29 00:59:41 -07001053static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1054 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001055{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001056 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001057 struct smack_known *skp;
1058 int check_priv = 0;
1059 int check_import = 0;
1060 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001061 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001062
Casey Schaufler19760ad2013-12-16 16:27:26 -08001063 /*
1064 * Check label validity here so import won't fail in post_setxattr
1065 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001066 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1067 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001068 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1069 check_priv = 1;
1070 check_import = 1;
1071 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1072 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1073 check_priv = 1;
1074 check_import = 1;
1075 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001076 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001077 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001078 if (size != TRANS_TRUE_SIZE ||
1079 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1080 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001081 } else
1082 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1083
Casey Schaufler19760ad2013-12-16 16:27:26 -08001084 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1085 rc = -EPERM;
1086
1087 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001088 skp = size ? smk_import_entry(value, size) : NULL;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001089 if (skp == NULL || (check_star &&
1090 (skp == &smack_known_star || skp == &smack_known_web)))
1091 rc = -EINVAL;
1092 }
1093
Eric Parisa2694342011-04-25 13:10:27 -04001094 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001095 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1096
Casey Schauflerd166c802014-08-27 14:51:27 -07001097 if (rc == 0) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001098 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001099 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
1100 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001101
1102 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001103}
1104
1105/**
1106 * smack_inode_post_setxattr - Apply the Smack update approved above
1107 * @dentry: object
1108 * @name: attribute name
1109 * @value: attribute value
1110 * @size: attribute size
1111 * @flags: unused
1112 *
1113 * Set the pointer in the inode blob to the entry found
1114 * in the master label list.
1115 */
David Howells8f0cfa52008-04-29 00:59:41 -07001116static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1117 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001118{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001119 struct smack_known *skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001120 struct inode_smack *isp = dentry->d_inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001121
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001122 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1123 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1124 return;
1125 }
1126
Casey Schaufler676dac42010-12-02 06:43:39 -08001127 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001128 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001129 if (skp != NULL)
1130 isp->smk_inode = skp->smk_known;
Casey Schaufler676dac42010-12-02 06:43:39 -08001131 else
1132 isp->smk_inode = smack_known_invalid.smk_known;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001133 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001134 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001135 if (skp != NULL)
1136 isp->smk_task = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08001137 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001138 isp->smk_task = &smack_known_invalid;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001139 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001140 skp = smk_import_entry(value, size);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001141 if (skp != NULL)
1142 isp->smk_mmap = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001143 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001144 isp->smk_mmap = &smack_known_invalid;
1145 }
Casey Schauflere114e472008-02-04 22:29:50 -08001146
1147 return;
1148}
1149
Casey Schauflerce8a4322011-09-29 18:21:01 -07001150/**
Casey Schauflere114e472008-02-04 22:29:50 -08001151 * smack_inode_getxattr - Smack check on getxattr
1152 * @dentry: the object
1153 * @name: unused
1154 *
1155 * Returns 0 if access is permitted, an error code otherwise
1156 */
David Howells8f0cfa52008-04-29 00:59:41 -07001157static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001158{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001159 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001160 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001161
Eric Parisa2694342011-04-25 13:10:27 -04001162 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001163 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1164
Casey Schauflerd166c802014-08-27 14:51:27 -07001165 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1166 rc = smk_bu_inode(dentry->d_inode, MAY_READ, rc);
1167 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001168}
1169
Casey Schauflerce8a4322011-09-29 18:21:01 -07001170/**
Casey Schauflere114e472008-02-04 22:29:50 -08001171 * smack_inode_removexattr - Smack check on removexattr
1172 * @dentry: the object
1173 * @name: name of the attribute
1174 *
1175 * Removing the Smack attribute requires CAP_MAC_ADMIN
1176 *
1177 * Returns 0 if access is permitted, an error code otherwise
1178 */
David Howells8f0cfa52008-04-29 00:59:41 -07001179static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001180{
Casey Schaufler676dac42010-12-02 06:43:39 -08001181 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001182 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001183 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001184
Casey Schauflerbcdca222008-02-23 15:24:04 -08001185 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1186 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001187 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001188 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001189 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301190 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001191 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001192 rc = -EPERM;
1193 } else
1194 rc = cap_inode_removexattr(dentry, name);
1195
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001196 if (rc != 0)
1197 return rc;
1198
Eric Parisa2694342011-04-25 13:10:27 -04001199 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001200 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001201
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001202 rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001203 rc = smk_bu_inode(dentry->d_inode, MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001204 if (rc != 0)
1205 return rc;
1206
1207 isp = dentry->d_inode->i_security;
1208 /*
1209 * Don't do anything special for these.
1210 * XATTR_NAME_SMACKIPIN
1211 * XATTR_NAME_SMACKIPOUT
1212 * XATTR_NAME_SMACKEXEC
1213 */
1214 if (strcmp(name, XATTR_NAME_SMACK) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001215 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001216 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001217 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001218 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1219 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001220
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001221 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001222}
1223
1224/**
1225 * smack_inode_getsecurity - get smack xattrs
1226 * @inode: the object
1227 * @name: attribute name
1228 * @buffer: where to put the result
Randy Dunlap251a2a92009-02-18 11:42:33 -08001229 * @alloc: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001230 *
1231 * Returns the size of the attribute or an error code
1232 */
1233static int smack_inode_getsecurity(const struct inode *inode,
1234 const char *name, void **buffer,
1235 bool alloc)
1236{
1237 struct socket_smack *ssp;
1238 struct socket *sock;
1239 struct super_block *sbp;
1240 struct inode *ip = (struct inode *)inode;
1241 char *isp;
1242 int ilen;
1243 int rc = 0;
1244
1245 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
1246 isp = smk_of_inode(inode);
Konstantin Khlebnikovda1b6352014-08-07 20:52:49 +04001247 ilen = strlen(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08001248 *buffer = isp;
1249 return ilen;
1250 }
1251
1252 /*
1253 * The rest of the Smack xattrs are only on sockets.
1254 */
1255 sbp = ip->i_sb;
1256 if (sbp->s_magic != SOCKFS_MAGIC)
1257 return -EOPNOTSUPP;
1258
1259 sock = SOCKET_I(ip);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08001260 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08001261 return -EOPNOTSUPP;
1262
1263 ssp = sock->sk->sk_security;
1264
1265 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07001266 isp = ssp->smk_in->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001267 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001268 isp = ssp->smk_out->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001269 else
1270 return -EOPNOTSUPP;
1271
Konstantin Khlebnikovda1b6352014-08-07 20:52:49 +04001272 ilen = strlen(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08001273 if (rc == 0) {
1274 *buffer = isp;
1275 rc = ilen;
1276 }
1277
1278 return rc;
1279}
1280
1281
1282/**
1283 * smack_inode_listsecurity - list the Smack attributes
1284 * @inode: the object
1285 * @buffer: where they go
1286 * @buffer_size: size of buffer
1287 *
1288 * Returns 0 on success, -EINVAL otherwise
1289 */
1290static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1291 size_t buffer_size)
1292{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001293 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001294
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001295 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001296 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001297
1298 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001299}
1300
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001301/**
1302 * smack_inode_getsecid - Extract inode's security id
1303 * @inode: inode to extract the info from
1304 * @secid: where result will be saved
1305 */
1306static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
1307{
1308 struct inode_smack *isp = inode->i_security;
1309
1310 *secid = smack_to_secid(isp->smk_inode);
1311}
1312
Casey Schauflere114e472008-02-04 22:29:50 -08001313/*
1314 * File Hooks
1315 */
1316
1317/**
1318 * smack_file_permission - Smack check on file operations
1319 * @file: unused
1320 * @mask: unused
1321 *
1322 * Returns 0
1323 *
1324 * Should access checks be done on each read or write?
1325 * UNICOS and SELinux say yes.
1326 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1327 *
1328 * I'll say no for now. Smack does not do the frequent
1329 * label changing that SELinux does.
1330 */
1331static int smack_file_permission(struct file *file, int mask)
1332{
1333 return 0;
1334}
1335
1336/**
1337 * smack_file_alloc_security - assign a file security blob
1338 * @file: the object
1339 *
1340 * The security blob for a file is a pointer to the master
1341 * label list, so no allocation is done.
1342 *
1343 * Returns 0
1344 */
1345static int smack_file_alloc_security(struct file *file)
1346{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001347 struct smack_known *skp = smk_of_current();
1348
1349 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001350 return 0;
1351}
1352
1353/**
1354 * smack_file_free_security - clear a file security blob
1355 * @file: the object
1356 *
1357 * The security blob for a file is a pointer to the master
1358 * label list, so no memory is freed.
1359 */
1360static void smack_file_free_security(struct file *file)
1361{
1362 file->f_security = NULL;
1363}
1364
1365/**
1366 * smack_file_ioctl - Smack check on ioctls
1367 * @file: the object
1368 * @cmd: what to do
1369 * @arg: unused
1370 *
1371 * Relies heavily on the correct use of the ioctl command conventions.
1372 *
1373 * Returns 0 if allowed, error code otherwise
1374 */
1375static int smack_file_ioctl(struct file *file, unsigned int cmd,
1376 unsigned long arg)
1377{
1378 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001379 struct smk_audit_info ad;
1380
Eric Parisf48b7392011-04-25 12:54:27 -04001381 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001382 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001383
Casey Schauflerd166c802014-08-27 14:51:27 -07001384 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001385 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001386 rc = smk_bu_file(file, MAY_WRITE, rc);
1387 }
Casey Schauflere114e472008-02-04 22:29:50 -08001388
Casey Schauflerd166c802014-08-27 14:51:27 -07001389 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02001390 rc = smk_curacc(file->f_security, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001391 rc = smk_bu_file(file, MAY_READ, rc);
1392 }
Casey Schauflere114e472008-02-04 22:29:50 -08001393
1394 return rc;
1395}
1396
1397/**
1398 * smack_file_lock - Smack check on file locking
1399 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001400 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001401 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001402 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001403 */
1404static int smack_file_lock(struct file *file, unsigned int cmd)
1405{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001406 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001407 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001408
Eric Paris92f42502011-04-25 13:15:55 -04001409 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1410 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflerd166c802014-08-27 14:51:27 -07001411 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
1412 rc = smk_bu_file(file, MAY_LOCK, rc);
1413 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001414}
1415
1416/**
1417 * smack_file_fcntl - Smack check on fcntl
1418 * @file: the object
1419 * @cmd: what action to check
1420 * @arg: unused
1421 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001422 * Generally these operations are harmless.
1423 * File locking operations present an obvious mechanism
1424 * for passing information, so they require write access.
1425 *
Casey Schauflere114e472008-02-04 22:29:50 -08001426 * Returns 0 if current has access, error code otherwise
1427 */
1428static int smack_file_fcntl(struct file *file, unsigned int cmd,
1429 unsigned long arg)
1430{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001431 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001432 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001433
Etienne Bassetecfcc532009-04-08 20:40:06 +02001434
Casey Schauflere114e472008-02-04 22:29:50 -08001435 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001436 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001437 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001438 case F_SETLK:
1439 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001440 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1441 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1442 rc = smk_curacc(file->f_security, MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001443 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001444 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001445 case F_SETOWN:
1446 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001447 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1448 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001449 rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001450 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001451 break;
1452 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001453 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001454 }
1455
1456 return rc;
1457}
1458
1459/**
Al Viroe5467852012-05-30 13:30:51 -04001460 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001461 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1462 * if mapping anonymous memory.
1463 * @file contains the file structure for file to map (may be NULL).
1464 * @reqprot contains the protection requested by the application.
1465 * @prot contains the protection that will be applied by the kernel.
1466 * @flags contains the operational flags.
1467 * Return 0 if permission is granted.
1468 */
Al Viroe5467852012-05-30 13:30:51 -04001469static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001470 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001471 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001472{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001473 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001474 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001475 struct smack_rule *srp;
1476 struct task_smack *tsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001477 char *osmack;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001478 struct inode_smack *isp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001479 int may;
1480 int mmay;
1481 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001482 int rc;
1483
Al Viro496ad9a2013-01-23 17:07:38 -05001484 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001485 return 0;
1486
Al Viro496ad9a2013-01-23 17:07:38 -05001487 isp = file_inode(file)->i_security;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001488 if (isp->smk_mmap == NULL)
1489 return 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001490 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001491
1492 tsp = current_security();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001493 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001494 rc = 0;
1495
1496 rcu_read_lock();
1497 /*
1498 * For each Smack rule associated with the subject
1499 * label verify that the SMACK64MMAP also has access
1500 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001501 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001502 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001503 osmack = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001504 /*
1505 * Matching labels always allows access.
1506 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001507 if (mkp->smk_known == osmack)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001508 continue;
1509 /*
1510 * If there is a matching local rule take
1511 * that into account as well.
1512 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001513 may = smk_access_entry(srp->smk_subject->smk_known, osmack,
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001514 &tsp->smk_rules);
1515 if (may == -ENOENT)
1516 may = srp->smk_access;
1517 else
1518 may &= srp->smk_access;
1519 /*
1520 * If may is zero the SMACK64MMAP subject can't
1521 * possibly have less access.
1522 */
1523 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001524 continue;
1525
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001526 /*
1527 * Fetch the global list entry.
1528 * If there isn't one a SMACK64MMAP subject
1529 * can't have as much access as current.
1530 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001531 mmay = smk_access_entry(mkp->smk_known, osmack,
1532 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001533 if (mmay == -ENOENT) {
1534 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001535 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001536 }
1537 /*
1538 * If there is a local entry it modifies the
1539 * potential access, too.
1540 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001541 tmay = smk_access_entry(mkp->smk_known, osmack,
1542 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001543 if (tmay != -ENOENT)
1544 mmay &= tmay;
1545
1546 /*
1547 * If there is any access available to current that is
1548 * not available to a SMACK64MMAP subject
1549 * deny access.
1550 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001551 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001552 rc = -EACCES;
1553 break;
1554 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001555 }
1556
1557 rcu_read_unlock();
1558
1559 return rc;
1560}
1561
1562/**
Casey Schauflere114e472008-02-04 22:29:50 -08001563 * smack_file_set_fowner - set the file security blob value
1564 * @file: object in question
1565 *
1566 * Returns 0
1567 * Further research may be required on this one.
1568 */
1569static int smack_file_set_fowner(struct file *file)
1570{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001571 struct smack_known *skp = smk_of_current();
1572
1573 file->f_security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08001574 return 0;
1575}
1576
1577/**
1578 * smack_file_send_sigiotask - Smack on sigio
1579 * @tsk: The target task
1580 * @fown: the object the signal come from
1581 * @signum: unused
1582 *
1583 * Allow a privileged task to get signals even if it shouldn't
1584 *
1585 * Returns 0 if a subject with the object's smack could
1586 * write to the task, an error code otherwise.
1587 */
1588static int smack_file_send_sigiotask(struct task_struct *tsk,
1589 struct fown_struct *fown, int signum)
1590{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001591 struct smack_known *skp;
1592 struct smack_known *tkp = smk_of_task(tsk->cred->security);
Casey Schauflere114e472008-02-04 22:29:50 -08001593 struct file *file;
1594 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001595 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001596
1597 /*
1598 * struct fown_struct is never outside the context of a struct file
1599 */
1600 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001601
Etienne Bassetecfcc532009-04-08 20:40:06 +02001602 /* we don't log here as rc can be overriden */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001603 skp = smk_find_entry(file->f_security);
1604 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, NULL);
Casey Schauflerd166c802014-08-27 14:51:27 -07001605 rc = smk_bu_note("sigiotask", skp, tkp->smk_known, MAY_WRITE, rc);
David Howells5cd9c582008-08-14 11:37:28 +01001606 if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001607 rc = 0;
1608
1609 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1610 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001611 smack_log(file->f_security, tkp->smk_known, MAY_WRITE, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001612 return rc;
1613}
1614
1615/**
1616 * smack_file_receive - Smack file receive check
1617 * @file: the object
1618 *
1619 * Returns 0 if current has access, error code otherwise
1620 */
1621static int smack_file_receive(struct file *file)
1622{
Casey Schauflerd166c802014-08-27 14:51:27 -07001623 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001624 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001625 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001626
Casey Schaufler4482a442013-12-30 17:37:45 -08001627 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001628 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001629 /*
1630 * This code relies on bitmasks.
1631 */
1632 if (file->f_mode & FMODE_READ)
1633 may = MAY_READ;
1634 if (file->f_mode & FMODE_WRITE)
1635 may |= MAY_WRITE;
1636
Casey Schauflerd166c802014-08-27 14:51:27 -07001637 rc = smk_curacc(file->f_security, may, &ad);
1638 rc = smk_bu_file(file, may, rc);
1639 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001640}
1641
Casey Schaufler531f1d42011-09-19 12:41:42 -07001642/**
Eric Paris83d49852012-04-04 13:45:40 -04001643 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001644 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001645 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001646 *
1647 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001648 * Allow the open only if the task has read access. There are
1649 * many read operations (e.g. fstat) that you can do with an
1650 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001651 *
1652 * Returns 0
1653 */
Eric Paris83d49852012-04-04 13:45:40 -04001654static int smack_file_open(struct file *file, const struct cred *cred)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001655{
Casey Schauflera6834c02014-04-21 11:10:26 -07001656 struct task_smack *tsp = cred->security;
Al Viro496ad9a2013-01-23 17:07:38 -05001657 struct inode_smack *isp = file_inode(file)->i_security;
Casey Schauflera6834c02014-04-21 11:10:26 -07001658 struct smk_audit_info ad;
1659 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001660
Marcin Niesluchowskid83d2c22014-08-19 14:26:32 +02001661 if (smack_privileged(CAP_MAC_OVERRIDE)) {
1662 file->f_security = isp->smk_inode;
Casey Schauflera6834c02014-04-21 11:10:26 -07001663 return 0;
Marcin Niesluchowskid83d2c22014-08-19 14:26:32 +02001664 }
Casey Schaufler531f1d42011-09-19 12:41:42 -07001665
Casey Schauflera6834c02014-04-21 11:10:26 -07001666 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1667 smk_ad_setfield_u_fs_path(&ad, file->f_path);
1668 rc = smk_access(tsp->smk_task, isp->smk_inode, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001669 rc = smk_bu_credfile(cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001670 if (rc == 0)
1671 file->f_security = isp->smk_inode;
1672
1673 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001674}
1675
Casey Schauflere114e472008-02-04 22:29:50 -08001676/*
1677 * Task hooks
1678 */
1679
1680/**
David Howellsee18d642009-09-02 09:14:21 +01001681 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1682 * @new: the new credentials
1683 * @gfp: the atomicity of any memory allocations
1684 *
1685 * Prepare a blank set of credentials for modification. This must allocate all
1686 * the memory the LSM module might require such that cred_transfer() can
1687 * complete without error.
1688 */
1689static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1690{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001691 struct task_smack *tsp;
1692
1693 tsp = new_task_smack(NULL, NULL, gfp);
1694 if (tsp == NULL)
Casey Schaufler676dac42010-12-02 06:43:39 -08001695 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001696
1697 cred->security = tsp;
1698
David Howellsee18d642009-09-02 09:14:21 +01001699 return 0;
1700}
1701
1702
1703/**
David Howellsf1752ee2008-11-14 10:39:17 +11001704 * smack_cred_free - "free" task-level security credentials
1705 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001706 *
Casey Schauflere114e472008-02-04 22:29:50 -08001707 */
David Howellsf1752ee2008-11-14 10:39:17 +11001708static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001709{
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001710 struct task_smack *tsp = cred->security;
1711 struct smack_rule *rp;
1712 struct list_head *l;
1713 struct list_head *n;
1714
1715 if (tsp == NULL)
1716 return;
1717 cred->security = NULL;
1718
1719 list_for_each_safe(l, n, &tsp->smk_rules) {
1720 rp = list_entry(l, struct smack_rule, list);
1721 list_del(&rp->list);
1722 kfree(rp);
1723 }
1724 kfree(tsp);
Casey Schauflere114e472008-02-04 22:29:50 -08001725}
1726
1727/**
David Howellsd84f4f92008-11-14 10:39:23 +11001728 * smack_cred_prepare - prepare new set of credentials for modification
1729 * @new: the new credentials
1730 * @old: the original credentials
1731 * @gfp: the atomicity of any memory allocations
1732 *
1733 * Prepare a new set of credentials for modification.
1734 */
1735static int smack_cred_prepare(struct cred *new, const struct cred *old,
1736 gfp_t gfp)
1737{
Casey Schaufler676dac42010-12-02 06:43:39 -08001738 struct task_smack *old_tsp = old->security;
1739 struct task_smack *new_tsp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001740 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001741
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001742 new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
Casey Schaufler676dac42010-12-02 06:43:39 -08001743 if (new_tsp == NULL)
1744 return -ENOMEM;
1745
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001746 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1747 if (rc != 0)
1748 return rc;
1749
Casey Schaufler676dac42010-12-02 06:43:39 -08001750 new->security = new_tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11001751 return 0;
1752}
1753
Randy Dunlap251a2a92009-02-18 11:42:33 -08001754/**
David Howellsee18d642009-09-02 09:14:21 +01001755 * smack_cred_transfer - Transfer the old credentials to the new credentials
1756 * @new: the new credentials
1757 * @old: the original credentials
1758 *
1759 * Fill in a set of blank credentials from another set of credentials.
1760 */
1761static void smack_cred_transfer(struct cred *new, const struct cred *old)
1762{
Casey Schaufler676dac42010-12-02 06:43:39 -08001763 struct task_smack *old_tsp = old->security;
1764 struct task_smack *new_tsp = new->security;
1765
1766 new_tsp->smk_task = old_tsp->smk_task;
1767 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001768 mutex_init(&new_tsp->smk_rules_lock);
1769 INIT_LIST_HEAD(&new_tsp->smk_rules);
1770
1771
1772 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001773}
1774
1775/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001776 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001777 * @new: points to the set of credentials to be modified.
1778 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001779 *
1780 * Set the security data for a kernel service.
1781 */
1782static int smack_kernel_act_as(struct cred *new, u32 secid)
1783{
Casey Schaufler676dac42010-12-02 06:43:39 -08001784 struct task_smack *new_tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001785 struct smack_known *skp = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001786
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001787 if (skp == NULL)
David Howells3a3b7ce2008-11-14 10:39:28 +11001788 return -EINVAL;
1789
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001790 new_tsp->smk_task = skp;
David Howells3a3b7ce2008-11-14 10:39:28 +11001791 return 0;
1792}
1793
1794/**
1795 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001796 * @new: points to the set of credentials to be modified
1797 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001798 *
1799 * Set the file creation context in a set of credentials to the same
1800 * as the objective context of the specified inode
1801 */
1802static int smack_kernel_create_files_as(struct cred *new,
1803 struct inode *inode)
1804{
1805 struct inode_smack *isp = inode->i_security;
Casey Schaufler676dac42010-12-02 06:43:39 -08001806 struct task_smack *tsp = new->security;
David Howells3a3b7ce2008-11-14 10:39:28 +11001807
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001808 tsp->smk_forked = smk_find_entry(isp->smk_inode);
1809 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001810 return 0;
1811}
1812
1813/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001814 * smk_curacc_on_task - helper to log task related access
1815 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001816 * @access: the access requested
1817 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001818 *
1819 * Return 0 if access is permitted
1820 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001821static int smk_curacc_on_task(struct task_struct *p, int access,
1822 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001823{
1824 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001825 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflerd166c802014-08-27 14:51:27 -07001826 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001827
Casey Schaufler531f1d42011-09-19 12:41:42 -07001828 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001829 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001830 rc = smk_curacc(skp->smk_known, access, &ad);
1831 rc = smk_bu_task(p, access, rc);
1832 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001833}
1834
1835/**
Casey Schauflere114e472008-02-04 22:29:50 -08001836 * smack_task_setpgid - Smack check on setting pgid
1837 * @p: the task object
1838 * @pgid: unused
1839 *
1840 * Return 0 if write access is permitted
1841 */
1842static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1843{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001844 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001845}
1846
1847/**
1848 * smack_task_getpgid - Smack access check for getpgid
1849 * @p: the object task
1850 *
1851 * Returns 0 if current can read the object task, error code otherwise
1852 */
1853static int smack_task_getpgid(struct task_struct *p)
1854{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001855 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001856}
1857
1858/**
1859 * smack_task_getsid - Smack access check for getsid
1860 * @p: the object task
1861 *
1862 * Returns 0 if current can read the object task, error code otherwise
1863 */
1864static int smack_task_getsid(struct task_struct *p)
1865{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001866 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001867}
1868
1869/**
1870 * smack_task_getsecid - get the secid of the task
1871 * @p: the object task
1872 * @secid: where to put the result
1873 *
1874 * Sets the secid to contain a u32 version of the smack label.
1875 */
1876static void smack_task_getsecid(struct task_struct *p, u32 *secid)
1877{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001878 struct smack_known *skp = smk_of_task(task_security(p));
1879
1880 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08001881}
1882
1883/**
1884 * smack_task_setnice - Smack check on setting nice
1885 * @p: the task object
1886 * @nice: unused
1887 *
1888 * Return 0 if write access is permitted
1889 */
1890static int smack_task_setnice(struct task_struct *p, int nice)
1891{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001892 int rc;
1893
1894 rc = cap_task_setnice(p, nice);
1895 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001896 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001897 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001898}
1899
1900/**
1901 * smack_task_setioprio - Smack check on setting ioprio
1902 * @p: the task object
1903 * @ioprio: unused
1904 *
1905 * Return 0 if write access is permitted
1906 */
1907static int smack_task_setioprio(struct task_struct *p, int ioprio)
1908{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001909 int rc;
1910
1911 rc = cap_task_setioprio(p, ioprio);
1912 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001913 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001914 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001915}
1916
1917/**
1918 * smack_task_getioprio - Smack check on reading ioprio
1919 * @p: the task object
1920 *
1921 * Return 0 if read access is permitted
1922 */
1923static int smack_task_getioprio(struct task_struct *p)
1924{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001925 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001926}
1927
1928/**
1929 * smack_task_setscheduler - Smack check on setting scheduler
1930 * @p: the task object
1931 * @policy: unused
1932 * @lp: unused
1933 *
1934 * Return 0 if read access is permitted
1935 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001936static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08001937{
Casey Schauflerbcdca222008-02-23 15:24:04 -08001938 int rc;
1939
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09001940 rc = cap_task_setscheduler(p);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001941 if (rc == 0)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001942 rc = smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001943 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001944}
1945
1946/**
1947 * smack_task_getscheduler - Smack check on reading scheduler
1948 * @p: the task object
1949 *
1950 * Return 0 if read access is permitted
1951 */
1952static int smack_task_getscheduler(struct task_struct *p)
1953{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001954 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001955}
1956
1957/**
1958 * smack_task_movememory - Smack check on moving memory
1959 * @p: the task object
1960 *
1961 * Return 0 if write access is permitted
1962 */
1963static int smack_task_movememory(struct task_struct *p)
1964{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001965 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001966}
1967
1968/**
1969 * smack_task_kill - Smack check on signal delivery
1970 * @p: the task object
1971 * @info: unused
1972 * @sig: unused
1973 * @secid: identifies the smack to use in lieu of current's
1974 *
1975 * Return 0 if write access is permitted
1976 *
1977 * The secid behavior is an artifact of an SELinux hack
1978 * in the USB code. Someday it may go away.
1979 */
1980static int smack_task_kill(struct task_struct *p, struct siginfo *info,
1981 int sig, u32 secid)
1982{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001983 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001984 struct smack_known *skp;
1985 struct smack_known *tkp = smk_of_task(task_security(p));
Casey Schauflerd166c802014-08-27 14:51:27 -07001986 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001987
1988 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1989 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08001990 /*
Casey Schauflere114e472008-02-04 22:29:50 -08001991 * Sending a signal requires that the sender
1992 * can write the receiver.
1993 */
Casey Schauflerd166c802014-08-27 14:51:27 -07001994 if (secid == 0) {
1995 rc = smk_curacc(tkp->smk_known, MAY_WRITE, &ad);
1996 rc = smk_bu_task(p, MAY_WRITE, rc);
1997 return rc;
1998 }
Casey Schauflere114e472008-02-04 22:29:50 -08001999 /*
2000 * If the secid isn't 0 we're dealing with some USB IO
2001 * specific behavior. This is not clean. For one thing
2002 * we can't take privilege into account.
2003 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002004 skp = smack_from_secid(secid);
Casey Schauflerd166c802014-08-27 14:51:27 -07002005 rc = smk_access(skp, tkp->smk_known, MAY_WRITE, &ad);
2006 rc = smk_bu_note("USB signal", skp, tkp->smk_known, MAY_WRITE, rc);
2007 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002008}
2009
2010/**
2011 * smack_task_wait - Smack access check for waiting
2012 * @p: task to wait for
2013 *
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002014 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -08002015 */
2016static int smack_task_wait(struct task_struct *p)
2017{
Casey Schauflere114e472008-02-04 22:29:50 -08002018 /*
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002019 * Allow the operation to succeed.
2020 * Zombies are bad.
2021 * In userless environments (e.g. phones) programs
2022 * get marked with SMACK64EXEC and even if the parent
2023 * and child shouldn't be talking the parent still
2024 * may expect to know when the child exits.
Casey Schauflere114e472008-02-04 22:29:50 -08002025 */
Casey Schauflerc00bedb2012-08-09 17:46:38 -07002026 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002027}
2028
2029/**
2030 * smack_task_to_inode - copy task smack into the inode blob
2031 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002032 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002033 *
2034 * Sets the smack pointer in the inode security blob
2035 */
2036static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2037{
2038 struct inode_smack *isp = inode->i_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002039 struct smack_known *skp = smk_of_task(task_security(p));
2040
2041 isp->smk_inode = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002042}
2043
2044/*
2045 * Socket hooks.
2046 */
2047
2048/**
2049 * smack_sk_alloc_security - Allocate a socket blob
2050 * @sk: the socket
2051 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002052 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002053 *
2054 * Assign Smack pointers to current
2055 *
2056 * Returns 0 on success, -ENOMEM is there's no memory
2057 */
2058static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2059{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002060 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002061 struct socket_smack *ssp;
2062
2063 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2064 if (ssp == NULL)
2065 return -ENOMEM;
2066
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002067 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002068 ssp->smk_out = skp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002069 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002070
2071 sk->sk_security = ssp;
2072
2073 return 0;
2074}
2075
2076/**
2077 * smack_sk_free_security - Free a socket blob
2078 * @sk: the socket
2079 *
2080 * Clears the blob pointer
2081 */
2082static void smack_sk_free_security(struct sock *sk)
2083{
2084 kfree(sk->sk_security);
2085}
2086
2087/**
Paul Moore07feee82009-03-27 17:10:54 -04002088* smack_host_label - check host based restrictions
2089* @sip: the object end
2090*
2091* looks for host based access restrictions
2092*
2093* This version will only be appropriate for really small sets of single label
2094* hosts. The caller is responsible for ensuring that the RCU read lock is
2095* taken before calling this function.
2096*
2097* Returns the label of the far end or NULL if it's not special.
2098*/
2099static char *smack_host_label(struct sockaddr_in *sip)
2100{
2101 struct smk_netlbladdr *snp;
2102 struct in_addr *siap = &sip->sin_addr;
2103
2104 if (siap->s_addr == 0)
2105 return NULL;
2106
2107 list_for_each_entry_rcu(snp, &smk_netlbladdr_list, list)
2108 /*
2109 * we break after finding the first match because
2110 * the list is sorted from longest to shortest mask
2111 * so we have found the most specific match
2112 */
2113 if ((&snp->smk_host.sin_addr)->s_addr ==
Etienne Basset43031542009-03-27 17:11:01 -04002114 (siap->s_addr & (&snp->smk_mask)->s_addr)) {
2115 /* we have found the special CIPSO option */
2116 if (snp->smk_label == smack_cipso_option)
2117 return NULL;
Paul Moore07feee82009-03-27 17:10:54 -04002118 return snp->smk_label;
Etienne Basset43031542009-03-27 17:11:01 -04002119 }
Paul Moore07feee82009-03-27 17:10:54 -04002120
2121 return NULL;
2122}
2123
2124/**
Casey Schauflere114e472008-02-04 22:29:50 -08002125 * smack_netlabel - Set the secattr on a socket
2126 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002127 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002128 *
2129 * Convert the outbound smack value (smk_out) to a
2130 * secattr and attach it to the socket.
2131 *
2132 * Returns 0 on success or an error code
2133 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002134static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002135{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002136 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002137 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002138 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002139
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002140 /*
2141 * Usually the netlabel code will handle changing the
2142 * packet labeling based on the label.
2143 * The case of a single label host is different, because
2144 * a single label host should never get a labeled packet
2145 * even though the label is usually associated with a packet
2146 * label.
2147 */
2148 local_bh_disable();
2149 bh_lock_sock_nested(sk);
2150
2151 if (ssp->smk_out == smack_net_ambient ||
2152 labeled == SMACK_UNLABELED_SOCKET)
2153 netlbl_sock_delattr(sk);
2154 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002155 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002156 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002157 }
2158
2159 bh_unlock_sock(sk);
2160 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002161
Casey Schauflere114e472008-02-04 22:29:50 -08002162 return rc;
2163}
2164
2165/**
Paul Moore07feee82009-03-27 17:10:54 -04002166 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2167 * @sk: the socket
2168 * @sap: the destination address
2169 *
2170 * Set the correct secattr for the given socket based on the destination
2171 * address and perform any outbound access checks needed.
2172 *
2173 * Returns 0 on success or an error code.
2174 *
2175 */
2176static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2177{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002178 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002179 int rc;
2180 int sk_lbl;
2181 char *hostsp;
2182 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002183 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002184
2185 rcu_read_lock();
2186 hostsp = smack_host_label(sap);
2187 if (hostsp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002188#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002189 struct lsm_network_audit net;
2190
Eric Paris48c62af2012-04-02 13:15:44 -04002191 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2192 ad.a.u.net->family = sap->sin_family;
2193 ad.a.u.net->dport = sap->sin_port;
2194 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002195#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002196 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002197 skp = ssp->smk_out;
2198 rc = smk_access(skp, hostsp, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002199 rc = smk_bu_note("IPv4 host check", skp, hostsp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002200 } else {
2201 sk_lbl = SMACK_CIPSO_SOCKET;
2202 rc = 0;
2203 }
2204 rcu_read_unlock();
2205 if (rc != 0)
2206 return rc;
2207
2208 return smack_netlabel(sk, sk_lbl);
2209}
2210
2211/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002212 * smk_ipv6_port_label - Smack port access table management
2213 * @sock: socket
2214 * @address: address
2215 *
2216 * Create or update the port list entry
2217 */
2218static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2219{
2220 struct sock *sk = sock->sk;
2221 struct sockaddr_in6 *addr6;
2222 struct socket_smack *ssp = sock->sk->sk_security;
2223 struct smk_port_label *spp;
2224 unsigned short port = 0;
2225
2226 if (address == NULL) {
2227 /*
2228 * This operation is changing the Smack information
2229 * on the bound socket. Take the changes to the port
2230 * as well.
2231 */
2232 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2233 if (sk != spp->smk_sock)
2234 continue;
2235 spp->smk_in = ssp->smk_in;
2236 spp->smk_out = ssp->smk_out;
2237 return;
2238 }
2239 /*
2240 * A NULL address is only used for updating existing
2241 * bound entries. If there isn't one, it's OK.
2242 */
2243 return;
2244 }
2245
2246 addr6 = (struct sockaddr_in6 *)address;
2247 port = ntohs(addr6->sin6_port);
2248 /*
2249 * This is a special case that is safely ignored.
2250 */
2251 if (port == 0)
2252 return;
2253
2254 /*
2255 * Look for an existing port list entry.
2256 * This is an indication that a port is getting reused.
2257 */
2258 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2259 if (spp->smk_port != port)
2260 continue;
2261 spp->smk_port = port;
2262 spp->smk_sock = sk;
2263 spp->smk_in = ssp->smk_in;
2264 spp->smk_out = ssp->smk_out;
2265 return;
2266 }
2267
2268 /*
2269 * A new port entry is required.
2270 */
2271 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2272 if (spp == NULL)
2273 return;
2274
2275 spp->smk_port = port;
2276 spp->smk_sock = sk;
2277 spp->smk_in = ssp->smk_in;
2278 spp->smk_out = ssp->smk_out;
2279
2280 list_add(&spp->list, &smk_ipv6_port_list);
2281 return;
2282}
2283
2284/**
2285 * smk_ipv6_port_check - check Smack port access
2286 * @sock: socket
2287 * @address: address
2288 *
2289 * Create or update the port list entry
2290 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002291static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002292 int act)
2293{
2294 __be16 *bep;
2295 __be32 *be32p;
Casey Schauflerc6739442013-05-22 18:42:56 -07002296 struct smk_port_label *spp;
2297 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002298 struct smack_known *skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002299 unsigned short port = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002300 char *object;
2301 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002302 int rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002303#ifdef CONFIG_AUDIT
2304 struct lsm_network_audit net;
2305#endif
2306
2307 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002308 skp = smack_net_ambient;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002309 object = ssp->smk_in->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002310 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002311 skp = ssp->smk_out;
2312 object = smack_net_ambient->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002313 }
2314
2315 /*
2316 * Get the IP address and port from the address.
2317 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002318 port = ntohs(address->sin6_port);
2319 bep = (__be16 *)(&address->sin6_addr);
2320 be32p = (__be32 *)(&address->sin6_addr);
Casey Schauflerc6739442013-05-22 18:42:56 -07002321
2322 /*
2323 * It's remote, so port lookup does no good.
2324 */
2325 if (be32p[0] || be32p[1] || be32p[2] || bep[6] || ntohs(bep[7]) != 1)
2326 goto auditout;
2327
2328 /*
2329 * It's local so the send check has to have passed.
2330 */
2331 if (act == SMK_RECEIVING) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002332 skp = &smack_known_web;
Casey Schauflerc6739442013-05-22 18:42:56 -07002333 goto auditout;
2334 }
2335
2336 list_for_each_entry(spp, &smk_ipv6_port_list, list) {
2337 if (spp->smk_port != port)
2338 continue;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002339 object = spp->smk_in->smk_known;
Casey Schauflerc6739442013-05-22 18:42:56 -07002340 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002341 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002342 break;
2343 }
2344
2345auditout:
2346
2347#ifdef CONFIG_AUDIT
2348 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2349 ad.a.u.net->family = sk->sk_family;
2350 ad.a.u.net->dport = port;
2351 if (act == SMK_RECEIVING)
Casey Schaufler6ea06242013-08-05 13:21:22 -07002352 ad.a.u.net->v6info.saddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002353 else
Casey Schaufler6ea06242013-08-05 13:21:22 -07002354 ad.a.u.net->v6info.daddr = address->sin6_addr;
Casey Schauflerc6739442013-05-22 18:42:56 -07002355#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002356 rc = smk_access(skp, object, MAY_WRITE, &ad);
2357 rc = smk_bu_note("IPv6 port check", skp, object, MAY_WRITE, rc);
2358 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002359}
2360
2361/**
Casey Schauflere114e472008-02-04 22:29:50 -08002362 * smack_inode_setsecurity - set smack xattrs
2363 * @inode: the object
2364 * @name: attribute name
2365 * @value: attribute value
2366 * @size: size of the attribute
2367 * @flags: unused
2368 *
2369 * Sets the named attribute in the appropriate blob
2370 *
2371 * Returns 0 on success, or an error code
2372 */
2373static int smack_inode_setsecurity(struct inode *inode, const char *name,
2374 const void *value, size_t size, int flags)
2375{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002376 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002377 struct inode_smack *nsp = inode->i_security;
2378 struct socket_smack *ssp;
2379 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002380 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002381
Casey Schauflerf7112e62012-05-06 15:22:02 -07002382 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302383 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002384
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002385 skp = smk_import_entry(value, size);
2386 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002387 return -EINVAL;
2388
2389 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002390 nsp->smk_inode = skp->smk_known;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002391 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002392 return 0;
2393 }
2394 /*
2395 * The rest of the Smack xattrs are only on sockets.
2396 */
2397 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2398 return -EOPNOTSUPP;
2399
2400 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002401 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002402 return -EOPNOTSUPP;
2403
2404 ssp = sock->sk->sk_security;
2405
2406 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002407 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002408 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002409 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002410 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002411 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2412 if (rc != 0)
2413 printk(KERN_WARNING
2414 "Smack: \"%s\" netlbl error %d.\n",
2415 __func__, -rc);
2416 }
Casey Schauflere114e472008-02-04 22:29:50 -08002417 } else
2418 return -EOPNOTSUPP;
2419
Casey Schauflerc6739442013-05-22 18:42:56 -07002420 if (sock->sk->sk_family == PF_INET6)
2421 smk_ipv6_port_label(sock, NULL);
2422
Casey Schauflere114e472008-02-04 22:29:50 -08002423 return 0;
2424}
2425
2426/**
2427 * smack_socket_post_create - finish socket setup
2428 * @sock: the socket
2429 * @family: protocol family
2430 * @type: unused
2431 * @protocol: unused
2432 * @kern: unused
2433 *
2434 * Sets the netlabel information on the socket
2435 *
2436 * Returns 0 on success, and error code otherwise
2437 */
2438static int smack_socket_post_create(struct socket *sock, int family,
2439 int type, int protocol, int kern)
2440{
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002441 if (family != PF_INET || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002442 return 0;
2443 /*
2444 * Set the outbound netlbl.
2445 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002446 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2447}
2448
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002449/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002450 * smack_socket_bind - record port binding information.
2451 * @sock: the socket
2452 * @address: the port address
2453 * @addrlen: size of the address
2454 *
2455 * Records the label bound to a port.
2456 *
2457 * Returns 0
2458 */
2459static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2460 int addrlen)
2461{
2462 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2463 smk_ipv6_port_label(sock, address);
2464
2465 return 0;
2466}
2467
2468/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002469 * smack_socket_connect - connect access check
2470 * @sock: the socket
2471 * @sap: the other end
2472 * @addrlen: size of sap
2473 *
2474 * Verifies that a connection may be possible
2475 *
2476 * Returns 0 on success, and error code otherwise
2477 */
2478static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2479 int addrlen)
2480{
Casey Schauflerc6739442013-05-22 18:42:56 -07002481 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002482
Casey Schauflerc6739442013-05-22 18:42:56 -07002483 if (sock->sk == NULL)
2484 return 0;
2485
2486 switch (sock->sk->sk_family) {
2487 case PF_INET:
2488 if (addrlen < sizeof(struct sockaddr_in))
2489 return -EINVAL;
2490 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2491 break;
2492 case PF_INET6:
2493 if (addrlen < sizeof(struct sockaddr_in6))
2494 return -EINVAL;
Casey Schaufler6ea06242013-08-05 13:21:22 -07002495 rc = smk_ipv6_port_check(sock->sk, (struct sockaddr_in6 *)sap,
2496 SMK_CONNECTING);
Casey Schauflerc6739442013-05-22 18:42:56 -07002497 break;
2498 }
2499 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002500}
2501
2502/**
2503 * smack_flags_to_may - convert S_ to MAY_ values
2504 * @flags: the S_ value
2505 *
2506 * Returns the equivalent MAY_ value
2507 */
2508static int smack_flags_to_may(int flags)
2509{
2510 int may = 0;
2511
2512 if (flags & S_IRUGO)
2513 may |= MAY_READ;
2514 if (flags & S_IWUGO)
2515 may |= MAY_WRITE;
2516 if (flags & S_IXUGO)
2517 may |= MAY_EXEC;
2518
2519 return may;
2520}
2521
2522/**
2523 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2524 * @msg: the object
2525 *
2526 * Returns 0
2527 */
2528static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2529{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002530 struct smack_known *skp = smk_of_current();
2531
2532 msg->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002533 return 0;
2534}
2535
2536/**
2537 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2538 * @msg: the object
2539 *
2540 * Clears the blob pointer
2541 */
2542static void smack_msg_msg_free_security(struct msg_msg *msg)
2543{
2544 msg->security = NULL;
2545}
2546
2547/**
2548 * smack_of_shm - the smack pointer for the shm
2549 * @shp: the object
2550 *
2551 * Returns a pointer to the smack value
2552 */
2553static char *smack_of_shm(struct shmid_kernel *shp)
2554{
2555 return (char *)shp->shm_perm.security;
2556}
2557
2558/**
2559 * smack_shm_alloc_security - Set the security blob for shm
2560 * @shp: the object
2561 *
2562 * Returns 0
2563 */
2564static int smack_shm_alloc_security(struct shmid_kernel *shp)
2565{
2566 struct kern_ipc_perm *isp = &shp->shm_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002567 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002568
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002569 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002570 return 0;
2571}
2572
2573/**
2574 * smack_shm_free_security - Clear the security blob for shm
2575 * @shp: the object
2576 *
2577 * Clears the blob pointer
2578 */
2579static void smack_shm_free_security(struct shmid_kernel *shp)
2580{
2581 struct kern_ipc_perm *isp = &shp->shm_perm;
2582
2583 isp->security = NULL;
2584}
2585
2586/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002587 * smk_curacc_shm : check if current has access on shm
2588 * @shp : the object
2589 * @access : access requested
2590 *
2591 * Returns 0 if current has the requested access, error code otherwise
2592 */
2593static int smk_curacc_shm(struct shmid_kernel *shp, int access)
2594{
2595 char *ssp = smack_of_shm(shp);
2596 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002597 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002598
2599#ifdef CONFIG_AUDIT
2600 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2601 ad.a.u.ipc_id = shp->shm_perm.id;
2602#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002603 rc = smk_curacc(ssp, access, &ad);
2604 rc = smk_bu_current("shm", ssp, access, rc);
2605 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002606}
2607
2608/**
Casey Schauflere114e472008-02-04 22:29:50 -08002609 * smack_shm_associate - Smack access check for shm
2610 * @shp: the object
2611 * @shmflg: access requested
2612 *
2613 * Returns 0 if current has the requested access, error code otherwise
2614 */
2615static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
2616{
Casey Schauflere114e472008-02-04 22:29:50 -08002617 int may;
2618
2619 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002620 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002621}
2622
2623/**
2624 * smack_shm_shmctl - Smack access check for shm
2625 * @shp: the object
2626 * @cmd: what it wants to do
2627 *
2628 * Returns 0 if current has the requested access, error code otherwise
2629 */
2630static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
2631{
Casey Schauflere114e472008-02-04 22:29:50 -08002632 int may;
2633
2634 switch (cmd) {
2635 case IPC_STAT:
2636 case SHM_STAT:
2637 may = MAY_READ;
2638 break;
2639 case IPC_SET:
2640 case SHM_LOCK:
2641 case SHM_UNLOCK:
2642 case IPC_RMID:
2643 may = MAY_READWRITE;
2644 break;
2645 case IPC_INFO:
2646 case SHM_INFO:
2647 /*
2648 * System level information.
2649 */
2650 return 0;
2651 default:
2652 return -EINVAL;
2653 }
Etienne Bassetecfcc532009-04-08 20:40:06 +02002654 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002655}
2656
2657/**
2658 * smack_shm_shmat - Smack access for shmat
2659 * @shp: the object
2660 * @shmaddr: unused
2661 * @shmflg: access requested
2662 *
2663 * Returns 0 if current has the requested access, error code otherwise
2664 */
2665static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
2666 int shmflg)
2667{
Casey Schauflere114e472008-02-04 22:29:50 -08002668 int may;
2669
2670 may = smack_flags_to_may(shmflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002671 return smk_curacc_shm(shp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002672}
2673
2674/**
2675 * smack_of_sem - the smack pointer for the sem
2676 * @sma: the object
2677 *
2678 * Returns a pointer to the smack value
2679 */
2680static char *smack_of_sem(struct sem_array *sma)
2681{
2682 return (char *)sma->sem_perm.security;
2683}
2684
2685/**
2686 * smack_sem_alloc_security - Set the security blob for sem
2687 * @sma: the object
2688 *
2689 * Returns 0
2690 */
2691static int smack_sem_alloc_security(struct sem_array *sma)
2692{
2693 struct kern_ipc_perm *isp = &sma->sem_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002694 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002695
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002696 isp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002697 return 0;
2698}
2699
2700/**
2701 * smack_sem_free_security - Clear the security blob for sem
2702 * @sma: the object
2703 *
2704 * Clears the blob pointer
2705 */
2706static void smack_sem_free_security(struct sem_array *sma)
2707{
2708 struct kern_ipc_perm *isp = &sma->sem_perm;
2709
2710 isp->security = NULL;
2711}
2712
2713/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002714 * smk_curacc_sem : check if current has access on sem
2715 * @sma : the object
2716 * @access : access requested
2717 *
2718 * Returns 0 if current has the requested access, error code otherwise
2719 */
2720static int smk_curacc_sem(struct sem_array *sma, int access)
2721{
2722 char *ssp = smack_of_sem(sma);
2723 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002724 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002725
2726#ifdef CONFIG_AUDIT
2727 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2728 ad.a.u.ipc_id = sma->sem_perm.id;
2729#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002730 rc = smk_curacc(ssp, access, &ad);
2731 rc = smk_bu_current("sem", ssp, access, rc);
2732 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002733}
2734
2735/**
Casey Schauflere114e472008-02-04 22:29:50 -08002736 * smack_sem_associate - Smack access check for sem
2737 * @sma: the object
2738 * @semflg: access requested
2739 *
2740 * Returns 0 if current has the requested access, error code otherwise
2741 */
2742static int smack_sem_associate(struct sem_array *sma, int semflg)
2743{
Casey Schauflere114e472008-02-04 22:29:50 -08002744 int may;
2745
2746 may = smack_flags_to_may(semflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002747 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002748}
2749
2750/**
2751 * smack_sem_shmctl - Smack access check for sem
2752 * @sma: the object
2753 * @cmd: what it wants to do
2754 *
2755 * Returns 0 if current has the requested access, error code otherwise
2756 */
2757static int smack_sem_semctl(struct sem_array *sma, int cmd)
2758{
Casey Schauflere114e472008-02-04 22:29:50 -08002759 int may;
2760
2761 switch (cmd) {
2762 case GETPID:
2763 case GETNCNT:
2764 case GETZCNT:
2765 case GETVAL:
2766 case GETALL:
2767 case IPC_STAT:
2768 case SEM_STAT:
2769 may = MAY_READ;
2770 break;
2771 case SETVAL:
2772 case SETALL:
2773 case IPC_RMID:
2774 case IPC_SET:
2775 may = MAY_READWRITE;
2776 break;
2777 case IPC_INFO:
2778 case SEM_INFO:
2779 /*
2780 * System level information
2781 */
2782 return 0;
2783 default:
2784 return -EINVAL;
2785 }
2786
Etienne Bassetecfcc532009-04-08 20:40:06 +02002787 return smk_curacc_sem(sma, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002788}
2789
2790/**
2791 * smack_sem_semop - Smack checks of semaphore operations
2792 * @sma: the object
2793 * @sops: unused
2794 * @nsops: unused
2795 * @alter: unused
2796 *
2797 * Treated as read and write in all cases.
2798 *
2799 * Returns 0 if access is allowed, error code otherwise
2800 */
2801static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
2802 unsigned nsops, int alter)
2803{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002804 return smk_curacc_sem(sma, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002805}
2806
2807/**
2808 * smack_msg_alloc_security - Set the security blob for msg
2809 * @msq: the object
2810 *
2811 * Returns 0
2812 */
2813static int smack_msg_queue_alloc_security(struct msg_queue *msq)
2814{
2815 struct kern_ipc_perm *kisp = &msq->q_perm;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002816 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002817
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002818 kisp->security = skp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08002819 return 0;
2820}
2821
2822/**
2823 * smack_msg_free_security - Clear the security blob for msg
2824 * @msq: the object
2825 *
2826 * Clears the blob pointer
2827 */
2828static void smack_msg_queue_free_security(struct msg_queue *msq)
2829{
2830 struct kern_ipc_perm *kisp = &msq->q_perm;
2831
2832 kisp->security = NULL;
2833}
2834
2835/**
2836 * smack_of_msq - the smack pointer for the msq
2837 * @msq: the object
2838 *
2839 * Returns a pointer to the smack value
2840 */
2841static char *smack_of_msq(struct msg_queue *msq)
2842{
2843 return (char *)msq->q_perm.security;
2844}
2845
2846/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002847 * smk_curacc_msq : helper to check if current has access on msq
2848 * @msq : the msq
2849 * @access : access requested
2850 *
2851 * return 0 if current has access, error otherwise
2852 */
2853static int smk_curacc_msq(struct msg_queue *msq, int access)
2854{
2855 char *msp = smack_of_msq(msq);
2856 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002857 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002858
2859#ifdef CONFIG_AUDIT
2860 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2861 ad.a.u.ipc_id = msq->q_perm.id;
2862#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002863 rc = smk_curacc(msp, access, &ad);
2864 rc = smk_bu_current("msq", msp, access, rc);
2865 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002866}
2867
2868/**
Casey Schauflere114e472008-02-04 22:29:50 -08002869 * smack_msg_queue_associate - Smack access check for msg_queue
2870 * @msq: the object
2871 * @msqflg: access requested
2872 *
2873 * Returns 0 if current has the requested access, error code otherwise
2874 */
2875static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
2876{
Casey Schauflere114e472008-02-04 22:29:50 -08002877 int may;
2878
2879 may = smack_flags_to_may(msqflg);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002880 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002881}
2882
2883/**
2884 * smack_msg_queue_msgctl - Smack access check for msg_queue
2885 * @msq: the object
2886 * @cmd: what it wants to do
2887 *
2888 * Returns 0 if current has the requested access, error code otherwise
2889 */
2890static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
2891{
Casey Schauflere114e472008-02-04 22:29:50 -08002892 int may;
2893
2894 switch (cmd) {
2895 case IPC_STAT:
2896 case MSG_STAT:
2897 may = MAY_READ;
2898 break;
2899 case IPC_SET:
2900 case IPC_RMID:
2901 may = MAY_READWRITE;
2902 break;
2903 case IPC_INFO:
2904 case MSG_INFO:
2905 /*
2906 * System level information
2907 */
2908 return 0;
2909 default:
2910 return -EINVAL;
2911 }
2912
Etienne Bassetecfcc532009-04-08 20:40:06 +02002913 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002914}
2915
2916/**
2917 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2918 * @msq: the object
2919 * @msg: unused
2920 * @msqflg: access requested
2921 *
2922 * Returns 0 if current has the requested access, error code otherwise
2923 */
2924static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
2925 int msqflg)
2926{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002927 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08002928
Etienne Bassetecfcc532009-04-08 20:40:06 +02002929 may = smack_flags_to_may(msqflg);
2930 return smk_curacc_msq(msq, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002931}
2932
2933/**
2934 * smack_msg_queue_msgsnd - Smack access check for msg_queue
2935 * @msq: the object
2936 * @msg: unused
2937 * @target: unused
2938 * @type: unused
2939 * @mode: unused
2940 *
2941 * Returns 0 if current has read and write access, error code otherwise
2942 */
2943static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
2944 struct task_struct *target, long type, int mode)
2945{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002946 return smk_curacc_msq(msq, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08002947}
2948
2949/**
2950 * smack_ipc_permission - Smack access for ipc_permission()
2951 * @ipp: the object permissions
2952 * @flag: access requested
2953 *
2954 * Returns 0 if current has read and write access, error code otherwise
2955 */
2956static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
2957{
2958 char *isp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002959 int may = smack_flags_to_may(flag);
2960 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002961 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002962
Etienne Bassetecfcc532009-04-08 20:40:06 +02002963#ifdef CONFIG_AUDIT
2964 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
2965 ad.a.u.ipc_id = ipp->id;
2966#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002967 rc = smk_curacc(isp, may, &ad);
2968 rc = smk_bu_current("svipc", isp, may, rc);
2969 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002970}
2971
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002972/**
2973 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08002974 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10002975 * @secid: where result will be saved
2976 */
2977static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
2978{
2979 char *smack = ipp->security;
2980
2981 *secid = smack_to_secid(smack);
2982}
2983
Casey Schauflere114e472008-02-04 22:29:50 -08002984/**
2985 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02002986 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08002987 * @inode: the object
2988 *
2989 * Set the inode's security blob if it hasn't been done already.
2990 */
2991static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
2992{
2993 struct super_block *sbp;
2994 struct superblock_smack *sbsp;
2995 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002996 struct smack_known *skp;
2997 struct smack_known *ckp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002998 char *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02002999 char trattr[TRANS_TRUE_SIZE];
3000 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003001 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003002 struct dentry *dp;
3003
3004 if (inode == NULL)
3005 return;
3006
3007 isp = inode->i_security;
3008
3009 mutex_lock(&isp->smk_lock);
3010 /*
3011 * If the inode is already instantiated
3012 * take the quick way out
3013 */
3014 if (isp->smk_flags & SMK_INODE_INSTANT)
3015 goto unlockandout;
3016
3017 sbp = inode->i_sb;
3018 sbsp = sbp->s_security;
3019 /*
3020 * We're going to use the superblock default label
3021 * if there's no label on the file.
3022 */
3023 final = sbsp->smk_default;
3024
3025 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003026 * If this is the root inode the superblock
3027 * may be in the process of initialization.
3028 * If that is the case use the root value out
3029 * of the superblock.
3030 */
3031 if (opt_dentry->d_parent == opt_dentry) {
Casey Schaufler36ea7352014-04-28 15:23:01 -07003032 if (sbp->s_magic == CGROUP_SUPER_MAGIC) {
3033 /*
3034 * The cgroup filesystem is never mounted,
3035 * so there's no opportunity to set the mount
3036 * options.
3037 */
3038 sbsp->smk_root = smack_known_star.smk_known;
3039 sbsp->smk_default = smack_known_star.smk_known;
3040 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003041 isp->smk_inode = sbsp->smk_root;
3042 isp->smk_flags |= SMK_INODE_INSTANT;
3043 goto unlockandout;
3044 }
3045
3046 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003047 * This is pretty hackish.
3048 * Casey says that we shouldn't have to do
3049 * file system specific code, but it does help
3050 * with keeping it simple.
3051 */
3052 switch (sbp->s_magic) {
3053 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003054 case PIPEFS_MAGIC:
3055 case SOCKFS_MAGIC:
3056 case CGROUP_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003057 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003058 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003059 * that the smack file system doesn't do
3060 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003061 *
Casey Schauflere114e472008-02-04 22:29:50 -08003062 * Casey says pipes are easy (?)
Casey Schaufler36ea7352014-04-28 15:23:01 -07003063 *
3064 * Socket access is controlled by the socket
3065 * structures associated with the task involved.
3066 *
3067 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003068 */
3069 final = smack_known_star.smk_known;
3070 break;
3071 case DEVPTS_SUPER_MAGIC:
3072 /*
3073 * devpts seems content with the label of the task.
3074 * Programs that change smack have to treat the
3075 * pty with respect.
3076 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003077 final = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003078 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003079 case PROC_SUPER_MAGIC:
3080 /*
3081 * Casey says procfs appears not to care.
3082 * The superblock default suffices.
3083 */
3084 break;
3085 case TMPFS_MAGIC:
3086 /*
3087 * Device labels should come from the filesystem,
3088 * but watch out, because they're volitile,
3089 * getting recreated on every reboot.
3090 */
3091 final = smack_known_star.smk_known;
3092 /*
3093 * No break.
3094 *
3095 * If a smack value has been set we want to use it,
3096 * but since tmpfs isn't giving us the opportunity
3097 * to set mount options simulate setting the
3098 * superblock default.
3099 */
3100 default:
3101 /*
3102 * This isn't an understood special case.
3103 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003104 */
3105
3106 /*
3107 * UNIX domain sockets use lower level socket data.
3108 */
3109 if (S_ISSOCK(inode->i_mode)) {
3110 final = smack_known_star.smk_known;
3111 break;
3112 }
3113 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003114 * No xattr support means, alas, no SMACK label.
3115 * Use the aforeapplied default.
3116 * It would be curious if the label of the task
3117 * does not match that assigned.
3118 */
3119 if (inode->i_op->getxattr == NULL)
3120 break;
3121 /*
3122 * Get the dentry for xattr.
3123 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003124 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003125 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3126 if (skp != NULL)
3127 final = skp->smk_known;
Casey Schaufler2267b132012-03-13 19:14:19 -07003128
3129 /*
3130 * Transmuting directory
3131 */
3132 if (S_ISDIR(inode->i_mode)) {
3133 /*
3134 * If this is a new directory and the label was
3135 * transmuted when the inode was initialized
3136 * set the transmute attribute on the directory
3137 * and mark the inode.
3138 *
3139 * If there is a transmute attribute on the
3140 * directory mark the inode.
3141 */
3142 if (isp->smk_flags & SMK_INODE_CHANGED) {
3143 isp->smk_flags &= ~SMK_INODE_CHANGED;
3144 rc = inode->i_op->setxattr(dp,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003145 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003146 TRANS_TRUE, TRANS_TRUE_SIZE,
3147 0);
3148 } else {
3149 rc = inode->i_op->getxattr(dp,
3150 XATTR_NAME_SMACKTRANSMUTE, trattr,
3151 TRANS_TRUE_SIZE);
3152 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3153 TRANS_TRUE_SIZE) != 0)
3154 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003155 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003156 if (rc >= 0)
3157 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003158 }
Casey Schaufler19760ad2013-12-16 16:27:26 -08003159 /*
3160 * Don't let the exec or mmap label be "*" or "@".
3161 */
3162 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3163 if (skp == &smack_known_star || skp == &smack_known_web)
3164 skp = NULL;
3165 isp->smk_task = skp;
3166 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3167 if (skp == &smack_known_star || skp == &smack_known_web)
3168 skp = NULL;
3169 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003170
Casey Schauflere114e472008-02-04 22:29:50 -08003171 dput(dp);
3172 break;
3173 }
3174
3175 if (final == NULL)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003176 isp->smk_inode = ckp->smk_known;
Casey Schauflere114e472008-02-04 22:29:50 -08003177 else
3178 isp->smk_inode = final;
3179
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003180 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003181
3182unlockandout:
3183 mutex_unlock(&isp->smk_lock);
3184 return;
3185}
3186
3187/**
3188 * smack_getprocattr - Smack process attribute access
3189 * @p: the object task
3190 * @name: the name of the attribute in /proc/.../attr
3191 * @value: where to put the result
3192 *
3193 * Places a copy of the task Smack into value
3194 *
3195 * Returns the length of the smack label or an error code
3196 */
3197static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3198{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003199 struct smack_known *skp = smk_of_task(task_security(p));
Casey Schauflere114e472008-02-04 22:29:50 -08003200 char *cp;
3201 int slen;
3202
3203 if (strcmp(name, "current") != 0)
3204 return -EINVAL;
3205
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003206 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003207 if (cp == NULL)
3208 return -ENOMEM;
3209
3210 slen = strlen(cp);
3211 *value = cp;
3212 return slen;
3213}
3214
3215/**
3216 * smack_setprocattr - Smack process attribute setting
3217 * @p: the object task
3218 * @name: the name of the attribute in /proc/.../attr
3219 * @value: the value to set
3220 * @size: the size of the value
3221 *
3222 * Sets the Smack value of the task. Only setting self
3223 * is permitted and only with privilege
3224 *
3225 * Returns the length of the smack label or an error code
3226 */
3227static int smack_setprocattr(struct task_struct *p, char *name,
3228 void *value, size_t size)
3229{
Casey Schaufler676dac42010-12-02 06:43:39 -08003230 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11003231 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003232 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003233
Casey Schauflere114e472008-02-04 22:29:50 -08003234 /*
3235 * Changing another process' Smack value is too dangerous
3236 * and supports no sane use case.
3237 */
3238 if (p != current)
3239 return -EPERM;
3240
Casey Schaufler1880eff2012-06-05 15:28:30 -07003241 if (!smack_privileged(CAP_MAC_ADMIN))
David Howells5cd9c582008-08-14 11:37:28 +01003242 return -EPERM;
3243
Casey Schauflerf7112e62012-05-06 15:22:02 -07003244 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003245 return -EINVAL;
3246
3247 if (strcmp(name, "current") != 0)
3248 return -EINVAL;
3249
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003250 skp = smk_import_entry(value, size);
3251 if (skp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08003252 return -EINVAL;
3253
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003254 /*
3255 * No process is ever allowed the web ("@") label.
3256 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003257 if (skp == &smack_known_web)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003258 return -EPERM;
3259
David Howellsd84f4f92008-11-14 10:39:23 +11003260 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003261 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003262 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003263
Casey Schaufler46a2f3b2012-08-22 11:44:03 -07003264 tsp = new->security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003265 tsp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003266
David Howellsd84f4f92008-11-14 10:39:23 +11003267 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003268 return size;
3269}
3270
3271/**
3272 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003273 * @sock: one sock
3274 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003275 * @newsk: unused
3276 *
3277 * Return 0 if a subject with the smack of sock could access
3278 * an object with the smack of other, otherwise an error code
3279 */
David S. Miller3610cda2011-01-05 15:38:53 -08003280static int smack_unix_stream_connect(struct sock *sock,
3281 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003282{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003283 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003284 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003285 struct socket_smack *ssp = sock->sk_security;
3286 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003287 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003288 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003289 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003290#ifdef CONFIG_AUDIT
3291 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003292#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003293
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003294 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3295 skp = ssp->smk_out;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003296 okp = osp->smk_out;
3297#ifdef CONFIG_AUDIT
3298 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3299 smk_ad_setfield_u_net_sk(&ad, other);
3300#endif
3301 rc = smk_access(skp, okp->smk_known, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07003302 rc = smk_bu_note("UDS connect", skp, okp->smk_known,
3303 MAY_WRITE, rc);
3304 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003305 rc = smk_access(okp, okp->smk_known, MAY_WRITE, NULL);
Casey Schauflerd166c802014-08-27 14:51:27 -07003306 rc = smk_bu_note("UDS connect", okp, okp->smk_known,
3307 MAY_WRITE, rc);
3308 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003309 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003310
Casey Schaufler975d5e52011-09-26 14:43:39 -07003311 /*
3312 * Cross reference the peer labels for SO_PEERSEC.
3313 */
3314 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003315 nsp->smk_packet = ssp->smk_out;
3316 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003317 }
3318
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003319 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003320}
3321
3322/**
3323 * smack_unix_may_send - Smack access on UDS
3324 * @sock: one socket
3325 * @other: the other socket
3326 *
3327 * Return 0 if a subject with the smack of sock could access
3328 * an object with the smack of other, otherwise an error code
3329 */
3330static int smack_unix_may_send(struct socket *sock, struct socket *other)
3331{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003332 struct socket_smack *ssp = sock->sk->sk_security;
3333 struct socket_smack *osp = other->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003334 struct smack_known *skp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003335 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003336 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003337
Kees Cook923e9a12012-04-10 13:26:44 -07003338#ifdef CONFIG_AUDIT
3339 struct lsm_network_audit net;
3340
Eric Paris48c62af2012-04-02 13:15:44 -04003341 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003342 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003343#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003344
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003345 if (smack_privileged(CAP_MAC_OVERRIDE))
3346 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003347
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003348 skp = ssp->smk_out;
Casey Schauflerd166c802014-08-27 14:51:27 -07003349 rc = smk_access(skp, osp->smk_in->smk_known, MAY_WRITE, &ad);
3350 rc = smk_bu_note("UDS send", skp, osp->smk_in->smk_known,
3351 MAY_WRITE, rc);
3352 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003353}
3354
3355/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003356 * smack_socket_sendmsg - Smack check based on destination host
3357 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003358 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003359 * @size: the size of the message
3360 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003361 * Return 0 if the current subject can write to the destination host.
3362 * For IPv4 this is only a question if the destination is a single label host.
3363 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003364 */
3365static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3366 int size)
3367{
3368 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003369 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schauflerc6739442013-05-22 18:42:56 -07003370 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003371
3372 /*
3373 * Perfectly reasonable for this to be NULL
3374 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003375 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003376 return 0;
3377
Casey Schauflerc6739442013-05-22 18:42:56 -07003378 switch (sip->sin_family) {
3379 case AF_INET:
3380 rc = smack_netlabel_send(sock->sk, sip);
3381 break;
3382 case AF_INET6:
3383 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
3384 break;
3385 }
3386 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003387}
3388
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003389/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003390 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003391 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003392 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003393 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003394 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003395 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003396static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3397 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003398{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003399 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003400 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003401 int acat;
3402 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003403
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003404 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003405 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003406 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003407 * If there are flags but no level netlabel isn't
3408 * behaving the way we expect it to.
3409 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003410 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003411 * Without guidance regarding the smack value
3412 * for the packet fall back on the network
3413 * ambient value.
3414 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003415 rcu_read_lock();
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003416 list_for_each_entry(skp, &smack_known_list, list) {
3417 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003418 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003419 /*
3420 * Compare the catsets. Use the netlbl APIs.
3421 */
3422 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3423 if ((skp->smk_netlabel.flags &
3424 NETLBL_SECATTR_MLS_CAT) == 0)
3425 found = 1;
3426 break;
3427 }
3428 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003429 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3430 acat + 1);
3431 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003432 skp->smk_netlabel.attr.mls.cat,
3433 kcat + 1);
3434 if (acat < 0 || kcat < 0)
3435 break;
3436 }
3437 if (acat == kcat) {
3438 found = 1;
3439 break;
3440 }
Casey Schauflere114e472008-02-04 22:29:50 -08003441 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003442 rcu_read_unlock();
3443
3444 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003445 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003446
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003447 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003448 return &smack_known_web;
3449 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003450 }
3451 if ((sap->flags & NETLBL_SECATTR_SECID) != 0) {
3452 /*
3453 * Looks like a fallback, which gives us a secid.
3454 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003455 skp = smack_from_secid(sap->attr.secid);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003456 /*
3457 * This has got to be a bug because it is
3458 * impossible to specify a fallback without
3459 * specifying the label, which will ensure
3460 * it has a secid, and the only way to get a
3461 * secid is from a fallback.
3462 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003463 BUG_ON(skp == NULL);
3464 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003465 }
3466 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003467 * Without guidance regarding the smack value
3468 * for the packet fall back on the network
3469 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003470 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003471 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003472}
3473
Casey Schaufler6ea06242013-08-05 13:21:22 -07003474static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003475{
Casey Schauflerc6739442013-05-22 18:42:56 -07003476 u8 nexthdr;
3477 int offset;
3478 int proto = -EINVAL;
3479 struct ipv6hdr _ipv6h;
3480 struct ipv6hdr *ip6;
3481 __be16 frag_off;
3482 struct tcphdr _tcph, *th;
3483 struct udphdr _udph, *uh;
3484 struct dccp_hdr _dccph, *dh;
3485
3486 sip->sin6_port = 0;
3487
3488 offset = skb_network_offset(skb);
3489 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3490 if (ip6 == NULL)
3491 return -EINVAL;
3492 sip->sin6_addr = ip6->saddr;
3493
3494 nexthdr = ip6->nexthdr;
3495 offset += sizeof(_ipv6h);
3496 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3497 if (offset < 0)
3498 return -EINVAL;
3499
3500 proto = nexthdr;
3501 switch (proto) {
3502 case IPPROTO_TCP:
3503 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3504 if (th != NULL)
3505 sip->sin6_port = th->source;
3506 break;
3507 case IPPROTO_UDP:
3508 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3509 if (uh != NULL)
3510 sip->sin6_port = uh->source;
3511 break;
3512 case IPPROTO_DCCP:
3513 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3514 if (dh != NULL)
3515 sip->sin6_port = dh->dccph_sport;
3516 break;
3517 }
3518 return proto;
3519}
3520
Casey Schauflere114e472008-02-04 22:29:50 -08003521/**
3522 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3523 * @sk: socket
3524 * @skb: packet
3525 *
3526 * Returns 0 if the packet should be delivered, an error code otherwise
3527 */
3528static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3529{
3530 struct netlbl_lsm_secattr secattr;
3531 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003532 struct smack_known *skp;
Casey Schaufler6ea06242013-08-05 13:21:22 -07003533 struct sockaddr_in6 sadd;
Casey Schauflerc6739442013-05-22 18:42:56 -07003534 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003535 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003536#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003537 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003538#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003539 switch (sk->sk_family) {
3540 case PF_INET:
3541 /*
3542 * Translate what netlabel gave us.
3543 */
3544 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003545
Casey Schauflerc6739442013-05-22 18:42:56 -07003546 rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
3547 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003548 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003549 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003550 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003551
Casey Schauflerc6739442013-05-22 18:42:56 -07003552 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003553
Etienne Bassetecfcc532009-04-08 20:40:06 +02003554#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003555 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3556 ad.a.u.net->family = sk->sk_family;
3557 ad.a.u.net->netif = skb->skb_iif;
3558 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003559#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003560 /*
3561 * Receiving a packet requires that the other end
3562 * be able to write here. Read access is not required.
3563 * This is the simplist possible security model
3564 * for networking.
3565 */
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003566 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07003567 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in->smk_known,
3568 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003569 if (rc != 0)
3570 netlbl_skbuff_err(skb, rc, 0);
3571 break;
3572 case PF_INET6:
3573 rc = smk_skb_to_addr_ipv6(skb, &sadd);
3574 if (rc == IPPROTO_UDP || rc == IPPROTO_TCP)
3575 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
3576 else
3577 rc = 0;
3578 break;
3579 }
Paul Moorea8134292008-10-10 10:16:31 -04003580 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003581}
3582
3583/**
3584 * smack_socket_getpeersec_stream - pull in packet label
3585 * @sock: the socket
3586 * @optval: user's destination
3587 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003588 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003589 *
3590 * returns zero on success, an error code otherwise
3591 */
3592static int smack_socket_getpeersec_stream(struct socket *sock,
3593 char __user *optval,
3594 int __user *optlen, unsigned len)
3595{
3596 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003597 char *rcp = "";
3598 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003599 int rc = 0;
3600
3601 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003602 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003603 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003604 slen = strlen(rcp) + 1;
3605 }
Casey Schauflere114e472008-02-04 22:29:50 -08003606
3607 if (slen > len)
3608 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003609 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003610 rc = -EFAULT;
3611
3612 if (put_user(slen, optlen) != 0)
3613 rc = -EFAULT;
3614
3615 return rc;
3616}
3617
3618
3619/**
3620 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003621 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003622 * @skb: packet data
3623 * @secid: pointer to where to put the secid of the packet
3624 *
3625 * Sets the netlabel socket state on sk from parent
3626 */
3627static int smack_socket_getpeersec_dgram(struct socket *sock,
3628 struct sk_buff *skb, u32 *secid)
3629
3630{
3631 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003632 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003633 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003634 int family = PF_UNSPEC;
3635 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003636 int rc;
3637
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003638 if (skb != NULL) {
3639 if (skb->protocol == htons(ETH_P_IP))
3640 family = PF_INET;
3641 else if (skb->protocol == htons(ETH_P_IPV6))
3642 family = PF_INET6;
Casey Schauflere114e472008-02-04 22:29:50 -08003643 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003644 if (family == PF_UNSPEC && sock != NULL)
3645 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003646
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003647 if (family == PF_UNIX) {
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003648 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003649 s = ssp->smk_out->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003650 } else if (family == PF_INET || family == PF_INET6) {
3651 /*
3652 * Translate what netlabel gave us.
3653 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003654 if (sock != NULL && sock->sk != NULL)
3655 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003656 netlbl_secattr_init(&secattr);
3657 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3658 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003659 skp = smack_from_secattr(&secattr, ssp);
3660 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003661 }
3662 netlbl_secattr_destroy(&secattr);
3663 }
3664 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003665 if (s == 0)
3666 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003667 return 0;
3668}
3669
3670/**
Paul Moore07feee82009-03-27 17:10:54 -04003671 * smack_sock_graft - Initialize a newly created socket with an existing sock
3672 * @sk: child sock
3673 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003674 *
Paul Moore07feee82009-03-27 17:10:54 -04003675 * Set the smk_{in,out} state of an existing sock based on the process that
3676 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003677 */
3678static void smack_sock_graft(struct sock *sk, struct socket *parent)
3679{
3680 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003681 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003682
Paul Moore07feee82009-03-27 17:10:54 -04003683 if (sk == NULL ||
3684 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003685 return;
3686
3687 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003688 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003689 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003690 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003691}
3692
3693/**
3694 * smack_inet_conn_request - Smack access check on connect
3695 * @sk: socket involved
3696 * @skb: packet
3697 * @req: unused
3698 *
3699 * Returns 0 if a task with the packet label could write to
3700 * the socket, otherwise an error code
3701 */
3702static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3703 struct request_sock *req)
3704{
Paul Moore07feee82009-03-27 17:10:54 -04003705 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003706 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003707 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003708 struct netlbl_lsm_secattr secattr;
3709 struct sockaddr_in addr;
3710 struct iphdr *hdr;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003711 char *hsp;
Casey Schauflere114e472008-02-04 22:29:50 -08003712 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003713 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003714#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003715 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003716#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003717
Casey Schauflerc6739442013-05-22 18:42:56 -07003718 if (family == PF_INET6) {
3719 /*
3720 * Handle mapped IPv4 packets arriving
3721 * via IPv6 sockets. Don't set up netlabel
3722 * processing on IPv6.
3723 */
3724 if (skb->protocol == htons(ETH_P_IP))
3725 family = PF_INET;
3726 else
3727 return 0;
3728 }
Casey Schauflere114e472008-02-04 22:29:50 -08003729
Paul Moore07feee82009-03-27 17:10:54 -04003730 netlbl_secattr_init(&secattr);
3731 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003732 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003733 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08003734 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003735 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04003736 netlbl_secattr_destroy(&secattr);
3737
Etienne Bassetecfcc532009-04-08 20:40:06 +02003738#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003739 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3740 ad.a.u.net->family = family;
3741 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003742 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
3743#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003744 /*
Paul Moore07feee82009-03-27 17:10:54 -04003745 * Receiving a packet requires that the other end be able to write
3746 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08003747 */
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003748 rc = smk_access(skp, ssp->smk_in->smk_known, MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07003749 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in->smk_known,
3750 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();
Casey Schauflerf7112e62012-05-06 15:22:02 -07003768 hsp = smack_host_label(&addr);
3769 rcu_read_unlock();
3770
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003771 if (hsp == 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
3823 key->security = skp->smk_known;
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{
3912 char **rule = (char **)vrule;
3913 *rule = NULL;
3914
3915 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3916 return -EINVAL;
3917
Al Viro5af75d82008-12-16 05:59:26 -05003918 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003919 return -EINVAL;
3920
3921 *rule = smk_import(rulestr, 0);
3922
3923 return 0;
3924}
3925
3926/**
3927 * smack_audit_rule_known - Distinguish Smack audit rules
3928 * @krule: rule of interest, in Audit kernel representation format
3929 *
3930 * This is used to filter Smack rules from remaining Audit ones.
3931 * If it's proved that this rule belongs to us, the
3932 * audit_rule_match hook will be called to do the final judgement.
3933 */
3934static int smack_audit_rule_known(struct audit_krule *krule)
3935{
3936 struct audit_field *f;
3937 int i;
3938
3939 for (i = 0; i < krule->field_count; i++) {
3940 f = &krule->fields[i];
3941
3942 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
3943 return 1;
3944 }
3945
3946 return 0;
3947}
3948
3949/**
3950 * smack_audit_rule_match - Audit given object ?
3951 * @secid: security id for identifying the object to test
3952 * @field: audit rule flags given from user-space
3953 * @op: required testing operator
3954 * @vrule: smack internal rule presentation
3955 * @actx: audit context associated with the check
3956 *
3957 * The core Audit hook. It's used to take the decision of
3958 * whether to audit or not to audit a given object.
3959 */
3960static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
3961 struct audit_context *actx)
3962{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003963 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003964 char *rule = vrule;
3965
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05003966 if (unlikely(!rule)) {
3967 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003968 return -ENOENT;
3969 }
3970
3971 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
3972 return 0;
3973
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003974 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003975
3976 /*
3977 * No need to do string comparisons. If a match occurs,
3978 * both pointers will point to the same smack_known
3979 * label.
3980 */
Al Viro5af75d82008-12-16 05:59:26 -05003981 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003982 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05003983 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003984 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003985
3986 return 0;
3987}
3988
3989/**
3990 * smack_audit_rule_free - free smack rule representation
3991 * @vrule: rule to be freed.
3992 *
3993 * No memory was allocated.
3994 */
3995static void smack_audit_rule_free(void *vrule)
3996{
3997 /* No-op */
3998}
3999
4000#endif /* CONFIG_AUDIT */
4001
Randy Dunlap251a2a92009-02-18 11:42:33 -08004002/**
David Quigley746df9b2013-05-22 12:50:35 -04004003 * smack_ismaclabel - check if xattr @name references a smack MAC label
4004 * @name: Full xattr name to check.
4005 */
4006static int smack_ismaclabel(const char *name)
4007{
4008 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4009}
4010
4011
4012/**
Casey Schauflere114e472008-02-04 22:29:50 -08004013 * smack_secid_to_secctx - return the smack label for a secid
4014 * @secid: incoming integer
4015 * @secdata: destination
4016 * @seclen: how long it is
4017 *
4018 * Exists for networking code.
4019 */
4020static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4021{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004022 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004023
Eric Parisd5630b92010-10-13 16:24:48 -04004024 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004025 *secdata = skp->smk_known;
4026 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004027 return 0;
4028}
4029
Randy Dunlap251a2a92009-02-18 11:42:33 -08004030/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004031 * smack_secctx_to_secid - return the secid for a smack label
4032 * @secdata: smack label
4033 * @seclen: how long result is
4034 * @secid: outgoing integer
4035 *
4036 * Exists for audit and networking code.
4037 */
David Howellse52c17642008-04-29 20:52:51 +01004038static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004039{
4040 *secid = smack_to_secid(secdata);
4041 return 0;
4042}
4043
Randy Dunlap251a2a92009-02-18 11:42:33 -08004044/**
Casey Schauflere114e472008-02-04 22:29:50 -08004045 * smack_release_secctx - don't do anything.
Randy Dunlap251a2a92009-02-18 11:42:33 -08004046 * @secdata: unused
4047 * @seclen: unused
Casey Schauflere114e472008-02-04 22:29:50 -08004048 *
4049 * Exists to make sure nothing gets done, and properly
4050 */
4051static void smack_release_secctx(char *secdata, u32 seclen)
4052{
4053}
4054
David P. Quigley1ee65e32009-09-03 14:25:57 -04004055static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4056{
4057 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4058}
4059
4060static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4061{
4062 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4063}
4064
4065static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4066{
4067 int len = 0;
4068 len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);
4069
4070 if (len < 0)
4071 return len;
4072 *ctxlen = len;
4073 return 0;
4074}
4075
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02004076struct security_operations smack_ops = {
4077 .name = "smack",
4078
Ingo Molnar9e488582009-05-07 19:26:19 +10004079 .ptrace_access_check = smack_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01004080 .ptrace_traceme = smack_ptrace_traceme,
Casey Schauflere114e472008-02-04 22:29:50 -08004081 .syslog = smack_syslog,
Casey Schauflere114e472008-02-04 22:29:50 -08004082
4083 .sb_alloc_security = smack_sb_alloc_security,
4084 .sb_free_security = smack_sb_free_security,
4085 .sb_copy_data = smack_sb_copy_data,
4086 .sb_kern_mount = smack_sb_kern_mount,
4087 .sb_statfs = smack_sb_statfs,
Casey Schauflere114e472008-02-04 22:29:50 -08004088
Casey Schaufler676dac42010-12-02 06:43:39 -08004089 .bprm_set_creds = smack_bprm_set_creds,
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03004090 .bprm_committing_creds = smack_bprm_committing_creds,
4091 .bprm_secureexec = smack_bprm_secureexec,
Casey Schaufler676dac42010-12-02 06:43:39 -08004092
Casey Schauflere114e472008-02-04 22:29:50 -08004093 .inode_alloc_security = smack_inode_alloc_security,
4094 .inode_free_security = smack_inode_free_security,
4095 .inode_init_security = smack_inode_init_security,
4096 .inode_link = smack_inode_link,
4097 .inode_unlink = smack_inode_unlink,
4098 .inode_rmdir = smack_inode_rmdir,
4099 .inode_rename = smack_inode_rename,
4100 .inode_permission = smack_inode_permission,
4101 .inode_setattr = smack_inode_setattr,
4102 .inode_getattr = smack_inode_getattr,
4103 .inode_setxattr = smack_inode_setxattr,
4104 .inode_post_setxattr = smack_inode_post_setxattr,
4105 .inode_getxattr = smack_inode_getxattr,
4106 .inode_removexattr = smack_inode_removexattr,
4107 .inode_getsecurity = smack_inode_getsecurity,
4108 .inode_setsecurity = smack_inode_setsecurity,
4109 .inode_listsecurity = smack_inode_listsecurity,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004110 .inode_getsecid = smack_inode_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004111
4112 .file_permission = smack_file_permission,
4113 .file_alloc_security = smack_file_alloc_security,
4114 .file_free_security = smack_file_free_security,
4115 .file_ioctl = smack_file_ioctl,
4116 .file_lock = smack_file_lock,
4117 .file_fcntl = smack_file_fcntl,
Al Viroe5467852012-05-30 13:30:51 -04004118 .mmap_file = smack_mmap_file,
4119 .mmap_addr = cap_mmap_addr,
Casey Schauflere114e472008-02-04 22:29:50 -08004120 .file_set_fowner = smack_file_set_fowner,
4121 .file_send_sigiotask = smack_file_send_sigiotask,
4122 .file_receive = smack_file_receive,
4123
Eric Paris83d49852012-04-04 13:45:40 -04004124 .file_open = smack_file_open,
Casey Schaufler531f1d42011-09-19 12:41:42 -07004125
David Howellsee18d642009-09-02 09:14:21 +01004126 .cred_alloc_blank = smack_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11004127 .cred_free = smack_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11004128 .cred_prepare = smack_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01004129 .cred_transfer = smack_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11004130 .kernel_act_as = smack_kernel_act_as,
4131 .kernel_create_files_as = smack_kernel_create_files_as,
Casey Schauflere114e472008-02-04 22:29:50 -08004132 .task_setpgid = smack_task_setpgid,
4133 .task_getpgid = smack_task_getpgid,
4134 .task_getsid = smack_task_getsid,
4135 .task_getsecid = smack_task_getsecid,
4136 .task_setnice = smack_task_setnice,
4137 .task_setioprio = smack_task_setioprio,
4138 .task_getioprio = smack_task_getioprio,
4139 .task_setscheduler = smack_task_setscheduler,
4140 .task_getscheduler = smack_task_getscheduler,
4141 .task_movememory = smack_task_movememory,
4142 .task_kill = smack_task_kill,
4143 .task_wait = smack_task_wait,
Casey Schauflere114e472008-02-04 22:29:50 -08004144 .task_to_inode = smack_task_to_inode,
4145
4146 .ipc_permission = smack_ipc_permission,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004147 .ipc_getsecid = smack_ipc_getsecid,
Casey Schauflere114e472008-02-04 22:29:50 -08004148
4149 .msg_msg_alloc_security = smack_msg_msg_alloc_security,
4150 .msg_msg_free_security = smack_msg_msg_free_security,
4151
4152 .msg_queue_alloc_security = smack_msg_queue_alloc_security,
4153 .msg_queue_free_security = smack_msg_queue_free_security,
4154 .msg_queue_associate = smack_msg_queue_associate,
4155 .msg_queue_msgctl = smack_msg_queue_msgctl,
4156 .msg_queue_msgsnd = smack_msg_queue_msgsnd,
4157 .msg_queue_msgrcv = smack_msg_queue_msgrcv,
4158
4159 .shm_alloc_security = smack_shm_alloc_security,
4160 .shm_free_security = smack_shm_free_security,
4161 .shm_associate = smack_shm_associate,
4162 .shm_shmctl = smack_shm_shmctl,
4163 .shm_shmat = smack_shm_shmat,
4164
4165 .sem_alloc_security = smack_sem_alloc_security,
4166 .sem_free_security = smack_sem_free_security,
4167 .sem_associate = smack_sem_associate,
4168 .sem_semctl = smack_sem_semctl,
4169 .sem_semop = smack_sem_semop,
4170
Casey Schauflere114e472008-02-04 22:29:50 -08004171 .d_instantiate = smack_d_instantiate,
4172
4173 .getprocattr = smack_getprocattr,
4174 .setprocattr = smack_setprocattr,
4175
4176 .unix_stream_connect = smack_unix_stream_connect,
4177 .unix_may_send = smack_unix_may_send,
4178
4179 .socket_post_create = smack_socket_post_create,
Casey Schauflerc6739442013-05-22 18:42:56 -07004180 .socket_bind = smack_socket_bind,
Casey Schaufler6d3dc072008-12-31 12:54:12 -05004181 .socket_connect = smack_socket_connect,
4182 .socket_sendmsg = smack_socket_sendmsg,
Casey Schauflere114e472008-02-04 22:29:50 -08004183 .socket_sock_rcv_skb = smack_socket_sock_rcv_skb,
4184 .socket_getpeersec_stream = smack_socket_getpeersec_stream,
4185 .socket_getpeersec_dgram = smack_socket_getpeersec_dgram,
4186 .sk_alloc_security = smack_sk_alloc_security,
4187 .sk_free_security = smack_sk_free_security,
4188 .sock_graft = smack_sock_graft,
4189 .inet_conn_request = smack_inet_conn_request,
Paul Moore07feee82009-03-27 17:10:54 -04004190 .inet_csk_clone = smack_inet_csk_clone,
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004191
Casey Schauflere114e472008-02-04 22:29:50 -08004192 /* key management security hooks */
4193#ifdef CONFIG_KEYS
4194 .key_alloc = smack_key_alloc,
4195 .key_free = smack_key_free,
4196 .key_permission = smack_key_permission,
4197#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004198
4199 /* Audit hooks */
4200#ifdef CONFIG_AUDIT
4201 .audit_rule_init = smack_audit_rule_init,
4202 .audit_rule_known = smack_audit_rule_known,
4203 .audit_rule_match = smack_audit_rule_match,
4204 .audit_rule_free = smack_audit_rule_free,
4205#endif /* CONFIG_AUDIT */
4206
David Quigley746df9b2013-05-22 12:50:35 -04004207 .ismaclabel = smack_ismaclabel,
Casey Schauflere114e472008-02-04 22:29:50 -08004208 .secid_to_secctx = smack_secid_to_secctx,
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004209 .secctx_to_secid = smack_secctx_to_secid,
Casey Schauflere114e472008-02-04 22:29:50 -08004210 .release_secctx = smack_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04004211 .inode_notifysecctx = smack_inode_notifysecctx,
4212 .inode_setsecctx = smack_inode_setsecctx,
4213 .inode_getsecctx = smack_inode_getsecctx,
Casey Schauflere114e472008-02-04 22:29:50 -08004214};
4215
Etienne Basset7198e2e2009-03-24 20:53:24 +01004216
Casey Schaufler86812bb2012-04-17 18:55:46 -07004217static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004218{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004219 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004220 * Initialize rule list locks
4221 */
4222 mutex_init(&smack_known_huh.smk_rules_lock);
4223 mutex_init(&smack_known_hat.smk_rules_lock);
4224 mutex_init(&smack_known_floor.smk_rules_lock);
4225 mutex_init(&smack_known_star.smk_rules_lock);
4226 mutex_init(&smack_known_invalid.smk_rules_lock);
4227 mutex_init(&smack_known_web.smk_rules_lock);
4228 /*
4229 * Initialize rule lists
4230 */
4231 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4232 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4233 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4234 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
4235 INIT_LIST_HEAD(&smack_known_invalid.smk_rules);
4236 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4237 /*
4238 * Create the known labels list
4239 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004240 smk_insert_entry(&smack_known_huh);
4241 smk_insert_entry(&smack_known_hat);
4242 smk_insert_entry(&smack_known_star);
4243 smk_insert_entry(&smack_known_floor);
4244 smk_insert_entry(&smack_known_invalid);
4245 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004246}
4247
Casey Schauflere114e472008-02-04 22:29:50 -08004248/**
4249 * smack_init - initialize the smack system
4250 *
4251 * Returns 0
4252 */
4253static __init int smack_init(void)
4254{
David Howellsd84f4f92008-11-14 10:39:23 +11004255 struct cred *cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004256 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004257
Casey Schaufler7898e1f2011-01-17 08:05:27 -08004258 if (!security_module_enable(&smack_ops))
4259 return 0;
4260
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004261 tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
4262 GFP_KERNEL);
Casey Schaufler676dac42010-12-02 06:43:39 -08004263 if (tsp == NULL)
4264 return -ENOMEM;
4265
Casey Schauflere114e472008-02-04 22:29:50 -08004266 printk(KERN_INFO "Smack: Initializing.\n");
4267
4268 /*
4269 * Set the security state for the initial task.
4270 */
David Howellsd84f4f92008-11-14 10:39:23 +11004271 cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004272 cred->security = tsp;
Casey Schauflere114e472008-02-04 22:29:50 -08004273
Casey Schaufler86812bb2012-04-17 18:55:46 -07004274 /* initialize the smack_known_list */
4275 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004276
4277 /*
4278 * Register with LSM
4279 */
4280 if (register_security(&smack_ops))
4281 panic("smack: Unable to register with kernel.\n");
4282
4283 return 0;
4284}
4285
4286/*
4287 * Smack requires early initialization in order to label
4288 * all processes and objects when they are created.
4289 */
4290security_initcall(smack_init);