blob: 8bc7b04769a878e1b53989372012be3b31e74427 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Casey Schauflere114e472008-02-04 22:29:50 -08002/*
3 * Simplified MAC Kernel (smack) security module
4 *
5 * This file contains the smack hook function implementations.
6 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02007 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08008 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03009 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -080010 *
11 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040012 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000013 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020014 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030015 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080016 */
17
18#include <linux/xattr.h>
19#include <linux/pagemap.h>
20#include <linux/mount.h>
21#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080022#include <linux/kd.h>
23#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040024#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/tcp.h>
26#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070027#include <linux/dccp.h>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020028#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080030#include <linux/mutex.h>
Casey Schauflere114e472008-02-04 22:29:50 -080031#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070032#include <net/ip.h>
33#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100034#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070035#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050036#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030037#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000038#include <linux/msg.h>
39#include <linux/shm.h>
40#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053041#include <linux/parser.h>
David Howells2febd252018-11-01 23:07:24 +000042#include <linux/fs_context.h>
43#include <linux/fs_parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080044#include "smack.h"
45
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020046#define TRANS_TRUE "TRUE"
47#define TRANS_TRUE_SIZE 4
48
Casey Schauflerc6739442013-05-22 18:42:56 -070049#define SMK_CONNECTING 0
50#define SMK_RECEIVING 1
51#define SMK_SENDING 2
52
Casey Schaufler21abb1e2015-07-22 14:25:31 -070053#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053054DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080055static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070056#endif
Rohit1a5b4722014-10-15 17:40:41 +053057static struct kmem_cache *smack_inode_cache;
Casey Schaufler4e328b02019-04-02 11:37:12 -070058struct kmem_cache *smack_rule_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080059int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070060
Al Viroc3300aa2018-12-16 01:52:24 -050061#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
62static struct {
63 const char *name;
64 int len;
65 int opt;
66} smk_mount_opts[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +010067 {"smackfsdef", sizeof("smackfsdef") - 1, Opt_fsdefault},
Al Viroc3300aa2018-12-16 01:52:24 -050068 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
Eric Biggerse5bfad32019-08-21 22:54:41 -0700290 buffer = kzalloc(SMK_LONGLABEL, GFP_NOFS);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700291 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/**
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700308 * init_inode_smack - initialize an inode security blob
luanshia1a07f22019-07-05 10:35:20 +0800309 * @inode: inode to extract the info from
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200310 * @skp: a pointer to the Smack label entry to use in the blob
Casey Schauflere114e472008-02-04 22:29:50 -0800311 *
Casey Schauflere114e472008-02-04 22:29:50 -0800312 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700313static void init_inode_smack(struct inode *inode, struct smack_known *skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800314{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700315 struct inode_smack *isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -0800316
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200317 isp->smk_inode = skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800318 isp->smk_flags = 0;
319 mutex_init(&isp->smk_lock);
Casey Schauflere114e472008-02-04 22:29:50 -0800320}
321
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800322/**
Casey Schauflerbbd36622018-11-12 09:30:56 -0800323 * init_task_smack - initialize a task security blob
324 * @tsp: blob to initialize
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100325 * @task: a pointer to the Smack label for the running task
326 * @forked: a pointer to the Smack label for the forked task
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800327 *
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800328 */
Casey Schauflerbbd36622018-11-12 09:30:56 -0800329static void init_task_smack(struct task_smack *tsp, struct smack_known *task,
330 struct smack_known *forked)
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800331{
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800332 tsp->smk_task = task;
333 tsp->smk_forked = forked;
334 INIT_LIST_HEAD(&tsp->smk_rules);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200335 INIT_LIST_HEAD(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800336 mutex_init(&tsp->smk_rules_lock);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800337}
338
339/**
340 * smk_copy_rules - copy a rule set
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +0100341 * @nhead: new rules header pointer
342 * @ohead: old rules header pointer
343 * @gfp: type of the memory for the allocation
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800344 *
345 * Returns 0 on success, -ENOMEM on error
346 */
347static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
348 gfp_t gfp)
349{
350 struct smack_rule *nrp;
351 struct smack_rule *orp;
352 int rc = 0;
353
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800354 list_for_each_entry_rcu(orp, ohead, list) {
Casey Schaufler4e328b02019-04-02 11:37:12 -0700355 nrp = kmem_cache_zalloc(smack_rule_cache, gfp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800356 if (nrp == NULL) {
357 rc = -ENOMEM;
358 break;
359 }
360 *nrp = *orp;
361 list_add_rcu(&nrp->list, nhead);
362 }
363 return rc;
364}
365
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100366/**
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200367 * smk_copy_relabel - copy smk_relabel labels list
368 * @nhead: new rules header pointer
369 * @ohead: old rules header pointer
370 * @gfp: type of the memory for the allocation
371 *
372 * Returns 0 on success, -ENOMEM on error
373 */
374static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
375 gfp_t gfp)
376{
377 struct smack_known_list_elem *nklep;
378 struct smack_known_list_elem *oklep;
379
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200380 list_for_each_entry(oklep, ohead, list) {
381 nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
382 if (nklep == NULL) {
383 smk_destroy_label_list(nhead);
384 return -ENOMEM;
385 }
386 nklep->smk_label = oklep->smk_label;
387 list_add(&nklep->list, nhead);
388 }
389
390 return 0;
391}
392
393/**
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100394 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
395 * @mode - input mode in form of PTRACE_MODE_*
396 *
397 * Returns a converted MAY_* mode usable by smack rules
398 */
399static inline unsigned int smk_ptrace_mode(unsigned int mode)
400{
Jann Horn3dfb7d82016-01-20 15:00:01 -0800401 if (mode & PTRACE_MODE_ATTACH)
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100402 return MAY_READWRITE;
Jann Horn3dfb7d82016-01-20 15:00:01 -0800403 if (mode & PTRACE_MODE_READ)
404 return MAY_READ;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100405
406 return 0;
407}
408
409/**
410 * smk_ptrace_rule_check - helper for ptrace access
411 * @tracer: tracer process
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200412 * @tracee_known: label entry of the process that's about to be traced
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100413 * @mode: ptrace attachment mode (PTRACE_MODE_*)
414 * @func: name of the function that called us, used for audit
415 *
416 * Returns 0 on access granted, -error on error
417 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200418static int smk_ptrace_rule_check(struct task_struct *tracer,
419 struct smack_known *tracee_known,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100420 unsigned int mode, const char *func)
421{
422 int rc;
423 struct smk_audit_info ad, *saip = NULL;
424 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200425 struct smack_known *tracer_known;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700426 const struct cred *tracercred;
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100427
428 if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
429 smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
430 smk_ad_setfield_u_tsk(&ad, tracer);
431 saip = &ad;
432 }
433
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300434 rcu_read_lock();
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700435 tracercred = __task_cred(tracer);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800436 tsp = smack_cred(tracercred);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200437 tracer_known = smk_of_task(tsp);
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100438
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100439 if ((mode & PTRACE_MODE_ATTACH) &&
440 (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
441 smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200442 if (tracer_known->smk_known == tracee_known->smk_known)
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100443 rc = 0;
444 else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
445 rc = -EACCES;
Casey Schauflerdcb569c2018-09-18 16:09:16 -0700446 else if (smack_privileged_cred(CAP_SYS_PTRACE, tracercred))
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100447 rc = 0;
448 else
449 rc = -EACCES;
450
451 if (saip)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200452 smack_log(tracer_known->smk_known,
453 tracee_known->smk_known,
454 0, rc, saip);
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100455
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300456 rcu_read_unlock();
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100457 return rc;
458 }
459
460 /* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200461 rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300462
463 rcu_read_unlock();
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100464 return rc;
465}
466
Casey Schauflere114e472008-02-04 22:29:50 -0800467/*
468 * LSM hooks.
469 * We he, that is fun!
470 */
471
472/**
Ingo Molnar9e488582009-05-07 19:26:19 +1000473 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
Casey Schauflere114e472008-02-04 22:29:50 -0800474 * @ctp: child task pointer
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100475 * @mode: ptrace attachment mode (PTRACE_MODE_*)
Casey Schauflere114e472008-02-04 22:29:50 -0800476 *
477 * Returns 0 if access is OK, an error code otherwise
478 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100479 * Do the capability checks.
Casey Schauflere114e472008-02-04 22:29:50 -0800480 */
Ingo Molnar9e488582009-05-07 19:26:19 +1000481static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
Casey Schauflere114e472008-02-04 22:29:50 -0800482{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700483 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -0800484
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300485 skp = smk_of_task_struct(ctp);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200486
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -0700487 return smk_ptrace_rule_check(current, skp, mode, __func__);
David Howells5cd9c582008-08-14 11:37:28 +0100488}
Casey Schauflere114e472008-02-04 22:29:50 -0800489
David Howells5cd9c582008-08-14 11:37:28 +0100490/**
491 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
492 * @ptp: parent task pointer
493 *
494 * Returns 0 if access is OK, an error code otherwise
495 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100496 * Do the capability checks, and require PTRACE_MODE_ATTACH.
David Howells5cd9c582008-08-14 11:37:28 +0100497 */
498static int smack_ptrace_traceme(struct task_struct *ptp)
499{
500 int rc;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700501 struct smack_known *skp;
David Howells5cd9c582008-08-14 11:37:28 +0100502
Casey Schauflerb17103a2018-11-09 16:12:56 -0800503 skp = smk_of_task(smack_cred(current_cred()));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200504
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200505 rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -0800506 return rc;
507}
508
509/**
510 * smack_syslog - Smack approval on syslog
luanshia1a07f22019-07-05 10:35:20 +0800511 * @typefrom_file: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800512 *
Casey Schauflere114e472008-02-04 22:29:50 -0800513 * Returns 0 on success, error code otherwise.
514 */
Eric Paris12b30522010-11-15 18:36:29 -0500515static int smack_syslog(int typefrom_file)
Casey Schauflere114e472008-02-04 22:29:50 -0800516{
Eric Paris12b30522010-11-15 18:36:29 -0500517 int rc = 0;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700518 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -0800519
Casey Schaufler1880eff2012-06-05 15:28:30 -0700520 if (smack_privileged(CAP_MAC_OVERRIDE))
Casey Schauflere114e472008-02-04 22:29:50 -0800521 return 0;
522
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800523 if (smack_syslog_label != NULL && smack_syslog_label != skp)
Casey Schauflere114e472008-02-04 22:29:50 -0800524 rc = -EACCES;
525
526 return rc;
527}
528
Casey Schauflere114e472008-02-04 22:29:50 -0800529/*
530 * Superblock Hooks.
531 */
532
533/**
534 * smack_sb_alloc_security - allocate a superblock blob
535 * @sb: the superblock getting the blob
536 *
537 * Returns 0 on success or -ENOMEM on error.
538 */
539static int smack_sb_alloc_security(struct super_block *sb)
540{
541 struct superblock_smack *sbsp;
542
543 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
544
545 if (sbsp == NULL)
546 return -ENOMEM;
547
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200548 sbsp->smk_root = &smack_known_floor;
549 sbsp->smk_default = &smack_known_floor;
550 sbsp->smk_floor = &smack_known_floor;
551 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700552 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500553 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700554 */
Casey Schauflere114e472008-02-04 22:29:50 -0800555 sb->s_security = sbsp;
556
557 return 0;
558}
559
560/**
561 * smack_sb_free_security - free a superblock blob
562 * @sb: the superblock getting the blob
563 *
564 */
565static void smack_sb_free_security(struct super_block *sb)
566{
567 kfree(sb->s_security);
568 sb->s_security = NULL;
569}
570
Al Viro12085b12018-12-13 15:18:05 -0500571struct smack_mnt_opts {
572 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
573};
574
Al Viro204cc0c2018-12-13 13:41:47 -0500575static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800576{
Al Viro12085b12018-12-13 15:18:05 -0500577 struct smack_mnt_opts *opts = mnt_opts;
578 kfree(opts->fsdefault);
579 kfree(opts->fsfloor);
580 kfree(opts->fshat);
581 kfree(opts->fsroot);
582 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500583 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800584}
585
Al Viro55c0e5b2018-12-16 01:09:45 -0500586static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530587{
Al Viro55c0e5b2018-12-16 01:09:45 -0500588 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530589
Al Viro55c0e5b2018-12-16 01:09:45 -0500590 if (!opts) {
591 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
592 if (!opts)
593 return -ENOMEM;
594 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530595 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500596 if (!s)
597 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530598
Al Viro55c0e5b2018-12-16 01:09:45 -0500599 switch (token) {
600 case Opt_fsdefault:
601 if (opts->fsdefault)
602 goto out_opt_err;
603 opts->fsdefault = s;
604 break;
605 case Opt_fsfloor:
606 if (opts->fsfloor)
607 goto out_opt_err;
608 opts->fsfloor = s;
609 break;
610 case Opt_fshat:
611 if (opts->fshat)
612 goto out_opt_err;
613 opts->fshat = s;
614 break;
615 case Opt_fsroot:
616 if (opts->fsroot)
617 goto out_opt_err;
618 opts->fsroot = s;
619 break;
620 case Opt_fstransmute:
621 if (opts->fstransmute)
622 goto out_opt_err;
623 opts->fstransmute = s;
624 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530625 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530626 return 0;
627
628out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530629 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500630 return -EINVAL;
631}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530632
Al Viro0b520752018-12-23 16:02:47 -0500633/**
634 * smack_fs_context_dup - Duplicate the security data on fs_context duplication
635 * @fc: The new filesystem context.
636 * @src_fc: The source filesystem context being duplicated.
637 *
638 * Returns 0 on success or -ENOMEM on error.
639 */
640static int smack_fs_context_dup(struct fs_context *fc,
641 struct fs_context *src_fc)
642{
643 struct smack_mnt_opts *dst, *src = src_fc->security;
644
645 if (!src)
646 return 0;
647
648 fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
649 if (!fc->security)
650 return -ENOMEM;
651 dst = fc->security;
652
653 if (src->fsdefault) {
654 dst->fsdefault = kstrdup(src->fsdefault, GFP_KERNEL);
655 if (!dst->fsdefault)
656 return -ENOMEM;
657 }
658 if (src->fsfloor) {
659 dst->fsfloor = kstrdup(src->fsfloor, GFP_KERNEL);
660 if (!dst->fsfloor)
661 return -ENOMEM;
662 }
663 if (src->fshat) {
664 dst->fshat = kstrdup(src->fshat, GFP_KERNEL);
665 if (!dst->fshat)
666 return -ENOMEM;
667 }
668 if (src->fsroot) {
669 dst->fsroot = kstrdup(src->fsroot, GFP_KERNEL);
670 if (!dst->fsroot)
671 return -ENOMEM;
672 }
673 if (src->fstransmute) {
674 dst->fstransmute = kstrdup(src->fstransmute, GFP_KERNEL);
675 if (!dst->fstransmute)
676 return -ENOMEM;
677 }
678 return 0;
679}
680
David Howells2febd252018-11-01 23:07:24 +0000681static const struct fs_parameter_spec smack_param_specs[] = {
Casey Schaufler6e7739f2019-05-31 11:53:33 +0100682 fsparam_string("smackfsdef", Opt_fsdefault),
683 fsparam_string("smackfsdefault", Opt_fsdefault),
684 fsparam_string("smackfsfloor", Opt_fsfloor),
685 fsparam_string("smackfshat", Opt_fshat),
686 fsparam_string("smackfsroot", Opt_fsroot),
687 fsparam_string("smackfstransmute", Opt_fstransmute),
David Howells2febd252018-11-01 23:07:24 +0000688 {}
689};
690
691static const struct fs_parameter_description smack_fs_parameters = {
692 .name = "smack",
693 .specs = smack_param_specs,
694};
695
696/**
697 * smack_fs_context_parse_param - Parse a single mount parameter
698 * @fc: The new filesystem context being constructed.
699 * @param: The parameter.
700 *
701 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
702 * error.
703 */
704static int smack_fs_context_parse_param(struct fs_context *fc,
705 struct fs_parameter *param)
706{
707 struct fs_parse_result result;
708 int opt, rc;
709
710 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
711 if (opt < 0)
712 return opt;
713
714 rc = smack_add_opt(opt, param->string, &fc->security);
715 if (!rc)
716 param->string = NULL;
717 return rc;
718}
719
Al Virod2497e12018-12-16 01:37:06 -0500720static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530721{
Al Virod2497e12018-12-16 01:37:06 -0500722 char *from = options, *to = options;
723 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530724
Al Viroc3300aa2018-12-16 01:52:24 -0500725 while (1) {
726 char *next = strchr(from, ',');
727 int token, len, rc;
728 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530729
Al Viroc3300aa2018-12-16 01:52:24 -0500730 if (next)
731 len = next - from;
732 else
733 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530734
Al Viroc3300aa2018-12-16 01:52:24 -0500735 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500736 if (token != Opt_error) {
737 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
738 rc = smack_add_opt(token, arg, mnt_opts);
739 if (unlikely(rc)) {
740 kfree(arg);
741 if (*mnt_opts)
742 smack_free_mnt_opts(*mnt_opts);
743 *mnt_opts = NULL;
744 return rc;
745 }
746 } else {
747 if (!first) { // copy with preceding comma
748 from--;
749 len++;
750 }
751 if (to != from)
752 memmove(to, from, len);
753 to += len;
754 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530755 }
Al Viroc3300aa2018-12-16 01:52:24 -0500756 if (!from[len])
757 break;
758 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530759 }
Al Virod2497e12018-12-16 01:37:06 -0500760 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530761 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530762}
763
764/**
765 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800766 * @sb: the file system superblock
luanshia1a07f22019-07-05 10:35:20 +0800767 * @mnt_opts: Smack mount options
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530768 * @kern_flags: mount option from kernel space or user space
769 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800770 *
771 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530772 *
773 * Allow filesystems with binary mount data to explicitly set Smack mount
774 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800775 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530776static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500777 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530778 unsigned long kern_flags,
779 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800780{
781 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000782 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800783 struct superblock_smack *sp = sb->s_security;
784 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800785 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500786 struct smack_mnt_opts *opts = mnt_opts;
787 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800788
Seth Forshee9f50eda2015-09-23 15:16:06 -0500789 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800790 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700791
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700792 if (inode->i_security == NULL) {
793 int rc = lsm_inode_alloc(inode);
794
795 if (rc)
796 return rc;
797 }
798
Himanshu Shukla2097f592016-11-10 16:19:52 +0530799 if (!smack_privileged(CAP_MAC_ADMIN)) {
800 /*
801 * Unprivileged mounts don't get to specify Smack values.
802 */
Al Viro12085b12018-12-13 15:18:05 -0500803 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530804 return -EPERM;
805 /*
806 * Unprivileged mounts get root and default from the caller.
807 */
808 skp = smk_of_current();
809 sp->smk_root = skp;
810 sp->smk_default = skp;
811 /*
812 * For a handful of fs types with no user-controlled
813 * backing store it's okay to trust security labels
814 * in the filesystem. The rest are untrusted.
815 */
816 if (sb->s_user_ns != &init_user_ns &&
817 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
818 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500819 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530820 sp->smk_flags |= SMK_SB_UNTRUSTED;
821 }
822 }
823
Seth Forshee9f50eda2015-09-23 15:16:06 -0500824 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800825
Al Viro12085b12018-12-13 15:18:05 -0500826 if (opts) {
827 if (opts->fsdefault) {
828 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200829 if (IS_ERR(skp))
830 return PTR_ERR(skp);
831 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500832 }
833 if (opts->fsfloor) {
834 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530835 if (IS_ERR(skp))
836 return PTR_ERR(skp);
837 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500838 }
839 if (opts->fshat) {
840 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530841 if (IS_ERR(skp))
842 return PTR_ERR(skp);
843 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500844 }
845 if (opts->fsroot) {
846 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200847 if (IS_ERR(skp))
848 return PTR_ERR(skp);
849 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500850 }
851 if (opts->fstransmute) {
852 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200853 if (IS_ERR(skp))
854 return PTR_ERR(skp);
855 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500856 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800857 }
858 }
859
860 /*
861 * Initialize the root inode.
862 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700863 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800864
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700865 if (transmute) {
866 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700867 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700868 }
Casey Schauflere830b392013-05-22 18:43:07 -0700869
Casey Schauflere114e472008-02-04 22:29:50 -0800870 return 0;
871}
872
873/**
874 * smack_sb_statfs - Smack check on statfs
875 * @dentry: identifies the file system in question
876 *
877 * Returns 0 if current can read the floor of the filesystem,
878 * and error code otherwise
879 */
880static int smack_sb_statfs(struct dentry *dentry)
881{
882 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200883 int rc;
884 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800885
Eric Parisa2694342011-04-25 13:10:27 -0400886 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200887 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
888
889 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700890 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200891 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800892}
893
Casey Schauflere114e472008-02-04 22:29:50 -0800894/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800895 * BPRM hooks
896 */
897
Casey Schauflerce8a4322011-09-29 18:21:01 -0700898/**
899 * smack_bprm_set_creds - set creds for exec
900 * @bprm: the exec information
901 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100902 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700903 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800904static int smack_bprm_set_creds(struct linux_binprm *bprm)
905{
Al Viro496ad9a2013-01-23 17:07:38 -0500906 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800907 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800908 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500909 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800910 int rc;
911
Kees Cookddb4a142017-07-18 15:25:23 -0700912 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800913 return 0;
914
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800915 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300916 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800917 return 0;
918
Seth Forshee809c02e2016-04-26 14:36:22 -0500919 sbsp = inode->i_sb->s_security;
920 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
921 isp->smk_task != sbsp->smk_root)
922 return 0;
923
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300924 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100925 struct task_struct *tracer;
926 rc = 0;
927
928 rcu_read_lock();
929 tracer = ptrace_parent(current);
930 if (likely(tracer != NULL))
931 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200932 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100933 PTRACE_MODE_ATTACH,
934 __func__);
935 rcu_read_unlock();
936
937 if (rc != 0)
938 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200939 }
940 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300941 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800942
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300943 bsp->smk_task = isp->smk_task;
944 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800945
Kees Cookccbb6e12017-07-18 15:25:26 -0700946 /* Decide if this is a secure exec. */
947 if (bsp->smk_task != bsp->smk_forked)
948 bprm->secureexec = 1;
949
Casey Schaufler676dac42010-12-02 06:43:39 -0800950 return 0;
951}
952
953/*
Casey Schauflere114e472008-02-04 22:29:50 -0800954 * Inode hooks
955 */
956
957/**
958 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800959 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800960 *
luanshia1a07f22019-07-05 10:35:20 +0800961 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800962 */
963static int smack_inode_alloc_security(struct inode *inode)
964{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700965 struct smack_known *skp = smk_of_current();
966
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700967 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800968 return 0;
969}
970
971/**
Casey Schauflere114e472008-02-04 22:29:50 -0800972 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200973 * @inode: the newly created inode
974 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500975 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800976 * @name: where to put the attribute name
977 * @value: where to put the attribute value
978 * @len: where to put the length of the attribute
979 *
980 * Returns 0 if it all works out, -ENOMEM if there's no memory
981 */
982static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900983 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500984 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800985{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800986 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700987 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200988 struct smack_known *isp = smk_of_inode(inode);
989 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800990 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800991
Tetsuo Handa95489062013-07-25 05:44:02 +0900992 if (name)
993 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800994
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100995 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800996 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200997 may = smk_access_entry(skp->smk_known, dsp->smk_known,
998 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800999 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001000
1001 /*
1002 * If the access rule allows transmutation and
1003 * the directory requests transmutation then
1004 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001005 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001006 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001007 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001008 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001009 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001010 issp->smk_flags |= SMK_INODE_CHANGED;
1011 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001012
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001013 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001014 if (*value == NULL)
1015 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001016
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001017 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001018 }
Casey Schauflere114e472008-02-04 22:29:50 -08001019
1020 return 0;
1021}
1022
1023/**
1024 * smack_inode_link - Smack check on link
1025 * @old_dentry: the existing object
1026 * @dir: unused
1027 * @new_dentry: the new object
1028 *
1029 * Returns 0 if access is permitted, an error code otherwise
1030 */
1031static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1032 struct dentry *new_dentry)
1033{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001034 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001035 struct smk_audit_info ad;
1036 int rc;
1037
Eric Parisa2694342011-04-25 13:10:27 -04001038 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001039 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001040
David Howellsc6f493d2015-03-17 22:26:22 +00001041 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001043 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001044
David Howells88025652015-01-29 12:02:32 +00001045 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001046 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001047 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1048 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001049 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001050 }
1051
1052 return rc;
1053}
1054
1055/**
1056 * smack_inode_unlink - Smack check on inode deletion
1057 * @dir: containing directory object
1058 * @dentry: file to unlink
1059 *
1060 * Returns 0 if current can write the containing directory
1061 * and the object, error code otherwise
1062 */
1063static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1064{
David Howellsc6f493d2015-03-17 22:26:22 +00001065 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001066 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001067 int rc;
1068
Eric Parisa2694342011-04-25 13:10:27 -04001069 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001070 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1071
Casey Schauflere114e472008-02-04 22:29:50 -08001072 /*
1073 * You need write access to the thing you're unlinking
1074 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001075 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001076 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001077 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001078 /*
1079 * You also need write access to the containing directory
1080 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001081 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001082 smk_ad_setfield_u_fs_inode(&ad, dir);
1083 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001084 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 }
Casey Schauflere114e472008-02-04 22:29:50 -08001086 return rc;
1087}
1088
1089/**
1090 * smack_inode_rmdir - Smack check on directory deletion
1091 * @dir: containing directory object
1092 * @dentry: directory to unlink
1093 *
1094 * Returns 0 if current can write the containing directory
1095 * and the directory, error code otherwise
1096 */
1097static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1098{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001099 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001100 int rc;
1101
Eric Parisa2694342011-04-25 13:10:27 -04001102 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001103 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1104
Casey Schauflere114e472008-02-04 22:29:50 -08001105 /*
1106 * You need write access to the thing you're removing
1107 */
David Howellsc6f493d2015-03-17 22:26:22 +00001108 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1109 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001110 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001111 /*
1112 * You also need write access to the containing directory
1113 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001114 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001115 smk_ad_setfield_u_fs_inode(&ad, dir);
1116 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001117 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001118 }
Casey Schauflere114e472008-02-04 22:29:50 -08001119
1120 return rc;
1121}
1122
1123/**
1124 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001125 * @old_inode: unused
1126 * @old_dentry: the old object
1127 * @new_inode: unused
1128 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001129 *
1130 * Read and write access is required on both the old and
1131 * new directories.
1132 *
1133 * Returns 0 if access is permitted, an error code otherwise
1134 */
1135static int smack_inode_rename(struct inode *old_inode,
1136 struct dentry *old_dentry,
1137 struct inode *new_inode,
1138 struct dentry *new_dentry)
1139{
1140 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001141 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001142 struct smk_audit_info ad;
1143
Eric Parisa2694342011-04-25 13:10:27 -04001144 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001146
David Howellsc6f493d2015-03-17 22:26:22 +00001147 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001149 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001150
David Howells88025652015-01-29 12:02:32 +00001151 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001152 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001153 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1154 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001155 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001156 }
Casey Schauflere114e472008-02-04 22:29:50 -08001157 return rc;
1158}
1159
1160/**
1161 * smack_inode_permission - Smack version of permission()
1162 * @inode: the inode in question
1163 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001164 *
1165 * This is the important Smack hook.
1166 *
luanshia1a07f22019-07-05 10:35:20 +08001167 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001168 */
Al Viroe74f71e2011-06-20 19:38:15 -04001169static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001170{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001171 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001172 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001173 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001174 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001175
1176 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001177 /*
1178 * No permission to check. Existence test. Yup, it's there.
1179 */
1180 if (mask == 0)
1181 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001182
Seth Forshee9f50eda2015-09-23 15:16:06 -05001183 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1184 if (smk_of_inode(inode) != sbsp->smk_root)
1185 return -EACCES;
1186 }
1187
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001188 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001189 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001190 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001192 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001193 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1194 rc = smk_bu_inode(inode, mask, rc);
1195 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001196}
1197
1198/**
1199 * smack_inode_setattr - Smack check for setting attributes
1200 * @dentry: the object
1201 * @iattr: for the force flag
1202 *
1203 * Returns 0 if access is permitted, an error code otherwise
1204 */
1205static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1206{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001207 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001208 int rc;
1209
Casey Schauflere114e472008-02-04 22:29:50 -08001210 /*
1211 * Need to allow for clearing the setuid bit.
1212 */
1213 if (iattr->ia_valid & ATTR_FORCE)
1214 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001215 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001216 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001217
David Howellsc6f493d2015-03-17 22:26:22 +00001218 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1219 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001220 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001221}
1222
1223/**
1224 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001225 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001226 *
1227 * Returns 0 if access is permitted, an error code otherwise
1228 */
Al Viro3f7036a2015-03-08 19:28:30 -04001229static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001230{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001231 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001232 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001233 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001234
Eric Parisf48b7392011-04-25 12:54:27 -04001235 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001236 smk_ad_setfield_u_fs_path(&ad, *path);
1237 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1238 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001239 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001240}
1241
1242/**
1243 * smack_inode_setxattr - Smack check for setting xattrs
1244 * @dentry: the object
1245 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001246 * @value: value of the attribute
1247 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001248 * @flags: unused
1249 *
1250 * This protects the Smack attribute explicitly.
1251 *
1252 * Returns 0 if access is permitted, an error code otherwise
1253 */
David Howells8f0cfa52008-04-29 00:59:41 -07001254static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1255 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001256{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001257 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001258 struct smack_known *skp;
1259 int check_priv = 0;
1260 int check_import = 0;
1261 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001262 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001263
Casey Schaufler19760ad2013-12-16 16:27:26 -08001264 /*
1265 * Check label validity here so import won't fail in post_setxattr
1266 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001267 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1268 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001269 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1270 check_priv = 1;
1271 check_import = 1;
1272 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1273 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1274 check_priv = 1;
1275 check_import = 1;
1276 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001277 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001278 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001279 if (size != TRANS_TRUE_SIZE ||
1280 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1281 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001282 } else
1283 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1284
Casey Schaufler19760ad2013-12-16 16:27:26 -08001285 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1286 rc = -EPERM;
1287
1288 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001289 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001290 if (IS_ERR(skp))
1291 rc = PTR_ERR(skp);
1292 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001293 (skp == &smack_known_star || skp == &smack_known_web)))
1294 rc = -EINVAL;
1295 }
1296
Eric Parisa2694342011-04-25 13:10:27 -04001297 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001298 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1299
Casey Schauflerd166c802014-08-27 14:51:27 -07001300 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001301 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1302 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001303 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001304
1305 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001306}
1307
1308/**
1309 * smack_inode_post_setxattr - Apply the Smack update approved above
1310 * @dentry: object
1311 * @name: attribute name
1312 * @value: attribute value
1313 * @size: attribute size
1314 * @flags: unused
1315 *
1316 * Set the pointer in the inode blob to the entry found
1317 * in the master label list.
1318 */
David Howells8f0cfa52008-04-29 00:59:41 -07001319static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1320 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001321{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001322 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001323 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001324
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001325 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1326 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1327 return;
1328 }
1329
Casey Schaufler676dac42010-12-02 06:43:39 -08001330 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001331 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001332 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001333 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001334 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001335 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001336 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001337 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001338 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001339 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001340 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001341 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001342 }
Casey Schauflere114e472008-02-04 22:29:50 -08001343
1344 return;
1345}
1346
Casey Schauflerce8a4322011-09-29 18:21:01 -07001347/**
Casey Schauflere114e472008-02-04 22:29:50 -08001348 * smack_inode_getxattr - Smack check on getxattr
1349 * @dentry: the object
1350 * @name: unused
1351 *
1352 * Returns 0 if access is permitted, an error code otherwise
1353 */
David Howells8f0cfa52008-04-29 00:59:41 -07001354static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001355{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001356 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001357 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001358
Eric Parisa2694342011-04-25 13:10:27 -04001359 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001360 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1361
David Howellsc6f493d2015-03-17 22:26:22 +00001362 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1363 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001364 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001365}
1366
Casey Schauflerce8a4322011-09-29 18:21:01 -07001367/**
Casey Schauflere114e472008-02-04 22:29:50 -08001368 * smack_inode_removexattr - Smack check on removexattr
1369 * @dentry: the object
1370 * @name: name of the attribute
1371 *
1372 * Removing the Smack attribute requires CAP_MAC_ADMIN
1373 *
1374 * Returns 0 if access is permitted, an error code otherwise
1375 */
David Howells8f0cfa52008-04-29 00:59:41 -07001376static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001377{
Casey Schaufler676dac42010-12-02 06:43:39 -08001378 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001379 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001380 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001381
Casey Schauflerbcdca222008-02-23 15:24:04 -08001382 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1383 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001384 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001385 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001386 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301387 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001388 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001389 rc = -EPERM;
1390 } else
1391 rc = cap_inode_removexattr(dentry, name);
1392
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001393 if (rc != 0)
1394 return rc;
1395
Eric Parisa2694342011-04-25 13:10:27 -04001396 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001397 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001398
David Howellsc6f493d2015-03-17 22:26:22 +00001399 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1400 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001401 if (rc != 0)
1402 return rc;
1403
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001404 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001405 /*
1406 * Don't do anything special for these.
1407 * XATTR_NAME_SMACKIPIN
1408 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001409 */
José Bollo80124952016-01-12 21:23:40 +01001410 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001411 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001412 struct superblock_smack *sbsp = sbp->s_security;
1413
1414 isp->smk_inode = sbsp->smk_default;
1415 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001416 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001417 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001418 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001419 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1420 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001421
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001422 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001423}
1424
1425/**
1426 * smack_inode_getsecurity - get smack xattrs
1427 * @inode: the object
1428 * @name: attribute name
1429 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001430 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001431 *
1432 * Returns the size of the attribute or an error code
1433 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001434static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001435 const char *name, void **buffer,
1436 bool alloc)
1437{
1438 struct socket_smack *ssp;
1439 struct socket *sock;
1440 struct super_block *sbp;
1441 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001442 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001443
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001444 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001445 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001446 else {
1447 /*
1448 * The rest of the Smack xattrs are only on sockets.
1449 */
1450 sbp = ip->i_sb;
1451 if (sbp->s_magic != SOCKFS_MAGIC)
1452 return -EOPNOTSUPP;
1453
1454 sock = SOCKET_I(ip);
1455 if (sock == NULL || sock->sk == NULL)
1456 return -EOPNOTSUPP;
1457
1458 ssp = sock->sk->sk_security;
1459
1460 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1461 isp = ssp->smk_in;
1462 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1463 isp = ssp->smk_out;
1464 else
1465 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001466 }
1467
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001468 if (alloc) {
1469 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1470 if (*buffer == NULL)
1471 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001472 }
1473
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001474 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001475}
1476
1477
1478/**
1479 * smack_inode_listsecurity - list the Smack attributes
1480 * @inode: the object
1481 * @buffer: where they go
1482 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001483 */
1484static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1485 size_t buffer_size)
1486{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001487 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001488
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001489 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001490 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001491
1492 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001493}
1494
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001495/**
1496 * smack_inode_getsecid - Extract inode's security id
1497 * @inode: inode to extract the info from
1498 * @secid: where result will be saved
1499 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001500static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001501{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001502 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001503
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001504 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001505}
1506
Casey Schauflere114e472008-02-04 22:29:50 -08001507/*
1508 * File Hooks
1509 */
1510
Casey Schaufler491a0b02016-01-26 15:08:35 -08001511/*
1512 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001513 *
1514 * Should access checks be done on each read or write?
1515 * UNICOS and SELinux say yes.
1516 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1517 *
1518 * I'll say no for now. Smack does not do the frequent
1519 * label changing that SELinux does.
1520 */
Casey Schauflere114e472008-02-04 22:29:50 -08001521
1522/**
1523 * smack_file_alloc_security - assign a file security blob
1524 * @file: the object
1525 *
1526 * The security blob for a file is a pointer to the master
1527 * label list, so no allocation is done.
1528 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001529 * f_security is the owner security information. It
1530 * isn't used on file access checks, it's for send_sigio.
1531 *
Casey Schauflere114e472008-02-04 22:29:50 -08001532 * Returns 0
1533 */
1534static int smack_file_alloc_security(struct file *file)
1535{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001536 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001537
Casey Schauflerf28952a2018-11-12 09:38:53 -08001538 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001539 return 0;
1540}
1541
1542/**
Casey Schauflere114e472008-02-04 22:29:50 -08001543 * smack_file_ioctl - Smack check on ioctls
1544 * @file: the object
1545 * @cmd: what to do
1546 * @arg: unused
1547 *
1548 * Relies heavily on the correct use of the ioctl command conventions.
1549 *
1550 * Returns 0 if allowed, error code otherwise
1551 */
1552static int smack_file_ioctl(struct file *file, unsigned int cmd,
1553 unsigned long arg)
1554{
1555 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001556 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001557 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001558
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001559 if (unlikely(IS_PRIVATE(inode)))
1560 return 0;
1561
Eric Parisf48b7392011-04-25 12:54:27 -04001562 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001563 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001564
Casey Schauflerd166c802014-08-27 14:51:27 -07001565 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001566 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001567 rc = smk_bu_file(file, MAY_WRITE, rc);
1568 }
Casey Schauflere114e472008-02-04 22:29:50 -08001569
Casey Schauflerd166c802014-08-27 14:51:27 -07001570 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001571 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001572 rc = smk_bu_file(file, MAY_READ, rc);
1573 }
Casey Schauflere114e472008-02-04 22:29:50 -08001574
1575 return rc;
1576}
1577
1578/**
1579 * smack_file_lock - Smack check on file locking
1580 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001581 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001582 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001583 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001584 */
1585static int smack_file_lock(struct file *file, unsigned int cmd)
1586{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001587 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001588 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001589 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001590
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001591 if (unlikely(IS_PRIVATE(inode)))
1592 return 0;
1593
Eric Paris92f42502011-04-25 13:15:55 -04001594 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1595 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001596 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001597 rc = smk_bu_file(file, MAY_LOCK, rc);
1598 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001599}
1600
1601/**
1602 * smack_file_fcntl - Smack check on fcntl
1603 * @file: the object
1604 * @cmd: what action to check
1605 * @arg: unused
1606 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001607 * Generally these operations are harmless.
1608 * File locking operations present an obvious mechanism
1609 * for passing information, so they require write access.
1610 *
Casey Schauflere114e472008-02-04 22:29:50 -08001611 * Returns 0 if current has access, error code otherwise
1612 */
1613static int smack_file_fcntl(struct file *file, unsigned int cmd,
1614 unsigned long arg)
1615{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001616 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001617 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001618 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001619
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001620 if (unlikely(IS_PRIVATE(inode)))
1621 return 0;
1622
Casey Schauflere114e472008-02-04 22:29:50 -08001623 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001624 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001625 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001626 case F_SETLK:
1627 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001628 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1629 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001630 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001631 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001632 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001633 case F_SETOWN:
1634 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001635 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1636 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001637 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001638 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001639 break;
1640 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001641 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001642 }
1643
1644 return rc;
1645}
1646
1647/**
Al Viroe5467852012-05-30 13:30:51 -04001648 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001649 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1650 * if mapping anonymous memory.
1651 * @file contains the file structure for file to map (may be NULL).
1652 * @reqprot contains the protection requested by the application.
1653 * @prot contains the protection that will be applied by the kernel.
1654 * @flags contains the operational flags.
1655 * Return 0 if permission is granted.
1656 */
Al Viroe5467852012-05-30 13:30:51 -04001657static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001658 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001659 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001660{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001661 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001662 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001663 struct smack_rule *srp;
1664 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001665 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001666 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001667 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001668 int may;
1669 int mmay;
1670 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001671 int rc;
1672
Al Viro496ad9a2013-01-23 17:07:38 -05001673 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001674 return 0;
1675
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001676 if (unlikely(IS_PRIVATE(file_inode(file))))
1677 return 0;
1678
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001679 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001680 if (isp->smk_mmap == NULL)
1681 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001682 sbsp = file_inode(file)->i_sb->s_security;
1683 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1684 isp->smk_mmap != sbsp->smk_root)
1685 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001686 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001687
Casey Schauflerb17103a2018-11-09 16:12:56 -08001688 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001689 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001690 rc = 0;
1691
1692 rcu_read_lock();
1693 /*
1694 * For each Smack rule associated with the subject
1695 * label verify that the SMACK64MMAP also has access
1696 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001697 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001698 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001699 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001700 /*
1701 * Matching labels always allows access.
1702 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001703 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001704 continue;
1705 /*
1706 * If there is a matching local rule take
1707 * that into account as well.
1708 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001709 may = smk_access_entry(srp->smk_subject->smk_known,
1710 okp->smk_known,
1711 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001712 if (may == -ENOENT)
1713 may = srp->smk_access;
1714 else
1715 may &= srp->smk_access;
1716 /*
1717 * If may is zero the SMACK64MMAP subject can't
1718 * possibly have less access.
1719 */
1720 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001721 continue;
1722
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001723 /*
1724 * Fetch the global list entry.
1725 * If there isn't one a SMACK64MMAP subject
1726 * can't have as much access as current.
1727 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001728 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1729 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001730 if (mmay == -ENOENT) {
1731 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001732 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001733 }
1734 /*
1735 * If there is a local entry it modifies the
1736 * potential access, too.
1737 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001738 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1739 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001740 if (tmay != -ENOENT)
1741 mmay &= tmay;
1742
1743 /*
1744 * If there is any access available to current that is
1745 * not available to a SMACK64MMAP subject
1746 * deny access.
1747 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001748 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001749 rc = -EACCES;
1750 break;
1751 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001752 }
1753
1754 rcu_read_unlock();
1755
1756 return rc;
1757}
1758
1759/**
Casey Schauflere114e472008-02-04 22:29:50 -08001760 * smack_file_set_fowner - set the file security blob value
1761 * @file: object in question
1762 *
Casey Schauflere114e472008-02-04 22:29:50 -08001763 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001764static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001765{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001766 struct smack_known **blob = smack_file(file);
1767
1768 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001769}
1770
1771/**
1772 * smack_file_send_sigiotask - Smack on sigio
1773 * @tsk: The target task
1774 * @fown: the object the signal come from
1775 * @signum: unused
1776 *
1777 * Allow a privileged task to get signals even if it shouldn't
1778 *
1779 * Returns 0 if a subject with the object's smack could
1780 * write to the task, an error code otherwise.
1781 */
1782static int smack_file_send_sigiotask(struct task_struct *tsk,
1783 struct fown_struct *fown, int signum)
1784{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001785 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001786 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001787 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001788 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001789 struct file *file;
1790 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001791 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001792
1793 /*
1794 * struct fown_struct is never outside the context of a struct file
1795 */
1796 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001797
Etienne Bassetecfcc532009-04-08 20:40:06 +02001798 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001799 blob = smack_file(file);
1800 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001801 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1802 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001803
1804 rcu_read_lock();
1805 tcred = __task_cred(tsk);
1806 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001807 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001808 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001809
1810 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1811 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001812 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001813 return rc;
1814}
1815
1816/**
1817 * smack_file_receive - Smack file receive check
1818 * @file: the object
1819 *
1820 * Returns 0 if current has access, error code otherwise
1821 */
1822static int smack_file_receive(struct file *file)
1823{
Casey Schauflerd166c802014-08-27 14:51:27 -07001824 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001825 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001826 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001827 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001828 struct socket *sock;
1829 struct task_smack *tsp;
1830 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001831
Seung-Woo Kim97775822015-04-17 15:25:04 +09001832 if (unlikely(IS_PRIVATE(inode)))
1833 return 0;
1834
Casey Schaufler4482a442013-12-30 17:37:45 -08001835 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001836 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001837
Casey Schaufler51d59af2017-05-31 08:53:42 -07001838 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001839 sock = SOCKET_I(inode);
1840 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001841 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001842 /*
1843 * If the receiving process can't write to the
1844 * passed socket or if the passed socket can't
1845 * write to the receiving process don't accept
1846 * the passed socket.
1847 */
1848 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1849 rc = smk_bu_file(file, may, rc);
1850 if (rc < 0)
1851 return rc;
1852 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1853 rc = smk_bu_file(file, may, rc);
1854 return rc;
1855 }
Casey Schauflere114e472008-02-04 22:29:50 -08001856 /*
1857 * This code relies on bitmasks.
1858 */
1859 if (file->f_mode & FMODE_READ)
1860 may = MAY_READ;
1861 if (file->f_mode & FMODE_WRITE)
1862 may |= MAY_WRITE;
1863
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001864 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001865 rc = smk_bu_file(file, may, rc);
1866 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001867}
1868
Casey Schaufler531f1d42011-09-19 12:41:42 -07001869/**
Eric Paris83d49852012-04-04 13:45:40 -04001870 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001871 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001872 *
1873 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001874 * Allow the open only if the task has read access. There are
1875 * many read operations (e.g. fstat) that you can do with an
1876 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001877 *
luanshia1a07f22019-07-05 10:35:20 +08001878 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001879 */
Al Viro94817692018-07-10 14:13:18 -04001880static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001881{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001882 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001883 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001884 struct smk_audit_info ad;
1885 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001886
Casey Schauflera6834c02014-04-21 11:10:26 -07001887 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1888 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301889 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001890 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001891
1892 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001893}
1894
Casey Schauflere114e472008-02-04 22:29:50 -08001895/*
1896 * Task hooks
1897 */
1898
1899/**
David Howellsee18d642009-09-02 09:14:21 +01001900 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001901 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001902 * @gfp: the atomicity of any memory allocations
1903 *
1904 * Prepare a blank set of credentials for modification. This must allocate all
1905 * the memory the LSM module might require such that cred_transfer() can
1906 * complete without error.
1907 */
1908static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1909{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001910 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001911 return 0;
1912}
1913
1914
1915/**
David Howellsf1752ee2008-11-14 10:39:17 +11001916 * smack_cred_free - "free" task-level security credentials
1917 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001918 *
Casey Schauflere114e472008-02-04 22:29:50 -08001919 */
David Howellsf1752ee2008-11-14 10:39:17 +11001920static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001921{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001922 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001923 struct smack_rule *rp;
1924 struct list_head *l;
1925 struct list_head *n;
1926
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001927 smk_destroy_label_list(&tsp->smk_relabel);
1928
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001929 list_for_each_safe(l, n, &tsp->smk_rules) {
1930 rp = list_entry(l, struct smack_rule, list);
1931 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001932 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001933 }
Casey Schauflere114e472008-02-04 22:29:50 -08001934}
1935
1936/**
David Howellsd84f4f92008-11-14 10:39:23 +11001937 * smack_cred_prepare - prepare new set of credentials for modification
1938 * @new: the new credentials
1939 * @old: the original credentials
1940 * @gfp: the atomicity of any memory allocations
1941 *
1942 * Prepare a new set of credentials for modification.
1943 */
1944static int smack_cred_prepare(struct cred *new, const struct cred *old,
1945 gfp_t gfp)
1946{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001947 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001948 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001949 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001950
Casey Schauflerbbd36622018-11-12 09:30:56 -08001951 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301952
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001953 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1954 if (rc != 0)
1955 return rc;
1956
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001957 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1958 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001959 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001960}
1961
Randy Dunlap251a2a92009-02-18 11:42:33 -08001962/**
David Howellsee18d642009-09-02 09:14:21 +01001963 * smack_cred_transfer - Transfer the old credentials to the new credentials
1964 * @new: the new credentials
1965 * @old: the original credentials
1966 *
1967 * Fill in a set of blank credentials from another set of credentials.
1968 */
1969static void smack_cred_transfer(struct cred *new, const struct cred *old)
1970{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001971 struct task_smack *old_tsp = smack_cred(old);
1972 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001973
1974 new_tsp->smk_task = old_tsp->smk_task;
1975 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001976 mutex_init(&new_tsp->smk_rules_lock);
1977 INIT_LIST_HEAD(&new_tsp->smk_rules);
1978
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001979 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001980}
1981
1982/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001983 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001984 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001985 * @secid: where to put the result
1986 *
1987 * Sets the secid to contain a u32 version of the smack label.
1988 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001989static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001990{
1991 struct smack_known *skp;
1992
1993 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001994 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001995 *secid = skp->smk_secid;
1996 rcu_read_unlock();
1997}
1998
1999/**
David Howells3a3b7ce2008-11-14 10:39:28 +11002000 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002001 * @new: points to the set of credentials to be modified.
2002 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002003 *
2004 * Set the security data for a kernel service.
2005 */
2006static int smack_kernel_act_as(struct cred *new, u32 secid)
2007{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002008 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002009
Casey Schaufler152f91d2016-11-14 09:38:15 -08002010 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002011 return 0;
2012}
2013
2014/**
2015 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002016 * @new: points to the set of credentials to be modified
2017 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002018 *
2019 * Set the file creation context in a set of credentials to the same
2020 * as the objective context of the specified inode
2021 */
2022static int smack_kernel_create_files_as(struct cred *new,
2023 struct inode *inode)
2024{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002025 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002026 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002027
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002028 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002029 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002030 return 0;
2031}
2032
2033/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002034 * smk_curacc_on_task - helper to log task related access
2035 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002036 * @access: the access requested
2037 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002038 *
2039 * Return 0 if access is permitted
2040 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002041static int smk_curacc_on_task(struct task_struct *p, int access,
2042 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002043{
2044 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002045 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002046 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002047
Casey Schaufler531f1d42011-09-19 12:41:42 -07002048 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002049 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002050 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002051 rc = smk_bu_task(p, access, rc);
2052 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002053}
2054
2055/**
Casey Schauflere114e472008-02-04 22:29:50 -08002056 * smack_task_setpgid - Smack check on setting pgid
2057 * @p: the task object
2058 * @pgid: unused
2059 *
2060 * Return 0 if write access is permitted
2061 */
2062static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2063{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002064 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002065}
2066
2067/**
2068 * smack_task_getpgid - Smack access check for getpgid
2069 * @p: the object task
2070 *
2071 * Returns 0 if current can read the object task, error code otherwise
2072 */
2073static int smack_task_getpgid(struct task_struct *p)
2074{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002075 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002076}
2077
2078/**
2079 * smack_task_getsid - Smack access check for getsid
2080 * @p: the object task
2081 *
2082 * Returns 0 if current can read the object task, error code otherwise
2083 */
2084static int smack_task_getsid(struct task_struct *p)
2085{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002086 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002087}
2088
2089/**
2090 * smack_task_getsecid - get the secid of the task
2091 * @p: the object task
2092 * @secid: where to put the result
2093 *
2094 * Sets the secid to contain a u32 version of the smack label.
2095 */
2096static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2097{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002098 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002099
2100 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002101}
2102
2103/**
2104 * smack_task_setnice - Smack check on setting nice
2105 * @p: the task object
2106 * @nice: unused
2107 *
2108 * Return 0 if write access is permitted
2109 */
2110static int smack_task_setnice(struct task_struct *p, int nice)
2111{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002112 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002113}
2114
2115/**
2116 * smack_task_setioprio - Smack check on setting ioprio
2117 * @p: the task object
2118 * @ioprio: unused
2119 *
2120 * Return 0 if write access is permitted
2121 */
2122static int smack_task_setioprio(struct task_struct *p, int ioprio)
2123{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002124 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002125}
2126
2127/**
2128 * smack_task_getioprio - Smack check on reading ioprio
2129 * @p: the task object
2130 *
2131 * Return 0 if read access is permitted
2132 */
2133static int smack_task_getioprio(struct task_struct *p)
2134{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002135 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002136}
2137
2138/**
2139 * smack_task_setscheduler - Smack check on setting scheduler
2140 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002141 *
2142 * Return 0 if read access is permitted
2143 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002144static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002145{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002146 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002147}
2148
2149/**
2150 * smack_task_getscheduler - Smack check on reading scheduler
2151 * @p: the task object
2152 *
2153 * Return 0 if read access is permitted
2154 */
2155static int smack_task_getscheduler(struct task_struct *p)
2156{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002157 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002158}
2159
2160/**
2161 * smack_task_movememory - Smack check on moving memory
2162 * @p: the task object
2163 *
2164 * Return 0 if write access is permitted
2165 */
2166static int smack_task_movememory(struct task_struct *p)
2167{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002168 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002169}
2170
2171/**
2172 * smack_task_kill - Smack check on signal delivery
2173 * @p: the task object
2174 * @info: unused
2175 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002176 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002177 *
2178 * Return 0 if write access is permitted
2179 *
Casey Schauflere114e472008-02-04 22:29:50 -08002180 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002181static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002182 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002183{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002184 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002185 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002186 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002187 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002188
Rafal Krypa18d872f2016-04-04 11:14:53 +02002189 if (!sig)
2190 return 0; /* null signal; existence test */
2191
Etienne Bassetecfcc532009-04-08 20:40:06 +02002192 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2193 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002194 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002195 * Sending a signal requires that the sender
2196 * can write the receiver.
2197 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002198 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002199 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2200 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002201 return rc;
2202 }
Casey Schauflere114e472008-02-04 22:29:50 -08002203 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002204 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002205 * specific behavior. This is not clean. For one thing
2206 * we can't take privilege into account.
2207 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002208 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002209 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2210 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002211 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002212}
2213
2214/**
Casey Schauflere114e472008-02-04 22:29:50 -08002215 * smack_task_to_inode - copy task smack into the inode blob
2216 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002217 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002218 *
2219 * Sets the smack pointer in the inode security blob
2220 */
2221static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2222{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002223 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002224 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002225
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002226 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002227 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002228}
2229
2230/*
2231 * Socket hooks.
2232 */
2233
2234/**
2235 * smack_sk_alloc_security - Allocate a socket blob
2236 * @sk: the socket
2237 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002238 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002239 *
2240 * Assign Smack pointers to current
2241 *
2242 * Returns 0 on success, -ENOMEM is there's no memory
2243 */
2244static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2245{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002246 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002247 struct socket_smack *ssp;
2248
2249 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2250 if (ssp == NULL)
2251 return -ENOMEM;
2252
jooseong lee08382c92016-11-03 11:54:39 +01002253 /*
2254 * Sockets created by kernel threads receive web label.
2255 */
2256 if (unlikely(current->flags & PF_KTHREAD)) {
2257 ssp->smk_in = &smack_known_web;
2258 ssp->smk_out = &smack_known_web;
2259 } else {
2260 ssp->smk_in = skp;
2261 ssp->smk_out = skp;
2262 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002263 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002264
2265 sk->sk_security = ssp;
2266
2267 return 0;
2268}
2269
2270/**
2271 * smack_sk_free_security - Free a socket blob
2272 * @sk: the socket
2273 *
2274 * Clears the blob pointer
2275 */
2276static void smack_sk_free_security(struct sock *sk)
2277{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302278#ifdef SMACK_IPV6_PORT_LABELING
2279 struct smk_port_label *spp;
2280
2281 if (sk->sk_family == PF_INET6) {
2282 rcu_read_lock();
2283 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2284 if (spp->smk_sock != sk)
2285 continue;
2286 spp->smk_can_reuse = 1;
2287 break;
2288 }
2289 rcu_read_unlock();
2290 }
2291#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002292 kfree(sk->sk_security);
2293}
2294
2295/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002296* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002297* @sip: the object end
2298*
2299* looks for host based access restrictions
2300*
2301* This version will only be appropriate for really small sets of single label
2302* hosts. The caller is responsible for ensuring that the RCU read lock is
2303* taken before calling this function.
2304*
2305* Returns the label of the far end or NULL if it's not special.
2306*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002307static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002308{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002309 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002310 struct in_addr *siap = &sip->sin_addr;
2311
2312 if (siap->s_addr == 0)
2313 return NULL;
2314
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002315 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2316 /*
2317 * we break after finding the first match because
2318 * the list is sorted from longest to shortest mask
2319 * so we have found the most specific match
2320 */
2321 if (snp->smk_host.s_addr ==
2322 (siap->s_addr & snp->smk_mask.s_addr))
2323 return snp->smk_label;
2324
2325 return NULL;
2326}
2327
2328#if IS_ENABLED(CONFIG_IPV6)
2329/*
2330 * smk_ipv6_localhost - Check for local ipv6 host address
2331 * @sip: the address
2332 *
2333 * Returns boolean true if this is the localhost address
2334 */
2335static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2336{
2337 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2338 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2339
2340 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2341 ntohs(be16p[7]) == 1)
2342 return true;
2343 return false;
2344}
2345
2346/**
2347* smack_ipv6host_label - check host based restrictions
2348* @sip: the object end
2349*
2350* looks for host based access restrictions
2351*
2352* This version will only be appropriate for really small sets of single label
2353* hosts. The caller is responsible for ensuring that the RCU read lock is
2354* taken before calling this function.
2355*
2356* Returns the label of the far end or NULL if it's not special.
2357*/
2358static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2359{
2360 struct smk_net6addr *snp;
2361 struct in6_addr *sap = &sip->sin6_addr;
2362 int i;
2363 int found = 0;
2364
2365 /*
2366 * It's local. Don't look for a host label.
2367 */
2368 if (smk_ipv6_localhost(sip))
2369 return NULL;
2370
2371 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002372 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002373 * If the label is NULL the entry has
2374 * been renounced. Ignore it.
2375 */
2376 if (snp->smk_label == NULL)
2377 continue;
2378 /*
Paul Moore07feee82009-03-27 17:10:54 -04002379 * we break after finding the first match because
2380 * the list is sorted from longest to shortest mask
2381 * so we have found the most specific match
2382 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002383 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002384 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2385 snp->smk_host.s6_addr16[i]) {
2386 found = 0;
2387 break;
2388 }
Etienne Basset43031542009-03-27 17:11:01 -04002389 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002390 if (found)
2391 return snp->smk_label;
2392 }
Paul Moore07feee82009-03-27 17:10:54 -04002393
2394 return NULL;
2395}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002396#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002397
2398/**
Casey Schauflere114e472008-02-04 22:29:50 -08002399 * smack_netlabel - Set the secattr on a socket
2400 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002401 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002402 *
2403 * Convert the outbound smack value (smk_out) to a
2404 * secattr and attach it to the socket.
2405 *
2406 * Returns 0 on success or an error code
2407 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002408static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002409{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002410 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002411 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002412 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002413
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002414 /*
2415 * Usually the netlabel code will handle changing the
2416 * packet labeling based on the label.
2417 * The case of a single label host is different, because
2418 * a single label host should never get a labeled packet
2419 * even though the label is usually associated with a packet
2420 * label.
2421 */
2422 local_bh_disable();
2423 bh_lock_sock_nested(sk);
2424
2425 if (ssp->smk_out == smack_net_ambient ||
2426 labeled == SMACK_UNLABELED_SOCKET)
2427 netlbl_sock_delattr(sk);
2428 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002429 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002430 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002431 }
2432
2433 bh_unlock_sock(sk);
2434 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002435
Casey Schauflere114e472008-02-04 22:29:50 -08002436 return rc;
2437}
2438
2439/**
Paul Moore07feee82009-03-27 17:10:54 -04002440 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2441 * @sk: the socket
2442 * @sap: the destination address
2443 *
2444 * Set the correct secattr for the given socket based on the destination
2445 * address and perform any outbound access checks needed.
2446 *
2447 * Returns 0 on success or an error code.
2448 *
2449 */
2450static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2451{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002452 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002453 int rc;
2454 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002455 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002456 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002457 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002458
2459 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002460 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002461 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002462#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002463 struct lsm_network_audit net;
2464
Eric Paris48c62af2012-04-02 13:15:44 -04002465 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2466 ad.a.u.net->family = sap->sin_family;
2467 ad.a.u.net->dport = sap->sin_port;
2468 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002469#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002470 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002471 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002472 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2473 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002474 } else {
2475 sk_lbl = SMACK_CIPSO_SOCKET;
2476 rc = 0;
2477 }
2478 rcu_read_unlock();
2479 if (rc != 0)
2480 return rc;
2481
2482 return smack_netlabel(sk, sk_lbl);
2483}
2484
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002485#if IS_ENABLED(CONFIG_IPV6)
2486/**
2487 * smk_ipv6_check - check Smack access
2488 * @subject: subject Smack label
2489 * @object: object Smack label
2490 * @address: address
2491 * @act: the action being taken
2492 *
2493 * Check an IPv6 access
2494 */
2495static int smk_ipv6_check(struct smack_known *subject,
2496 struct smack_known *object,
2497 struct sockaddr_in6 *address, int act)
2498{
2499#ifdef CONFIG_AUDIT
2500 struct lsm_network_audit net;
2501#endif
2502 struct smk_audit_info ad;
2503 int rc;
2504
2505#ifdef CONFIG_AUDIT
2506 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2507 ad.a.u.net->family = PF_INET6;
2508 ad.a.u.net->dport = ntohs(address->sin6_port);
2509 if (act == SMK_RECEIVING)
2510 ad.a.u.net->v6info.saddr = address->sin6_addr;
2511 else
2512 ad.a.u.net->v6info.daddr = address->sin6_addr;
2513#endif
2514 rc = smk_access(subject, object, MAY_WRITE, &ad);
2515 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2516 return rc;
2517}
2518#endif /* CONFIG_IPV6 */
2519
2520#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002521/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002522 * smk_ipv6_port_label - Smack port access table management
2523 * @sock: socket
2524 * @address: address
2525 *
2526 * Create or update the port list entry
2527 */
2528static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2529{
2530 struct sock *sk = sock->sk;
2531 struct sockaddr_in6 *addr6;
2532 struct socket_smack *ssp = sock->sk->sk_security;
2533 struct smk_port_label *spp;
2534 unsigned short port = 0;
2535
2536 if (address == NULL) {
2537 /*
2538 * This operation is changing the Smack information
2539 * on the bound socket. Take the changes to the port
2540 * as well.
2541 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302542 rcu_read_lock();
2543 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002544 if (sk != spp->smk_sock)
2545 continue;
2546 spp->smk_in = ssp->smk_in;
2547 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302548 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002549 return;
2550 }
2551 /*
2552 * A NULL address is only used for updating existing
2553 * bound entries. If there isn't one, it's OK.
2554 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302555 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002556 return;
2557 }
2558
2559 addr6 = (struct sockaddr_in6 *)address;
2560 port = ntohs(addr6->sin6_port);
2561 /*
2562 * This is a special case that is safely ignored.
2563 */
2564 if (port == 0)
2565 return;
2566
2567 /*
2568 * Look for an existing port list entry.
2569 * This is an indication that a port is getting reused.
2570 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302571 rcu_read_lock();
2572 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302573 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002574 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302575 if (spp->smk_can_reuse != 1) {
2576 rcu_read_unlock();
2577 return;
2578 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002579 spp->smk_port = port;
2580 spp->smk_sock = sk;
2581 spp->smk_in = ssp->smk_in;
2582 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302583 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302584 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002585 return;
2586 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302587 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002588 /*
2589 * A new port entry is required.
2590 */
2591 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2592 if (spp == NULL)
2593 return;
2594
2595 spp->smk_port = port;
2596 spp->smk_sock = sk;
2597 spp->smk_in = ssp->smk_in;
2598 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302599 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302600 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002601
Vishal Goel3c7ce342016-11-23 10:31:08 +05302602 mutex_lock(&smack_ipv6_lock);
2603 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2604 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002605 return;
2606}
2607
2608/**
2609 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002610 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002611 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002612 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002613 *
2614 * Create or update the port list entry
2615 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002616static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002617 int act)
2618{
Casey Schauflerc6739442013-05-22 18:42:56 -07002619 struct smk_port_label *spp;
2620 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002621 struct smack_known *skp = NULL;
2622 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002623 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002624
2625 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002626 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002627 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002628 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002629 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002630 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002631 }
2632
2633 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002634 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002635 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002636 if (skp != NULL && object != NULL)
2637 return smk_ipv6_check(skp, object, address, act);
2638 if (skp == NULL)
2639 skp = smack_net_ambient;
2640 if (object == NULL)
2641 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002642
2643 /*
2644 * It's remote, so port lookup does no good.
2645 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002646 if (!smk_ipv6_localhost(address))
2647 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002648
2649 /*
2650 * It's local so the send check has to have passed.
2651 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002652 if (act == SMK_RECEIVING)
2653 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002654
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002655 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302656 rcu_read_lock();
2657 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302658 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002659 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002660 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002661 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002662 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002663 break;
2664 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302665 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002666
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002667 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002668}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002669#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002670
2671/**
Casey Schauflere114e472008-02-04 22:29:50 -08002672 * smack_inode_setsecurity - set smack xattrs
2673 * @inode: the object
2674 * @name: attribute name
2675 * @value: attribute value
2676 * @size: size of the attribute
2677 * @flags: unused
2678 *
2679 * Sets the named attribute in the appropriate blob
2680 *
2681 * Returns 0 on success, or an error code
2682 */
2683static int smack_inode_setsecurity(struct inode *inode, const char *name,
2684 const void *value, size_t size, int flags)
2685{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002686 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002687 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002688 struct socket_smack *ssp;
2689 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002690 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002691
Casey Schauflerf7112e62012-05-06 15:22:02 -07002692 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302693 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002694
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002695 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002696 if (IS_ERR(skp))
2697 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002698
2699 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002700 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002701 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002702 return 0;
2703 }
2704 /*
2705 * The rest of the Smack xattrs are only on sockets.
2706 */
2707 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2708 return -EOPNOTSUPP;
2709
2710 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002711 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002712 return -EOPNOTSUPP;
2713
2714 ssp = sock->sk->sk_security;
2715
2716 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002717 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002718 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002719 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002720 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002721 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2722 if (rc != 0)
2723 printk(KERN_WARNING
2724 "Smack: \"%s\" netlbl error %d.\n",
2725 __func__, -rc);
2726 }
Casey Schauflere114e472008-02-04 22:29:50 -08002727 } else
2728 return -EOPNOTSUPP;
2729
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002730#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002731 if (sock->sk->sk_family == PF_INET6)
2732 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002733#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002734
Casey Schauflere114e472008-02-04 22:29:50 -08002735 return 0;
2736}
2737
2738/**
2739 * smack_socket_post_create - finish socket setup
2740 * @sock: the socket
2741 * @family: protocol family
2742 * @type: unused
2743 * @protocol: unused
2744 * @kern: unused
2745 *
2746 * Sets the netlabel information on the socket
2747 *
2748 * Returns 0 on success, and error code otherwise
2749 */
2750static int smack_socket_post_create(struct socket *sock, int family,
2751 int type, int protocol, int kern)
2752{
Marcin Lis74123012015-01-22 15:40:33 +01002753 struct socket_smack *ssp;
2754
2755 if (sock->sk == NULL)
2756 return 0;
2757
2758 /*
2759 * Sockets created by kernel threads receive web label.
2760 */
2761 if (unlikely(current->flags & PF_KTHREAD)) {
2762 ssp = sock->sk->sk_security;
2763 ssp->smk_in = &smack_known_web;
2764 ssp->smk_out = &smack_known_web;
2765 }
2766
2767 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002768 return 0;
2769 /*
2770 * Set the outbound netlbl.
2771 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002772 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2773}
2774
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002775/**
2776 * smack_socket_socketpair - create socket pair
2777 * @socka: one socket
2778 * @sockb: another socket
2779 *
2780 * Cross reference the peer labels for SO_PEERSEC
2781 *
luanshia1a07f22019-07-05 10:35:20 +08002782 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002783 */
2784static int smack_socket_socketpair(struct socket *socka,
2785 struct socket *sockb)
2786{
2787 struct socket_smack *asp = socka->sk->sk_security;
2788 struct socket_smack *bsp = sockb->sk->sk_security;
2789
2790 asp->smk_packet = bsp->smk_out;
2791 bsp->smk_packet = asp->smk_out;
2792
2793 return 0;
2794}
2795
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002796#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002797/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002798 * smack_socket_bind - record port binding information.
2799 * @sock: the socket
2800 * @address: the port address
2801 * @addrlen: size of the address
2802 *
2803 * Records the label bound to a port.
2804 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002805 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002806 */
2807static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2808 int addrlen)
2809{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002810 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2811 if (addrlen < SIN6_LEN_RFC2133 ||
2812 address->sa_family != AF_INET6)
2813 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002814 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002815 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002816 return 0;
2817}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002818#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002819
2820/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002821 * smack_socket_connect - connect access check
2822 * @sock: the socket
2823 * @sap: the other end
2824 * @addrlen: size of sap
2825 *
2826 * Verifies that a connection may be possible
2827 *
2828 * Returns 0 on success, and error code otherwise
2829 */
2830static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2831 int addrlen)
2832{
Casey Schauflerc6739442013-05-22 18:42:56 -07002833 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002834
Casey Schauflerc6739442013-05-22 18:42:56 -07002835 if (sock->sk == NULL)
2836 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002837 if (sock->sk->sk_family != PF_INET &&
2838 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2839 return 0;
2840 if (addrlen < offsetofend(struct sockaddr, sa_family))
2841 return 0;
2842 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2843 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002844#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002845 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002846#endif
2847
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002848 if (addrlen < SIN6_LEN_RFC2133)
2849 return 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002850#ifdef SMACK_IPV6_SECMARK_LABELING
2851 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002852 if (rsp != NULL) {
2853 struct socket_smack *ssp = sock->sk->sk_security;
2854
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002855 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002856 SMK_CONNECTING);
2857 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002858#endif
2859#ifdef SMACK_IPV6_PORT_LABELING
2860 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2861#endif
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002862 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002863 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002864 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2865 return 0;
2866 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002867 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002868}
2869
2870/**
2871 * smack_flags_to_may - convert S_ to MAY_ values
2872 * @flags: the S_ value
2873 *
2874 * Returns the equivalent MAY_ value
2875 */
2876static int smack_flags_to_may(int flags)
2877{
2878 int may = 0;
2879
2880 if (flags & S_IRUGO)
2881 may |= MAY_READ;
2882 if (flags & S_IWUGO)
2883 may |= MAY_WRITE;
2884 if (flags & S_IXUGO)
2885 may |= MAY_EXEC;
2886
2887 return may;
2888}
2889
2890/**
2891 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2892 * @msg: the object
2893 *
2894 * Returns 0
2895 */
2896static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2897{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002898 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002899
Casey Schauflerecd5f822018-11-20 11:55:02 -08002900 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002901 return 0;
2902}
2903
2904/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002905 * smack_of_ipc - the smack pointer for the ipc
2906 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002907 *
2908 * Returns a pointer to the smack value
2909 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002910static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002911{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002912 struct smack_known **blob = smack_ipc(isp);
2913
2914 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002915}
2916
2917/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002918 * smack_ipc_alloc_security - Set the security blob for ipc
2919 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002920 *
2921 * Returns 0
2922 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002923static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002924{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002925 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002926
Casey Schaufler019bcca2018-09-21 17:19:54 -07002927 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002928 return 0;
2929}
2930
2931/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002932 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002933 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002934 * @access : access requested
2935 *
2936 * Returns 0 if current has the requested access, error code otherwise
2937 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002938static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002939{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002940 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002941 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002942 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002943
2944#ifdef CONFIG_AUDIT
2945 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002946 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002947#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002948 rc = smk_curacc(ssp, access, &ad);
2949 rc = smk_bu_current("shm", ssp, access, rc);
2950 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002951}
2952
2953/**
Casey Schauflere114e472008-02-04 22:29:50 -08002954 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002955 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002956 * @shmflg: access requested
2957 *
2958 * Returns 0 if current has the requested access, error code otherwise
2959 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002960static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002961{
Casey Schauflere114e472008-02-04 22:29:50 -08002962 int may;
2963
2964 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002965 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002966}
2967
2968/**
2969 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002970 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002971 * @cmd: what it wants to do
2972 *
2973 * Returns 0 if current has the requested access, error code otherwise
2974 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002975static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002976{
Casey Schauflere114e472008-02-04 22:29:50 -08002977 int may;
2978
2979 switch (cmd) {
2980 case IPC_STAT:
2981 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002982 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002983 may = MAY_READ;
2984 break;
2985 case IPC_SET:
2986 case SHM_LOCK:
2987 case SHM_UNLOCK:
2988 case IPC_RMID:
2989 may = MAY_READWRITE;
2990 break;
2991 case IPC_INFO:
2992 case SHM_INFO:
2993 /*
2994 * System level information.
2995 */
2996 return 0;
2997 default:
2998 return -EINVAL;
2999 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003000 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003001}
3002
3003/**
3004 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003005 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003006 * @shmaddr: unused
3007 * @shmflg: access requested
3008 *
3009 * Returns 0 if current has the requested access, error code otherwise
3010 */
luanshia1a07f22019-07-05 10:35:20 +08003011static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003012 int shmflg)
3013{
Casey Schauflere114e472008-02-04 22:29:50 -08003014 int may;
3015
3016 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003017 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003018}
3019
3020/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003021 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003022 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003023 * @access : access requested
3024 *
3025 * Returns 0 if current has the requested access, error code otherwise
3026 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003027static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003028{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003029 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003030 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003031 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032
3033#ifdef CONFIG_AUDIT
3034 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003035 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003036#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003037 rc = smk_curacc(ssp, access, &ad);
3038 rc = smk_bu_current("sem", ssp, access, rc);
3039 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003040}
3041
3042/**
Casey Schauflere114e472008-02-04 22:29:50 -08003043 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003044 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003045 * @semflg: access requested
3046 *
3047 * Returns 0 if current has the requested access, error code otherwise
3048 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003049static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003050{
Casey Schauflere114e472008-02-04 22:29:50 -08003051 int may;
3052
3053 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003054 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003055}
3056
3057/**
3058 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003059 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003060 * @cmd: what it wants to do
3061 *
3062 * Returns 0 if current has the requested access, error code otherwise
3063 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003064static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003065{
Casey Schauflere114e472008-02-04 22:29:50 -08003066 int may;
3067
3068 switch (cmd) {
3069 case GETPID:
3070 case GETNCNT:
3071 case GETZCNT:
3072 case GETVAL:
3073 case GETALL:
3074 case IPC_STAT:
3075 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003076 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003077 may = MAY_READ;
3078 break;
3079 case SETVAL:
3080 case SETALL:
3081 case IPC_RMID:
3082 case IPC_SET:
3083 may = MAY_READWRITE;
3084 break;
3085 case IPC_INFO:
3086 case SEM_INFO:
3087 /*
3088 * System level information
3089 */
3090 return 0;
3091 default:
3092 return -EINVAL;
3093 }
3094
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003095 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003096}
3097
3098/**
3099 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003100 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003101 * @sops: unused
3102 * @nsops: unused
3103 * @alter: unused
3104 *
3105 * Treated as read and write in all cases.
3106 *
3107 * Returns 0 if access is allowed, error code otherwise
3108 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003109static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003110 unsigned nsops, int alter)
3111{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003112 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003113}
3114
3115/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003116 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003117 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003118 * @access : access requested
3119 *
3120 * return 0 if current has access, error otherwise
3121 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003122static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003123{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003124 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003125 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003126 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003127
3128#ifdef CONFIG_AUDIT
3129 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003130 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003131#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003132 rc = smk_curacc(msp, access, &ad);
3133 rc = smk_bu_current("msq", msp, access, rc);
3134 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003135}
3136
3137/**
Casey Schauflere114e472008-02-04 22:29:50 -08003138 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003139 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003140 * @msqflg: access requested
3141 *
3142 * Returns 0 if current has the requested access, error code otherwise
3143 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003144static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003145{
Casey Schauflere114e472008-02-04 22:29:50 -08003146 int may;
3147
3148 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003149 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003150}
3151
3152/**
3153 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003154 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003155 * @cmd: what it wants to do
3156 *
3157 * Returns 0 if current has the requested access, error code otherwise
3158 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003159static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003160{
Casey Schauflere114e472008-02-04 22:29:50 -08003161 int may;
3162
3163 switch (cmd) {
3164 case IPC_STAT:
3165 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003166 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003167 may = MAY_READ;
3168 break;
3169 case IPC_SET:
3170 case IPC_RMID:
3171 may = MAY_READWRITE;
3172 break;
3173 case IPC_INFO:
3174 case MSG_INFO:
3175 /*
3176 * System level information
3177 */
3178 return 0;
3179 default:
3180 return -EINVAL;
3181 }
3182
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003183 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003184}
3185
3186/**
3187 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003188 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003189 * @msg: unused
3190 * @msqflg: access requested
3191 *
3192 * Returns 0 if current has the requested access, error code otherwise
3193 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003194static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003195 int msqflg)
3196{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003197 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003198
Etienne Bassetecfcc532009-04-08 20:40:06 +02003199 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003200 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003201}
3202
3203/**
3204 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003205 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003206 * @msg: unused
3207 * @target: unused
3208 * @type: unused
3209 * @mode: unused
3210 *
3211 * Returns 0 if current has read and write access, error code otherwise
3212 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003213static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003214 struct task_struct *target, long type, int mode)
3215{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003216 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003217}
3218
3219/**
3220 * smack_ipc_permission - Smack access for ipc_permission()
3221 * @ipp: the object permissions
3222 * @flag: access requested
3223 *
3224 * Returns 0 if current has read and write access, error code otherwise
3225 */
3226static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3227{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003228 struct smack_known **blob = smack_ipc(ipp);
3229 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003230 int may = smack_flags_to_may(flag);
3231 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003232 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003233
Etienne Bassetecfcc532009-04-08 20:40:06 +02003234#ifdef CONFIG_AUDIT
3235 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3236 ad.a.u.ipc_id = ipp->id;
3237#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003238 rc = smk_curacc(iskp, may, &ad);
3239 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003240 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003241}
3242
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003243/**
3244 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003245 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003246 * @secid: where result will be saved
3247 */
3248static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3249{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003250 struct smack_known **blob = smack_ipc(ipp);
3251 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003252
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003253 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003254}
3255
Casey Schauflere114e472008-02-04 22:29:50 -08003256/**
3257 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003258 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003259 * @inode: the object
3260 *
3261 * Set the inode's security blob if it hasn't been done already.
3262 */
3263static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3264{
3265 struct super_block *sbp;
3266 struct superblock_smack *sbsp;
3267 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003268 struct smack_known *skp;
3269 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003270 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003271 char trattr[TRANS_TRUE_SIZE];
3272 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003273 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003274 struct dentry *dp;
3275
3276 if (inode == NULL)
3277 return;
3278
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003279 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003280
3281 mutex_lock(&isp->smk_lock);
3282 /*
3283 * If the inode is already instantiated
3284 * take the quick way out
3285 */
3286 if (isp->smk_flags & SMK_INODE_INSTANT)
3287 goto unlockandout;
3288
3289 sbp = inode->i_sb;
3290 sbsp = sbp->s_security;
3291 /*
3292 * We're going to use the superblock default label
3293 * if there's no label on the file.
3294 */
3295 final = sbsp->smk_default;
3296
3297 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003298 * If this is the root inode the superblock
3299 * may be in the process of initialization.
3300 * If that is the case use the root value out
3301 * of the superblock.
3302 */
3303 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003304 switch (sbp->s_magic) {
3305 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003306 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003307 /*
3308 * The cgroup filesystem is never mounted,
3309 * so there's no opportunity to set the mount
3310 * options.
3311 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003312 sbsp->smk_root = &smack_known_star;
3313 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003314 isp->smk_inode = sbsp->smk_root;
3315 break;
3316 case TMPFS_MAGIC:
3317 /*
3318 * What about shmem/tmpfs anonymous files with dentry
3319 * obtained from d_alloc_pseudo()?
3320 */
3321 isp->smk_inode = smk_of_current();
3322 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003323 case PIPEFS_MAGIC:
3324 isp->smk_inode = smk_of_current();
3325 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003326 case SOCKFS_MAGIC:
3327 /*
3328 * Socket access is controlled by the socket
3329 * structures associated with the task involved.
3330 */
3331 isp->smk_inode = &smack_known_star;
3332 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003333 default:
3334 isp->smk_inode = sbsp->smk_root;
3335 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003336 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003337 isp->smk_flags |= SMK_INODE_INSTANT;
3338 goto unlockandout;
3339 }
3340
3341 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003342 * This is pretty hackish.
3343 * Casey says that we shouldn't have to do
3344 * file system specific code, but it does help
3345 * with keeping it simple.
3346 */
3347 switch (sbp->s_magic) {
3348 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003349 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003350 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003351 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003352 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003353 * that the smack file system doesn't do
3354 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003355 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003356 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003357 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003358 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003359 break;
3360 case DEVPTS_SUPER_MAGIC:
3361 /*
3362 * devpts seems content with the label of the task.
3363 * Programs that change smack have to treat the
3364 * pty with respect.
3365 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003366 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003367 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003368 case PROC_SUPER_MAGIC:
3369 /*
3370 * Casey says procfs appears not to care.
3371 * The superblock default suffices.
3372 */
3373 break;
3374 case TMPFS_MAGIC:
3375 /*
3376 * Device labels should come from the filesystem,
3377 * but watch out, because they're volitile,
3378 * getting recreated on every reboot.
3379 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003380 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003381 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003382 * If a smack value has been set we want to use it,
3383 * but since tmpfs isn't giving us the opportunity
3384 * to set mount options simulate setting the
3385 * superblock default.
3386 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003387 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003388 default:
3389 /*
3390 * This isn't an understood special case.
3391 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003392 */
3393
3394 /*
3395 * UNIX domain sockets use lower level socket data.
3396 */
3397 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003398 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003399 break;
3400 }
3401 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003402 * No xattr support means, alas, no SMACK label.
3403 * Use the aforeapplied default.
3404 * It would be curious if the label of the task
3405 * does not match that assigned.
3406 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003407 if (!(inode->i_opflags & IOP_XATTR))
3408 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003409 /*
3410 * Get the dentry for xattr.
3411 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003412 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003413 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003414 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003415 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003416
3417 /*
3418 * Transmuting directory
3419 */
3420 if (S_ISDIR(inode->i_mode)) {
3421 /*
3422 * If this is a new directory and the label was
3423 * transmuted when the inode was initialized
3424 * set the transmute attribute on the directory
3425 * and mark the inode.
3426 *
3427 * If there is a transmute attribute on the
3428 * directory mark the inode.
3429 */
3430 if (isp->smk_flags & SMK_INODE_CHANGED) {
3431 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003432 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003433 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003434 TRANS_TRUE, TRANS_TRUE_SIZE,
3435 0);
3436 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003437 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003438 XATTR_NAME_SMACKTRANSMUTE, trattr,
3439 TRANS_TRUE_SIZE);
3440 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3441 TRANS_TRUE_SIZE) != 0)
3442 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003443 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003444 if (rc >= 0)
3445 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003446 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003447 /*
3448 * Don't let the exec or mmap label be "*" or "@".
3449 */
3450 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3451 if (IS_ERR(skp) || skp == &smack_known_star ||
3452 skp == &smack_known_web)
3453 skp = NULL;
3454 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003455
Casey Schaufler19760ad2013-12-16 16:27:26 -08003456 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003457 if (IS_ERR(skp) || skp == &smack_known_star ||
3458 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003459 skp = NULL;
3460 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003461
Casey Schauflere114e472008-02-04 22:29:50 -08003462 dput(dp);
3463 break;
3464 }
3465
3466 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003467 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003468 else
3469 isp->smk_inode = final;
3470
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003471 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003472
3473unlockandout:
3474 mutex_unlock(&isp->smk_lock);
3475 return;
3476}
3477
3478/**
3479 * smack_getprocattr - Smack process attribute access
3480 * @p: the object task
3481 * @name: the name of the attribute in /proc/.../attr
3482 * @value: where to put the result
3483 *
3484 * Places a copy of the task Smack into value
3485 *
3486 * Returns the length of the smack label or an error code
3487 */
3488static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3489{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003490 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003491 char *cp;
3492 int slen;
3493
3494 if (strcmp(name, "current") != 0)
3495 return -EINVAL;
3496
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003497 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003498 if (cp == NULL)
3499 return -ENOMEM;
3500
3501 slen = strlen(cp);
3502 *value = cp;
3503 return slen;
3504}
3505
3506/**
3507 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003508 * @name: the name of the attribute in /proc/.../attr
3509 * @value: the value to set
3510 * @size: the size of the value
3511 *
3512 * Sets the Smack value of the task. Only setting self
3513 * is permitted and only with privilege
3514 *
3515 * Returns the length of the smack label or an error code
3516 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003517static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003518{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003519 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003520 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003521 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003522 struct smack_known_list_elem *sklep;
3523 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003524
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003525 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003526 return -EPERM;
3527
Casey Schauflerf7112e62012-05-06 15:22:02 -07003528 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003529 return -EINVAL;
3530
3531 if (strcmp(name, "current") != 0)
3532 return -EINVAL;
3533
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003534 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003535 if (IS_ERR(skp))
3536 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003537
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003538 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303539 * No process is ever allowed the web ("@") label
3540 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003541 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303542 if (skp == &smack_known_web || skp == &smack_known_star)
3543 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003544
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003545 if (!smack_privileged(CAP_MAC_ADMIN)) {
3546 rc = -EPERM;
3547 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3548 if (sklep->smk_label == skp) {
3549 rc = 0;
3550 break;
3551 }
3552 if (rc)
3553 return rc;
3554 }
3555
David Howellsd84f4f92008-11-14 10:39:23 +11003556 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003557 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003558 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003559
Casey Schauflerb17103a2018-11-09 16:12:56 -08003560 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003561 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003562 /*
3563 * process can change its label only once
3564 */
3565 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003566
David Howellsd84f4f92008-11-14 10:39:23 +11003567 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003568 return size;
3569}
3570
3571/**
3572 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003573 * @sock: one sock
3574 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003575 * @newsk: unused
3576 *
3577 * Return 0 if a subject with the smack of sock could access
3578 * an object with the smack of other, otherwise an error code
3579 */
David S. Miller3610cda2011-01-05 15:38:53 -08003580static int smack_unix_stream_connect(struct sock *sock,
3581 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003582{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003583 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003584 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003585 struct socket_smack *ssp = sock->sk_security;
3586 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003587 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003588 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003589 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003590#ifdef CONFIG_AUDIT
3591 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003592#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003593
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003594 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3595 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003596 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003597#ifdef CONFIG_AUDIT
3598 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3599 smk_ad_setfield_u_net_sk(&ad, other);
3600#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003601 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3602 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003603 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003604 okp = osp->smk_out;
3605 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003606 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003607 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003608 MAY_WRITE, rc);
3609 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003610 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003611
Casey Schaufler975d5e52011-09-26 14:43:39 -07003612 /*
3613 * Cross reference the peer labels for SO_PEERSEC.
3614 */
3615 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003616 nsp->smk_packet = ssp->smk_out;
3617 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003618 }
3619
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003620 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003621}
3622
3623/**
3624 * smack_unix_may_send - Smack access on UDS
3625 * @sock: one socket
3626 * @other: the other socket
3627 *
3628 * Return 0 if a subject with the smack of sock could access
3629 * an object with the smack of other, otherwise an error code
3630 */
3631static int smack_unix_may_send(struct socket *sock, struct socket *other)
3632{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003633 struct socket_smack *ssp = sock->sk->sk_security;
3634 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003635 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003636 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003637
Kees Cook923e9a12012-04-10 13:26:44 -07003638#ifdef CONFIG_AUDIT
3639 struct lsm_network_audit net;
3640
Eric Paris48c62af2012-04-02 13:15:44 -04003641 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003642 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003643#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003644
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003645 if (smack_privileged(CAP_MAC_OVERRIDE))
3646 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003647
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003648 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3649 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003650 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003651}
3652
3653/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003654 * smack_socket_sendmsg - Smack check based on destination host
3655 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003656 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003657 * @size: the size of the message
3658 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003659 * Return 0 if the current subject can write to the destination host.
3660 * For IPv4 this is only a question if the destination is a single label host.
3661 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003662 */
3663static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3664 int size)
3665{
3666 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003667#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003668 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003669#endif
3670#ifdef SMACK_IPV6_SECMARK_LABELING
3671 struct socket_smack *ssp = sock->sk->sk_security;
3672 struct smack_known *rsp;
3673#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003674 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003675
3676 /*
3677 * Perfectly reasonable for this to be NULL
3678 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003679 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003680 return 0;
3681
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003682 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003683 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003684 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3685 sip->sin_family != AF_INET)
3686 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003687 rc = smack_netlabel_send(sock->sk, sip);
3688 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003689#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003690 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003691 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3692 sap->sin6_family != AF_INET6)
3693 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003694#ifdef SMACK_IPV6_SECMARK_LABELING
3695 rsp = smack_ipv6host_label(sap);
3696 if (rsp != NULL)
3697 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3698 SMK_CONNECTING);
3699#endif
3700#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003701 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003702#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003703#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003704 break;
3705 }
3706 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003707}
3708
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003709/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003710 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003711 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003712 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003713 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003714 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003715 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003716static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3717 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003718{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003719 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003720 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003721 int acat;
3722 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003723
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003724 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003725 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003726 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003727 * If there are flags but no level netlabel isn't
3728 * behaving the way we expect it to.
3729 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003730 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003731 * Without guidance regarding the smack value
3732 * for the packet fall back on the network
3733 * ambient value.
3734 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003735 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303736 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003737 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003738 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003739 /*
3740 * Compare the catsets. Use the netlbl APIs.
3741 */
3742 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3743 if ((skp->smk_netlabel.flags &
3744 NETLBL_SECATTR_MLS_CAT) == 0)
3745 found = 1;
3746 break;
3747 }
3748 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003749 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3750 acat + 1);
3751 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003752 skp->smk_netlabel.attr.mls.cat,
3753 kcat + 1);
3754 if (acat < 0 || kcat < 0)
3755 break;
3756 }
3757 if (acat == kcat) {
3758 found = 1;
3759 break;
3760 }
Casey Schauflere114e472008-02-04 22:29:50 -08003761 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003762 rcu_read_unlock();
3763
3764 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003765 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003766
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003767 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003768 return &smack_known_web;
3769 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003770 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003771 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003772 /*
3773 * Looks like a fallback, which gives us a secid.
3774 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003775 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003776 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003777 * Without guidance regarding the smack value
3778 * for the packet fall back on the network
3779 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003780 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003781 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003782}
3783
Casey Schaufler69f287a2014-12-12 17:08:40 -08003784#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003785static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003786{
Casey Schauflerc6739442013-05-22 18:42:56 -07003787 u8 nexthdr;
3788 int offset;
3789 int proto = -EINVAL;
3790 struct ipv6hdr _ipv6h;
3791 struct ipv6hdr *ip6;
3792 __be16 frag_off;
3793 struct tcphdr _tcph, *th;
3794 struct udphdr _udph, *uh;
3795 struct dccp_hdr _dccph, *dh;
3796
3797 sip->sin6_port = 0;
3798
3799 offset = skb_network_offset(skb);
3800 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3801 if (ip6 == NULL)
3802 return -EINVAL;
3803 sip->sin6_addr = ip6->saddr;
3804
3805 nexthdr = ip6->nexthdr;
3806 offset += sizeof(_ipv6h);
3807 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3808 if (offset < 0)
3809 return -EINVAL;
3810
3811 proto = nexthdr;
3812 switch (proto) {
3813 case IPPROTO_TCP:
3814 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3815 if (th != NULL)
3816 sip->sin6_port = th->source;
3817 break;
3818 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003819 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003820 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3821 if (uh != NULL)
3822 sip->sin6_port = uh->source;
3823 break;
3824 case IPPROTO_DCCP:
3825 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3826 if (dh != NULL)
3827 sip->sin6_port = dh->dccph_sport;
3828 break;
3829 }
3830 return proto;
3831}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003832#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003833
Casey Schauflere114e472008-02-04 22:29:50 -08003834/**
3835 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3836 * @sk: socket
3837 * @skb: packet
3838 *
3839 * Returns 0 if the packet should be delivered, an error code otherwise
3840 */
3841static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3842{
3843 struct netlbl_lsm_secattr secattr;
3844 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003845 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003846 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003847 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003848 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003849#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003850 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003851#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003852#if IS_ENABLED(CONFIG_IPV6)
3853 struct sockaddr_in6 sadd;
3854 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003855
3856 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3857 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003858#endif /* CONFIG_IPV6 */
3859
Piotr Sawicki129a9982018-07-19 11:42:58 +02003860 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003861 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003862#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3863 /*
3864 * If there is a secmark use it rather than the CIPSO label.
3865 * If there is no secmark fall back to CIPSO.
3866 * The secmark is assumed to reflect policy better.
3867 */
3868 if (skb && skb->secmark != 0) {
3869 skp = smack_from_secid(skb->secmark);
3870 goto access_check;
3871 }
3872#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003873 /*
3874 * Translate what netlabel gave us.
3875 */
3876 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003877
Piotr Sawicki129a9982018-07-19 11:42:58 +02003878 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003879 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003880 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003881 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003882 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003883
Casey Schauflerc6739442013-05-22 18:42:56 -07003884 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003885
Casey Schaufler69f287a2014-12-12 17:08:40 -08003886#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3887access_check:
3888#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003889#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003890 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003891 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003892 ad.a.u.net->netif = skb->skb_iif;
3893 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003894#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003895 /*
3896 * Receiving a packet requires that the other end
3897 * be able to write here. Read access is not required.
3898 * This is the simplist possible security model
3899 * for networking.
3900 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003901 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3902 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003903 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003904 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003905 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003906 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003907#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003908 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003909 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003910 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3911 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003912 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003913#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003914 if (skb && skb->secmark != 0)
3915 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003916 else if (smk_ipv6_localhost(&sadd))
3917 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003918 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003919 skp = smack_ipv6host_label(&sadd);
3920 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003921 skp = smack_net_ambient;
Jia-Ju Bai3f4287e2019-07-23 18:00:15 +08003922 if (skb == NULL)
3923 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003924#ifdef CONFIG_AUDIT
3925 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003926 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003927 ad.a.u.net->netif = skb->skb_iif;
3928 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3929#endif /* CONFIG_AUDIT */
3930 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3931 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3932 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003933#endif /* SMACK_IPV6_SECMARK_LABELING */
3934#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003935 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003936#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003937 if (rc != 0)
3938 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3939 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003940 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003941#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003942 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003943
Paul Moorea8134292008-10-10 10:16:31 -04003944 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003945}
3946
3947/**
3948 * smack_socket_getpeersec_stream - pull in packet label
3949 * @sock: the socket
3950 * @optval: user's destination
3951 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003952 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003953 *
3954 * returns zero on success, an error code otherwise
3955 */
3956static int smack_socket_getpeersec_stream(struct socket *sock,
3957 char __user *optval,
3958 int __user *optlen, unsigned len)
3959{
3960 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003961 char *rcp = "";
3962 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003963 int rc = 0;
3964
3965 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003966 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003967 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003968 slen = strlen(rcp) + 1;
3969 }
Casey Schauflere114e472008-02-04 22:29:50 -08003970
3971 if (slen > len)
3972 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003973 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003974 rc = -EFAULT;
3975
3976 if (put_user(slen, optlen) != 0)
3977 rc = -EFAULT;
3978
3979 return rc;
3980}
3981
3982
3983/**
3984 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003985 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003986 * @skb: packet data
3987 * @secid: pointer to where to put the secid of the packet
3988 *
3989 * Sets the netlabel socket state on sk from parent
3990 */
3991static int smack_socket_getpeersec_dgram(struct socket *sock,
3992 struct sk_buff *skb, u32 *secid)
3993
3994{
3995 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003996 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003997 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003998 int family = PF_UNSPEC;
3999 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004000 int rc;
4001
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004002 if (skb != NULL) {
4003 if (skb->protocol == htons(ETH_P_IP))
4004 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004005#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004006 else if (skb->protocol == htons(ETH_P_IPV6))
4007 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004008#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004009 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004010 if (family == PF_UNSPEC && sock != NULL)
4011 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004012
Casey Schaufler69f287a2014-12-12 17:08:40 -08004013 switch (family) {
4014 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004015 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004016 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004017 break;
4018 case PF_INET:
4019#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4020 s = skb->secmark;
4021 if (s != 0)
4022 break;
4023#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004024 /*
4025 * Translate what netlabel gave us.
4026 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004027 if (sock != NULL && sock->sk != NULL)
4028 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004029 netlbl_secattr_init(&secattr);
4030 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4031 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004032 skp = smack_from_secattr(&secattr, ssp);
4033 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004034 }
4035 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004036 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004037 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004038#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004039 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004040#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004041 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004042 }
4043 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004044 if (s == 0)
4045 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004046 return 0;
4047}
4048
4049/**
Paul Moore07feee82009-03-27 17:10:54 -04004050 * smack_sock_graft - Initialize a newly created socket with an existing sock
4051 * @sk: child sock
4052 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004053 *
Paul Moore07feee82009-03-27 17:10:54 -04004054 * Set the smk_{in,out} state of an existing sock based on the process that
4055 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004056 */
4057static void smack_sock_graft(struct sock *sk, struct socket *parent)
4058{
4059 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004060 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004061
Paul Moore07feee82009-03-27 17:10:54 -04004062 if (sk == NULL ||
4063 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004064 return;
4065
4066 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004067 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004068 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004069 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004070}
4071
4072/**
4073 * smack_inet_conn_request - Smack access check on connect
4074 * @sk: socket involved
4075 * @skb: packet
4076 * @req: unused
4077 *
4078 * Returns 0 if a task with the packet label could write to
4079 * the socket, otherwise an error code
4080 */
4081static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4082 struct request_sock *req)
4083{
Paul Moore07feee82009-03-27 17:10:54 -04004084 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004085 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004086 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004087 struct netlbl_lsm_secattr secattr;
4088 struct sockaddr_in addr;
4089 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004090 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004091 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004092 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004093#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004094 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004095#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004096
Casey Schaufler69f287a2014-12-12 17:08:40 -08004097#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004098 if (family == PF_INET6) {
4099 /*
4100 * Handle mapped IPv4 packets arriving
4101 * via IPv6 sockets. Don't set up netlabel
4102 * processing on IPv6.
4103 */
4104 if (skb->protocol == htons(ETH_P_IP))
4105 family = PF_INET;
4106 else
4107 return 0;
4108 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004109#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004110
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004111#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4112 /*
4113 * If there is a secmark use it rather than the CIPSO label.
4114 * If there is no secmark fall back to CIPSO.
4115 * The secmark is assumed to reflect policy better.
4116 */
4117 if (skb && skb->secmark != 0) {
4118 skp = smack_from_secid(skb->secmark);
4119 goto access_check;
4120 }
4121#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4122
Paul Moore07feee82009-03-27 17:10:54 -04004123 netlbl_secattr_init(&secattr);
4124 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004125 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004126 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004127 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004128 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004129 netlbl_secattr_destroy(&secattr);
4130
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004131#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4132access_check:
4133#endif
4134
Etienne Bassetecfcc532009-04-08 20:40:06 +02004135#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004136 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4137 ad.a.u.net->family = family;
4138 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004139 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4140#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004141 /*
Paul Moore07feee82009-03-27 17:10:54 -04004142 * Receiving a packet requires that the other end be able to write
4143 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004144 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004145 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4146 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004147 if (rc != 0)
4148 return rc;
4149
4150 /*
4151 * Save the peer's label in the request_sock so we can later setup
4152 * smk_packet in the child socket so that SO_PEERCRED can report it.
4153 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004154 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004155
4156 /*
4157 * We need to decide if we want to label the incoming connection here
4158 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004159 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004160 */
4161 hdr = ip_hdr(skb);
4162 addr.sin_addr.s_addr = hdr->saddr;
4163 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004164 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004165 rcu_read_unlock();
4166
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004167 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004168 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004169 else
Paul Moore07feee82009-03-27 17:10:54 -04004170 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004171
4172 return rc;
4173}
4174
Paul Moore07feee82009-03-27 17:10:54 -04004175/**
4176 * smack_inet_csk_clone - Copy the connection information to the new socket
4177 * @sk: the new socket
4178 * @req: the connection's request_sock
4179 *
4180 * Transfer the connection's peer label to the newly created socket.
4181 */
4182static void smack_inet_csk_clone(struct sock *sk,
4183 const struct request_sock *req)
4184{
4185 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004186 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004187
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004188 if (req->peer_secid != 0) {
4189 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004190 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004191 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004192 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004193}
4194
Casey Schauflere114e472008-02-04 22:29:50 -08004195/*
4196 * Key management security hooks
4197 *
4198 * Casey has not tested key support very heavily.
4199 * The permission check is most likely too restrictive.
4200 * If you care about keys please have a look.
4201 */
4202#ifdef CONFIG_KEYS
4203
4204/**
4205 * smack_key_alloc - Set the key security blob
4206 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004207 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004208 * @flags: unused
4209 *
4210 * No allocation required
4211 *
4212 * Returns 0
4213 */
David Howellsd84f4f92008-11-14 10:39:23 +11004214static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004215 unsigned long flags)
4216{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004217 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004218
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004219 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004220 return 0;
4221}
4222
4223/**
4224 * smack_key_free - Clear the key security blob
4225 * @key: the object
4226 *
4227 * Clear the blob pointer
4228 */
4229static void smack_key_free(struct key *key)
4230{
4231 key->security = NULL;
4232}
4233
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004234/**
Casey Schauflere114e472008-02-04 22:29:50 -08004235 * smack_key_permission - Smack access on a key
4236 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004237 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004238 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004239 *
4240 * Return 0 if the task has read and write to the object,
4241 * an error code otherwise
4242 */
4243static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004244 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004245{
4246 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004247 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004248 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004249 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004250 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004251
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004252 /*
4253 * Validate requested permissions
4254 */
4255 if (perm & ~KEY_NEED_ALL)
4256 return -EINVAL;
4257
Casey Schauflere114e472008-02-04 22:29:50 -08004258 keyp = key_ref_to_ptr(key_ref);
4259 if (keyp == NULL)
4260 return -EINVAL;
4261 /*
4262 * If the key hasn't been initialized give it access so that
4263 * it may do so.
4264 */
4265 if (keyp->security == NULL)
4266 return 0;
4267 /*
4268 * This should not occur
4269 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004270 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004271 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004272
4273 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4274 return 0;
4275
Etienne Bassetecfcc532009-04-08 20:40:06 +02004276#ifdef CONFIG_AUDIT
4277 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4278 ad.a.u.key_struct.key = keyp->serial;
4279 ad.a.u.key_struct.key_desc = keyp->description;
4280#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004281 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4282 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004283 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004284 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004285 rc = smk_access(tkp, keyp->security, request, &ad);
4286 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4287 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004288}
José Bollo7fc5f362015-02-17 15:41:22 +01004289
4290/*
4291 * smack_key_getsecurity - Smack label tagging the key
4292 * @key points to the key to be queried
4293 * @_buffer points to a pointer that should be set to point to the
4294 * resulting string (if no label or an error occurs).
4295 * Return the length of the string (including terminating NUL) or -ve if
4296 * an error.
4297 * May also return 0 (and a NULL buffer pointer) if there is no label.
4298 */
4299static int smack_key_getsecurity(struct key *key, char **_buffer)
4300{
4301 struct smack_known *skp = key->security;
4302 size_t length;
4303 char *copy;
4304
4305 if (key->security == NULL) {
4306 *_buffer = NULL;
4307 return 0;
4308 }
4309
4310 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4311 if (copy == NULL)
4312 return -ENOMEM;
4313 length = strlen(copy) + 1;
4314
4315 *_buffer = copy;
4316 return length;
4317}
4318
Casey Schauflere114e472008-02-04 22:29:50 -08004319#endif /* CONFIG_KEYS */
4320
4321/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004322 * Smack Audit hooks
4323 *
4324 * Audit requires a unique representation of each Smack specific
4325 * rule. This unique representation is used to distinguish the
4326 * object to be audited from remaining kernel objects and also
4327 * works as a glue between the audit hooks.
4328 *
4329 * Since repository entries are added but never deleted, we'll use
4330 * the smack_known label address related to the given audit rule as
4331 * the needed unique representation. This also better fits the smack
4332 * model where nearly everything is a label.
4333 */
4334#ifdef CONFIG_AUDIT
4335
4336/**
4337 * smack_audit_rule_init - Initialize a smack audit rule
4338 * @field: audit rule fields given from user-space (audit.h)
4339 * @op: required testing operator (=, !=, >, <, ...)
4340 * @rulestr: smack label to be audited
4341 * @vrule: pointer to save our own audit rule representation
4342 *
4343 * Prepare to audit cases where (@field @op @rulestr) is true.
4344 * The label to be audited is created if necessay.
4345 */
4346static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4347{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004348 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004349 char **rule = (char **)vrule;
4350 *rule = NULL;
4351
4352 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4353 return -EINVAL;
4354
Al Viro5af75d82008-12-16 05:59:26 -05004355 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004356 return -EINVAL;
4357
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004358 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004359 if (IS_ERR(skp))
4360 return PTR_ERR(skp);
4361
4362 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004363
4364 return 0;
4365}
4366
4367/**
4368 * smack_audit_rule_known - Distinguish Smack audit rules
4369 * @krule: rule of interest, in Audit kernel representation format
4370 *
4371 * This is used to filter Smack rules from remaining Audit ones.
4372 * If it's proved that this rule belongs to us, the
4373 * audit_rule_match hook will be called to do the final judgement.
4374 */
4375static int smack_audit_rule_known(struct audit_krule *krule)
4376{
4377 struct audit_field *f;
4378 int i;
4379
4380 for (i = 0; i < krule->field_count; i++) {
4381 f = &krule->fields[i];
4382
4383 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4384 return 1;
4385 }
4386
4387 return 0;
4388}
4389
4390/**
4391 * smack_audit_rule_match - Audit given object ?
4392 * @secid: security id for identifying the object to test
4393 * @field: audit rule flags given from user-space
4394 * @op: required testing operator
4395 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004396 *
4397 * The core Audit hook. It's used to take the decision of
4398 * whether to audit or not to audit a given object.
4399 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004400static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004401{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004402 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004403 char *rule = vrule;
4404
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004405 if (unlikely(!rule)) {
4406 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004407 return -ENOENT;
4408 }
4409
4410 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4411 return 0;
4412
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004413 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004414
4415 /*
4416 * No need to do string comparisons. If a match occurs,
4417 * both pointers will point to the same smack_known
4418 * label.
4419 */
Al Viro5af75d82008-12-16 05:59:26 -05004420 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004421 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004422 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004423 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004424
4425 return 0;
4426}
4427
Casey Schaufler491a0b02016-01-26 15:08:35 -08004428/*
4429 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004430 * No memory was allocated.
4431 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004432
4433#endif /* CONFIG_AUDIT */
4434
Randy Dunlap251a2a92009-02-18 11:42:33 -08004435/**
David Quigley746df9b2013-05-22 12:50:35 -04004436 * smack_ismaclabel - check if xattr @name references a smack MAC label
4437 * @name: Full xattr name to check.
4438 */
4439static int smack_ismaclabel(const char *name)
4440{
4441 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4442}
4443
4444
4445/**
Casey Schauflere114e472008-02-04 22:29:50 -08004446 * smack_secid_to_secctx - return the smack label for a secid
4447 * @secid: incoming integer
4448 * @secdata: destination
4449 * @seclen: how long it is
4450 *
4451 * Exists for networking code.
4452 */
4453static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4454{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004455 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004456
Eric Parisd5630b92010-10-13 16:24:48 -04004457 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004458 *secdata = skp->smk_known;
4459 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004460 return 0;
4461}
4462
Randy Dunlap251a2a92009-02-18 11:42:33 -08004463/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004464 * smack_secctx_to_secid - return the secid for a smack label
4465 * @secdata: smack label
4466 * @seclen: how long result is
4467 * @secid: outgoing integer
4468 *
4469 * Exists for audit and networking code.
4470 */
David Howellse52c17642008-04-29 20:52:51 +01004471static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004472{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004473 struct smack_known *skp = smk_find_entry(secdata);
4474
4475 if (skp)
4476 *secid = skp->smk_secid;
4477 else
4478 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004479 return 0;
4480}
4481
Casey Schaufler491a0b02016-01-26 15:08:35 -08004482/*
4483 * There used to be a smack_release_secctx hook
4484 * that did nothing back when hooks were in a vector.
4485 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004486 */
Casey Schauflere114e472008-02-04 22:29:50 -08004487
David P. Quigley1ee65e32009-09-03 14:25:57 -04004488static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4489{
4490 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4491}
4492
4493static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4494{
4495 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4496}
4497
4498static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4499{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004500 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004501
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004502 *ctx = skp->smk_known;
4503 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004504 return 0;
4505}
4506
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004507static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4508{
4509
4510 struct task_smack *tsp;
4511 struct smack_known *skp;
4512 struct inode_smack *isp;
4513 struct cred *new_creds = *new;
4514
4515 if (new_creds == NULL) {
4516 new_creds = prepare_creds();
4517 if (new_creds == NULL)
4518 return -ENOMEM;
4519 }
4520
Casey Schauflerb17103a2018-11-09 16:12:56 -08004521 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004522
4523 /*
4524 * Get label from overlay inode and set it in create_sid
4525 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004526 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004527 skp = isp->smk_inode;
4528 tsp->smk_task = skp;
4529 *new = new_creds;
4530 return 0;
4531}
4532
4533static int smack_inode_copy_up_xattr(const char *name)
4534{
4535 /*
4536 * Return 1 if this is the smack access Smack attribute.
4537 */
4538 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4539 return 1;
4540
4541 return -EOPNOTSUPP;
4542}
4543
4544static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4545 struct qstr *name,
4546 const struct cred *old,
4547 struct cred *new)
4548{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004549 struct task_smack *otsp = smack_cred(old);
4550 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004551 struct inode_smack *isp;
4552 int may;
4553
4554 /*
4555 * Use the process credential unless all of
4556 * the transmuting criteria are met
4557 */
4558 ntsp->smk_task = otsp->smk_task;
4559
4560 /*
4561 * the attribute of the containing directory
4562 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004563 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004564
4565 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4566 rcu_read_lock();
4567 may = smk_access_entry(otsp->smk_task->smk_known,
4568 isp->smk_inode->smk_known,
4569 &otsp->smk_task->smk_rules);
4570 rcu_read_unlock();
4571
4572 /*
4573 * If the directory is transmuting and the rule
4574 * providing access is transmuting use the containing
4575 * directory label instead of the process label.
4576 */
4577 if (may > 0 && (may & MAY_TRANSMUTE))
4578 ntsp->smk_task = isp->smk_inode;
4579 }
4580 return 0;
4581}
4582
Casey Schauflerbbd36622018-11-12 09:30:56 -08004583struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4584 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004585 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004586 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004587 .lbs_ipc = sizeof(struct smack_known *),
4588 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004589};
4590
James Morrisca97d932017-02-15 00:18:51 +11004591static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004592 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4593 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4594 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004595
Al Viro0b520752018-12-23 16:02:47 -05004596 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004597 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4598
Casey Schauflere20b0432015-05-02 15:11:36 -07004599 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4600 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004601 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004602 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004603 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304604 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004605
Casey Schauflere20b0432015-05-02 15:11:36 -07004606 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004607
Casey Schauflere20b0432015-05-02 15:11:36 -07004608 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004609 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4610 LSM_HOOK_INIT(inode_link, smack_inode_link),
4611 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4612 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4613 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4614 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4615 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4616 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4617 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4618 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4619 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4620 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4621 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4622 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4623 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4624 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004625
Casey Schauflere20b0432015-05-02 15:11:36 -07004626 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004627 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4628 LSM_HOOK_INIT(file_lock, smack_file_lock),
4629 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4630 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4631 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4632 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4633 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4634 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004635
Casey Schauflere20b0432015-05-02 15:11:36 -07004636 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004637
Casey Schauflere20b0432015-05-02 15:11:36 -07004638 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4639 LSM_HOOK_INIT(cred_free, smack_cred_free),
4640 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4641 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004642 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004643 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4644 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4645 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4646 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4647 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4648 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4649 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4650 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4651 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4652 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4653 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4654 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4655 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004656 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004657
Casey Schauflere20b0432015-05-02 15:11:36 -07004658 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4659 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004660
Casey Schauflere20b0432015-05-02 15:11:36 -07004661 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004662
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004663 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004664 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4665 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4666 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4667 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004668
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004669 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004670 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4671 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4672 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004673
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004674 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004675 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4676 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4677 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004678
Casey Schauflere20b0432015-05-02 15:11:36 -07004679 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004680
Casey Schauflere20b0432015-05-02 15:11:36 -07004681 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4682 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004683
Casey Schauflere20b0432015-05-02 15:11:36 -07004684 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4685 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004686
Casey Schauflere20b0432015-05-02 15:11:36 -07004687 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004688 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004689#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004690 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004691#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004692 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4693 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4694 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4695 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4696 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4697 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4698 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4699 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4700 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4701 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004702
Casey Schauflere114e472008-02-04 22:29:50 -08004703 /* key management security hooks */
4704#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004705 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4706 LSM_HOOK_INIT(key_free, smack_key_free),
4707 LSM_HOOK_INIT(key_permission, smack_key_permission),
4708 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004709#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004710
4711 /* Audit hooks */
4712#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004713 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4714 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4715 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004716#endif /* CONFIG_AUDIT */
4717
Casey Schauflere20b0432015-05-02 15:11:36 -07004718 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4719 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4720 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004721 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4722 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4723 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004724 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4725 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4726 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004727};
4728
Etienne Basset7198e2e2009-03-24 20:53:24 +01004729
Casey Schaufler86812bb2012-04-17 18:55:46 -07004730static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004731{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004732 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004733 * Initialize rule list locks
4734 */
4735 mutex_init(&smack_known_huh.smk_rules_lock);
4736 mutex_init(&smack_known_hat.smk_rules_lock);
4737 mutex_init(&smack_known_floor.smk_rules_lock);
4738 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004739 mutex_init(&smack_known_web.smk_rules_lock);
4740 /*
4741 * Initialize rule lists
4742 */
4743 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4744 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4745 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4746 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004747 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4748 /*
4749 * Create the known labels list
4750 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004751 smk_insert_entry(&smack_known_huh);
4752 smk_insert_entry(&smack_known_hat);
4753 smk_insert_entry(&smack_known_star);
4754 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004755 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004756}
4757
Casey Schauflere114e472008-02-04 22:29:50 -08004758/**
4759 * smack_init - initialize the smack system
4760 *
luanshia1a07f22019-07-05 10:35:20 +08004761 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004762 */
4763static __init int smack_init(void)
4764{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004765 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004766 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004767
Rohit1a5b4722014-10-15 17:40:41 +05304768 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4769 if (!smack_inode_cache)
4770 return -ENOMEM;
4771
Casey Schaufler4e328b02019-04-02 11:37:12 -07004772 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4773 if (!smack_rule_cache) {
4774 kmem_cache_destroy(smack_inode_cache);
4775 return -ENOMEM;
4776 }
4777
Casey Schauflerbbd36622018-11-12 09:30:56 -08004778 /*
4779 * Set the security state for the initial task.
4780 */
4781 tsp = smack_cred(cred);
4782 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4783
4784 /*
4785 * Register with LSM
4786 */
4787 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004788 smack_enabled = 1;
4789
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004790 pr_info("Smack: Initializing.\n");
4791#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4792 pr_info("Smack: Netfilter enabled.\n");
4793#endif
4794#ifdef SMACK_IPV6_PORT_LABELING
4795 pr_info("Smack: IPv6 port labeling enabled.\n");
4796#endif
4797#ifdef SMACK_IPV6_SECMARK_LABELING
4798 pr_info("Smack: IPv6 Netfilter enabled.\n");
4799#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004800
Casey Schaufler86812bb2012-04-17 18:55:46 -07004801 /* initialize the smack_known_list */
4802 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004803
Casey Schauflere114e472008-02-04 22:29:50 -08004804 return 0;
4805}
4806
4807/*
4808 * Smack requires early initialization in order to label
4809 * all processes and objects when they are created.
4810 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004811DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004812 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004813 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004814 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004815 .init = smack_init,
4816};