blob: 0d6bb53efe745fc5aa3c95e2c5a8e5197d8db32d [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
Al Virod7167b12019-09-07 07:23:15 -0400681static const struct fs_parameter_spec smack_fs_parameters[] = {
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
David Howells2febd252018-11-01 23:07:24 +0000691/**
692 * smack_fs_context_parse_param - Parse a single mount parameter
693 * @fc: The new filesystem context being constructed.
694 * @param: The parameter.
695 *
696 * Returns 0 on success, -ENOPARAM to pass the parameter on or anything else on
697 * error.
698 */
699static int smack_fs_context_parse_param(struct fs_context *fc,
700 struct fs_parameter *param)
701{
702 struct fs_parse_result result;
703 int opt, rc;
704
Al Virod7167b12019-09-07 07:23:15 -0400705 opt = fs_parse(fc, smack_fs_parameters, param, &result);
David Howells2febd252018-11-01 23:07:24 +0000706 if (opt < 0)
707 return opt;
708
709 rc = smack_add_opt(opt, param->string, &fc->security);
710 if (!rc)
711 param->string = NULL;
712 return rc;
713}
714
Al Virod2497e12018-12-16 01:37:06 -0500715static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530716{
Al Virod2497e12018-12-16 01:37:06 -0500717 char *from = options, *to = options;
718 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530719
Al Viroc3300aa2018-12-16 01:52:24 -0500720 while (1) {
721 char *next = strchr(from, ',');
722 int token, len, rc;
723 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530724
Al Viroc3300aa2018-12-16 01:52:24 -0500725 if (next)
726 len = next - from;
727 else
728 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530729
Al Viroc3300aa2018-12-16 01:52:24 -0500730 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500731 if (token != Opt_error) {
732 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
733 rc = smack_add_opt(token, arg, mnt_opts);
734 if (unlikely(rc)) {
735 kfree(arg);
736 if (*mnt_opts)
737 smack_free_mnt_opts(*mnt_opts);
738 *mnt_opts = NULL;
739 return rc;
740 }
741 } else {
742 if (!first) { // copy with preceding comma
743 from--;
744 len++;
745 }
746 if (to != from)
747 memmove(to, from, len);
748 to += len;
749 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530750 }
Al Viroc3300aa2018-12-16 01:52:24 -0500751 if (!from[len])
752 break;
753 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530754 }
Al Virod2497e12018-12-16 01:37:06 -0500755 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530756 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530757}
758
759/**
760 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800761 * @sb: the file system superblock
luanshia1a07f22019-07-05 10:35:20 +0800762 * @mnt_opts: Smack mount options
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530763 * @kern_flags: mount option from kernel space or user space
764 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800765 *
766 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530767 *
768 * Allow filesystems with binary mount data to explicitly set Smack mount
769 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800770 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530771static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500772 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530773 unsigned long kern_flags,
774 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800775{
776 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000777 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800778 struct superblock_smack *sp = sb->s_security;
779 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800780 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500781 struct smack_mnt_opts *opts = mnt_opts;
782 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800783
Seth Forshee9f50eda2015-09-23 15:16:06 -0500784 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800785 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700786
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700787 if (inode->i_security == NULL) {
788 int rc = lsm_inode_alloc(inode);
789
790 if (rc)
791 return rc;
792 }
793
Himanshu Shukla2097f592016-11-10 16:19:52 +0530794 if (!smack_privileged(CAP_MAC_ADMIN)) {
795 /*
796 * Unprivileged mounts don't get to specify Smack values.
797 */
Al Viro12085b12018-12-13 15:18:05 -0500798 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530799 return -EPERM;
800 /*
801 * Unprivileged mounts get root and default from the caller.
802 */
803 skp = smk_of_current();
804 sp->smk_root = skp;
805 sp->smk_default = skp;
806 /*
807 * For a handful of fs types with no user-controlled
808 * backing store it's okay to trust security labels
809 * in the filesystem. The rest are untrusted.
810 */
811 if (sb->s_user_ns != &init_user_ns &&
812 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
813 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500814 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530815 sp->smk_flags |= SMK_SB_UNTRUSTED;
816 }
817 }
818
Seth Forshee9f50eda2015-09-23 15:16:06 -0500819 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800820
Al Viro12085b12018-12-13 15:18:05 -0500821 if (opts) {
822 if (opts->fsdefault) {
823 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200824 if (IS_ERR(skp))
825 return PTR_ERR(skp);
826 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500827 }
828 if (opts->fsfloor) {
829 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530830 if (IS_ERR(skp))
831 return PTR_ERR(skp);
832 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500833 }
834 if (opts->fshat) {
835 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530836 if (IS_ERR(skp))
837 return PTR_ERR(skp);
838 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500839 }
840 if (opts->fsroot) {
841 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200842 if (IS_ERR(skp))
843 return PTR_ERR(skp);
844 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500845 }
846 if (opts->fstransmute) {
847 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200848 if (IS_ERR(skp))
849 return PTR_ERR(skp);
850 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500851 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800852 }
853 }
854
855 /*
856 * Initialize the root inode.
857 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700858 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800859
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700860 if (transmute) {
861 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700862 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700863 }
Casey Schauflere830b392013-05-22 18:43:07 -0700864
Casey Schauflere114e472008-02-04 22:29:50 -0800865 return 0;
866}
867
868/**
869 * smack_sb_statfs - Smack check on statfs
870 * @dentry: identifies the file system in question
871 *
872 * Returns 0 if current can read the floor of the filesystem,
873 * and error code otherwise
874 */
875static int smack_sb_statfs(struct dentry *dentry)
876{
877 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200878 int rc;
879 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800880
Eric Parisa2694342011-04-25 13:10:27 -0400881 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200882 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
883
884 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700885 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200886 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800887}
888
Casey Schauflere114e472008-02-04 22:29:50 -0800889/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800890 * BPRM hooks
891 */
892
Casey Schauflerce8a4322011-09-29 18:21:01 -0700893/**
894 * smack_bprm_set_creds - set creds for exec
895 * @bprm: the exec information
896 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100897 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700898 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800899static int smack_bprm_set_creds(struct linux_binprm *bprm)
900{
Al Viro496ad9a2013-01-23 17:07:38 -0500901 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800902 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800903 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500904 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800905 int rc;
906
Kees Cookddb4a142017-07-18 15:25:23 -0700907 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800908 return 0;
909
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800910 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300911 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800912 return 0;
913
Seth Forshee809c02e2016-04-26 14:36:22 -0500914 sbsp = inode->i_sb->s_security;
915 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
916 isp->smk_task != sbsp->smk_root)
917 return 0;
918
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300919 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100920 struct task_struct *tracer;
921 rc = 0;
922
923 rcu_read_lock();
924 tracer = ptrace_parent(current);
925 if (likely(tracer != NULL))
926 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200927 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100928 PTRACE_MODE_ATTACH,
929 __func__);
930 rcu_read_unlock();
931
932 if (rc != 0)
933 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200934 }
935 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300936 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800937
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300938 bsp->smk_task = isp->smk_task;
939 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800940
Kees Cookccbb6e12017-07-18 15:25:26 -0700941 /* Decide if this is a secure exec. */
942 if (bsp->smk_task != bsp->smk_forked)
943 bprm->secureexec = 1;
944
Casey Schaufler676dac42010-12-02 06:43:39 -0800945 return 0;
946}
947
948/*
Casey Schauflere114e472008-02-04 22:29:50 -0800949 * Inode hooks
950 */
951
952/**
953 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800954 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800955 *
luanshia1a07f22019-07-05 10:35:20 +0800956 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800957 */
958static int smack_inode_alloc_security(struct inode *inode)
959{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700960 struct smack_known *skp = smk_of_current();
961
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700962 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800963 return 0;
964}
965
966/**
Casey Schauflere114e472008-02-04 22:29:50 -0800967 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200968 * @inode: the newly created inode
969 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500970 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800971 * @name: where to put the attribute name
972 * @value: where to put the attribute value
973 * @len: where to put the length of the attribute
974 *
975 * Returns 0 if it all works out, -ENOMEM if there's no memory
976 */
977static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900978 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500979 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800980{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800981 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700982 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200983 struct smack_known *isp = smk_of_inode(inode);
984 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800985 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800986
Tetsuo Handa95489062013-07-25 05:44:02 +0900987 if (name)
988 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800989
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100990 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800991 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200992 may = smk_access_entry(skp->smk_known, dsp->smk_known,
993 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800994 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200995
996 /*
997 * If the access rule allows transmutation and
998 * the directory requests transmutation then
999 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -07001000 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001001 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001002 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001003 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001004 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001005 issp->smk_flags |= SMK_INODE_CHANGED;
1006 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001007
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001008 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001009 if (*value == NULL)
1010 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001011
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001012 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001013 }
Casey Schauflere114e472008-02-04 22:29:50 -08001014
1015 return 0;
1016}
1017
1018/**
1019 * smack_inode_link - Smack check on link
1020 * @old_dentry: the existing object
1021 * @dir: unused
1022 * @new_dentry: the new object
1023 *
1024 * Returns 0 if access is permitted, an error code otherwise
1025 */
1026static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1027 struct dentry *new_dentry)
1028{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001029 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001030 struct smk_audit_info ad;
1031 int rc;
1032
Eric Parisa2694342011-04-25 13:10:27 -04001033 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001034 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001035
David Howellsc6f493d2015-03-17 22:26:22 +00001036 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001037 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001038 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001039
David Howells88025652015-01-29 12:02:32 +00001040 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001041 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001042 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1043 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001044 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001045 }
1046
1047 return rc;
1048}
1049
1050/**
1051 * smack_inode_unlink - Smack check on inode deletion
1052 * @dir: containing directory object
1053 * @dentry: file to unlink
1054 *
1055 * Returns 0 if current can write the containing directory
1056 * and the object, error code otherwise
1057 */
1058static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1059{
David Howellsc6f493d2015-03-17 22:26:22 +00001060 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001061 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001062 int rc;
1063
Eric Parisa2694342011-04-25 13:10:27 -04001064 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001065 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1066
Casey Schauflere114e472008-02-04 22:29:50 -08001067 /*
1068 * You need write access to the thing you're unlinking
1069 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001070 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001071 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001072 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001073 /*
1074 * You also need write access to the containing directory
1075 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001076 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001077 smk_ad_setfield_u_fs_inode(&ad, dir);
1078 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001079 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001080 }
Casey Schauflere114e472008-02-04 22:29:50 -08001081 return rc;
1082}
1083
1084/**
1085 * smack_inode_rmdir - Smack check on directory deletion
1086 * @dir: containing directory object
1087 * @dentry: directory to unlink
1088 *
1089 * Returns 0 if current can write the containing directory
1090 * and the directory, error code otherwise
1091 */
1092static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1093{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001094 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001095 int rc;
1096
Eric Parisa2694342011-04-25 13:10:27 -04001097 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001098 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1099
Casey Schauflere114e472008-02-04 22:29:50 -08001100 /*
1101 * You need write access to the thing you're removing
1102 */
David Howellsc6f493d2015-03-17 22:26:22 +00001103 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1104 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001105 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001106 /*
1107 * You also need write access to the containing directory
1108 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001109 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001110 smk_ad_setfield_u_fs_inode(&ad, dir);
1111 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001112 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001113 }
Casey Schauflere114e472008-02-04 22:29:50 -08001114
1115 return rc;
1116}
1117
1118/**
1119 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001120 * @old_inode: unused
1121 * @old_dentry: the old object
1122 * @new_inode: unused
1123 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001124 *
1125 * Read and write access is required on both the old and
1126 * new directories.
1127 *
1128 * Returns 0 if access is permitted, an error code otherwise
1129 */
1130static int smack_inode_rename(struct inode *old_inode,
1131 struct dentry *old_dentry,
1132 struct inode *new_inode,
1133 struct dentry *new_dentry)
1134{
1135 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001136 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001137 struct smk_audit_info ad;
1138
Eric Parisa2694342011-04-25 13:10:27 -04001139 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001140 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001141
David Howellsc6f493d2015-03-17 22:26:22 +00001142 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001143 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001144 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001145
David Howells88025652015-01-29 12:02:32 +00001146 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001147 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1149 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001150 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001151 }
Casey Schauflere114e472008-02-04 22:29:50 -08001152 return rc;
1153}
1154
1155/**
1156 * smack_inode_permission - Smack version of permission()
1157 * @inode: the inode in question
1158 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001159 *
1160 * This is the important Smack hook.
1161 *
luanshia1a07f22019-07-05 10:35:20 +08001162 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001163 */
Al Viroe74f71e2011-06-20 19:38:15 -04001164static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001165{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001166 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001167 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001168 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001169 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001170
1171 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001172 /*
1173 * No permission to check. Existence test. Yup, it's there.
1174 */
1175 if (mask == 0)
1176 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001177
Seth Forshee9f50eda2015-09-23 15:16:06 -05001178 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1179 if (smk_of_inode(inode) != sbsp->smk_root)
1180 return -EACCES;
1181 }
1182
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001183 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001184 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001185 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001186 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001187 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001188 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1189 rc = smk_bu_inode(inode, mask, rc);
1190 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001191}
1192
1193/**
1194 * smack_inode_setattr - Smack check for setting attributes
1195 * @dentry: the object
1196 * @iattr: for the force flag
1197 *
1198 * Returns 0 if access is permitted, an error code otherwise
1199 */
1200static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1201{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001202 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001203 int rc;
1204
Casey Schauflere114e472008-02-04 22:29:50 -08001205 /*
1206 * Need to allow for clearing the setuid bit.
1207 */
1208 if (iattr->ia_valid & ATTR_FORCE)
1209 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001210 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001211 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001212
David Howellsc6f493d2015-03-17 22:26:22 +00001213 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1214 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001215 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001216}
1217
1218/**
1219 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001220 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001221 *
1222 * Returns 0 if access is permitted, an error code otherwise
1223 */
Al Viro3f7036a2015-03-08 19:28:30 -04001224static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001225{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001226 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001227 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001228 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001229
Eric Parisf48b7392011-04-25 12:54:27 -04001230 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001231 smk_ad_setfield_u_fs_path(&ad, *path);
1232 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1233 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001234 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001235}
1236
1237/**
1238 * smack_inode_setxattr - Smack check for setting xattrs
1239 * @dentry: the object
1240 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001241 * @value: value of the attribute
1242 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001243 * @flags: unused
1244 *
1245 * This protects the Smack attribute explicitly.
1246 *
1247 * Returns 0 if access is permitted, an error code otherwise
1248 */
David Howells8f0cfa52008-04-29 00:59:41 -07001249static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1250 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001251{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001252 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001253 struct smack_known *skp;
1254 int check_priv = 0;
1255 int check_import = 0;
1256 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001257 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001258
Casey Schaufler19760ad2013-12-16 16:27:26 -08001259 /*
1260 * Check label validity here so import won't fail in post_setxattr
1261 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001262 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1263 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001264 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1265 check_priv = 1;
1266 check_import = 1;
1267 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1268 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1269 check_priv = 1;
1270 check_import = 1;
1271 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001272 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001273 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001274 if (size != TRANS_TRUE_SIZE ||
1275 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1276 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001277 } else
1278 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1279
Casey Schaufler19760ad2013-12-16 16:27:26 -08001280 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1281 rc = -EPERM;
1282
1283 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001284 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001285 if (IS_ERR(skp))
1286 rc = PTR_ERR(skp);
1287 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001288 (skp == &smack_known_star || skp == &smack_known_web)))
1289 rc = -EINVAL;
1290 }
1291
Eric Parisa2694342011-04-25 13:10:27 -04001292 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001293 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1294
Casey Schauflerd166c802014-08-27 14:51:27 -07001295 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001296 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1297 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001298 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001299
1300 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001301}
1302
1303/**
1304 * smack_inode_post_setxattr - Apply the Smack update approved above
1305 * @dentry: object
1306 * @name: attribute name
1307 * @value: attribute value
1308 * @size: attribute size
1309 * @flags: unused
1310 *
1311 * Set the pointer in the inode blob to the entry found
1312 * in the master label list.
1313 */
David Howells8f0cfa52008-04-29 00:59:41 -07001314static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1315 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001316{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001317 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001318 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001319
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001320 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1321 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1322 return;
1323 }
1324
Casey Schaufler676dac42010-12-02 06:43:39 -08001325 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001326 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001327 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001328 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001329 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 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))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001332 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001333 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 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_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001337 }
Casey Schauflere114e472008-02-04 22:29:50 -08001338
1339 return;
1340}
1341
Casey Schauflerce8a4322011-09-29 18:21:01 -07001342/**
Casey Schauflere114e472008-02-04 22:29:50 -08001343 * smack_inode_getxattr - Smack check on getxattr
1344 * @dentry: the object
1345 * @name: unused
1346 *
1347 * Returns 0 if access is permitted, an error code otherwise
1348 */
David Howells8f0cfa52008-04-29 00:59:41 -07001349static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001350{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001351 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001352 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001353
Eric Parisa2694342011-04-25 13:10:27 -04001354 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001355 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1356
David Howellsc6f493d2015-03-17 22:26:22 +00001357 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1358 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001359 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001360}
1361
Casey Schauflerce8a4322011-09-29 18:21:01 -07001362/**
Casey Schauflere114e472008-02-04 22:29:50 -08001363 * smack_inode_removexattr - Smack check on removexattr
1364 * @dentry: the object
1365 * @name: name of the attribute
1366 *
1367 * Removing the Smack attribute requires CAP_MAC_ADMIN
1368 *
1369 * Returns 0 if access is permitted, an error code otherwise
1370 */
David Howells8f0cfa52008-04-29 00:59:41 -07001371static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001372{
Casey Schaufler676dac42010-12-02 06:43:39 -08001373 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001374 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001375 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001376
Casey Schauflerbcdca222008-02-23 15:24:04 -08001377 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1378 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001379 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001380 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001381 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301382 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001383 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001384 rc = -EPERM;
1385 } else
1386 rc = cap_inode_removexattr(dentry, name);
1387
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001388 if (rc != 0)
1389 return rc;
1390
Eric Parisa2694342011-04-25 13:10:27 -04001391 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001392 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001393
David Howellsc6f493d2015-03-17 22:26:22 +00001394 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1395 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001396 if (rc != 0)
1397 return rc;
1398
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001399 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001400 /*
1401 * Don't do anything special for these.
1402 * XATTR_NAME_SMACKIPIN
1403 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001404 */
José Bollo80124952016-01-12 21:23:40 +01001405 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001406 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001407 struct superblock_smack *sbsp = sbp->s_security;
1408
1409 isp->smk_inode = sbsp->smk_default;
1410 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001411 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001412 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001413 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001414 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1415 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001416
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001417 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001418}
1419
1420/**
1421 * smack_inode_getsecurity - get smack xattrs
1422 * @inode: the object
1423 * @name: attribute name
1424 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001425 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001426 *
1427 * Returns the size of the attribute or an error code
1428 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001429static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001430 const char *name, void **buffer,
1431 bool alloc)
1432{
1433 struct socket_smack *ssp;
1434 struct socket *sock;
1435 struct super_block *sbp;
1436 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001437 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001438
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001439 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001440 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001441 else {
1442 /*
1443 * The rest of the Smack xattrs are only on sockets.
1444 */
1445 sbp = ip->i_sb;
1446 if (sbp->s_magic != SOCKFS_MAGIC)
1447 return -EOPNOTSUPP;
1448
1449 sock = SOCKET_I(ip);
1450 if (sock == NULL || sock->sk == NULL)
1451 return -EOPNOTSUPP;
1452
1453 ssp = sock->sk->sk_security;
1454
1455 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1456 isp = ssp->smk_in;
1457 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1458 isp = ssp->smk_out;
1459 else
1460 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001461 }
1462
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001463 if (alloc) {
1464 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1465 if (*buffer == NULL)
1466 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001467 }
1468
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001469 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001470}
1471
1472
1473/**
1474 * smack_inode_listsecurity - list the Smack attributes
1475 * @inode: the object
1476 * @buffer: where they go
1477 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001478 */
1479static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1480 size_t buffer_size)
1481{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001482 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001483
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001484 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001485 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001486
1487 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001488}
1489
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001490/**
1491 * smack_inode_getsecid - Extract inode's security id
1492 * @inode: inode to extract the info from
1493 * @secid: where result will be saved
1494 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001495static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001496{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001497 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001498
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001499 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001500}
1501
Casey Schauflere114e472008-02-04 22:29:50 -08001502/*
1503 * File Hooks
1504 */
1505
Casey Schaufler491a0b02016-01-26 15:08:35 -08001506/*
1507 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001508 *
1509 * Should access checks be done on each read or write?
1510 * UNICOS and SELinux say yes.
1511 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1512 *
1513 * I'll say no for now. Smack does not do the frequent
1514 * label changing that SELinux does.
1515 */
Casey Schauflere114e472008-02-04 22:29:50 -08001516
1517/**
1518 * smack_file_alloc_security - assign a file security blob
1519 * @file: the object
1520 *
1521 * The security blob for a file is a pointer to the master
1522 * label list, so no allocation is done.
1523 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001524 * f_security is the owner security information. It
1525 * isn't used on file access checks, it's for send_sigio.
1526 *
Casey Schauflere114e472008-02-04 22:29:50 -08001527 * Returns 0
1528 */
1529static int smack_file_alloc_security(struct file *file)
1530{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001531 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001532
Casey Schauflerf28952a2018-11-12 09:38:53 -08001533 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001534 return 0;
1535}
1536
1537/**
Casey Schauflere114e472008-02-04 22:29:50 -08001538 * smack_file_ioctl - Smack check on ioctls
1539 * @file: the object
1540 * @cmd: what to do
1541 * @arg: unused
1542 *
1543 * Relies heavily on the correct use of the ioctl command conventions.
1544 *
1545 * Returns 0 if allowed, error code otherwise
1546 */
1547static int smack_file_ioctl(struct file *file, unsigned int cmd,
1548 unsigned long arg)
1549{
1550 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001551 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001552 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001553
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001554 if (unlikely(IS_PRIVATE(inode)))
1555 return 0;
1556
Eric Parisf48b7392011-04-25 12:54:27 -04001557 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001558 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001559
Casey Schauflerd166c802014-08-27 14:51:27 -07001560 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001561 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001562 rc = smk_bu_file(file, MAY_WRITE, rc);
1563 }
Casey Schauflere114e472008-02-04 22:29:50 -08001564
Casey Schauflerd166c802014-08-27 14:51:27 -07001565 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001566 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001567 rc = smk_bu_file(file, MAY_READ, rc);
1568 }
Casey Schauflere114e472008-02-04 22:29:50 -08001569
1570 return rc;
1571}
1572
1573/**
1574 * smack_file_lock - Smack check on file locking
1575 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001576 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001577 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001578 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001579 */
1580static int smack_file_lock(struct file *file, unsigned int cmd)
1581{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001582 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001583 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001584 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001585
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001586 if (unlikely(IS_PRIVATE(inode)))
1587 return 0;
1588
Eric Paris92f42502011-04-25 13:15:55 -04001589 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1590 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001591 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001592 rc = smk_bu_file(file, MAY_LOCK, rc);
1593 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001594}
1595
1596/**
1597 * smack_file_fcntl - Smack check on fcntl
1598 * @file: the object
1599 * @cmd: what action to check
1600 * @arg: unused
1601 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001602 * Generally these operations are harmless.
1603 * File locking operations present an obvious mechanism
1604 * for passing information, so they require write access.
1605 *
Casey Schauflere114e472008-02-04 22:29:50 -08001606 * Returns 0 if current has access, error code otherwise
1607 */
1608static int smack_file_fcntl(struct file *file, unsigned int cmd,
1609 unsigned long arg)
1610{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001611 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001612 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001613 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001614
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001615 if (unlikely(IS_PRIVATE(inode)))
1616 return 0;
1617
Casey Schauflere114e472008-02-04 22:29:50 -08001618 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001619 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001620 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001621 case F_SETLK:
1622 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001623 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1624 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001625 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001626 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001627 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001628 case F_SETOWN:
1629 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001630 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1631 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001632 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001633 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001634 break;
1635 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001636 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001637 }
1638
1639 return rc;
1640}
1641
1642/**
Al Viroe5467852012-05-30 13:30:51 -04001643 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001644 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1645 * if mapping anonymous memory.
1646 * @file contains the file structure for file to map (may be NULL).
1647 * @reqprot contains the protection requested by the application.
1648 * @prot contains the protection that will be applied by the kernel.
1649 * @flags contains the operational flags.
1650 * Return 0 if permission is granted.
1651 */
Al Viroe5467852012-05-30 13:30:51 -04001652static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001653 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001654 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001655{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001656 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001657 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001658 struct smack_rule *srp;
1659 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001660 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001661 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001662 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001663 int may;
1664 int mmay;
1665 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001666 int rc;
1667
Al Viro496ad9a2013-01-23 17:07:38 -05001668 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001669 return 0;
1670
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001671 if (unlikely(IS_PRIVATE(file_inode(file))))
1672 return 0;
1673
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001674 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001675 if (isp->smk_mmap == NULL)
1676 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001677 sbsp = file_inode(file)->i_sb->s_security;
1678 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1679 isp->smk_mmap != sbsp->smk_root)
1680 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001681 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001682
Casey Schauflerb17103a2018-11-09 16:12:56 -08001683 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001684 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001685 rc = 0;
1686
1687 rcu_read_lock();
1688 /*
1689 * For each Smack rule associated with the subject
1690 * label verify that the SMACK64MMAP also has access
1691 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001692 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001693 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001694 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001695 /*
1696 * Matching labels always allows access.
1697 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001698 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001699 continue;
1700 /*
1701 * If there is a matching local rule take
1702 * that into account as well.
1703 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001704 may = smk_access_entry(srp->smk_subject->smk_known,
1705 okp->smk_known,
1706 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001707 if (may == -ENOENT)
1708 may = srp->smk_access;
1709 else
1710 may &= srp->smk_access;
1711 /*
1712 * If may is zero the SMACK64MMAP subject can't
1713 * possibly have less access.
1714 */
1715 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001716 continue;
1717
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001718 /*
1719 * Fetch the global list entry.
1720 * If there isn't one a SMACK64MMAP subject
1721 * can't have as much access as current.
1722 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001723 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1724 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001725 if (mmay == -ENOENT) {
1726 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001727 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001728 }
1729 /*
1730 * If there is a local entry it modifies the
1731 * potential access, too.
1732 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001733 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1734 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001735 if (tmay != -ENOENT)
1736 mmay &= tmay;
1737
1738 /*
1739 * If there is any access available to current that is
1740 * not available to a SMACK64MMAP subject
1741 * deny access.
1742 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001743 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001744 rc = -EACCES;
1745 break;
1746 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001747 }
1748
1749 rcu_read_unlock();
1750
1751 return rc;
1752}
1753
1754/**
Casey Schauflere114e472008-02-04 22:29:50 -08001755 * smack_file_set_fowner - set the file security blob value
1756 * @file: object in question
1757 *
Casey Schauflere114e472008-02-04 22:29:50 -08001758 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001759static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001760{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001761 struct smack_known **blob = smack_file(file);
1762
1763 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001764}
1765
1766/**
1767 * smack_file_send_sigiotask - Smack on sigio
1768 * @tsk: The target task
1769 * @fown: the object the signal come from
1770 * @signum: unused
1771 *
1772 * Allow a privileged task to get signals even if it shouldn't
1773 *
1774 * Returns 0 if a subject with the object's smack could
1775 * write to the task, an error code otherwise.
1776 */
1777static int smack_file_send_sigiotask(struct task_struct *tsk,
1778 struct fown_struct *fown, int signum)
1779{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001780 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001781 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001782 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001783 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001784 struct file *file;
1785 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001786 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001787
1788 /*
1789 * struct fown_struct is never outside the context of a struct file
1790 */
1791 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001792
Etienne Bassetecfcc532009-04-08 20:40:06 +02001793 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001794 blob = smack_file(file);
1795 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001796 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1797 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001798
1799 rcu_read_lock();
1800 tcred = __task_cred(tsk);
1801 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001802 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001803 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001804
1805 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1806 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001807 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001808 return rc;
1809}
1810
1811/**
1812 * smack_file_receive - Smack file receive check
1813 * @file: the object
1814 *
1815 * Returns 0 if current has access, error code otherwise
1816 */
1817static int smack_file_receive(struct file *file)
1818{
Casey Schauflerd166c802014-08-27 14:51:27 -07001819 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001820 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001821 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001822 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001823 struct socket *sock;
1824 struct task_smack *tsp;
1825 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001826
Seung-Woo Kim97775822015-04-17 15:25:04 +09001827 if (unlikely(IS_PRIVATE(inode)))
1828 return 0;
1829
Casey Schaufler4482a442013-12-30 17:37:45 -08001830 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001831 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001832
Casey Schaufler51d59af2017-05-31 08:53:42 -07001833 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001834 sock = SOCKET_I(inode);
1835 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001836 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001837 /*
1838 * If the receiving process can't write to the
1839 * passed socket or if the passed socket can't
1840 * write to the receiving process don't accept
1841 * the passed socket.
1842 */
1843 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1844 rc = smk_bu_file(file, may, rc);
1845 if (rc < 0)
1846 return rc;
1847 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1848 rc = smk_bu_file(file, may, rc);
1849 return rc;
1850 }
Casey Schauflere114e472008-02-04 22:29:50 -08001851 /*
1852 * This code relies on bitmasks.
1853 */
1854 if (file->f_mode & FMODE_READ)
1855 may = MAY_READ;
1856 if (file->f_mode & FMODE_WRITE)
1857 may |= MAY_WRITE;
1858
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001859 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001860 rc = smk_bu_file(file, may, rc);
1861 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001862}
1863
Casey Schaufler531f1d42011-09-19 12:41:42 -07001864/**
Eric Paris83d49852012-04-04 13:45:40 -04001865 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001866 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001867 *
1868 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001869 * Allow the open only if the task has read access. There are
1870 * many read operations (e.g. fstat) that you can do with an
1871 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001872 *
luanshia1a07f22019-07-05 10:35:20 +08001873 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001874 */
Al Viro94817692018-07-10 14:13:18 -04001875static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001876{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001877 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001878 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001879 struct smk_audit_info ad;
1880 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001881
Casey Schauflera6834c02014-04-21 11:10:26 -07001882 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1883 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301884 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001885 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001886
1887 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001888}
1889
Casey Schauflere114e472008-02-04 22:29:50 -08001890/*
1891 * Task hooks
1892 */
1893
1894/**
David Howellsee18d642009-09-02 09:14:21 +01001895 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001896 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001897 * @gfp: the atomicity of any memory allocations
1898 *
1899 * Prepare a blank set of credentials for modification. This must allocate all
1900 * the memory the LSM module might require such that cred_transfer() can
1901 * complete without error.
1902 */
1903static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1904{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001905 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001906 return 0;
1907}
1908
1909
1910/**
David Howellsf1752ee2008-11-14 10:39:17 +11001911 * smack_cred_free - "free" task-level security credentials
1912 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001913 *
Casey Schauflere114e472008-02-04 22:29:50 -08001914 */
David Howellsf1752ee2008-11-14 10:39:17 +11001915static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001916{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001917 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001918 struct smack_rule *rp;
1919 struct list_head *l;
1920 struct list_head *n;
1921
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001922 smk_destroy_label_list(&tsp->smk_relabel);
1923
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001924 list_for_each_safe(l, n, &tsp->smk_rules) {
1925 rp = list_entry(l, struct smack_rule, list);
1926 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001927 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001928 }
Casey Schauflere114e472008-02-04 22:29:50 -08001929}
1930
1931/**
David Howellsd84f4f92008-11-14 10:39:23 +11001932 * smack_cred_prepare - prepare new set of credentials for modification
1933 * @new: the new credentials
1934 * @old: the original credentials
1935 * @gfp: the atomicity of any memory allocations
1936 *
1937 * Prepare a new set of credentials for modification.
1938 */
1939static int smack_cred_prepare(struct cred *new, const struct cred *old,
1940 gfp_t gfp)
1941{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001942 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001943 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001944 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001945
Casey Schauflerbbd36622018-11-12 09:30:56 -08001946 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301947
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001948 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1949 if (rc != 0)
1950 return rc;
1951
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001952 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1953 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001954 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001955}
1956
Randy Dunlap251a2a92009-02-18 11:42:33 -08001957/**
David Howellsee18d642009-09-02 09:14:21 +01001958 * smack_cred_transfer - Transfer the old credentials to the new credentials
1959 * @new: the new credentials
1960 * @old: the original credentials
1961 *
1962 * Fill in a set of blank credentials from another set of credentials.
1963 */
1964static void smack_cred_transfer(struct cred *new, const struct cred *old)
1965{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001966 struct task_smack *old_tsp = smack_cred(old);
1967 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001968
1969 new_tsp->smk_task = old_tsp->smk_task;
1970 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001971 mutex_init(&new_tsp->smk_rules_lock);
1972 INIT_LIST_HEAD(&new_tsp->smk_rules);
1973
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001974 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001975}
1976
1977/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001978 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001979 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001980 * @secid: where to put the result
1981 *
1982 * Sets the secid to contain a u32 version of the smack label.
1983 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001984static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001985{
1986 struct smack_known *skp;
1987
1988 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001989 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001990 *secid = skp->smk_secid;
1991 rcu_read_unlock();
1992}
1993
1994/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001995 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001996 * @new: points to the set of credentials to be modified.
1997 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001998 *
1999 * Set the security data for a kernel service.
2000 */
2001static int smack_kernel_act_as(struct cred *new, u32 secid)
2002{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002003 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002004
Casey Schaufler152f91d2016-11-14 09:38:15 -08002005 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002006 return 0;
2007}
2008
2009/**
2010 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002011 * @new: points to the set of credentials to be modified
2012 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002013 *
2014 * Set the file creation context in a set of credentials to the same
2015 * as the objective context of the specified inode
2016 */
2017static int smack_kernel_create_files_as(struct cred *new,
2018 struct inode *inode)
2019{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002020 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002021 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002022
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002023 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002024 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002025 return 0;
2026}
2027
2028/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002029 * smk_curacc_on_task - helper to log task related access
2030 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002031 * @access: the access requested
2032 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002033 *
2034 * Return 0 if access is permitted
2035 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002036static int smk_curacc_on_task(struct task_struct *p, int access,
2037 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002038{
2039 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002040 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002041 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002042
Casey Schaufler531f1d42011-09-19 12:41:42 -07002043 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002044 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002045 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002046 rc = smk_bu_task(p, access, rc);
2047 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002048}
2049
2050/**
Casey Schauflere114e472008-02-04 22:29:50 -08002051 * smack_task_setpgid - Smack check on setting pgid
2052 * @p: the task object
2053 * @pgid: unused
2054 *
2055 * Return 0 if write access is permitted
2056 */
2057static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2058{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002059 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002060}
2061
2062/**
2063 * smack_task_getpgid - Smack access check for getpgid
2064 * @p: the object task
2065 *
2066 * Returns 0 if current can read the object task, error code otherwise
2067 */
2068static int smack_task_getpgid(struct task_struct *p)
2069{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002070 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002071}
2072
2073/**
2074 * smack_task_getsid - Smack access check for getsid
2075 * @p: the object task
2076 *
2077 * Returns 0 if current can read the object task, error code otherwise
2078 */
2079static int smack_task_getsid(struct task_struct *p)
2080{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002081 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002082}
2083
2084/**
2085 * smack_task_getsecid - get the secid of the task
2086 * @p: the object task
2087 * @secid: where to put the result
2088 *
2089 * Sets the secid to contain a u32 version of the smack label.
2090 */
2091static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2092{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002093 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002094
2095 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002096}
2097
2098/**
2099 * smack_task_setnice - Smack check on setting nice
2100 * @p: the task object
2101 * @nice: unused
2102 *
2103 * Return 0 if write access is permitted
2104 */
2105static int smack_task_setnice(struct task_struct *p, int nice)
2106{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002107 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002108}
2109
2110/**
2111 * smack_task_setioprio - Smack check on setting ioprio
2112 * @p: the task object
2113 * @ioprio: unused
2114 *
2115 * Return 0 if write access is permitted
2116 */
2117static int smack_task_setioprio(struct task_struct *p, int ioprio)
2118{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002119 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002120}
2121
2122/**
2123 * smack_task_getioprio - Smack check on reading ioprio
2124 * @p: the task object
2125 *
2126 * Return 0 if read access is permitted
2127 */
2128static int smack_task_getioprio(struct task_struct *p)
2129{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002130 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002131}
2132
2133/**
2134 * smack_task_setscheduler - Smack check on setting scheduler
2135 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002136 *
2137 * Return 0 if read access is permitted
2138 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002139static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002140{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002141 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002142}
2143
2144/**
2145 * smack_task_getscheduler - Smack check on reading scheduler
2146 * @p: the task object
2147 *
2148 * Return 0 if read access is permitted
2149 */
2150static int smack_task_getscheduler(struct task_struct *p)
2151{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002152 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002153}
2154
2155/**
2156 * smack_task_movememory - Smack check on moving memory
2157 * @p: the task object
2158 *
2159 * Return 0 if write access is permitted
2160 */
2161static int smack_task_movememory(struct task_struct *p)
2162{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002163 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002164}
2165
2166/**
2167 * smack_task_kill - Smack check on signal delivery
2168 * @p: the task object
2169 * @info: unused
2170 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002171 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002172 *
2173 * Return 0 if write access is permitted
2174 *
Casey Schauflere114e472008-02-04 22:29:50 -08002175 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002176static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002177 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002178{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002179 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002180 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002181 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002182 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002183
Rafal Krypa18d872f2016-04-04 11:14:53 +02002184 if (!sig)
2185 return 0; /* null signal; existence test */
2186
Etienne Bassetecfcc532009-04-08 20:40:06 +02002187 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2188 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002189 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002190 * Sending a signal requires that the sender
2191 * can write the receiver.
2192 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002193 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002194 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2195 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002196 return rc;
2197 }
Casey Schauflere114e472008-02-04 22:29:50 -08002198 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002199 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002200 * specific behavior. This is not clean. For one thing
2201 * we can't take privilege into account.
2202 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002203 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002204 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2205 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002206 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002207}
2208
2209/**
Casey Schauflere114e472008-02-04 22:29:50 -08002210 * smack_task_to_inode - copy task smack into the inode blob
2211 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002212 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002213 *
2214 * Sets the smack pointer in the inode security blob
2215 */
2216static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2217{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002218 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002219 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002220
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002221 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002222 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002223}
2224
2225/*
2226 * Socket hooks.
2227 */
2228
2229/**
2230 * smack_sk_alloc_security - Allocate a socket blob
2231 * @sk: the socket
2232 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002233 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002234 *
2235 * Assign Smack pointers to current
2236 *
2237 * Returns 0 on success, -ENOMEM is there's no memory
2238 */
2239static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2240{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002241 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002242 struct socket_smack *ssp;
2243
2244 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2245 if (ssp == NULL)
2246 return -ENOMEM;
2247
jooseong lee08382c92016-11-03 11:54:39 +01002248 /*
2249 * Sockets created by kernel threads receive web label.
2250 */
2251 if (unlikely(current->flags & PF_KTHREAD)) {
2252 ssp->smk_in = &smack_known_web;
2253 ssp->smk_out = &smack_known_web;
2254 } else {
2255 ssp->smk_in = skp;
2256 ssp->smk_out = skp;
2257 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002258 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002259
2260 sk->sk_security = ssp;
2261
2262 return 0;
2263}
2264
2265/**
2266 * smack_sk_free_security - Free a socket blob
2267 * @sk: the socket
2268 *
2269 * Clears the blob pointer
2270 */
2271static void smack_sk_free_security(struct sock *sk)
2272{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302273#ifdef SMACK_IPV6_PORT_LABELING
2274 struct smk_port_label *spp;
2275
2276 if (sk->sk_family == PF_INET6) {
2277 rcu_read_lock();
2278 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2279 if (spp->smk_sock != sk)
2280 continue;
2281 spp->smk_can_reuse = 1;
2282 break;
2283 }
2284 rcu_read_unlock();
2285 }
2286#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002287 kfree(sk->sk_security);
2288}
2289
2290/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002291* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002292* @sip: the object end
2293*
2294* looks for host based access restrictions
2295*
2296* This version will only be appropriate for really small sets of single label
2297* hosts. The caller is responsible for ensuring that the RCU read lock is
2298* taken before calling this function.
2299*
2300* Returns the label of the far end or NULL if it's not special.
2301*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002302static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002303{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002304 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002305 struct in_addr *siap = &sip->sin_addr;
2306
2307 if (siap->s_addr == 0)
2308 return NULL;
2309
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002310 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2311 /*
2312 * we break after finding the first match because
2313 * the list is sorted from longest to shortest mask
2314 * so we have found the most specific match
2315 */
2316 if (snp->smk_host.s_addr ==
2317 (siap->s_addr & snp->smk_mask.s_addr))
2318 return snp->smk_label;
2319
2320 return NULL;
2321}
2322
2323#if IS_ENABLED(CONFIG_IPV6)
2324/*
2325 * smk_ipv6_localhost - Check for local ipv6 host address
2326 * @sip: the address
2327 *
2328 * Returns boolean true if this is the localhost address
2329 */
2330static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2331{
2332 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2333 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2334
2335 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2336 ntohs(be16p[7]) == 1)
2337 return true;
2338 return false;
2339}
2340
2341/**
2342* smack_ipv6host_label - check host based restrictions
2343* @sip: the object end
2344*
2345* looks for host based access restrictions
2346*
2347* This version will only be appropriate for really small sets of single label
2348* hosts. The caller is responsible for ensuring that the RCU read lock is
2349* taken before calling this function.
2350*
2351* Returns the label of the far end or NULL if it's not special.
2352*/
2353static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2354{
2355 struct smk_net6addr *snp;
2356 struct in6_addr *sap = &sip->sin6_addr;
2357 int i;
2358 int found = 0;
2359
2360 /*
2361 * It's local. Don't look for a host label.
2362 */
2363 if (smk_ipv6_localhost(sip))
2364 return NULL;
2365
2366 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002367 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002368 * If the label is NULL the entry has
2369 * been renounced. Ignore it.
2370 */
2371 if (snp->smk_label == NULL)
2372 continue;
2373 /*
Paul Moore07feee82009-03-27 17:10:54 -04002374 * we break after finding the first match because
2375 * the list is sorted from longest to shortest mask
2376 * so we have found the most specific match
2377 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002378 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002379 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2380 snp->smk_host.s6_addr16[i]) {
2381 found = 0;
2382 break;
2383 }
Etienne Basset43031542009-03-27 17:11:01 -04002384 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002385 if (found)
2386 return snp->smk_label;
2387 }
Paul Moore07feee82009-03-27 17:10:54 -04002388
2389 return NULL;
2390}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002391#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002392
2393/**
Casey Schauflere114e472008-02-04 22:29:50 -08002394 * smack_netlabel - Set the secattr on a socket
2395 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002396 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002397 *
2398 * Convert the outbound smack value (smk_out) to a
2399 * secattr and attach it to the socket.
2400 *
2401 * Returns 0 on success or an error code
2402 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002403static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002404{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002405 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002406 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002407 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002408
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002409 /*
2410 * Usually the netlabel code will handle changing the
2411 * packet labeling based on the label.
2412 * The case of a single label host is different, because
2413 * a single label host should never get a labeled packet
2414 * even though the label is usually associated with a packet
2415 * label.
2416 */
2417 local_bh_disable();
2418 bh_lock_sock_nested(sk);
2419
2420 if (ssp->smk_out == smack_net_ambient ||
2421 labeled == SMACK_UNLABELED_SOCKET)
2422 netlbl_sock_delattr(sk);
2423 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002424 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002425 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002426 }
2427
2428 bh_unlock_sock(sk);
2429 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002430
Casey Schauflere114e472008-02-04 22:29:50 -08002431 return rc;
2432}
2433
2434/**
Paul Moore07feee82009-03-27 17:10:54 -04002435 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2436 * @sk: the socket
2437 * @sap: the destination address
2438 *
2439 * Set the correct secattr for the given socket based on the destination
2440 * address and perform any outbound access checks needed.
2441 *
2442 * Returns 0 on success or an error code.
2443 *
2444 */
2445static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2446{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002447 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002448 int rc;
2449 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002450 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002451 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002452 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002453
2454 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002455 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002456 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002457#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002458 struct lsm_network_audit net;
2459
Eric Paris48c62af2012-04-02 13:15:44 -04002460 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2461 ad.a.u.net->family = sap->sin_family;
2462 ad.a.u.net->dport = sap->sin_port;
2463 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002464#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002465 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002466 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002467 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2468 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002469 } else {
2470 sk_lbl = SMACK_CIPSO_SOCKET;
2471 rc = 0;
2472 }
2473 rcu_read_unlock();
2474 if (rc != 0)
2475 return rc;
2476
2477 return smack_netlabel(sk, sk_lbl);
2478}
2479
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002480#if IS_ENABLED(CONFIG_IPV6)
2481/**
2482 * smk_ipv6_check - check Smack access
2483 * @subject: subject Smack label
2484 * @object: object Smack label
2485 * @address: address
2486 * @act: the action being taken
2487 *
2488 * Check an IPv6 access
2489 */
2490static int smk_ipv6_check(struct smack_known *subject,
2491 struct smack_known *object,
2492 struct sockaddr_in6 *address, int act)
2493{
2494#ifdef CONFIG_AUDIT
2495 struct lsm_network_audit net;
2496#endif
2497 struct smk_audit_info ad;
2498 int rc;
2499
2500#ifdef CONFIG_AUDIT
2501 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2502 ad.a.u.net->family = PF_INET6;
2503 ad.a.u.net->dport = ntohs(address->sin6_port);
2504 if (act == SMK_RECEIVING)
2505 ad.a.u.net->v6info.saddr = address->sin6_addr;
2506 else
2507 ad.a.u.net->v6info.daddr = address->sin6_addr;
2508#endif
2509 rc = smk_access(subject, object, MAY_WRITE, &ad);
2510 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2511 return rc;
2512}
2513#endif /* CONFIG_IPV6 */
2514
2515#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002516/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002517 * smk_ipv6_port_label - Smack port access table management
2518 * @sock: socket
2519 * @address: address
2520 *
2521 * Create or update the port list entry
2522 */
2523static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2524{
2525 struct sock *sk = sock->sk;
2526 struct sockaddr_in6 *addr6;
2527 struct socket_smack *ssp = sock->sk->sk_security;
2528 struct smk_port_label *spp;
2529 unsigned short port = 0;
2530
2531 if (address == NULL) {
2532 /*
2533 * This operation is changing the Smack information
2534 * on the bound socket. Take the changes to the port
2535 * as well.
2536 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302537 rcu_read_lock();
2538 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002539 if (sk != spp->smk_sock)
2540 continue;
2541 spp->smk_in = ssp->smk_in;
2542 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302543 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002544 return;
2545 }
2546 /*
2547 * A NULL address is only used for updating existing
2548 * bound entries. If there isn't one, it's OK.
2549 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302550 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002551 return;
2552 }
2553
2554 addr6 = (struct sockaddr_in6 *)address;
2555 port = ntohs(addr6->sin6_port);
2556 /*
2557 * This is a special case that is safely ignored.
2558 */
2559 if (port == 0)
2560 return;
2561
2562 /*
2563 * Look for an existing port list entry.
2564 * This is an indication that a port is getting reused.
2565 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302566 rcu_read_lock();
2567 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302568 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002569 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302570 if (spp->smk_can_reuse != 1) {
2571 rcu_read_unlock();
2572 return;
2573 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002574 spp->smk_port = port;
2575 spp->smk_sock = sk;
2576 spp->smk_in = ssp->smk_in;
2577 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302578 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302579 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002580 return;
2581 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302582 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002583 /*
2584 * A new port entry is required.
2585 */
2586 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2587 if (spp == NULL)
2588 return;
2589
2590 spp->smk_port = port;
2591 spp->smk_sock = sk;
2592 spp->smk_in = ssp->smk_in;
2593 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302594 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302595 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002596
Vishal Goel3c7ce342016-11-23 10:31:08 +05302597 mutex_lock(&smack_ipv6_lock);
2598 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2599 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002600 return;
2601}
2602
2603/**
2604 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002605 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002606 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002607 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002608 *
2609 * Create or update the port list entry
2610 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002611static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002612 int act)
2613{
Casey Schauflerc6739442013-05-22 18:42:56 -07002614 struct smk_port_label *spp;
2615 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002616 struct smack_known *skp = NULL;
2617 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002618 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002619
2620 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002621 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002622 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002624 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002625 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002626 }
2627
2628 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002629 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002630 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002631 if (skp != NULL && object != NULL)
2632 return smk_ipv6_check(skp, object, address, act);
2633 if (skp == NULL)
2634 skp = smack_net_ambient;
2635 if (object == NULL)
2636 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002637
2638 /*
2639 * It's remote, so port lookup does no good.
2640 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002641 if (!smk_ipv6_localhost(address))
2642 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002643
2644 /*
2645 * It's local so the send check has to have passed.
2646 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002647 if (act == SMK_RECEIVING)
2648 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002649
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002650 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302651 rcu_read_lock();
2652 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302653 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002654 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002655 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002656 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002657 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002658 break;
2659 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302660 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002661
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002662 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002663}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002664#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002665
2666/**
Casey Schauflere114e472008-02-04 22:29:50 -08002667 * smack_inode_setsecurity - set smack xattrs
2668 * @inode: the object
2669 * @name: attribute name
2670 * @value: attribute value
2671 * @size: size of the attribute
2672 * @flags: unused
2673 *
2674 * Sets the named attribute in the appropriate blob
2675 *
2676 * Returns 0 on success, or an error code
2677 */
2678static int smack_inode_setsecurity(struct inode *inode, const char *name,
2679 const void *value, size_t size, int flags)
2680{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002681 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002682 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002683 struct socket_smack *ssp;
2684 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002685 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002686
Casey Schauflerf7112e62012-05-06 15:22:02 -07002687 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302688 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002689
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002690 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002691 if (IS_ERR(skp))
2692 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002693
2694 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002695 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002696 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002697 return 0;
2698 }
2699 /*
2700 * The rest of the Smack xattrs are only on sockets.
2701 */
2702 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2703 return -EOPNOTSUPP;
2704
2705 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002706 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002707 return -EOPNOTSUPP;
2708
2709 ssp = sock->sk->sk_security;
2710
2711 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002712 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002713 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002714 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002715 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002716 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2717 if (rc != 0)
2718 printk(KERN_WARNING
2719 "Smack: \"%s\" netlbl error %d.\n",
2720 __func__, -rc);
2721 }
Casey Schauflere114e472008-02-04 22:29:50 -08002722 } else
2723 return -EOPNOTSUPP;
2724
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002725#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002726 if (sock->sk->sk_family == PF_INET6)
2727 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002728#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002729
Casey Schauflere114e472008-02-04 22:29:50 -08002730 return 0;
2731}
2732
2733/**
2734 * smack_socket_post_create - finish socket setup
2735 * @sock: the socket
2736 * @family: protocol family
2737 * @type: unused
2738 * @protocol: unused
2739 * @kern: unused
2740 *
2741 * Sets the netlabel information on the socket
2742 *
2743 * Returns 0 on success, and error code otherwise
2744 */
2745static int smack_socket_post_create(struct socket *sock, int family,
2746 int type, int protocol, int kern)
2747{
Marcin Lis74123012015-01-22 15:40:33 +01002748 struct socket_smack *ssp;
2749
2750 if (sock->sk == NULL)
2751 return 0;
2752
2753 /*
2754 * Sockets created by kernel threads receive web label.
2755 */
2756 if (unlikely(current->flags & PF_KTHREAD)) {
2757 ssp = sock->sk->sk_security;
2758 ssp->smk_in = &smack_known_web;
2759 ssp->smk_out = &smack_known_web;
2760 }
2761
2762 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002763 return 0;
2764 /*
2765 * Set the outbound netlbl.
2766 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002767 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2768}
2769
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002770/**
2771 * smack_socket_socketpair - create socket pair
2772 * @socka: one socket
2773 * @sockb: another socket
2774 *
2775 * Cross reference the peer labels for SO_PEERSEC
2776 *
luanshia1a07f22019-07-05 10:35:20 +08002777 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002778 */
2779static int smack_socket_socketpair(struct socket *socka,
2780 struct socket *sockb)
2781{
2782 struct socket_smack *asp = socka->sk->sk_security;
2783 struct socket_smack *bsp = sockb->sk->sk_security;
2784
2785 asp->smk_packet = bsp->smk_out;
2786 bsp->smk_packet = asp->smk_out;
2787
2788 return 0;
2789}
2790
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002791#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002792/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002793 * smack_socket_bind - record port binding information.
2794 * @sock: the socket
2795 * @address: the port address
2796 * @addrlen: size of the address
2797 *
2798 * Records the label bound to a port.
2799 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002800 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002801 */
2802static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2803 int addrlen)
2804{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002805 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2806 if (addrlen < SIN6_LEN_RFC2133 ||
2807 address->sa_family != AF_INET6)
2808 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002809 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002810 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002811 return 0;
2812}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002813#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002814
2815/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002816 * smack_socket_connect - connect access check
2817 * @sock: the socket
2818 * @sap: the other end
2819 * @addrlen: size of sap
2820 *
2821 * Verifies that a connection may be possible
2822 *
2823 * Returns 0 on success, and error code otherwise
2824 */
2825static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2826 int addrlen)
2827{
Casey Schauflerc6739442013-05-22 18:42:56 -07002828 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002829
Casey Schauflerc6739442013-05-22 18:42:56 -07002830 if (sock->sk == NULL)
2831 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002832 if (sock->sk->sk_family != PF_INET &&
2833 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2834 return 0;
2835 if (addrlen < offsetofend(struct sockaddr, sa_family))
2836 return 0;
2837 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2838 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002839#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002840 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002841#endif
2842
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002843 if (addrlen < SIN6_LEN_RFC2133)
2844 return 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002845#ifdef SMACK_IPV6_SECMARK_LABELING
2846 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002847 if (rsp != NULL) {
2848 struct socket_smack *ssp = sock->sk->sk_security;
2849
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002850 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002851 SMK_CONNECTING);
2852 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002853#endif
2854#ifdef SMACK_IPV6_PORT_LABELING
2855 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2856#endif
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002857 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002858 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002859 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2860 return 0;
2861 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002862 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002863}
2864
2865/**
2866 * smack_flags_to_may - convert S_ to MAY_ values
2867 * @flags: the S_ value
2868 *
2869 * Returns the equivalent MAY_ value
2870 */
2871static int smack_flags_to_may(int flags)
2872{
2873 int may = 0;
2874
2875 if (flags & S_IRUGO)
2876 may |= MAY_READ;
2877 if (flags & S_IWUGO)
2878 may |= MAY_WRITE;
2879 if (flags & S_IXUGO)
2880 may |= MAY_EXEC;
2881
2882 return may;
2883}
2884
2885/**
2886 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2887 * @msg: the object
2888 *
2889 * Returns 0
2890 */
2891static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2892{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002893 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002894
Casey Schauflerecd5f822018-11-20 11:55:02 -08002895 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002896 return 0;
2897}
2898
2899/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002900 * smack_of_ipc - the smack pointer for the ipc
2901 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002902 *
2903 * Returns a pointer to the smack value
2904 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002905static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002906{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002907 struct smack_known **blob = smack_ipc(isp);
2908
2909 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002910}
2911
2912/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002913 * smack_ipc_alloc_security - Set the security blob for ipc
2914 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002915 *
2916 * Returns 0
2917 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002918static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002919{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002920 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002921
Casey Schaufler019bcca2018-09-21 17:19:54 -07002922 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002923 return 0;
2924}
2925
2926/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002927 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002928 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002929 * @access : access requested
2930 *
2931 * Returns 0 if current has the requested access, error code otherwise
2932 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002933static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002934{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002935 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002936 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002937 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002938
2939#ifdef CONFIG_AUDIT
2940 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002941 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002942#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002943 rc = smk_curacc(ssp, access, &ad);
2944 rc = smk_bu_current("shm", ssp, access, rc);
2945 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002946}
2947
2948/**
Casey Schauflere114e472008-02-04 22:29:50 -08002949 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002950 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002951 * @shmflg: access requested
2952 *
2953 * Returns 0 if current has the requested access, error code otherwise
2954 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002955static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002956{
Casey Schauflere114e472008-02-04 22:29:50 -08002957 int may;
2958
2959 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002960 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002961}
2962
2963/**
2964 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002965 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002966 * @cmd: what it wants to do
2967 *
2968 * Returns 0 if current has the requested access, error code otherwise
2969 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002970static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002971{
Casey Schauflere114e472008-02-04 22:29:50 -08002972 int may;
2973
2974 switch (cmd) {
2975 case IPC_STAT:
2976 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002977 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002978 may = MAY_READ;
2979 break;
2980 case IPC_SET:
2981 case SHM_LOCK:
2982 case SHM_UNLOCK:
2983 case IPC_RMID:
2984 may = MAY_READWRITE;
2985 break;
2986 case IPC_INFO:
2987 case SHM_INFO:
2988 /*
2989 * System level information.
2990 */
2991 return 0;
2992 default:
2993 return -EINVAL;
2994 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002995 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002996}
2997
2998/**
2999 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003000 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003001 * @shmaddr: unused
3002 * @shmflg: access requested
3003 *
3004 * Returns 0 if current has the requested access, error code otherwise
3005 */
luanshia1a07f22019-07-05 10:35:20 +08003006static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003007 int shmflg)
3008{
Casey Schauflere114e472008-02-04 22:29:50 -08003009 int may;
3010
3011 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003012 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003013}
3014
3015/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003016 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003017 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003018 * @access : access requested
3019 *
3020 * Returns 0 if current has the requested access, error code otherwise
3021 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003022static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003023{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003024 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003025 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003026 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003027
3028#ifdef CONFIG_AUDIT
3029 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003030 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003031#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003032 rc = smk_curacc(ssp, access, &ad);
3033 rc = smk_bu_current("sem", ssp, access, rc);
3034 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003035}
3036
3037/**
Casey Schauflere114e472008-02-04 22:29:50 -08003038 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003039 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003040 * @semflg: access requested
3041 *
3042 * Returns 0 if current has the requested access, error code otherwise
3043 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003044static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003045{
Casey Schauflere114e472008-02-04 22:29:50 -08003046 int may;
3047
3048 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003049 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003050}
3051
3052/**
3053 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003054 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003055 * @cmd: what it wants to do
3056 *
3057 * Returns 0 if current has the requested access, error code otherwise
3058 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003059static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003060{
Casey Schauflere114e472008-02-04 22:29:50 -08003061 int may;
3062
3063 switch (cmd) {
3064 case GETPID:
3065 case GETNCNT:
3066 case GETZCNT:
3067 case GETVAL:
3068 case GETALL:
3069 case IPC_STAT:
3070 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003071 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003072 may = MAY_READ;
3073 break;
3074 case SETVAL:
3075 case SETALL:
3076 case IPC_RMID:
3077 case IPC_SET:
3078 may = MAY_READWRITE;
3079 break;
3080 case IPC_INFO:
3081 case SEM_INFO:
3082 /*
3083 * System level information
3084 */
3085 return 0;
3086 default:
3087 return -EINVAL;
3088 }
3089
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003090 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003091}
3092
3093/**
3094 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003095 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003096 * @sops: unused
3097 * @nsops: unused
3098 * @alter: unused
3099 *
3100 * Treated as read and write in all cases.
3101 *
3102 * Returns 0 if access is allowed, error code otherwise
3103 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003104static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003105 unsigned nsops, int alter)
3106{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003107 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003108}
3109
3110/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003111 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003112 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003113 * @access : access requested
3114 *
3115 * return 0 if current has access, error otherwise
3116 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003117static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003118{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003120 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003121 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003122
3123#ifdef CONFIG_AUDIT
3124 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003125 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003126#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003127 rc = smk_curacc(msp, access, &ad);
3128 rc = smk_bu_current("msq", msp, access, rc);
3129 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003130}
3131
3132/**
Casey Schauflere114e472008-02-04 22:29:50 -08003133 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003134 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003135 * @msqflg: access requested
3136 *
3137 * Returns 0 if current has the requested access, error code otherwise
3138 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003139static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003140{
Casey Schauflere114e472008-02-04 22:29:50 -08003141 int may;
3142
3143 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003144 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003145}
3146
3147/**
3148 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003149 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003150 * @cmd: what it wants to do
3151 *
3152 * Returns 0 if current has the requested access, error code otherwise
3153 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003154static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003155{
Casey Schauflere114e472008-02-04 22:29:50 -08003156 int may;
3157
3158 switch (cmd) {
3159 case IPC_STAT:
3160 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003161 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003162 may = MAY_READ;
3163 break;
3164 case IPC_SET:
3165 case IPC_RMID:
3166 may = MAY_READWRITE;
3167 break;
3168 case IPC_INFO:
3169 case MSG_INFO:
3170 /*
3171 * System level information
3172 */
3173 return 0;
3174 default:
3175 return -EINVAL;
3176 }
3177
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003178 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003179}
3180
3181/**
3182 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003183 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003184 * @msg: unused
3185 * @msqflg: access requested
3186 *
3187 * Returns 0 if current has the requested access, error code otherwise
3188 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003189static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003190 int msqflg)
3191{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003192 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003193
Etienne Bassetecfcc532009-04-08 20:40:06 +02003194 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003195 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003196}
3197
3198/**
3199 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003200 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003201 * @msg: unused
3202 * @target: unused
3203 * @type: unused
3204 * @mode: unused
3205 *
3206 * Returns 0 if current has read and write access, error code otherwise
3207 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003208static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003209 struct task_struct *target, long type, int mode)
3210{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003211 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003212}
3213
3214/**
3215 * smack_ipc_permission - Smack access for ipc_permission()
3216 * @ipp: the object permissions
3217 * @flag: access requested
3218 *
3219 * Returns 0 if current has read and write access, error code otherwise
3220 */
3221static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3222{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003223 struct smack_known **blob = smack_ipc(ipp);
3224 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003225 int may = smack_flags_to_may(flag);
3226 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003227 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003228
Etienne Bassetecfcc532009-04-08 20:40:06 +02003229#ifdef CONFIG_AUDIT
3230 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3231 ad.a.u.ipc_id = ipp->id;
3232#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003233 rc = smk_curacc(iskp, may, &ad);
3234 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003235 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003236}
3237
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003238/**
3239 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003240 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003241 * @secid: where result will be saved
3242 */
3243static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3244{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003245 struct smack_known **blob = smack_ipc(ipp);
3246 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003247
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003248 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003249}
3250
Casey Schauflere114e472008-02-04 22:29:50 -08003251/**
3252 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003253 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003254 * @inode: the object
3255 *
3256 * Set the inode's security blob if it hasn't been done already.
3257 */
3258static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3259{
3260 struct super_block *sbp;
3261 struct superblock_smack *sbsp;
3262 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003263 struct smack_known *skp;
3264 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003265 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003266 char trattr[TRANS_TRUE_SIZE];
3267 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003268 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003269 struct dentry *dp;
3270
3271 if (inode == NULL)
3272 return;
3273
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003274 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003275
3276 mutex_lock(&isp->smk_lock);
3277 /*
3278 * If the inode is already instantiated
3279 * take the quick way out
3280 */
3281 if (isp->smk_flags & SMK_INODE_INSTANT)
3282 goto unlockandout;
3283
3284 sbp = inode->i_sb;
3285 sbsp = sbp->s_security;
3286 /*
3287 * We're going to use the superblock default label
3288 * if there's no label on the file.
3289 */
3290 final = sbsp->smk_default;
3291
3292 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003293 * If this is the root inode the superblock
3294 * may be in the process of initialization.
3295 * If that is the case use the root value out
3296 * of the superblock.
3297 */
3298 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003299 switch (sbp->s_magic) {
3300 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003301 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003302 /*
3303 * The cgroup filesystem is never mounted,
3304 * so there's no opportunity to set the mount
3305 * options.
3306 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003307 sbsp->smk_root = &smack_known_star;
3308 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003309 isp->smk_inode = sbsp->smk_root;
3310 break;
3311 case TMPFS_MAGIC:
3312 /*
3313 * What about shmem/tmpfs anonymous files with dentry
3314 * obtained from d_alloc_pseudo()?
3315 */
3316 isp->smk_inode = smk_of_current();
3317 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003318 case PIPEFS_MAGIC:
3319 isp->smk_inode = smk_of_current();
3320 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003321 case SOCKFS_MAGIC:
3322 /*
3323 * Socket access is controlled by the socket
3324 * structures associated with the task involved.
3325 */
3326 isp->smk_inode = &smack_known_star;
3327 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003328 default:
3329 isp->smk_inode = sbsp->smk_root;
3330 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003331 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003332 isp->smk_flags |= SMK_INODE_INSTANT;
3333 goto unlockandout;
3334 }
3335
3336 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003337 * This is pretty hackish.
3338 * Casey says that we shouldn't have to do
3339 * file system specific code, but it does help
3340 * with keeping it simple.
3341 */
3342 switch (sbp->s_magic) {
3343 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003344 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003345 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003346 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003347 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003348 * that the smack file system doesn't do
3349 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003350 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003351 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003352 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003353 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003354 break;
3355 case DEVPTS_SUPER_MAGIC:
3356 /*
3357 * devpts seems content with the label of the task.
3358 * Programs that change smack have to treat the
3359 * pty with respect.
3360 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003361 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003362 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003363 case PROC_SUPER_MAGIC:
3364 /*
3365 * Casey says procfs appears not to care.
3366 * The superblock default suffices.
3367 */
3368 break;
3369 case TMPFS_MAGIC:
3370 /*
3371 * Device labels should come from the filesystem,
3372 * but watch out, because they're volitile,
3373 * getting recreated on every reboot.
3374 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003375 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003376 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003377 * If a smack value has been set we want to use it,
3378 * but since tmpfs isn't giving us the opportunity
3379 * to set mount options simulate setting the
3380 * superblock default.
3381 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003382 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003383 default:
3384 /*
3385 * This isn't an understood special case.
3386 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003387 */
3388
3389 /*
3390 * UNIX domain sockets use lower level socket data.
3391 */
3392 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003393 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003394 break;
3395 }
3396 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003397 * No xattr support means, alas, no SMACK label.
3398 * Use the aforeapplied default.
3399 * It would be curious if the label of the task
3400 * does not match that assigned.
3401 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003402 if (!(inode->i_opflags & IOP_XATTR))
3403 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003404 /*
3405 * Get the dentry for xattr.
3406 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003407 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003408 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003409 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003410 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003411
3412 /*
3413 * Transmuting directory
3414 */
3415 if (S_ISDIR(inode->i_mode)) {
3416 /*
3417 * If this is a new directory and the label was
3418 * transmuted when the inode was initialized
3419 * set the transmute attribute on the directory
3420 * and mark the inode.
3421 *
3422 * If there is a transmute attribute on the
3423 * directory mark the inode.
3424 */
3425 if (isp->smk_flags & SMK_INODE_CHANGED) {
3426 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003427 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003428 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003429 TRANS_TRUE, TRANS_TRUE_SIZE,
3430 0);
3431 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003432 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003433 XATTR_NAME_SMACKTRANSMUTE, trattr,
3434 TRANS_TRUE_SIZE);
3435 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3436 TRANS_TRUE_SIZE) != 0)
3437 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003438 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003439 if (rc >= 0)
3440 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003441 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003442 /*
3443 * Don't let the exec or mmap label be "*" or "@".
3444 */
3445 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3446 if (IS_ERR(skp) || skp == &smack_known_star ||
3447 skp == &smack_known_web)
3448 skp = NULL;
3449 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003450
Casey Schaufler19760ad2013-12-16 16:27:26 -08003451 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003452 if (IS_ERR(skp) || skp == &smack_known_star ||
3453 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003454 skp = NULL;
3455 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003456
Casey Schauflere114e472008-02-04 22:29:50 -08003457 dput(dp);
3458 break;
3459 }
3460
3461 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003462 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003463 else
3464 isp->smk_inode = final;
3465
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003466 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003467
3468unlockandout:
3469 mutex_unlock(&isp->smk_lock);
3470 return;
3471}
3472
3473/**
3474 * smack_getprocattr - Smack process attribute access
3475 * @p: the object task
3476 * @name: the name of the attribute in /proc/.../attr
3477 * @value: where to put the result
3478 *
3479 * Places a copy of the task Smack into value
3480 *
3481 * Returns the length of the smack label or an error code
3482 */
3483static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3484{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003485 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003486 char *cp;
3487 int slen;
3488
3489 if (strcmp(name, "current") != 0)
3490 return -EINVAL;
3491
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003492 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003493 if (cp == NULL)
3494 return -ENOMEM;
3495
3496 slen = strlen(cp);
3497 *value = cp;
3498 return slen;
3499}
3500
3501/**
3502 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003503 * @name: the name of the attribute in /proc/.../attr
3504 * @value: the value to set
3505 * @size: the size of the value
3506 *
3507 * Sets the Smack value of the task. Only setting self
3508 * is permitted and only with privilege
3509 *
3510 * Returns the length of the smack label or an error code
3511 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003512static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003513{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003514 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003515 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003516 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003517 struct smack_known_list_elem *sklep;
3518 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003519
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003520 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003521 return -EPERM;
3522
Casey Schauflerf7112e62012-05-06 15:22:02 -07003523 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003524 return -EINVAL;
3525
3526 if (strcmp(name, "current") != 0)
3527 return -EINVAL;
3528
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003529 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003530 if (IS_ERR(skp))
3531 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003532
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003533 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303534 * No process is ever allowed the web ("@") label
3535 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003536 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303537 if (skp == &smack_known_web || skp == &smack_known_star)
3538 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003539
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003540 if (!smack_privileged(CAP_MAC_ADMIN)) {
3541 rc = -EPERM;
3542 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3543 if (sklep->smk_label == skp) {
3544 rc = 0;
3545 break;
3546 }
3547 if (rc)
3548 return rc;
3549 }
3550
David Howellsd84f4f92008-11-14 10:39:23 +11003551 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003552 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003553 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003554
Casey Schauflerb17103a2018-11-09 16:12:56 -08003555 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003556 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003557 /*
3558 * process can change its label only once
3559 */
3560 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003561
David Howellsd84f4f92008-11-14 10:39:23 +11003562 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003563 return size;
3564}
3565
3566/**
3567 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003568 * @sock: one sock
3569 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003570 * @newsk: unused
3571 *
3572 * Return 0 if a subject with the smack of sock could access
3573 * an object with the smack of other, otherwise an error code
3574 */
David S. Miller3610cda2011-01-05 15:38:53 -08003575static int smack_unix_stream_connect(struct sock *sock,
3576 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003577{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003578 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003579 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003580 struct socket_smack *ssp = sock->sk_security;
3581 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003582 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003583 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003584 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003585#ifdef CONFIG_AUDIT
3586 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003587#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003588
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003589 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3590 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003591 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003592#ifdef CONFIG_AUDIT
3593 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3594 smk_ad_setfield_u_net_sk(&ad, other);
3595#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003596 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3597 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003598 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003599 okp = osp->smk_out;
3600 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003601 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003602 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003603 MAY_WRITE, rc);
3604 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003605 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003606
Casey Schaufler975d5e52011-09-26 14:43:39 -07003607 /*
3608 * Cross reference the peer labels for SO_PEERSEC.
3609 */
3610 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003611 nsp->smk_packet = ssp->smk_out;
3612 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003613 }
3614
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003615 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003616}
3617
3618/**
3619 * smack_unix_may_send - Smack access on UDS
3620 * @sock: one socket
3621 * @other: the other socket
3622 *
3623 * Return 0 if a subject with the smack of sock could access
3624 * an object with the smack of other, otherwise an error code
3625 */
3626static int smack_unix_may_send(struct socket *sock, struct socket *other)
3627{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003628 struct socket_smack *ssp = sock->sk->sk_security;
3629 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003630 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003631 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003632
Kees Cook923e9a12012-04-10 13:26:44 -07003633#ifdef CONFIG_AUDIT
3634 struct lsm_network_audit net;
3635
Eric Paris48c62af2012-04-02 13:15:44 -04003636 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003637 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003638#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003639
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003640 if (smack_privileged(CAP_MAC_OVERRIDE))
3641 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003642
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003643 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3644 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003645 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003646}
3647
3648/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003649 * smack_socket_sendmsg - Smack check based on destination host
3650 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003651 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003652 * @size: the size of the message
3653 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003654 * Return 0 if the current subject can write to the destination host.
3655 * For IPv4 this is only a question if the destination is a single label host.
3656 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003657 */
3658static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3659 int size)
3660{
3661 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003662#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003663 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003664#endif
3665#ifdef SMACK_IPV6_SECMARK_LABELING
3666 struct socket_smack *ssp = sock->sk->sk_security;
3667 struct smack_known *rsp;
3668#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003669 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003670
3671 /*
3672 * Perfectly reasonable for this to be NULL
3673 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003674 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003675 return 0;
3676
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003677 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003678 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003679 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3680 sip->sin_family != AF_INET)
3681 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003682 rc = smack_netlabel_send(sock->sk, sip);
3683 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003684#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003685 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003686 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3687 sap->sin6_family != AF_INET6)
3688 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003689#ifdef SMACK_IPV6_SECMARK_LABELING
3690 rsp = smack_ipv6host_label(sap);
3691 if (rsp != NULL)
3692 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3693 SMK_CONNECTING);
3694#endif
3695#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003696 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003697#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003698#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003699 break;
3700 }
3701 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003702}
3703
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003704/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003705 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003706 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003707 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003708 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003709 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003710 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003711static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3712 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003713{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003714 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003715 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003716 int acat;
3717 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003718
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003719 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003720 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003721 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003722 * If there are flags but no level netlabel isn't
3723 * behaving the way we expect it to.
3724 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003725 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003726 * Without guidance regarding the smack value
3727 * for the packet fall back on the network
3728 * ambient value.
3729 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003730 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303731 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003732 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003733 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003734 /*
3735 * Compare the catsets. Use the netlbl APIs.
3736 */
3737 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3738 if ((skp->smk_netlabel.flags &
3739 NETLBL_SECATTR_MLS_CAT) == 0)
3740 found = 1;
3741 break;
3742 }
3743 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003744 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3745 acat + 1);
3746 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003747 skp->smk_netlabel.attr.mls.cat,
3748 kcat + 1);
3749 if (acat < 0 || kcat < 0)
3750 break;
3751 }
3752 if (acat == kcat) {
3753 found = 1;
3754 break;
3755 }
Casey Schauflere114e472008-02-04 22:29:50 -08003756 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003757 rcu_read_unlock();
3758
3759 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003760 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003761
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003762 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003763 return &smack_known_web;
3764 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003765 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003766 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003767 /*
3768 * Looks like a fallback, which gives us a secid.
3769 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003770 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003771 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003772 * Without guidance regarding the smack value
3773 * for the packet fall back on the network
3774 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003775 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003776 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003777}
3778
Casey Schaufler69f287a2014-12-12 17:08:40 -08003779#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003780static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003781{
Casey Schauflerc6739442013-05-22 18:42:56 -07003782 u8 nexthdr;
3783 int offset;
3784 int proto = -EINVAL;
3785 struct ipv6hdr _ipv6h;
3786 struct ipv6hdr *ip6;
3787 __be16 frag_off;
3788 struct tcphdr _tcph, *th;
3789 struct udphdr _udph, *uh;
3790 struct dccp_hdr _dccph, *dh;
3791
3792 sip->sin6_port = 0;
3793
3794 offset = skb_network_offset(skb);
3795 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3796 if (ip6 == NULL)
3797 return -EINVAL;
3798 sip->sin6_addr = ip6->saddr;
3799
3800 nexthdr = ip6->nexthdr;
3801 offset += sizeof(_ipv6h);
3802 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3803 if (offset < 0)
3804 return -EINVAL;
3805
3806 proto = nexthdr;
3807 switch (proto) {
3808 case IPPROTO_TCP:
3809 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3810 if (th != NULL)
3811 sip->sin6_port = th->source;
3812 break;
3813 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003814 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003815 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3816 if (uh != NULL)
3817 sip->sin6_port = uh->source;
3818 break;
3819 case IPPROTO_DCCP:
3820 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3821 if (dh != NULL)
3822 sip->sin6_port = dh->dccph_sport;
3823 break;
3824 }
3825 return proto;
3826}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003827#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003828
Casey Schauflere114e472008-02-04 22:29:50 -08003829/**
3830 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3831 * @sk: socket
3832 * @skb: packet
3833 *
3834 * Returns 0 if the packet should be delivered, an error code otherwise
3835 */
3836static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3837{
3838 struct netlbl_lsm_secattr secattr;
3839 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003840 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003841 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003842 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003843 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003844#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003845 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003846#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003847#if IS_ENABLED(CONFIG_IPV6)
3848 struct sockaddr_in6 sadd;
3849 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003850
3851 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3852 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003853#endif /* CONFIG_IPV6 */
3854
Piotr Sawicki129a9982018-07-19 11:42:58 +02003855 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003856 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003857#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3858 /*
3859 * If there is a secmark use it rather than the CIPSO label.
3860 * If there is no secmark fall back to CIPSO.
3861 * The secmark is assumed to reflect policy better.
3862 */
3863 if (skb && skb->secmark != 0) {
3864 skp = smack_from_secid(skb->secmark);
3865 goto access_check;
3866 }
3867#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003868 /*
3869 * Translate what netlabel gave us.
3870 */
3871 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003872
Piotr Sawicki129a9982018-07-19 11:42:58 +02003873 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003874 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003875 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003876 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003877 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003878
Casey Schauflerc6739442013-05-22 18:42:56 -07003879 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003880
Casey Schaufler69f287a2014-12-12 17:08:40 -08003881#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3882access_check:
3883#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003884#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003885 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003886 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003887 ad.a.u.net->netif = skb->skb_iif;
3888 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003889#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003890 /*
3891 * Receiving a packet requires that the other end
3892 * be able to write here. Read access is not required.
3893 * This is the simplist possible security model
3894 * for networking.
3895 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003896 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3897 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003898 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003899 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003900 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003901 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003902#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003903 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003904 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003905 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3906 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003907 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003908#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003909 if (skb && skb->secmark != 0)
3910 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003911 else if (smk_ipv6_localhost(&sadd))
3912 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003913 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003914 skp = smack_ipv6host_label(&sadd);
3915 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916 skp = smack_net_ambient;
Jia-Ju Bai3f4287e2019-07-23 18:00:15 +08003917 if (skb == NULL)
3918 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003919#ifdef CONFIG_AUDIT
3920 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003921 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003922 ad.a.u.net->netif = skb->skb_iif;
3923 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3924#endif /* CONFIG_AUDIT */
3925 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3926 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3927 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003928#endif /* SMACK_IPV6_SECMARK_LABELING */
3929#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003930 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003931#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003932 if (rc != 0)
3933 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3934 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003935 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003936#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003937 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003938
Paul Moorea8134292008-10-10 10:16:31 -04003939 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003940}
3941
3942/**
3943 * smack_socket_getpeersec_stream - pull in packet label
3944 * @sock: the socket
3945 * @optval: user's destination
3946 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003947 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003948 *
3949 * returns zero on success, an error code otherwise
3950 */
3951static int smack_socket_getpeersec_stream(struct socket *sock,
3952 char __user *optval,
3953 int __user *optlen, unsigned len)
3954{
3955 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003956 char *rcp = "";
3957 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003958 int rc = 0;
3959
3960 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003961 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003962 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003963 slen = strlen(rcp) + 1;
3964 }
Casey Schauflere114e472008-02-04 22:29:50 -08003965
3966 if (slen > len)
3967 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003968 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003969 rc = -EFAULT;
3970
3971 if (put_user(slen, optlen) != 0)
3972 rc = -EFAULT;
3973
3974 return rc;
3975}
3976
3977
3978/**
3979 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003980 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003981 * @skb: packet data
3982 * @secid: pointer to where to put the secid of the packet
3983 *
3984 * Sets the netlabel socket state on sk from parent
3985 */
3986static int smack_socket_getpeersec_dgram(struct socket *sock,
3987 struct sk_buff *skb, u32 *secid)
3988
3989{
3990 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003991 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003992 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003993 int family = PF_UNSPEC;
3994 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003995 int rc;
3996
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003997 if (skb != NULL) {
3998 if (skb->protocol == htons(ETH_P_IP))
3999 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004000#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004001 else if (skb->protocol == htons(ETH_P_IPV6))
4002 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004003#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004004 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004005 if (family == PF_UNSPEC && sock != NULL)
4006 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004007
Casey Schaufler69f287a2014-12-12 17:08:40 -08004008 switch (family) {
4009 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004010 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004011 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004012 break;
4013 case PF_INET:
4014#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4015 s = skb->secmark;
4016 if (s != 0)
4017 break;
4018#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004019 /*
4020 * Translate what netlabel gave us.
4021 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004022 if (sock != NULL && sock->sk != NULL)
4023 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004024 netlbl_secattr_init(&secattr);
4025 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4026 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004027 skp = smack_from_secattr(&secattr, ssp);
4028 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004029 }
4030 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004031 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004032 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004033#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004034 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004035#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004036 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004037 }
4038 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004039 if (s == 0)
4040 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004041 return 0;
4042}
4043
4044/**
Paul Moore07feee82009-03-27 17:10:54 -04004045 * smack_sock_graft - Initialize a newly created socket with an existing sock
4046 * @sk: child sock
4047 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004048 *
Paul Moore07feee82009-03-27 17:10:54 -04004049 * Set the smk_{in,out} state of an existing sock based on the process that
4050 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004051 */
4052static void smack_sock_graft(struct sock *sk, struct socket *parent)
4053{
4054 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004055 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004056
Paul Moore07feee82009-03-27 17:10:54 -04004057 if (sk == NULL ||
4058 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004059 return;
4060
4061 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004062 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004063 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004064 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004065}
4066
4067/**
4068 * smack_inet_conn_request - Smack access check on connect
4069 * @sk: socket involved
4070 * @skb: packet
4071 * @req: unused
4072 *
4073 * Returns 0 if a task with the packet label could write to
4074 * the socket, otherwise an error code
4075 */
4076static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4077 struct request_sock *req)
4078{
Paul Moore07feee82009-03-27 17:10:54 -04004079 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004080 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004081 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004082 struct netlbl_lsm_secattr secattr;
4083 struct sockaddr_in addr;
4084 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004085 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004086 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004087 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004088#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004089 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004090#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004091
Casey Schaufler69f287a2014-12-12 17:08:40 -08004092#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004093 if (family == PF_INET6) {
4094 /*
4095 * Handle mapped IPv4 packets arriving
4096 * via IPv6 sockets. Don't set up netlabel
4097 * processing on IPv6.
4098 */
4099 if (skb->protocol == htons(ETH_P_IP))
4100 family = PF_INET;
4101 else
4102 return 0;
4103 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004104#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004105
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004106#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4107 /*
4108 * If there is a secmark use it rather than the CIPSO label.
4109 * If there is no secmark fall back to CIPSO.
4110 * The secmark is assumed to reflect policy better.
4111 */
4112 if (skb && skb->secmark != 0) {
4113 skp = smack_from_secid(skb->secmark);
4114 goto access_check;
4115 }
4116#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4117
Paul Moore07feee82009-03-27 17:10:54 -04004118 netlbl_secattr_init(&secattr);
4119 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004120 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004121 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004122 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004123 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004124 netlbl_secattr_destroy(&secattr);
4125
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004126#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4127access_check:
4128#endif
4129
Etienne Bassetecfcc532009-04-08 20:40:06 +02004130#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004131 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4132 ad.a.u.net->family = family;
4133 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004134 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4135#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004136 /*
Paul Moore07feee82009-03-27 17:10:54 -04004137 * Receiving a packet requires that the other end be able to write
4138 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004139 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004140 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4141 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004142 if (rc != 0)
4143 return rc;
4144
4145 /*
4146 * Save the peer's label in the request_sock so we can later setup
4147 * smk_packet in the child socket so that SO_PEERCRED can report it.
4148 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004149 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004150
4151 /*
4152 * We need to decide if we want to label the incoming connection here
4153 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004154 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004155 */
4156 hdr = ip_hdr(skb);
4157 addr.sin_addr.s_addr = hdr->saddr;
4158 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004159 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004160 rcu_read_unlock();
4161
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004162 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004163 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004164 else
Paul Moore07feee82009-03-27 17:10:54 -04004165 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004166
4167 return rc;
4168}
4169
Paul Moore07feee82009-03-27 17:10:54 -04004170/**
4171 * smack_inet_csk_clone - Copy the connection information to the new socket
4172 * @sk: the new socket
4173 * @req: the connection's request_sock
4174 *
4175 * Transfer the connection's peer label to the newly created socket.
4176 */
4177static void smack_inet_csk_clone(struct sock *sk,
4178 const struct request_sock *req)
4179{
4180 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004181 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004182
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004183 if (req->peer_secid != 0) {
4184 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004185 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004186 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004187 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004188}
4189
Casey Schauflere114e472008-02-04 22:29:50 -08004190/*
4191 * Key management security hooks
4192 *
4193 * Casey has not tested key support very heavily.
4194 * The permission check is most likely too restrictive.
4195 * If you care about keys please have a look.
4196 */
4197#ifdef CONFIG_KEYS
4198
4199/**
4200 * smack_key_alloc - Set the key security blob
4201 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004202 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004203 * @flags: unused
4204 *
4205 * No allocation required
4206 *
4207 * Returns 0
4208 */
David Howellsd84f4f92008-11-14 10:39:23 +11004209static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004210 unsigned long flags)
4211{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004212 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004213
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004214 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004215 return 0;
4216}
4217
4218/**
4219 * smack_key_free - Clear the key security blob
4220 * @key: the object
4221 *
4222 * Clear the blob pointer
4223 */
4224static void smack_key_free(struct key *key)
4225{
4226 key->security = NULL;
4227}
4228
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004229/**
Casey Schauflere114e472008-02-04 22:29:50 -08004230 * smack_key_permission - Smack access on a key
4231 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004232 * @cred: the credentials to use
David Howells8c0637e2020-05-12 15:16:29 +01004233 * @need_perm: requested key permission
Casey Schauflere114e472008-02-04 22:29:50 -08004234 *
4235 * Return 0 if the task has read and write to the object,
4236 * an error code otherwise
4237 */
4238static int smack_key_permission(key_ref_t key_ref,
David Howells8c0637e2020-05-12 15:16:29 +01004239 const struct cred *cred,
4240 enum key_need_perm need_perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004241{
4242 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004243 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004244 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004245 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004246 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004247
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004248 /*
4249 * Validate requested permissions
4250 */
David Howells8c0637e2020-05-12 15:16:29 +01004251 switch (need_perm) {
4252 case KEY_NEED_READ:
4253 case KEY_NEED_SEARCH:
4254 case KEY_NEED_VIEW:
4255 request |= MAY_READ;
4256 break;
4257 case KEY_NEED_WRITE:
4258 case KEY_NEED_LINK:
4259 case KEY_NEED_SETATTR:
4260 request |= MAY_WRITE;
4261 break;
4262 case KEY_NEED_UNSPECIFIED:
4263 case KEY_NEED_UNLINK:
4264 case KEY_SYSADMIN_OVERRIDE:
4265 case KEY_AUTHTOKEN_OVERRIDE:
4266 case KEY_DEFER_PERM_CHECK:
4267 return 0;
4268 default:
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004269 return -EINVAL;
David Howells8c0637e2020-05-12 15:16:29 +01004270 }
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004271
Casey Schauflere114e472008-02-04 22:29:50 -08004272 keyp = key_ref_to_ptr(key_ref);
4273 if (keyp == NULL)
4274 return -EINVAL;
4275 /*
4276 * If the key hasn't been initialized give it access so that
4277 * it may do so.
4278 */
4279 if (keyp->security == NULL)
4280 return 0;
4281 /*
4282 * This should not occur
4283 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004284 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004285 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004286
4287 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4288 return 0;
4289
Etienne Bassetecfcc532009-04-08 20:40:06 +02004290#ifdef CONFIG_AUDIT
4291 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4292 ad.a.u.key_struct.key = keyp->serial;
4293 ad.a.u.key_struct.key_desc = keyp->description;
4294#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07004295 rc = smk_access(tkp, keyp->security, request, &ad);
4296 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4297 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004298}
José Bollo7fc5f362015-02-17 15:41:22 +01004299
4300/*
4301 * smack_key_getsecurity - Smack label tagging the key
4302 * @key points to the key to be queried
4303 * @_buffer points to a pointer that should be set to point to the
4304 * resulting string (if no label or an error occurs).
4305 * Return the length of the string (including terminating NUL) or -ve if
4306 * an error.
4307 * May also return 0 (and a NULL buffer pointer) if there is no label.
4308 */
4309static int smack_key_getsecurity(struct key *key, char **_buffer)
4310{
4311 struct smack_known *skp = key->security;
4312 size_t length;
4313 char *copy;
4314
4315 if (key->security == NULL) {
4316 *_buffer = NULL;
4317 return 0;
4318 }
4319
4320 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4321 if (copy == NULL)
4322 return -ENOMEM;
4323 length = strlen(copy) + 1;
4324
4325 *_buffer = copy;
4326 return length;
4327}
4328
Casey Schauflere114e472008-02-04 22:29:50 -08004329#endif /* CONFIG_KEYS */
4330
4331/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004332 * Smack Audit hooks
4333 *
4334 * Audit requires a unique representation of each Smack specific
4335 * rule. This unique representation is used to distinguish the
4336 * object to be audited from remaining kernel objects and also
4337 * works as a glue between the audit hooks.
4338 *
4339 * Since repository entries are added but never deleted, we'll use
4340 * the smack_known label address related to the given audit rule as
4341 * the needed unique representation. This also better fits the smack
4342 * model where nearly everything is a label.
4343 */
4344#ifdef CONFIG_AUDIT
4345
4346/**
4347 * smack_audit_rule_init - Initialize a smack audit rule
4348 * @field: audit rule fields given from user-space (audit.h)
4349 * @op: required testing operator (=, !=, >, <, ...)
4350 * @rulestr: smack label to be audited
4351 * @vrule: pointer to save our own audit rule representation
4352 *
4353 * Prepare to audit cases where (@field @op @rulestr) is true.
4354 * The label to be audited is created if necessay.
4355 */
4356static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4357{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004358 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004359 char **rule = (char **)vrule;
4360 *rule = NULL;
4361
4362 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4363 return -EINVAL;
4364
Al Viro5af75d82008-12-16 05:59:26 -05004365 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004366 return -EINVAL;
4367
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004368 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004369 if (IS_ERR(skp))
4370 return PTR_ERR(skp);
4371
4372 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004373
4374 return 0;
4375}
4376
4377/**
4378 * smack_audit_rule_known - Distinguish Smack audit rules
4379 * @krule: rule of interest, in Audit kernel representation format
4380 *
4381 * This is used to filter Smack rules from remaining Audit ones.
4382 * If it's proved that this rule belongs to us, the
4383 * audit_rule_match hook will be called to do the final judgement.
4384 */
4385static int smack_audit_rule_known(struct audit_krule *krule)
4386{
4387 struct audit_field *f;
4388 int i;
4389
4390 for (i = 0; i < krule->field_count; i++) {
4391 f = &krule->fields[i];
4392
4393 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4394 return 1;
4395 }
4396
4397 return 0;
4398}
4399
4400/**
4401 * smack_audit_rule_match - Audit given object ?
4402 * @secid: security id for identifying the object to test
4403 * @field: audit rule flags given from user-space
4404 * @op: required testing operator
4405 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004406 *
4407 * The core Audit hook. It's used to take the decision of
4408 * whether to audit or not to audit a given object.
4409 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004410static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004411{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004412 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004413 char *rule = vrule;
4414
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004415 if (unlikely(!rule)) {
4416 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004417 return -ENOENT;
4418 }
4419
4420 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4421 return 0;
4422
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004423 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004424
4425 /*
4426 * No need to do string comparisons. If a match occurs,
4427 * both pointers will point to the same smack_known
4428 * label.
4429 */
Al Viro5af75d82008-12-16 05:59:26 -05004430 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004431 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004432 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004433 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004434
4435 return 0;
4436}
4437
Casey Schaufler491a0b02016-01-26 15:08:35 -08004438/*
4439 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004440 * No memory was allocated.
4441 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004442
4443#endif /* CONFIG_AUDIT */
4444
Randy Dunlap251a2a92009-02-18 11:42:33 -08004445/**
David Quigley746df9b2013-05-22 12:50:35 -04004446 * smack_ismaclabel - check if xattr @name references a smack MAC label
4447 * @name: Full xattr name to check.
4448 */
4449static int smack_ismaclabel(const char *name)
4450{
4451 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4452}
4453
4454
4455/**
Casey Schauflere114e472008-02-04 22:29:50 -08004456 * smack_secid_to_secctx - return the smack label for a secid
4457 * @secid: incoming integer
4458 * @secdata: destination
4459 * @seclen: how long it is
4460 *
4461 * Exists for networking code.
4462 */
4463static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4464{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004465 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004466
Eric Parisd5630b92010-10-13 16:24:48 -04004467 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004468 *secdata = skp->smk_known;
4469 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004470 return 0;
4471}
4472
Randy Dunlap251a2a92009-02-18 11:42:33 -08004473/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004474 * smack_secctx_to_secid - return the secid for a smack label
4475 * @secdata: smack label
4476 * @seclen: how long result is
4477 * @secid: outgoing integer
4478 *
4479 * Exists for audit and networking code.
4480 */
David Howellse52c17642008-04-29 20:52:51 +01004481static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004482{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004483 struct smack_known *skp = smk_find_entry(secdata);
4484
4485 if (skp)
4486 *secid = skp->smk_secid;
4487 else
4488 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004489 return 0;
4490}
4491
Casey Schaufler491a0b02016-01-26 15:08:35 -08004492/*
4493 * There used to be a smack_release_secctx hook
4494 * that did nothing back when hooks were in a vector.
4495 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004496 */
Casey Schauflere114e472008-02-04 22:29:50 -08004497
David P. Quigley1ee65e32009-09-03 14:25:57 -04004498static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4499{
4500 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4501}
4502
4503static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4504{
4505 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4506}
4507
4508static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4509{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004510 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004511
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004512 *ctx = skp->smk_known;
4513 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004514 return 0;
4515}
4516
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004517static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4518{
4519
4520 struct task_smack *tsp;
4521 struct smack_known *skp;
4522 struct inode_smack *isp;
4523 struct cred *new_creds = *new;
4524
4525 if (new_creds == NULL) {
4526 new_creds = prepare_creds();
4527 if (new_creds == NULL)
4528 return -ENOMEM;
4529 }
4530
Casey Schauflerb17103a2018-11-09 16:12:56 -08004531 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004532
4533 /*
4534 * Get label from overlay inode and set it in create_sid
4535 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004536 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004537 skp = isp->smk_inode;
4538 tsp->smk_task = skp;
4539 *new = new_creds;
4540 return 0;
4541}
4542
4543static int smack_inode_copy_up_xattr(const char *name)
4544{
4545 /*
4546 * Return 1 if this is the smack access Smack attribute.
4547 */
4548 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4549 return 1;
4550
4551 return -EOPNOTSUPP;
4552}
4553
4554static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4555 struct qstr *name,
4556 const struct cred *old,
4557 struct cred *new)
4558{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004559 struct task_smack *otsp = smack_cred(old);
4560 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004561 struct inode_smack *isp;
4562 int may;
4563
4564 /*
4565 * Use the process credential unless all of
4566 * the transmuting criteria are met
4567 */
4568 ntsp->smk_task = otsp->smk_task;
4569
4570 /*
4571 * the attribute of the containing directory
4572 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004573 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004574
4575 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4576 rcu_read_lock();
4577 may = smk_access_entry(otsp->smk_task->smk_known,
4578 isp->smk_inode->smk_known,
4579 &otsp->smk_task->smk_rules);
4580 rcu_read_unlock();
4581
4582 /*
4583 * If the directory is transmuting and the rule
4584 * providing access is transmuting use the containing
4585 * directory label instead of the process label.
4586 */
4587 if (may > 0 && (may & MAY_TRANSMUTE))
4588 ntsp->smk_task = isp->smk_inode;
4589 }
4590 return 0;
4591}
4592
Casey Schauflerbbd36622018-11-12 09:30:56 -08004593struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4594 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004595 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004596 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004597 .lbs_ipc = sizeof(struct smack_known *),
4598 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004599};
4600
James Morrisca97d932017-02-15 00:18:51 +11004601static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004602 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4603 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4604 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004605
Al Viro0b520752018-12-23 16:02:47 -05004606 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004607 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4608
Casey Schauflere20b0432015-05-02 15:11:36 -07004609 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4610 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004611 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004612 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004613 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304614 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004615
Casey Schauflere20b0432015-05-02 15:11:36 -07004616 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004617
Casey Schauflere20b0432015-05-02 15:11:36 -07004618 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004619 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4620 LSM_HOOK_INIT(inode_link, smack_inode_link),
4621 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4622 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4623 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4624 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4625 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4626 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4627 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4628 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4629 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4630 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4631 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4632 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4633 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4634 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004635
Casey Schauflere20b0432015-05-02 15:11:36 -07004636 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004637 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4638 LSM_HOOK_INIT(file_lock, smack_file_lock),
4639 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4640 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4641 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4642 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4643 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4644 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004645
Casey Schauflere20b0432015-05-02 15:11:36 -07004646 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004647
Casey Schauflere20b0432015-05-02 15:11:36 -07004648 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4649 LSM_HOOK_INIT(cred_free, smack_cred_free),
4650 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4651 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004652 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004653 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4654 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4655 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4656 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4657 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4658 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4659 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4660 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4661 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4662 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4663 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4664 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4665 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004666 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004667
Casey Schauflere20b0432015-05-02 15:11:36 -07004668 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4669 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004670
Casey Schauflere20b0432015-05-02 15:11:36 -07004671 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004672
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004673 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004674 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4675 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4676 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4677 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004678
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004679 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004680 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4681 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4682 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004683
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004684 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004685 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4686 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4687 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004688
Casey Schauflere20b0432015-05-02 15:11:36 -07004689 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004690
Casey Schauflere20b0432015-05-02 15:11:36 -07004691 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4692 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004693
Casey Schauflere20b0432015-05-02 15:11:36 -07004694 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4695 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004696
Casey Schauflere20b0432015-05-02 15:11:36 -07004697 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004698 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004699#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004700 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004701#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004702 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4703 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4704 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4705 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4706 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4707 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4708 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4709 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4710 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4711 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004712
Casey Schauflere114e472008-02-04 22:29:50 -08004713 /* key management security hooks */
4714#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004715 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4716 LSM_HOOK_INIT(key_free, smack_key_free),
4717 LSM_HOOK_INIT(key_permission, smack_key_permission),
4718 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004719#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004720
4721 /* Audit hooks */
4722#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004723 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4724 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4725 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004726#endif /* CONFIG_AUDIT */
4727
Casey Schauflere20b0432015-05-02 15:11:36 -07004728 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4729 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4730 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004731 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4732 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4733 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004734 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4735 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4736 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004737};
4738
Etienne Basset7198e2e2009-03-24 20:53:24 +01004739
Casey Schaufler86812bb2012-04-17 18:55:46 -07004740static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004741{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004742 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004743 * Initialize rule list locks
4744 */
4745 mutex_init(&smack_known_huh.smk_rules_lock);
4746 mutex_init(&smack_known_hat.smk_rules_lock);
4747 mutex_init(&smack_known_floor.smk_rules_lock);
4748 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004749 mutex_init(&smack_known_web.smk_rules_lock);
4750 /*
4751 * Initialize rule lists
4752 */
4753 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4754 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4755 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4756 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004757 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4758 /*
4759 * Create the known labels list
4760 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004761 smk_insert_entry(&smack_known_huh);
4762 smk_insert_entry(&smack_known_hat);
4763 smk_insert_entry(&smack_known_star);
4764 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004765 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004766}
4767
Casey Schauflere114e472008-02-04 22:29:50 -08004768/**
4769 * smack_init - initialize the smack system
4770 *
luanshia1a07f22019-07-05 10:35:20 +08004771 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004772 */
4773static __init int smack_init(void)
4774{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004775 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004776 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004777
Rohit1a5b4722014-10-15 17:40:41 +05304778 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4779 if (!smack_inode_cache)
4780 return -ENOMEM;
4781
Casey Schaufler4e328b02019-04-02 11:37:12 -07004782 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4783 if (!smack_rule_cache) {
4784 kmem_cache_destroy(smack_inode_cache);
4785 return -ENOMEM;
4786 }
4787
Casey Schauflerbbd36622018-11-12 09:30:56 -08004788 /*
4789 * Set the security state for the initial task.
4790 */
4791 tsp = smack_cred(cred);
4792 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4793
4794 /*
4795 * Register with LSM
4796 */
4797 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004798 smack_enabled = 1;
4799
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004800 pr_info("Smack: Initializing.\n");
4801#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4802 pr_info("Smack: Netfilter enabled.\n");
4803#endif
4804#ifdef SMACK_IPV6_PORT_LABELING
4805 pr_info("Smack: IPv6 port labeling enabled.\n");
4806#endif
4807#ifdef SMACK_IPV6_SECMARK_LABELING
4808 pr_info("Smack: IPv6 Netfilter enabled.\n");
4809#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004810
Casey Schaufler86812bb2012-04-17 18:55:46 -07004811 /* initialize the smack_known_list */
4812 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004813
Casey Schauflere114e472008-02-04 22:29:50 -08004814 return 0;
4815}
4816
4817/*
4818 * Smack requires early initialization in order to label
4819 * all processes and objects when they are created.
4820 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004821DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004822 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004823 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004824 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004825 .init = smack_init,
4826};