blob: ed17049d39d5d77e5ce3d9384ebbd803f8637fa0 [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 Schaufler21abb1e2015-07-22 14:25:31 -07002829#if IS_ENABLED(CONFIG_IPV6)
2830 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2831#endif
2832#ifdef SMACK_IPV6_SECMARK_LABELING
2833 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002834 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002835#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002836
Casey Schauflerc6739442013-05-22 18:42:56 -07002837 if (sock->sk == NULL)
2838 return 0;
2839
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002840#ifdef SMACK_IPV6_SECMARK_LABELING
2841 ssp = sock->sk->sk_security;
2842#endif
2843
Casey Schauflerc6739442013-05-22 18:42:56 -07002844 switch (sock->sk->sk_family) {
2845 case PF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002846 if (addrlen < sizeof(struct sockaddr_in) ||
2847 sap->sa_family != AF_INET)
Casey Schauflerc6739442013-05-22 18:42:56 -07002848 return -EINVAL;
2849 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2850 break;
2851 case PF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002852 if (addrlen < SIN6_LEN_RFC2133 || sap->sa_family != AF_INET6)
Casey Schauflerc6739442013-05-22 18:42:56 -07002853 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002854#ifdef SMACK_IPV6_SECMARK_LABELING
2855 rsp = smack_ipv6host_label(sip);
2856 if (rsp != NULL)
2857 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002858 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002859#endif
2860#ifdef SMACK_IPV6_PORT_LABELING
2861 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2862#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002863 break;
2864 }
2865 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002866}
2867
2868/**
2869 * smack_flags_to_may - convert S_ to MAY_ values
2870 * @flags: the S_ value
2871 *
2872 * Returns the equivalent MAY_ value
2873 */
2874static int smack_flags_to_may(int flags)
2875{
2876 int may = 0;
2877
2878 if (flags & S_IRUGO)
2879 may |= MAY_READ;
2880 if (flags & S_IWUGO)
2881 may |= MAY_WRITE;
2882 if (flags & S_IXUGO)
2883 may |= MAY_EXEC;
2884
2885 return may;
2886}
2887
2888/**
2889 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2890 * @msg: the object
2891 *
2892 * Returns 0
2893 */
2894static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2895{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002896 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002897
Casey Schauflerecd5f822018-11-20 11:55:02 -08002898 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002899 return 0;
2900}
2901
2902/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002903 * smack_of_ipc - the smack pointer for the ipc
2904 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002905 *
2906 * Returns a pointer to the smack value
2907 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002908static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002909{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002910 struct smack_known **blob = smack_ipc(isp);
2911
2912 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002913}
2914
2915/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002916 * smack_ipc_alloc_security - Set the security blob for ipc
2917 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002918 *
2919 * Returns 0
2920 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002921static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002922{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002923 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002924
Casey Schaufler019bcca2018-09-21 17:19:54 -07002925 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002926 return 0;
2927}
2928
2929/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002930 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002931 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002932 * @access : access requested
2933 *
2934 * Returns 0 if current has the requested access, error code otherwise
2935 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002936static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002937{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002938 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002939 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002940 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002941
2942#ifdef CONFIG_AUDIT
2943 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002944 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002945#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002946 rc = smk_curacc(ssp, access, &ad);
2947 rc = smk_bu_current("shm", ssp, access, rc);
2948 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002949}
2950
2951/**
Casey Schauflere114e472008-02-04 22:29:50 -08002952 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002953 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002954 * @shmflg: access requested
2955 *
2956 * Returns 0 if current has the requested access, error code otherwise
2957 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002958static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002959{
Casey Schauflere114e472008-02-04 22:29:50 -08002960 int may;
2961
2962 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002963 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002964}
2965
2966/**
2967 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002968 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002969 * @cmd: what it wants to do
2970 *
2971 * Returns 0 if current has the requested access, error code otherwise
2972 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002973static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002974{
Casey Schauflere114e472008-02-04 22:29:50 -08002975 int may;
2976
2977 switch (cmd) {
2978 case IPC_STAT:
2979 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002980 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002981 may = MAY_READ;
2982 break;
2983 case IPC_SET:
2984 case SHM_LOCK:
2985 case SHM_UNLOCK:
2986 case IPC_RMID:
2987 may = MAY_READWRITE;
2988 break;
2989 case IPC_INFO:
2990 case SHM_INFO:
2991 /*
2992 * System level information.
2993 */
2994 return 0;
2995 default:
2996 return -EINVAL;
2997 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002998 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002999}
3000
3001/**
3002 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003003 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003004 * @shmaddr: unused
3005 * @shmflg: access requested
3006 *
3007 * Returns 0 if current has the requested access, error code otherwise
3008 */
luanshia1a07f22019-07-05 10:35:20 +08003009static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003010 int shmflg)
3011{
Casey Schauflere114e472008-02-04 22:29:50 -08003012 int may;
3013
3014 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003015 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003016}
3017
3018/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003019 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003020 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003021 * @access : access requested
3022 *
3023 * Returns 0 if current has the requested access, error code otherwise
3024 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003025static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003026{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003027 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003028 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003029 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003030
3031#ifdef CONFIG_AUDIT
3032 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003033 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003034#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003035 rc = smk_curacc(ssp, access, &ad);
3036 rc = smk_bu_current("sem", ssp, access, rc);
3037 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003038}
3039
3040/**
Casey Schauflere114e472008-02-04 22:29:50 -08003041 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003042 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003043 * @semflg: access requested
3044 *
3045 * Returns 0 if current has the requested access, error code otherwise
3046 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003047static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003048{
Casey Schauflere114e472008-02-04 22:29:50 -08003049 int may;
3050
3051 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003052 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003053}
3054
3055/**
3056 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003057 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003058 * @cmd: what it wants to do
3059 *
3060 * Returns 0 if current has the requested access, error code otherwise
3061 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003062static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003063{
Casey Schauflere114e472008-02-04 22:29:50 -08003064 int may;
3065
3066 switch (cmd) {
3067 case GETPID:
3068 case GETNCNT:
3069 case GETZCNT:
3070 case GETVAL:
3071 case GETALL:
3072 case IPC_STAT:
3073 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003074 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003075 may = MAY_READ;
3076 break;
3077 case SETVAL:
3078 case SETALL:
3079 case IPC_RMID:
3080 case IPC_SET:
3081 may = MAY_READWRITE;
3082 break;
3083 case IPC_INFO:
3084 case SEM_INFO:
3085 /*
3086 * System level information
3087 */
3088 return 0;
3089 default:
3090 return -EINVAL;
3091 }
3092
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003093 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003094}
3095
3096/**
3097 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003098 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003099 * @sops: unused
3100 * @nsops: unused
3101 * @alter: unused
3102 *
3103 * Treated as read and write in all cases.
3104 *
3105 * Returns 0 if access is allowed, error code otherwise
3106 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003107static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003108 unsigned nsops, int alter)
3109{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003110 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003111}
3112
3113/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003114 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003115 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003116 * @access : access requested
3117 *
3118 * return 0 if current has access, error otherwise
3119 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003120static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003121{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003122 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003123 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003124 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003125
3126#ifdef CONFIG_AUDIT
3127 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003128 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003129#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003130 rc = smk_curacc(msp, access, &ad);
3131 rc = smk_bu_current("msq", msp, access, rc);
3132 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003133}
3134
3135/**
Casey Schauflere114e472008-02-04 22:29:50 -08003136 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003137 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003138 * @msqflg: access requested
3139 *
3140 * Returns 0 if current has the requested access, error code otherwise
3141 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003142static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003143{
Casey Schauflere114e472008-02-04 22:29:50 -08003144 int may;
3145
3146 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003147 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003148}
3149
3150/**
3151 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003152 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003153 * @cmd: what it wants to do
3154 *
3155 * Returns 0 if current has the requested access, error code otherwise
3156 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003157static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003158{
Casey Schauflere114e472008-02-04 22:29:50 -08003159 int may;
3160
3161 switch (cmd) {
3162 case IPC_STAT:
3163 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003164 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003165 may = MAY_READ;
3166 break;
3167 case IPC_SET:
3168 case IPC_RMID:
3169 may = MAY_READWRITE;
3170 break;
3171 case IPC_INFO:
3172 case MSG_INFO:
3173 /*
3174 * System level information
3175 */
3176 return 0;
3177 default:
3178 return -EINVAL;
3179 }
3180
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003181 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003182}
3183
3184/**
3185 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003186 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003187 * @msg: unused
3188 * @msqflg: access requested
3189 *
3190 * Returns 0 if current has the requested access, error code otherwise
3191 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003192static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003193 int msqflg)
3194{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003195 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003196
Etienne Bassetecfcc532009-04-08 20:40:06 +02003197 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003198 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003199}
3200
3201/**
3202 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003203 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003204 * @msg: unused
3205 * @target: unused
3206 * @type: unused
3207 * @mode: unused
3208 *
3209 * Returns 0 if current has read and write access, error code otherwise
3210 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003211static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003212 struct task_struct *target, long type, int mode)
3213{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003214 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003215}
3216
3217/**
3218 * smack_ipc_permission - Smack access for ipc_permission()
3219 * @ipp: the object permissions
3220 * @flag: access requested
3221 *
3222 * Returns 0 if current has read and write access, error code otherwise
3223 */
3224static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3225{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003226 struct smack_known **blob = smack_ipc(ipp);
3227 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003228 int may = smack_flags_to_may(flag);
3229 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003230 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003231
Etienne Bassetecfcc532009-04-08 20:40:06 +02003232#ifdef CONFIG_AUDIT
3233 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3234 ad.a.u.ipc_id = ipp->id;
3235#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003236 rc = smk_curacc(iskp, may, &ad);
3237 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003238 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003239}
3240
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003241/**
3242 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003243 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003244 * @secid: where result will be saved
3245 */
3246static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3247{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003248 struct smack_known **blob = smack_ipc(ipp);
3249 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003250
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003251 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003252}
3253
Casey Schauflere114e472008-02-04 22:29:50 -08003254/**
3255 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003256 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003257 * @inode: the object
3258 *
3259 * Set the inode's security blob if it hasn't been done already.
3260 */
3261static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3262{
3263 struct super_block *sbp;
3264 struct superblock_smack *sbsp;
3265 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003266 struct smack_known *skp;
3267 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003268 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003269 char trattr[TRANS_TRUE_SIZE];
3270 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003271 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003272 struct dentry *dp;
3273
3274 if (inode == NULL)
3275 return;
3276
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003277 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003278
3279 mutex_lock(&isp->smk_lock);
3280 /*
3281 * If the inode is already instantiated
3282 * take the quick way out
3283 */
3284 if (isp->smk_flags & SMK_INODE_INSTANT)
3285 goto unlockandout;
3286
3287 sbp = inode->i_sb;
3288 sbsp = sbp->s_security;
3289 /*
3290 * We're going to use the superblock default label
3291 * if there's no label on the file.
3292 */
3293 final = sbsp->smk_default;
3294
3295 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003296 * If this is the root inode the superblock
3297 * may be in the process of initialization.
3298 * If that is the case use the root value out
3299 * of the superblock.
3300 */
3301 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003302 switch (sbp->s_magic) {
3303 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003304 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003305 /*
3306 * The cgroup filesystem is never mounted,
3307 * so there's no opportunity to set the mount
3308 * options.
3309 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003310 sbsp->smk_root = &smack_known_star;
3311 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003312 isp->smk_inode = sbsp->smk_root;
3313 break;
3314 case TMPFS_MAGIC:
3315 /*
3316 * What about shmem/tmpfs anonymous files with dentry
3317 * obtained from d_alloc_pseudo()?
3318 */
3319 isp->smk_inode = smk_of_current();
3320 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003321 case PIPEFS_MAGIC:
3322 isp->smk_inode = smk_of_current();
3323 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003324 case SOCKFS_MAGIC:
3325 /*
3326 * Socket access is controlled by the socket
3327 * structures associated with the task involved.
3328 */
3329 isp->smk_inode = &smack_known_star;
3330 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003331 default:
3332 isp->smk_inode = sbsp->smk_root;
3333 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003334 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003335 isp->smk_flags |= SMK_INODE_INSTANT;
3336 goto unlockandout;
3337 }
3338
3339 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003340 * This is pretty hackish.
3341 * Casey says that we shouldn't have to do
3342 * file system specific code, but it does help
3343 * with keeping it simple.
3344 */
3345 switch (sbp->s_magic) {
3346 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003347 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003348 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003349 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003350 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003351 * that the smack file system doesn't do
3352 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003353 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003354 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003355 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003356 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003357 break;
3358 case DEVPTS_SUPER_MAGIC:
3359 /*
3360 * devpts seems content with the label of the task.
3361 * Programs that change smack have to treat the
3362 * pty with respect.
3363 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003364 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003365 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003366 case PROC_SUPER_MAGIC:
3367 /*
3368 * Casey says procfs appears not to care.
3369 * The superblock default suffices.
3370 */
3371 break;
3372 case TMPFS_MAGIC:
3373 /*
3374 * Device labels should come from the filesystem,
3375 * but watch out, because they're volitile,
3376 * getting recreated on every reboot.
3377 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003378 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003379 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003380 * If a smack value has been set we want to use it,
3381 * but since tmpfs isn't giving us the opportunity
3382 * to set mount options simulate setting the
3383 * superblock default.
3384 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003385 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003386 default:
3387 /*
3388 * This isn't an understood special case.
3389 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003390 */
3391
3392 /*
3393 * UNIX domain sockets use lower level socket data.
3394 */
3395 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003396 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003397 break;
3398 }
3399 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003400 * No xattr support means, alas, no SMACK label.
3401 * Use the aforeapplied default.
3402 * It would be curious if the label of the task
3403 * does not match that assigned.
3404 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003405 if (!(inode->i_opflags & IOP_XATTR))
3406 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003407 /*
3408 * Get the dentry for xattr.
3409 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003410 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003411 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003412 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003413 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003414
3415 /*
3416 * Transmuting directory
3417 */
3418 if (S_ISDIR(inode->i_mode)) {
3419 /*
3420 * If this is a new directory and the label was
3421 * transmuted when the inode was initialized
3422 * set the transmute attribute on the directory
3423 * and mark the inode.
3424 *
3425 * If there is a transmute attribute on the
3426 * directory mark the inode.
3427 */
3428 if (isp->smk_flags & SMK_INODE_CHANGED) {
3429 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003430 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003431 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003432 TRANS_TRUE, TRANS_TRUE_SIZE,
3433 0);
3434 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003435 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003436 XATTR_NAME_SMACKTRANSMUTE, trattr,
3437 TRANS_TRUE_SIZE);
3438 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3439 TRANS_TRUE_SIZE) != 0)
3440 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003441 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003442 if (rc >= 0)
3443 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003444 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003445 /*
3446 * Don't let the exec or mmap label be "*" or "@".
3447 */
3448 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3449 if (IS_ERR(skp) || skp == &smack_known_star ||
3450 skp == &smack_known_web)
3451 skp = NULL;
3452 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003453
Casey Schaufler19760ad2013-12-16 16:27:26 -08003454 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003455 if (IS_ERR(skp) || skp == &smack_known_star ||
3456 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003457 skp = NULL;
3458 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003459
Casey Schauflere114e472008-02-04 22:29:50 -08003460 dput(dp);
3461 break;
3462 }
3463
3464 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003465 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003466 else
3467 isp->smk_inode = final;
3468
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003469 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003470
3471unlockandout:
3472 mutex_unlock(&isp->smk_lock);
3473 return;
3474}
3475
3476/**
3477 * smack_getprocattr - Smack process attribute access
3478 * @p: the object task
3479 * @name: the name of the attribute in /proc/.../attr
3480 * @value: where to put the result
3481 *
3482 * Places a copy of the task Smack into value
3483 *
3484 * Returns the length of the smack label or an error code
3485 */
3486static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3487{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003488 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003489 char *cp;
3490 int slen;
3491
3492 if (strcmp(name, "current") != 0)
3493 return -EINVAL;
3494
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003495 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003496 if (cp == NULL)
3497 return -ENOMEM;
3498
3499 slen = strlen(cp);
3500 *value = cp;
3501 return slen;
3502}
3503
3504/**
3505 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003506 * @name: the name of the attribute in /proc/.../attr
3507 * @value: the value to set
3508 * @size: the size of the value
3509 *
3510 * Sets the Smack value of the task. Only setting self
3511 * is permitted and only with privilege
3512 *
3513 * Returns the length of the smack label or an error code
3514 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003515static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003516{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003517 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003518 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003519 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003520 struct smack_known_list_elem *sklep;
3521 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003522
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003523 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003524 return -EPERM;
3525
Casey Schauflerf7112e62012-05-06 15:22:02 -07003526 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003527 return -EINVAL;
3528
3529 if (strcmp(name, "current") != 0)
3530 return -EINVAL;
3531
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003532 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003533 if (IS_ERR(skp))
3534 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003535
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003536 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303537 * No process is ever allowed the web ("@") label
3538 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003539 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303540 if (skp == &smack_known_web || skp == &smack_known_star)
3541 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003542
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003543 if (!smack_privileged(CAP_MAC_ADMIN)) {
3544 rc = -EPERM;
3545 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3546 if (sklep->smk_label == skp) {
3547 rc = 0;
3548 break;
3549 }
3550 if (rc)
3551 return rc;
3552 }
3553
David Howellsd84f4f92008-11-14 10:39:23 +11003554 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003555 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003556 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003557
Casey Schauflerb17103a2018-11-09 16:12:56 -08003558 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003559 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003560 /*
3561 * process can change its label only once
3562 */
3563 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003564
David Howellsd84f4f92008-11-14 10:39:23 +11003565 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003566 return size;
3567}
3568
3569/**
3570 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003571 * @sock: one sock
3572 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003573 * @newsk: unused
3574 *
3575 * Return 0 if a subject with the smack of sock could access
3576 * an object with the smack of other, otherwise an error code
3577 */
David S. Miller3610cda2011-01-05 15:38:53 -08003578static int smack_unix_stream_connect(struct sock *sock,
3579 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003580{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003581 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003582 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003583 struct socket_smack *ssp = sock->sk_security;
3584 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003585 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003586 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003587 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003588#ifdef CONFIG_AUDIT
3589 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003590#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003591
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003592 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3593 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003594 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003595#ifdef CONFIG_AUDIT
3596 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3597 smk_ad_setfield_u_net_sk(&ad, other);
3598#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003599 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3600 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003601 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003602 okp = osp->smk_out;
3603 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003604 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003605 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003606 MAY_WRITE, rc);
3607 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003608 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003609
Casey Schaufler975d5e52011-09-26 14:43:39 -07003610 /*
3611 * Cross reference the peer labels for SO_PEERSEC.
3612 */
3613 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003614 nsp->smk_packet = ssp->smk_out;
3615 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003616 }
3617
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003618 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003619}
3620
3621/**
3622 * smack_unix_may_send - Smack access on UDS
3623 * @sock: one socket
3624 * @other: the other socket
3625 *
3626 * Return 0 if a subject with the smack of sock could access
3627 * an object with the smack of other, otherwise an error code
3628 */
3629static int smack_unix_may_send(struct socket *sock, struct socket *other)
3630{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003631 struct socket_smack *ssp = sock->sk->sk_security;
3632 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003633 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003634 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003635
Kees Cook923e9a12012-04-10 13:26:44 -07003636#ifdef CONFIG_AUDIT
3637 struct lsm_network_audit net;
3638
Eric Paris48c62af2012-04-02 13:15:44 -04003639 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003640 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003641#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003642
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003643 if (smack_privileged(CAP_MAC_OVERRIDE))
3644 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003645
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003646 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3647 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003648 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003649}
3650
3651/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003652 * smack_socket_sendmsg - Smack check based on destination host
3653 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003654 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003655 * @size: the size of the message
3656 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003657 * Return 0 if the current subject can write to the destination host.
3658 * For IPv4 this is only a question if the destination is a single label host.
3659 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003660 */
3661static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3662 int size)
3663{
3664 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003665#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003666 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003667#endif
3668#ifdef SMACK_IPV6_SECMARK_LABELING
3669 struct socket_smack *ssp = sock->sk->sk_security;
3670 struct smack_known *rsp;
3671#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003672 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003673
3674 /*
3675 * Perfectly reasonable for this to be NULL
3676 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003677 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003678 return 0;
3679
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003680 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003681 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003682 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3683 sip->sin_family != AF_INET)
3684 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003685 rc = smack_netlabel_send(sock->sk, sip);
3686 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003687#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003688 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003689 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3690 sap->sin6_family != AF_INET6)
3691 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003692#ifdef SMACK_IPV6_SECMARK_LABELING
3693 rsp = smack_ipv6host_label(sap);
3694 if (rsp != NULL)
3695 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3696 SMK_CONNECTING);
3697#endif
3698#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003699 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003700#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003701#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003702 break;
3703 }
3704 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003705}
3706
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003707/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003708 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003709 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003710 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003711 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003712 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003713 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003714static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3715 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003716{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003717 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003718 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003719 int acat;
3720 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003721
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003722 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003723 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003724 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003725 * If there are flags but no level netlabel isn't
3726 * behaving the way we expect it to.
3727 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003728 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003729 * Without guidance regarding the smack value
3730 * for the packet fall back on the network
3731 * ambient value.
3732 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003733 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303734 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003735 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003736 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003737 /*
3738 * Compare the catsets. Use the netlbl APIs.
3739 */
3740 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3741 if ((skp->smk_netlabel.flags &
3742 NETLBL_SECATTR_MLS_CAT) == 0)
3743 found = 1;
3744 break;
3745 }
3746 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003747 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3748 acat + 1);
3749 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003750 skp->smk_netlabel.attr.mls.cat,
3751 kcat + 1);
3752 if (acat < 0 || kcat < 0)
3753 break;
3754 }
3755 if (acat == kcat) {
3756 found = 1;
3757 break;
3758 }
Casey Schauflere114e472008-02-04 22:29:50 -08003759 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003760 rcu_read_unlock();
3761
3762 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003763 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003764
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003765 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003766 return &smack_known_web;
3767 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003768 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003769 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003770 /*
3771 * Looks like a fallback, which gives us a secid.
3772 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003773 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003774 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003775 * Without guidance regarding the smack value
3776 * for the packet fall back on the network
3777 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003778 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003779 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003780}
3781
Casey Schaufler69f287a2014-12-12 17:08:40 -08003782#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003783static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003784{
Casey Schauflerc6739442013-05-22 18:42:56 -07003785 u8 nexthdr;
3786 int offset;
3787 int proto = -EINVAL;
3788 struct ipv6hdr _ipv6h;
3789 struct ipv6hdr *ip6;
3790 __be16 frag_off;
3791 struct tcphdr _tcph, *th;
3792 struct udphdr _udph, *uh;
3793 struct dccp_hdr _dccph, *dh;
3794
3795 sip->sin6_port = 0;
3796
3797 offset = skb_network_offset(skb);
3798 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3799 if (ip6 == NULL)
3800 return -EINVAL;
3801 sip->sin6_addr = ip6->saddr;
3802
3803 nexthdr = ip6->nexthdr;
3804 offset += sizeof(_ipv6h);
3805 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3806 if (offset < 0)
3807 return -EINVAL;
3808
3809 proto = nexthdr;
3810 switch (proto) {
3811 case IPPROTO_TCP:
3812 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3813 if (th != NULL)
3814 sip->sin6_port = th->source;
3815 break;
3816 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003817 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003818 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3819 if (uh != NULL)
3820 sip->sin6_port = uh->source;
3821 break;
3822 case IPPROTO_DCCP:
3823 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3824 if (dh != NULL)
3825 sip->sin6_port = dh->dccph_sport;
3826 break;
3827 }
3828 return proto;
3829}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003830#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003831
Casey Schauflere114e472008-02-04 22:29:50 -08003832/**
3833 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3834 * @sk: socket
3835 * @skb: packet
3836 *
3837 * Returns 0 if the packet should be delivered, an error code otherwise
3838 */
3839static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3840{
3841 struct netlbl_lsm_secattr secattr;
3842 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003843 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003844 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003845 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003846 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003847#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003848 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003849#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003850#if IS_ENABLED(CONFIG_IPV6)
3851 struct sockaddr_in6 sadd;
3852 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003853
3854 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3855 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003856#endif /* CONFIG_IPV6 */
3857
Piotr Sawicki129a9982018-07-19 11:42:58 +02003858 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003859 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003860#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3861 /*
3862 * If there is a secmark use it rather than the CIPSO label.
3863 * If there is no secmark fall back to CIPSO.
3864 * The secmark is assumed to reflect policy better.
3865 */
3866 if (skb && skb->secmark != 0) {
3867 skp = smack_from_secid(skb->secmark);
3868 goto access_check;
3869 }
3870#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003871 /*
3872 * Translate what netlabel gave us.
3873 */
3874 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003875
Piotr Sawicki129a9982018-07-19 11:42:58 +02003876 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003877 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003878 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003879 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003880 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003881
Casey Schauflerc6739442013-05-22 18:42:56 -07003882 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003883
Casey Schaufler69f287a2014-12-12 17:08:40 -08003884#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3885access_check:
3886#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003887#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003888 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003889 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003890 ad.a.u.net->netif = skb->skb_iif;
3891 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003892#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003893 /*
3894 * Receiving a packet requires that the other end
3895 * be able to write here. Read access is not required.
3896 * This is the simplist possible security model
3897 * for networking.
3898 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003899 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3900 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003901 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003902 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003903 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003904 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003905#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003906 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003907 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003908 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3909 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003910 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003911#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003912 if (skb && skb->secmark != 0)
3913 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003914 else if (smk_ipv6_localhost(&sadd))
3915 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003916 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003917 skp = smack_ipv6host_label(&sadd);
3918 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003919 skp = smack_net_ambient;
Jia-Ju Bai3f4287e2019-07-23 18:00:15 +08003920 if (skb == NULL)
3921 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003922#ifdef CONFIG_AUDIT
3923 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003924 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003925 ad.a.u.net->netif = skb->skb_iif;
3926 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3927#endif /* CONFIG_AUDIT */
3928 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3929 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3930 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003931#endif /* SMACK_IPV6_SECMARK_LABELING */
3932#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003933 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003934#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003935 if (rc != 0)
3936 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3937 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003938 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003939#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003940 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003941
Paul Moorea8134292008-10-10 10:16:31 -04003942 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003943}
3944
3945/**
3946 * smack_socket_getpeersec_stream - pull in packet label
3947 * @sock: the socket
3948 * @optval: user's destination
3949 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003950 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003951 *
3952 * returns zero on success, an error code otherwise
3953 */
3954static int smack_socket_getpeersec_stream(struct socket *sock,
3955 char __user *optval,
3956 int __user *optlen, unsigned len)
3957{
3958 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003959 char *rcp = "";
3960 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003961 int rc = 0;
3962
3963 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003964 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003965 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003966 slen = strlen(rcp) + 1;
3967 }
Casey Schauflere114e472008-02-04 22:29:50 -08003968
3969 if (slen > len)
3970 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003971 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003972 rc = -EFAULT;
3973
3974 if (put_user(slen, optlen) != 0)
3975 rc = -EFAULT;
3976
3977 return rc;
3978}
3979
3980
3981/**
3982 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003983 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003984 * @skb: packet data
3985 * @secid: pointer to where to put the secid of the packet
3986 *
3987 * Sets the netlabel socket state on sk from parent
3988 */
3989static int smack_socket_getpeersec_dgram(struct socket *sock,
3990 struct sk_buff *skb, u32 *secid)
3991
3992{
3993 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003994 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003995 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003996 int family = PF_UNSPEC;
3997 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003998 int rc;
3999
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004000 if (skb != NULL) {
4001 if (skb->protocol == htons(ETH_P_IP))
4002 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004003#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004004 else if (skb->protocol == htons(ETH_P_IPV6))
4005 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004006#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004007 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004008 if (family == PF_UNSPEC && sock != NULL)
4009 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004010
Casey Schaufler69f287a2014-12-12 17:08:40 -08004011 switch (family) {
4012 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004013 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004014 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004015 break;
4016 case PF_INET:
4017#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4018 s = skb->secmark;
4019 if (s != 0)
4020 break;
4021#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004022 /*
4023 * Translate what netlabel gave us.
4024 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004025 if (sock != NULL && sock->sk != NULL)
4026 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004027 netlbl_secattr_init(&secattr);
4028 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4029 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004030 skp = smack_from_secattr(&secattr, ssp);
4031 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004032 }
4033 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004034 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004035 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004036#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004037 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004038#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004039 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004040 }
4041 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004042 if (s == 0)
4043 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004044 return 0;
4045}
4046
4047/**
Paul Moore07feee82009-03-27 17:10:54 -04004048 * smack_sock_graft - Initialize a newly created socket with an existing sock
4049 * @sk: child sock
4050 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004051 *
Paul Moore07feee82009-03-27 17:10:54 -04004052 * Set the smk_{in,out} state of an existing sock based on the process that
4053 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004054 */
4055static void smack_sock_graft(struct sock *sk, struct socket *parent)
4056{
4057 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004058 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004059
Paul Moore07feee82009-03-27 17:10:54 -04004060 if (sk == NULL ||
4061 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004062 return;
4063
4064 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004065 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004066 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004067 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004068}
4069
4070/**
4071 * smack_inet_conn_request - Smack access check on connect
4072 * @sk: socket involved
4073 * @skb: packet
4074 * @req: unused
4075 *
4076 * Returns 0 if a task with the packet label could write to
4077 * the socket, otherwise an error code
4078 */
4079static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4080 struct request_sock *req)
4081{
Paul Moore07feee82009-03-27 17:10:54 -04004082 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004083 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004084 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004085 struct netlbl_lsm_secattr secattr;
4086 struct sockaddr_in addr;
4087 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004088 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004089 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004090 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004091#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004092 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004093#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004094
Casey Schaufler69f287a2014-12-12 17:08:40 -08004095#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004096 if (family == PF_INET6) {
4097 /*
4098 * Handle mapped IPv4 packets arriving
4099 * via IPv6 sockets. Don't set up netlabel
4100 * processing on IPv6.
4101 */
4102 if (skb->protocol == htons(ETH_P_IP))
4103 family = PF_INET;
4104 else
4105 return 0;
4106 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004107#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004108
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004109#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4110 /*
4111 * If there is a secmark use it rather than the CIPSO label.
4112 * If there is no secmark fall back to CIPSO.
4113 * The secmark is assumed to reflect policy better.
4114 */
4115 if (skb && skb->secmark != 0) {
4116 skp = smack_from_secid(skb->secmark);
4117 goto access_check;
4118 }
4119#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4120
Paul Moore07feee82009-03-27 17:10:54 -04004121 netlbl_secattr_init(&secattr);
4122 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004123 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004124 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004125 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004126 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004127 netlbl_secattr_destroy(&secattr);
4128
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004129#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4130access_check:
4131#endif
4132
Etienne Bassetecfcc532009-04-08 20:40:06 +02004133#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004134 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4135 ad.a.u.net->family = family;
4136 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004137 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4138#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004139 /*
Paul Moore07feee82009-03-27 17:10:54 -04004140 * Receiving a packet requires that the other end be able to write
4141 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004142 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004143 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4144 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004145 if (rc != 0)
4146 return rc;
4147
4148 /*
4149 * Save the peer's label in the request_sock so we can later setup
4150 * smk_packet in the child socket so that SO_PEERCRED can report it.
4151 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004152 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004153
4154 /*
4155 * We need to decide if we want to label the incoming connection here
4156 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004157 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004158 */
4159 hdr = ip_hdr(skb);
4160 addr.sin_addr.s_addr = hdr->saddr;
4161 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004162 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004163 rcu_read_unlock();
4164
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004165 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004166 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004167 else
Paul Moore07feee82009-03-27 17:10:54 -04004168 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004169
4170 return rc;
4171}
4172
Paul Moore07feee82009-03-27 17:10:54 -04004173/**
4174 * smack_inet_csk_clone - Copy the connection information to the new socket
4175 * @sk: the new socket
4176 * @req: the connection's request_sock
4177 *
4178 * Transfer the connection's peer label to the newly created socket.
4179 */
4180static void smack_inet_csk_clone(struct sock *sk,
4181 const struct request_sock *req)
4182{
4183 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004184 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004185
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004186 if (req->peer_secid != 0) {
4187 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004188 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004189 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004190 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004191}
4192
Casey Schauflere114e472008-02-04 22:29:50 -08004193/*
4194 * Key management security hooks
4195 *
4196 * Casey has not tested key support very heavily.
4197 * The permission check is most likely too restrictive.
4198 * If you care about keys please have a look.
4199 */
4200#ifdef CONFIG_KEYS
4201
4202/**
4203 * smack_key_alloc - Set the key security blob
4204 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004205 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004206 * @flags: unused
4207 *
4208 * No allocation required
4209 *
4210 * Returns 0
4211 */
David Howellsd84f4f92008-11-14 10:39:23 +11004212static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004213 unsigned long flags)
4214{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004215 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004216
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004217 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004218 return 0;
4219}
4220
4221/**
4222 * smack_key_free - Clear the key security blob
4223 * @key: the object
4224 *
4225 * Clear the blob pointer
4226 */
4227static void smack_key_free(struct key *key)
4228{
4229 key->security = NULL;
4230}
4231
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004232/**
Casey Schauflere114e472008-02-04 22:29:50 -08004233 * smack_key_permission - Smack access on a key
4234 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004235 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004236 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004237 *
4238 * Return 0 if the task has read and write to the object,
4239 * an error code otherwise
4240 */
4241static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004242 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004243{
4244 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004245 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004246 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004247 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004248 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004249
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004250 /*
4251 * Validate requested permissions
4252 */
4253 if (perm & ~KEY_NEED_ALL)
4254 return -EINVAL;
4255
Casey Schauflere114e472008-02-04 22:29:50 -08004256 keyp = key_ref_to_ptr(key_ref);
4257 if (keyp == NULL)
4258 return -EINVAL;
4259 /*
4260 * If the key hasn't been initialized give it access so that
4261 * it may do so.
4262 */
4263 if (keyp->security == NULL)
4264 return 0;
4265 /*
4266 * This should not occur
4267 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004268 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004269 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004270
4271 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4272 return 0;
4273
Etienne Bassetecfcc532009-04-08 20:40:06 +02004274#ifdef CONFIG_AUDIT
4275 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4276 ad.a.u.key_struct.key = keyp->serial;
4277 ad.a.u.key_struct.key_desc = keyp->description;
4278#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004279 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4280 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004281 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004282 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004283 rc = smk_access(tkp, keyp->security, request, &ad);
4284 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4285 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004286}
José Bollo7fc5f362015-02-17 15:41:22 +01004287
4288/*
4289 * smack_key_getsecurity - Smack label tagging the key
4290 * @key points to the key to be queried
4291 * @_buffer points to a pointer that should be set to point to the
4292 * resulting string (if no label or an error occurs).
4293 * Return the length of the string (including terminating NUL) or -ve if
4294 * an error.
4295 * May also return 0 (and a NULL buffer pointer) if there is no label.
4296 */
4297static int smack_key_getsecurity(struct key *key, char **_buffer)
4298{
4299 struct smack_known *skp = key->security;
4300 size_t length;
4301 char *copy;
4302
4303 if (key->security == NULL) {
4304 *_buffer = NULL;
4305 return 0;
4306 }
4307
4308 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4309 if (copy == NULL)
4310 return -ENOMEM;
4311 length = strlen(copy) + 1;
4312
4313 *_buffer = copy;
4314 return length;
4315}
4316
Casey Schauflere114e472008-02-04 22:29:50 -08004317#endif /* CONFIG_KEYS */
4318
4319/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004320 * Smack Audit hooks
4321 *
4322 * Audit requires a unique representation of each Smack specific
4323 * rule. This unique representation is used to distinguish the
4324 * object to be audited from remaining kernel objects and also
4325 * works as a glue between the audit hooks.
4326 *
4327 * Since repository entries are added but never deleted, we'll use
4328 * the smack_known label address related to the given audit rule as
4329 * the needed unique representation. This also better fits the smack
4330 * model where nearly everything is a label.
4331 */
4332#ifdef CONFIG_AUDIT
4333
4334/**
4335 * smack_audit_rule_init - Initialize a smack audit rule
4336 * @field: audit rule fields given from user-space (audit.h)
4337 * @op: required testing operator (=, !=, >, <, ...)
4338 * @rulestr: smack label to be audited
4339 * @vrule: pointer to save our own audit rule representation
4340 *
4341 * Prepare to audit cases where (@field @op @rulestr) is true.
4342 * The label to be audited is created if necessay.
4343 */
4344static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4345{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004346 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004347 char **rule = (char **)vrule;
4348 *rule = NULL;
4349
4350 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4351 return -EINVAL;
4352
Al Viro5af75d82008-12-16 05:59:26 -05004353 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004354 return -EINVAL;
4355
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004356 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004357 if (IS_ERR(skp))
4358 return PTR_ERR(skp);
4359
4360 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004361
4362 return 0;
4363}
4364
4365/**
4366 * smack_audit_rule_known - Distinguish Smack audit rules
4367 * @krule: rule of interest, in Audit kernel representation format
4368 *
4369 * This is used to filter Smack rules from remaining Audit ones.
4370 * If it's proved that this rule belongs to us, the
4371 * audit_rule_match hook will be called to do the final judgement.
4372 */
4373static int smack_audit_rule_known(struct audit_krule *krule)
4374{
4375 struct audit_field *f;
4376 int i;
4377
4378 for (i = 0; i < krule->field_count; i++) {
4379 f = &krule->fields[i];
4380
4381 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4382 return 1;
4383 }
4384
4385 return 0;
4386}
4387
4388/**
4389 * smack_audit_rule_match - Audit given object ?
4390 * @secid: security id for identifying the object to test
4391 * @field: audit rule flags given from user-space
4392 * @op: required testing operator
4393 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004394 *
4395 * The core Audit hook. It's used to take the decision of
4396 * whether to audit or not to audit a given object.
4397 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004398static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004399{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004400 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004401 char *rule = vrule;
4402
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004403 if (unlikely(!rule)) {
4404 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004405 return -ENOENT;
4406 }
4407
4408 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4409 return 0;
4410
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004411 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004412
4413 /*
4414 * No need to do string comparisons. If a match occurs,
4415 * both pointers will point to the same smack_known
4416 * label.
4417 */
Al Viro5af75d82008-12-16 05:59:26 -05004418 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004419 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004420 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004421 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004422
4423 return 0;
4424}
4425
Casey Schaufler491a0b02016-01-26 15:08:35 -08004426/*
4427 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004428 * No memory was allocated.
4429 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004430
4431#endif /* CONFIG_AUDIT */
4432
Randy Dunlap251a2a92009-02-18 11:42:33 -08004433/**
David Quigley746df9b2013-05-22 12:50:35 -04004434 * smack_ismaclabel - check if xattr @name references a smack MAC label
4435 * @name: Full xattr name to check.
4436 */
4437static int smack_ismaclabel(const char *name)
4438{
4439 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4440}
4441
4442
4443/**
Casey Schauflere114e472008-02-04 22:29:50 -08004444 * smack_secid_to_secctx - return the smack label for a secid
4445 * @secid: incoming integer
4446 * @secdata: destination
4447 * @seclen: how long it is
4448 *
4449 * Exists for networking code.
4450 */
4451static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4452{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004453 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004454
Eric Parisd5630b92010-10-13 16:24:48 -04004455 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004456 *secdata = skp->smk_known;
4457 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004458 return 0;
4459}
4460
Randy Dunlap251a2a92009-02-18 11:42:33 -08004461/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004462 * smack_secctx_to_secid - return the secid for a smack label
4463 * @secdata: smack label
4464 * @seclen: how long result is
4465 * @secid: outgoing integer
4466 *
4467 * Exists for audit and networking code.
4468 */
David Howellse52c17642008-04-29 20:52:51 +01004469static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004470{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004471 struct smack_known *skp = smk_find_entry(secdata);
4472
4473 if (skp)
4474 *secid = skp->smk_secid;
4475 else
4476 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004477 return 0;
4478}
4479
Casey Schaufler491a0b02016-01-26 15:08:35 -08004480/*
4481 * There used to be a smack_release_secctx hook
4482 * that did nothing back when hooks were in a vector.
4483 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004484 */
Casey Schauflere114e472008-02-04 22:29:50 -08004485
David P. Quigley1ee65e32009-09-03 14:25:57 -04004486static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4487{
4488 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4489}
4490
4491static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4492{
4493 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4494}
4495
4496static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4497{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004498 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004499
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004500 *ctx = skp->smk_known;
4501 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004502 return 0;
4503}
4504
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004505static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4506{
4507
4508 struct task_smack *tsp;
4509 struct smack_known *skp;
4510 struct inode_smack *isp;
4511 struct cred *new_creds = *new;
4512
4513 if (new_creds == NULL) {
4514 new_creds = prepare_creds();
4515 if (new_creds == NULL)
4516 return -ENOMEM;
4517 }
4518
Casey Schauflerb17103a2018-11-09 16:12:56 -08004519 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004520
4521 /*
4522 * Get label from overlay inode and set it in create_sid
4523 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004524 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004525 skp = isp->smk_inode;
4526 tsp->smk_task = skp;
4527 *new = new_creds;
4528 return 0;
4529}
4530
4531static int smack_inode_copy_up_xattr(const char *name)
4532{
4533 /*
4534 * Return 1 if this is the smack access Smack attribute.
4535 */
4536 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4537 return 1;
4538
4539 return -EOPNOTSUPP;
4540}
4541
4542static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4543 struct qstr *name,
4544 const struct cred *old,
4545 struct cred *new)
4546{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004547 struct task_smack *otsp = smack_cred(old);
4548 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004549 struct inode_smack *isp;
4550 int may;
4551
4552 /*
4553 * Use the process credential unless all of
4554 * the transmuting criteria are met
4555 */
4556 ntsp->smk_task = otsp->smk_task;
4557
4558 /*
4559 * the attribute of the containing directory
4560 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004561 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004562
4563 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4564 rcu_read_lock();
4565 may = smk_access_entry(otsp->smk_task->smk_known,
4566 isp->smk_inode->smk_known,
4567 &otsp->smk_task->smk_rules);
4568 rcu_read_unlock();
4569
4570 /*
4571 * If the directory is transmuting and the rule
4572 * providing access is transmuting use the containing
4573 * directory label instead of the process label.
4574 */
4575 if (may > 0 && (may & MAY_TRANSMUTE))
4576 ntsp->smk_task = isp->smk_inode;
4577 }
4578 return 0;
4579}
4580
Casey Schauflerbbd36622018-11-12 09:30:56 -08004581struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4582 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004583 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004584 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004585 .lbs_ipc = sizeof(struct smack_known *),
4586 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004587};
4588
James Morrisca97d932017-02-15 00:18:51 +11004589static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004590 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4591 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4592 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004593
Al Viro0b520752018-12-23 16:02:47 -05004594 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004595 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4596
Casey Schauflere20b0432015-05-02 15:11:36 -07004597 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4598 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004599 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004600 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004601 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304602 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004603
Casey Schauflere20b0432015-05-02 15:11:36 -07004604 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004605
Casey Schauflere20b0432015-05-02 15:11:36 -07004606 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004607 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4608 LSM_HOOK_INIT(inode_link, smack_inode_link),
4609 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4610 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4611 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4612 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4613 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4614 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4615 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4616 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4617 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4618 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4619 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4620 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4621 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4622 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004623
Casey Schauflere20b0432015-05-02 15:11:36 -07004624 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004625 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4626 LSM_HOOK_INIT(file_lock, smack_file_lock),
4627 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4628 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4629 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4630 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4631 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4632 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004633
Casey Schauflere20b0432015-05-02 15:11:36 -07004634 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004635
Casey Schauflere20b0432015-05-02 15:11:36 -07004636 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4637 LSM_HOOK_INIT(cred_free, smack_cred_free),
4638 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4639 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004640 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004641 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4642 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4643 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4644 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4645 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4646 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4647 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4648 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4649 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4650 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4651 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4652 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4653 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004654 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004655
Casey Schauflere20b0432015-05-02 15:11:36 -07004656 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4657 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004658
Casey Schauflere20b0432015-05-02 15:11:36 -07004659 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004660
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004661 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004662 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4663 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4664 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4665 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004666
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004667 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004668 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4669 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4670 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004671
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004672 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004673 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4674 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4675 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004676
Casey Schauflere20b0432015-05-02 15:11:36 -07004677 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004678
Casey Schauflere20b0432015-05-02 15:11:36 -07004679 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4680 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004681
Casey Schauflere20b0432015-05-02 15:11:36 -07004682 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4683 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004684
Casey Schauflere20b0432015-05-02 15:11:36 -07004685 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004686 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004687#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004688 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004689#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004690 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4691 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4692 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4693 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4694 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4695 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4696 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4697 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4698 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4699 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004700
Casey Schauflere114e472008-02-04 22:29:50 -08004701 /* key management security hooks */
4702#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004703 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4704 LSM_HOOK_INIT(key_free, smack_key_free),
4705 LSM_HOOK_INIT(key_permission, smack_key_permission),
4706 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004707#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004708
4709 /* Audit hooks */
4710#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004711 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4712 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4713 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004714#endif /* CONFIG_AUDIT */
4715
Casey Schauflere20b0432015-05-02 15:11:36 -07004716 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4717 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4718 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004719 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4720 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4721 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004722 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4723 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4724 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004725};
4726
Etienne Basset7198e2e2009-03-24 20:53:24 +01004727
Casey Schaufler86812bb2012-04-17 18:55:46 -07004728static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004729{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004730 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004731 * Initialize rule list locks
4732 */
4733 mutex_init(&smack_known_huh.smk_rules_lock);
4734 mutex_init(&smack_known_hat.smk_rules_lock);
4735 mutex_init(&smack_known_floor.smk_rules_lock);
4736 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004737 mutex_init(&smack_known_web.smk_rules_lock);
4738 /*
4739 * Initialize rule lists
4740 */
4741 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4742 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4743 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4744 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004745 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4746 /*
4747 * Create the known labels list
4748 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004749 smk_insert_entry(&smack_known_huh);
4750 smk_insert_entry(&smack_known_hat);
4751 smk_insert_entry(&smack_known_star);
4752 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004753 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004754}
4755
Casey Schauflere114e472008-02-04 22:29:50 -08004756/**
4757 * smack_init - initialize the smack system
4758 *
luanshia1a07f22019-07-05 10:35:20 +08004759 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004760 */
4761static __init int smack_init(void)
4762{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004763 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004764 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004765
Rohit1a5b4722014-10-15 17:40:41 +05304766 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4767 if (!smack_inode_cache)
4768 return -ENOMEM;
4769
Casey Schaufler4e328b02019-04-02 11:37:12 -07004770 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4771 if (!smack_rule_cache) {
4772 kmem_cache_destroy(smack_inode_cache);
4773 return -ENOMEM;
4774 }
4775
Casey Schauflerbbd36622018-11-12 09:30:56 -08004776 /*
4777 * Set the security state for the initial task.
4778 */
4779 tsp = smack_cred(cred);
4780 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4781
4782 /*
4783 * Register with LSM
4784 */
4785 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004786 smack_enabled = 1;
4787
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004788 pr_info("Smack: Initializing.\n");
4789#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4790 pr_info("Smack: Netfilter enabled.\n");
4791#endif
4792#ifdef SMACK_IPV6_PORT_LABELING
4793 pr_info("Smack: IPv6 port labeling enabled.\n");
4794#endif
4795#ifdef SMACK_IPV6_SECMARK_LABELING
4796 pr_info("Smack: IPv6 Netfilter enabled.\n");
4797#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004798
Casey Schaufler86812bb2012-04-17 18:55:46 -07004799 /* initialize the smack_known_list */
4800 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004801
Casey Schauflere114e472008-02-04 22:29:50 -08004802 return 0;
4803}
4804
4805/*
4806 * Smack requires early initialization in order to label
4807 * all processes and objects when they are created.
4808 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004809DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004810 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004811 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004812 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004813 .init = smack_init,
4814};