blob: 0ac8f4518d0720abed5662a06037ffad9c75e4eb [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/**
Eric W. Biedermanb8bff592020-03-22 15:46:24 -0500894 * smack_bprm_creds_for_exec - Update bprm->cred if needed for exec
Casey Schauflerce8a4322011-09-29 18:21:01 -0700895 * @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 */
Eric W. Biedermanb8bff592020-03-22 15:46:24 -0500899static int smack_bprm_creds_for_exec(struct linux_binprm *bprm)
Casey Schaufler676dac42010-12-02 06:43:39 -0800900{
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
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800907 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300908 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800909 return 0;
910
Seth Forshee809c02e2016-04-26 14:36:22 -0500911 sbsp = inode->i_sb->s_security;
912 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
913 isp->smk_task != sbsp->smk_root)
914 return 0;
915
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300916 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100917 struct task_struct *tracer;
918 rc = 0;
919
920 rcu_read_lock();
921 tracer = ptrace_parent(current);
922 if (likely(tracer != NULL))
923 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200924 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100925 PTRACE_MODE_ATTACH,
926 __func__);
927 rcu_read_unlock();
928
929 if (rc != 0)
930 return rc;
Jann Horn3675f052019-07-04 20:44:44 +0200931 }
932 if (bprm->unsafe & ~LSM_UNSAFE_PTRACE)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300933 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800934
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300935 bsp->smk_task = isp->smk_task;
936 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800937
Kees Cookccbb6e12017-07-18 15:25:26 -0700938 /* Decide if this is a secure exec. */
939 if (bsp->smk_task != bsp->smk_forked)
940 bprm->secureexec = 1;
941
Casey Schaufler676dac42010-12-02 06:43:39 -0800942 return 0;
943}
944
945/*
Casey Schauflere114e472008-02-04 22:29:50 -0800946 * Inode hooks
947 */
948
949/**
950 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800951 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800952 *
luanshia1a07f22019-07-05 10:35:20 +0800953 * Returns 0
Casey Schauflere114e472008-02-04 22:29:50 -0800954 */
955static int smack_inode_alloc_security(struct inode *inode)
956{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700957 struct smack_known *skp = smk_of_current();
958
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700959 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800960 return 0;
961}
962
963/**
Casey Schauflere114e472008-02-04 22:29:50 -0800964 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200965 * @inode: the newly created inode
966 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500967 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800968 * @name: where to put the attribute name
969 * @value: where to put the attribute value
970 * @len: where to put the length of the attribute
971 *
972 * Returns 0 if it all works out, -ENOMEM if there's no memory
973 */
974static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900975 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500976 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800977{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800978 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700979 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200980 struct smack_known *isp = smk_of_inode(inode);
981 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800982 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800983
Tetsuo Handa95489062013-07-25 05:44:02 +0900984 if (name)
985 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800986
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100987 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800988 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200989 may = smk_access_entry(skp->smk_known, dsp->smk_known,
990 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800991 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200992
993 /*
994 * If the access rule allows transmutation and
995 * the directory requests transmutation then
996 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700997 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200998 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800999 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -07001000 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001001 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -07001002 issp->smk_flags |= SMK_INODE_CHANGED;
1003 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001004
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001005 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -08001006 if (*value == NULL)
1007 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001008
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001009 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +01001010 }
Casey Schauflere114e472008-02-04 22:29:50 -08001011
1012 return 0;
1013}
1014
1015/**
1016 * smack_inode_link - Smack check on link
1017 * @old_dentry: the existing object
1018 * @dir: unused
1019 * @new_dentry: the new object
1020 *
1021 * Returns 0 if access is permitted, an error code otherwise
1022 */
1023static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
1024 struct dentry *new_dentry)
1025{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001026 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001027 struct smk_audit_info ad;
1028 int rc;
1029
Eric Parisa2694342011-04-25 13:10:27 -04001030 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001031 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001032
David Howellsc6f493d2015-03-17 22:26:22 +00001033 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001034 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001035 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001036
David Howells88025652015-01-29 12:02:32 +00001037 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001038 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001039 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1040 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001041 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001042 }
1043
1044 return rc;
1045}
1046
1047/**
1048 * smack_inode_unlink - Smack check on inode deletion
1049 * @dir: containing directory object
1050 * @dentry: file to unlink
1051 *
1052 * Returns 0 if current can write the containing directory
1053 * and the object, error code otherwise
1054 */
1055static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
1056{
David Howellsc6f493d2015-03-17 22:26:22 +00001057 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001058 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001059 int rc;
1060
Eric Parisa2694342011-04-25 13:10:27 -04001061 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001062 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1063
Casey Schauflere114e472008-02-04 22:29:50 -08001064 /*
1065 * You need write access to the thing you're unlinking
1066 */
Etienne Bassetecfcc532009-04-08 20:40:06 +02001067 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001068 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001069 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001070 /*
1071 * You also need write access to the containing directory
1072 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001073 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001074 smk_ad_setfield_u_fs_inode(&ad, dir);
1075 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001076 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001077 }
Casey Schauflere114e472008-02-04 22:29:50 -08001078 return rc;
1079}
1080
1081/**
1082 * smack_inode_rmdir - Smack check on directory deletion
1083 * @dir: containing directory object
1084 * @dentry: directory to unlink
1085 *
1086 * Returns 0 if current can write the containing directory
1087 * and the directory, error code otherwise
1088 */
1089static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1090{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001091 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001092 int rc;
1093
Eric Parisa2694342011-04-25 13:10:27 -04001094 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001095 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1096
Casey Schauflere114e472008-02-04 22:29:50 -08001097 /*
1098 * You need write access to the thing you're removing
1099 */
David Howellsc6f493d2015-03-17 22:26:22 +00001100 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1101 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001102 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001103 /*
1104 * You also need write access to the containing directory
1105 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001106 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001107 smk_ad_setfield_u_fs_inode(&ad, dir);
1108 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001109 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001110 }
Casey Schauflere114e472008-02-04 22:29:50 -08001111
1112 return rc;
1113}
1114
1115/**
1116 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001117 * @old_inode: unused
1118 * @old_dentry: the old object
1119 * @new_inode: unused
1120 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001121 *
1122 * Read and write access is required on both the old and
1123 * new directories.
1124 *
1125 * Returns 0 if access is permitted, an error code otherwise
1126 */
1127static int smack_inode_rename(struct inode *old_inode,
1128 struct dentry *old_dentry,
1129 struct inode *new_inode,
1130 struct dentry *new_dentry)
1131{
1132 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001133 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001134 struct smk_audit_info ad;
1135
Eric Parisa2694342011-04-25 13:10:27 -04001136 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001137 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001138
David Howellsc6f493d2015-03-17 22:26:22 +00001139 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001140 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001141 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001142
David Howells88025652015-01-29 12:02:32 +00001143 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001144 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1146 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001147 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001148 }
Casey Schauflere114e472008-02-04 22:29:50 -08001149 return rc;
1150}
1151
1152/**
1153 * smack_inode_permission - Smack version of permission()
1154 * @inode: the inode in question
1155 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001156 *
1157 * This is the important Smack hook.
1158 *
luanshia1a07f22019-07-05 10:35:20 +08001159 * Returns 0 if access is permitted, an error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001160 */
Al Viroe74f71e2011-06-20 19:38:15 -04001161static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001162{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001163 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001164 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001165 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001166 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001167
1168 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001169 /*
1170 * No permission to check. Existence test. Yup, it's there.
1171 */
1172 if (mask == 0)
1173 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001174
Seth Forshee9f50eda2015-09-23 15:16:06 -05001175 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1176 if (smk_of_inode(inode) != sbsp->smk_root)
1177 return -EACCES;
1178 }
1179
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001180 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001181 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001182 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001183 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001184 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001185 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1186 rc = smk_bu_inode(inode, mask, rc);
1187 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001188}
1189
1190/**
1191 * smack_inode_setattr - Smack check for setting attributes
1192 * @dentry: the object
1193 * @iattr: for the force flag
1194 *
1195 * Returns 0 if access is permitted, an error code otherwise
1196 */
1197static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1198{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001199 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001200 int rc;
1201
Casey Schauflere114e472008-02-04 22:29:50 -08001202 /*
1203 * Need to allow for clearing the setuid bit.
1204 */
1205 if (iattr->ia_valid & ATTR_FORCE)
1206 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001207 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001208 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001209
David Howellsc6f493d2015-03-17 22:26:22 +00001210 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1211 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001212 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001213}
1214
1215/**
1216 * smack_inode_getattr - Smack check for getting attributes
luanshia1a07f22019-07-05 10:35:20 +08001217 * @path: path to extract the info from
Casey Schauflere114e472008-02-04 22:29:50 -08001218 *
1219 * Returns 0 if access is permitted, an error code otherwise
1220 */
Al Viro3f7036a2015-03-08 19:28:30 -04001221static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001222{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001223 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001224 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001225 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001226
Eric Parisf48b7392011-04-25 12:54:27 -04001227 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001228 smk_ad_setfield_u_fs_path(&ad, *path);
1229 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1230 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001231 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001232}
1233
1234/**
1235 * smack_inode_setxattr - Smack check for setting xattrs
1236 * @dentry: the object
1237 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001238 * @value: value of the attribute
1239 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001240 * @flags: unused
1241 *
1242 * This protects the Smack attribute explicitly.
1243 *
1244 * Returns 0 if access is permitted, an error code otherwise
1245 */
David Howells8f0cfa52008-04-29 00:59:41 -07001246static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1247 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001248{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001249 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001250 struct smack_known *skp;
1251 int check_priv = 0;
1252 int check_import = 0;
1253 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001254 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001255
Casey Schaufler19760ad2013-12-16 16:27:26 -08001256 /*
1257 * Check label validity here so import won't fail in post_setxattr
1258 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001259 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1260 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001261 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1262 check_priv = 1;
1263 check_import = 1;
1264 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1265 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1266 check_priv = 1;
1267 check_import = 1;
1268 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001269 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001270 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001271 if (size != TRANS_TRUE_SIZE ||
1272 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1273 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001274 } else
1275 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1276
Casey Schaufler19760ad2013-12-16 16:27:26 -08001277 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1278 rc = -EPERM;
1279
1280 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001281 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001282 if (IS_ERR(skp))
1283 rc = PTR_ERR(skp);
1284 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001285 (skp == &smack_known_star || skp == &smack_known_web)))
1286 rc = -EINVAL;
1287 }
1288
Eric Parisa2694342011-04-25 13:10:27 -04001289 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001290 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1291
Casey Schauflerd166c802014-08-27 14:51:27 -07001292 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001293 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1294 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001295 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001296
1297 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001298}
1299
1300/**
1301 * smack_inode_post_setxattr - Apply the Smack update approved above
1302 * @dentry: object
1303 * @name: attribute name
1304 * @value: attribute value
1305 * @size: attribute size
1306 * @flags: unused
1307 *
1308 * Set the pointer in the inode blob to the entry found
1309 * in the master label list.
1310 */
David Howells8f0cfa52008-04-29 00:59:41 -07001311static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1312 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001313{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001314 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001315 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001316
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001317 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1318 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1319 return;
1320 }
1321
Casey Schaufler676dac42010-12-02 06:43:39 -08001322 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001323 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001324 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001325 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001326 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001327 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001328 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001329 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001330 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001331 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001332 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001333 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001334 }
Casey Schauflere114e472008-02-04 22:29:50 -08001335
1336 return;
1337}
1338
Casey Schauflerce8a4322011-09-29 18:21:01 -07001339/**
Casey Schauflere114e472008-02-04 22:29:50 -08001340 * smack_inode_getxattr - Smack check on getxattr
1341 * @dentry: the object
1342 * @name: unused
1343 *
1344 * Returns 0 if access is permitted, an error code otherwise
1345 */
David Howells8f0cfa52008-04-29 00:59:41 -07001346static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001347{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001348 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001349 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001350
Eric Parisa2694342011-04-25 13:10:27 -04001351 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001352 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1353
David Howellsc6f493d2015-03-17 22:26:22 +00001354 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1355 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001356 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001357}
1358
Casey Schauflerce8a4322011-09-29 18:21:01 -07001359/**
Casey Schauflere114e472008-02-04 22:29:50 -08001360 * smack_inode_removexattr - Smack check on removexattr
1361 * @dentry: the object
1362 * @name: name of the attribute
1363 *
1364 * Removing the Smack attribute requires CAP_MAC_ADMIN
1365 *
1366 * Returns 0 if access is permitted, an error code otherwise
1367 */
David Howells8f0cfa52008-04-29 00:59:41 -07001368static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001369{
Casey Schaufler676dac42010-12-02 06:43:39 -08001370 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001371 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001372 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001373
Casey Schauflerbcdca222008-02-23 15:24:04 -08001374 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1375 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001376 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001377 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001378 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301379 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001380 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001381 rc = -EPERM;
1382 } else
1383 rc = cap_inode_removexattr(dentry, name);
1384
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001385 if (rc != 0)
1386 return rc;
1387
Eric Parisa2694342011-04-25 13:10:27 -04001388 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001389 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001390
David Howellsc6f493d2015-03-17 22:26:22 +00001391 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1392 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001393 if (rc != 0)
1394 return rc;
1395
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001396 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001397 /*
1398 * Don't do anything special for these.
1399 * XATTR_NAME_SMACKIPIN
1400 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001401 */
José Bollo80124952016-01-12 21:23:40 +01001402 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001403 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001404 struct superblock_smack *sbsp = sbp->s_security;
1405
1406 isp->smk_inode = sbsp->smk_default;
1407 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001408 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001409 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001410 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001411 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1412 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001413
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001414 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001415}
1416
1417/**
1418 * smack_inode_getsecurity - get smack xattrs
1419 * @inode: the object
1420 * @name: attribute name
1421 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001422 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001423 *
1424 * Returns the size of the attribute or an error code
1425 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001426static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001427 const char *name, void **buffer,
1428 bool alloc)
1429{
1430 struct socket_smack *ssp;
1431 struct socket *sock;
1432 struct super_block *sbp;
1433 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001434 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001435
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001436 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001437 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001438 else {
1439 /*
1440 * The rest of the Smack xattrs are only on sockets.
1441 */
1442 sbp = ip->i_sb;
1443 if (sbp->s_magic != SOCKFS_MAGIC)
1444 return -EOPNOTSUPP;
1445
1446 sock = SOCKET_I(ip);
1447 if (sock == NULL || sock->sk == NULL)
1448 return -EOPNOTSUPP;
1449
1450 ssp = sock->sk->sk_security;
1451
1452 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1453 isp = ssp->smk_in;
1454 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1455 isp = ssp->smk_out;
1456 else
1457 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001458 }
1459
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001460 if (alloc) {
1461 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1462 if (*buffer == NULL)
1463 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001464 }
1465
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001466 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001467}
1468
1469
1470/**
1471 * smack_inode_listsecurity - list the Smack attributes
1472 * @inode: the object
1473 * @buffer: where they go
1474 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001475 */
1476static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1477 size_t buffer_size)
1478{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001479 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001480
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001481 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001482 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001483
1484 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001485}
1486
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001487/**
1488 * smack_inode_getsecid - Extract inode's security id
1489 * @inode: inode to extract the info from
1490 * @secid: where result will be saved
1491 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001492static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001493{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001494 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001495
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001496 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001497}
1498
Casey Schauflere114e472008-02-04 22:29:50 -08001499/*
1500 * File Hooks
1501 */
1502
Casey Schaufler491a0b02016-01-26 15:08:35 -08001503/*
1504 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001505 *
1506 * Should access checks be done on each read or write?
1507 * UNICOS and SELinux say yes.
1508 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1509 *
1510 * I'll say no for now. Smack does not do the frequent
1511 * label changing that SELinux does.
1512 */
Casey Schauflere114e472008-02-04 22:29:50 -08001513
1514/**
1515 * smack_file_alloc_security - assign a file security blob
1516 * @file: the object
1517 *
1518 * The security blob for a file is a pointer to the master
1519 * label list, so no allocation is done.
1520 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001521 * f_security is the owner security information. It
1522 * isn't used on file access checks, it's for send_sigio.
1523 *
Casey Schauflere114e472008-02-04 22:29:50 -08001524 * Returns 0
1525 */
1526static int smack_file_alloc_security(struct file *file)
1527{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001528 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001529
Casey Schauflerf28952a2018-11-12 09:38:53 -08001530 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001531 return 0;
1532}
1533
1534/**
Casey Schauflere114e472008-02-04 22:29:50 -08001535 * smack_file_ioctl - Smack check on ioctls
1536 * @file: the object
1537 * @cmd: what to do
1538 * @arg: unused
1539 *
1540 * Relies heavily on the correct use of the ioctl command conventions.
1541 *
1542 * Returns 0 if allowed, error code otherwise
1543 */
1544static int smack_file_ioctl(struct file *file, unsigned int cmd,
1545 unsigned long arg)
1546{
1547 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001548 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001549 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001550
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001551 if (unlikely(IS_PRIVATE(inode)))
1552 return 0;
1553
Eric Parisf48b7392011-04-25 12:54:27 -04001554 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001555 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001556
Casey Schauflerd166c802014-08-27 14:51:27 -07001557 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001558 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001559 rc = smk_bu_file(file, MAY_WRITE, rc);
1560 }
Casey Schauflere114e472008-02-04 22:29:50 -08001561
Casey Schauflerd166c802014-08-27 14:51:27 -07001562 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001563 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001564 rc = smk_bu_file(file, MAY_READ, rc);
1565 }
Casey Schauflere114e472008-02-04 22:29:50 -08001566
1567 return rc;
1568}
1569
1570/**
1571 * smack_file_lock - Smack check on file locking
1572 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001573 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001574 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001575 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001576 */
1577static int smack_file_lock(struct file *file, unsigned int cmd)
1578{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001579 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001580 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001581 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001582
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001583 if (unlikely(IS_PRIVATE(inode)))
1584 return 0;
1585
Eric Paris92f42502011-04-25 13:15:55 -04001586 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1587 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001588 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001589 rc = smk_bu_file(file, MAY_LOCK, rc);
1590 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001591}
1592
1593/**
1594 * smack_file_fcntl - Smack check on fcntl
1595 * @file: the object
1596 * @cmd: what action to check
1597 * @arg: unused
1598 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001599 * Generally these operations are harmless.
1600 * File locking operations present an obvious mechanism
1601 * for passing information, so they require write access.
1602 *
Casey Schauflere114e472008-02-04 22:29:50 -08001603 * Returns 0 if current has access, error code otherwise
1604 */
1605static int smack_file_fcntl(struct file *file, unsigned int cmd,
1606 unsigned long arg)
1607{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001608 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001609 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001610 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001611
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001612 if (unlikely(IS_PRIVATE(inode)))
1613 return 0;
1614
Casey Schauflere114e472008-02-04 22:29:50 -08001615 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001616 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001617 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001618 case F_SETLK:
1619 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001620 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1621 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001622 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001623 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001624 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001625 case F_SETOWN:
1626 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001627 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1628 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001629 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001630 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001631 break;
1632 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001633 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001634 }
1635
1636 return rc;
1637}
1638
1639/**
Al Viroe5467852012-05-30 13:30:51 -04001640 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001641 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1642 * if mapping anonymous memory.
1643 * @file contains the file structure for file to map (may be NULL).
1644 * @reqprot contains the protection requested by the application.
1645 * @prot contains the protection that will be applied by the kernel.
1646 * @flags contains the operational flags.
1647 * Return 0 if permission is granted.
1648 */
Al Viroe5467852012-05-30 13:30:51 -04001649static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001650 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001651 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001652{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001653 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001654 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001655 struct smack_rule *srp;
1656 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001657 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001658 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001659 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001660 int may;
1661 int mmay;
1662 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001663 int rc;
1664
Al Viro496ad9a2013-01-23 17:07:38 -05001665 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001666 return 0;
1667
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001668 if (unlikely(IS_PRIVATE(file_inode(file))))
1669 return 0;
1670
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001671 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001672 if (isp->smk_mmap == NULL)
1673 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001674 sbsp = file_inode(file)->i_sb->s_security;
1675 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1676 isp->smk_mmap != sbsp->smk_root)
1677 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001678 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001679
Casey Schauflerb17103a2018-11-09 16:12:56 -08001680 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001681 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001682 rc = 0;
1683
1684 rcu_read_lock();
1685 /*
1686 * For each Smack rule associated with the subject
1687 * label verify that the SMACK64MMAP also has access
1688 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001689 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001690 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001691 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001692 /*
1693 * Matching labels always allows access.
1694 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001695 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001696 continue;
1697 /*
1698 * If there is a matching local rule take
1699 * that into account as well.
1700 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001701 may = smk_access_entry(srp->smk_subject->smk_known,
1702 okp->smk_known,
1703 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001704 if (may == -ENOENT)
1705 may = srp->smk_access;
1706 else
1707 may &= srp->smk_access;
1708 /*
1709 * If may is zero the SMACK64MMAP subject can't
1710 * possibly have less access.
1711 */
1712 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001713 continue;
1714
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001715 /*
1716 * Fetch the global list entry.
1717 * If there isn't one a SMACK64MMAP subject
1718 * can't have as much access as current.
1719 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001720 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1721 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001722 if (mmay == -ENOENT) {
1723 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001724 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001725 }
1726 /*
1727 * If there is a local entry it modifies the
1728 * potential access, too.
1729 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001730 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1731 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001732 if (tmay != -ENOENT)
1733 mmay &= tmay;
1734
1735 /*
1736 * If there is any access available to current that is
1737 * not available to a SMACK64MMAP subject
1738 * deny access.
1739 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001740 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001741 rc = -EACCES;
1742 break;
1743 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001744 }
1745
1746 rcu_read_unlock();
1747
1748 return rc;
1749}
1750
1751/**
Casey Schauflere114e472008-02-04 22:29:50 -08001752 * smack_file_set_fowner - set the file security blob value
1753 * @file: object in question
1754 *
Casey Schauflere114e472008-02-04 22:29:50 -08001755 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001756static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001757{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001758 struct smack_known **blob = smack_file(file);
1759
1760 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001761}
1762
1763/**
1764 * smack_file_send_sigiotask - Smack on sigio
1765 * @tsk: The target task
1766 * @fown: the object the signal come from
1767 * @signum: unused
1768 *
1769 * Allow a privileged task to get signals even if it shouldn't
1770 *
1771 * Returns 0 if a subject with the object's smack could
1772 * write to the task, an error code otherwise.
1773 */
1774static int smack_file_send_sigiotask(struct task_struct *tsk,
1775 struct fown_struct *fown, int signum)
1776{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001777 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001778 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001779 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001780 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001781 struct file *file;
1782 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001783 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001784
1785 /*
1786 * struct fown_struct is never outside the context of a struct file
1787 */
1788 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001789
Etienne Bassetecfcc532009-04-08 20:40:06 +02001790 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001791 blob = smack_file(file);
1792 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001793 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1794 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001795
1796 rcu_read_lock();
1797 tcred = __task_cred(tsk);
1798 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001799 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001800 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001801
1802 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1803 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001804 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001805 return rc;
1806}
1807
1808/**
1809 * smack_file_receive - Smack file receive check
1810 * @file: the object
1811 *
1812 * Returns 0 if current has access, error code otherwise
1813 */
1814static int smack_file_receive(struct file *file)
1815{
Casey Schauflerd166c802014-08-27 14:51:27 -07001816 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001817 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001818 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001819 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001820 struct socket *sock;
1821 struct task_smack *tsp;
1822 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001823
Seung-Woo Kim97775822015-04-17 15:25:04 +09001824 if (unlikely(IS_PRIVATE(inode)))
1825 return 0;
1826
Casey Schaufler4482a442013-12-30 17:37:45 -08001827 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001828 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001829
Casey Schaufler51d59af2017-05-31 08:53:42 -07001830 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001831 sock = SOCKET_I(inode);
1832 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001833 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001834 /*
1835 * If the receiving process can't write to the
1836 * passed socket or if the passed socket can't
1837 * write to the receiving process don't accept
1838 * the passed socket.
1839 */
1840 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1841 rc = smk_bu_file(file, may, rc);
1842 if (rc < 0)
1843 return rc;
1844 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1845 rc = smk_bu_file(file, may, rc);
1846 return rc;
1847 }
Casey Schauflere114e472008-02-04 22:29:50 -08001848 /*
1849 * This code relies on bitmasks.
1850 */
1851 if (file->f_mode & FMODE_READ)
1852 may = MAY_READ;
1853 if (file->f_mode & FMODE_WRITE)
1854 may |= MAY_WRITE;
1855
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001856 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001857 rc = smk_bu_file(file, may, rc);
1858 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001859}
1860
Casey Schaufler531f1d42011-09-19 12:41:42 -07001861/**
Eric Paris83d49852012-04-04 13:45:40 -04001862 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001863 * @file: the object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001864 *
1865 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001866 * Allow the open only if the task has read access. There are
1867 * many read operations (e.g. fstat) that you can do with an
1868 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001869 *
luanshia1a07f22019-07-05 10:35:20 +08001870 * Returns 0 if current has access, error code otherwise
Casey Schaufler531f1d42011-09-19 12:41:42 -07001871 */
Al Viro94817692018-07-10 14:13:18 -04001872static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001873{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001874 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001875 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001876 struct smk_audit_info ad;
1877 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001878
Casey Schauflera6834c02014-04-21 11:10:26 -07001879 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1880 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301881 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001882 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001883
1884 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001885}
1886
Casey Schauflere114e472008-02-04 22:29:50 -08001887/*
1888 * Task hooks
1889 */
1890
1891/**
David Howellsee18d642009-09-02 09:14:21 +01001892 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
luanshia1a07f22019-07-05 10:35:20 +08001893 * @cred: the new credentials
David Howellsee18d642009-09-02 09:14:21 +01001894 * @gfp: the atomicity of any memory allocations
1895 *
1896 * Prepare a blank set of credentials for modification. This must allocate all
1897 * the memory the LSM module might require such that cred_transfer() can
1898 * complete without error.
1899 */
1900static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1901{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001902 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001903 return 0;
1904}
1905
1906
1907/**
David Howellsf1752ee2008-11-14 10:39:17 +11001908 * smack_cred_free - "free" task-level security credentials
1909 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001910 *
Casey Schauflere114e472008-02-04 22:29:50 -08001911 */
David Howellsf1752ee2008-11-14 10:39:17 +11001912static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001913{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001914 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001915 struct smack_rule *rp;
1916 struct list_head *l;
1917 struct list_head *n;
1918
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001919 smk_destroy_label_list(&tsp->smk_relabel);
1920
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001921 list_for_each_safe(l, n, &tsp->smk_rules) {
1922 rp = list_entry(l, struct smack_rule, list);
1923 list_del(&rp->list);
Casey Schaufler4e328b02019-04-02 11:37:12 -07001924 kmem_cache_free(smack_rule_cache, rp);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001925 }
Casey Schauflere114e472008-02-04 22:29:50 -08001926}
1927
1928/**
David Howellsd84f4f92008-11-14 10:39:23 +11001929 * smack_cred_prepare - prepare new set of credentials for modification
1930 * @new: the new credentials
1931 * @old: the original credentials
1932 * @gfp: the atomicity of any memory allocations
1933 *
1934 * Prepare a new set of credentials for modification.
1935 */
1936static int smack_cred_prepare(struct cred *new, const struct cred *old,
1937 gfp_t gfp)
1938{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001939 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001940 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001941 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001942
Casey Schauflerbbd36622018-11-12 09:30:56 -08001943 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301944
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001945 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1946 if (rc != 0)
1947 return rc;
1948
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001949 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1950 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001951 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001952}
1953
Randy Dunlap251a2a92009-02-18 11:42:33 -08001954/**
David Howellsee18d642009-09-02 09:14:21 +01001955 * smack_cred_transfer - Transfer the old credentials to the new credentials
1956 * @new: the new credentials
1957 * @old: the original credentials
1958 *
1959 * Fill in a set of blank credentials from another set of credentials.
1960 */
1961static void smack_cred_transfer(struct cred *new, const struct cred *old)
1962{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001963 struct task_smack *old_tsp = smack_cred(old);
1964 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001965
1966 new_tsp->smk_task = old_tsp->smk_task;
1967 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001968 mutex_init(&new_tsp->smk_rules_lock);
1969 INIT_LIST_HEAD(&new_tsp->smk_rules);
1970
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001971 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001972}
1973
1974/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001975 * smack_cred_getsecid - get the secid corresponding to a creds structure
luanshia1a07f22019-07-05 10:35:20 +08001976 * @cred: the object creds
Matthew Garrett3ec30112018-01-08 13:36:19 -08001977 * @secid: where to put the result
1978 *
1979 * Sets the secid to contain a u32 version of the smack label.
1980 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001981static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001982{
1983 struct smack_known *skp;
1984
1985 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001986 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001987 *secid = skp->smk_secid;
1988 rcu_read_unlock();
1989}
1990
1991/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001992 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001993 * @new: points to the set of credentials to be modified.
1994 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001995 *
1996 * Set the security data for a kernel service.
1997 */
1998static int smack_kernel_act_as(struct cred *new, u32 secid)
1999{
Casey Schauflerb17103a2018-11-09 16:12:56 -08002000 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002001
Casey Schaufler152f91d2016-11-14 09:38:15 -08002002 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11002003 return 0;
2004}
2005
2006/**
2007 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08002008 * @new: points to the set of credentials to be modified
2009 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11002010 *
2011 * Set the file creation context in a set of credentials to the same
2012 * as the objective context of the specified inode
2013 */
2014static int smack_kernel_create_files_as(struct cred *new,
2015 struct inode *inode)
2016{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002017 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08002018 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11002019
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002020 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002021 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11002022 return 0;
2023}
2024
2025/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002026 * smk_curacc_on_task - helper to log task related access
2027 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07002028 * @access: the access requested
2029 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02002030 *
2031 * Return 0 if access is permitted
2032 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07002033static int smk_curacc_on_task(struct task_struct *p, int access,
2034 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002035{
2036 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002037 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002038 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002039
Casey Schaufler531f1d42011-09-19 12:41:42 -07002040 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002041 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002042 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07002043 rc = smk_bu_task(p, access, rc);
2044 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002045}
2046
2047/**
Casey Schauflere114e472008-02-04 22:29:50 -08002048 * smack_task_setpgid - Smack check on setting pgid
2049 * @p: the task object
2050 * @pgid: unused
2051 *
2052 * Return 0 if write access is permitted
2053 */
2054static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
2055{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002056 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002057}
2058
2059/**
2060 * smack_task_getpgid - Smack access check for getpgid
2061 * @p: the object task
2062 *
2063 * Returns 0 if current can read the object task, error code otherwise
2064 */
2065static int smack_task_getpgid(struct task_struct *p)
2066{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002067 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002068}
2069
2070/**
2071 * smack_task_getsid - Smack access check for getsid
2072 * @p: the object task
2073 *
2074 * Returns 0 if current can read the object task, error code otherwise
2075 */
2076static int smack_task_getsid(struct task_struct *p)
2077{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002078 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002079}
2080
2081/**
2082 * smack_task_getsecid - get the secid of the task
2083 * @p: the object task
2084 * @secid: where to put the result
2085 *
2086 * Sets the secid to contain a u32 version of the smack label.
2087 */
2088static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2089{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002090 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002091
2092 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002093}
2094
2095/**
2096 * smack_task_setnice - Smack check on setting nice
2097 * @p: the task object
2098 * @nice: unused
2099 *
2100 * Return 0 if write access is permitted
2101 */
2102static int smack_task_setnice(struct task_struct *p, int nice)
2103{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002104 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002105}
2106
2107/**
2108 * smack_task_setioprio - Smack check on setting ioprio
2109 * @p: the task object
2110 * @ioprio: unused
2111 *
2112 * Return 0 if write access is permitted
2113 */
2114static int smack_task_setioprio(struct task_struct *p, int ioprio)
2115{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002116 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002117}
2118
2119/**
2120 * smack_task_getioprio - Smack check on reading ioprio
2121 * @p: the task object
2122 *
2123 * Return 0 if read access is permitted
2124 */
2125static int smack_task_getioprio(struct task_struct *p)
2126{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002127 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002128}
2129
2130/**
2131 * smack_task_setscheduler - Smack check on setting scheduler
2132 * @p: the task object
Casey Schauflere114e472008-02-04 22:29:50 -08002133 *
2134 * Return 0 if read access is permitted
2135 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002136static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002137{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002138 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002139}
2140
2141/**
2142 * smack_task_getscheduler - Smack check on reading scheduler
2143 * @p: the task object
2144 *
2145 * Return 0 if read access is permitted
2146 */
2147static int smack_task_getscheduler(struct task_struct *p)
2148{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002149 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002150}
2151
2152/**
2153 * smack_task_movememory - Smack check on moving memory
2154 * @p: the task object
2155 *
2156 * Return 0 if write access is permitted
2157 */
2158static int smack_task_movememory(struct task_struct *p)
2159{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002160 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002161}
2162
2163/**
2164 * smack_task_kill - Smack check on signal delivery
2165 * @p: the task object
2166 * @info: unused
2167 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002168 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002169 *
2170 * Return 0 if write access is permitted
2171 *
Casey Schauflere114e472008-02-04 22:29:50 -08002172 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002173static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002174 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002175{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002176 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002177 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002178 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002179 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002180
Rafal Krypa18d872f2016-04-04 11:14:53 +02002181 if (!sig)
2182 return 0; /* null signal; existence test */
2183
Etienne Bassetecfcc532009-04-08 20:40:06 +02002184 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2185 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002186 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002187 * Sending a signal requires that the sender
2188 * can write the receiver.
2189 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002190 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002191 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2192 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002193 return rc;
2194 }
Casey Schauflere114e472008-02-04 22:29:50 -08002195 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002196 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002197 * specific behavior. This is not clean. For one thing
2198 * we can't take privilege into account.
2199 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002200 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002201 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2202 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002203 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002204}
2205
2206/**
Casey Schauflere114e472008-02-04 22:29:50 -08002207 * smack_task_to_inode - copy task smack into the inode blob
2208 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002209 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002210 *
2211 * Sets the smack pointer in the inode security blob
2212 */
2213static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2214{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002215 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002216 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002217
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002218 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002219 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002220}
2221
2222/*
2223 * Socket hooks.
2224 */
2225
2226/**
2227 * smack_sk_alloc_security - Allocate a socket blob
2228 * @sk: the socket
2229 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002230 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002231 *
2232 * Assign Smack pointers to current
2233 *
2234 * Returns 0 on success, -ENOMEM is there's no memory
2235 */
2236static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2237{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002238 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002239 struct socket_smack *ssp;
2240
2241 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2242 if (ssp == NULL)
2243 return -ENOMEM;
2244
jooseong lee08382c92016-11-03 11:54:39 +01002245 /*
2246 * Sockets created by kernel threads receive web label.
2247 */
2248 if (unlikely(current->flags & PF_KTHREAD)) {
2249 ssp->smk_in = &smack_known_web;
2250 ssp->smk_out = &smack_known_web;
2251 } else {
2252 ssp->smk_in = skp;
2253 ssp->smk_out = skp;
2254 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002255 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002256
2257 sk->sk_security = ssp;
2258
2259 return 0;
2260}
2261
2262/**
2263 * smack_sk_free_security - Free a socket blob
2264 * @sk: the socket
2265 *
2266 * Clears the blob pointer
2267 */
2268static void smack_sk_free_security(struct sock *sk)
2269{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302270#ifdef SMACK_IPV6_PORT_LABELING
2271 struct smk_port_label *spp;
2272
2273 if (sk->sk_family == PF_INET6) {
2274 rcu_read_lock();
2275 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2276 if (spp->smk_sock != sk)
2277 continue;
2278 spp->smk_can_reuse = 1;
2279 break;
2280 }
2281 rcu_read_unlock();
2282 }
2283#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002284 kfree(sk->sk_security);
2285}
2286
2287/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002288* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002289* @sip: the object end
2290*
2291* looks for host based access restrictions
2292*
2293* This version will only be appropriate for really small sets of single label
2294* hosts. The caller is responsible for ensuring that the RCU read lock is
2295* taken before calling this function.
2296*
2297* Returns the label of the far end or NULL if it's not special.
2298*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002299static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002300{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002301 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002302 struct in_addr *siap = &sip->sin_addr;
2303
2304 if (siap->s_addr == 0)
2305 return NULL;
2306
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002307 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2308 /*
2309 * we break after finding the first match because
2310 * the list is sorted from longest to shortest mask
2311 * so we have found the most specific match
2312 */
2313 if (snp->smk_host.s_addr ==
2314 (siap->s_addr & snp->smk_mask.s_addr))
2315 return snp->smk_label;
2316
2317 return NULL;
2318}
2319
2320#if IS_ENABLED(CONFIG_IPV6)
2321/*
2322 * smk_ipv6_localhost - Check for local ipv6 host address
2323 * @sip: the address
2324 *
2325 * Returns boolean true if this is the localhost address
2326 */
2327static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2328{
2329 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2330 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2331
2332 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2333 ntohs(be16p[7]) == 1)
2334 return true;
2335 return false;
2336}
2337
2338/**
2339* smack_ipv6host_label - check host based restrictions
2340* @sip: the object end
2341*
2342* looks for host based access restrictions
2343*
2344* This version will only be appropriate for really small sets of single label
2345* hosts. The caller is responsible for ensuring that the RCU read lock is
2346* taken before calling this function.
2347*
2348* Returns the label of the far end or NULL if it's not special.
2349*/
2350static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2351{
2352 struct smk_net6addr *snp;
2353 struct in6_addr *sap = &sip->sin6_addr;
2354 int i;
2355 int found = 0;
2356
2357 /*
2358 * It's local. Don't look for a host label.
2359 */
2360 if (smk_ipv6_localhost(sip))
2361 return NULL;
2362
2363 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002364 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002365 * If the label is NULL the entry has
2366 * been renounced. Ignore it.
2367 */
2368 if (snp->smk_label == NULL)
2369 continue;
2370 /*
Paul Moore07feee82009-03-27 17:10:54 -04002371 * we break after finding the first match because
2372 * the list is sorted from longest to shortest mask
2373 * so we have found the most specific match
2374 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002375 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002376 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2377 snp->smk_host.s6_addr16[i]) {
2378 found = 0;
2379 break;
2380 }
Etienne Basset43031542009-03-27 17:11:01 -04002381 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002382 if (found)
2383 return snp->smk_label;
2384 }
Paul Moore07feee82009-03-27 17:10:54 -04002385
2386 return NULL;
2387}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002388#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002389
2390/**
Casey Schauflere114e472008-02-04 22:29:50 -08002391 * smack_netlabel - Set the secattr on a socket
2392 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002393 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002394 *
2395 * Convert the outbound smack value (smk_out) to a
2396 * secattr and attach it to the socket.
2397 *
2398 * Returns 0 on success or an error code
2399 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002400static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002401{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002402 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002403 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002404 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002405
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002406 /*
2407 * Usually the netlabel code will handle changing the
2408 * packet labeling based on the label.
2409 * The case of a single label host is different, because
2410 * a single label host should never get a labeled packet
2411 * even though the label is usually associated with a packet
2412 * label.
2413 */
2414 local_bh_disable();
2415 bh_lock_sock_nested(sk);
2416
2417 if (ssp->smk_out == smack_net_ambient ||
2418 labeled == SMACK_UNLABELED_SOCKET)
2419 netlbl_sock_delattr(sk);
2420 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002421 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002422 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002423 }
2424
2425 bh_unlock_sock(sk);
2426 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002427
Casey Schauflere114e472008-02-04 22:29:50 -08002428 return rc;
2429}
2430
2431/**
Paul Moore07feee82009-03-27 17:10:54 -04002432 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2433 * @sk: the socket
2434 * @sap: the destination address
2435 *
2436 * Set the correct secattr for the given socket based on the destination
2437 * address and perform any outbound access checks needed.
2438 *
2439 * Returns 0 on success or an error code.
2440 *
2441 */
2442static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2443{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002444 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002445 int rc;
2446 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002447 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002448 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002449 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002450
2451 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002452 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002453 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002454#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002455 struct lsm_network_audit net;
2456
Eric Paris48c62af2012-04-02 13:15:44 -04002457 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2458 ad.a.u.net->family = sap->sin_family;
2459 ad.a.u.net->dport = sap->sin_port;
2460 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002461#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002462 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002463 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002464 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2465 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002466 } else {
2467 sk_lbl = SMACK_CIPSO_SOCKET;
2468 rc = 0;
2469 }
2470 rcu_read_unlock();
2471 if (rc != 0)
2472 return rc;
2473
2474 return smack_netlabel(sk, sk_lbl);
2475}
2476
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002477#if IS_ENABLED(CONFIG_IPV6)
2478/**
2479 * smk_ipv6_check - check Smack access
2480 * @subject: subject Smack label
2481 * @object: object Smack label
2482 * @address: address
2483 * @act: the action being taken
2484 *
2485 * Check an IPv6 access
2486 */
2487static int smk_ipv6_check(struct smack_known *subject,
2488 struct smack_known *object,
2489 struct sockaddr_in6 *address, int act)
2490{
2491#ifdef CONFIG_AUDIT
2492 struct lsm_network_audit net;
2493#endif
2494 struct smk_audit_info ad;
2495 int rc;
2496
2497#ifdef CONFIG_AUDIT
2498 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2499 ad.a.u.net->family = PF_INET6;
2500 ad.a.u.net->dport = ntohs(address->sin6_port);
2501 if (act == SMK_RECEIVING)
2502 ad.a.u.net->v6info.saddr = address->sin6_addr;
2503 else
2504 ad.a.u.net->v6info.daddr = address->sin6_addr;
2505#endif
2506 rc = smk_access(subject, object, MAY_WRITE, &ad);
2507 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2508 return rc;
2509}
2510#endif /* CONFIG_IPV6 */
2511
2512#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002513/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002514 * smk_ipv6_port_label - Smack port access table management
2515 * @sock: socket
2516 * @address: address
2517 *
2518 * Create or update the port list entry
2519 */
2520static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2521{
2522 struct sock *sk = sock->sk;
2523 struct sockaddr_in6 *addr6;
2524 struct socket_smack *ssp = sock->sk->sk_security;
2525 struct smk_port_label *spp;
2526 unsigned short port = 0;
2527
2528 if (address == NULL) {
2529 /*
2530 * This operation is changing the Smack information
2531 * on the bound socket. Take the changes to the port
2532 * as well.
2533 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302534 rcu_read_lock();
2535 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002536 if (sk != spp->smk_sock)
2537 continue;
2538 spp->smk_in = ssp->smk_in;
2539 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302540 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002541 return;
2542 }
2543 /*
2544 * A NULL address is only used for updating existing
2545 * bound entries. If there isn't one, it's OK.
2546 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302547 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002548 return;
2549 }
2550
2551 addr6 = (struct sockaddr_in6 *)address;
2552 port = ntohs(addr6->sin6_port);
2553 /*
2554 * This is a special case that is safely ignored.
2555 */
2556 if (port == 0)
2557 return;
2558
2559 /*
2560 * Look for an existing port list entry.
2561 * This is an indication that a port is getting reused.
2562 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302563 rcu_read_lock();
2564 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302565 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002566 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302567 if (spp->smk_can_reuse != 1) {
2568 rcu_read_unlock();
2569 return;
2570 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002571 spp->smk_port = port;
2572 spp->smk_sock = sk;
2573 spp->smk_in = ssp->smk_in;
2574 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302575 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302576 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002577 return;
2578 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302579 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002580 /*
2581 * A new port entry is required.
2582 */
2583 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2584 if (spp == NULL)
2585 return;
2586
2587 spp->smk_port = port;
2588 spp->smk_sock = sk;
2589 spp->smk_in = ssp->smk_in;
2590 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302591 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302592 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002593
Vishal Goel3c7ce342016-11-23 10:31:08 +05302594 mutex_lock(&smack_ipv6_lock);
2595 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2596 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002597 return;
2598}
2599
2600/**
2601 * smk_ipv6_port_check - check Smack port access
luanshia1a07f22019-07-05 10:35:20 +08002602 * @sk: socket
Casey Schauflerc6739442013-05-22 18:42:56 -07002603 * @address: address
luanshia1a07f22019-07-05 10:35:20 +08002604 * @act: the action being taken
Casey Schauflerc6739442013-05-22 18:42:56 -07002605 *
2606 * Create or update the port list entry
2607 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002608static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002609 int act)
2610{
Casey Schauflerc6739442013-05-22 18:42:56 -07002611 struct smk_port_label *spp;
2612 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002613 struct smack_known *skp = NULL;
2614 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002615 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002616
2617 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002618 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002619 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002620 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002621 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002622 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002623 }
2624
2625 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002626 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002627 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002628 if (skp != NULL && object != NULL)
2629 return smk_ipv6_check(skp, object, address, act);
2630 if (skp == NULL)
2631 skp = smack_net_ambient;
2632 if (object == NULL)
2633 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002634
2635 /*
2636 * It's remote, so port lookup does no good.
2637 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002638 if (!smk_ipv6_localhost(address))
2639 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002640
2641 /*
2642 * It's local so the send check has to have passed.
2643 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002644 if (act == SMK_RECEIVING)
2645 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002646
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002647 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302648 rcu_read_lock();
2649 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302650 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002651 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002652 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002653 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002654 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002655 break;
2656 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302657 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002658
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002659 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002660}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002661#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002662
2663/**
Casey Schauflere114e472008-02-04 22:29:50 -08002664 * smack_inode_setsecurity - set smack xattrs
2665 * @inode: the object
2666 * @name: attribute name
2667 * @value: attribute value
2668 * @size: size of the attribute
2669 * @flags: unused
2670 *
2671 * Sets the named attribute in the appropriate blob
2672 *
2673 * Returns 0 on success, or an error code
2674 */
2675static int smack_inode_setsecurity(struct inode *inode, const char *name,
2676 const void *value, size_t size, int flags)
2677{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002678 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002679 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002680 struct socket_smack *ssp;
2681 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002682 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002683
Casey Schauflerf7112e62012-05-06 15:22:02 -07002684 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302685 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002686
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002687 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002688 if (IS_ERR(skp))
2689 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002690
2691 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002692 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002693 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002694 return 0;
2695 }
2696 /*
2697 * The rest of the Smack xattrs are only on sockets.
2698 */
2699 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2700 return -EOPNOTSUPP;
2701
2702 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002703 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002704 return -EOPNOTSUPP;
2705
2706 ssp = sock->sk->sk_security;
2707
2708 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002709 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002710 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002711 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002712 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002713 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2714 if (rc != 0)
2715 printk(KERN_WARNING
2716 "Smack: \"%s\" netlbl error %d.\n",
2717 __func__, -rc);
2718 }
Casey Schauflere114e472008-02-04 22:29:50 -08002719 } else
2720 return -EOPNOTSUPP;
2721
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002722#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002723 if (sock->sk->sk_family == PF_INET6)
2724 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002725#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002726
Casey Schauflere114e472008-02-04 22:29:50 -08002727 return 0;
2728}
2729
2730/**
2731 * smack_socket_post_create - finish socket setup
2732 * @sock: the socket
2733 * @family: protocol family
2734 * @type: unused
2735 * @protocol: unused
2736 * @kern: unused
2737 *
2738 * Sets the netlabel information on the socket
2739 *
2740 * Returns 0 on success, and error code otherwise
2741 */
2742static int smack_socket_post_create(struct socket *sock, int family,
2743 int type, int protocol, int kern)
2744{
Marcin Lis74123012015-01-22 15:40:33 +01002745 struct socket_smack *ssp;
2746
2747 if (sock->sk == NULL)
2748 return 0;
2749
2750 /*
2751 * Sockets created by kernel threads receive web label.
2752 */
2753 if (unlikely(current->flags & PF_KTHREAD)) {
2754 ssp = sock->sk->sk_security;
2755 ssp->smk_in = &smack_known_web;
2756 ssp->smk_out = &smack_known_web;
2757 }
2758
2759 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002760 return 0;
2761 /*
2762 * Set the outbound netlbl.
2763 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002764 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2765}
2766
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002767/**
2768 * smack_socket_socketpair - create socket pair
2769 * @socka: one socket
2770 * @sockb: another socket
2771 *
2772 * Cross reference the peer labels for SO_PEERSEC
2773 *
luanshia1a07f22019-07-05 10:35:20 +08002774 * Returns 0
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002775 */
2776static int smack_socket_socketpair(struct socket *socka,
2777 struct socket *sockb)
2778{
2779 struct socket_smack *asp = socka->sk->sk_security;
2780 struct socket_smack *bsp = sockb->sk->sk_security;
2781
2782 asp->smk_packet = bsp->smk_out;
2783 bsp->smk_packet = asp->smk_out;
2784
2785 return 0;
2786}
2787
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002788#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002789/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002790 * smack_socket_bind - record port binding information.
2791 * @sock: the socket
2792 * @address: the port address
2793 * @addrlen: size of the address
2794 *
2795 * Records the label bound to a port.
2796 *
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002797 * Returns 0 on success, and error code otherwise
Casey Schauflerc6739442013-05-22 18:42:56 -07002798 */
2799static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2800 int addrlen)
2801{
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002802 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6) {
2803 if (addrlen < SIN6_LEN_RFC2133 ||
2804 address->sa_family != AF_INET6)
2805 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07002806 smk_ipv6_port_label(sock, address);
Tetsuo Handab9ef5512019-04-12 19:59:35 +09002807 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002808 return 0;
2809}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002810#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002811
2812/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002813 * smack_socket_connect - connect access check
2814 * @sock: the socket
2815 * @sap: the other end
2816 * @addrlen: size of sap
2817 *
2818 * Verifies that a connection may be possible
2819 *
2820 * Returns 0 on success, and error code otherwise
2821 */
2822static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2823 int addrlen)
2824{
Casey Schauflerc6739442013-05-22 18:42:56 -07002825 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002826
Casey Schauflerc6739442013-05-22 18:42:56 -07002827 if (sock->sk == NULL)
2828 return 0;
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002829 if (sock->sk->sk_family != PF_INET &&
2830 (!IS_ENABLED(CONFIG_IPV6) || sock->sk->sk_family != PF_INET6))
2831 return 0;
2832 if (addrlen < offsetofend(struct sockaddr, sa_family))
2833 return 0;
2834 if (IS_ENABLED(CONFIG_IPV6) && sap->sa_family == AF_INET6) {
2835 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002836#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002837 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002838#endif
2839
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002840 if (addrlen < SIN6_LEN_RFC2133)
2841 return 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002842#ifdef SMACK_IPV6_SECMARK_LABELING
2843 rsp = smack_ipv6host_label(sip);
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002844 if (rsp != NULL) {
2845 struct socket_smack *ssp = sock->sk->sk_security;
2846
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002847 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002848 SMK_CONNECTING);
2849 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002850#endif
2851#ifdef SMACK_IPV6_PORT_LABELING
2852 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2853#endif
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002854 return rc;
Casey Schauflerc6739442013-05-22 18:42:56 -07002855 }
Casey Schaufler87fbfff2020-02-03 09:15:00 -08002856 if (sap->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
2857 return 0;
2858 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
Casey Schauflerc6739442013-05-22 18:42:56 -07002859 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002860}
2861
2862/**
2863 * smack_flags_to_may - convert S_ to MAY_ values
2864 * @flags: the S_ value
2865 *
2866 * Returns the equivalent MAY_ value
2867 */
2868static int smack_flags_to_may(int flags)
2869{
2870 int may = 0;
2871
2872 if (flags & S_IRUGO)
2873 may |= MAY_READ;
2874 if (flags & S_IWUGO)
2875 may |= MAY_WRITE;
2876 if (flags & S_IXUGO)
2877 may |= MAY_EXEC;
2878
2879 return may;
2880}
2881
2882/**
2883 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2884 * @msg: the object
2885 *
2886 * Returns 0
2887 */
2888static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2889{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002890 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002891
Casey Schauflerecd5f822018-11-20 11:55:02 -08002892 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002893 return 0;
2894}
2895
2896/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002897 * smack_of_ipc - the smack pointer for the ipc
2898 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002899 *
2900 * Returns a pointer to the smack value
2901 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002902static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002903{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002904 struct smack_known **blob = smack_ipc(isp);
2905
2906 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002907}
2908
2909/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002910 * smack_ipc_alloc_security - Set the security blob for ipc
2911 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002912 *
2913 * Returns 0
2914 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002915static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002916{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002917 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002918
Casey Schaufler019bcca2018-09-21 17:19:54 -07002919 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002920 return 0;
2921}
2922
2923/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002924 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002925 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002926 * @access : access requested
2927 *
2928 * Returns 0 if current has the requested access, error code otherwise
2929 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002930static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002931{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002932 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002933 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002934 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002935
2936#ifdef CONFIG_AUDIT
2937 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002938 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002939#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002940 rc = smk_curacc(ssp, access, &ad);
2941 rc = smk_bu_current("shm", ssp, access, rc);
2942 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002943}
2944
2945/**
Casey Schauflere114e472008-02-04 22:29:50 -08002946 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002947 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002948 * @shmflg: access requested
2949 *
2950 * Returns 0 if current has the requested access, error code otherwise
2951 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002952static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002953{
Casey Schauflere114e472008-02-04 22:29:50 -08002954 int may;
2955
2956 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002957 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002958}
2959
2960/**
2961 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002962 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002963 * @cmd: what it wants to do
2964 *
2965 * Returns 0 if current has the requested access, error code otherwise
2966 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002967static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002968{
Casey Schauflere114e472008-02-04 22:29:50 -08002969 int may;
2970
2971 switch (cmd) {
2972 case IPC_STAT:
2973 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002974 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002975 may = MAY_READ;
2976 break;
2977 case IPC_SET:
2978 case SHM_LOCK:
2979 case SHM_UNLOCK:
2980 case IPC_RMID:
2981 may = MAY_READWRITE;
2982 break;
2983 case IPC_INFO:
2984 case SHM_INFO:
2985 /*
2986 * System level information.
2987 */
2988 return 0;
2989 default:
2990 return -EINVAL;
2991 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002992 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002993}
2994
2995/**
2996 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002997 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002998 * @shmaddr: unused
2999 * @shmflg: access requested
3000 *
3001 * Returns 0 if current has the requested access, error code otherwise
3002 */
luanshia1a07f22019-07-05 10:35:20 +08003003static int smack_shm_shmat(struct kern_ipc_perm *isp, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08003004 int shmflg)
3005{
Casey Schauflere114e472008-02-04 22:29:50 -08003006 int may;
3007
3008 may = smack_flags_to_may(shmflg);
luanshia1a07f22019-07-05 10:35:20 +08003009 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003010}
3011
3012/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003013 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003014 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02003015 * @access : access requested
3016 *
3017 * Returns 0 if current has the requested access, error code otherwise
3018 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003019static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003020{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003021 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003022 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003023 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003024
3025#ifdef CONFIG_AUDIT
3026 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003027 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003028#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003029 rc = smk_curacc(ssp, access, &ad);
3030 rc = smk_bu_current("sem", ssp, access, rc);
3031 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032}
3033
3034/**
Casey Schauflere114e472008-02-04 22:29:50 -08003035 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003036 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003037 * @semflg: access requested
3038 *
3039 * Returns 0 if current has the requested access, error code otherwise
3040 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003041static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003042{
Casey Schauflere114e472008-02-04 22:29:50 -08003043 int may;
3044
3045 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003046 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003047}
3048
3049/**
3050 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003051 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003052 * @cmd: what it wants to do
3053 *
3054 * Returns 0 if current has the requested access, error code otherwise
3055 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003056static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003057{
Casey Schauflere114e472008-02-04 22:29:50 -08003058 int may;
3059
3060 switch (cmd) {
3061 case GETPID:
3062 case GETNCNT:
3063 case GETZCNT:
3064 case GETVAL:
3065 case GETALL:
3066 case IPC_STAT:
3067 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07003068 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003069 may = MAY_READ;
3070 break;
3071 case SETVAL:
3072 case SETALL:
3073 case IPC_RMID:
3074 case IPC_SET:
3075 may = MAY_READWRITE;
3076 break;
3077 case IPC_INFO:
3078 case SEM_INFO:
3079 /*
3080 * System level information
3081 */
3082 return 0;
3083 default:
3084 return -EINVAL;
3085 }
3086
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003087 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003088}
3089
3090/**
3091 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003092 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003093 * @sops: unused
3094 * @nsops: unused
3095 * @alter: unused
3096 *
3097 * Treated as read and write in all cases.
3098 *
3099 * Returns 0 if access is allowed, error code otherwise
3100 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003101static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003102 unsigned nsops, int alter)
3103{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003104 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003105}
3106
3107/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003108 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003109 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003110 * @access : access requested
3111 *
3112 * return 0 if current has access, error otherwise
3113 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003114static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003115{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003116 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003117 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003118 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003119
3120#ifdef CONFIG_AUDIT
3121 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003122 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003123#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003124 rc = smk_curacc(msp, access, &ad);
3125 rc = smk_bu_current("msq", msp, access, rc);
3126 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003127}
3128
3129/**
Casey Schauflere114e472008-02-04 22:29:50 -08003130 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003131 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003132 * @msqflg: access requested
3133 *
3134 * Returns 0 if current has the requested access, error code otherwise
3135 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003136static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003137{
Casey Schauflere114e472008-02-04 22:29:50 -08003138 int may;
3139
3140 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003141 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003142}
3143
3144/**
3145 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003146 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003147 * @cmd: what it wants to do
3148 *
3149 * Returns 0 if current has the requested access, error code otherwise
3150 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003151static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003152{
Casey Schauflere114e472008-02-04 22:29:50 -08003153 int may;
3154
3155 switch (cmd) {
3156 case IPC_STAT:
3157 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003158 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003159 may = MAY_READ;
3160 break;
3161 case IPC_SET:
3162 case IPC_RMID:
3163 may = MAY_READWRITE;
3164 break;
3165 case IPC_INFO:
3166 case MSG_INFO:
3167 /*
3168 * System level information
3169 */
3170 return 0;
3171 default:
3172 return -EINVAL;
3173 }
3174
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003175 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003176}
3177
3178/**
3179 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003180 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003181 * @msg: unused
3182 * @msqflg: access requested
3183 *
3184 * Returns 0 if current has the requested access, error code otherwise
3185 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003186static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003187 int msqflg)
3188{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003189 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003190
Etienne Bassetecfcc532009-04-08 20:40:06 +02003191 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003192 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003193}
3194
3195/**
3196 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003197 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003198 * @msg: unused
3199 * @target: unused
3200 * @type: unused
3201 * @mode: unused
3202 *
3203 * Returns 0 if current has read and write access, error code otherwise
3204 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003205static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003206 struct task_struct *target, long type, int mode)
3207{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003208 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003209}
3210
3211/**
3212 * smack_ipc_permission - Smack access for ipc_permission()
3213 * @ipp: the object permissions
3214 * @flag: access requested
3215 *
3216 * Returns 0 if current has read and write access, error code otherwise
3217 */
3218static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3219{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003220 struct smack_known **blob = smack_ipc(ipp);
3221 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003222 int may = smack_flags_to_may(flag);
3223 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003224 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003225
Etienne Bassetecfcc532009-04-08 20:40:06 +02003226#ifdef CONFIG_AUDIT
3227 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3228 ad.a.u.ipc_id = ipp->id;
3229#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003230 rc = smk_curacc(iskp, may, &ad);
3231 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003232 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003233}
3234
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003235/**
3236 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003237 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003238 * @secid: where result will be saved
3239 */
3240static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3241{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003242 struct smack_known **blob = smack_ipc(ipp);
3243 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003244
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003245 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003246}
3247
Casey Schauflere114e472008-02-04 22:29:50 -08003248/**
3249 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003250 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003251 * @inode: the object
3252 *
3253 * Set the inode's security blob if it hasn't been done already.
3254 */
3255static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3256{
3257 struct super_block *sbp;
3258 struct superblock_smack *sbsp;
3259 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003260 struct smack_known *skp;
3261 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003262 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003263 char trattr[TRANS_TRUE_SIZE];
3264 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003265 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003266 struct dentry *dp;
3267
3268 if (inode == NULL)
3269 return;
3270
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003271 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003272
3273 mutex_lock(&isp->smk_lock);
3274 /*
3275 * If the inode is already instantiated
3276 * take the quick way out
3277 */
3278 if (isp->smk_flags & SMK_INODE_INSTANT)
3279 goto unlockandout;
3280
3281 sbp = inode->i_sb;
3282 sbsp = sbp->s_security;
3283 /*
3284 * We're going to use the superblock default label
3285 * if there's no label on the file.
3286 */
3287 final = sbsp->smk_default;
3288
3289 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003290 * If this is the root inode the superblock
3291 * may be in the process of initialization.
3292 * If that is the case use the root value out
3293 * of the superblock.
3294 */
3295 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003296 switch (sbp->s_magic) {
3297 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003298 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003299 /*
3300 * The cgroup filesystem is never mounted,
3301 * so there's no opportunity to set the mount
3302 * options.
3303 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003304 sbsp->smk_root = &smack_known_star;
3305 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003306 isp->smk_inode = sbsp->smk_root;
3307 break;
3308 case TMPFS_MAGIC:
3309 /*
3310 * What about shmem/tmpfs anonymous files with dentry
3311 * obtained from d_alloc_pseudo()?
3312 */
3313 isp->smk_inode = smk_of_current();
3314 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003315 case PIPEFS_MAGIC:
3316 isp->smk_inode = smk_of_current();
3317 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003318 case SOCKFS_MAGIC:
3319 /*
3320 * Socket access is controlled by the socket
3321 * structures associated with the task involved.
3322 */
3323 isp->smk_inode = &smack_known_star;
3324 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003325 default:
3326 isp->smk_inode = sbsp->smk_root;
3327 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003328 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003329 isp->smk_flags |= SMK_INODE_INSTANT;
3330 goto unlockandout;
3331 }
3332
3333 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003334 * This is pretty hackish.
3335 * Casey says that we shouldn't have to do
3336 * file system specific code, but it does help
3337 * with keeping it simple.
3338 */
3339 switch (sbp->s_magic) {
3340 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003341 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003342 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003343 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003344 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003345 * that the smack file system doesn't do
3346 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003347 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003348 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003349 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003350 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003351 break;
3352 case DEVPTS_SUPER_MAGIC:
3353 /*
3354 * devpts seems content with the label of the task.
3355 * Programs that change smack have to treat the
3356 * pty with respect.
3357 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003358 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003359 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003360 case PROC_SUPER_MAGIC:
3361 /*
3362 * Casey says procfs appears not to care.
3363 * The superblock default suffices.
3364 */
3365 break;
3366 case TMPFS_MAGIC:
3367 /*
3368 * Device labels should come from the filesystem,
3369 * but watch out, because they're volitile,
3370 * getting recreated on every reboot.
3371 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003372 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003373 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003374 * If a smack value has been set we want to use it,
3375 * but since tmpfs isn't giving us the opportunity
3376 * to set mount options simulate setting the
3377 * superblock default.
3378 */
Gustavo A. R. Silva09186e52019-02-08 14:54:53 -06003379 /* Fall through */
Casey Schauflere114e472008-02-04 22:29:50 -08003380 default:
3381 /*
3382 * This isn't an understood special case.
3383 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003384 */
3385
3386 /*
3387 * UNIX domain sockets use lower level socket data.
3388 */
3389 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003390 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003391 break;
3392 }
3393 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003394 * No xattr support means, alas, no SMACK label.
3395 * Use the aforeapplied default.
3396 * It would be curious if the label of the task
3397 * does not match that assigned.
3398 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003399 if (!(inode->i_opflags & IOP_XATTR))
3400 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003401 /*
3402 * Get the dentry for xattr.
3403 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003404 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003405 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003406 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003407 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003408
3409 /*
3410 * Transmuting directory
3411 */
3412 if (S_ISDIR(inode->i_mode)) {
3413 /*
3414 * If this is a new directory and the label was
3415 * transmuted when the inode was initialized
3416 * set the transmute attribute on the directory
3417 * and mark the inode.
3418 *
3419 * If there is a transmute attribute on the
3420 * directory mark the inode.
3421 */
3422 if (isp->smk_flags & SMK_INODE_CHANGED) {
3423 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003424 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003425 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003426 TRANS_TRUE, TRANS_TRUE_SIZE,
3427 0);
3428 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003429 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003430 XATTR_NAME_SMACKTRANSMUTE, trattr,
3431 TRANS_TRUE_SIZE);
3432 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3433 TRANS_TRUE_SIZE) != 0)
3434 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003435 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003436 if (rc >= 0)
3437 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003438 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003439 /*
3440 * Don't let the exec or mmap label be "*" or "@".
3441 */
3442 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3443 if (IS_ERR(skp) || skp == &smack_known_star ||
3444 skp == &smack_known_web)
3445 skp = NULL;
3446 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003447
Casey Schaufler19760ad2013-12-16 16:27:26 -08003448 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003449 if (IS_ERR(skp) || skp == &smack_known_star ||
3450 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003451 skp = NULL;
3452 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003453
Casey Schauflere114e472008-02-04 22:29:50 -08003454 dput(dp);
3455 break;
3456 }
3457
3458 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003459 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003460 else
3461 isp->smk_inode = final;
3462
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003463 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003464
3465unlockandout:
3466 mutex_unlock(&isp->smk_lock);
3467 return;
3468}
3469
3470/**
3471 * smack_getprocattr - Smack process attribute access
3472 * @p: the object task
3473 * @name: the name of the attribute in /proc/.../attr
3474 * @value: where to put the result
3475 *
3476 * Places a copy of the task Smack into value
3477 *
3478 * Returns the length of the smack label or an error code
3479 */
3480static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3481{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003482 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003483 char *cp;
3484 int slen;
3485
3486 if (strcmp(name, "current") != 0)
3487 return -EINVAL;
3488
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003489 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003490 if (cp == NULL)
3491 return -ENOMEM;
3492
3493 slen = strlen(cp);
3494 *value = cp;
3495 return slen;
3496}
3497
3498/**
3499 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003500 * @name: the name of the attribute in /proc/.../attr
3501 * @value: the value to set
3502 * @size: the size of the value
3503 *
3504 * Sets the Smack value of the task. Only setting self
3505 * is permitted and only with privilege
3506 *
3507 * Returns the length of the smack label or an error code
3508 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003509static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003510{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003511 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003512 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003513 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003514 struct smack_known_list_elem *sklep;
3515 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003516
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003517 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003518 return -EPERM;
3519
Casey Schauflerf7112e62012-05-06 15:22:02 -07003520 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003521 return -EINVAL;
3522
3523 if (strcmp(name, "current") != 0)
3524 return -EINVAL;
3525
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003526 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003527 if (IS_ERR(skp))
3528 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003529
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003530 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303531 * No process is ever allowed the web ("@") label
3532 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003533 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303534 if (skp == &smack_known_web || skp == &smack_known_star)
3535 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003536
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003537 if (!smack_privileged(CAP_MAC_ADMIN)) {
3538 rc = -EPERM;
3539 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3540 if (sklep->smk_label == skp) {
3541 rc = 0;
3542 break;
3543 }
3544 if (rc)
3545 return rc;
3546 }
3547
David Howellsd84f4f92008-11-14 10:39:23 +11003548 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003549 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003550 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003551
Casey Schauflerb17103a2018-11-09 16:12:56 -08003552 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003553 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003554 /*
3555 * process can change its label only once
3556 */
3557 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003558
David Howellsd84f4f92008-11-14 10:39:23 +11003559 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003560 return size;
3561}
3562
3563/**
3564 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003565 * @sock: one sock
3566 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003567 * @newsk: unused
3568 *
3569 * Return 0 if a subject with the smack of sock could access
3570 * an object with the smack of other, otherwise an error code
3571 */
David S. Miller3610cda2011-01-05 15:38:53 -08003572static int smack_unix_stream_connect(struct sock *sock,
3573 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003574{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003575 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003576 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003577 struct socket_smack *ssp = sock->sk_security;
3578 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003579 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003580 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003581 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003582#ifdef CONFIG_AUDIT
3583 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003584#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003585
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003586 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3587 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003588 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003589#ifdef CONFIG_AUDIT
3590 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3591 smk_ad_setfield_u_net_sk(&ad, other);
3592#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003593 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3594 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003595 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003596 okp = osp->smk_out;
3597 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003598 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003599 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003600 MAY_WRITE, rc);
3601 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003602 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003603
Casey Schaufler975d5e52011-09-26 14:43:39 -07003604 /*
3605 * Cross reference the peer labels for SO_PEERSEC.
3606 */
3607 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003608 nsp->smk_packet = ssp->smk_out;
3609 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003610 }
3611
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003612 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003613}
3614
3615/**
3616 * smack_unix_may_send - Smack access on UDS
3617 * @sock: one socket
3618 * @other: the other socket
3619 *
3620 * Return 0 if a subject with the smack of sock could access
3621 * an object with the smack of other, otherwise an error code
3622 */
3623static int smack_unix_may_send(struct socket *sock, struct socket *other)
3624{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003625 struct socket_smack *ssp = sock->sk->sk_security;
3626 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003627 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003628 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003629
Kees Cook923e9a12012-04-10 13:26:44 -07003630#ifdef CONFIG_AUDIT
3631 struct lsm_network_audit net;
3632
Eric Paris48c62af2012-04-02 13:15:44 -04003633 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003634 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003635#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003636
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003637 if (smack_privileged(CAP_MAC_OVERRIDE))
3638 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003639
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003640 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3641 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003642 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003643}
3644
3645/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003646 * smack_socket_sendmsg - Smack check based on destination host
3647 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003648 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003649 * @size: the size of the message
3650 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003651 * Return 0 if the current subject can write to the destination host.
3652 * For IPv4 this is only a question if the destination is a single label host.
3653 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003654 */
3655static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3656 int size)
3657{
3658 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003659#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003660 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003661#endif
3662#ifdef SMACK_IPV6_SECMARK_LABELING
3663 struct socket_smack *ssp = sock->sk->sk_security;
3664 struct smack_known *rsp;
3665#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003666 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003667
3668 /*
3669 * Perfectly reasonable for this to be NULL
3670 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003671 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003672 return 0;
3673
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003674 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003675 case AF_INET:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003676 if (msg->msg_namelen < sizeof(struct sockaddr_in) ||
3677 sip->sin_family != AF_INET)
3678 return -EINVAL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003679 rc = smack_netlabel_send(sock->sk, sip);
3680 break;
Casey Schaufler619ae032019-04-30 14:13:32 -07003681#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003682 case AF_INET6:
Tetsuo Handab9ef5512019-04-12 19:59:35 +09003683 if (msg->msg_namelen < SIN6_LEN_RFC2133 ||
3684 sap->sin6_family != AF_INET6)
3685 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003686#ifdef SMACK_IPV6_SECMARK_LABELING
3687 rsp = smack_ipv6host_label(sap);
3688 if (rsp != NULL)
3689 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3690 SMK_CONNECTING);
3691#endif
3692#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003693 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003694#endif
Casey Schaufler619ae032019-04-30 14:13:32 -07003695#endif /* IS_ENABLED(CONFIG_IPV6) */
Casey Schauflerc6739442013-05-22 18:42:56 -07003696 break;
3697 }
3698 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003699}
3700
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003701/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003702 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003703 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003704 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003705 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003706 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003707 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003708static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3709 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003710{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003711 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003712 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003713 int acat;
3714 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003715
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003716 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003717 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003718 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003719 * If there are flags but no level netlabel isn't
3720 * behaving the way we expect it to.
3721 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003722 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003723 * Without guidance regarding the smack value
3724 * for the packet fall back on the network
3725 * ambient value.
3726 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003727 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303728 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003729 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003730 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003731 /*
3732 * Compare the catsets. Use the netlbl APIs.
3733 */
3734 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3735 if ((skp->smk_netlabel.flags &
3736 NETLBL_SECATTR_MLS_CAT) == 0)
3737 found = 1;
3738 break;
3739 }
3740 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003741 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3742 acat + 1);
3743 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003744 skp->smk_netlabel.attr.mls.cat,
3745 kcat + 1);
3746 if (acat < 0 || kcat < 0)
3747 break;
3748 }
3749 if (acat == kcat) {
3750 found = 1;
3751 break;
3752 }
Casey Schauflere114e472008-02-04 22:29:50 -08003753 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003754 rcu_read_unlock();
3755
3756 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003757 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003758
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003759 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003760 return &smack_known_web;
3761 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003762 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003763 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003764 /*
3765 * Looks like a fallback, which gives us a secid.
3766 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003767 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003768 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003769 * Without guidance regarding the smack value
3770 * for the packet fall back on the network
3771 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003772 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003773 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003774}
3775
Casey Schaufler69f287a2014-12-12 17:08:40 -08003776#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003777static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003778{
Casey Schauflerc6739442013-05-22 18:42:56 -07003779 u8 nexthdr;
3780 int offset;
3781 int proto = -EINVAL;
3782 struct ipv6hdr _ipv6h;
3783 struct ipv6hdr *ip6;
3784 __be16 frag_off;
3785 struct tcphdr _tcph, *th;
3786 struct udphdr _udph, *uh;
3787 struct dccp_hdr _dccph, *dh;
3788
3789 sip->sin6_port = 0;
3790
3791 offset = skb_network_offset(skb);
3792 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3793 if (ip6 == NULL)
3794 return -EINVAL;
3795 sip->sin6_addr = ip6->saddr;
3796
3797 nexthdr = ip6->nexthdr;
3798 offset += sizeof(_ipv6h);
3799 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3800 if (offset < 0)
3801 return -EINVAL;
3802
3803 proto = nexthdr;
3804 switch (proto) {
3805 case IPPROTO_TCP:
3806 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3807 if (th != NULL)
3808 sip->sin6_port = th->source;
3809 break;
3810 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003811 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003812 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3813 if (uh != NULL)
3814 sip->sin6_port = uh->source;
3815 break;
3816 case IPPROTO_DCCP:
3817 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3818 if (dh != NULL)
3819 sip->sin6_port = dh->dccph_sport;
3820 break;
3821 }
3822 return proto;
3823}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003824#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003825
Casey Schauflere114e472008-02-04 22:29:50 -08003826/**
3827 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3828 * @sk: socket
3829 * @skb: packet
3830 *
3831 * Returns 0 if the packet should be delivered, an error code otherwise
3832 */
3833static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3834{
3835 struct netlbl_lsm_secattr secattr;
3836 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003837 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003838 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003839 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003840 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003841#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003842 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003843#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003844#if IS_ENABLED(CONFIG_IPV6)
3845 struct sockaddr_in6 sadd;
3846 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003847
3848 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3849 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003850#endif /* CONFIG_IPV6 */
3851
Piotr Sawicki129a9982018-07-19 11:42:58 +02003852 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003853 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003854#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3855 /*
3856 * If there is a secmark use it rather than the CIPSO label.
3857 * If there is no secmark fall back to CIPSO.
3858 * The secmark is assumed to reflect policy better.
3859 */
3860 if (skb && skb->secmark != 0) {
3861 skp = smack_from_secid(skb->secmark);
3862 goto access_check;
3863 }
3864#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003865 /*
3866 * Translate what netlabel gave us.
3867 */
3868 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003869
Piotr Sawicki129a9982018-07-19 11:42:58 +02003870 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003871 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003872 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003873 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003874 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003875
Casey Schauflerc6739442013-05-22 18:42:56 -07003876 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003877
Casey Schaufler69f287a2014-12-12 17:08:40 -08003878#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3879access_check:
3880#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003881#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003882 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003883 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003884 ad.a.u.net->netif = skb->skb_iif;
3885 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003886#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003887 /*
3888 * Receiving a packet requires that the other end
3889 * be able to write here. Read access is not required.
3890 * This is the simplist possible security model
3891 * for networking.
3892 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003893 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3894 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003895 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003896 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003897 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003898 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003899#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003900 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003901 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003902 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3903 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003904 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003905#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003906 if (skb && skb->secmark != 0)
3907 skp = smack_from_secid(skb->secmark);
Casey Schauflerf7450bc2019-04-03 14:28:38 -07003908 else if (smk_ipv6_localhost(&sadd))
3909 break;
Casey Schauflerc6739442013-05-22 18:42:56 -07003910 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003911 skp = smack_ipv6host_label(&sadd);
3912 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003913 skp = smack_net_ambient;
Jia-Ju Bai3f4287e2019-07-23 18:00:15 +08003914 if (skb == NULL)
3915 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916#ifdef CONFIG_AUDIT
3917 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003918 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003919 ad.a.u.net->netif = skb->skb_iif;
3920 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3921#endif /* CONFIG_AUDIT */
3922 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3923 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3924 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003925#endif /* SMACK_IPV6_SECMARK_LABELING */
3926#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003927 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003928#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003929 if (rc != 0)
3930 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3931 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003932 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003933#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003934 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003935
Paul Moorea8134292008-10-10 10:16:31 -04003936 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003937}
3938
3939/**
3940 * smack_socket_getpeersec_stream - pull in packet label
3941 * @sock: the socket
3942 * @optval: user's destination
3943 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003944 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003945 *
3946 * returns zero on success, an error code otherwise
3947 */
3948static int smack_socket_getpeersec_stream(struct socket *sock,
3949 char __user *optval,
3950 int __user *optlen, unsigned len)
3951{
3952 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003953 char *rcp = "";
3954 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003955 int rc = 0;
3956
3957 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003958 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003959 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003960 slen = strlen(rcp) + 1;
3961 }
Casey Schauflere114e472008-02-04 22:29:50 -08003962
3963 if (slen > len)
3964 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003965 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003966 rc = -EFAULT;
3967
3968 if (put_user(slen, optlen) != 0)
3969 rc = -EFAULT;
3970
3971 return rc;
3972}
3973
3974
3975/**
3976 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003977 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003978 * @skb: packet data
3979 * @secid: pointer to where to put the secid of the packet
3980 *
3981 * Sets the netlabel socket state on sk from parent
3982 */
3983static int smack_socket_getpeersec_dgram(struct socket *sock,
3984 struct sk_buff *skb, u32 *secid)
3985
3986{
3987 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003988 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003989 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003990 int family = PF_UNSPEC;
3991 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003992 int rc;
3993
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003994 if (skb != NULL) {
3995 if (skb->protocol == htons(ETH_P_IP))
3996 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003997#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003998 else if (skb->protocol == htons(ETH_P_IPV6))
3999 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004000#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004001 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004002 if (family == PF_UNSPEC && sock != NULL)
4003 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08004004
Casey Schaufler69f287a2014-12-12 17:08:40 -08004005 switch (family) {
4006 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004007 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004008 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004009 break;
4010 case PF_INET:
4011#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4012 s = skb->secmark;
4013 if (s != 0)
4014 break;
4015#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004016 /*
4017 * Translate what netlabel gave us.
4018 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004019 if (sock != NULL && sock->sk != NULL)
4020 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004021 netlbl_secattr_init(&secattr);
4022 rc = netlbl_skbuff_getattr(skb, family, &secattr);
4023 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004024 skp = smack_from_secattr(&secattr, ssp);
4025 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004026 }
4027 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08004028 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08004029 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004030#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08004031 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004032#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08004033 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08004034 }
4035 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08004036 if (s == 0)
4037 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08004038 return 0;
4039}
4040
4041/**
Paul Moore07feee82009-03-27 17:10:54 -04004042 * smack_sock_graft - Initialize a newly created socket with an existing sock
4043 * @sk: child sock
4044 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08004045 *
Paul Moore07feee82009-03-27 17:10:54 -04004046 * Set the smk_{in,out} state of an existing sock based on the process that
4047 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08004048 */
4049static void smack_sock_graft(struct sock *sk, struct socket *parent)
4050{
4051 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004052 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08004053
Paul Moore07feee82009-03-27 17:10:54 -04004054 if (sk == NULL ||
4055 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08004056 return;
4057
4058 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004059 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004060 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04004061 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08004062}
4063
4064/**
4065 * smack_inet_conn_request - Smack access check on connect
4066 * @sk: socket involved
4067 * @skb: packet
4068 * @req: unused
4069 *
4070 * Returns 0 if a task with the packet label could write to
4071 * the socket, otherwise an error code
4072 */
4073static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4074 struct request_sock *req)
4075{
Paul Moore07feee82009-03-27 17:10:54 -04004076 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07004077 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004078 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04004079 struct netlbl_lsm_secattr secattr;
4080 struct sockaddr_in addr;
4081 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004082 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08004083 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004084 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07004085#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004086 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07004087#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004088
Casey Schaufler69f287a2014-12-12 17:08:40 -08004089#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004090 if (family == PF_INET6) {
4091 /*
4092 * Handle mapped IPv4 packets arriving
4093 * via IPv6 sockets. Don't set up netlabel
4094 * processing on IPv6.
4095 */
4096 if (skb->protocol == htons(ETH_P_IP))
4097 family = PF_INET;
4098 else
4099 return 0;
4100 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004101#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004102
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004103#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4104 /*
4105 * If there is a secmark use it rather than the CIPSO label.
4106 * If there is no secmark fall back to CIPSO.
4107 * The secmark is assumed to reflect policy better.
4108 */
4109 if (skb && skb->secmark != 0) {
4110 skp = smack_from_secid(skb->secmark);
4111 goto access_check;
4112 }
4113#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4114
Paul Moore07feee82009-03-27 17:10:54 -04004115 netlbl_secattr_init(&secattr);
4116 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004117 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004118 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004119 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004120 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004121 netlbl_secattr_destroy(&secattr);
4122
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004123#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4124access_check:
4125#endif
4126
Etienne Bassetecfcc532009-04-08 20:40:06 +02004127#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004128 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4129 ad.a.u.net->family = family;
4130 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004131 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4132#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004133 /*
Paul Moore07feee82009-03-27 17:10:54 -04004134 * Receiving a packet requires that the other end be able to write
4135 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004136 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004137 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4138 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004139 if (rc != 0)
4140 return rc;
4141
4142 /*
4143 * Save the peer's label in the request_sock so we can later setup
4144 * smk_packet in the child socket so that SO_PEERCRED can report it.
4145 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004146 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004147
4148 /*
4149 * We need to decide if we want to label the incoming connection here
4150 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004151 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004152 */
4153 hdr = ip_hdr(skb);
4154 addr.sin_addr.s_addr = hdr->saddr;
4155 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004156 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004157 rcu_read_unlock();
4158
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004159 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004160 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004161 else
Paul Moore07feee82009-03-27 17:10:54 -04004162 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004163
4164 return rc;
4165}
4166
Paul Moore07feee82009-03-27 17:10:54 -04004167/**
4168 * smack_inet_csk_clone - Copy the connection information to the new socket
4169 * @sk: the new socket
4170 * @req: the connection's request_sock
4171 *
4172 * Transfer the connection's peer label to the newly created socket.
4173 */
4174static void smack_inet_csk_clone(struct sock *sk,
4175 const struct request_sock *req)
4176{
4177 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004178 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004179
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004180 if (req->peer_secid != 0) {
4181 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004182 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004183 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004184 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004185}
4186
Casey Schauflere114e472008-02-04 22:29:50 -08004187/*
4188 * Key management security hooks
4189 *
4190 * Casey has not tested key support very heavily.
4191 * The permission check is most likely too restrictive.
4192 * If you care about keys please have a look.
4193 */
4194#ifdef CONFIG_KEYS
4195
4196/**
4197 * smack_key_alloc - Set the key security blob
4198 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004199 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004200 * @flags: unused
4201 *
4202 * No allocation required
4203 *
4204 * Returns 0
4205 */
David Howellsd84f4f92008-11-14 10:39:23 +11004206static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004207 unsigned long flags)
4208{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004209 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004210
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004211 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004212 return 0;
4213}
4214
4215/**
4216 * smack_key_free - Clear the key security blob
4217 * @key: the object
4218 *
4219 * Clear the blob pointer
4220 */
4221static void smack_key_free(struct key *key)
4222{
4223 key->security = NULL;
4224}
4225
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004226/**
Casey Schauflere114e472008-02-04 22:29:50 -08004227 * smack_key_permission - Smack access on a key
4228 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004229 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004230 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004231 *
4232 * Return 0 if the task has read and write to the object,
4233 * an error code otherwise
4234 */
4235static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004236 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004237{
4238 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004239 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004240 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004241 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004242 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004243
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004244 /*
4245 * Validate requested permissions
4246 */
4247 if (perm & ~KEY_NEED_ALL)
4248 return -EINVAL;
4249
Casey Schauflere114e472008-02-04 22:29:50 -08004250 keyp = key_ref_to_ptr(key_ref);
4251 if (keyp == NULL)
4252 return -EINVAL;
4253 /*
4254 * If the key hasn't been initialized give it access so that
4255 * it may do so.
4256 */
4257 if (keyp->security == NULL)
4258 return 0;
4259 /*
4260 * This should not occur
4261 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004262 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004263 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004264
4265 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4266 return 0;
4267
Etienne Bassetecfcc532009-04-08 20:40:06 +02004268#ifdef CONFIG_AUDIT
4269 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4270 ad.a.u.key_struct.key = keyp->serial;
4271 ad.a.u.key_struct.key_desc = keyp->description;
4272#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004273 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4274 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004275 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004276 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004277 rc = smk_access(tkp, keyp->security, request, &ad);
4278 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4279 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004280}
José Bollo7fc5f362015-02-17 15:41:22 +01004281
4282/*
4283 * smack_key_getsecurity - Smack label tagging the key
4284 * @key points to the key to be queried
4285 * @_buffer points to a pointer that should be set to point to the
4286 * resulting string (if no label or an error occurs).
4287 * Return the length of the string (including terminating NUL) or -ve if
4288 * an error.
4289 * May also return 0 (and a NULL buffer pointer) if there is no label.
4290 */
4291static int smack_key_getsecurity(struct key *key, char **_buffer)
4292{
4293 struct smack_known *skp = key->security;
4294 size_t length;
4295 char *copy;
4296
4297 if (key->security == NULL) {
4298 *_buffer = NULL;
4299 return 0;
4300 }
4301
4302 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4303 if (copy == NULL)
4304 return -ENOMEM;
4305 length = strlen(copy) + 1;
4306
4307 *_buffer = copy;
4308 return length;
4309}
4310
Casey Schauflere114e472008-02-04 22:29:50 -08004311#endif /* CONFIG_KEYS */
4312
4313/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004314 * Smack Audit hooks
4315 *
4316 * Audit requires a unique representation of each Smack specific
4317 * rule. This unique representation is used to distinguish the
4318 * object to be audited from remaining kernel objects and also
4319 * works as a glue between the audit hooks.
4320 *
4321 * Since repository entries are added but never deleted, we'll use
4322 * the smack_known label address related to the given audit rule as
4323 * the needed unique representation. This also better fits the smack
4324 * model where nearly everything is a label.
4325 */
4326#ifdef CONFIG_AUDIT
4327
4328/**
4329 * smack_audit_rule_init - Initialize a smack audit rule
4330 * @field: audit rule fields given from user-space (audit.h)
4331 * @op: required testing operator (=, !=, >, <, ...)
4332 * @rulestr: smack label to be audited
4333 * @vrule: pointer to save our own audit rule representation
4334 *
4335 * Prepare to audit cases where (@field @op @rulestr) is true.
4336 * The label to be audited is created if necessay.
4337 */
4338static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4339{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004340 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004341 char **rule = (char **)vrule;
4342 *rule = NULL;
4343
4344 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4345 return -EINVAL;
4346
Al Viro5af75d82008-12-16 05:59:26 -05004347 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004348 return -EINVAL;
4349
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004350 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004351 if (IS_ERR(skp))
4352 return PTR_ERR(skp);
4353
4354 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004355
4356 return 0;
4357}
4358
4359/**
4360 * smack_audit_rule_known - Distinguish Smack audit rules
4361 * @krule: rule of interest, in Audit kernel representation format
4362 *
4363 * This is used to filter Smack rules from remaining Audit ones.
4364 * If it's proved that this rule belongs to us, the
4365 * audit_rule_match hook will be called to do the final judgement.
4366 */
4367static int smack_audit_rule_known(struct audit_krule *krule)
4368{
4369 struct audit_field *f;
4370 int i;
4371
4372 for (i = 0; i < krule->field_count; i++) {
4373 f = &krule->fields[i];
4374
4375 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4376 return 1;
4377 }
4378
4379 return 0;
4380}
4381
4382/**
4383 * smack_audit_rule_match - Audit given object ?
4384 * @secid: security id for identifying the object to test
4385 * @field: audit rule flags given from user-space
4386 * @op: required testing operator
4387 * @vrule: smack internal rule presentation
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004388 *
4389 * The core Audit hook. It's used to take the decision of
4390 * whether to audit or not to audit a given object.
4391 */
Richard Guy Briggs90462a52019-01-31 11:52:11 -05004392static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004393{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004394 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004395 char *rule = vrule;
4396
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004397 if (unlikely(!rule)) {
4398 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004399 return -ENOENT;
4400 }
4401
4402 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4403 return 0;
4404
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004405 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004406
4407 /*
4408 * No need to do string comparisons. If a match occurs,
4409 * both pointers will point to the same smack_known
4410 * label.
4411 */
Al Viro5af75d82008-12-16 05:59:26 -05004412 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004413 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004414 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004415 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004416
4417 return 0;
4418}
4419
Casey Schaufler491a0b02016-01-26 15:08:35 -08004420/*
4421 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004422 * No memory was allocated.
4423 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004424
4425#endif /* CONFIG_AUDIT */
4426
Randy Dunlap251a2a92009-02-18 11:42:33 -08004427/**
David Quigley746df9b2013-05-22 12:50:35 -04004428 * smack_ismaclabel - check if xattr @name references a smack MAC label
4429 * @name: Full xattr name to check.
4430 */
4431static int smack_ismaclabel(const char *name)
4432{
4433 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4434}
4435
4436
4437/**
Casey Schauflere114e472008-02-04 22:29:50 -08004438 * smack_secid_to_secctx - return the smack label for a secid
4439 * @secid: incoming integer
4440 * @secdata: destination
4441 * @seclen: how long it is
4442 *
4443 * Exists for networking code.
4444 */
4445static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4446{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004447 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004448
Eric Parisd5630b92010-10-13 16:24:48 -04004449 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004450 *secdata = skp->smk_known;
4451 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004452 return 0;
4453}
4454
Randy Dunlap251a2a92009-02-18 11:42:33 -08004455/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004456 * smack_secctx_to_secid - return the secid for a smack label
4457 * @secdata: smack label
4458 * @seclen: how long result is
4459 * @secid: outgoing integer
4460 *
4461 * Exists for audit and networking code.
4462 */
David Howellse52c17642008-04-29 20:52:51 +01004463static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004464{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004465 struct smack_known *skp = smk_find_entry(secdata);
4466
4467 if (skp)
4468 *secid = skp->smk_secid;
4469 else
4470 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004471 return 0;
4472}
4473
Casey Schaufler491a0b02016-01-26 15:08:35 -08004474/*
4475 * There used to be a smack_release_secctx hook
4476 * that did nothing back when hooks were in a vector.
4477 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004478 */
Casey Schauflere114e472008-02-04 22:29:50 -08004479
David P. Quigley1ee65e32009-09-03 14:25:57 -04004480static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4481{
4482 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4483}
4484
4485static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4486{
4487 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4488}
4489
4490static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4491{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004492 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004493
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004494 *ctx = skp->smk_known;
4495 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004496 return 0;
4497}
4498
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004499static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4500{
4501
4502 struct task_smack *tsp;
4503 struct smack_known *skp;
4504 struct inode_smack *isp;
4505 struct cred *new_creds = *new;
4506
4507 if (new_creds == NULL) {
4508 new_creds = prepare_creds();
4509 if (new_creds == NULL)
4510 return -ENOMEM;
4511 }
4512
Casey Schauflerb17103a2018-11-09 16:12:56 -08004513 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004514
4515 /*
4516 * Get label from overlay inode and set it in create_sid
4517 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004518 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004519 skp = isp->smk_inode;
4520 tsp->smk_task = skp;
4521 *new = new_creds;
4522 return 0;
4523}
4524
4525static int smack_inode_copy_up_xattr(const char *name)
4526{
4527 /*
4528 * Return 1 if this is the smack access Smack attribute.
4529 */
4530 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4531 return 1;
4532
4533 return -EOPNOTSUPP;
4534}
4535
4536static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4537 struct qstr *name,
4538 const struct cred *old,
4539 struct cred *new)
4540{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004541 struct task_smack *otsp = smack_cred(old);
4542 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004543 struct inode_smack *isp;
4544 int may;
4545
4546 /*
4547 * Use the process credential unless all of
4548 * the transmuting criteria are met
4549 */
4550 ntsp->smk_task = otsp->smk_task;
4551
4552 /*
4553 * the attribute of the containing directory
4554 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004555 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004556
4557 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4558 rcu_read_lock();
4559 may = smk_access_entry(otsp->smk_task->smk_known,
4560 isp->smk_inode->smk_known,
4561 &otsp->smk_task->smk_rules);
4562 rcu_read_unlock();
4563
4564 /*
4565 * If the directory is transmuting and the rule
4566 * providing access is transmuting use the containing
4567 * directory label instead of the process label.
4568 */
4569 if (may > 0 && (may & MAY_TRANSMUTE))
4570 ntsp->smk_task = isp->smk_inode;
4571 }
4572 return 0;
4573}
4574
Casey Schauflerbbd36622018-11-12 09:30:56 -08004575struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4576 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004577 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004578 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004579 .lbs_ipc = sizeof(struct smack_known *),
4580 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004581};
4582
James Morrisca97d932017-02-15 00:18:51 +11004583static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004584 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4585 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4586 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004587
Al Viro0b520752018-12-23 16:02:47 -05004588 LSM_HOOK_INIT(fs_context_dup, smack_fs_context_dup),
David Howells2febd252018-11-01 23:07:24 +00004589 LSM_HOOK_INIT(fs_context_parse_param, smack_fs_context_parse_param),
4590
Casey Schauflere20b0432015-05-02 15:11:36 -07004591 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4592 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004593 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004594 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004595 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304596 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004597
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05004598 LSM_HOOK_INIT(bprm_creds_for_exec, smack_bprm_creds_for_exec),
Casey Schaufler676dac42010-12-02 06:43:39 -08004599
Casey Schauflere20b0432015-05-02 15:11:36 -07004600 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004601 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4602 LSM_HOOK_INIT(inode_link, smack_inode_link),
4603 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4604 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4605 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4606 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4607 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4608 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4609 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4610 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4611 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4612 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4613 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4614 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4615 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4616 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004617
Casey Schauflere20b0432015-05-02 15:11:36 -07004618 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004619 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4620 LSM_HOOK_INIT(file_lock, smack_file_lock),
4621 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4622 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4623 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4624 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4625 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4626 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004627
Casey Schauflere20b0432015-05-02 15:11:36 -07004628 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004629
Casey Schauflere20b0432015-05-02 15:11:36 -07004630 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4631 LSM_HOOK_INIT(cred_free, smack_cred_free),
4632 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4633 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004634 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004635 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4636 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4637 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4638 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4639 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4640 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4641 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4642 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4643 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4644 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4645 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4646 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4647 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004648 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004649
Casey Schauflere20b0432015-05-02 15:11:36 -07004650 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4651 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004652
Casey Schauflere20b0432015-05-02 15:11:36 -07004653 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004654
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004655 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004656 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4657 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4658 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4659 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004660
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004661 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004662 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4663 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4664 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004665
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004666 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004667 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4668 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4669 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004670
Casey Schauflere20b0432015-05-02 15:11:36 -07004671 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004672
Casey Schauflere20b0432015-05-02 15:11:36 -07004673 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4674 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004675
Casey Schauflere20b0432015-05-02 15:11:36 -07004676 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4677 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004678
Casey Schauflere20b0432015-05-02 15:11:36 -07004679 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004680 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004681#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004682 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004683#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004684 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4685 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4686 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4687 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4688 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4689 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4690 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4691 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4692 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4693 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004694
Casey Schauflere114e472008-02-04 22:29:50 -08004695 /* key management security hooks */
4696#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004697 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4698 LSM_HOOK_INIT(key_free, smack_key_free),
4699 LSM_HOOK_INIT(key_permission, smack_key_permission),
4700 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004701#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004702
4703 /* Audit hooks */
4704#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004705 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4706 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4707 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004708#endif /* CONFIG_AUDIT */
4709
Casey Schauflere20b0432015-05-02 15:11:36 -07004710 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4711 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4712 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004713 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4714 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4715 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004716 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4717 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4718 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004719};
4720
Etienne Basset7198e2e2009-03-24 20:53:24 +01004721
Casey Schaufler86812bb2012-04-17 18:55:46 -07004722static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004723{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004724 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004725 * Initialize rule list locks
4726 */
4727 mutex_init(&smack_known_huh.smk_rules_lock);
4728 mutex_init(&smack_known_hat.smk_rules_lock);
4729 mutex_init(&smack_known_floor.smk_rules_lock);
4730 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004731 mutex_init(&smack_known_web.smk_rules_lock);
4732 /*
4733 * Initialize rule lists
4734 */
4735 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4736 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4737 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4738 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004739 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4740 /*
4741 * Create the known labels list
4742 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004743 smk_insert_entry(&smack_known_huh);
4744 smk_insert_entry(&smack_known_hat);
4745 smk_insert_entry(&smack_known_star);
4746 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004747 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004748}
4749
Casey Schauflere114e472008-02-04 22:29:50 -08004750/**
4751 * smack_init - initialize the smack system
4752 *
luanshia1a07f22019-07-05 10:35:20 +08004753 * Returns 0 on success, -ENOMEM is there's no memory
Casey Schauflere114e472008-02-04 22:29:50 -08004754 */
4755static __init int smack_init(void)
4756{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004757 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004758 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004759
Rohit1a5b4722014-10-15 17:40:41 +05304760 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4761 if (!smack_inode_cache)
4762 return -ENOMEM;
4763
Casey Schaufler4e328b02019-04-02 11:37:12 -07004764 smack_rule_cache = KMEM_CACHE(smack_rule, 0);
4765 if (!smack_rule_cache) {
4766 kmem_cache_destroy(smack_inode_cache);
4767 return -ENOMEM;
4768 }
4769
Casey Schauflerbbd36622018-11-12 09:30:56 -08004770 /*
4771 * Set the security state for the initial task.
4772 */
4773 tsp = smack_cred(cred);
4774 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4775
4776 /*
4777 * Register with LSM
4778 */
4779 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004780 smack_enabled = 1;
4781
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004782 pr_info("Smack: Initializing.\n");
4783#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4784 pr_info("Smack: Netfilter enabled.\n");
4785#endif
4786#ifdef SMACK_IPV6_PORT_LABELING
4787 pr_info("Smack: IPv6 port labeling enabled.\n");
4788#endif
4789#ifdef SMACK_IPV6_SECMARK_LABELING
4790 pr_info("Smack: IPv6 Netfilter enabled.\n");
4791#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004792
Casey Schaufler86812bb2012-04-17 18:55:46 -07004793 /* initialize the smack_known_list */
4794 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004795
Casey Schauflere114e472008-02-04 22:29:50 -08004796 return 0;
4797}
4798
4799/*
4800 * Smack requires early initialization in order to label
4801 * all processes and objects when they are created.
4802 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004803DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004804 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004805 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004806 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004807 .init = smack_init,
4808};