blob: 646c0b4aa8c415676e8c8b411430e56d6599e00a [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 = {
David Howells2febd252018-11-01 23:07:24 +0000692 .specs = smack_param_specs,
693};
694
695/**
696 * smack_fs_context_parse_param - Parse a single mount parameter
697 * @fc: The new filesystem context being constructed.
698 * @param: The parameter.
699 *
700 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
701 * error.
702 */
703static int smack_fs_context_parse_param(struct fs_context *fc,
704 struct fs_parameter *param)
705{
706 struct fs_parse_result result;
707 int opt, rc;
708
709 opt = fs_parse(fc, &smack_fs_parameters, param, &result);
710 if (opt < 0)
711 return opt;
712
713 rc = smack_add_opt(opt, param->string, &fc->security);
714 if (!rc)
715 param->string = NULL;
716 return rc;
717}
718
Al Virod2497e12018-12-16 01:37:06 -0500719static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530720{
Al Virod2497e12018-12-16 01:37:06 -0500721 char *from = options, *to = options;
722 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530723
Al Viroc3300aa2018-12-16 01:52:24 -0500724 while (1) {
725 char *next = strchr(from, ',');
726 int token, len, rc;
727 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530728
Al Viroc3300aa2018-12-16 01:52:24 -0500729 if (next)
730 len = next - from;
731 else
732 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530733
Al Viroc3300aa2018-12-16 01:52:24 -0500734 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500735 if (token != Opt_error) {
736 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
737 rc = smack_add_opt(token, arg, mnt_opts);
738 if (unlikely(rc)) {
739 kfree(arg);
740 if (*mnt_opts)
741 smack_free_mnt_opts(*mnt_opts);
742 *mnt_opts = NULL;
743 return rc;
744 }
745 } else {
746 if (!first) { // copy with preceding comma
747 from--;
748 len++;
749 }
750 if (to != from)
751 memmove(to, from, len);
752 to += len;
753 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530754 }
Al Viroc3300aa2018-12-16 01:52:24 -0500755 if (!from[len])
756 break;
757 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530758 }
Al Virod2497e12018-12-16 01:37:06 -0500759 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530760 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530761}
762
763/**
764 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800765 * @sb: the file system superblock
luanshia1a07f22019-07-05 10:35:20 +0800766 * @mnt_opts: Smack mount options
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530767 * @kern_flags: mount option from kernel space or user space
768 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800769 *
770 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530771 *
772 * Allow filesystems with binary mount data to explicitly set Smack mount
773 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800774 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530775static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500776 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530777 unsigned long kern_flags,
778 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800779{
780 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000781 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800782 struct superblock_smack *sp = sb->s_security;
783 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800784 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500785 struct smack_mnt_opts *opts = mnt_opts;
786 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800787
Seth Forshee9f50eda2015-09-23 15:16:06 -0500788 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800789 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700790
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700791 if (inode->i_security == NULL) {
792 int rc = lsm_inode_alloc(inode);
793
794 if (rc)
795 return rc;
796 }
797
Himanshu Shukla2097f592016-11-10 16:19:52 +0530798 if (!smack_privileged(CAP_MAC_ADMIN)) {
799 /*
800 * Unprivileged mounts don't get to specify Smack values.
801 */
Al Viro12085b12018-12-13 15:18:05 -0500802 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530803 return -EPERM;
804 /*
805 * Unprivileged mounts get root and default from the caller.
806 */
807 skp = smk_of_current();
808 sp->smk_root = skp;
809 sp->smk_default = skp;
810 /*
811 * For a handful of fs types with no user-controlled
812 * backing store it's okay to trust security labels
813 * in the filesystem. The rest are untrusted.
814 */
815 if (sb->s_user_ns != &init_user_ns &&
816 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
817 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500818 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530819 sp->smk_flags |= SMK_SB_UNTRUSTED;
820 }
821 }
822
Seth Forshee9f50eda2015-09-23 15:16:06 -0500823 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800824
Al Viro12085b12018-12-13 15:18:05 -0500825 if (opts) {
826 if (opts->fsdefault) {
827 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200828 if (IS_ERR(skp))
829 return PTR_ERR(skp);
830 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500831 }
832 if (opts->fsfloor) {
833 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530834 if (IS_ERR(skp))
835 return PTR_ERR(skp);
836 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500837 }
838 if (opts->fshat) {
839 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530840 if (IS_ERR(skp))
841 return PTR_ERR(skp);
842 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500843 }
844 if (opts->fsroot) {
845 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200846 if (IS_ERR(skp))
847 return PTR_ERR(skp);
848 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500849 }
850 if (opts->fstransmute) {
851 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200852 if (IS_ERR(skp))
853 return PTR_ERR(skp);
854 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500855 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800856 }
857 }
858
859 /*
860 * Initialize the root inode.
861 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700862 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800863
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700864 if (transmute) {
865 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700866 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700867 }
Casey Schauflere830b392013-05-22 18:43:07 -0700868
Casey Schauflere114e472008-02-04 22:29:50 -0800869 return 0;
870}
871
872/**
873 * smack_sb_statfs - Smack check on statfs
874 * @dentry: identifies the file system in question
875 *
876 * Returns 0 if current can read the floor of the filesystem,
877 * and error code otherwise
878 */
879static int smack_sb_statfs(struct dentry *dentry)
880{
881 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200882 int rc;
883 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800884
Eric Parisa2694342011-04-25 13:10:27 -0400885 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200886 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
887
888 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700889 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200890 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800891}
892
Casey Schauflere114e472008-02-04 22:29:50 -0800893/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800894 * BPRM hooks
895 */
896
Casey Schauflerce8a4322011-09-29 18:21:01 -0700897/**
898 * smack_bprm_set_creds - set creds for exec
899 * @bprm: the exec information
900 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100901 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700902 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800903static int smack_bprm_set_creds(struct linux_binprm *bprm)
904{
Al Viro496ad9a2013-01-23 17:07:38 -0500905 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800906 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800907 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500908 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800909 int rc;
910
Kees Cookddb4a142017-07-18 15:25:23 -0700911 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800912 return 0;
913
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800914 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300915 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800916 return 0;
917
Seth Forshee809c02e2016-04-26 14:36:22 -0500918 sbsp = inode->i_sb->s_security;
919 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
920 isp->smk_task != sbsp->smk_root)
921 return 0;
922
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300923 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100924 struct task_struct *tracer;
925 rc = 0;
926
927 rcu_read_lock();
928 tracer = ptrace_parent(current);
929 if (likely(tracer != NULL))
930 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200931 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100932 PTRACE_MODE_ATTACH,
933 __func__);
934 rcu_read_unlock();
935
936 if (rc != 0)
937 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200938 }
939 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300940 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800941
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300942 bsp->smk_task = isp->smk_task;
943 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800944
Kees Cookccbb6e12017-07-18 15:25:26 -0700945 /* Decide if this is a secure exec. */
946 if (bsp->smk_task != bsp->smk_forked)
947 bprm->secureexec = 1;
948
Casey Schaufler676dac42010-12-02 06:43:39 -0800949 return 0;
950}
951
952/*
Casey Schauflere114e472008-02-04 22:29:50 -0800953 * Inode hooks
954 */
955
956/**
957 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800958 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800959 *
luanshia1a07f22019-07-05 10:35:20 +0800960 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800961 */
962static int smack_inode_alloc_security(struct inode *inode)
963{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700964 struct smack_known *skp = smk_of_current();
965
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700966 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800967 return 0;
968}
969
970/**
Casey Schauflere114e472008-02-04 22:29:50 -0800971 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200972 * @inode: the newly created inode
973 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500974 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800975 * @name: where to put the attribute name
976 * @value: where to put the attribute value
977 * @len: where to put the length of the attribute
978 *
979 * Returns 0 if it all works out, -ENOMEM if there's no memory
980 */
981static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900982 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500983 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800984{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800985 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700986 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200987 struct smack_known *isp = smk_of_inode(inode);
988 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800989 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800990
Tetsuo Handa95489062013-07-25 05:44:02 +0900991 if (name)
992 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800993
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100994 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800995 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200996 may = smk_access_entry(skp->smk_known, dsp->smk_known,
997 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800998 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200999
1000 /*
1001 * If the access rule allows transmutation and
1002 * the directory requests transmutation then
1003 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001004 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001005 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001006 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001007 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001008 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001009 issp->smk_flags |= SMK_INODE_CHANGED;
1010 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001011
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001012 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001013 if (*value == NULL)
1014 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001015
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001016 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001017 }
Casey Schauflere114e472008-02-04 22:29:50 -08001018
1019 return 0;
1020}
1021
1022/**
1023 * smack_inode_link - Smack check on link
1024 * @old_dentry: the existing object
1025 * @dir: unused
1026 * @new_dentry: the new object
1027 *
1028 * Returns 0 if access is permitted, an error code otherwise
1029 */
1030static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1031 struct dentry *new_dentry)
1032{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001033 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001034 struct smk_audit_info ad;
1035 int rc;
1036
Eric Parisa2694342011-04-25 13:10:27 -04001037 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001038 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001039
David Howellsc6f493d2015-03-17 22:26:22 +00001040 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001041 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001042 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001043
David Howells88025652015-01-29 12:02:32 +00001044 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001045 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001046 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1047 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001048 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001049 }
1050
1051 return rc;
1052}
1053
1054/**
1055 * smack_inode_unlink - Smack check on inode deletion
1056 * @dir: containing directory object
1057 * @dentry: file to unlink
1058 *
1059 * Returns 0 if current can write the containing directory
1060 * and the object, error code otherwise
1061 */
1062static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1063{
David Howellsc6f493d2015-03-17 22:26:22 +00001064 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001065 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001066 int rc;
1067
Eric Parisa2694342011-04-25 13:10:27 -04001068 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001069 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1070
Casey Schauflere114e472008-02-04 22:29:50 -08001071 /*
1072 * You need write access to the thing you're unlinking
1073 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001074 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001075 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001076 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001077 /*
1078 * You also need write access to the containing directory
1079 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001080 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001081 smk_ad_setfield_u_fs_inode(&ad, dir);
1082 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001083 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001084 }
Casey Schauflere114e472008-02-04 22:29:50 -08001085 return rc;
1086}
1087
1088/**
1089 * smack_inode_rmdir - Smack check on directory deletion
1090 * @dir: containing directory object
1091 * @dentry: directory to unlink
1092 *
1093 * Returns 0 if current can write the containing directory
1094 * and the directory, error code otherwise
1095 */
1096static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1097{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001098 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001099 int rc;
1100
Eric Parisa2694342011-04-25 13:10:27 -04001101 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1103
Casey Schauflere114e472008-02-04 22:29:50 -08001104 /*
1105 * You need write access to the thing you're removing
1106 */
David Howellsc6f493d2015-03-17 22:26:22 +00001107 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1108 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001109 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001110 /*
1111 * You also need write access to the containing directory
1112 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001113 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001114 smk_ad_setfield_u_fs_inode(&ad, dir);
1115 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001116 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001117 }
Casey Schauflere114e472008-02-04 22:29:50 -08001118
1119 return rc;
1120}
1121
1122/**
1123 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001124 * @old_inode: unused
1125 * @old_dentry: the old object
1126 * @new_inode: unused
1127 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001128 *
1129 * Read and write access is required on both the old and
1130 * new directories.
1131 *
1132 * Returns 0 if access is permitted, an error code otherwise
1133 */
1134static int smack_inode_rename(struct inode *old_inode,
1135 struct dentry *old_dentry,
1136 struct inode *new_inode,
1137 struct dentry *new_dentry)
1138{
1139 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001140 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001141 struct smk_audit_info ad;
1142
Eric Parisa2694342011-04-25 13:10:27 -04001143 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001144 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001145
David Howellsc6f493d2015-03-17 22:26:22 +00001146 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001147 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001148 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001149
David Howells88025652015-01-29 12:02:32 +00001150 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001151 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001152 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1153 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001154 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001155 }
Casey Schauflere114e472008-02-04 22:29:50 -08001156 return rc;
1157}
1158
1159/**
1160 * smack_inode_permission - Smack version of permission()
1161 * @inode: the inode in question
1162 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001163 *
1164 * This is the important Smack hook.
1165 *
luanshia1a07f22019-07-05 10:35:20 +08001166 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001167 */
Al Viroe74f71e2011-06-20 19:38:15 -04001168static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001169{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001170 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001172 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001173 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001174
1175 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001176 /*
1177 * No permission to check. Existence test. Yup, it's there.
1178 */
1179 if (mask == 0)
1180 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001181
Seth Forshee9f50eda2015-09-23 15:16:06 -05001182 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1183 if (smk_of_inode(inode) != sbsp->smk_root)
1184 return -EACCES;
1185 }
1186
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001187 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001188 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001189 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001190 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001191 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001192 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1193 rc = smk_bu_inode(inode, mask, rc);
1194 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001195}
1196
1197/**
1198 * smack_inode_setattr - Smack check for setting attributes
1199 * @dentry: the object
1200 * @iattr: for the force flag
1201 *
1202 * Returns 0 if access is permitted, an error code otherwise
1203 */
1204static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1205{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001206 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001207 int rc;
1208
Casey Schauflere114e472008-02-04 22:29:50 -08001209 /*
1210 * Need to allow for clearing the setuid bit.
1211 */
1212 if (iattr->ia_valid & ATTR_FORCE)
1213 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001214 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001215 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001216
David Howellsc6f493d2015-03-17 22:26:22 +00001217 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1218 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001219 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001220}
1221
1222/**
1223 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001224 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001225 *
1226 * Returns 0 if access is permitted, an error code otherwise
1227 */
Al Viro3f7036a2015-03-08 19:28:30 -04001228static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001229{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001230 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001231 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001232 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001233
Eric Parisf48b7392011-04-25 12:54:27 -04001234 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001235 smk_ad_setfield_u_fs_path(&ad, *path);
1236 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1237 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001238 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001239}
1240
1241/**
1242 * smack_inode_setxattr - Smack check for setting xattrs
1243 * @dentry: the object
1244 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001245 * @value: value of the attribute
1246 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001247 * @flags: unused
1248 *
1249 * This protects the Smack attribute explicitly.
1250 *
1251 * Returns 0 if access is permitted, an error code otherwise
1252 */
David Howells8f0cfa52008-04-29 00:59:41 -07001253static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1254 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001255{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001256 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001257 struct smack_known *skp;
1258 int check_priv = 0;
1259 int check_import = 0;
1260 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001261 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001262
Casey Schaufler19760ad2013-12-16 16:27:26 -08001263 /*
1264 * Check label validity here so import won't fail in post_setxattr
1265 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001266 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1267 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001268 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1269 check_priv = 1;
1270 check_import = 1;
1271 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1272 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1273 check_priv = 1;
1274 check_import = 1;
1275 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001276 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001277 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001278 if (size != TRANS_TRUE_SIZE ||
1279 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1280 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001281 } else
1282 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1283
Casey Schaufler19760ad2013-12-16 16:27:26 -08001284 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1285 rc = -EPERM;
1286
1287 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001288 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001289 if (IS_ERR(skp))
1290 rc = PTR_ERR(skp);
1291 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001292 (skp == &smack_known_star || skp == &smack_known_web)))
1293 rc = -EINVAL;
1294 }
1295
Eric Parisa2694342011-04-25 13:10:27 -04001296 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001297 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1298
Casey Schauflerd166c802014-08-27 14:51:27 -07001299 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001300 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1301 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001302 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001303
1304 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001305}
1306
1307/**
1308 * smack_inode_post_setxattr - Apply the Smack update approved above
1309 * @dentry: object
1310 * @name: attribute name
1311 * @value: attribute value
1312 * @size: attribute size
1313 * @flags: unused
1314 *
1315 * Set the pointer in the inode blob to the entry found
1316 * in the master label list.
1317 */
David Howells8f0cfa52008-04-29 00:59:41 -07001318static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1319 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001320{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001321 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001322 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001323
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001324 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1325 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1326 return;
1327 }
1328
Casey Schaufler676dac42010-12-02 06:43:39 -08001329 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001330 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001331 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001332 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001333 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001334 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001335 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001336 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001337 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001338 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001339 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001340 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001341 }
Casey Schauflere114e472008-02-04 22:29:50 -08001342
1343 return;
1344}
1345
Casey Schauflerce8a4322011-09-29 18:21:01 -07001346/**
Casey Schauflere114e472008-02-04 22:29:50 -08001347 * smack_inode_getxattr - Smack check on getxattr
1348 * @dentry: the object
1349 * @name: unused
1350 *
1351 * Returns 0 if access is permitted, an error code otherwise
1352 */
David Howells8f0cfa52008-04-29 00:59:41 -07001353static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001354{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001355 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001356 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001357
Eric Parisa2694342011-04-25 13:10:27 -04001358 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001359 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1360
David Howellsc6f493d2015-03-17 22:26:22 +00001361 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1362 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001363 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001364}
1365
Casey Schauflerce8a4322011-09-29 18:21:01 -07001366/**
Casey Schauflere114e472008-02-04 22:29:50 -08001367 * smack_inode_removexattr - Smack check on removexattr
1368 * @dentry: the object
1369 * @name: name of the attribute
1370 *
1371 * Removing the Smack attribute requires CAP_MAC_ADMIN
1372 *
1373 * Returns 0 if access is permitted, an error code otherwise
1374 */
David Howells8f0cfa52008-04-29 00:59:41 -07001375static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001376{
Casey Schaufler676dac42010-12-02 06:43:39 -08001377 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001378 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001379 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001380
Casey Schauflerbcdca222008-02-23 15:24:04 -08001381 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1382 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001383 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001384 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001385 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301386 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001387 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001388 rc = -EPERM;
1389 } else
1390 rc = cap_inode_removexattr(dentry, name);
1391
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001392 if (rc != 0)
1393 return rc;
1394
Eric Parisa2694342011-04-25 13:10:27 -04001395 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001396 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001397
David Howellsc6f493d2015-03-17 22:26:22 +00001398 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1399 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001400 if (rc != 0)
1401 return rc;
1402
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001403 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001404 /*
1405 * Don't do anything special for these.
1406 * XATTR_NAME_SMACKIPIN
1407 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001408 */
José Bollo80124952016-01-12 21:23:40 +01001409 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001410 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001411 struct superblock_smack *sbsp = sbp->s_security;
1412
1413 isp->smk_inode = sbsp->smk_default;
1414 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001415 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001416 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001417 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001418 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1419 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001420
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001421 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001422}
1423
1424/**
1425 * smack_inode_getsecurity - get smack xattrs
1426 * @inode: the object
1427 * @name: attribute name
1428 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001429 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001430 *
1431 * Returns the size of the attribute or an error code
1432 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001433static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001434 const char *name, void **buffer,
1435 bool alloc)
1436{
1437 struct socket_smack *ssp;
1438 struct socket *sock;
1439 struct super_block *sbp;
1440 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001441 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001442
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001443 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001444 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001445 else {
1446 /*
1447 * The rest of the Smack xattrs are only on sockets.
1448 */
1449 sbp = ip->i_sb;
1450 if (sbp->s_magic != SOCKFS_MAGIC)
1451 return -EOPNOTSUPP;
1452
1453 sock = SOCKET_I(ip);
1454 if (sock == NULL || sock->sk == NULL)
1455 return -EOPNOTSUPP;
1456
1457 ssp = sock->sk->sk_security;
1458
1459 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1460 isp = ssp->smk_in;
1461 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1462 isp = ssp->smk_out;
1463 else
1464 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001465 }
1466
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001467 if (alloc) {
1468 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1469 if (*buffer == NULL)
1470 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001471 }
1472
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001473 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001474}
1475
1476
1477/**
1478 * smack_inode_listsecurity - list the Smack attributes
1479 * @inode: the object
1480 * @buffer: where they go
1481 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001482 */
1483static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1484 size_t buffer_size)
1485{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001486 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001487
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001488 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001489 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001490
1491 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001492}
1493
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001494/**
1495 * smack_inode_getsecid - Extract inode's security id
1496 * @inode: inode to extract the info from
1497 * @secid: where result will be saved
1498 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001499static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001500{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001501 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001502
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001503 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001504}
1505
Casey Schauflere114e472008-02-04 22:29:50 -08001506/*
1507 * File Hooks
1508 */
1509
Casey Schaufler491a0b02016-01-26 15:08:35 -08001510/*
1511 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001512 *
1513 * Should access checks be done on each read or write?
1514 * UNICOS and SELinux say yes.
1515 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1516 *
1517 * I'll say no for now. Smack does not do the frequent
1518 * label changing that SELinux does.
1519 */
Casey Schauflere114e472008-02-04 22:29:50 -08001520
1521/**
1522 * smack_file_alloc_security - assign a file security blob
1523 * @file: the object
1524 *
1525 * The security blob for a file is a pointer to the master
1526 * label list, so no allocation is done.
1527 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001528 * f_security is the owner security information. It
1529 * isn't used on file access checks, it's for send_sigio.
1530 *
Casey Schauflere114e472008-02-04 22:29:50 -08001531 * Returns 0
1532 */
1533static int smack_file_alloc_security(struct file *file)
1534{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001535 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001536
Casey Schauflerf28952a2018-11-12 09:38:53 -08001537 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001538 return 0;
1539}
1540
1541/**
Casey Schauflere114e472008-02-04 22:29:50 -08001542 * smack_file_ioctl - Smack check on ioctls
1543 * @file: the object
1544 * @cmd: what to do
1545 * @arg: unused
1546 *
1547 * Relies heavily on the correct use of the ioctl command conventions.
1548 *
1549 * Returns 0 if allowed, error code otherwise
1550 */
1551static int smack_file_ioctl(struct file *file, unsigned int cmd,
1552 unsigned long arg)
1553{
1554 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001555 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001556 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001557
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001558 if (unlikely(IS_PRIVATE(inode)))
1559 return 0;
1560
Eric Parisf48b7392011-04-25 12:54:27 -04001561 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001562 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001563
Casey Schauflerd166c802014-08-27 14:51:27 -07001564 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001565 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001566 rc = smk_bu_file(file, MAY_WRITE, rc);
1567 }
Casey Schauflere114e472008-02-04 22:29:50 -08001568
Casey Schauflerd166c802014-08-27 14:51:27 -07001569 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001570 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001571 rc = smk_bu_file(file, MAY_READ, rc);
1572 }
Casey Schauflere114e472008-02-04 22:29:50 -08001573
1574 return rc;
1575}
1576
1577/**
1578 * smack_file_lock - Smack check on file locking
1579 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001580 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001581 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001582 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001583 */
1584static int smack_file_lock(struct file *file, unsigned int cmd)
1585{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001586 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001587 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001588 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001589
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001590 if (unlikely(IS_PRIVATE(inode)))
1591 return 0;
1592
Eric Paris92f42502011-04-25 13:15:55 -04001593 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1594 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001595 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001596 rc = smk_bu_file(file, MAY_LOCK, rc);
1597 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001598}
1599
1600/**
1601 * smack_file_fcntl - Smack check on fcntl
1602 * @file: the object
1603 * @cmd: what action to check
1604 * @arg: unused
1605 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001606 * Generally these operations are harmless.
1607 * File locking operations present an obvious mechanism
1608 * for passing information, so they require write access.
1609 *
Casey Schauflere114e472008-02-04 22:29:50 -08001610 * Returns 0 if current has access, error code otherwise
1611 */
1612static int smack_file_fcntl(struct file *file, unsigned int cmd,
1613 unsigned long arg)
1614{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001615 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001616 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001617 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001618
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001619 if (unlikely(IS_PRIVATE(inode)))
1620 return 0;
1621
Casey Schauflere114e472008-02-04 22:29:50 -08001622 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001623 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001624 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001625 case F_SETLK:
1626 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001627 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1628 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001629 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001630 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001631 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001632 case F_SETOWN:
1633 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001634 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1635 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001636 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001637 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001638 break;
1639 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001640 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001641 }
1642
1643 return rc;
1644}
1645
1646/**
Al Viroe5467852012-05-30 13:30:51 -04001647 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001648 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1649 * if mapping anonymous memory.
1650 * @file contains the file structure for file to map (may be NULL).
1651 * @reqprot contains the protection requested by the application.
1652 * @prot contains the protection that will be applied by the kernel.
1653 * @flags contains the operational flags.
1654 * Return 0 if permission is granted.
1655 */
Al Viroe5467852012-05-30 13:30:51 -04001656static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001657 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001658 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001659{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001660 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001661 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001662 struct smack_rule *srp;
1663 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001664 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001665 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001666 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001667 int may;
1668 int mmay;
1669 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001670 int rc;
1671
Al Viro496ad9a2013-01-23 17:07:38 -05001672 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001673 return 0;
1674
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001675 if (unlikely(IS_PRIVATE(file_inode(file))))
1676 return 0;
1677
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001678 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001679 if (isp->smk_mmap == NULL)
1680 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001681 sbsp = file_inode(file)->i_sb->s_security;
1682 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1683 isp->smk_mmap != sbsp->smk_root)
1684 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001685 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001686
Casey Schauflerb17103a2018-11-09 16:12:56 -08001687 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001688 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001689 rc = 0;
1690
1691 rcu_read_lock();
1692 /*
1693 * For each Smack rule associated with the subject
1694 * label verify that the SMACK64MMAP also has access
1695 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001696 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001697 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001698 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001699 /*
1700 * Matching labels always allows access.
1701 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001702 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001703 continue;
1704 /*
1705 * If there is a matching local rule take
1706 * that into account as well.
1707 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001708 may = smk_access_entry(srp->smk_subject->smk_known,
1709 okp->smk_known,
1710 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001711 if (may == -ENOENT)
1712 may = srp->smk_access;
1713 else
1714 may &= srp->smk_access;
1715 /*
1716 * If may is zero the SMACK64MMAP subject can't
1717 * possibly have less access.
1718 */
1719 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001720 continue;
1721
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001722 /*
1723 * Fetch the global list entry.
1724 * If there isn't one a SMACK64MMAP subject
1725 * can't have as much access as current.
1726 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001727 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1728 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001729 if (mmay == -ENOENT) {
1730 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001731 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001732 }
1733 /*
1734 * If there is a local entry it modifies the
1735 * potential access, too.
1736 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001737 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1738 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001739 if (tmay != -ENOENT)
1740 mmay &= tmay;
1741
1742 /*
1743 * If there is any access available to current that is
1744 * not available to a SMACK64MMAP subject
1745 * deny access.
1746 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001747 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001748 rc = -EACCES;
1749 break;
1750 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001751 }
1752
1753 rcu_read_unlock();
1754
1755 return rc;
1756}
1757
1758/**
Casey Schauflere114e472008-02-04 22:29:50 -08001759 * smack_file_set_fowner - set the file security blob value
1760 * @file: object in question
1761 *
Casey Schauflere114e472008-02-04 22:29:50 -08001762 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001763static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001764{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001765 struct smack_known **blob = smack_file(file);
1766
1767 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001768}
1769
1770/**
1771 * smack_file_send_sigiotask - Smack on sigio
1772 * @tsk: The target task
1773 * @fown: the object the signal come from
1774 * @signum: unused
1775 *
1776 * Allow a privileged task to get signals even if it shouldn't
1777 *
1778 * Returns 0 if a subject with the object's smack could
1779 * write to the task, an error code otherwise.
1780 */
1781static int smack_file_send_sigiotask(struct task_struct *tsk,
1782 struct fown_struct *fown, int signum)
1783{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001784 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001785 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001786 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001787 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001788 struct file *file;
1789 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001790 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001791
1792 /*
1793 * struct fown_struct is never outside the context of a struct file
1794 */
1795 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001796
Etienne Bassetecfcc532009-04-08 20:40:06 +02001797 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001798 blob = smack_file(file);
1799 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001800 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1801 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001802
1803 rcu_read_lock();
1804 tcred = __task_cred(tsk);
1805 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001806 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001807 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001808
1809 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1810 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001811 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001812 return rc;
1813}
1814
1815/**
1816 * smack_file_receive - Smack file receive check
1817 * @file: the object
1818 *
1819 * Returns 0 if current has access, error code otherwise
1820 */
1821static int smack_file_receive(struct file *file)
1822{
Casey Schauflerd166c802014-08-27 14:51:27 -07001823 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001824 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001825 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001826 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001827 struct socket *sock;
1828 struct task_smack *tsp;
1829 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001830
Seung-Woo Kim97775822015-04-17 15:25:04 +09001831 if (unlikely(IS_PRIVATE(inode)))
1832 return 0;
1833
Casey Schaufler4482a442013-12-30 17:37:45 -08001834 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001835 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001836
Casey Schaufler51d59af2017-05-31 08:53:42 -07001837 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001838 sock = SOCKET_I(inode);
1839 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001840 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001841 /*
1842 * If the receiving process can't write to the
1843 * passed socket or if the passed socket can't
1844 * write to the receiving process don't accept
1845 * the passed socket.
1846 */
1847 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1848 rc = smk_bu_file(file, may, rc);
1849 if (rc < 0)
1850 return rc;
1851 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1852 rc = smk_bu_file(file, may, rc);
1853 return rc;
1854 }
Casey Schauflere114e472008-02-04 22:29:50 -08001855 /*
1856 * This code relies on bitmasks.
1857 */
1858 if (file->f_mode & FMODE_READ)
1859 may = MAY_READ;
1860 if (file->f_mode & FMODE_WRITE)
1861 may |= MAY_WRITE;
1862
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001863 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001864 rc = smk_bu_file(file, may, rc);
1865 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001866}
1867
Casey Schaufler531f1d42011-09-19 12:41:42 -07001868/**
Eric Paris83d49852012-04-04 13:45:40 -04001869 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001870 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001871 *
1872 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001873 * Allow the open only if the task has read access. There are
1874 * many read operations (e.g. fstat) that you can do with an
1875 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001876 *
luanshia1a07f22019-07-05 10:35:20 +08001877 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001878 */
Al Viro94817692018-07-10 14:13:18 -04001879static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001880{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001881 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001882 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001883 struct smk_audit_info ad;
1884 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001885
Casey Schauflera6834c02014-04-21 11:10:26 -07001886 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1887 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301888 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001889 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001890
1891 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001892}
1893
Casey Schauflere114e472008-02-04 22:29:50 -08001894/*
1895 * Task hooks
1896 */
1897
1898/**
David Howellsee18d642009-09-02 09:14:21 +01001899 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001900 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001901 * @gfp: the atomicity of any memory allocations
1902 *
1903 * Prepare a blank set of credentials for modification. This must allocate all
1904 * the memory the LSM module might require such that cred_transfer() can
1905 * complete without error.
1906 */
1907static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1908{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001909 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001910 return 0;
1911}
1912
1913
1914/**
David Howellsf1752ee2008-11-14 10:39:17 +11001915 * smack_cred_free - "free" task-level security credentials
1916 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001917 *
Casey Schauflere114e472008-02-04 22:29:50 -08001918 */
David Howellsf1752ee2008-11-14 10:39:17 +11001919static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001920{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001921 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001922 struct smack_rule *rp;
1923 struct list_head *l;
1924 struct list_head *n;
1925
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001926 smk_destroy_label_list(&tsp->smk_relabel);
1927
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001928 list_for_each_safe(l, n, &tsp->smk_rules) {
1929 rp = list_entry(l, struct smack_rule, list);
1930 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001931 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001932 }
Casey Schauflere114e472008-02-04 22:29:50 -08001933}
1934
1935/**
David Howellsd84f4f92008-11-14 10:39:23 +11001936 * smack_cred_prepare - prepare new set of credentials for modification
1937 * @new: the new credentials
1938 * @old: the original credentials
1939 * @gfp: the atomicity of any memory allocations
1940 *
1941 * Prepare a new set of credentials for modification.
1942 */
1943static int smack_cred_prepare(struct cred *new, const struct cred *old,
1944 gfp_t gfp)
1945{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001946 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001947 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001948 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001949
Casey Schauflerbbd36622018-11-12 09:30:56 -08001950 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301951
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001952 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1953 if (rc != 0)
1954 return rc;
1955
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001956 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1957 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001958 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001959}
1960
Randy Dunlap251a2a92009-02-18 11:42:33 -08001961/**
David Howellsee18d642009-09-02 09:14:21 +01001962 * smack_cred_transfer - Transfer the old credentials to the new credentials
1963 * @new: the new credentials
1964 * @old: the original credentials
1965 *
1966 * Fill in a set of blank credentials from another set of credentials.
1967 */
1968static void smack_cred_transfer(struct cred *new, const struct cred *old)
1969{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001970 struct task_smack *old_tsp = smack_cred(old);
1971 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001972
1973 new_tsp->smk_task = old_tsp->smk_task;
1974 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001975 mutex_init(&new_tsp->smk_rules_lock);
1976 INIT_LIST_HEAD(&new_tsp->smk_rules);
1977
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001978 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001979}
1980
1981/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001982 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001983 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001984 * @secid: where to put the result
1985 *
1986 * Sets the secid to contain a u32 version of the smack label.
1987 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001988static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001989{
1990 struct smack_known *skp;
1991
1992 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001993 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001994 *secid = skp->smk_secid;
1995 rcu_read_unlock();
1996}
1997
1998/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001999 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08002000 * @new: points to the set of credentials to be modified.
2001 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11002002 *
2003 * Set the security data for a kernel service.
2004 */
2005static int smack_kernel_act_as(struct cred *new, u32 secid)
2006{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002007 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002008
Casey Schaufler152f91d2016-11-14 09:38:15 -08002009 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002010 return 0;
2011}
2012
2013/**
2014 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002015 * @new: points to the set of credentials to be modified
2016 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002017 *
2018 * Set the file creation context in a set of credentials to the same
2019 * as the objective context of the specified inode
2020 */
2021static int smack_kernel_create_files_as(struct cred *new,
2022 struct inode *inode)
2023{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002024 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002025 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002026
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002027 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002028 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002029 return 0;
2030}
2031
2032/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002033 * smk_curacc_on_task - helper to log task related access
2034 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002035 * @access: the access requested
2036 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002037 *
2038 * Return 0 if access is permitted
2039 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002040static int smk_curacc_on_task(struct task_struct *p, int access,
2041 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002042{
2043 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002044 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002045 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002046
Casey Schaufler531f1d42011-09-19 12:41:42 -07002047 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002048 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002049 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002050 rc = smk_bu_task(p, access, rc);
2051 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002052}
2053
2054/**
Casey Schauflere114e472008-02-04 22:29:50 -08002055 * smack_task_setpgid - Smack check on setting pgid
2056 * @p: the task object
2057 * @pgid: unused
2058 *
2059 * Return 0 if write access is permitted
2060 */
2061static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2062{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002063 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002064}
2065
2066/**
2067 * smack_task_getpgid - Smack access check for getpgid
2068 * @p: the object task
2069 *
2070 * Returns 0 if current can read the object task, error code otherwise
2071 */
2072static int smack_task_getpgid(struct task_struct *p)
2073{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002074 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002075}
2076
2077/**
2078 * smack_task_getsid - Smack access check for getsid
2079 * @p: the object task
2080 *
2081 * Returns 0 if current can read the object task, error code otherwise
2082 */
2083static int smack_task_getsid(struct task_struct *p)
2084{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002085 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002086}
2087
2088/**
2089 * smack_task_getsecid - get the secid of the task
2090 * @p: the object task
2091 * @secid: where to put the result
2092 *
2093 * Sets the secid to contain a u32 version of the smack label.
2094 */
2095static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2096{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002097 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002098
2099 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002100}
2101
2102/**
2103 * smack_task_setnice - Smack check on setting nice
2104 * @p: the task object
2105 * @nice: unused
2106 *
2107 * Return 0 if write access is permitted
2108 */
2109static int smack_task_setnice(struct task_struct *p, int nice)
2110{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002111 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002112}
2113
2114/**
2115 * smack_task_setioprio - Smack check on setting ioprio
2116 * @p: the task object
2117 * @ioprio: unused
2118 *
2119 * Return 0 if write access is permitted
2120 */
2121static int smack_task_setioprio(struct task_struct *p, int ioprio)
2122{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002123 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002124}
2125
2126/**
2127 * smack_task_getioprio - Smack check on reading ioprio
2128 * @p: the task object
2129 *
2130 * Return 0 if read access is permitted
2131 */
2132static int smack_task_getioprio(struct task_struct *p)
2133{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002134 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002135}
2136
2137/**
2138 * smack_task_setscheduler - Smack check on setting scheduler
2139 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002140 *
2141 * Return 0 if read access is permitted
2142 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002143static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002144{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002145 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002146}
2147
2148/**
2149 * smack_task_getscheduler - Smack check on reading scheduler
2150 * @p: the task object
2151 *
2152 * Return 0 if read access is permitted
2153 */
2154static int smack_task_getscheduler(struct task_struct *p)
2155{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002156 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002157}
2158
2159/**
2160 * smack_task_movememory - Smack check on moving memory
2161 * @p: the task object
2162 *
2163 * Return 0 if write access is permitted
2164 */
2165static int smack_task_movememory(struct task_struct *p)
2166{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002167 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002168}
2169
2170/**
2171 * smack_task_kill - Smack check on signal delivery
2172 * @p: the task object
2173 * @info: unused
2174 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002175 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002176 *
2177 * Return 0 if write access is permitted
2178 *
Casey Schauflere114e472008-02-04 22:29:50 -08002179 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002180static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002181 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002182{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002183 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002184 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002185 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002186 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002187
Rafal Krypa18d872f2016-04-04 11:14:53 +02002188 if (!sig)
2189 return 0; /* null signal; existence test */
2190
Etienne Bassetecfcc532009-04-08 20:40:06 +02002191 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2192 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002193 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002194 * Sending a signal requires that the sender
2195 * can write the receiver.
2196 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002197 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002198 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2199 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002200 return rc;
2201 }
Casey Schauflere114e472008-02-04 22:29:50 -08002202 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002203 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002204 * specific behavior. This is not clean. For one thing
2205 * we can't take privilege into account.
2206 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002207 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002208 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2209 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002210 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002211}
2212
2213/**
Casey Schauflere114e472008-02-04 22:29:50 -08002214 * smack_task_to_inode - copy task smack into the inode blob
2215 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002216 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002217 *
2218 * Sets the smack pointer in the inode security blob
2219 */
2220static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2221{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002222 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002223 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002224
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002225 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002226 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002227}
2228
2229/*
2230 * Socket hooks.
2231 */
2232
2233/**
2234 * smack_sk_alloc_security - Allocate a socket blob
2235 * @sk: the socket
2236 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002237 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002238 *
2239 * Assign Smack pointers to current
2240 *
2241 * Returns 0 on success, -ENOMEM is there's no memory
2242 */
2243static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2244{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002245 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002246 struct socket_smack *ssp;
2247
2248 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2249 if (ssp == NULL)
2250 return -ENOMEM;
2251
jooseong lee08382c92016-11-03 11:54:39 +01002252 /*
2253 * Sockets created by kernel threads receive web label.
2254 */
2255 if (unlikely(current->flags & PF_KTHREAD)) {
2256 ssp->smk_in = &smack_known_web;
2257 ssp->smk_out = &smack_known_web;
2258 } else {
2259 ssp->smk_in = skp;
2260 ssp->smk_out = skp;
2261 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002262 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002263
2264 sk->sk_security = ssp;
2265
2266 return 0;
2267}
2268
2269/**
2270 * smack_sk_free_security - Free a socket blob
2271 * @sk: the socket
2272 *
2273 * Clears the blob pointer
2274 */
2275static void smack_sk_free_security(struct sock *sk)
2276{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302277#ifdef SMACK_IPV6_PORT_LABELING
2278 struct smk_port_label *spp;
2279
2280 if (sk->sk_family == PF_INET6) {
2281 rcu_read_lock();
2282 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2283 if (spp->smk_sock != sk)
2284 continue;
2285 spp->smk_can_reuse = 1;
2286 break;
2287 }
2288 rcu_read_unlock();
2289 }
2290#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002291 kfree(sk->sk_security);
2292}
2293
2294/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002295* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002296* @sip: the object end
2297*
2298* looks for host based access restrictions
2299*
2300* This version will only be appropriate for really small sets of single label
2301* hosts. The caller is responsible for ensuring that the RCU read lock is
2302* taken before calling this function.
2303*
2304* Returns the label of the far end or NULL if it's not special.
2305*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002306static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002307{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002308 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002309 struct in_addr *siap = &sip->sin_addr;
2310
2311 if (siap->s_addr == 0)
2312 return NULL;
2313
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002314 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2315 /*
2316 * we break after finding the first match because
2317 * the list is sorted from longest to shortest mask
2318 * so we have found the most specific match
2319 */
2320 if (snp->smk_host.s_addr ==
2321 (siap->s_addr & snp->smk_mask.s_addr))
2322 return snp->smk_label;
2323
2324 return NULL;
2325}
2326
2327#if IS_ENABLED(CONFIG_IPV6)
2328/*
2329 * smk_ipv6_localhost - Check for local ipv6 host address
2330 * @sip: the address
2331 *
2332 * Returns boolean true if this is the localhost address
2333 */
2334static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2335{
2336 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2337 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2338
2339 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2340 ntohs(be16p[7]) == 1)
2341 return true;
2342 return false;
2343}
2344
2345/**
2346* smack_ipv6host_label - check host based restrictions
2347* @sip: the object end
2348*
2349* looks for host based access restrictions
2350*
2351* This version will only be appropriate for really small sets of single label
2352* hosts. The caller is responsible for ensuring that the RCU read lock is
2353* taken before calling this function.
2354*
2355* Returns the label of the far end or NULL if it's not special.
2356*/
2357static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2358{
2359 struct smk_net6addr *snp;
2360 struct in6_addr *sap = &sip->sin6_addr;
2361 int i;
2362 int found = 0;
2363
2364 /*
2365 * It's local. Don't look for a host label.
2366 */
2367 if (smk_ipv6_localhost(sip))
2368 return NULL;
2369
2370 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002371 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002372 * If the label is NULL the entry has
2373 * been renounced. Ignore it.
2374 */
2375 if (snp->smk_label == NULL)
2376 continue;
2377 /*
Paul Moore07feee82009-03-27 17:10:54 -04002378 * we break after finding the first match because
2379 * the list is sorted from longest to shortest mask
2380 * so we have found the most specific match
2381 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002382 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002383 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2384 snp->smk_host.s6_addr16[i]) {
2385 found = 0;
2386 break;
2387 }
Etienne Basset43031542009-03-27 17:11:01 -04002388 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002389 if (found)
2390 return snp->smk_label;
2391 }
Paul Moore07feee82009-03-27 17:10:54 -04002392
2393 return NULL;
2394}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002395#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002396
2397/**
Casey Schauflere114e472008-02-04 22:29:50 -08002398 * smack_netlabel - Set the secattr on a socket
2399 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002400 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002401 *
2402 * Convert the outbound smack value (smk_out) to a
2403 * secattr and attach it to the socket.
2404 *
2405 * Returns 0 on success or an error code
2406 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002407static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002408{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002409 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002410 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002411 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002412
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002413 /*
2414 * Usually the netlabel code will handle changing the
2415 * packet labeling based on the label.
2416 * The case of a single label host is different, because
2417 * a single label host should never get a labeled packet
2418 * even though the label is usually associated with a packet
2419 * label.
2420 */
2421 local_bh_disable();
2422 bh_lock_sock_nested(sk);
2423
2424 if (ssp->smk_out == smack_net_ambient ||
2425 labeled == SMACK_UNLABELED_SOCKET)
2426 netlbl_sock_delattr(sk);
2427 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002428 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002429 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002430 }
2431
2432 bh_unlock_sock(sk);
2433 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002434
Casey Schauflere114e472008-02-04 22:29:50 -08002435 return rc;
2436}
2437
2438/**
Paul Moore07feee82009-03-27 17:10:54 -04002439 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2440 * @sk: the socket
2441 * @sap: the destination address
2442 *
2443 * Set the correct secattr for the given socket based on the destination
2444 * address and perform any outbound access checks needed.
2445 *
2446 * Returns 0 on success or an error code.
2447 *
2448 */
2449static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2450{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002451 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002452 int rc;
2453 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002454 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002455 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002456 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002457
2458 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002459 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002460 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002461#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002462 struct lsm_network_audit net;
2463
Eric Paris48c62af2012-04-02 13:15:44 -04002464 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2465 ad.a.u.net->family = sap->sin_family;
2466 ad.a.u.net->dport = sap->sin_port;
2467 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002468#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002469 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002470 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002471 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2472 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002473 } else {
2474 sk_lbl = SMACK_CIPSO_SOCKET;
2475 rc = 0;
2476 }
2477 rcu_read_unlock();
2478 if (rc != 0)
2479 return rc;
2480
2481 return smack_netlabel(sk, sk_lbl);
2482}
2483
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002484#if IS_ENABLED(CONFIG_IPV6)
2485/**
2486 * smk_ipv6_check - check Smack access
2487 * @subject: subject Smack label
2488 * @object: object Smack label
2489 * @address: address
2490 * @act: the action being taken
2491 *
2492 * Check an IPv6 access
2493 */
2494static int smk_ipv6_check(struct smack_known *subject,
2495 struct smack_known *object,
2496 struct sockaddr_in6 *address, int act)
2497{
2498#ifdef CONFIG_AUDIT
2499 struct lsm_network_audit net;
2500#endif
2501 struct smk_audit_info ad;
2502 int rc;
2503
2504#ifdef CONFIG_AUDIT
2505 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2506 ad.a.u.net->family = PF_INET6;
2507 ad.a.u.net->dport = ntohs(address->sin6_port);
2508 if (act == SMK_RECEIVING)
2509 ad.a.u.net->v6info.saddr = address->sin6_addr;
2510 else
2511 ad.a.u.net->v6info.daddr = address->sin6_addr;
2512#endif
2513 rc = smk_access(subject, object, MAY_WRITE, &ad);
2514 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2515 return rc;
2516}
2517#endif /* CONFIG_IPV6 */
2518
2519#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002520/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002521 * smk_ipv6_port_label - Smack port access table management
2522 * @sock: socket
2523 * @address: address
2524 *
2525 * Create or update the port list entry
2526 */
2527static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2528{
2529 struct sock *sk = sock->sk;
2530 struct sockaddr_in6 *addr6;
2531 struct socket_smack *ssp = sock->sk->sk_security;
2532 struct smk_port_label *spp;
2533 unsigned short port = 0;
2534
2535 if (address == NULL) {
2536 /*
2537 * This operation is changing the Smack information
2538 * on the bound socket. Take the changes to the port
2539 * as well.
2540 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302541 rcu_read_lock();
2542 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002543 if (sk != spp->smk_sock)
2544 continue;
2545 spp->smk_in = ssp->smk_in;
2546 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302547 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002548 return;
2549 }
2550 /*
2551 * A NULL address is only used for updating existing
2552 * bound entries. If there isn't one, it's OK.
2553 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302554 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002555 return;
2556 }
2557
2558 addr6 = (struct sockaddr_in6 *)address;
2559 port = ntohs(addr6->sin6_port);
2560 /*
2561 * This is a special case that is safely ignored.
2562 */
2563 if (port == 0)
2564 return;
2565
2566 /*
2567 * Look for an existing port list entry.
2568 * This is an indication that a port is getting reused.
2569 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302570 rcu_read_lock();
2571 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302572 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002573 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302574 if (spp->smk_can_reuse != 1) {
2575 rcu_read_unlock();
2576 return;
2577 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002578 spp->smk_port = port;
2579 spp->smk_sock = sk;
2580 spp->smk_in = ssp->smk_in;
2581 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302582 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302583 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002584 return;
2585 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302586 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002587 /*
2588 * A new port entry is required.
2589 */
2590 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2591 if (spp == NULL)
2592 return;
2593
2594 spp->smk_port = port;
2595 spp->smk_sock = sk;
2596 spp->smk_in = ssp->smk_in;
2597 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302598 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302599 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002600
Vishal Goel3c7ce342016-11-23 10:31:08 +05302601 mutex_lock(&smack_ipv6_lock);
2602 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2603 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002604 return;
2605}
2606
2607/**
2608 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002609 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002610 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002611 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002612 *
2613 * Create or update the port list entry
2614 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002615static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002616 int act)
2617{
Casey Schauflerc6739442013-05-22 18:42:56 -07002618 struct smk_port_label *spp;
2619 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002620 struct smack_known *skp = NULL;
2621 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002622 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002623
2624 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002625 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002626 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002627 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002628 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002629 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002630 }
2631
2632 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002633 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002634 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002635 if (skp != NULL && object != NULL)
2636 return smk_ipv6_check(skp, object, address, act);
2637 if (skp == NULL)
2638 skp = smack_net_ambient;
2639 if (object == NULL)
2640 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002641
2642 /*
2643 * It's remote, so port lookup does no good.
2644 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002645 if (!smk_ipv6_localhost(address))
2646 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002647
2648 /*
2649 * It's local so the send check has to have passed.
2650 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002651 if (act == SMK_RECEIVING)
2652 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002653
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002654 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302655 rcu_read_lock();
2656 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302657 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002658 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002659 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002660 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002661 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002662 break;
2663 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302664 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002665
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002666 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002667}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002668#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002669
2670/**
Casey Schauflere114e472008-02-04 22:29:50 -08002671 * smack_inode_setsecurity - set smack xattrs
2672 * @inode: the object
2673 * @name: attribute name
2674 * @value: attribute value
2675 * @size: size of the attribute
2676 * @flags: unused
2677 *
2678 * Sets the named attribute in the appropriate blob
2679 *
2680 * Returns 0 on success, or an error code
2681 */
2682static int smack_inode_setsecurity(struct inode *inode, const char *name,
2683 const void *value, size_t size, int flags)
2684{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002685 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002686 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002687 struct socket_smack *ssp;
2688 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002689 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002690
Casey Schauflerf7112e62012-05-06 15:22:02 -07002691 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302692 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002693
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002694 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002695 if (IS_ERR(skp))
2696 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002697
2698 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002699 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002700 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002701 return 0;
2702 }
2703 /*
2704 * The rest of the Smack xattrs are only on sockets.
2705 */
2706 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2707 return -EOPNOTSUPP;
2708
2709 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002710 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002711 return -EOPNOTSUPP;
2712
2713 ssp = sock->sk->sk_security;
2714
2715 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002716 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002717 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002718 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002719 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002720 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2721 if (rc != 0)
2722 printk(KERN_WARNING
2723 "Smack: \"%s\" netlbl error %d.\n",
2724 __func__, -rc);
2725 }
Casey Schauflere114e472008-02-04 22:29:50 -08002726 } else
2727 return -EOPNOTSUPP;
2728
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002729#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002730 if (sock->sk->sk_family == PF_INET6)
2731 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002732#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002733
Casey Schauflere114e472008-02-04 22:29:50 -08002734 return 0;
2735}
2736
2737/**
2738 * smack_socket_post_create - finish socket setup
2739 * @sock: the socket
2740 * @family: protocol family
2741 * @type: unused
2742 * @protocol: unused
2743 * @kern: unused
2744 *
2745 * Sets the netlabel information on the socket
2746 *
2747 * Returns 0 on success, and error code otherwise
2748 */
2749static int smack_socket_post_create(struct socket *sock, int family,
2750 int type, int protocol, int kern)
2751{
Marcin Lis74123012015-01-22 15:40:33 +01002752 struct socket_smack *ssp;
2753
2754 if (sock->sk == NULL)
2755 return 0;
2756
2757 /*
2758 * Sockets created by kernel threads receive web label.
2759 */
2760 if (unlikely(current->flags & PF_KTHREAD)) {
2761 ssp = sock->sk->sk_security;
2762 ssp->smk_in = &smack_known_web;
2763 ssp->smk_out = &smack_known_web;
2764 }
2765
2766 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002767 return 0;
2768 /*
2769 * Set the outbound netlbl.
2770 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002771 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2772}
2773
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002774/**
2775 * smack_socket_socketpair - create socket pair
2776 * @socka: one socket
2777 * @sockb: another socket
2778 *
2779 * Cross reference the peer labels for SO_PEERSEC
2780 *
luanshia1a07f22019-07-05 10:35:20 +08002781 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002782 */
2783static int smack_socket_socketpair(struct socket *socka,
2784 struct socket *sockb)
2785{
2786 struct socket_smack *asp = socka->sk->sk_security;
2787 struct socket_smack *bsp = sockb->sk->sk_security;
2788
2789 asp->smk_packet = bsp->smk_out;
2790 bsp->smk_packet = asp->smk_out;
2791
2792 return 0;
2793}
2794
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002795#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002796/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002797 * smack_socket_bind - record port binding information.
2798 * @sock: the socket
2799 * @address: the port address
2800 * @addrlen: size of the address
2801 *
2802 * Records the label bound to a port.
2803 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002804 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002805 */
2806static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2807 int addrlen)
2808{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002809 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2810 if (addrlen < SIN6_LEN_RFC2133 ||
2811 address->sa_family != AF_INET6)
2812 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002813 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002814 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002815 return 0;
2816}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002817#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002818
2819/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002820 * smack_socket_connect - connect access check
2821 * @sock: the socket
2822 * @sap: the other end
2823 * @addrlen: size of sap
2824 *
2825 * Verifies that a connection may be possible
2826 *
2827 * Returns 0 on success, and error code otherwise
2828 */
2829static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2830 int addrlen)
2831{
Casey Schauflerc6739442013-05-22 18:42:56 -07002832 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002833#if IS_ENABLED(CONFIG_IPV6)
2834 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2835#endif
2836#ifdef SMACK_IPV6_SECMARK_LABELING
2837 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002838 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002839#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002840
Casey Schauflerc6739442013-05-22 18:42:56 -07002841 if (sock->sk == NULL)
2842 return 0;
2843
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002844#ifdef SMACK_IPV6_SECMARK_LABELING
2845 ssp = sock->sk->sk_security;
2846#endif
2847
Casey Schauflerc6739442013-05-22 18:42:56 -07002848 switch (sock->sk->sk_family) {
2849 case PF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002850 if (addrlen < sizeof(struct sockaddr_in) ||
2851 sap->sa_family != AF_INET)
Casey Schauflerc6739442013-05-22 18:42:56 -07002852 return -EINVAL;
2853 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2854 break;
2855 case PF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002856 if (addrlen < SIN6_LEN_RFC2133 || sap->sa_family != AF_INET6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002857 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002858#ifdef SMACK_IPV6_SECMARK_LABELING
2859 rsp = smack_ipv6host_label(sip);
2860 if (rsp != NULL)
2861 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002862 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002863#endif
2864#ifdef SMACK_IPV6_PORT_LABELING
2865 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2866#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002867 break;
2868 }
2869 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002870}
2871
2872/**
2873 * smack_flags_to_may - convert S_ to MAY_ values
2874 * @flags: the S_ value
2875 *
2876 * Returns the equivalent MAY_ value
2877 */
2878static int smack_flags_to_may(int flags)
2879{
2880 int may = 0;
2881
2882 if (flags & S_IRUGO)
2883 may |= MAY_READ;
2884 if (flags & S_IWUGO)
2885 may |= MAY_WRITE;
2886 if (flags & S_IXUGO)
2887 may |= MAY_EXEC;
2888
2889 return may;
2890}
2891
2892/**
2893 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2894 * @msg: the object
2895 *
2896 * Returns 0
2897 */
2898static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2899{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002900 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002901
Casey Schauflerecd5f822018-11-20 11:55:02 -08002902 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002903 return 0;
2904}
2905
2906/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002907 * smack_of_ipc - the smack pointer for the ipc
2908 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002909 *
2910 * Returns a pointer to the smack value
2911 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002912static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002913{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002914 struct smack_known **blob = smack_ipc(isp);
2915
2916 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002917}
2918
2919/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002920 * smack_ipc_alloc_security - Set the security blob for ipc
2921 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002922 *
2923 * Returns 0
2924 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002925static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002926{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002927 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002928
Casey Schaufler019bcca2018-09-21 17:19:54 -07002929 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002930 return 0;
2931}
2932
2933/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002934 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002935 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002936 * @access : access requested
2937 *
2938 * Returns 0 if current has the requested access, error code otherwise
2939 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002940static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002941{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002942 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002943 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002944 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002945
2946#ifdef CONFIG_AUDIT
2947 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002948 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002949#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002950 rc = smk_curacc(ssp, access, &ad);
2951 rc = smk_bu_current("shm", ssp, access, rc);
2952 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002953}
2954
2955/**
Casey Schauflere114e472008-02-04 22:29:50 -08002956 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002957 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002958 * @shmflg: access requested
2959 *
2960 * Returns 0 if current has the requested access, error code otherwise
2961 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002962static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002963{
Casey Schauflere114e472008-02-04 22:29:50 -08002964 int may;
2965
2966 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002967 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002968}
2969
2970/**
2971 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002972 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002973 * @cmd: what it wants to do
2974 *
2975 * Returns 0 if current has the requested access, error code otherwise
2976 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002977static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002978{
Casey Schauflere114e472008-02-04 22:29:50 -08002979 int may;
2980
2981 switch (cmd) {
2982 case IPC_STAT:
2983 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002984 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002985 may = MAY_READ;
2986 break;
2987 case IPC_SET:
2988 case SHM_LOCK:
2989 case SHM_UNLOCK:
2990 case IPC_RMID:
2991 may = MAY_READWRITE;
2992 break;
2993 case IPC_INFO:
2994 case SHM_INFO:
2995 /*
2996 * System level information.
2997 */
2998 return 0;
2999 default:
3000 return -EINVAL;
3001 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003002 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003003}
3004
3005/**
3006 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003007 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003008 * @shmaddr: unused
3009 * @shmflg: access requested
3010 *
3011 * Returns 0 if current has the requested access, error code otherwise
3012 */
luanshia1a07f22019-07-05 10:35:20 +08003013static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003014 int shmflg)
3015{
Casey Schauflere114e472008-02-04 22:29:50 -08003016 int may;
3017
3018 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003019 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003020}
3021
3022/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003023 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003024 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003025 * @access : access requested
3026 *
3027 * Returns 0 if current has the requested access, error code otherwise
3028 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003029static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003030{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003031 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003033 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003034
3035#ifdef CONFIG_AUDIT
3036 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003037 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003038#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003039 rc = smk_curacc(ssp, access, &ad);
3040 rc = smk_bu_current("sem", ssp, access, rc);
3041 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003042}
3043
3044/**
Casey Schauflere114e472008-02-04 22:29:50 -08003045 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003046 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003047 * @semflg: access requested
3048 *
3049 * Returns 0 if current has the requested access, error code otherwise
3050 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003051static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003052{
Casey Schauflere114e472008-02-04 22:29:50 -08003053 int may;
3054
3055 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003056 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003057}
3058
3059/**
3060 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003061 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003062 * @cmd: what it wants to do
3063 *
3064 * Returns 0 if current has the requested access, error code otherwise
3065 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003066static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003067{
Casey Schauflere114e472008-02-04 22:29:50 -08003068 int may;
3069
3070 switch (cmd) {
3071 case GETPID:
3072 case GETNCNT:
3073 case GETZCNT:
3074 case GETVAL:
3075 case GETALL:
3076 case IPC_STAT:
3077 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003078 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003079 may = MAY_READ;
3080 break;
3081 case SETVAL:
3082 case SETALL:
3083 case IPC_RMID:
3084 case IPC_SET:
3085 may = MAY_READWRITE;
3086 break;
3087 case IPC_INFO:
3088 case SEM_INFO:
3089 /*
3090 * System level information
3091 */
3092 return 0;
3093 default:
3094 return -EINVAL;
3095 }
3096
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003097 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003098}
3099
3100/**
3101 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003102 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003103 * @sops: unused
3104 * @nsops: unused
3105 * @alter: unused
3106 *
3107 * Treated as read and write in all cases.
3108 *
3109 * Returns 0 if access is allowed, error code otherwise
3110 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003111static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003112 unsigned nsops, int alter)
3113{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003114 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003115}
3116
3117/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003118 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003120 * @access : access requested
3121 *
3122 * return 0 if current has access, error otherwise
3123 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003124static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003125{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003126 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003127 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003128 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003129
3130#ifdef CONFIG_AUDIT
3131 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003132 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003133#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003134 rc = smk_curacc(msp, access, &ad);
3135 rc = smk_bu_current("msq", msp, access, rc);
3136 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003137}
3138
3139/**
Casey Schauflere114e472008-02-04 22:29:50 -08003140 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003141 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003142 * @msqflg: access requested
3143 *
3144 * Returns 0 if current has the requested access, error code otherwise
3145 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003146static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003147{
Casey Schauflere114e472008-02-04 22:29:50 -08003148 int may;
3149
3150 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003151 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003152}
3153
3154/**
3155 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003156 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003157 * @cmd: what it wants to do
3158 *
3159 * Returns 0 if current has the requested access, error code otherwise
3160 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003161static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003162{
Casey Schauflere114e472008-02-04 22:29:50 -08003163 int may;
3164
3165 switch (cmd) {
3166 case IPC_STAT:
3167 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003168 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003169 may = MAY_READ;
3170 break;
3171 case IPC_SET:
3172 case IPC_RMID:
3173 may = MAY_READWRITE;
3174 break;
3175 case IPC_INFO:
3176 case MSG_INFO:
3177 /*
3178 * System level information
3179 */
3180 return 0;
3181 default:
3182 return -EINVAL;
3183 }
3184
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003185 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003186}
3187
3188/**
3189 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003190 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003191 * @msg: unused
3192 * @msqflg: access requested
3193 *
3194 * Returns 0 if current has the requested access, error code otherwise
3195 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003196static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003197 int msqflg)
3198{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003199 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003200
Etienne Bassetecfcc532009-04-08 20:40:06 +02003201 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003202 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003203}
3204
3205/**
3206 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003207 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003208 * @msg: unused
3209 * @target: unused
3210 * @type: unused
3211 * @mode: unused
3212 *
3213 * Returns 0 if current has read and write access, error code otherwise
3214 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003215static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003216 struct task_struct *target, long type, int mode)
3217{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003218 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003219}
3220
3221/**
3222 * smack_ipc_permission - Smack access for ipc_permission()
3223 * @ipp: the object permissions
3224 * @flag: access requested
3225 *
3226 * Returns 0 if current has read and write access, error code otherwise
3227 */
3228static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3229{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003230 struct smack_known **blob = smack_ipc(ipp);
3231 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003232 int may = smack_flags_to_may(flag);
3233 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003234 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003235
Etienne Bassetecfcc532009-04-08 20:40:06 +02003236#ifdef CONFIG_AUDIT
3237 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3238 ad.a.u.ipc_id = ipp->id;
3239#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003240 rc = smk_curacc(iskp, may, &ad);
3241 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003242 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003243}
3244
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003245/**
3246 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003247 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003248 * @secid: where result will be saved
3249 */
3250static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3251{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003252 struct smack_known **blob = smack_ipc(ipp);
3253 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003254
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003255 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003256}
3257
Casey Schauflere114e472008-02-04 22:29:50 -08003258/**
3259 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003260 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003261 * @inode: the object
3262 *
3263 * Set the inode's security blob if it hasn't been done already.
3264 */
3265static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3266{
3267 struct super_block *sbp;
3268 struct superblock_smack *sbsp;
3269 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003270 struct smack_known *skp;
3271 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003272 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003273 char trattr[TRANS_TRUE_SIZE];
3274 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003275 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003276 struct dentry *dp;
3277
3278 if (inode == NULL)
3279 return;
3280
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003281 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003282
3283 mutex_lock(&isp->smk_lock);
3284 /*
3285 * If the inode is already instantiated
3286 * take the quick way out
3287 */
3288 if (isp->smk_flags & SMK_INODE_INSTANT)
3289 goto unlockandout;
3290
3291 sbp = inode->i_sb;
3292 sbsp = sbp->s_security;
3293 /*
3294 * We're going to use the superblock default label
3295 * if there's no label on the file.
3296 */
3297 final = sbsp->smk_default;
3298
3299 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003300 * If this is the root inode the superblock
3301 * may be in the process of initialization.
3302 * If that is the case use the root value out
3303 * of the superblock.
3304 */
3305 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003306 switch (sbp->s_magic) {
3307 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003308 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003309 /*
3310 * The cgroup filesystem is never mounted,
3311 * so there's no opportunity to set the mount
3312 * options.
3313 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003314 sbsp->smk_root = &smack_known_star;
3315 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003316 isp->smk_inode = sbsp->smk_root;
3317 break;
3318 case TMPFS_MAGIC:
3319 /*
3320 * What about shmem/tmpfs anonymous files with dentry
3321 * obtained from d_alloc_pseudo()?
3322 */
3323 isp->smk_inode = smk_of_current();
3324 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003325 case PIPEFS_MAGIC:
3326 isp->smk_inode = smk_of_current();
3327 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003328 case SOCKFS_MAGIC:
3329 /*
3330 * Socket access is controlled by the socket
3331 * structures associated with the task involved.
3332 */
3333 isp->smk_inode = &smack_known_star;
3334 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003335 default:
3336 isp->smk_inode = sbsp->smk_root;
3337 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003338 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003339 isp->smk_flags |= SMK_INODE_INSTANT;
3340 goto unlockandout;
3341 }
3342
3343 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003344 * This is pretty hackish.
3345 * Casey says that we shouldn't have to do
3346 * file system specific code, but it does help
3347 * with keeping it simple.
3348 */
3349 switch (sbp->s_magic) {
3350 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003351 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003352 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003353 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003354 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003355 * that the smack file system doesn't do
3356 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003357 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003358 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003359 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003360 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003361 break;
3362 case DEVPTS_SUPER_MAGIC:
3363 /*
3364 * devpts seems content with the label of the task.
3365 * Programs that change smack have to treat the
3366 * pty with respect.
3367 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003368 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003369 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003370 case PROC_SUPER_MAGIC:
3371 /*
3372 * Casey says procfs appears not to care.
3373 * The superblock default suffices.
3374 */
3375 break;
3376 case TMPFS_MAGIC:
3377 /*
3378 * Device labels should come from the filesystem,
3379 * but watch out, because they're volitile,
3380 * getting recreated on every reboot.
3381 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003382 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003383 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003384 * If a smack value has been set we want to use it,
3385 * but since tmpfs isn't giving us the opportunity
3386 * to set mount options simulate setting the
3387 * superblock default.
3388 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003389 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003390 default:
3391 /*
3392 * This isn't an understood special case.
3393 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003394 */
3395
3396 /*
3397 * UNIX domain sockets use lower level socket data.
3398 */
3399 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003400 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003401 break;
3402 }
3403 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003404 * No xattr support means, alas, no SMACK label.
3405 * Use the aforeapplied default.
3406 * It would be curious if the label of the task
3407 * does not match that assigned.
3408 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003409 if (!(inode->i_opflags & IOP_XATTR))
3410 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003411 /*
3412 * Get the dentry for xattr.
3413 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003414 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003415 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003416 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003417 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003418
3419 /*
3420 * Transmuting directory
3421 */
3422 if (S_ISDIR(inode->i_mode)) {
3423 /*
3424 * If this is a new directory and the label was
3425 * transmuted when the inode was initialized
3426 * set the transmute attribute on the directory
3427 * and mark the inode.
3428 *
3429 * If there is a transmute attribute on the
3430 * directory mark the inode.
3431 */
3432 if (isp->smk_flags & SMK_INODE_CHANGED) {
3433 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003434 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003435 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003436 TRANS_TRUE, TRANS_TRUE_SIZE,
3437 0);
3438 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003439 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003440 XATTR_NAME_SMACKTRANSMUTE, trattr,
3441 TRANS_TRUE_SIZE);
3442 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3443 TRANS_TRUE_SIZE) != 0)
3444 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003445 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003446 if (rc >= 0)
3447 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003448 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003449 /*
3450 * Don't let the exec or mmap label be "*" or "@".
3451 */
3452 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3453 if (IS_ERR(skp) || skp == &smack_known_star ||
3454 skp == &smack_known_web)
3455 skp = NULL;
3456 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003457
Casey Schaufler19760ad2013-12-16 16:27:26 -08003458 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003459 if (IS_ERR(skp) || skp == &smack_known_star ||
3460 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003461 skp = NULL;
3462 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003463
Casey Schauflere114e472008-02-04 22:29:50 -08003464 dput(dp);
3465 break;
3466 }
3467
3468 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003469 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003470 else
3471 isp->smk_inode = final;
3472
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003473 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003474
3475unlockandout:
3476 mutex_unlock(&isp->smk_lock);
3477 return;
3478}
3479
3480/**
3481 * smack_getprocattr - Smack process attribute access
3482 * @p: the object task
3483 * @name: the name of the attribute in /proc/.../attr
3484 * @value: where to put the result
3485 *
3486 * Places a copy of the task Smack into value
3487 *
3488 * Returns the length of the smack label or an error code
3489 */
3490static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3491{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003492 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003493 char *cp;
3494 int slen;
3495
3496 if (strcmp(name, "current") != 0)
3497 return -EINVAL;
3498
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003499 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003500 if (cp == NULL)
3501 return -ENOMEM;
3502
3503 slen = strlen(cp);
3504 *value = cp;
3505 return slen;
3506}
3507
3508/**
3509 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003510 * @name: the name of the attribute in /proc/.../attr
3511 * @value: the value to set
3512 * @size: the size of the value
3513 *
3514 * Sets the Smack value of the task. Only setting self
3515 * is permitted and only with privilege
3516 *
3517 * Returns the length of the smack label or an error code
3518 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003519static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003520{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003521 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003522 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003523 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003524 struct smack_known_list_elem *sklep;
3525 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003526
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003527 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003528 return -EPERM;
3529
Casey Schauflerf7112e62012-05-06 15:22:02 -07003530 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003531 return -EINVAL;
3532
3533 if (strcmp(name, "current") != 0)
3534 return -EINVAL;
3535
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003536 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003537 if (IS_ERR(skp))
3538 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003539
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003540 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303541 * No process is ever allowed the web ("@") label
3542 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003543 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303544 if (skp == &smack_known_web || skp == &smack_known_star)
3545 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003546
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003547 if (!smack_privileged(CAP_MAC_ADMIN)) {
3548 rc = -EPERM;
3549 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3550 if (sklep->smk_label == skp) {
3551 rc = 0;
3552 break;
3553 }
3554 if (rc)
3555 return rc;
3556 }
3557
David Howellsd84f4f92008-11-14 10:39:23 +11003558 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003559 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003560 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003561
Casey Schauflerb17103a2018-11-09 16:12:56 -08003562 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003563 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003564 /*
3565 * process can change its label only once
3566 */
3567 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003568
David Howellsd84f4f92008-11-14 10:39:23 +11003569 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003570 return size;
3571}
3572
3573/**
3574 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003575 * @sock: one sock
3576 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003577 * @newsk: unused
3578 *
3579 * Return 0 if a subject with the smack of sock could access
3580 * an object with the smack of other, otherwise an error code
3581 */
David S. Miller3610cda2011-01-05 15:38:53 -08003582static int smack_unix_stream_connect(struct sock *sock,
3583 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003584{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003585 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003586 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003587 struct socket_smack *ssp = sock->sk_security;
3588 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003589 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003590 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003591 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003592#ifdef CONFIG_AUDIT
3593 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003594#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003595
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003596 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3597 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003598 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003599#ifdef CONFIG_AUDIT
3600 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3601 smk_ad_setfield_u_net_sk(&ad, other);
3602#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003603 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3604 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003605 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003606 okp = osp->smk_out;
3607 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003608 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003609 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003610 MAY_WRITE, rc);
3611 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003612 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003613
Casey Schaufler975d5e52011-09-26 14:43:39 -07003614 /*
3615 * Cross reference the peer labels for SO_PEERSEC.
3616 */
3617 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003618 nsp->smk_packet = ssp->smk_out;
3619 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003620 }
3621
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003622 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003623}
3624
3625/**
3626 * smack_unix_may_send - Smack access on UDS
3627 * @sock: one socket
3628 * @other: the other socket
3629 *
3630 * Return 0 if a subject with the smack of sock could access
3631 * an object with the smack of other, otherwise an error code
3632 */
3633static int smack_unix_may_send(struct socket *sock, struct socket *other)
3634{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003635 struct socket_smack *ssp = sock->sk->sk_security;
3636 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003637 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003638 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003639
Kees Cook923e9a12012-04-10 13:26:44 -07003640#ifdef CONFIG_AUDIT
3641 struct lsm_network_audit net;
3642
Eric Paris48c62af2012-04-02 13:15:44 -04003643 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003644 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003645#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003646
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003647 if (smack_privileged(CAP_MAC_OVERRIDE))
3648 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003649
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003650 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3651 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003652 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003653}
3654
3655/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003656 * smack_socket_sendmsg - Smack check based on destination host
3657 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003658 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003659 * @size: the size of the message
3660 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003661 * Return 0 if the current subject can write to the destination host.
3662 * For IPv4 this is only a question if the destination is a single label host.
3663 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003664 */
3665static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3666 int size)
3667{
3668 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003669#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003670 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003671#endif
3672#ifdef SMACK_IPV6_SECMARK_LABELING
3673 struct socket_smack *ssp = sock->sk->sk_security;
3674 struct smack_known *rsp;
3675#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003676 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003677
3678 /*
3679 * Perfectly reasonable for this to be NULL
3680 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003681 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003682 return 0;
3683
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003684 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003685 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003686 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3687 sip->sin_family != AF_INET)
3688 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003689 rc = smack_netlabel_send(sock->sk, sip);
3690 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003691#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003692 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003693 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3694 sap->sin6_family != AF_INET6)
3695 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003696#ifdef SMACK_IPV6_SECMARK_LABELING
3697 rsp = smack_ipv6host_label(sap);
3698 if (rsp != NULL)
3699 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3700 SMK_CONNECTING);
3701#endif
3702#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003703 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003704#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003705#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003706 break;
3707 }
3708 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003709}
3710
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003711/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003712 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003713 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003714 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003715 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003716 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003717 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003718static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3719 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003720{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003721 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003722 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003723 int acat;
3724 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003725
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003726 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003727 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003728 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003729 * If there are flags but no level netlabel isn't
3730 * behaving the way we expect it to.
3731 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003732 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003733 * Without guidance regarding the smack value
3734 * for the packet fall back on the network
3735 * ambient value.
3736 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003737 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303738 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003739 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003740 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003741 /*
3742 * Compare the catsets. Use the netlbl APIs.
3743 */
3744 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3745 if ((skp->smk_netlabel.flags &
3746 NETLBL_SECATTR_MLS_CAT) == 0)
3747 found = 1;
3748 break;
3749 }
3750 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003751 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3752 acat + 1);
3753 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003754 skp->smk_netlabel.attr.mls.cat,
3755 kcat + 1);
3756 if (acat < 0 || kcat < 0)
3757 break;
3758 }
3759 if (acat == kcat) {
3760 found = 1;
3761 break;
3762 }
Casey Schauflere114e472008-02-04 22:29:50 -08003763 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003764 rcu_read_unlock();
3765
3766 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003767 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003768
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003769 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003770 return &smack_known_web;
3771 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003772 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003773 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003774 /*
3775 * Looks like a fallback, which gives us a secid.
3776 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003777 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003778 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003779 * Without guidance regarding the smack value
3780 * for the packet fall back on the network
3781 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003782 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003783 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003784}
3785
Casey Schaufler69f287a2014-12-12 17:08:40 -08003786#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003787static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003788{
Casey Schauflerc6739442013-05-22 18:42:56 -07003789 u8 nexthdr;
3790 int offset;
3791 int proto = -EINVAL;
3792 struct ipv6hdr _ipv6h;
3793 struct ipv6hdr *ip6;
3794 __be16 frag_off;
3795 struct tcphdr _tcph, *th;
3796 struct udphdr _udph, *uh;
3797 struct dccp_hdr _dccph, *dh;
3798
3799 sip->sin6_port = 0;
3800
3801 offset = skb_network_offset(skb);
3802 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3803 if (ip6 == NULL)
3804 return -EINVAL;
3805 sip->sin6_addr = ip6->saddr;
3806
3807 nexthdr = ip6->nexthdr;
3808 offset += sizeof(_ipv6h);
3809 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3810 if (offset < 0)
3811 return -EINVAL;
3812
3813 proto = nexthdr;
3814 switch (proto) {
3815 case IPPROTO_TCP:
3816 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3817 if (th != NULL)
3818 sip->sin6_port = th->source;
3819 break;
3820 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003821 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003822 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3823 if (uh != NULL)
3824 sip->sin6_port = uh->source;
3825 break;
3826 case IPPROTO_DCCP:
3827 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3828 if (dh != NULL)
3829 sip->sin6_port = dh->dccph_sport;
3830 break;
3831 }
3832 return proto;
3833}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003834#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003835
Casey Schauflere114e472008-02-04 22:29:50 -08003836/**
3837 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3838 * @sk: socket
3839 * @skb: packet
3840 *
3841 * Returns 0 if the packet should be delivered, an error code otherwise
3842 */
3843static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3844{
3845 struct netlbl_lsm_secattr secattr;
3846 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003847 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003848 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003849 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003850 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003851#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003852 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003853#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003854#if IS_ENABLED(CONFIG_IPV6)
3855 struct sockaddr_in6 sadd;
3856 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003857
3858 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3859 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003860#endif /* CONFIG_IPV6 */
3861
Piotr Sawicki129a9982018-07-19 11:42:58 +02003862 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003863 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003864#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3865 /*
3866 * If there is a secmark use it rather than the CIPSO label.
3867 * If there is no secmark fall back to CIPSO.
3868 * The secmark is assumed to reflect policy better.
3869 */
3870 if (skb && skb->secmark != 0) {
3871 skp = smack_from_secid(skb->secmark);
3872 goto access_check;
3873 }
3874#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003875 /*
3876 * Translate what netlabel gave us.
3877 */
3878 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003879
Piotr Sawicki129a9982018-07-19 11:42:58 +02003880 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003881 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003882 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003883 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003884 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003885
Casey Schauflerc6739442013-05-22 18:42:56 -07003886 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003887
Casey Schaufler69f287a2014-12-12 17:08:40 -08003888#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3889access_check:
3890#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003891#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003892 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003893 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003894 ad.a.u.net->netif = skb->skb_iif;
3895 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003896#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003897 /*
3898 * Receiving a packet requires that the other end
3899 * be able to write here. Read access is not required.
3900 * This is the simplist possible security model
3901 * for networking.
3902 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003903 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3904 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003905 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003906 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003907 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003908 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003909#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003910 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003911 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003912 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3913 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003914 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003915#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916 if (skb && skb->secmark != 0)
3917 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003918 else if (smk_ipv6_localhost(&sadd))
3919 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003920 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003921 skp = smack_ipv6host_label(&sadd);
3922 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003923 skp = smack_net_ambient;
Jia-Ju Bai3f4287e2019-07-23 18:00:15 +08003924 if (skb == NULL)
3925 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003926#ifdef CONFIG_AUDIT
3927 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003928 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003929 ad.a.u.net->netif = skb->skb_iif;
3930 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3931#endif /* CONFIG_AUDIT */
3932 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3933 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3934 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003935#endif /* SMACK_IPV6_SECMARK_LABELING */
3936#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003937 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003938#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003939 if (rc != 0)
3940 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3941 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003942 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003943#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003944 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003945
Paul Moorea8134292008-10-10 10:16:31 -04003946 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003947}
3948
3949/**
3950 * smack_socket_getpeersec_stream - pull in packet label
3951 * @sock: the socket
3952 * @optval: user's destination
3953 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003954 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003955 *
3956 * returns zero on success, an error code otherwise
3957 */
3958static int smack_socket_getpeersec_stream(struct socket *sock,
3959 char __user *optval,
3960 int __user *optlen, unsigned len)
3961{
3962 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003963 char *rcp = "";
3964 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003965 int rc = 0;
3966
3967 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003968 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003969 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003970 slen = strlen(rcp) + 1;
3971 }
Casey Schauflere114e472008-02-04 22:29:50 -08003972
3973 if (slen > len)
3974 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003975 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003976 rc = -EFAULT;
3977
3978 if (put_user(slen, optlen) != 0)
3979 rc = -EFAULT;
3980
3981 return rc;
3982}
3983
3984
3985/**
3986 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003987 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003988 * @skb: packet data
3989 * @secid: pointer to where to put the secid of the packet
3990 *
3991 * Sets the netlabel socket state on sk from parent
3992 */
3993static int smack_socket_getpeersec_dgram(struct socket *sock,
3994 struct sk_buff *skb, u32 *secid)
3995
3996{
3997 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003998 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003999 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004000 int family = PF_UNSPEC;
4001 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08004002 int rc;
4003
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004004 if (skb != NULL) {
4005 if (skb->protocol == htons(ETH_P_IP))
4006 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004007#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004008 else if (skb->protocol == htons(ETH_P_IPV6))
4009 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004010#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004011 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004012 if (family == PF_UNSPEC && sock != NULL)
4013 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004014
Casey Schaufler69f287a2014-12-12 17:08:40 -08004015 switch (family) {
4016 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004017 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004018 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004019 break;
4020 case PF_INET:
4021#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4022 s = skb->secmark;
4023 if (s != 0)
4024 break;
4025#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004026 /*
4027 * Translate what netlabel gave us.
4028 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004029 if (sock != NULL && sock->sk != NULL)
4030 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004031 netlbl_secattr_init(&secattr);
4032 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4033 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004034 skp = smack_from_secattr(&secattr, ssp);
4035 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004036 }
4037 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004038 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004039 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004040#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004041 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004042#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004043 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004044 }
4045 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004046 if (s == 0)
4047 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004048 return 0;
4049}
4050
4051/**
Paul Moore07feee82009-03-27 17:10:54 -04004052 * smack_sock_graft - Initialize a newly created socket with an existing sock
4053 * @sk: child sock
4054 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004055 *
Paul Moore07feee82009-03-27 17:10:54 -04004056 * Set the smk_{in,out} state of an existing sock based on the process that
4057 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004058 */
4059static void smack_sock_graft(struct sock *sk, struct socket *parent)
4060{
4061 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004062 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004063
Paul Moore07feee82009-03-27 17:10:54 -04004064 if (sk == NULL ||
4065 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004066 return;
4067
4068 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004069 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004070 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004071 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004072}
4073
4074/**
4075 * smack_inet_conn_request - Smack access check on connect
4076 * @sk: socket involved
4077 * @skb: packet
4078 * @req: unused
4079 *
4080 * Returns 0 if a task with the packet label could write to
4081 * the socket, otherwise an error code
4082 */
4083static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4084 struct request_sock *req)
4085{
Paul Moore07feee82009-03-27 17:10:54 -04004086 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004087 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004088 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004089 struct netlbl_lsm_secattr secattr;
4090 struct sockaddr_in addr;
4091 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004092 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004093 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004094 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004095#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004096 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004097#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004098
Casey Schaufler69f287a2014-12-12 17:08:40 -08004099#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004100 if (family == PF_INET6) {
4101 /*
4102 * Handle mapped IPv4 packets arriving
4103 * via IPv6 sockets. Don't set up netlabel
4104 * processing on IPv6.
4105 */
4106 if (skb->protocol == htons(ETH_P_IP))
4107 family = PF_INET;
4108 else
4109 return 0;
4110 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004111#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004112
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004113#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4114 /*
4115 * If there is a secmark use it rather than the CIPSO label.
4116 * If there is no secmark fall back to CIPSO.
4117 * The secmark is assumed to reflect policy better.
4118 */
4119 if (skb && skb->secmark != 0) {
4120 skp = smack_from_secid(skb->secmark);
4121 goto access_check;
4122 }
4123#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4124
Paul Moore07feee82009-03-27 17:10:54 -04004125 netlbl_secattr_init(&secattr);
4126 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004127 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004128 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004129 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004130 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004131 netlbl_secattr_destroy(&secattr);
4132
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004133#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4134access_check:
4135#endif
4136
Etienne Bassetecfcc532009-04-08 20:40:06 +02004137#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004138 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4139 ad.a.u.net->family = family;
4140 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004141 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4142#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004143 /*
Paul Moore07feee82009-03-27 17:10:54 -04004144 * Receiving a packet requires that the other end be able to write
4145 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004146 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004147 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4148 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004149 if (rc != 0)
4150 return rc;
4151
4152 /*
4153 * Save the peer's label in the request_sock so we can later setup
4154 * smk_packet in the child socket so that SO_PEERCRED can report it.
4155 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004156 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004157
4158 /*
4159 * We need to decide if we want to label the incoming connection here
4160 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004161 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004162 */
4163 hdr = ip_hdr(skb);
4164 addr.sin_addr.s_addr = hdr->saddr;
4165 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004166 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004167 rcu_read_unlock();
4168
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004169 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004170 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004171 else
Paul Moore07feee82009-03-27 17:10:54 -04004172 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004173
4174 return rc;
4175}
4176
Paul Moore07feee82009-03-27 17:10:54 -04004177/**
4178 * smack_inet_csk_clone - Copy the connection information to the new socket
4179 * @sk: the new socket
4180 * @req: the connection's request_sock
4181 *
4182 * Transfer the connection's peer label to the newly created socket.
4183 */
4184static void smack_inet_csk_clone(struct sock *sk,
4185 const struct request_sock *req)
4186{
4187 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004188 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004189
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004190 if (req->peer_secid != 0) {
4191 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004192 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004193 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004194 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004195}
4196
Casey Schauflere114e472008-02-04 22:29:50 -08004197/*
4198 * Key management security hooks
4199 *
4200 * Casey has not tested key support very heavily.
4201 * The permission check is most likely too restrictive.
4202 * If you care about keys please have a look.
4203 */
4204#ifdef CONFIG_KEYS
4205
4206/**
4207 * smack_key_alloc - Set the key security blob
4208 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004209 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004210 * @flags: unused
4211 *
4212 * No allocation required
4213 *
4214 * Returns 0
4215 */
David Howellsd84f4f92008-11-14 10:39:23 +11004216static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004217 unsigned long flags)
4218{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004219 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004220
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004221 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004222 return 0;
4223}
4224
4225/**
4226 * smack_key_free - Clear the key security blob
4227 * @key: the object
4228 *
4229 * Clear the blob pointer
4230 */
4231static void smack_key_free(struct key *key)
4232{
4233 key->security = NULL;
4234}
4235
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004236/**
Casey Schauflere114e472008-02-04 22:29:50 -08004237 * smack_key_permission - Smack access on a key
4238 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004239 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004240 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004241 *
4242 * Return 0 if the task has read and write to the object,
4243 * an error code otherwise
4244 */
4245static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004246 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004247{
4248 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004249 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004250 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004251 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004252 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004253
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004254 /*
4255 * Validate requested permissions
4256 */
4257 if (perm & ~KEY_NEED_ALL)
4258 return -EINVAL;
4259
Casey Schauflere114e472008-02-04 22:29:50 -08004260 keyp = key_ref_to_ptr(key_ref);
4261 if (keyp == NULL)
4262 return -EINVAL;
4263 /*
4264 * If the key hasn't been initialized give it access so that
4265 * it may do so.
4266 */
4267 if (keyp->security == NULL)
4268 return 0;
4269 /*
4270 * This should not occur
4271 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004272 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004273 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004274
4275 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4276 return 0;
4277
Etienne Bassetecfcc532009-04-08 20:40:06 +02004278#ifdef CONFIG_AUDIT
4279 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4280 ad.a.u.key_struct.key = keyp->serial;
4281 ad.a.u.key_struct.key_desc = keyp->description;
4282#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004283 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4284 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004285 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004286 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004287 rc = smk_access(tkp, keyp->security, request, &ad);
4288 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4289 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004290}
José Bollo7fc5f362015-02-17 15:41:22 +01004291
4292/*
4293 * smack_key_getsecurity - Smack label tagging the key
4294 * @key points to the key to be queried
4295 * @_buffer points to a pointer that should be set to point to the
4296 * resulting string (if no label or an error occurs).
4297 * Return the length of the string (including terminating NUL) or -ve if
4298 * an error.
4299 * May also return 0 (and a NULL buffer pointer) if there is no label.
4300 */
4301static int smack_key_getsecurity(struct key *key, char **_buffer)
4302{
4303 struct smack_known *skp = key->security;
4304 size_t length;
4305 char *copy;
4306
4307 if (key->security == NULL) {
4308 *_buffer = NULL;
4309 return 0;
4310 }
4311
4312 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4313 if (copy == NULL)
4314 return -ENOMEM;
4315 length = strlen(copy) + 1;
4316
4317 *_buffer = copy;
4318 return length;
4319}
4320
Casey Schauflere114e472008-02-04 22:29:50 -08004321#endif /* CONFIG_KEYS */
4322
4323/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004324 * Smack Audit hooks
4325 *
4326 * Audit requires a unique representation of each Smack specific
4327 * rule. This unique representation is used to distinguish the
4328 * object to be audited from remaining kernel objects and also
4329 * works as a glue between the audit hooks.
4330 *
4331 * Since repository entries are added but never deleted, we'll use
4332 * the smack_known label address related to the given audit rule as
4333 * the needed unique representation. This also better fits the smack
4334 * model where nearly everything is a label.
4335 */
4336#ifdef CONFIG_AUDIT
4337
4338/**
4339 * smack_audit_rule_init - Initialize a smack audit rule
4340 * @field: audit rule fields given from user-space (audit.h)
4341 * @op: required testing operator (=, !=, >, <, ...)
4342 * @rulestr: smack label to be audited
4343 * @vrule: pointer to save our own audit rule representation
4344 *
4345 * Prepare to audit cases where (@field @op @rulestr) is true.
4346 * The label to be audited is created if necessay.
4347 */
4348static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4349{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004350 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004351 char **rule = (char **)vrule;
4352 *rule = NULL;
4353
4354 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4355 return -EINVAL;
4356
Al Viro5af75d82008-12-16 05:59:26 -05004357 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004358 return -EINVAL;
4359
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004360 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004361 if (IS_ERR(skp))
4362 return PTR_ERR(skp);
4363
4364 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004365
4366 return 0;
4367}
4368
4369/**
4370 * smack_audit_rule_known - Distinguish Smack audit rules
4371 * @krule: rule of interest, in Audit kernel representation format
4372 *
4373 * This is used to filter Smack rules from remaining Audit ones.
4374 * If it's proved that this rule belongs to us, the
4375 * audit_rule_match hook will be called to do the final judgement.
4376 */
4377static int smack_audit_rule_known(struct audit_krule *krule)
4378{
4379 struct audit_field *f;
4380 int i;
4381
4382 for (i = 0; i < krule->field_count; i++) {
4383 f = &krule->fields[i];
4384
4385 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4386 return 1;
4387 }
4388
4389 return 0;
4390}
4391
4392/**
4393 * smack_audit_rule_match - Audit given object ?
4394 * @secid: security id for identifying the object to test
4395 * @field: audit rule flags given from user-space
4396 * @op: required testing operator
4397 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004398 *
4399 * The core Audit hook. It's used to take the decision of
4400 * whether to audit or not to audit a given object.
4401 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004402static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004403{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004404 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004405 char *rule = vrule;
4406
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004407 if (unlikely(!rule)) {
4408 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004409 return -ENOENT;
4410 }
4411
4412 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4413 return 0;
4414
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004415 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004416
4417 /*
4418 * No need to do string comparisons. If a match occurs,
4419 * both pointers will point to the same smack_known
4420 * label.
4421 */
Al Viro5af75d82008-12-16 05:59:26 -05004422 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004423 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004424 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004425 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004426
4427 return 0;
4428}
4429
Casey Schaufler491a0b02016-01-26 15:08:35 -08004430/*
4431 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004432 * No memory was allocated.
4433 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004434
4435#endif /* CONFIG_AUDIT */
4436
Randy Dunlap251a2a92009-02-18 11:42:33 -08004437/**
David Quigley746df9b2013-05-22 12:50:35 -04004438 * smack_ismaclabel - check if xattr @name references a smack MAC label
4439 * @name: Full xattr name to check.
4440 */
4441static int smack_ismaclabel(const char *name)
4442{
4443 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4444}
4445
4446
4447/**
Casey Schauflere114e472008-02-04 22:29:50 -08004448 * smack_secid_to_secctx - return the smack label for a secid
4449 * @secid: incoming integer
4450 * @secdata: destination
4451 * @seclen: how long it is
4452 *
4453 * Exists for networking code.
4454 */
4455static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4456{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004457 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004458
Eric Parisd5630b92010-10-13 16:24:48 -04004459 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004460 *secdata = skp->smk_known;
4461 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004462 return 0;
4463}
4464
Randy Dunlap251a2a92009-02-18 11:42:33 -08004465/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004466 * smack_secctx_to_secid - return the secid for a smack label
4467 * @secdata: smack label
4468 * @seclen: how long result is
4469 * @secid: outgoing integer
4470 *
4471 * Exists for audit and networking code.
4472 */
David Howellse52c17642008-04-29 20:52:51 +01004473static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004474{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004475 struct smack_known *skp = smk_find_entry(secdata);
4476
4477 if (skp)
4478 *secid = skp->smk_secid;
4479 else
4480 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004481 return 0;
4482}
4483
Casey Schaufler491a0b02016-01-26 15:08:35 -08004484/*
4485 * There used to be a smack_release_secctx hook
4486 * that did nothing back when hooks were in a vector.
4487 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004488 */
Casey Schauflere114e472008-02-04 22:29:50 -08004489
David P. Quigley1ee65e32009-09-03 14:25:57 -04004490static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4491{
4492 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4493}
4494
4495static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4496{
4497 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4498}
4499
4500static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4501{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004502 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004503
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004504 *ctx = skp->smk_known;
4505 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004506 return 0;
4507}
4508
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004509static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4510{
4511
4512 struct task_smack *tsp;
4513 struct smack_known *skp;
4514 struct inode_smack *isp;
4515 struct cred *new_creds = *new;
4516
4517 if (new_creds == NULL) {
4518 new_creds = prepare_creds();
4519 if (new_creds == NULL)
4520 return -ENOMEM;
4521 }
4522
Casey Schauflerb17103a2018-11-09 16:12:56 -08004523 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004524
4525 /*
4526 * Get label from overlay inode and set it in create_sid
4527 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004528 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004529 skp = isp->smk_inode;
4530 tsp->smk_task = skp;
4531 *new = new_creds;
4532 return 0;
4533}
4534
4535static int smack_inode_copy_up_xattr(const char *name)
4536{
4537 /*
4538 * Return 1 if this is the smack access Smack attribute.
4539 */
4540 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4541 return 1;
4542
4543 return -EOPNOTSUPP;
4544}
4545
4546static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4547 struct qstr *name,
4548 const struct cred *old,
4549 struct cred *new)
4550{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004551 struct task_smack *otsp = smack_cred(old);
4552 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004553 struct inode_smack *isp;
4554 int may;
4555
4556 /*
4557 * Use the process credential unless all of
4558 * the transmuting criteria are met
4559 */
4560 ntsp->smk_task = otsp->smk_task;
4561
4562 /*
4563 * the attribute of the containing directory
4564 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004565 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004566
4567 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4568 rcu_read_lock();
4569 may = smk_access_entry(otsp->smk_task->smk_known,
4570 isp->smk_inode->smk_known,
4571 &otsp->smk_task->smk_rules);
4572 rcu_read_unlock();
4573
4574 /*
4575 * If the directory is transmuting and the rule
4576 * providing access is transmuting use the containing
4577 * directory label instead of the process label.
4578 */
4579 if (may > 0 && (may & MAY_TRANSMUTE))
4580 ntsp->smk_task = isp->smk_inode;
4581 }
4582 return 0;
4583}
4584
Casey Schauflerbbd36622018-11-12 09:30:56 -08004585struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4586 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004587 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004588 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004589 .lbs_ipc = sizeof(struct smack_known *),
4590 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004591};
4592
James Morrisca97d932017-02-15 00:18:51 +11004593static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004594 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4595 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4596 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004597
Al Viro0b520752018-12-23 16:02:47 -05004598 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004599 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4600
Casey Schauflere20b0432015-05-02 15:11:36 -07004601 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4602 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004603 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004604 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004605 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304606 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004607
Casey Schauflere20b0432015-05-02 15:11:36 -07004608 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004609
Casey Schauflere20b0432015-05-02 15:11:36 -07004610 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004611 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4612 LSM_HOOK_INIT(inode_link, smack_inode_link),
4613 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4614 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4615 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4616 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4617 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4618 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4619 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4620 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4621 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4622 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4623 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4624 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4625 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4626 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004627
Casey Schauflere20b0432015-05-02 15:11:36 -07004628 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004629 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4630 LSM_HOOK_INIT(file_lock, smack_file_lock),
4631 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4632 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4633 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4634 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4635 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4636 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004637
Casey Schauflere20b0432015-05-02 15:11:36 -07004638 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004639
Casey Schauflere20b0432015-05-02 15:11:36 -07004640 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4641 LSM_HOOK_INIT(cred_free, smack_cred_free),
4642 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4643 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004644 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004645 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4646 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4647 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4648 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4649 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4650 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4651 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4652 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4653 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4654 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4655 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4656 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4657 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004658 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004659
Casey Schauflere20b0432015-05-02 15:11:36 -07004660 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4661 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004662
Casey Schauflere20b0432015-05-02 15:11:36 -07004663 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004664
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004665 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004666 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4667 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4668 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4669 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004670
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004671 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004672 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4673 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4674 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004675
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004676 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004677 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4678 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4679 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004680
Casey Schauflere20b0432015-05-02 15:11:36 -07004681 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004682
Casey Schauflere20b0432015-05-02 15:11:36 -07004683 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4684 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004685
Casey Schauflere20b0432015-05-02 15:11:36 -07004686 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4687 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004688
Casey Schauflere20b0432015-05-02 15:11:36 -07004689 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004690 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004691#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004692 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004693#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004694 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4695 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4696 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4697 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4698 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4699 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4700 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4701 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4702 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4703 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004704
Casey Schauflere114e472008-02-04 22:29:50 -08004705 /* key management security hooks */
4706#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004707 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4708 LSM_HOOK_INIT(key_free, smack_key_free),
4709 LSM_HOOK_INIT(key_permission, smack_key_permission),
4710 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004711#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004712
4713 /* Audit hooks */
4714#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004715 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4716 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4717 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004718#endif /* CONFIG_AUDIT */
4719
Casey Schauflere20b0432015-05-02 15:11:36 -07004720 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4721 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4722 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004723 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4724 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4725 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004726 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4727 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4728 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004729};
4730
Etienne Basset7198e2e2009-03-24 20:53:24 +01004731
Casey Schaufler86812bb2012-04-17 18:55:46 -07004732static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004733{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004734 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004735 * Initialize rule list locks
4736 */
4737 mutex_init(&smack_known_huh.smk_rules_lock);
4738 mutex_init(&smack_known_hat.smk_rules_lock);
4739 mutex_init(&smack_known_floor.smk_rules_lock);
4740 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004741 mutex_init(&smack_known_web.smk_rules_lock);
4742 /*
4743 * Initialize rule lists
4744 */
4745 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4746 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4747 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4748 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004749 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4750 /*
4751 * Create the known labels list
4752 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004753 smk_insert_entry(&smack_known_huh);
4754 smk_insert_entry(&smack_known_hat);
4755 smk_insert_entry(&smack_known_star);
4756 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004757 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004758}
4759
Casey Schauflere114e472008-02-04 22:29:50 -08004760/**
4761 * smack_init - initialize the smack system
4762 *
luanshia1a07f22019-07-05 10:35:20 +08004763 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004764 */
4765static __init int smack_init(void)
4766{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004767 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004768 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004769
Rohit1a5b4722014-10-15 17:40:41 +05304770 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4771 if (!smack_inode_cache)
4772 return -ENOMEM;
4773
Casey Schaufler4e328b02019-04-02 11:37:12 -07004774 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4775 if (!smack_rule_cache) {
4776 kmem_cache_destroy(smack_inode_cache);
4777 return -ENOMEM;
4778 }
4779
Casey Schauflerbbd36622018-11-12 09:30:56 -08004780 /*
4781 * Set the security state for the initial task.
4782 */
4783 tsp = smack_cred(cred);
4784 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4785
4786 /*
4787 * Register with LSM
4788 */
4789 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004790 smack_enabled = 1;
4791
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004792 pr_info("Smack: Initializing.\n");
4793#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4794 pr_info("Smack: Netfilter enabled.\n");
4795#endif
4796#ifdef SMACK_IPV6_PORT_LABELING
4797 pr_info("Smack: IPv6 port labeling enabled.\n");
4798#endif
4799#ifdef SMACK_IPV6_SECMARK_LABELING
4800 pr_info("Smack: IPv6 Netfilter enabled.\n");
4801#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004802
Casey Schaufler86812bb2012-04-17 18:55:46 -07004803 /* initialize the smack_known_list */
4804 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004805
Casey Schauflere114e472008-02-04 22:29:50 -08004806 return 0;
4807}
4808
4809/*
4810 * Smack requires early initialization in order to label
4811 * all processes and objects when they are created.
4812 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004813DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004814 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004815 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004816 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004817 .init = smack_init,
4818};