blob: ddffda39d10713d5f52f3335a281c28570d08ab5 [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>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020031#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080033#include <linux/mutex.h>
34#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080035#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070036#include <net/ip.h>
37#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100038#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070039#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050040#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030041#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000042#include <linux/msg.h>
43#include <linux/shm.h>
44#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053045#include <linux/parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080046#include "smack.h"
47
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020048#define TRANS_TRUE "TRUE"
49#define TRANS_TRUE_SIZE 4
50
Casey Schauflerc6739442013-05-22 18:42:56 -070051#define SMK_CONNECTING 0
52#define SMK_RECEIVING 1
53#define SMK_SENDING 2
54
Casey Schaufler21abb1e2015-07-22 14:25:31 -070055#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053056DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080057static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070058#endif
Rohit1a5b4722014-10-15 17:40:41 +053059static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080060int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070061
Al Viroc3300aa2018-12-16 01:52:24 -050062#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
63static struct {
64 const char *name;
65 int len;
66 int opt;
67} smk_mount_opts[] = {
68 A(fsdefault), A(fsfloor), A(fshat), A(fsroot), A(fstransmute)
Vivek Trivedi3bf27892015-06-22 15:36:06 +053069};
Al Viroc3300aa2018-12-16 01:52:24 -050070#undef A
71
72static int match_opt_prefix(char *s, int l, char **arg)
73{
74 int i;
75
76 for (i = 0; i < ARRAY_SIZE(smk_mount_opts); i++) {
77 size_t len = smk_mount_opts[i].len;
78 if (len > l || memcmp(s, smk_mount_opts[i].name, len))
79 continue;
80 if (len == l || s[len] != '=')
81 continue;
82 *arg = s + len + 1;
83 return smk_mount_opts[i].opt;
84 }
85 return Opt_error;
86}
Vivek Trivedi3bf27892015-06-22 15:36:06 +053087
Casey Schaufler3d04c922015-08-12 11:56:02 -070088#ifdef CONFIG_SECURITY_SMACK_BRINGUP
89static char *smk_bu_mess[] = {
90 "Bringup Error", /* Unused */
91 "Bringup", /* SMACK_BRINGUP_ALLOW */
92 "Unconfined Subject", /* SMACK_UNCONFINED_SUBJECT */
93 "Unconfined Object", /* SMACK_UNCONFINED_OBJECT */
94};
95
Casey Schauflerd166c802014-08-27 14:51:27 -070096static void smk_bu_mode(int mode, char *s)
97{
98 int i = 0;
99
100 if (mode & MAY_READ)
101 s[i++] = 'r';
102 if (mode & MAY_WRITE)
103 s[i++] = 'w';
104 if (mode & MAY_EXEC)
105 s[i++] = 'x';
106 if (mode & MAY_APPEND)
107 s[i++] = 'a';
108 if (mode & MAY_TRANSMUTE)
109 s[i++] = 't';
110 if (mode & MAY_LOCK)
111 s[i++] = 'l';
112 if (i == 0)
113 s[i++] = '-';
114 s[i] = '\0';
115}
116#endif
117
118#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200119static int smk_bu_note(char *note, struct smack_known *sskp,
120 struct smack_known *oskp, int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700121{
122 char acc[SMK_NUM_ACCESS_TYPE + 1];
123
124 if (rc <= 0)
125 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700126 if (rc > SMACK_UNCONFINED_OBJECT)
127 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700128
129 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700130 pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200131 sskp->smk_known, oskp->smk_known, acc, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700132 return 0;
133}
134#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200135#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700136#endif
137
138#ifdef CONFIG_SECURITY_SMACK_BRINGUP
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200139static int smk_bu_current(char *note, struct smack_known *oskp,
140 int mode, int rc)
Casey Schauflerd166c802014-08-27 14:51:27 -0700141{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800142 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700143 char acc[SMK_NUM_ACCESS_TYPE + 1];
144
145 if (rc <= 0)
146 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700147 if (rc > SMACK_UNCONFINED_OBJECT)
148 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700149
150 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700151 pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200152 tsp->smk_task->smk_known, oskp->smk_known,
153 acc, current->comm, note);
Casey Schauflerd166c802014-08-27 14:51:27 -0700154 return 0;
155}
156#else
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200157#define smk_bu_current(note, oskp, mode, RC) (RC)
Casey Schauflerd166c802014-08-27 14:51:27 -0700158#endif
159
160#ifdef CONFIG_SECURITY_SMACK_BRINGUP
161static int smk_bu_task(struct task_struct *otp, int mode, int rc)
162{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800163 struct task_smack *tsp = smack_cred(current_cred());
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300164 struct smack_known *smk_task = smk_of_task_struct(otp);
Casey Schauflerd166c802014-08-27 14:51:27 -0700165 char acc[SMK_NUM_ACCESS_TYPE + 1];
166
167 if (rc <= 0)
168 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700169 if (rc > SMACK_UNCONFINED_OBJECT)
170 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700171
172 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700173 pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300174 tsp->smk_task->smk_known, smk_task->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700175 current->comm, otp->comm);
176 return 0;
177}
178#else
179#define smk_bu_task(otp, mode, RC) (RC)
180#endif
181
182#ifdef CONFIG_SECURITY_SMACK_BRINGUP
183static int smk_bu_inode(struct inode *inode, int mode, int rc)
184{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800185 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800186 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700187 char acc[SMK_NUM_ACCESS_TYPE + 1];
188
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700189 if (isp->smk_flags & SMK_INODE_IMPURE)
190 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
191 inode->i_sb->s_id, inode->i_ino, current->comm);
192
Casey Schauflerd166c802014-08-27 14:51:27 -0700193 if (rc <= 0)
194 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700195 if (rc > SMACK_UNCONFINED_OBJECT)
196 rc = 0;
197 if (rc == SMACK_UNCONFINED_SUBJECT &&
198 (mode & (MAY_WRITE | MAY_APPEND)))
199 isp->smk_flags |= SMK_INODE_IMPURE;
Casey Schauflerd166c802014-08-27 14:51:27 -0700200
201 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700202
203 pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
204 tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
Casey Schauflerd166c802014-08-27 14:51:27 -0700205 inode->i_sb->s_id, inode->i_ino, current->comm);
206 return 0;
207}
208#else
209#define smk_bu_inode(inode, mode, RC) (RC)
210#endif
211
212#ifdef CONFIG_SECURITY_SMACK_BRINGUP
213static int smk_bu_file(struct file *file, int mode, int rc)
214{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800215 struct task_smack *tsp = smack_cred(current_cred());
Casey Schauflerd166c802014-08-27 14:51:27 -0700216 struct smack_known *sskp = tsp->smk_task;
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800217 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800218 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700219 char acc[SMK_NUM_ACCESS_TYPE + 1];
220
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700221 if (isp->smk_flags & SMK_INODE_IMPURE)
222 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
223 inode->i_sb->s_id, inode->i_ino, current->comm);
224
Casey Schauflerd166c802014-08-27 14:51:27 -0700225 if (rc <= 0)
226 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700227 if (rc > SMACK_UNCONFINED_OBJECT)
228 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700229
230 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700231 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Casey Schaufler5e7270a2014-12-12 17:19:19 -0800232 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400233 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700234 current->comm);
235 return 0;
236}
237#else
238#define smk_bu_file(file, mode, RC) (RC)
239#endif
240
241#ifdef CONFIG_SECURITY_SMACK_BRINGUP
242static int smk_bu_credfile(const struct cred *cred, struct file *file,
243 int mode, int rc)
244{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800245 struct task_smack *tsp = smack_cred(cred);
Casey Schauflerd166c802014-08-27 14:51:27 -0700246 struct smack_known *sskp = tsp->smk_task;
Al Viro45063092016-12-04 18:24:56 -0500247 struct inode *inode = file_inode(file);
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800248 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerd166c802014-08-27 14:51:27 -0700249 char acc[SMK_NUM_ACCESS_TYPE + 1];
250
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700251 if (isp->smk_flags & SMK_INODE_IMPURE)
252 pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
253 inode->i_sb->s_id, inode->i_ino, current->comm);
254
Casey Schauflerd166c802014-08-27 14:51:27 -0700255 if (rc <= 0)
256 return rc;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700257 if (rc > SMACK_UNCONFINED_OBJECT)
258 rc = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -0700259
260 smk_bu_mode(mode, acc);
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700261 pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200262 sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
Al Viroa4555892014-10-21 20:11:25 -0400263 inode->i_sb->s_id, inode->i_ino, file,
Casey Schauflerd166c802014-08-27 14:51:27 -0700264 current->comm);
265 return 0;
266}
267#else
268#define smk_bu_credfile(cred, file, mode, RC) (RC)
269#endif
270
Casey Schauflere114e472008-02-04 22:29:50 -0800271/**
272 * smk_fetch - Fetch the smack label from a file.
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100273 * @name: type of the label (attribute)
Casey Schauflere114e472008-02-04 22:29:50 -0800274 * @ip: a pointer to the inode
275 * @dp: a pointer to the dentry
276 *
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200277 * Returns a pointer to the master list entry for the Smack label,
278 * NULL if there was no label to fetch, or an error code.
Casey Schauflere114e472008-02-04 22:29:50 -0800279 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700280static struct smack_known *smk_fetch(const char *name, struct inode *ip,
281 struct dentry *dp)
Casey Schauflere114e472008-02-04 22:29:50 -0800282{
283 int rc;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700284 char *buffer;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700285 struct smack_known *skp = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -0800286
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200287 if (!(ip->i_opflags & IOP_XATTR))
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200288 return ERR_PTR(-EOPNOTSUPP);
Casey Schauflere114e472008-02-04 22:29:50 -0800289
Casey Schauflerf7112e62012-05-06 15:22:02 -0700290 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
291 if (buffer == NULL)
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200292 return ERR_PTR(-ENOMEM);
Casey Schauflere114e472008-02-04 22:29:50 -0800293
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200294 rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200295 if (rc < 0)
296 skp = ERR_PTR(rc);
297 else if (rc == 0)
298 skp = NULL;
299 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700300 skp = smk_import_entry(buffer, rc);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700301
302 kfree(buffer);
303
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700304 return skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800305}
306
307/**
308 * new_inode_smack - allocate an inode security blob
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200309 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800310 *
311 * Returns the new blob or NULL if there's no memory available
312 */
Casey Schaufler1eddfe82015-07-30 14:35:14 -0700313static struct inode_smack *new_inode_smack(struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800314{
315 struct inode_smack *isp;
316
Rohit1a5b4722014-10-15 17:40:41 +0530317 isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800318 if (isp == NULL)
319 return NULL;
320
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200321 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800322 isp->smk_flags = 0;
323 mutex_init(&isp->smk_lock);
324
325 return isp;
326}
327
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800328/**
Casey Schauflerbbd36622018-11-12 09:30:56 -0800329 * init_task_smack - initialize a task security blob
330 * @tsp: blob to initialize
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100331 * @task: a pointer to the Smack label for the running task
332 * @forked: a pointer to the Smack label for the forked task
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800333 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800334 */
Casey Schauflerbbd36622018-11-12 09:30:56 -0800335static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
336 struct smack_known *forked)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800337{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800338 tsp->smk_task = task;
339 tsp->smk_forked = forked;
340 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200341 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800342 mutex_init(&tsp->smk_rules_lock);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800343}
344
345/**
346 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100347 * @nhead: new rules header pointer
348 * @ohead: old rules header pointer
349 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800350 *
351 * Returns 0 on success, -ENOMEM on error
352 */
353static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
354 gfp_t gfp)
355{
356 struct smack_rule *nrp;
357 struct smack_rule *orp;
358 int rc = 0;
359
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800360 list_for_each_entry_rcu(orp, ohead, list) {
361 nrp = kzalloc(sizeof(struct smack_rule), gfp);
362 if (nrp == NULL) {
363 rc = -ENOMEM;
364 break;
365 }
366 *nrp = *orp;
367 list_add_rcu(&nrp->list, nhead);
368 }
369 return rc;
370}
371
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100372/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200373 * smk_copy_relabel - copy smk_relabel labels list
374 * @nhead: new rules header pointer
375 * @ohead: old rules header pointer
376 * @gfp: type of the memory for the allocation
377 *
378 * Returns 0 on success, -ENOMEM on error
379 */
380static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
381 gfp_t gfp)
382{
383 struct smack_known_list_elem *nklep;
384 struct smack_known_list_elem *oklep;
385
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200386 list_for_each_entry(oklep, ohead, list) {
387 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
388 if (nklep == NULL) {
389 smk_destroy_label_list(nhead);
390 return -ENOMEM;
391 }
392 nklep->smk_label = oklep->smk_label;
393 list_add(&nklep->list, nhead);
394 }
395
396 return 0;
397}
398
399/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100400 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
401 * @mode - input mode in form of PTRACE_MODE_*
402 *
403 * Returns a converted MAY_* mode usable by smack rules
404 */
405static inline unsigned int smk_ptrace_mode(unsigned int mode)
406{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800407 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100408 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800409 if (mode & PTRACE_MODE_READ)
410 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100411
412 return 0;
413}
414
415/**
416 * smk_ptrace_rule_check - helper for ptrace access
417 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200418 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100419 * @mode: ptrace attachment mode (PTRACE_MODE_*)
420 * @func: name of the function that called us, used for audit
421 *
422 * Returns 0 on access granted, -error on error
423 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200424static int smk_ptrace_rule_check(struct task_struct *tracer,
425 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100426 unsigned int mode, const char *func)
427{
428 int rc;
429 struct smk_audit_info ad, *saip = NULL;
430 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200431 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700432 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100433
434 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
435 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
436 smk_ad_setfield_u_tsk(&ad, tracer);
437 saip = &ad;
438 }
439
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300440 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700441 tracercred = __task_cred(tracer);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800442 tsp = smack_cred(tracercred);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200443 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100444
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100445 if ((mode & PTRACE_MODE_ATTACH) &&
446 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
447 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200448 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100449 rc = 0;
450 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
451 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700452 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100453 rc = 0;
454 else
455 rc = -EACCES;
456
457 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200458 smack_log(tracer_known->smk_known,
459 tracee_known->smk_known,
460 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100461
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300462 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100463 return rc;
464 }
465
466 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200467 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300468
469 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100470 return rc;
471}
472
Casey Schauflere114e472008-02-04 22:29:50 -0800473/*
474 * LSM hooks.
475 * We he, that is fun!
476 */
477
478/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000479 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800480 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100481 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800482 *
483 * Returns 0 if access is OK, an error code otherwise
484 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100485 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800486 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000487static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800488{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700489 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800490
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300491 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200492
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700493 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100494}
Casey Schauflere114e472008-02-04 22:29:50 -0800495
David Howells5cd9c582008-08-14 11:37:28 +0100496/**
497 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
498 * @ptp: parent task pointer
499 *
500 * Returns 0 if access is OK, an error code otherwise
501 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100502 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100503 */
504static int smack_ptrace_traceme(struct task_struct *ptp)
505{
506 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700507 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100508
Casey Schauflerb17103a2018-11-09 16:12:56 -0800509 skp = smk_of_task(smack_cred(current_cred()));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200510
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200511 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800512 return rc;
513}
514
515/**
516 * smack_syslog - Smack approval on syslog
517 * @type: message type
518 *
Casey Schauflere114e472008-02-04 22:29:50 -0800519 * Returns 0 on success, error code otherwise.
520 */
Eric Paris12b30522010-11-15 18:36:29 -0500521static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800522{
Eric Paris12b30522010-11-15 18:36:29 -0500523 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700524 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800525
Casey Schaufler1880eff2012-06-05 15:28:30 -0700526 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800527 return 0;
528
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800529 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800530 rc = -EACCES;
531
532 return rc;
533}
534
535
536/*
537 * Superblock Hooks.
538 */
539
540/**
541 * smack_sb_alloc_security - allocate a superblock blob
542 * @sb: the superblock getting the blob
543 *
544 * Returns 0 on success or -ENOMEM on error.
545 */
546static int smack_sb_alloc_security(struct super_block *sb)
547{
548 struct superblock_smack *sbsp;
549
550 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
551
552 if (sbsp == NULL)
553 return -ENOMEM;
554
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200555 sbsp->smk_root = &smack_known_floor;
556 sbsp->smk_default = &smack_known_floor;
557 sbsp->smk_floor = &smack_known_floor;
558 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700559 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500560 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700561 */
Casey Schauflere114e472008-02-04 22:29:50 -0800562 sb->s_security = sbsp;
563
564 return 0;
565}
566
567/**
568 * smack_sb_free_security - free a superblock blob
569 * @sb: the superblock getting the blob
570 *
571 */
572static void smack_sb_free_security(struct super_block *sb)
573{
574 kfree(sb->s_security);
575 sb->s_security = NULL;
576}
577
Al Viro12085b12018-12-13 15:18:05 -0500578struct smack_mnt_opts {
579 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
580};
581
Al Viro204cc0c2018-12-13 13:41:47 -0500582static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800583{
Al Viro12085b12018-12-13 15:18:05 -0500584 struct smack_mnt_opts *opts = mnt_opts;
585 kfree(opts->fsdefault);
586 kfree(opts->fsfloor);
587 kfree(opts->fshat);
588 kfree(opts->fsroot);
589 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500590 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800591}
592
Al Viro55c0e5b2018-12-16 01:09:45 -0500593static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530594{
Al Viro55c0e5b2018-12-16 01:09:45 -0500595 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530596
Al Viro55c0e5b2018-12-16 01:09:45 -0500597 if (!opts) {
598 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
599 if (!opts)
600 return -ENOMEM;
601 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530602 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500603 if (!s)
604 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530605
Al Viro55c0e5b2018-12-16 01:09:45 -0500606 switch (token) {
607 case Opt_fsdefault:
608 if (opts->fsdefault)
609 goto out_opt_err;
610 opts->fsdefault = s;
611 break;
612 case Opt_fsfloor:
613 if (opts->fsfloor)
614 goto out_opt_err;
615 opts->fsfloor = s;
616 break;
617 case Opt_fshat:
618 if (opts->fshat)
619 goto out_opt_err;
620 opts->fshat = s;
621 break;
622 case Opt_fsroot:
623 if (opts->fsroot)
624 goto out_opt_err;
625 opts->fsroot = s;
626 break;
627 case Opt_fstransmute:
628 if (opts->fstransmute)
629 goto out_opt_err;
630 opts->fstransmute = s;
631 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530632 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530633 return 0;
634
635out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530636 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500637 return -EINVAL;
638}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530639
Al Virod2497e12018-12-16 01:37:06 -0500640static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530641{
Al Virod2497e12018-12-16 01:37:06 -0500642 char *from = options, *to = options;
643 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530644
Al Viroc3300aa2018-12-16 01:52:24 -0500645 while (1) {
646 char *next = strchr(from, ',');
647 int token, len, rc;
648 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530649
Al Viroc3300aa2018-12-16 01:52:24 -0500650 if (next)
651 len = next - from;
652 else
653 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530654
Al Viroc3300aa2018-12-16 01:52:24 -0500655 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500656 if (token != Opt_error) {
657 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
658 rc = smack_add_opt(token, arg, mnt_opts);
659 if (unlikely(rc)) {
660 kfree(arg);
661 if (*mnt_opts)
662 smack_free_mnt_opts(*mnt_opts);
663 *mnt_opts = NULL;
664 return rc;
665 }
666 } else {
667 if (!first) { // copy with preceding comma
668 from--;
669 len++;
670 }
671 if (to != from)
672 memmove(to, from, len);
673 to += len;
674 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530675 }
Al Viroc3300aa2018-12-16 01:52:24 -0500676 if (!from[len])
677 break;
678 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530679 }
Al Virod2497e12018-12-16 01:37:06 -0500680 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530681 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530682}
683
684/**
685 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800686 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530687 * @opts: Smack mount options
688 * @kern_flags: mount option from kernel space or user space
689 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800690 *
691 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530692 *
693 * Allow filesystems with binary mount data to explicitly set Smack mount
694 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800695 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530696static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500697 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530698 unsigned long kern_flags,
699 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800700{
701 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000702 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800703 struct superblock_smack *sp = sb->s_security;
704 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800705 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500706 struct smack_mnt_opts *opts = mnt_opts;
707 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800708
Seth Forshee9f50eda2015-09-23 15:16:06 -0500709 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800710 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700711
Himanshu Shukla2097f592016-11-10 16:19:52 +0530712 if (!smack_privileged(CAP_MAC_ADMIN)) {
713 /*
714 * Unprivileged mounts don't get to specify Smack values.
715 */
Al Viro12085b12018-12-13 15:18:05 -0500716 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530717 return -EPERM;
718 /*
719 * Unprivileged mounts get root and default from the caller.
720 */
721 skp = smk_of_current();
722 sp->smk_root = skp;
723 sp->smk_default = skp;
724 /*
725 * For a handful of fs types with no user-controlled
726 * backing store it's okay to trust security labels
727 * in the filesystem. The rest are untrusted.
728 */
729 if (sb->s_user_ns != &init_user_ns &&
730 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
731 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500732 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530733 sp->smk_flags |= SMK_SB_UNTRUSTED;
734 }
735 }
736
Seth Forshee9f50eda2015-09-23 15:16:06 -0500737 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800738
Al Viro12085b12018-12-13 15:18:05 -0500739 if (opts) {
740 if (opts->fsdefault) {
741 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200742 if (IS_ERR(skp))
743 return PTR_ERR(skp);
744 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500745 }
746 if (opts->fsfloor) {
747 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530748 if (IS_ERR(skp))
749 return PTR_ERR(skp);
750 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500751 }
752 if (opts->fshat) {
753 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530754 if (IS_ERR(skp))
755 return PTR_ERR(skp);
756 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500757 }
758 if (opts->fsroot) {
759 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200760 if (IS_ERR(skp))
761 return PTR_ERR(skp);
762 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500763 }
764 if (opts->fstransmute) {
765 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200766 if (IS_ERR(skp))
767 return PTR_ERR(skp);
768 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500769 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800770 }
771 }
772
773 /*
774 * Initialize the root inode.
775 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800776 isp = smack_inode(inode);
José Bollo55dfc5d2014-01-08 15:53:05 +0100777 if (isp == NULL) {
778 isp = new_inode_smack(sp->smk_root);
779 if (isp == NULL)
780 return -ENOMEM;
781 inode->i_security = isp;
Casey Schauflere830b392013-05-22 18:43:07 -0700782 } else
Casey Schauflere114e472008-02-04 22:29:50 -0800783 isp->smk_inode = sp->smk_root;
784
Casey Schauflere830b392013-05-22 18:43:07 -0700785 if (transmute)
786 isp->smk_flags |= SMK_INODE_TRANSMUTE;
787
Casey Schauflere114e472008-02-04 22:29:50 -0800788 return 0;
789}
790
791/**
792 * smack_sb_statfs - Smack check on statfs
793 * @dentry: identifies the file system in question
794 *
795 * Returns 0 if current can read the floor of the filesystem,
796 * and error code otherwise
797 */
798static int smack_sb_statfs(struct dentry *dentry)
799{
800 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200801 int rc;
802 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800803
Eric Parisa2694342011-04-25 13:10:27 -0400804 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200805 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
806
807 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700808 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200809 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800810}
811
Casey Schauflere114e472008-02-04 22:29:50 -0800812/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800813 * BPRM hooks
814 */
815
Casey Schauflerce8a4322011-09-29 18:21:01 -0700816/**
817 * smack_bprm_set_creds - set creds for exec
818 * @bprm: the exec information
819 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100820 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700821 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800822static int smack_bprm_set_creds(struct linux_binprm *bprm)
823{
Al Viro496ad9a2013-01-23 17:07:38 -0500824 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800825 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800826 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500827 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800828 int rc;
829
Kees Cookddb4a142017-07-18 15:25:23 -0700830 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800831 return 0;
832
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800833 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300834 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800835 return 0;
836
Seth Forshee809c02e2016-04-26 14:36:22 -0500837 sbsp = inode->i_sb->s_security;
838 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
839 isp->smk_task != sbsp->smk_root)
840 return 0;
841
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300842 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100843 struct task_struct *tracer;
844 rc = 0;
845
846 rcu_read_lock();
847 tracer = ptrace_parent(current);
848 if (likely(tracer != NULL))
849 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200850 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100851 PTRACE_MODE_ATTACH,
852 __func__);
853 rcu_read_unlock();
854
855 if (rc != 0)
856 return rc;
857 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300858 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800859
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300860 bsp->smk_task = isp->smk_task;
861 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800862
Kees Cookccbb6e12017-07-18 15:25:26 -0700863 /* Decide if this is a secure exec. */
864 if (bsp->smk_task != bsp->smk_forked)
865 bprm->secureexec = 1;
866
Casey Schaufler676dac42010-12-02 06:43:39 -0800867 return 0;
868}
869
870/*
Casey Schauflere114e472008-02-04 22:29:50 -0800871 * Inode hooks
872 */
873
874/**
875 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800876 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800877 *
878 * Returns 0 if it gets a blob, -ENOMEM otherwise
879 */
880static int smack_inode_alloc_security(struct inode *inode)
881{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700882 struct smack_known *skp = smk_of_current();
883
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200884 inode->i_security = new_inode_smack(skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800885 if (inode->i_security == NULL)
886 return -ENOMEM;
887 return 0;
888}
889
890/**
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530891 * smack_inode_free_rcu - Free inode_smack blob from cache
892 * @head: the rcu_head for getting inode_smack pointer
893 *
894 * Call back function called from call_rcu() to free
895 * the i_security blob pointer in inode
896 */
897static void smack_inode_free_rcu(struct rcu_head *head)
898{
899 struct inode_smack *issp;
900
901 issp = container_of(head, struct inode_smack, smk_rcu);
902 kmem_cache_free(smack_inode_cache, issp);
903}
904
905/**
906 * smack_inode_free_security - free an inode blob using call_rcu()
Randy Dunlap251a2a92009-02-18 11:42:33 -0800907 * @inode: the inode with a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800908 *
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530909 * Clears the blob pointer in inode using RCU
Casey Schauflere114e472008-02-04 22:29:50 -0800910 */
911static void smack_inode_free_security(struct inode *inode)
912{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800913 struct inode_smack *issp = smack_inode(inode);
Himanshu Shukla3d4f6732016-11-23 11:59:19 +0530914
915 /*
916 * The inode may still be referenced in a path walk and
917 * a call to smack_inode_permission() can be made
918 * after smack_inode_free_security() is called.
919 * To avoid race condition free the i_security via RCU
920 * and leave the current inode->i_security pointer intact.
921 * The inode will be freed after the RCU grace period too.
922 */
923 call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
Casey Schauflere114e472008-02-04 22:29:50 -0800924}
925
926/**
927 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200928 * @inode: the newly created inode
929 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500930 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800931 * @name: where to put the attribute name
932 * @value: where to put the attribute value
933 * @len: where to put the length of the attribute
934 *
935 * Returns 0 if it all works out, -ENOMEM if there's no memory
936 */
937static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900938 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500939 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800940{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800941 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700942 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200943 struct smack_known *isp = smk_of_inode(inode);
944 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800945 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800946
Tetsuo Handa95489062013-07-25 05:44:02 +0900947 if (name)
948 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800949
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100950 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800951 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200952 may = smk_access_entry(skp->smk_known, dsp->smk_known,
953 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800954 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200955
956 /*
957 * If the access rule allows transmutation and
958 * the directory requests transmutation then
959 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700960 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200961 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800962 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700963 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200964 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700965 issp->smk_flags |= SMK_INODE_CHANGED;
966 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200967
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200968 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800969 if (*value == NULL)
970 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800971
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200972 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100973 }
Casey Schauflere114e472008-02-04 22:29:50 -0800974
975 return 0;
976}
977
978/**
979 * smack_inode_link - Smack check on link
980 * @old_dentry: the existing object
981 * @dir: unused
982 * @new_dentry: the new object
983 *
984 * Returns 0 if access is permitted, an error code otherwise
985 */
986static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
987 struct dentry *new_dentry)
988{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200989 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200990 struct smk_audit_info ad;
991 int rc;
992
Eric Parisa2694342011-04-25 13:10:27 -0400993 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200994 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800995
David Howellsc6f493d2015-03-17 22:26:22 +0000996 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200997 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000998 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800999
David Howells88025652015-01-29 12:02:32 +00001000 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001001 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001002 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1003 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001004 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001005 }
1006
1007 return rc;
1008}
1009
1010/**
1011 * smack_inode_unlink - Smack check on inode deletion
1012 * @dir: containing directory object
1013 * @dentry: file to unlink
1014 *
1015 * Returns 0 if current can write the containing directory
1016 * and the object, error code otherwise
1017 */
1018static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1019{
David Howellsc6f493d2015-03-17 22:26:22 +00001020 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001021 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001022 int rc;
1023
Eric Parisa2694342011-04-25 13:10:27 -04001024 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001025 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1026
Casey Schauflere114e472008-02-04 22:29:50 -08001027 /*
1028 * You need write access to the thing you're unlinking
1029 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001030 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001031 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001032 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001033 /*
1034 * You also need write access to the containing directory
1035 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001037 smk_ad_setfield_u_fs_inode(&ad, dir);
1038 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001039 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001040 }
Casey Schauflere114e472008-02-04 22:29:50 -08001041 return rc;
1042}
1043
1044/**
1045 * smack_inode_rmdir - Smack check on directory deletion
1046 * @dir: containing directory object
1047 * @dentry: directory to unlink
1048 *
1049 * Returns 0 if current can write the containing directory
1050 * and the directory, error code otherwise
1051 */
1052static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1053{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001054 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001055 int rc;
1056
Eric Parisa2694342011-04-25 13:10:27 -04001057 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001058 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1059
Casey Schauflere114e472008-02-04 22:29:50 -08001060 /*
1061 * You need write access to the thing you're removing
1062 */
David Howellsc6f493d2015-03-17 22:26:22 +00001063 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1064 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001065 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001066 /*
1067 * You also need write access to the containing directory
1068 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001070 smk_ad_setfield_u_fs_inode(&ad, dir);
1071 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001072 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001073 }
Casey Schauflere114e472008-02-04 22:29:50 -08001074
1075 return rc;
1076}
1077
1078/**
1079 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001080 * @old_inode: unused
1081 * @old_dentry: the old object
1082 * @new_inode: unused
1083 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001084 *
1085 * Read and write access is required on both the old and
1086 * new directories.
1087 *
1088 * Returns 0 if access is permitted, an error code otherwise
1089 */
1090static int smack_inode_rename(struct inode *old_inode,
1091 struct dentry *old_dentry,
1092 struct inode *new_inode,
1093 struct dentry *new_dentry)
1094{
1095 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001096 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001097 struct smk_audit_info ad;
1098
Eric Parisa2694342011-04-25 13:10:27 -04001099 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001100 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001101
David Howellsc6f493d2015-03-17 22:26:22 +00001102 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001104 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001105
David Howells88025652015-01-29 12:02:32 +00001106 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001107 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001108 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1109 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001110 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001111 }
Casey Schauflere114e472008-02-04 22:29:50 -08001112 return rc;
1113}
1114
1115/**
1116 * smack_inode_permission - Smack version of permission()
1117 * @inode: the inode in question
1118 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001119 *
1120 * This is the important Smack hook.
1121 *
1122 * Returns 0 if access is permitted, -EACCES otherwise
1123 */
Al Viroe74f71e2011-06-20 19:38:15 -04001124static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001125{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001126 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001127 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001128 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001129 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001130
1131 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001132 /*
1133 * No permission to check. Existence test. Yup, it's there.
1134 */
1135 if (mask == 0)
1136 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001137
Seth Forshee9f50eda2015-09-23 15:16:06 -05001138 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1139 if (smk_of_inode(inode) != sbsp->smk_root)
1140 return -EACCES;
1141 }
1142
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001143 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001144 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001145 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001146 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001147 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001148 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1149 rc = smk_bu_inode(inode, mask, rc);
1150 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001151}
1152
1153/**
1154 * smack_inode_setattr - Smack check for setting attributes
1155 * @dentry: the object
1156 * @iattr: for the force flag
1157 *
1158 * Returns 0 if access is permitted, an error code otherwise
1159 */
1160static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1161{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001162 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001163 int rc;
1164
Casey Schauflere114e472008-02-04 22:29:50 -08001165 /*
1166 * Need to allow for clearing the setuid bit.
1167 */
1168 if (iattr->ia_valid & ATTR_FORCE)
1169 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001170 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001172
David Howellsc6f493d2015-03-17 22:26:22 +00001173 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1174 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001175 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001176}
1177
1178/**
1179 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001180 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001181 * @dentry: the object
1182 *
1183 * Returns 0 if access is permitted, an error code otherwise
1184 */
Al Viro3f7036a2015-03-08 19:28:30 -04001185static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001186{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001187 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001188 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001189 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001190
Eric Parisf48b7392011-04-25 12:54:27 -04001191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001192 smk_ad_setfield_u_fs_path(&ad, *path);
1193 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1194 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001195 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001196}
1197
1198/**
1199 * smack_inode_setxattr - Smack check for setting xattrs
1200 * @dentry: the object
1201 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001202 * @value: value of the attribute
1203 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001204 * @flags: unused
1205 *
1206 * This protects the Smack attribute explicitly.
1207 *
1208 * Returns 0 if access is permitted, an error code otherwise
1209 */
David Howells8f0cfa52008-04-29 00:59:41 -07001210static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1211 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001212{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001213 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001214 struct smack_known *skp;
1215 int check_priv = 0;
1216 int check_import = 0;
1217 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001218 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001219
Casey Schaufler19760ad2013-12-16 16:27:26 -08001220 /*
1221 * Check label validity here so import won't fail in post_setxattr
1222 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001223 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1224 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001225 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1226 check_priv = 1;
1227 check_import = 1;
1228 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1229 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1230 check_priv = 1;
1231 check_import = 1;
1232 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001233 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001234 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001235 if (size != TRANS_TRUE_SIZE ||
1236 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1237 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001238 } else
1239 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1240
Casey Schaufler19760ad2013-12-16 16:27:26 -08001241 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1242 rc = -EPERM;
1243
1244 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001245 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001246 if (IS_ERR(skp))
1247 rc = PTR_ERR(skp);
1248 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001249 (skp == &smack_known_star || skp == &smack_known_web)))
1250 rc = -EINVAL;
1251 }
1252
Eric Parisa2694342011-04-25 13:10:27 -04001253 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001254 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1255
Casey Schauflerd166c802014-08-27 14:51:27 -07001256 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001257 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1258 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001259 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001260
1261 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001262}
1263
1264/**
1265 * smack_inode_post_setxattr - Apply the Smack update approved above
1266 * @dentry: object
1267 * @name: attribute name
1268 * @value: attribute value
1269 * @size: attribute size
1270 * @flags: unused
1271 *
1272 * Set the pointer in the inode blob to the entry found
1273 * in the master label list.
1274 */
David Howells8f0cfa52008-04-29 00:59:41 -07001275static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1276 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001277{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001278 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001279 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001280
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001281 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1282 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1283 return;
1284 }
1285
Casey Schaufler676dac42010-12-02 06:43:39 -08001286 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001287 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001288 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001289 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001290 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001291 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001292 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001293 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001294 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001295 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001296 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001297 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001298 }
Casey Schauflere114e472008-02-04 22:29:50 -08001299
1300 return;
1301}
1302
Casey Schauflerce8a4322011-09-29 18:21:01 -07001303/**
Casey Schauflere114e472008-02-04 22:29:50 -08001304 * smack_inode_getxattr - Smack check on getxattr
1305 * @dentry: the object
1306 * @name: unused
1307 *
1308 * Returns 0 if access is permitted, an error code otherwise
1309 */
David Howells8f0cfa52008-04-29 00:59:41 -07001310static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001311{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001312 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001313 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001314
Eric Parisa2694342011-04-25 13:10:27 -04001315 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001316 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1317
David Howellsc6f493d2015-03-17 22:26:22 +00001318 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1319 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001320 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001321}
1322
Casey Schauflerce8a4322011-09-29 18:21:01 -07001323/**
Casey Schauflere114e472008-02-04 22:29:50 -08001324 * smack_inode_removexattr - Smack check on removexattr
1325 * @dentry: the object
1326 * @name: name of the attribute
1327 *
1328 * Removing the Smack attribute requires CAP_MAC_ADMIN
1329 *
1330 * Returns 0 if access is permitted, an error code otherwise
1331 */
David Howells8f0cfa52008-04-29 00:59:41 -07001332static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001333{
Casey Schaufler676dac42010-12-02 06:43:39 -08001334 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001335 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001336 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001337
Casey Schauflerbcdca222008-02-23 15:24:04 -08001338 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1339 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001340 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001341 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001342 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301343 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001344 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001345 rc = -EPERM;
1346 } else
1347 rc = cap_inode_removexattr(dentry, name);
1348
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001349 if (rc != 0)
1350 return rc;
1351
Eric Parisa2694342011-04-25 13:10:27 -04001352 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001353 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001354
David Howellsc6f493d2015-03-17 22:26:22 +00001355 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1356 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001357 if (rc != 0)
1358 return rc;
1359
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001360 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001361 /*
1362 * Don't do anything special for these.
1363 * XATTR_NAME_SMACKIPIN
1364 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001365 */
José Bollo80124952016-01-12 21:23:40 +01001366 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001367 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001368 struct superblock_smack *sbsp = sbp->s_security;
1369
1370 isp->smk_inode = sbsp->smk_default;
1371 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001372 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001373 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001374 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001375 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1376 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001377
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001378 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001379}
1380
1381/**
1382 * smack_inode_getsecurity - get smack xattrs
1383 * @inode: the object
1384 * @name: attribute name
1385 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001386 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001387 *
1388 * Returns the size of the attribute or an error code
1389 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001390static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001391 const char *name, void **buffer,
1392 bool alloc)
1393{
1394 struct socket_smack *ssp;
1395 struct socket *sock;
1396 struct super_block *sbp;
1397 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001398 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001399
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001400 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001401 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001402 else {
1403 /*
1404 * The rest of the Smack xattrs are only on sockets.
1405 */
1406 sbp = ip->i_sb;
1407 if (sbp->s_magic != SOCKFS_MAGIC)
1408 return -EOPNOTSUPP;
1409
1410 sock = SOCKET_I(ip);
1411 if (sock == NULL || sock->sk == NULL)
1412 return -EOPNOTSUPP;
1413
1414 ssp = sock->sk->sk_security;
1415
1416 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1417 isp = ssp->smk_in;
1418 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1419 isp = ssp->smk_out;
1420 else
1421 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001422 }
1423
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001424 if (alloc) {
1425 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1426 if (*buffer == NULL)
1427 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001428 }
1429
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001430 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001431}
1432
1433
1434/**
1435 * smack_inode_listsecurity - list the Smack attributes
1436 * @inode: the object
1437 * @buffer: where they go
1438 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001439 */
1440static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1441 size_t buffer_size)
1442{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001443 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001444
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001445 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001446 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001447
1448 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001449}
1450
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001451/**
1452 * smack_inode_getsecid - Extract inode's security id
1453 * @inode: inode to extract the info from
1454 * @secid: where result will be saved
1455 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001456static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001457{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001458 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001459
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001460 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001461}
1462
Casey Schauflere114e472008-02-04 22:29:50 -08001463/*
1464 * File Hooks
1465 */
1466
Casey Schaufler491a0b02016-01-26 15:08:35 -08001467/*
1468 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001469 *
1470 * Should access checks be done on each read or write?
1471 * UNICOS and SELinux say yes.
1472 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1473 *
1474 * I'll say no for now. Smack does not do the frequent
1475 * label changing that SELinux does.
1476 */
Casey Schauflere114e472008-02-04 22:29:50 -08001477
1478/**
1479 * smack_file_alloc_security - assign a file security blob
1480 * @file: the object
1481 *
1482 * The security blob for a file is a pointer to the master
1483 * label list, so no allocation is done.
1484 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001485 * f_security is the owner security information. It
1486 * isn't used on file access checks, it's for send_sigio.
1487 *
Casey Schauflere114e472008-02-04 22:29:50 -08001488 * Returns 0
1489 */
1490static int smack_file_alloc_security(struct file *file)
1491{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001492 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001493
Casey Schauflerf28952a2018-11-12 09:38:53 -08001494 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001495 return 0;
1496}
1497
1498/**
Casey Schauflere114e472008-02-04 22:29:50 -08001499 * smack_file_ioctl - Smack check on ioctls
1500 * @file: the object
1501 * @cmd: what to do
1502 * @arg: unused
1503 *
1504 * Relies heavily on the correct use of the ioctl command conventions.
1505 *
1506 * Returns 0 if allowed, error code otherwise
1507 */
1508static int smack_file_ioctl(struct file *file, unsigned int cmd,
1509 unsigned long arg)
1510{
1511 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001512 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001513 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001514
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001515 if (unlikely(IS_PRIVATE(inode)))
1516 return 0;
1517
Eric Parisf48b7392011-04-25 12:54:27 -04001518 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001519 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001520
Casey Schauflerd166c802014-08-27 14:51:27 -07001521 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001522 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001523 rc = smk_bu_file(file, MAY_WRITE, rc);
1524 }
Casey Schauflere114e472008-02-04 22:29:50 -08001525
Casey Schauflerd166c802014-08-27 14:51:27 -07001526 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001527 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001528 rc = smk_bu_file(file, MAY_READ, rc);
1529 }
Casey Schauflere114e472008-02-04 22:29:50 -08001530
1531 return rc;
1532}
1533
1534/**
1535 * smack_file_lock - Smack check on file locking
1536 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001537 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001538 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001539 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001540 */
1541static int smack_file_lock(struct file *file, unsigned int cmd)
1542{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001543 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001544 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001545 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001546
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001547 if (unlikely(IS_PRIVATE(inode)))
1548 return 0;
1549
Eric Paris92f42502011-04-25 13:15:55 -04001550 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1551 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001552 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001553 rc = smk_bu_file(file, MAY_LOCK, rc);
1554 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001555}
1556
1557/**
1558 * smack_file_fcntl - Smack check on fcntl
1559 * @file: the object
1560 * @cmd: what action to check
1561 * @arg: unused
1562 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001563 * Generally these operations are harmless.
1564 * File locking operations present an obvious mechanism
1565 * for passing information, so they require write access.
1566 *
Casey Schauflere114e472008-02-04 22:29:50 -08001567 * Returns 0 if current has access, error code otherwise
1568 */
1569static int smack_file_fcntl(struct file *file, unsigned int cmd,
1570 unsigned long arg)
1571{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001572 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001573 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001574 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001575
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001576 if (unlikely(IS_PRIVATE(inode)))
1577 return 0;
1578
Casey Schauflere114e472008-02-04 22:29:50 -08001579 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001580 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001581 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001582 case F_SETLK:
1583 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001584 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1585 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001586 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001587 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001588 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001589 case F_SETOWN:
1590 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001591 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1592 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001593 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001594 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001595 break;
1596 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001597 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001598 }
1599
1600 return rc;
1601}
1602
1603/**
Al Viroe5467852012-05-30 13:30:51 -04001604 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001605 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1606 * if mapping anonymous memory.
1607 * @file contains the file structure for file to map (may be NULL).
1608 * @reqprot contains the protection requested by the application.
1609 * @prot contains the protection that will be applied by the kernel.
1610 * @flags contains the operational flags.
1611 * Return 0 if permission is granted.
1612 */
Al Viroe5467852012-05-30 13:30:51 -04001613static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001614 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001615 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001616{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001617 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001618 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001619 struct smack_rule *srp;
1620 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001621 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001622 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001623 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001624 int may;
1625 int mmay;
1626 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001627 int rc;
1628
Al Viro496ad9a2013-01-23 17:07:38 -05001629 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001630 return 0;
1631
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001632 if (unlikely(IS_PRIVATE(file_inode(file))))
1633 return 0;
1634
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001635 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001636 if (isp->smk_mmap == NULL)
1637 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001638 sbsp = file_inode(file)->i_sb->s_security;
1639 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1640 isp->smk_mmap != sbsp->smk_root)
1641 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001642 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001643
Casey Schauflerb17103a2018-11-09 16:12:56 -08001644 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001645 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001646 rc = 0;
1647
1648 rcu_read_lock();
1649 /*
1650 * For each Smack rule associated with the subject
1651 * label verify that the SMACK64MMAP also has access
1652 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001653 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001654 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001655 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001656 /*
1657 * Matching labels always allows access.
1658 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001659 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001660 continue;
1661 /*
1662 * If there is a matching local rule take
1663 * that into account as well.
1664 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001665 may = smk_access_entry(srp->smk_subject->smk_known,
1666 okp->smk_known,
1667 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001668 if (may == -ENOENT)
1669 may = srp->smk_access;
1670 else
1671 may &= srp->smk_access;
1672 /*
1673 * If may is zero the SMACK64MMAP subject can't
1674 * possibly have less access.
1675 */
1676 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001677 continue;
1678
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001679 /*
1680 * Fetch the global list entry.
1681 * If there isn't one a SMACK64MMAP subject
1682 * can't have as much access as current.
1683 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001684 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1685 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001686 if (mmay == -ENOENT) {
1687 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001688 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001689 }
1690 /*
1691 * If there is a local entry it modifies the
1692 * potential access, too.
1693 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001694 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1695 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001696 if (tmay != -ENOENT)
1697 mmay &= tmay;
1698
1699 /*
1700 * If there is any access available to current that is
1701 * not available to a SMACK64MMAP subject
1702 * deny access.
1703 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001704 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001705 rc = -EACCES;
1706 break;
1707 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001708 }
1709
1710 rcu_read_unlock();
1711
1712 return rc;
1713}
1714
1715/**
Casey Schauflere114e472008-02-04 22:29:50 -08001716 * smack_file_set_fowner - set the file security blob value
1717 * @file: object in question
1718 *
Casey Schauflere114e472008-02-04 22:29:50 -08001719 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001720static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001721{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001722 struct smack_known **blob = smack_file(file);
1723
1724 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001725}
1726
1727/**
1728 * smack_file_send_sigiotask - Smack on sigio
1729 * @tsk: The target task
1730 * @fown: the object the signal come from
1731 * @signum: unused
1732 *
1733 * Allow a privileged task to get signals even if it shouldn't
1734 *
1735 * Returns 0 if a subject with the object's smack could
1736 * write to the task, an error code otherwise.
1737 */
1738static int smack_file_send_sigiotask(struct task_struct *tsk,
1739 struct fown_struct *fown, int signum)
1740{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001741 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001742 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001743 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001744 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001745 struct file *file;
1746 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001747 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001748
1749 /*
1750 * struct fown_struct is never outside the context of a struct file
1751 */
1752 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001753
Etienne Bassetecfcc532009-04-08 20:40:06 +02001754 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001755 blob = smack_file(file);
1756 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001757 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1758 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001759
1760 rcu_read_lock();
1761 tcred = __task_cred(tsk);
1762 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001763 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001764 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001765
1766 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1767 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001768 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001769 return rc;
1770}
1771
1772/**
1773 * smack_file_receive - Smack file receive check
1774 * @file: the object
1775 *
1776 * Returns 0 if current has access, error code otherwise
1777 */
1778static int smack_file_receive(struct file *file)
1779{
Casey Schauflerd166c802014-08-27 14:51:27 -07001780 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001781 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001782 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001783 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001784 struct socket *sock;
1785 struct task_smack *tsp;
1786 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001787
Seung-Woo Kim97775822015-04-17 15:25:04 +09001788 if (unlikely(IS_PRIVATE(inode)))
1789 return 0;
1790
Casey Schaufler4482a442013-12-30 17:37:45 -08001791 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001792 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001793
Casey Schaufler51d59af2017-05-31 08:53:42 -07001794 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001795 sock = SOCKET_I(inode);
1796 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001797 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001798 /*
1799 * If the receiving process can't write to the
1800 * passed socket or if the passed socket can't
1801 * write to the receiving process don't accept
1802 * the passed socket.
1803 */
1804 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1805 rc = smk_bu_file(file, may, rc);
1806 if (rc < 0)
1807 return rc;
1808 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1809 rc = smk_bu_file(file, may, rc);
1810 return rc;
1811 }
Casey Schauflere114e472008-02-04 22:29:50 -08001812 /*
1813 * This code relies on bitmasks.
1814 */
1815 if (file->f_mode & FMODE_READ)
1816 may = MAY_READ;
1817 if (file->f_mode & FMODE_WRITE)
1818 may |= MAY_WRITE;
1819
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001820 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001821 rc = smk_bu_file(file, may, rc);
1822 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001823}
1824
Casey Schaufler531f1d42011-09-19 12:41:42 -07001825/**
Eric Paris83d49852012-04-04 13:45:40 -04001826 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001827 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001828 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001829 *
1830 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001831 * Allow the open only if the task has read access. There are
1832 * many read operations (e.g. fstat) that you can do with an
1833 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001834 *
1835 * Returns 0
1836 */
Al Viro94817692018-07-10 14:13:18 -04001837static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001838{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001839 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001840 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001841 struct smk_audit_info ad;
1842 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001843
Casey Schauflera6834c02014-04-21 11:10:26 -07001844 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1845 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301846 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001847 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001848
1849 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001850}
1851
Casey Schauflere114e472008-02-04 22:29:50 -08001852/*
1853 * Task hooks
1854 */
1855
1856/**
David Howellsee18d642009-09-02 09:14:21 +01001857 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1858 * @new: the new credentials
1859 * @gfp: the atomicity of any memory allocations
1860 *
1861 * Prepare a blank set of credentials for modification. This must allocate all
1862 * the memory the LSM module might require such that cred_transfer() can
1863 * complete without error.
1864 */
1865static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1866{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001867 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001868 return 0;
1869}
1870
1871
1872/**
David Howellsf1752ee2008-11-14 10:39:17 +11001873 * smack_cred_free - "free" task-level security credentials
1874 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001875 *
Casey Schauflere114e472008-02-04 22:29:50 -08001876 */
David Howellsf1752ee2008-11-14 10:39:17 +11001877static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001878{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001879 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001880 struct smack_rule *rp;
1881 struct list_head *l;
1882 struct list_head *n;
1883
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001884 smk_destroy_label_list(&tsp->smk_relabel);
1885
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001886 list_for_each_safe(l, n, &tsp->smk_rules) {
1887 rp = list_entry(l, struct smack_rule, list);
1888 list_del(&rp->list);
1889 kfree(rp);
1890 }
Casey Schauflere114e472008-02-04 22:29:50 -08001891}
1892
1893/**
David Howellsd84f4f92008-11-14 10:39:23 +11001894 * smack_cred_prepare - prepare new set of credentials for modification
1895 * @new: the new credentials
1896 * @old: the original credentials
1897 * @gfp: the atomicity of any memory allocations
1898 *
1899 * Prepare a new set of credentials for modification.
1900 */
1901static int smack_cred_prepare(struct cred *new, const struct cred *old,
1902 gfp_t gfp)
1903{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001904 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001905 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001906 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001907
Casey Schauflerbbd36622018-11-12 09:30:56 -08001908 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301909
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001910 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1911 if (rc != 0)
1912 return rc;
1913
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001914 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1915 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001916 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001917}
1918
Randy Dunlap251a2a92009-02-18 11:42:33 -08001919/**
David Howellsee18d642009-09-02 09:14:21 +01001920 * smack_cred_transfer - Transfer the old credentials to the new credentials
1921 * @new: the new credentials
1922 * @old: the original credentials
1923 *
1924 * Fill in a set of blank credentials from another set of credentials.
1925 */
1926static void smack_cred_transfer(struct cred *new, const struct cred *old)
1927{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001928 struct task_smack *old_tsp = smack_cred(old);
1929 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001930
1931 new_tsp->smk_task = old_tsp->smk_task;
1932 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001933 mutex_init(&new_tsp->smk_rules_lock);
1934 INIT_LIST_HEAD(&new_tsp->smk_rules);
1935
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001936 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001937}
1938
1939/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001940 * smack_cred_getsecid - get the secid corresponding to a creds structure
1941 * @c: the object creds
1942 * @secid: where to put the result
1943 *
1944 * Sets the secid to contain a u32 version of the smack label.
1945 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001946static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001947{
1948 struct smack_known *skp;
1949
1950 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001951 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001952 *secid = skp->smk_secid;
1953 rcu_read_unlock();
1954}
1955
1956/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001957 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001958 * @new: points to the set of credentials to be modified.
1959 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001960 *
1961 * Set the security data for a kernel service.
1962 */
1963static int smack_kernel_act_as(struct cred *new, u32 secid)
1964{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001965 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11001966
Casey Schaufler152f91d2016-11-14 09:38:15 -08001967 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001968 return 0;
1969}
1970
1971/**
1972 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001973 * @new: points to the set of credentials to be modified
1974 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001975 *
1976 * Set the file creation context in a set of credentials to the same
1977 * as the objective context of the specified inode
1978 */
1979static int smack_kernel_create_files_as(struct cred *new,
1980 struct inode *inode)
1981{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001982 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08001983 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11001984
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001985 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001986 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001987 return 0;
1988}
1989
1990/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001991 * smk_curacc_on_task - helper to log task related access
1992 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001993 * @access: the access requested
1994 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001995 *
1996 * Return 0 if access is permitted
1997 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001998static int smk_curacc_on_task(struct task_struct *p, int access,
1999 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002000{
2001 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002002 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002003 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002004
Casey Schaufler531f1d42011-09-19 12:41:42 -07002005 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002006 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002007 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002008 rc = smk_bu_task(p, access, rc);
2009 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002010}
2011
2012/**
Casey Schauflere114e472008-02-04 22:29:50 -08002013 * smack_task_setpgid - Smack check on setting pgid
2014 * @p: the task object
2015 * @pgid: unused
2016 *
2017 * Return 0 if write access is permitted
2018 */
2019static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2020{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002021 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002022}
2023
2024/**
2025 * smack_task_getpgid - Smack access check for getpgid
2026 * @p: the object task
2027 *
2028 * Returns 0 if current can read the object task, error code otherwise
2029 */
2030static int smack_task_getpgid(struct task_struct *p)
2031{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002032 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002033}
2034
2035/**
2036 * smack_task_getsid - Smack access check for getsid
2037 * @p: the object task
2038 *
2039 * Returns 0 if current can read the object task, error code otherwise
2040 */
2041static int smack_task_getsid(struct task_struct *p)
2042{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002043 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002044}
2045
2046/**
2047 * smack_task_getsecid - get the secid of the task
2048 * @p: the object task
2049 * @secid: where to put the result
2050 *
2051 * Sets the secid to contain a u32 version of the smack label.
2052 */
2053static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2054{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002055 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002056
2057 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002058}
2059
2060/**
2061 * smack_task_setnice - Smack check on setting nice
2062 * @p: the task object
2063 * @nice: unused
2064 *
2065 * Return 0 if write access is permitted
2066 */
2067static int smack_task_setnice(struct task_struct *p, int nice)
2068{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002069 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002070}
2071
2072/**
2073 * smack_task_setioprio - Smack check on setting ioprio
2074 * @p: the task object
2075 * @ioprio: unused
2076 *
2077 * Return 0 if write access is permitted
2078 */
2079static int smack_task_setioprio(struct task_struct *p, int ioprio)
2080{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002081 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002082}
2083
2084/**
2085 * smack_task_getioprio - Smack check on reading ioprio
2086 * @p: the task object
2087 *
2088 * Return 0 if read access is permitted
2089 */
2090static int smack_task_getioprio(struct task_struct *p)
2091{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002092 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002093}
2094
2095/**
2096 * smack_task_setscheduler - Smack check on setting scheduler
2097 * @p: the task object
2098 * @policy: unused
2099 * @lp: unused
2100 *
2101 * Return 0 if read access is permitted
2102 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002103static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002104{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002105 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002106}
2107
2108/**
2109 * smack_task_getscheduler - Smack check on reading scheduler
2110 * @p: the task object
2111 *
2112 * Return 0 if read access is permitted
2113 */
2114static int smack_task_getscheduler(struct task_struct *p)
2115{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002116 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002117}
2118
2119/**
2120 * smack_task_movememory - Smack check on moving memory
2121 * @p: the task object
2122 *
2123 * Return 0 if write access is permitted
2124 */
2125static int smack_task_movememory(struct task_struct *p)
2126{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002127 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002128}
2129
2130/**
2131 * smack_task_kill - Smack check on signal delivery
2132 * @p: the task object
2133 * @info: unused
2134 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002135 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002136 *
2137 * Return 0 if write access is permitted
2138 *
Casey Schauflere114e472008-02-04 22:29:50 -08002139 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002140static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002141 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002142{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002143 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002144 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002145 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002146 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002147
Rafal Krypa18d872f2016-04-04 11:14:53 +02002148 if (!sig)
2149 return 0; /* null signal; existence test */
2150
Etienne Bassetecfcc532009-04-08 20:40:06 +02002151 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2152 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002153 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002154 * Sending a signal requires that the sender
2155 * can write the receiver.
2156 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002157 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002158 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2159 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002160 return rc;
2161 }
Casey Schauflere114e472008-02-04 22:29:50 -08002162 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002163 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002164 * specific behavior. This is not clean. For one thing
2165 * we can't take privilege into account.
2166 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002167 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002168 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2169 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002170 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002171}
2172
2173/**
Casey Schauflere114e472008-02-04 22:29:50 -08002174 * smack_task_to_inode - copy task smack into the inode blob
2175 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002176 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002177 *
2178 * Sets the smack pointer in the inode security blob
2179 */
2180static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2181{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002182 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002183 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002184
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002185 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002186 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002187}
2188
2189/*
2190 * Socket hooks.
2191 */
2192
2193/**
2194 * smack_sk_alloc_security - Allocate a socket blob
2195 * @sk: the socket
2196 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002197 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002198 *
2199 * Assign Smack pointers to current
2200 *
2201 * Returns 0 on success, -ENOMEM is there's no memory
2202 */
2203static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2204{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002205 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002206 struct socket_smack *ssp;
2207
2208 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2209 if (ssp == NULL)
2210 return -ENOMEM;
2211
jooseong lee08382c92016-11-03 11:54:39 +01002212 /*
2213 * Sockets created by kernel threads receive web label.
2214 */
2215 if (unlikely(current->flags & PF_KTHREAD)) {
2216 ssp->smk_in = &smack_known_web;
2217 ssp->smk_out = &smack_known_web;
2218 } else {
2219 ssp->smk_in = skp;
2220 ssp->smk_out = skp;
2221 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002222 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002223
2224 sk->sk_security = ssp;
2225
2226 return 0;
2227}
2228
2229/**
2230 * smack_sk_free_security - Free a socket blob
2231 * @sk: the socket
2232 *
2233 * Clears the blob pointer
2234 */
2235static void smack_sk_free_security(struct sock *sk)
2236{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302237#ifdef SMACK_IPV6_PORT_LABELING
2238 struct smk_port_label *spp;
2239
2240 if (sk->sk_family == PF_INET6) {
2241 rcu_read_lock();
2242 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2243 if (spp->smk_sock != sk)
2244 continue;
2245 spp->smk_can_reuse = 1;
2246 break;
2247 }
2248 rcu_read_unlock();
2249 }
2250#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002251 kfree(sk->sk_security);
2252}
2253
2254/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002255* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002256* @sip: the object end
2257*
2258* looks for host based access restrictions
2259*
2260* This version will only be appropriate for really small sets of single label
2261* hosts. The caller is responsible for ensuring that the RCU read lock is
2262* taken before calling this function.
2263*
2264* Returns the label of the far end or NULL if it's not special.
2265*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002266static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002267{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002268 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002269 struct in_addr *siap = &sip->sin_addr;
2270
2271 if (siap->s_addr == 0)
2272 return NULL;
2273
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002274 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2275 /*
2276 * we break after finding the first match because
2277 * the list is sorted from longest to shortest mask
2278 * so we have found the most specific match
2279 */
2280 if (snp->smk_host.s_addr ==
2281 (siap->s_addr & snp->smk_mask.s_addr))
2282 return snp->smk_label;
2283
2284 return NULL;
2285}
2286
2287#if IS_ENABLED(CONFIG_IPV6)
2288/*
2289 * smk_ipv6_localhost - Check for local ipv6 host address
2290 * @sip: the address
2291 *
2292 * Returns boolean true if this is the localhost address
2293 */
2294static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2295{
2296 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2297 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2298
2299 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2300 ntohs(be16p[7]) == 1)
2301 return true;
2302 return false;
2303}
2304
2305/**
2306* smack_ipv6host_label - check host based restrictions
2307* @sip: the object end
2308*
2309* looks for host based access restrictions
2310*
2311* This version will only be appropriate for really small sets of single label
2312* hosts. The caller is responsible for ensuring that the RCU read lock is
2313* taken before calling this function.
2314*
2315* Returns the label of the far end or NULL if it's not special.
2316*/
2317static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2318{
2319 struct smk_net6addr *snp;
2320 struct in6_addr *sap = &sip->sin6_addr;
2321 int i;
2322 int found = 0;
2323
2324 /*
2325 * It's local. Don't look for a host label.
2326 */
2327 if (smk_ipv6_localhost(sip))
2328 return NULL;
2329
2330 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002331 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002332 * If the label is NULL the entry has
2333 * been renounced. Ignore it.
2334 */
2335 if (snp->smk_label == NULL)
2336 continue;
2337 /*
Paul Moore07feee82009-03-27 17:10:54 -04002338 * we break after finding the first match because
2339 * the list is sorted from longest to shortest mask
2340 * so we have found the most specific match
2341 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002342 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002343 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2344 snp->smk_host.s6_addr16[i]) {
2345 found = 0;
2346 break;
2347 }
Etienne Basset43031542009-03-27 17:11:01 -04002348 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002349 if (found)
2350 return snp->smk_label;
2351 }
Paul Moore07feee82009-03-27 17:10:54 -04002352
2353 return NULL;
2354}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002355#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002356
2357/**
Casey Schauflere114e472008-02-04 22:29:50 -08002358 * smack_netlabel - Set the secattr on a socket
2359 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002360 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002361 *
2362 * Convert the outbound smack value (smk_out) to a
2363 * secattr and attach it to the socket.
2364 *
2365 * Returns 0 on success or an error code
2366 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002367static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002368{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002369 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002370 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002371 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002372
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002373 /*
2374 * Usually the netlabel code will handle changing the
2375 * packet labeling based on the label.
2376 * The case of a single label host is different, because
2377 * a single label host should never get a labeled packet
2378 * even though the label is usually associated with a packet
2379 * label.
2380 */
2381 local_bh_disable();
2382 bh_lock_sock_nested(sk);
2383
2384 if (ssp->smk_out == smack_net_ambient ||
2385 labeled == SMACK_UNLABELED_SOCKET)
2386 netlbl_sock_delattr(sk);
2387 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002388 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002389 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002390 }
2391
2392 bh_unlock_sock(sk);
2393 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002394
Casey Schauflere114e472008-02-04 22:29:50 -08002395 return rc;
2396}
2397
2398/**
Paul Moore07feee82009-03-27 17:10:54 -04002399 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2400 * @sk: the socket
2401 * @sap: the destination address
2402 *
2403 * Set the correct secattr for the given socket based on the destination
2404 * address and perform any outbound access checks needed.
2405 *
2406 * Returns 0 on success or an error code.
2407 *
2408 */
2409static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2410{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002411 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002412 int rc;
2413 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002414 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002415 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002416 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002417
2418 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002419 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002420 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002421#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002422 struct lsm_network_audit net;
2423
Eric Paris48c62af2012-04-02 13:15:44 -04002424 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2425 ad.a.u.net->family = sap->sin_family;
2426 ad.a.u.net->dport = sap->sin_port;
2427 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002428#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002429 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002430 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002431 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2432 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002433 } else {
2434 sk_lbl = SMACK_CIPSO_SOCKET;
2435 rc = 0;
2436 }
2437 rcu_read_unlock();
2438 if (rc != 0)
2439 return rc;
2440
2441 return smack_netlabel(sk, sk_lbl);
2442}
2443
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002444#if IS_ENABLED(CONFIG_IPV6)
2445/**
2446 * smk_ipv6_check - check Smack access
2447 * @subject: subject Smack label
2448 * @object: object Smack label
2449 * @address: address
2450 * @act: the action being taken
2451 *
2452 * Check an IPv6 access
2453 */
2454static int smk_ipv6_check(struct smack_known *subject,
2455 struct smack_known *object,
2456 struct sockaddr_in6 *address, int act)
2457{
2458#ifdef CONFIG_AUDIT
2459 struct lsm_network_audit net;
2460#endif
2461 struct smk_audit_info ad;
2462 int rc;
2463
2464#ifdef CONFIG_AUDIT
2465 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2466 ad.a.u.net->family = PF_INET6;
2467 ad.a.u.net->dport = ntohs(address->sin6_port);
2468 if (act == SMK_RECEIVING)
2469 ad.a.u.net->v6info.saddr = address->sin6_addr;
2470 else
2471 ad.a.u.net->v6info.daddr = address->sin6_addr;
2472#endif
2473 rc = smk_access(subject, object, MAY_WRITE, &ad);
2474 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2475 return rc;
2476}
2477#endif /* CONFIG_IPV6 */
2478
2479#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002480/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002481 * smk_ipv6_port_label - Smack port access table management
2482 * @sock: socket
2483 * @address: address
2484 *
2485 * Create or update the port list entry
2486 */
2487static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2488{
2489 struct sock *sk = sock->sk;
2490 struct sockaddr_in6 *addr6;
2491 struct socket_smack *ssp = sock->sk->sk_security;
2492 struct smk_port_label *spp;
2493 unsigned short port = 0;
2494
2495 if (address == NULL) {
2496 /*
2497 * This operation is changing the Smack information
2498 * on the bound socket. Take the changes to the port
2499 * as well.
2500 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302501 rcu_read_lock();
2502 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002503 if (sk != spp->smk_sock)
2504 continue;
2505 spp->smk_in = ssp->smk_in;
2506 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302507 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002508 return;
2509 }
2510 /*
2511 * A NULL address is only used for updating existing
2512 * bound entries. If there isn't one, it's OK.
2513 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302514 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002515 return;
2516 }
2517
2518 addr6 = (struct sockaddr_in6 *)address;
2519 port = ntohs(addr6->sin6_port);
2520 /*
2521 * This is a special case that is safely ignored.
2522 */
2523 if (port == 0)
2524 return;
2525
2526 /*
2527 * Look for an existing port list entry.
2528 * This is an indication that a port is getting reused.
2529 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302530 rcu_read_lock();
2531 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302532 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002533 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302534 if (spp->smk_can_reuse != 1) {
2535 rcu_read_unlock();
2536 return;
2537 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002538 spp->smk_port = port;
2539 spp->smk_sock = sk;
2540 spp->smk_in = ssp->smk_in;
2541 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302542 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302543 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002544 return;
2545 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302546 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002547 /*
2548 * A new port entry is required.
2549 */
2550 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2551 if (spp == NULL)
2552 return;
2553
2554 spp->smk_port = port;
2555 spp->smk_sock = sk;
2556 spp->smk_in = ssp->smk_in;
2557 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302558 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302559 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002560
Vishal Goel3c7ce342016-11-23 10:31:08 +05302561 mutex_lock(&smack_ipv6_lock);
2562 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2563 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002564 return;
2565}
2566
2567/**
2568 * smk_ipv6_port_check - check Smack port access
2569 * @sock: socket
2570 * @address: address
2571 *
2572 * Create or update the port list entry
2573 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002574static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002575 int act)
2576{
Casey Schauflerc6739442013-05-22 18:42:56 -07002577 struct smk_port_label *spp;
2578 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002579 struct smack_known *skp = NULL;
2580 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002581 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002582
2583 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002584 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002585 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002586 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002587 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002588 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002589 }
2590
2591 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002592 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002593 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002594 if (skp != NULL && object != NULL)
2595 return smk_ipv6_check(skp, object, address, act);
2596 if (skp == NULL)
2597 skp = smack_net_ambient;
2598 if (object == NULL)
2599 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002600
2601 /*
2602 * It's remote, so port lookup does no good.
2603 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002604 if (!smk_ipv6_localhost(address))
2605 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002606
2607 /*
2608 * It's local so the send check has to have passed.
2609 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002610 if (act == SMK_RECEIVING)
2611 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002612
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002613 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302614 rcu_read_lock();
2615 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302616 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002617 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002618 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002619 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002620 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002621 break;
2622 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302623 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002624
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002625 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002626}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002627#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002628
2629/**
Casey Schauflere114e472008-02-04 22:29:50 -08002630 * smack_inode_setsecurity - set smack xattrs
2631 * @inode: the object
2632 * @name: attribute name
2633 * @value: attribute value
2634 * @size: size of the attribute
2635 * @flags: unused
2636 *
2637 * Sets the named attribute in the appropriate blob
2638 *
2639 * Returns 0 on success, or an error code
2640 */
2641static int smack_inode_setsecurity(struct inode *inode, const char *name,
2642 const void *value, size_t size, int flags)
2643{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002644 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002645 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002646 struct socket_smack *ssp;
2647 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002648 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002649
Casey Schauflerf7112e62012-05-06 15:22:02 -07002650 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302651 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002652
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002653 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002654 if (IS_ERR(skp))
2655 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002656
2657 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002658 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002659 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002660 return 0;
2661 }
2662 /*
2663 * The rest of the Smack xattrs are only on sockets.
2664 */
2665 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2666 return -EOPNOTSUPP;
2667
2668 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002669 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002670 return -EOPNOTSUPP;
2671
2672 ssp = sock->sk->sk_security;
2673
2674 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002675 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002676 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002677 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002678 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002679 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2680 if (rc != 0)
2681 printk(KERN_WARNING
2682 "Smack: \"%s\" netlbl error %d.\n",
2683 __func__, -rc);
2684 }
Casey Schauflere114e472008-02-04 22:29:50 -08002685 } else
2686 return -EOPNOTSUPP;
2687
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002688#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002689 if (sock->sk->sk_family == PF_INET6)
2690 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002691#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002692
Casey Schauflere114e472008-02-04 22:29:50 -08002693 return 0;
2694}
2695
2696/**
2697 * smack_socket_post_create - finish socket setup
2698 * @sock: the socket
2699 * @family: protocol family
2700 * @type: unused
2701 * @protocol: unused
2702 * @kern: unused
2703 *
2704 * Sets the netlabel information on the socket
2705 *
2706 * Returns 0 on success, and error code otherwise
2707 */
2708static int smack_socket_post_create(struct socket *sock, int family,
2709 int type, int protocol, int kern)
2710{
Marcin Lis74123012015-01-22 15:40:33 +01002711 struct socket_smack *ssp;
2712
2713 if (sock->sk == NULL)
2714 return 0;
2715
2716 /*
2717 * Sockets created by kernel threads receive web label.
2718 */
2719 if (unlikely(current->flags & PF_KTHREAD)) {
2720 ssp = sock->sk->sk_security;
2721 ssp->smk_in = &smack_known_web;
2722 ssp->smk_out = &smack_known_web;
2723 }
2724
2725 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002726 return 0;
2727 /*
2728 * Set the outbound netlbl.
2729 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002730 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2731}
2732
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002733/**
2734 * smack_socket_socketpair - create socket pair
2735 * @socka: one socket
2736 * @sockb: another socket
2737 *
2738 * Cross reference the peer labels for SO_PEERSEC
2739 *
2740 * Returns 0 on success, and error code otherwise
2741 */
2742static int smack_socket_socketpair(struct socket *socka,
2743 struct socket *sockb)
2744{
2745 struct socket_smack *asp = socka->sk->sk_security;
2746 struct socket_smack *bsp = sockb->sk->sk_security;
2747
2748 asp->smk_packet = bsp->smk_out;
2749 bsp->smk_packet = asp->smk_out;
2750
2751 return 0;
2752}
2753
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002754#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002755/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002756 * smack_socket_bind - record port binding information.
2757 * @sock: the socket
2758 * @address: the port address
2759 * @addrlen: size of the address
2760 *
2761 * Records the label bound to a port.
2762 *
2763 * Returns 0
2764 */
2765static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2766 int addrlen)
2767{
2768 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2769 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002770 return 0;
2771}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002772#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002773
2774/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002775 * smack_socket_connect - connect access check
2776 * @sock: the socket
2777 * @sap: the other end
2778 * @addrlen: size of sap
2779 *
2780 * Verifies that a connection may be possible
2781 *
2782 * Returns 0 on success, and error code otherwise
2783 */
2784static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2785 int addrlen)
2786{
Casey Schauflerc6739442013-05-22 18:42:56 -07002787 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002788#if IS_ENABLED(CONFIG_IPV6)
2789 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2790#endif
2791#ifdef SMACK_IPV6_SECMARK_LABELING
2792 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002793 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002794#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002795
Casey Schauflerc6739442013-05-22 18:42:56 -07002796 if (sock->sk == NULL)
2797 return 0;
2798
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002799#ifdef SMACK_IPV6_SECMARK_LABELING
2800 ssp = sock->sk->sk_security;
2801#endif
2802
Casey Schauflerc6739442013-05-22 18:42:56 -07002803 switch (sock->sk->sk_family) {
2804 case PF_INET:
2805 if (addrlen < sizeof(struct sockaddr_in))
2806 return -EINVAL;
2807 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2808 break;
2809 case PF_INET6:
2810 if (addrlen < sizeof(struct sockaddr_in6))
2811 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002812#ifdef SMACK_IPV6_SECMARK_LABELING
2813 rsp = smack_ipv6host_label(sip);
2814 if (rsp != NULL)
2815 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002816 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002817#endif
2818#ifdef SMACK_IPV6_PORT_LABELING
2819 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2820#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002821 break;
2822 }
2823 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002824}
2825
2826/**
2827 * smack_flags_to_may - convert S_ to MAY_ values
2828 * @flags: the S_ value
2829 *
2830 * Returns the equivalent MAY_ value
2831 */
2832static int smack_flags_to_may(int flags)
2833{
2834 int may = 0;
2835
2836 if (flags & S_IRUGO)
2837 may |= MAY_READ;
2838 if (flags & S_IWUGO)
2839 may |= MAY_WRITE;
2840 if (flags & S_IXUGO)
2841 may |= MAY_EXEC;
2842
2843 return may;
2844}
2845
2846/**
2847 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2848 * @msg: the object
2849 *
2850 * Returns 0
2851 */
2852static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2853{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002854 struct smack_known *skp = smk_of_current();
2855
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002856 msg->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002857 return 0;
2858}
2859
2860/**
2861 * smack_msg_msg_free_security - Clear the security blob for msg_msg
2862 * @msg: the object
2863 *
2864 * Clears the blob pointer
2865 */
2866static void smack_msg_msg_free_security(struct msg_msg *msg)
2867{
2868 msg->security = NULL;
2869}
2870
2871/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002872 * smack_of_ipc - the smack pointer for the ipc
2873 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002874 *
2875 * Returns a pointer to the smack value
2876 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002877static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002878{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002879 return (struct smack_known *)isp->security;
Casey Schauflere114e472008-02-04 22:29:50 -08002880}
2881
2882/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002883 * smack_ipc_alloc_security - Set the security blob for ipc
2884 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002885 *
2886 * Returns 0
2887 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002888static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002889{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002890 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002891
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002892 isp->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002893 return 0;
2894}
2895
2896/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002897 * smack_ipc_free_security - Clear the security blob for ipc
2898 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002899 *
2900 * Clears the blob pointer
2901 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002902static void smack_ipc_free_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002903{
Casey Schauflere114e472008-02-04 22:29:50 -08002904 isp->security = NULL;
2905}
2906
2907/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002908 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002909 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002910 * @access : access requested
2911 *
2912 * Returns 0 if current has the requested access, error code otherwise
2913 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002914static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002915{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002916 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002917 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002918 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002919
2920#ifdef CONFIG_AUDIT
2921 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002922 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002923#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002924 rc = smk_curacc(ssp, access, &ad);
2925 rc = smk_bu_current("shm", ssp, access, rc);
2926 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002927}
2928
2929/**
Casey Schauflere114e472008-02-04 22:29:50 -08002930 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002931 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002932 * @shmflg: access requested
2933 *
2934 * Returns 0 if current has the requested access, error code otherwise
2935 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002936static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002937{
Casey Schauflere114e472008-02-04 22:29:50 -08002938 int may;
2939
2940 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002941 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002942}
2943
2944/**
2945 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002946 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002947 * @cmd: what it wants to do
2948 *
2949 * Returns 0 if current has the requested access, error code otherwise
2950 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002951static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002952{
Casey Schauflere114e472008-02-04 22:29:50 -08002953 int may;
2954
2955 switch (cmd) {
2956 case IPC_STAT:
2957 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002958 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002959 may = MAY_READ;
2960 break;
2961 case IPC_SET:
2962 case SHM_LOCK:
2963 case SHM_UNLOCK:
2964 case IPC_RMID:
2965 may = MAY_READWRITE;
2966 break;
2967 case IPC_INFO:
2968 case SHM_INFO:
2969 /*
2970 * System level information.
2971 */
2972 return 0;
2973 default:
2974 return -EINVAL;
2975 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002976 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002977}
2978
2979/**
2980 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002981 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002982 * @shmaddr: unused
2983 * @shmflg: access requested
2984 *
2985 * Returns 0 if current has the requested access, error code otherwise
2986 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002987static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08002988 int shmflg)
2989{
Casey Schauflere114e472008-02-04 22:29:50 -08002990 int may;
2991
2992 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002993 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002994}
2995
2996/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002997 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002998 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002999 * @access : access requested
3000 *
3001 * Returns 0 if current has the requested access, error code otherwise
3002 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003003static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003004{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003005 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003006 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003007 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003008
3009#ifdef CONFIG_AUDIT
3010 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003011 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003012#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003013 rc = smk_curacc(ssp, access, &ad);
3014 rc = smk_bu_current("sem", ssp, access, rc);
3015 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003016}
3017
3018/**
Casey Schauflere114e472008-02-04 22:29:50 -08003019 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003020 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003021 * @semflg: access requested
3022 *
3023 * Returns 0 if current has the requested access, error code otherwise
3024 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003025static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003026{
Casey Schauflere114e472008-02-04 22:29:50 -08003027 int may;
3028
3029 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003030 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003031}
3032
3033/**
3034 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003035 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003036 * @cmd: what it wants to do
3037 *
3038 * Returns 0 if current has the requested access, error code otherwise
3039 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003040static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003041{
Casey Schauflere114e472008-02-04 22:29:50 -08003042 int may;
3043
3044 switch (cmd) {
3045 case GETPID:
3046 case GETNCNT:
3047 case GETZCNT:
3048 case GETVAL:
3049 case GETALL:
3050 case IPC_STAT:
3051 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003052 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003053 may = MAY_READ;
3054 break;
3055 case SETVAL:
3056 case SETALL:
3057 case IPC_RMID:
3058 case IPC_SET:
3059 may = MAY_READWRITE;
3060 break;
3061 case IPC_INFO:
3062 case SEM_INFO:
3063 /*
3064 * System level information
3065 */
3066 return 0;
3067 default:
3068 return -EINVAL;
3069 }
3070
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003071 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003072}
3073
3074/**
3075 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003076 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003077 * @sops: unused
3078 * @nsops: unused
3079 * @alter: unused
3080 *
3081 * Treated as read and write in all cases.
3082 *
3083 * Returns 0 if access is allowed, error code otherwise
3084 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003085static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003086 unsigned nsops, int alter)
3087{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003088 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003089}
3090
3091/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003092 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003093 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003094 * @access : access requested
3095 *
3096 * return 0 if current has access, error otherwise
3097 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003098static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003099{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003100 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003101 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003102 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003103
3104#ifdef CONFIG_AUDIT
3105 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003106 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003107#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003108 rc = smk_curacc(msp, access, &ad);
3109 rc = smk_bu_current("msq", msp, access, rc);
3110 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003111}
3112
3113/**
Casey Schauflere114e472008-02-04 22:29:50 -08003114 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003115 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003116 * @msqflg: access requested
3117 *
3118 * Returns 0 if current has the requested access, error code otherwise
3119 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003120static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003121{
Casey Schauflere114e472008-02-04 22:29:50 -08003122 int may;
3123
3124 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003125 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003126}
3127
3128/**
3129 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003130 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003131 * @cmd: what it wants to do
3132 *
3133 * Returns 0 if current has the requested access, error code otherwise
3134 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003135static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003136{
Casey Schauflere114e472008-02-04 22:29:50 -08003137 int may;
3138
3139 switch (cmd) {
3140 case IPC_STAT:
3141 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003142 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003143 may = MAY_READ;
3144 break;
3145 case IPC_SET:
3146 case IPC_RMID:
3147 may = MAY_READWRITE;
3148 break;
3149 case IPC_INFO:
3150 case MSG_INFO:
3151 /*
3152 * System level information
3153 */
3154 return 0;
3155 default:
3156 return -EINVAL;
3157 }
3158
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003159 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003160}
3161
3162/**
3163 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003164 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003165 * @msg: unused
3166 * @msqflg: access requested
3167 *
3168 * Returns 0 if current has the requested access, error code otherwise
3169 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003170static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003171 int msqflg)
3172{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003173 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003174
Etienne Bassetecfcc532009-04-08 20:40:06 +02003175 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003176 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003177}
3178
3179/**
3180 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003181 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003182 * @msg: unused
3183 * @target: unused
3184 * @type: unused
3185 * @mode: unused
3186 *
3187 * Returns 0 if current has read and write access, error code otherwise
3188 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003189static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003190 struct task_struct *target, long type, int mode)
3191{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003192 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003193}
3194
3195/**
3196 * smack_ipc_permission - Smack access for ipc_permission()
3197 * @ipp: the object permissions
3198 * @flag: access requested
3199 *
3200 * Returns 0 if current has read and write access, error code otherwise
3201 */
3202static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3203{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003204 struct smack_known *iskp = ipp->security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003205 int may = smack_flags_to_may(flag);
3206 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003207 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003208
Etienne Bassetecfcc532009-04-08 20:40:06 +02003209#ifdef CONFIG_AUDIT
3210 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3211 ad.a.u.ipc_id = ipp->id;
3212#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003213 rc = smk_curacc(iskp, may, &ad);
3214 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003215 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003216}
3217
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003218/**
3219 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003220 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003221 * @secid: where result will be saved
3222 */
3223static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3224{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003225 struct smack_known *iskp = ipp->security;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003226
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003227 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003228}
3229
Casey Schauflere114e472008-02-04 22:29:50 -08003230/**
3231 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003232 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003233 * @inode: the object
3234 *
3235 * Set the inode's security blob if it hasn't been done already.
3236 */
3237static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3238{
3239 struct super_block *sbp;
3240 struct superblock_smack *sbsp;
3241 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003242 struct smack_known *skp;
3243 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003244 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003245 char trattr[TRANS_TRUE_SIZE];
3246 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003247 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003248 struct dentry *dp;
3249
3250 if (inode == NULL)
3251 return;
3252
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003253 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003254
3255 mutex_lock(&isp->smk_lock);
3256 /*
3257 * If the inode is already instantiated
3258 * take the quick way out
3259 */
3260 if (isp->smk_flags & SMK_INODE_INSTANT)
3261 goto unlockandout;
3262
3263 sbp = inode->i_sb;
3264 sbsp = sbp->s_security;
3265 /*
3266 * We're going to use the superblock default label
3267 * if there's no label on the file.
3268 */
3269 final = sbsp->smk_default;
3270
3271 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003272 * If this is the root inode the superblock
3273 * may be in the process of initialization.
3274 * If that is the case use the root value out
3275 * of the superblock.
3276 */
3277 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003278 switch (sbp->s_magic) {
3279 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003280 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003281 /*
3282 * The cgroup filesystem is never mounted,
3283 * so there's no opportunity to set the mount
3284 * options.
3285 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003286 sbsp->smk_root = &smack_known_star;
3287 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003288 isp->smk_inode = sbsp->smk_root;
3289 break;
3290 case TMPFS_MAGIC:
3291 /*
3292 * What about shmem/tmpfs anonymous files with dentry
3293 * obtained from d_alloc_pseudo()?
3294 */
3295 isp->smk_inode = smk_of_current();
3296 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003297 case PIPEFS_MAGIC:
3298 isp->smk_inode = smk_of_current();
3299 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003300 case SOCKFS_MAGIC:
3301 /*
3302 * Socket access is controlled by the socket
3303 * structures associated with the task involved.
3304 */
3305 isp->smk_inode = &smack_known_star;
3306 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003307 default:
3308 isp->smk_inode = sbsp->smk_root;
3309 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003310 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003311 isp->smk_flags |= SMK_INODE_INSTANT;
3312 goto unlockandout;
3313 }
3314
3315 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003316 * This is pretty hackish.
3317 * Casey says that we shouldn't have to do
3318 * file system specific code, but it does help
3319 * with keeping it simple.
3320 */
3321 switch (sbp->s_magic) {
3322 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003323 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003324 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003325 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003326 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003327 * that the smack file system doesn't do
3328 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003329 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003330 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003331 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003332 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003333 break;
3334 case DEVPTS_SUPER_MAGIC:
3335 /*
3336 * devpts seems content with the label of the task.
3337 * Programs that change smack have to treat the
3338 * pty with respect.
3339 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003340 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003341 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003342 case PROC_SUPER_MAGIC:
3343 /*
3344 * Casey says procfs appears not to care.
3345 * The superblock default suffices.
3346 */
3347 break;
3348 case TMPFS_MAGIC:
3349 /*
3350 * Device labels should come from the filesystem,
3351 * but watch out, because they're volitile,
3352 * getting recreated on every reboot.
3353 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003354 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003355 /*
Gustavo A. R. Silvab1fed3e2018-08-01 17:38:54 -05003356 * Fall through.
Casey Schauflere114e472008-02-04 22:29:50 -08003357 *
3358 * If a smack value has been set we want to use it,
3359 * but since tmpfs isn't giving us the opportunity
3360 * to set mount options simulate setting the
3361 * superblock default.
3362 */
3363 default:
3364 /*
3365 * This isn't an understood special case.
3366 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003367 */
3368
3369 /*
3370 * UNIX domain sockets use lower level socket data.
3371 */
3372 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003373 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003374 break;
3375 }
3376 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003377 * No xattr support means, alas, no SMACK label.
3378 * Use the aforeapplied default.
3379 * It would be curious if the label of the task
3380 * does not match that assigned.
3381 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003382 if (!(inode->i_opflags & IOP_XATTR))
3383 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003384 /*
3385 * Get the dentry for xattr.
3386 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003387 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003388 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003389 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003390 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003391
3392 /*
3393 * Transmuting directory
3394 */
3395 if (S_ISDIR(inode->i_mode)) {
3396 /*
3397 * If this is a new directory and the label was
3398 * transmuted when the inode was initialized
3399 * set the transmute attribute on the directory
3400 * and mark the inode.
3401 *
3402 * If there is a transmute attribute on the
3403 * directory mark the inode.
3404 */
3405 if (isp->smk_flags & SMK_INODE_CHANGED) {
3406 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003407 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003408 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003409 TRANS_TRUE, TRANS_TRUE_SIZE,
3410 0);
3411 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003412 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003413 XATTR_NAME_SMACKTRANSMUTE, trattr,
3414 TRANS_TRUE_SIZE);
3415 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3416 TRANS_TRUE_SIZE) != 0)
3417 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003418 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003419 if (rc >= 0)
3420 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003421 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003422 /*
3423 * Don't let the exec or mmap label be "*" or "@".
3424 */
3425 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3426 if (IS_ERR(skp) || skp == &smack_known_star ||
3427 skp == &smack_known_web)
3428 skp = NULL;
3429 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003430
Casey Schaufler19760ad2013-12-16 16:27:26 -08003431 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003432 if (IS_ERR(skp) || skp == &smack_known_star ||
3433 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003434 skp = NULL;
3435 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003436
Casey Schauflere114e472008-02-04 22:29:50 -08003437 dput(dp);
3438 break;
3439 }
3440
3441 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003442 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003443 else
3444 isp->smk_inode = final;
3445
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003446 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003447
3448unlockandout:
3449 mutex_unlock(&isp->smk_lock);
3450 return;
3451}
3452
3453/**
3454 * smack_getprocattr - Smack process attribute access
3455 * @p: the object task
3456 * @name: the name of the attribute in /proc/.../attr
3457 * @value: where to put the result
3458 *
3459 * Places a copy of the task Smack into value
3460 *
3461 * Returns the length of the smack label or an error code
3462 */
3463static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3464{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003465 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003466 char *cp;
3467 int slen;
3468
3469 if (strcmp(name, "current") != 0)
3470 return -EINVAL;
3471
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003472 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003473 if (cp == NULL)
3474 return -ENOMEM;
3475
3476 slen = strlen(cp);
3477 *value = cp;
3478 return slen;
3479}
3480
3481/**
3482 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003483 * @name: the name of the attribute in /proc/.../attr
3484 * @value: the value to set
3485 * @size: the size of the value
3486 *
3487 * Sets the Smack value of the task. Only setting self
3488 * is permitted and only with privilege
3489 *
3490 * Returns the length of the smack label or an error code
3491 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003492static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003493{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003494 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003495 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003496 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003497 struct smack_known_list_elem *sklep;
3498 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003499
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003500 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003501 return -EPERM;
3502
Casey Schauflerf7112e62012-05-06 15:22:02 -07003503 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003504 return -EINVAL;
3505
3506 if (strcmp(name, "current") != 0)
3507 return -EINVAL;
3508
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003509 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003510 if (IS_ERR(skp))
3511 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003512
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003513 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303514 * No process is ever allowed the web ("@") label
3515 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003516 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303517 if (skp == &smack_known_web || skp == &smack_known_star)
3518 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003519
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003520 if (!smack_privileged(CAP_MAC_ADMIN)) {
3521 rc = -EPERM;
3522 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3523 if (sklep->smk_label == skp) {
3524 rc = 0;
3525 break;
3526 }
3527 if (rc)
3528 return rc;
3529 }
3530
David Howellsd84f4f92008-11-14 10:39:23 +11003531 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003532 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003533 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003534
Casey Schauflerb17103a2018-11-09 16:12:56 -08003535 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003536 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003537 /*
3538 * process can change its label only once
3539 */
3540 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003541
David Howellsd84f4f92008-11-14 10:39:23 +11003542 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003543 return size;
3544}
3545
3546/**
3547 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003548 * @sock: one sock
3549 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003550 * @newsk: unused
3551 *
3552 * Return 0 if a subject with the smack of sock could access
3553 * an object with the smack of other, otherwise an error code
3554 */
David S. Miller3610cda2011-01-05 15:38:53 -08003555static int smack_unix_stream_connect(struct sock *sock,
3556 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003557{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003558 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003559 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003560 struct socket_smack *ssp = sock->sk_security;
3561 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003562 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003563 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003564 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003565#ifdef CONFIG_AUDIT
3566 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003567#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003568
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003569 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3570 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003571 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003572#ifdef CONFIG_AUDIT
3573 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3574 smk_ad_setfield_u_net_sk(&ad, other);
3575#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003576 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3577 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003578 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003579 okp = osp->smk_out;
3580 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003581 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003582 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003583 MAY_WRITE, rc);
3584 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003585 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003586
Casey Schaufler975d5e52011-09-26 14:43:39 -07003587 /*
3588 * Cross reference the peer labels for SO_PEERSEC.
3589 */
3590 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003591 nsp->smk_packet = ssp->smk_out;
3592 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003593 }
3594
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003595 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003596}
3597
3598/**
3599 * smack_unix_may_send - Smack access on UDS
3600 * @sock: one socket
3601 * @other: the other socket
3602 *
3603 * Return 0 if a subject with the smack of sock could access
3604 * an object with the smack of other, otherwise an error code
3605 */
3606static int smack_unix_may_send(struct socket *sock, struct socket *other)
3607{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003608 struct socket_smack *ssp = sock->sk->sk_security;
3609 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003610 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003611 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003612
Kees Cook923e9a12012-04-10 13:26:44 -07003613#ifdef CONFIG_AUDIT
3614 struct lsm_network_audit net;
3615
Eric Paris48c62af2012-04-02 13:15:44 -04003616 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003617 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003618#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003619
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003620 if (smack_privileged(CAP_MAC_OVERRIDE))
3621 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003622
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003623 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3624 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003625 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003626}
3627
3628/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003629 * smack_socket_sendmsg - Smack check based on destination host
3630 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003631 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003632 * @size: the size of the message
3633 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003634 * Return 0 if the current subject can write to the destination host.
3635 * For IPv4 this is only a question if the destination is a single label host.
3636 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003637 */
3638static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3639 int size)
3640{
3641 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003642#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003643 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003644#endif
3645#ifdef SMACK_IPV6_SECMARK_LABELING
3646 struct socket_smack *ssp = sock->sk->sk_security;
3647 struct smack_known *rsp;
3648#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003649 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003650
3651 /*
3652 * Perfectly reasonable for this to be NULL
3653 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003654 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003655 return 0;
3656
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003657 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003658 case AF_INET:
3659 rc = smack_netlabel_send(sock->sk, sip);
3660 break;
3661 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003662#ifdef SMACK_IPV6_SECMARK_LABELING
3663 rsp = smack_ipv6host_label(sap);
3664 if (rsp != NULL)
3665 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3666 SMK_CONNECTING);
3667#endif
3668#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003669 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003670#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003671 break;
3672 }
3673 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003674}
3675
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003676/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003677 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003678 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003679 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003680 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003681 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003682 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003683static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3684 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003685{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003686 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003687 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003688 int acat;
3689 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003690
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003691 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003692 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003693 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003694 * If there are flags but no level netlabel isn't
3695 * behaving the way we expect it to.
3696 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003697 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003698 * Without guidance regarding the smack value
3699 * for the packet fall back on the network
3700 * ambient value.
3701 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003702 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303703 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003704 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003705 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003706 /*
3707 * Compare the catsets. Use the netlbl APIs.
3708 */
3709 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3710 if ((skp->smk_netlabel.flags &
3711 NETLBL_SECATTR_MLS_CAT) == 0)
3712 found = 1;
3713 break;
3714 }
3715 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003716 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3717 acat + 1);
3718 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003719 skp->smk_netlabel.attr.mls.cat,
3720 kcat + 1);
3721 if (acat < 0 || kcat < 0)
3722 break;
3723 }
3724 if (acat == kcat) {
3725 found = 1;
3726 break;
3727 }
Casey Schauflere114e472008-02-04 22:29:50 -08003728 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003729 rcu_read_unlock();
3730
3731 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003732 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003733
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003734 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003735 return &smack_known_web;
3736 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003737 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003738 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003739 /*
3740 * Looks like a fallback, which gives us a secid.
3741 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003742 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003743 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003744 * Without guidance regarding the smack value
3745 * for the packet fall back on the network
3746 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003747 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003748 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003749}
3750
Casey Schaufler69f287a2014-12-12 17:08:40 -08003751#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003752static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003753{
Casey Schauflerc6739442013-05-22 18:42:56 -07003754 u8 nexthdr;
3755 int offset;
3756 int proto = -EINVAL;
3757 struct ipv6hdr _ipv6h;
3758 struct ipv6hdr *ip6;
3759 __be16 frag_off;
3760 struct tcphdr _tcph, *th;
3761 struct udphdr _udph, *uh;
3762 struct dccp_hdr _dccph, *dh;
3763
3764 sip->sin6_port = 0;
3765
3766 offset = skb_network_offset(skb);
3767 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3768 if (ip6 == NULL)
3769 return -EINVAL;
3770 sip->sin6_addr = ip6->saddr;
3771
3772 nexthdr = ip6->nexthdr;
3773 offset += sizeof(_ipv6h);
3774 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3775 if (offset < 0)
3776 return -EINVAL;
3777
3778 proto = nexthdr;
3779 switch (proto) {
3780 case IPPROTO_TCP:
3781 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3782 if (th != NULL)
3783 sip->sin6_port = th->source;
3784 break;
3785 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003786 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003787 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3788 if (uh != NULL)
3789 sip->sin6_port = uh->source;
3790 break;
3791 case IPPROTO_DCCP:
3792 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3793 if (dh != NULL)
3794 sip->sin6_port = dh->dccph_sport;
3795 break;
3796 }
3797 return proto;
3798}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003799#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003800
Casey Schauflere114e472008-02-04 22:29:50 -08003801/**
3802 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3803 * @sk: socket
3804 * @skb: packet
3805 *
3806 * Returns 0 if the packet should be delivered, an error code otherwise
3807 */
3808static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3809{
3810 struct netlbl_lsm_secattr secattr;
3811 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003812 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003813 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003814 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003815 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003816#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003817 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003818#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003819#if IS_ENABLED(CONFIG_IPV6)
3820 struct sockaddr_in6 sadd;
3821 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003822
3823 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3824 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003825#endif /* CONFIG_IPV6 */
3826
Piotr Sawicki129a9982018-07-19 11:42:58 +02003827 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003828 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003829#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3830 /*
3831 * If there is a secmark use it rather than the CIPSO label.
3832 * If there is no secmark fall back to CIPSO.
3833 * The secmark is assumed to reflect policy better.
3834 */
3835 if (skb && skb->secmark != 0) {
3836 skp = smack_from_secid(skb->secmark);
3837 goto access_check;
3838 }
3839#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003840 /*
3841 * Translate what netlabel gave us.
3842 */
3843 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003844
Piotr Sawicki129a9982018-07-19 11:42:58 +02003845 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003846 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003847 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003848 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003849 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003850
Casey Schauflerc6739442013-05-22 18:42:56 -07003851 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003852
Casey Schaufler69f287a2014-12-12 17:08:40 -08003853#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3854access_check:
3855#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003856#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003857 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003858 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003859 ad.a.u.net->netif = skb->skb_iif;
3860 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003861#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003862 /*
3863 * Receiving a packet requires that the other end
3864 * be able to write here. Read access is not required.
3865 * This is the simplist possible security model
3866 * for networking.
3867 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003868 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3869 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003870 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003871 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003872 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003873 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003874#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003875 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003876 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003877 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3878 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003879 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003880#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003881 if (skb && skb->secmark != 0)
3882 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003883 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003884 skp = smack_ipv6host_label(&sadd);
3885 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003886 skp = smack_net_ambient;
3887#ifdef CONFIG_AUDIT
3888 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003889 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003890 ad.a.u.net->netif = skb->skb_iif;
3891 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3892#endif /* CONFIG_AUDIT */
3893 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3894 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3895 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003896#endif /* SMACK_IPV6_SECMARK_LABELING */
3897#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003898 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003899#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003900 if (rc != 0)
3901 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3902 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003903 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003904#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003905 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003906
Paul Moorea8134292008-10-10 10:16:31 -04003907 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003908}
3909
3910/**
3911 * smack_socket_getpeersec_stream - pull in packet label
3912 * @sock: the socket
3913 * @optval: user's destination
3914 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003915 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003916 *
3917 * returns zero on success, an error code otherwise
3918 */
3919static int smack_socket_getpeersec_stream(struct socket *sock,
3920 char __user *optval,
3921 int __user *optlen, unsigned len)
3922{
3923 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003924 char *rcp = "";
3925 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003926 int rc = 0;
3927
3928 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003929 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003930 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003931 slen = strlen(rcp) + 1;
3932 }
Casey Schauflere114e472008-02-04 22:29:50 -08003933
3934 if (slen > len)
3935 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003936 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003937 rc = -EFAULT;
3938
3939 if (put_user(slen, optlen) != 0)
3940 rc = -EFAULT;
3941
3942 return rc;
3943}
3944
3945
3946/**
3947 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003948 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003949 * @skb: packet data
3950 * @secid: pointer to where to put the secid of the packet
3951 *
3952 * Sets the netlabel socket state on sk from parent
3953 */
3954static int smack_socket_getpeersec_dgram(struct socket *sock,
3955 struct sk_buff *skb, u32 *secid)
3956
3957{
3958 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003959 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003960 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003961 int family = PF_UNSPEC;
3962 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003963 int rc;
3964
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003965 if (skb != NULL) {
3966 if (skb->protocol == htons(ETH_P_IP))
3967 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003968#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003969 else if (skb->protocol == htons(ETH_P_IPV6))
3970 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003971#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003972 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003973 if (family == PF_UNSPEC && sock != NULL)
3974 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003975
Casey Schaufler69f287a2014-12-12 17:08:40 -08003976 switch (family) {
3977 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003978 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003979 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003980 break;
3981 case PF_INET:
3982#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3983 s = skb->secmark;
3984 if (s != 0)
3985 break;
3986#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003987 /*
3988 * Translate what netlabel gave us.
3989 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003990 if (sock != NULL && sock->sk != NULL)
3991 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003992 netlbl_secattr_init(&secattr);
3993 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3994 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003995 skp = smack_from_secattr(&secattr, ssp);
3996 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003997 }
3998 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003999 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004000 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004001#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004002 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004003#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004004 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004005 }
4006 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004007 if (s == 0)
4008 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004009 return 0;
4010}
4011
4012/**
Paul Moore07feee82009-03-27 17:10:54 -04004013 * smack_sock_graft - Initialize a newly created socket with an existing sock
4014 * @sk: child sock
4015 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004016 *
Paul Moore07feee82009-03-27 17:10:54 -04004017 * Set the smk_{in,out} state of an existing sock based on the process that
4018 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004019 */
4020static void smack_sock_graft(struct sock *sk, struct socket *parent)
4021{
4022 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004023 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004024
Paul Moore07feee82009-03-27 17:10:54 -04004025 if (sk == NULL ||
4026 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004027 return;
4028
4029 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004030 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004031 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004032 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004033}
4034
4035/**
4036 * smack_inet_conn_request - Smack access check on connect
4037 * @sk: socket involved
4038 * @skb: packet
4039 * @req: unused
4040 *
4041 * Returns 0 if a task with the packet label could write to
4042 * the socket, otherwise an error code
4043 */
4044static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4045 struct request_sock *req)
4046{
Paul Moore07feee82009-03-27 17:10:54 -04004047 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004048 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004049 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004050 struct netlbl_lsm_secattr secattr;
4051 struct sockaddr_in addr;
4052 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004053 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004054 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004055 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004056#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004057 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004058#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004059
Casey Schaufler69f287a2014-12-12 17:08:40 -08004060#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004061 if (family == PF_INET6) {
4062 /*
4063 * Handle mapped IPv4 packets arriving
4064 * via IPv6 sockets. Don't set up netlabel
4065 * processing on IPv6.
4066 */
4067 if (skb->protocol == htons(ETH_P_IP))
4068 family = PF_INET;
4069 else
4070 return 0;
4071 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004072#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004073
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004074#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4075 /*
4076 * If there is a secmark use it rather than the CIPSO label.
4077 * If there is no secmark fall back to CIPSO.
4078 * The secmark is assumed to reflect policy better.
4079 */
4080 if (skb && skb->secmark != 0) {
4081 skp = smack_from_secid(skb->secmark);
4082 goto access_check;
4083 }
4084#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4085
Paul Moore07feee82009-03-27 17:10:54 -04004086 netlbl_secattr_init(&secattr);
4087 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004088 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004089 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004090 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004091 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004092 netlbl_secattr_destroy(&secattr);
4093
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004094#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4095access_check:
4096#endif
4097
Etienne Bassetecfcc532009-04-08 20:40:06 +02004098#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004099 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4100 ad.a.u.net->family = family;
4101 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004102 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4103#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004104 /*
Paul Moore07feee82009-03-27 17:10:54 -04004105 * Receiving a packet requires that the other end be able to write
4106 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004107 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004108 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4109 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004110 if (rc != 0)
4111 return rc;
4112
4113 /*
4114 * Save the peer's label in the request_sock so we can later setup
4115 * smk_packet in the child socket so that SO_PEERCRED can report it.
4116 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004117 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004118
4119 /*
4120 * We need to decide if we want to label the incoming connection here
4121 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004122 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004123 */
4124 hdr = ip_hdr(skb);
4125 addr.sin_addr.s_addr = hdr->saddr;
4126 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004127 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004128 rcu_read_unlock();
4129
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004130 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004131 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004132 else
Paul Moore07feee82009-03-27 17:10:54 -04004133 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004134
4135 return rc;
4136}
4137
Paul Moore07feee82009-03-27 17:10:54 -04004138/**
4139 * smack_inet_csk_clone - Copy the connection information to the new socket
4140 * @sk: the new socket
4141 * @req: the connection's request_sock
4142 *
4143 * Transfer the connection's peer label to the newly created socket.
4144 */
4145static void smack_inet_csk_clone(struct sock *sk,
4146 const struct request_sock *req)
4147{
4148 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004149 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004150
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004151 if (req->peer_secid != 0) {
4152 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004153 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004154 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004155 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004156}
4157
Casey Schauflere114e472008-02-04 22:29:50 -08004158/*
4159 * Key management security hooks
4160 *
4161 * Casey has not tested key support very heavily.
4162 * The permission check is most likely too restrictive.
4163 * If you care about keys please have a look.
4164 */
4165#ifdef CONFIG_KEYS
4166
4167/**
4168 * smack_key_alloc - Set the key security blob
4169 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004170 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004171 * @flags: unused
4172 *
4173 * No allocation required
4174 *
4175 * Returns 0
4176 */
David Howellsd84f4f92008-11-14 10:39:23 +11004177static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004178 unsigned long flags)
4179{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004180 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004181
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004182 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004183 return 0;
4184}
4185
4186/**
4187 * smack_key_free - Clear the key security blob
4188 * @key: the object
4189 *
4190 * Clear the blob pointer
4191 */
4192static void smack_key_free(struct key *key)
4193{
4194 key->security = NULL;
4195}
4196
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004197/**
Casey Schauflere114e472008-02-04 22:29:50 -08004198 * smack_key_permission - Smack access on a key
4199 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004200 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004201 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004202 *
4203 * Return 0 if the task has read and write to the object,
4204 * an error code otherwise
4205 */
4206static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004207 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004208{
4209 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004210 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004211 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004212 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004213 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004214
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004215 /*
4216 * Validate requested permissions
4217 */
4218 if (perm & ~KEY_NEED_ALL)
4219 return -EINVAL;
4220
Casey Schauflere114e472008-02-04 22:29:50 -08004221 keyp = key_ref_to_ptr(key_ref);
4222 if (keyp == NULL)
4223 return -EINVAL;
4224 /*
4225 * If the key hasn't been initialized give it access so that
4226 * it may do so.
4227 */
4228 if (keyp->security == NULL)
4229 return 0;
4230 /*
4231 * This should not occur
4232 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004233 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004234 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004235
4236 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4237 return 0;
4238
Etienne Bassetecfcc532009-04-08 20:40:06 +02004239#ifdef CONFIG_AUDIT
4240 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4241 ad.a.u.key_struct.key = keyp->serial;
4242 ad.a.u.key_struct.key_desc = keyp->description;
4243#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004244 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4245 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004246 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004247 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004248 rc = smk_access(tkp, keyp->security, request, &ad);
4249 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4250 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004251}
José Bollo7fc5f362015-02-17 15:41:22 +01004252
4253/*
4254 * smack_key_getsecurity - Smack label tagging the key
4255 * @key points to the key to be queried
4256 * @_buffer points to a pointer that should be set to point to the
4257 * resulting string (if no label or an error occurs).
4258 * Return the length of the string (including terminating NUL) or -ve if
4259 * an error.
4260 * May also return 0 (and a NULL buffer pointer) if there is no label.
4261 */
4262static int smack_key_getsecurity(struct key *key, char **_buffer)
4263{
4264 struct smack_known *skp = key->security;
4265 size_t length;
4266 char *copy;
4267
4268 if (key->security == NULL) {
4269 *_buffer = NULL;
4270 return 0;
4271 }
4272
4273 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4274 if (copy == NULL)
4275 return -ENOMEM;
4276 length = strlen(copy) + 1;
4277
4278 *_buffer = copy;
4279 return length;
4280}
4281
Casey Schauflere114e472008-02-04 22:29:50 -08004282#endif /* CONFIG_KEYS */
4283
4284/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004285 * Smack Audit hooks
4286 *
4287 * Audit requires a unique representation of each Smack specific
4288 * rule. This unique representation is used to distinguish the
4289 * object to be audited from remaining kernel objects and also
4290 * works as a glue between the audit hooks.
4291 *
4292 * Since repository entries are added but never deleted, we'll use
4293 * the smack_known label address related to the given audit rule as
4294 * the needed unique representation. This also better fits the smack
4295 * model where nearly everything is a label.
4296 */
4297#ifdef CONFIG_AUDIT
4298
4299/**
4300 * smack_audit_rule_init - Initialize a smack audit rule
4301 * @field: audit rule fields given from user-space (audit.h)
4302 * @op: required testing operator (=, !=, >, <, ...)
4303 * @rulestr: smack label to be audited
4304 * @vrule: pointer to save our own audit rule representation
4305 *
4306 * Prepare to audit cases where (@field @op @rulestr) is true.
4307 * The label to be audited is created if necessay.
4308 */
4309static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4310{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004311 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004312 char **rule = (char **)vrule;
4313 *rule = NULL;
4314
4315 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4316 return -EINVAL;
4317
Al Viro5af75d82008-12-16 05:59:26 -05004318 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004319 return -EINVAL;
4320
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004321 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004322 if (IS_ERR(skp))
4323 return PTR_ERR(skp);
4324
4325 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004326
4327 return 0;
4328}
4329
4330/**
4331 * smack_audit_rule_known - Distinguish Smack audit rules
4332 * @krule: rule of interest, in Audit kernel representation format
4333 *
4334 * This is used to filter Smack rules from remaining Audit ones.
4335 * If it's proved that this rule belongs to us, the
4336 * audit_rule_match hook will be called to do the final judgement.
4337 */
4338static int smack_audit_rule_known(struct audit_krule *krule)
4339{
4340 struct audit_field *f;
4341 int i;
4342
4343 for (i = 0; i < krule->field_count; i++) {
4344 f = &krule->fields[i];
4345
4346 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4347 return 1;
4348 }
4349
4350 return 0;
4351}
4352
4353/**
4354 * smack_audit_rule_match - Audit given object ?
4355 * @secid: security id for identifying the object to test
4356 * @field: audit rule flags given from user-space
4357 * @op: required testing operator
4358 * @vrule: smack internal rule presentation
4359 * @actx: audit context associated with the check
4360 *
4361 * The core Audit hook. It's used to take the decision of
4362 * whether to audit or not to audit a given object.
4363 */
4364static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4365 struct audit_context *actx)
4366{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004367 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004368 char *rule = vrule;
4369
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004370 if (unlikely(!rule)) {
4371 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004372 return -ENOENT;
4373 }
4374
4375 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4376 return 0;
4377
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004378 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004379
4380 /*
4381 * No need to do string comparisons. If a match occurs,
4382 * both pointers will point to the same smack_known
4383 * label.
4384 */
Al Viro5af75d82008-12-16 05:59:26 -05004385 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004386 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004387 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004388 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004389
4390 return 0;
4391}
4392
Casey Schaufler491a0b02016-01-26 15:08:35 -08004393/*
4394 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004395 * No memory was allocated.
4396 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004397
4398#endif /* CONFIG_AUDIT */
4399
Randy Dunlap251a2a92009-02-18 11:42:33 -08004400/**
David Quigley746df9b2013-05-22 12:50:35 -04004401 * smack_ismaclabel - check if xattr @name references a smack MAC label
4402 * @name: Full xattr name to check.
4403 */
4404static int smack_ismaclabel(const char *name)
4405{
4406 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4407}
4408
4409
4410/**
Casey Schauflere114e472008-02-04 22:29:50 -08004411 * smack_secid_to_secctx - return the smack label for a secid
4412 * @secid: incoming integer
4413 * @secdata: destination
4414 * @seclen: how long it is
4415 *
4416 * Exists for networking code.
4417 */
4418static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4419{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004420 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004421
Eric Parisd5630b92010-10-13 16:24:48 -04004422 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004423 *secdata = skp->smk_known;
4424 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004425 return 0;
4426}
4427
Randy Dunlap251a2a92009-02-18 11:42:33 -08004428/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004429 * smack_secctx_to_secid - return the secid for a smack label
4430 * @secdata: smack label
4431 * @seclen: how long result is
4432 * @secid: outgoing integer
4433 *
4434 * Exists for audit and networking code.
4435 */
David Howellse52c17642008-04-29 20:52:51 +01004436static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004437{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004438 struct smack_known *skp = smk_find_entry(secdata);
4439
4440 if (skp)
4441 *secid = skp->smk_secid;
4442 else
4443 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004444 return 0;
4445}
4446
Casey Schaufler491a0b02016-01-26 15:08:35 -08004447/*
4448 * There used to be a smack_release_secctx hook
4449 * that did nothing back when hooks were in a vector.
4450 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004451 */
Casey Schauflere114e472008-02-04 22:29:50 -08004452
David P. Quigley1ee65e32009-09-03 14:25:57 -04004453static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4454{
4455 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4456}
4457
4458static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4459{
4460 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4461}
4462
4463static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4464{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004465 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004466
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004467 *ctx = skp->smk_known;
4468 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004469 return 0;
4470}
4471
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004472static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4473{
4474
4475 struct task_smack *tsp;
4476 struct smack_known *skp;
4477 struct inode_smack *isp;
4478 struct cred *new_creds = *new;
4479
4480 if (new_creds == NULL) {
4481 new_creds = prepare_creds();
4482 if (new_creds == NULL)
4483 return -ENOMEM;
4484 }
4485
Casey Schauflerb17103a2018-11-09 16:12:56 -08004486 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004487
4488 /*
4489 * Get label from overlay inode and set it in create_sid
4490 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004491 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004492 skp = isp->smk_inode;
4493 tsp->smk_task = skp;
4494 *new = new_creds;
4495 return 0;
4496}
4497
4498static int smack_inode_copy_up_xattr(const char *name)
4499{
4500 /*
4501 * Return 1 if this is the smack access Smack attribute.
4502 */
4503 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4504 return 1;
4505
4506 return -EOPNOTSUPP;
4507}
4508
4509static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4510 struct qstr *name,
4511 const struct cred *old,
4512 struct cred *new)
4513{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004514 struct task_smack *otsp = smack_cred(old);
4515 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004516 struct inode_smack *isp;
4517 int may;
4518
4519 /*
4520 * Use the process credential unless all of
4521 * the transmuting criteria are met
4522 */
4523 ntsp->smk_task = otsp->smk_task;
4524
4525 /*
4526 * the attribute of the containing directory
4527 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004528 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004529
4530 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4531 rcu_read_lock();
4532 may = smk_access_entry(otsp->smk_task->smk_known,
4533 isp->smk_inode->smk_known,
4534 &otsp->smk_task->smk_rules);
4535 rcu_read_unlock();
4536
4537 /*
4538 * If the directory is transmuting and the rule
4539 * providing access is transmuting use the containing
4540 * directory label instead of the process label.
4541 */
4542 if (may > 0 && (may & MAY_TRANSMUTE))
4543 ntsp->smk_task = isp->smk_inode;
4544 }
4545 return 0;
4546}
4547
Casey Schauflerbbd36622018-11-12 09:30:56 -08004548struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4549 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004550 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004551};
4552
James Morrisca97d932017-02-15 00:18:51 +11004553static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004554 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4555 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4556 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004557
Casey Schauflere20b0432015-05-02 15:11:36 -07004558 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4559 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004560 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004561 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004562 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304563 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004564
Casey Schauflere20b0432015-05-02 15:11:36 -07004565 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004566
Casey Schauflere20b0432015-05-02 15:11:36 -07004567 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
4568 LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
4569 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4570 LSM_HOOK_INIT(inode_link, smack_inode_link),
4571 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4572 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4573 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4574 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4575 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4576 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4577 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4578 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4579 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4580 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4581 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4582 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4583 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4584 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004585
Casey Schauflere20b0432015-05-02 15:11:36 -07004586 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004587 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4588 LSM_HOOK_INIT(file_lock, smack_file_lock),
4589 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4590 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4591 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4592 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4593 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4594 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004595
Casey Schauflere20b0432015-05-02 15:11:36 -07004596 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004597
Casey Schauflere20b0432015-05-02 15:11:36 -07004598 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4599 LSM_HOOK_INIT(cred_free, smack_cred_free),
4600 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4601 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004602 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004603 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4604 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4605 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4606 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4607 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4608 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4609 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4610 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4611 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4612 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4613 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4614 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4615 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004616 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004617
Casey Schauflere20b0432015-05-02 15:11:36 -07004618 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4619 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004620
Casey Schauflere20b0432015-05-02 15:11:36 -07004621 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
4622 LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004623
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004624 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
4625 LSM_HOOK_INIT(msg_queue_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004626 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4627 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4628 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4629 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004630
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004631 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
4632 LSM_HOOK_INIT(shm_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004633 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4634 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4635 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004636
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004637 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
4638 LSM_HOOK_INIT(sem_free_security, smack_ipc_free_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004639 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4640 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4641 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004642
Casey Schauflere20b0432015-05-02 15:11:36 -07004643 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004644
Casey Schauflere20b0432015-05-02 15:11:36 -07004645 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4646 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004647
Casey Schauflere20b0432015-05-02 15:11:36 -07004648 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4649 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004650
Casey Schauflere20b0432015-05-02 15:11:36 -07004651 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004652 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004653#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004654 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004655#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004656 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4657 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4658 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4659 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4660 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4661 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4662 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4663 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4664 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4665 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004666
Casey Schauflere114e472008-02-04 22:29:50 -08004667 /* key management security hooks */
4668#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004669 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4670 LSM_HOOK_INIT(key_free, smack_key_free),
4671 LSM_HOOK_INIT(key_permission, smack_key_permission),
4672 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004673#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004674
4675 /* Audit hooks */
4676#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004677 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4678 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4679 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004680#endif /* CONFIG_AUDIT */
4681
Casey Schauflere20b0432015-05-02 15:11:36 -07004682 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4683 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4684 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004685 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4686 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4687 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004688 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4689 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4690 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004691};
4692
Etienne Basset7198e2e2009-03-24 20:53:24 +01004693
Casey Schaufler86812bb2012-04-17 18:55:46 -07004694static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004695{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004696 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004697 * Initialize rule list locks
4698 */
4699 mutex_init(&smack_known_huh.smk_rules_lock);
4700 mutex_init(&smack_known_hat.smk_rules_lock);
4701 mutex_init(&smack_known_floor.smk_rules_lock);
4702 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004703 mutex_init(&smack_known_web.smk_rules_lock);
4704 /*
4705 * Initialize rule lists
4706 */
4707 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4708 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4709 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4710 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004711 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4712 /*
4713 * Create the known labels list
4714 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004715 smk_insert_entry(&smack_known_huh);
4716 smk_insert_entry(&smack_known_hat);
4717 smk_insert_entry(&smack_known_star);
4718 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004719 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004720}
4721
Casey Schauflere114e472008-02-04 22:29:50 -08004722/**
4723 * smack_init - initialize the smack system
4724 *
4725 * Returns 0
4726 */
4727static __init int smack_init(void)
4728{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004729 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004730 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004731
Rohit1a5b4722014-10-15 17:40:41 +05304732 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4733 if (!smack_inode_cache)
4734 return -ENOMEM;
4735
Casey Schauflerbbd36622018-11-12 09:30:56 -08004736 lsm_early_cred(cred);
Casey Schaufler676dac42010-12-02 06:43:39 -08004737
Casey Schauflerbbd36622018-11-12 09:30:56 -08004738 /*
4739 * Set the security state for the initial task.
4740 */
4741 tsp = smack_cred(cred);
4742 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4743
4744 /*
4745 * Register with LSM
4746 */
4747 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004748 smack_enabled = 1;
4749
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004750 pr_info("Smack: Initializing.\n");
4751#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4752 pr_info("Smack: Netfilter enabled.\n");
4753#endif
4754#ifdef SMACK_IPV6_PORT_LABELING
4755 pr_info("Smack: IPv6 port labeling enabled.\n");
4756#endif
4757#ifdef SMACK_IPV6_SECMARK_LABELING
4758 pr_info("Smack: IPv6 Netfilter enabled.\n");
4759#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004760
Casey Schaufler86812bb2012-04-17 18:55:46 -07004761 /* initialize the smack_known_list */
4762 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004763
Casey Schauflere114e472008-02-04 22:29:50 -08004764 return 0;
4765}
4766
4767/*
4768 * Smack requires early initialization in order to label
4769 * all processes and objects when they are created.
4770 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004771DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004772 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004773 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004774 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004775 .init = smack_init,
4776};