blob: 0b848b1f6366ea53631dbf104ea71bd7841b4971 [file] [log] [blame]
Casey Schauflere114e472008-02-04 22:29:50 -08001/*
2 * Simplified MAC Kernel (smack) security module
3 *
4 * This file contains the smack hook function implementations.
5 *
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02006 * Authors:
Casey Schauflere114e472008-02-04 22:29:50 -08007 * Casey Schaufler <casey@schaufler-ca.com>
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +03008 * Jarkko Sakkinen <jarkko.sakkinen@intel.com>
Casey Schauflere114e472008-02-04 22:29:50 -08009 *
10 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
Paul Moore07feee82009-03-27 17:10:54 -040011 * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000012 * Paul Moore <paul@paul-moore.com>
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020013 * Copyright (C) 2010 Nokia Corporation
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +030014 * Copyright (C) 2011 Intel Corporation.
Casey Schauflere114e472008-02-04 22:29:50 -080015 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
19 */
20
21#include <linux/xattr.h>
22#include <linux/pagemap.h>
23#include <linux/mount.h>
24#include <linux/stat.h>
Casey Schauflere114e472008-02-04 22:29:50 -080025#include <linux/kd.h>
26#include <asm/ioctls.h>
Paul Moore07feee82009-03-27 17:10:54 -040027#include <linux/ip.h>
Casey Schauflere114e472008-02-04 22:29:50 -080028#include <linux/tcp.h>
29#include <linux/udp.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070030#include <linux/dccp.h>
Piotr Sawickid66a8ac2018-07-19 11:47:31 +020031#include <linux/icmpv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Casey Schauflere114e472008-02-04 22:29:50 -080033#include <linux/mutex.h>
34#include <linux/pipe_fs_i.h>
Casey Schauflere114e472008-02-04 22:29:50 -080035#include <net/cipso_ipv4.h>
Casey Schauflerc6739442013-05-22 18:42:56 -070036#include <net/ip.h>
37#include <net/ipv6.h>
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +100038#include <linux/audit.h>
Nick Black1fd7317d2009-09-22 16:43:33 -070039#include <linux/magic.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050040#include <linux/dcache.h>
Jarkko Sakkinen16014d82011-10-14 13:16:24 +030041#include <linux/personality.h>
Al Viro40401532012-02-13 03:58:52 +000042#include <linux/msg.h>
43#include <linux/shm.h>
44#include <linux/binfmts.h>
Vivek Trivedi3bf27892015-06-22 15:36:06 +053045#include <linux/parser.h>
Casey Schauflere114e472008-02-04 22:29:50 -080046#include "smack.h"
47
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +020048#define TRANS_TRUE "TRUE"
49#define TRANS_TRUE_SIZE 4
50
Casey Schauflerc6739442013-05-22 18:42:56 -070051#define SMK_CONNECTING 0
52#define SMK_RECEIVING 1
53#define SMK_SENDING 2
54
Casey Schaufler21abb1e2015-07-22 14:25:31 -070055#ifdef SMACK_IPV6_PORT_LABELING
Vishal Goel3c7ce342016-11-23 10:31:08 +053056DEFINE_MUTEX(smack_ipv6_lock);
Geliang Tang8b549ef2015-09-27 23:10:25 +080057static LIST_HEAD(smk_ipv6_port_list);
Casey Schaufler21abb1e2015-07-22 14:25:31 -070058#endif
Rohit1a5b4722014-10-15 17:40:41 +053059static struct kmem_cache *smack_inode_cache;
Casey Schaufler69f287a2014-12-12 17:08:40 -080060int smack_enabled;
Casey Schauflerc6739442013-05-22 18:42:56 -070061
Al Viroc3300aa2018-12-16 01:52:24 -050062#define A(s) {"smack"#s, sizeof("smack"#s) - 1, Opt_##s}
63static struct {
64 const char *name;
65 int len;
66 int opt;
67} smk_mount_opts[] = {
68 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
Casey Schauflerf7112e62012-05-06 15:22:02 -0700290 buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
291 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
309 * @isp: the blob to initialize
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) {
355 nrp = kzalloc(sizeof(struct smack_rule), gfp);
356 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
511 * @type: message type
512 *
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
529
530/*
531 * Superblock Hooks.
532 */
533
534/**
535 * smack_sb_alloc_security - allocate a superblock blob
536 * @sb: the superblock getting the blob
537 *
538 * Returns 0 on success or -ENOMEM on error.
539 */
540static int smack_sb_alloc_security(struct super_block *sb)
541{
542 struct superblock_smack *sbsp;
543
544 sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);
545
546 if (sbsp == NULL)
547 return -ENOMEM;
548
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200549 sbsp->smk_root = &smack_known_floor;
550 sbsp->smk_default = &smack_known_floor;
551 sbsp->smk_floor = &smack_known_floor;
552 sbsp->smk_hat = &smack_known_hat;
Casey Schauflere830b392013-05-22 18:43:07 -0700553 /*
Seth Forshee9f50eda2015-09-23 15:16:06 -0500554 * SMK_SB_INITIALIZED will be zero from kzalloc.
Casey Schauflere830b392013-05-22 18:43:07 -0700555 */
Casey Schauflere114e472008-02-04 22:29:50 -0800556 sb->s_security = sbsp;
557
558 return 0;
559}
560
561/**
562 * smack_sb_free_security - free a superblock blob
563 * @sb: the superblock getting the blob
564 *
565 */
566static void smack_sb_free_security(struct super_block *sb)
567{
568 kfree(sb->s_security);
569 sb->s_security = NULL;
570}
571
Al Viro12085b12018-12-13 15:18:05 -0500572struct smack_mnt_opts {
573 const char *fsdefault, *fsfloor, *fshat, *fsroot, *fstransmute;
574};
575
Al Viro204cc0c2018-12-13 13:41:47 -0500576static void smack_free_mnt_opts(void *mnt_opts)
Casey Schauflere114e472008-02-04 22:29:50 -0800577{
Al Viro12085b12018-12-13 15:18:05 -0500578 struct smack_mnt_opts *opts = mnt_opts;
579 kfree(opts->fsdefault);
580 kfree(opts->fsfloor);
581 kfree(opts->fshat);
582 kfree(opts->fsroot);
583 kfree(opts->fstransmute);
Al Viro204cc0c2018-12-13 13:41:47 -0500584 kfree(opts);
Casey Schauflere114e472008-02-04 22:29:50 -0800585}
586
Al Viro55c0e5b2018-12-16 01:09:45 -0500587static int smack_add_opt(int token, const char *s, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530588{
Al Viro55c0e5b2018-12-16 01:09:45 -0500589 struct smack_mnt_opts *opts = *mnt_opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530590
Al Viro55c0e5b2018-12-16 01:09:45 -0500591 if (!opts) {
592 opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
593 if (!opts)
594 return -ENOMEM;
595 *mnt_opts = opts;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530596 }
Al Viro55c0e5b2018-12-16 01:09:45 -0500597 if (!s)
598 return -ENOMEM;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530599
Al Viro55c0e5b2018-12-16 01:09:45 -0500600 switch (token) {
601 case Opt_fsdefault:
602 if (opts->fsdefault)
603 goto out_opt_err;
604 opts->fsdefault = s;
605 break;
606 case Opt_fsfloor:
607 if (opts->fsfloor)
608 goto out_opt_err;
609 opts->fsfloor = s;
610 break;
611 case Opt_fshat:
612 if (opts->fshat)
613 goto out_opt_err;
614 opts->fshat = s;
615 break;
616 case Opt_fsroot:
617 if (opts->fsroot)
618 goto out_opt_err;
619 opts->fsroot = s;
620 break;
621 case Opt_fstransmute:
622 if (opts->fstransmute)
623 goto out_opt_err;
624 opts->fstransmute = s;
625 break;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530626 }
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530627 return 0;
628
629out_opt_err:
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530630 pr_warn("Smack: duplicate mount options\n");
Al Viro55c0e5b2018-12-16 01:09:45 -0500631 return -EINVAL;
632}
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530633
Al Virod2497e12018-12-16 01:37:06 -0500634static int smack_sb_eat_lsm_opts(char *options, void **mnt_opts)
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530635{
Al Virod2497e12018-12-16 01:37:06 -0500636 char *from = options, *to = options;
637 bool first = true;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530638
Al Viroc3300aa2018-12-16 01:52:24 -0500639 while (1) {
640 char *next = strchr(from, ',');
641 int token, len, rc;
642 char *arg = NULL;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530643
Al Viroc3300aa2018-12-16 01:52:24 -0500644 if (next)
645 len = next - from;
646 else
647 len = strlen(from);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530648
Al Viroc3300aa2018-12-16 01:52:24 -0500649 token = match_opt_prefix(from, len, &arg);
Al Virod2497e12018-12-16 01:37:06 -0500650 if (token != Opt_error) {
651 arg = kmemdup_nul(arg, from + len - arg, GFP_KERNEL);
652 rc = smack_add_opt(token, arg, mnt_opts);
653 if (unlikely(rc)) {
654 kfree(arg);
655 if (*mnt_opts)
656 smack_free_mnt_opts(*mnt_opts);
657 *mnt_opts = NULL;
658 return rc;
659 }
660 } else {
661 if (!first) { // copy with preceding comma
662 from--;
663 len++;
664 }
665 if (to != from)
666 memmove(to, from, len);
667 to += len;
668 first = false;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530669 }
Al Viroc3300aa2018-12-16 01:52:24 -0500670 if (!from[len])
671 break;
672 from += len + 1;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530673 }
Al Virod2497e12018-12-16 01:37:06 -0500674 *to = '\0';
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530675 return 0;
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530676}
677
678/**
679 * smack_set_mnt_opts - set Smack specific mount options
Casey Schauflere114e472008-02-04 22:29:50 -0800680 * @sb: the file system superblock
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530681 * @opts: Smack mount options
682 * @kern_flags: mount option from kernel space or user space
683 * @set_kern_flags: where to store converted mount opts
Casey Schauflere114e472008-02-04 22:29:50 -0800684 *
685 * Returns 0 on success, an error code on failure
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530686 *
687 * Allow filesystems with binary mount data to explicitly set Smack mount
688 * labels.
Casey Schauflere114e472008-02-04 22:29:50 -0800689 */
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530690static int smack_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500691 void *mnt_opts,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530692 unsigned long kern_flags,
693 unsigned long *set_kern_flags)
Casey Schauflere114e472008-02-04 22:29:50 -0800694{
695 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000696 struct inode *inode = d_backing_inode(root);
Casey Schauflere114e472008-02-04 22:29:50 -0800697 struct superblock_smack *sp = sb->s_security;
698 struct inode_smack *isp;
Casey Schaufler24ea1b62013-12-30 09:38:00 -0800699 struct smack_known *skp;
Al Viro12085b12018-12-13 15:18:05 -0500700 struct smack_mnt_opts *opts = mnt_opts;
701 bool transmute = false;
Casey Schauflere114e472008-02-04 22:29:50 -0800702
Seth Forshee9f50eda2015-09-23 15:16:06 -0500703 if (sp->smk_flags & SMK_SB_INITIALIZED)
Casey Schauflere114e472008-02-04 22:29:50 -0800704 return 0;
Casey Schauflereb982cb2012-05-23 17:46:58 -0700705
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700706 if (inode->i_security == NULL) {
707 int rc = lsm_inode_alloc(inode);
708
709 if (rc)
710 return rc;
711 }
712
Himanshu Shukla2097f592016-11-10 16:19:52 +0530713 if (!smack_privileged(CAP_MAC_ADMIN)) {
714 /*
715 * Unprivileged mounts don't get to specify Smack values.
716 */
Al Viro12085b12018-12-13 15:18:05 -0500717 if (opts)
Himanshu Shukla2097f592016-11-10 16:19:52 +0530718 return -EPERM;
719 /*
720 * Unprivileged mounts get root and default from the caller.
721 */
722 skp = smk_of_current();
723 sp->smk_root = skp;
724 sp->smk_default = skp;
725 /*
726 * For a handful of fs types with no user-controlled
727 * backing store it's okay to trust security labels
728 * in the filesystem. The rest are untrusted.
729 */
730 if (sb->s_user_ns != &init_user_ns &&
731 sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
732 sb->s_magic != RAMFS_MAGIC) {
Al Viro12085b12018-12-13 15:18:05 -0500733 transmute = true;
Himanshu Shukla2097f592016-11-10 16:19:52 +0530734 sp->smk_flags |= SMK_SB_UNTRUSTED;
735 }
736 }
737
Seth Forshee9f50eda2015-09-23 15:16:06 -0500738 sp->smk_flags |= SMK_SB_INITIALIZED;
Casey Schauflere114e472008-02-04 22:29:50 -0800739
Al Viro12085b12018-12-13 15:18:05 -0500740 if (opts) {
741 if (opts->fsdefault) {
742 skp = smk_import_entry(opts->fsdefault, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200743 if (IS_ERR(skp))
744 return PTR_ERR(skp);
745 sp->smk_default = skp;
Al Viro12085b12018-12-13 15:18:05 -0500746 }
747 if (opts->fsfloor) {
748 skp = smk_import_entry(opts->fsfloor, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530749 if (IS_ERR(skp))
750 return PTR_ERR(skp);
751 sp->smk_floor = skp;
Al Viro12085b12018-12-13 15:18:05 -0500752 }
753 if (opts->fshat) {
754 skp = smk_import_entry(opts->fshat, 0);
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530755 if (IS_ERR(skp))
756 return PTR_ERR(skp);
757 sp->smk_hat = skp;
Al Viro12085b12018-12-13 15:18:05 -0500758 }
759 if (opts->fsroot) {
760 skp = smk_import_entry(opts->fsroot, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200761 if (IS_ERR(skp))
762 return PTR_ERR(skp);
763 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500764 }
765 if (opts->fstransmute) {
766 skp = smk_import_entry(opts->fstransmute, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +0200767 if (IS_ERR(skp))
768 return PTR_ERR(skp);
769 sp->smk_root = skp;
Al Viro12085b12018-12-13 15:18:05 -0500770 transmute = true;
Casey Schauflere114e472008-02-04 22:29:50 -0800771 }
772 }
773
774 /*
775 * Initialize the root inode.
776 */
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700777 init_inode_smack(inode, sp->smk_root);
Casey Schauflere114e472008-02-04 22:29:50 -0800778
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700779 if (transmute) {
780 isp = smack_inode(inode);
Casey Schauflere830b392013-05-22 18:43:07 -0700781 isp->smk_flags |= SMK_INODE_TRANSMUTE;
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700782 }
Casey Schauflere830b392013-05-22 18:43:07 -0700783
Casey Schauflere114e472008-02-04 22:29:50 -0800784 return 0;
785}
786
787/**
788 * smack_sb_statfs - Smack check on statfs
789 * @dentry: identifies the file system in question
790 *
791 * Returns 0 if current can read the floor of the filesystem,
792 * and error code otherwise
793 */
794static int smack_sb_statfs(struct dentry *dentry)
795{
796 struct superblock_smack *sbp = dentry->d_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200797 int rc;
798 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800799
Eric Parisa2694342011-04-25 13:10:27 -0400800 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200801 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
802
803 rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700804 rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200805 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -0800806}
807
Casey Schauflere114e472008-02-04 22:29:50 -0800808/*
Casey Schaufler676dac42010-12-02 06:43:39 -0800809 * BPRM hooks
810 */
811
Casey Schauflerce8a4322011-09-29 18:21:01 -0700812/**
813 * smack_bprm_set_creds - set creds for exec
814 * @bprm: the exec information
815 *
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100816 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schauflerce8a4322011-09-29 18:21:01 -0700817 */
Casey Schaufler676dac42010-12-02 06:43:39 -0800818static int smack_bprm_set_creds(struct linux_binprm *bprm)
819{
Al Viro496ad9a2013-01-23 17:07:38 -0500820 struct inode *inode = file_inode(bprm->file);
Casey Schauflerb17103a2018-11-09 16:12:56 -0800821 struct task_smack *bsp = smack_cred(bprm->cred);
Casey Schaufler676dac42010-12-02 06:43:39 -0800822 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -0500823 struct superblock_smack *sbsp;
Casey Schaufler676dac42010-12-02 06:43:39 -0800824 int rc;
825
Kees Cookddb4a142017-07-18 15:25:23 -0700826 if (bprm->called_set_creds)
Casey Schaufler676dac42010-12-02 06:43:39 -0800827 return 0;
828
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800829 isp = smack_inode(inode);
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300830 if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
Casey Schaufler676dac42010-12-02 06:43:39 -0800831 return 0;
832
Seth Forshee809c02e2016-04-26 14:36:22 -0500833 sbsp = inode->i_sb->s_security;
834 if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
835 isp->smk_task != sbsp->smk_root)
836 return 0;
837
Eric W. Biederman9227dd22017-01-23 17:26:31 +1300838 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100839 struct task_struct *tracer;
840 rc = 0;
841
842 rcu_read_lock();
843 tracer = ptrace_parent(current);
844 if (likely(tracer != NULL))
845 rc = smk_ptrace_rule_check(tracer,
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200846 isp->smk_task,
Lukasz Pawelczyk56638842014-03-11 17:07:05 +0100847 PTRACE_MODE_ATTACH,
848 __func__);
849 rcu_read_unlock();
850
851 if (rc != 0)
852 return rc;
853 } else if (bprm->unsafe)
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300854 return -EPERM;
Casey Schaufler676dac42010-12-02 06:43:39 -0800855
Jarkko Sakkinen84088ba2011-10-07 09:27:53 +0300856 bsp->smk_task = isp->smk_task;
857 bprm->per_clear |= PER_CLEAR_ON_SETID;
Casey Schaufler676dac42010-12-02 06:43:39 -0800858
Kees Cookccbb6e12017-07-18 15:25:26 -0700859 /* Decide if this is a secure exec. */
860 if (bsp->smk_task != bsp->smk_forked)
861 bprm->secureexec = 1;
862
Casey Schaufler676dac42010-12-02 06:43:39 -0800863 return 0;
864}
865
866/*
Casey Schauflere114e472008-02-04 22:29:50 -0800867 * Inode hooks
868 */
869
870/**
871 * smack_inode_alloc_security - allocate an inode blob
Randy Dunlap251a2a92009-02-18 11:42:33 -0800872 * @inode: the inode in need of a blob
Casey Schauflere114e472008-02-04 22:29:50 -0800873 *
874 * Returns 0 if it gets a blob, -ENOMEM otherwise
875 */
876static int smack_inode_alloc_security(struct inode *inode)
877{
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700878 struct smack_known *skp = smk_of_current();
879
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700880 init_inode_smack(inode, skp);
Casey Schauflere114e472008-02-04 22:29:50 -0800881 return 0;
882}
883
884/**
Casey Schauflere114e472008-02-04 22:29:50 -0800885 * smack_inode_init_security - copy out the smack from an inode
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +0200886 * @inode: the newly created inode
887 * @dir: containing directory object
Eric Paris2a7dba32011-02-01 11:05:39 -0500888 * @qstr: unused
Casey Schauflere114e472008-02-04 22:29:50 -0800889 * @name: where to put the attribute name
890 * @value: where to put the attribute value
891 * @len: where to put the length of the attribute
892 *
893 * Returns 0 if it all works out, -ENOMEM if there's no memory
894 */
895static int smack_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +0900896 const struct qstr *qstr, const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -0500897 void **value, size_t *len)
Casey Schauflere114e472008-02-04 22:29:50 -0800898{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800899 struct inode_smack *issp = smack_inode(inode);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700900 struct smack_known *skp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200901 struct smack_known *isp = smk_of_inode(inode);
902 struct smack_known *dsp = smk_of_inode(dir);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800903 int may;
Casey Schauflere114e472008-02-04 22:29:50 -0800904
Tetsuo Handa95489062013-07-25 05:44:02 +0900905 if (name)
906 *name = XATTR_SMACK_SUFFIX;
Casey Schauflere114e472008-02-04 22:29:50 -0800907
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100908 if (value && len) {
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800909 rcu_read_lock();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200910 may = smk_access_entry(skp->smk_known, dsp->smk_known,
911 &skp->smk_rules);
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800912 rcu_read_unlock();
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200913
914 /*
915 * If the access rule allows transmutation and
916 * the directory requests transmutation then
917 * by all means transmute.
Casey Schaufler2267b132012-03-13 19:14:19 -0700918 * Mark the inode as changed.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200919 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800920 if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler2267b132012-03-13 19:14:19 -0700921 smk_inode_transmutable(dir)) {
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200922 isp = dsp;
Casey Schaufler2267b132012-03-13 19:14:19 -0700923 issp->smk_flags |= SMK_INODE_CHANGED;
924 }
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200925
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200926 *value = kstrdup(isp->smk_known, GFP_NOFS);
Casey Schauflere114e472008-02-04 22:29:50 -0800927 if (*value == NULL)
928 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -0800929
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200930 *len = strlen(isp->smk_known);
Lukasz Pawelczyk68390cc2014-11-26 15:31:07 +0100931 }
Casey Schauflere114e472008-02-04 22:29:50 -0800932
933 return 0;
934}
935
936/**
937 * smack_inode_link - Smack check on link
938 * @old_dentry: the existing object
939 * @dir: unused
940 * @new_dentry: the new object
941 *
942 * Returns 0 if access is permitted, an error code otherwise
943 */
944static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
945 struct dentry *new_dentry)
946{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200947 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200948 struct smk_audit_info ad;
949 int rc;
950
Eric Parisa2694342011-04-25 13:10:27 -0400951 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200952 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -0800953
David Howellsc6f493d2015-03-17 22:26:22 +0000954 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200955 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000956 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800957
David Howells88025652015-01-29 12:02:32 +0000958 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +0000959 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200960 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
961 rc = smk_curacc(isp, MAY_WRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +0000962 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -0800963 }
964
965 return rc;
966}
967
968/**
969 * smack_inode_unlink - Smack check on inode deletion
970 * @dir: containing directory object
971 * @dentry: file to unlink
972 *
973 * Returns 0 if current can write the containing directory
974 * and the object, error code otherwise
975 */
976static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
977{
David Howellsc6f493d2015-03-17 22:26:22 +0000978 struct inode *ip = d_backing_inode(dentry);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200979 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -0800980 int rc;
981
Eric Parisa2694342011-04-25 13:10:27 -0400982 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200983 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
984
Casey Schauflere114e472008-02-04 22:29:50 -0800985 /*
986 * You need write access to the thing you're unlinking
987 */
Etienne Bassetecfcc532009-04-08 20:40:06 +0200988 rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700989 rc = smk_bu_inode(ip, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200990 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -0800991 /*
992 * You also need write access to the containing directory
993 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +0400994 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200995 smk_ad_setfield_u_fs_inode(&ad, dir);
996 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -0700997 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +0200998 }
Casey Schauflere114e472008-02-04 22:29:50 -0800999 return rc;
1000}
1001
1002/**
1003 * smack_inode_rmdir - Smack check on directory deletion
1004 * @dir: containing directory object
1005 * @dentry: directory to unlink
1006 *
1007 * Returns 0 if current can write the containing directory
1008 * and the directory, error code otherwise
1009 */
1010static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
1011{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001012 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001013 int rc;
1014
Eric Parisa2694342011-04-25 13:10:27 -04001015 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001016 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1017
Casey Schauflere114e472008-02-04 22:29:50 -08001018 /*
1019 * You need write access to the thing you're removing
1020 */
David Howellsc6f493d2015-03-17 22:26:22 +00001021 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1022 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001023 if (rc == 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08001024 /*
1025 * You also need write access to the containing directory
1026 */
Igor Zhbanovcdb56b62013-03-19 13:49:47 +04001027 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001028 smk_ad_setfield_u_fs_inode(&ad, dir);
1029 rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001030 rc = smk_bu_inode(dir, MAY_WRITE, rc);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001031 }
Casey Schauflere114e472008-02-04 22:29:50 -08001032
1033 return rc;
1034}
1035
1036/**
1037 * smack_inode_rename - Smack check on rename
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001038 * @old_inode: unused
1039 * @old_dentry: the old object
1040 * @new_inode: unused
1041 * @new_dentry: the new object
Casey Schauflere114e472008-02-04 22:29:50 -08001042 *
1043 * Read and write access is required on both the old and
1044 * new directories.
1045 *
1046 * Returns 0 if access is permitted, an error code otherwise
1047 */
1048static int smack_inode_rename(struct inode *old_inode,
1049 struct dentry *old_dentry,
1050 struct inode *new_inode,
1051 struct dentry *new_dentry)
1052{
1053 int rc;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001054 struct smack_known *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001055 struct smk_audit_info ad;
1056
Eric Parisa2694342011-04-25 13:10:27 -04001057 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001058 smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001059
David Howellsc6f493d2015-03-17 22:26:22 +00001060 isp = smk_of_inode(d_backing_inode(old_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001061 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001062 rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001063
David Howells88025652015-01-29 12:02:32 +00001064 if (rc == 0 && d_is_positive(new_dentry)) {
David Howellsc6f493d2015-03-17 22:26:22 +00001065 isp = smk_of_inode(d_backing_inode(new_dentry));
Etienne Bassetecfcc532009-04-08 20:40:06 +02001066 smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
1067 rc = smk_curacc(isp, MAY_READWRITE, &ad);
David Howellsc6f493d2015-03-17 22:26:22 +00001068 rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001069 }
Casey Schauflere114e472008-02-04 22:29:50 -08001070 return rc;
1071}
1072
1073/**
1074 * smack_inode_permission - Smack version of permission()
1075 * @inode: the inode in question
1076 * @mask: the access requested
Casey Schauflere114e472008-02-04 22:29:50 -08001077 *
1078 * This is the important Smack hook.
1079 *
1080 * Returns 0 if access is permitted, -EACCES otherwise
1081 */
Al Viroe74f71e2011-06-20 19:38:15 -04001082static int smack_inode_permission(struct inode *inode, int mask)
Casey Schauflere114e472008-02-04 22:29:50 -08001083{
Seth Forshee9f50eda2015-09-23 15:16:06 -05001084 struct superblock_smack *sbsp = inode->i_sb->s_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001085 struct smk_audit_info ad;
Al Viroe74f71e2011-06-20 19:38:15 -04001086 int no_block = mask & MAY_NOT_BLOCK;
Casey Schauflerd166c802014-08-27 14:51:27 -07001087 int rc;
Eric Parisd09ca732010-07-23 11:43:57 -04001088
1089 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
Casey Schauflere114e472008-02-04 22:29:50 -08001090 /*
1091 * No permission to check. Existence test. Yup, it's there.
1092 */
1093 if (mask == 0)
1094 return 0;
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001095
Seth Forshee9f50eda2015-09-23 15:16:06 -05001096 if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
1097 if (smk_of_inode(inode) != sbsp->smk_root)
1098 return -EACCES;
1099 }
1100
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001101 /* May be droppable after audit */
Al Viroe74f71e2011-06-20 19:38:15 -04001102 if (no_block)
Andi Kleen8c9e80e2011-04-21 17:23:19 -07001103 return -ECHILD;
Eric Parisf48b7392011-04-25 12:54:27 -04001104 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001105 smk_ad_setfield_u_fs_inode(&ad, inode);
Casey Schauflerd166c802014-08-27 14:51:27 -07001106 rc = smk_curacc(smk_of_inode(inode), mask, &ad);
1107 rc = smk_bu_inode(inode, mask, rc);
1108 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001109}
1110
1111/**
1112 * smack_inode_setattr - Smack check for setting attributes
1113 * @dentry: the object
1114 * @iattr: for the force flag
1115 *
1116 * Returns 0 if access is permitted, an error code otherwise
1117 */
1118static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
1119{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001120 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001121 int rc;
1122
Casey Schauflere114e472008-02-04 22:29:50 -08001123 /*
1124 * Need to allow for clearing the setuid bit.
1125 */
1126 if (iattr->ia_valid & ATTR_FORCE)
1127 return 0;
Eric Parisa2694342011-04-25 13:10:27 -04001128 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001129 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflere114e472008-02-04 22:29:50 -08001130
David Howellsc6f493d2015-03-17 22:26:22 +00001131 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1132 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001133 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001134}
1135
1136/**
1137 * smack_inode_getattr - Smack check for getting attributes
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001138 * @mnt: vfsmount of the object
Casey Schauflere114e472008-02-04 22:29:50 -08001139 * @dentry: the object
1140 *
1141 * Returns 0 if access is permitted, an error code otherwise
1142 */
Al Viro3f7036a2015-03-08 19:28:30 -04001143static int smack_inode_getattr(const struct path *path)
Casey Schauflere114e472008-02-04 22:29:50 -08001144{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001145 struct smk_audit_info ad;
David Howellsc6f493d2015-03-17 22:26:22 +00001146 struct inode *inode = d_backing_inode(path->dentry);
Casey Schauflerd166c802014-08-27 14:51:27 -07001147 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001148
Eric Parisf48b7392011-04-25 12:54:27 -04001149 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Al Viro3f7036a2015-03-08 19:28:30 -04001150 smk_ad_setfield_u_fs_path(&ad, *path);
1151 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
1152 rc = smk_bu_inode(inode, MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001153 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001154}
1155
1156/**
1157 * smack_inode_setxattr - Smack check for setting xattrs
1158 * @dentry: the object
1159 * @name: name of the attribute
Lukasz Pawelczyke95ef492014-08-29 17:02:53 +02001160 * @value: value of the attribute
1161 * @size: size of the value
Casey Schauflere114e472008-02-04 22:29:50 -08001162 * @flags: unused
1163 *
1164 * This protects the Smack attribute explicitly.
1165 *
1166 * Returns 0 if access is permitted, an error code otherwise
1167 */
David Howells8f0cfa52008-04-29 00:59:41 -07001168static int smack_inode_setxattr(struct dentry *dentry, const char *name,
1169 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001170{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001171 struct smk_audit_info ad;
Casey Schaufler19760ad2013-12-16 16:27:26 -08001172 struct smack_known *skp;
1173 int check_priv = 0;
1174 int check_import = 0;
1175 int check_star = 0;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001176 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001177
Casey Schaufler19760ad2013-12-16 16:27:26 -08001178 /*
1179 * Check label validity here so import won't fail in post_setxattr
1180 */
Casey Schauflerbcdca222008-02-23 15:24:04 -08001181 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1182 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler19760ad2013-12-16 16:27:26 -08001183 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
1184 check_priv = 1;
1185 check_import = 1;
1186 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1187 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1188 check_priv = 1;
1189 check_import = 1;
1190 check_star = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001191 } else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
Casey Schaufler19760ad2013-12-16 16:27:26 -08001192 check_priv = 1;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001193 if (size != TRANS_TRUE_SIZE ||
1194 strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
1195 rc = -EINVAL;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001196 } else
1197 rc = cap_inode_setxattr(dentry, name, value, size, flags);
1198
Casey Schaufler19760ad2013-12-16 16:27:26 -08001199 if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
1200 rc = -EPERM;
1201
1202 if (rc == 0 && check_import) {
Konstantin Khlebnikovb862e562014-08-07 20:52:43 +04001203 skp = size ? smk_import_entry(value, size) : NULL;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001204 if (IS_ERR(skp))
1205 rc = PTR_ERR(skp);
1206 else if (skp == NULL || (check_star &&
Casey Schaufler19760ad2013-12-16 16:27:26 -08001207 (skp == &smack_known_star || skp == &smack_known_web)))
1208 rc = -EINVAL;
1209 }
1210
Eric Parisa2694342011-04-25 13:10:27 -04001211 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001212 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1213
Casey Schauflerd166c802014-08-27 14:51:27 -07001214 if (rc == 0) {
David Howellsc6f493d2015-03-17 22:26:22 +00001215 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1216 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001217 }
Casey Schauflerbcdca222008-02-23 15:24:04 -08001218
1219 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001220}
1221
1222/**
1223 * smack_inode_post_setxattr - Apply the Smack update approved above
1224 * @dentry: object
1225 * @name: attribute name
1226 * @value: attribute value
1227 * @size: attribute size
1228 * @flags: unused
1229 *
1230 * Set the pointer in the inode blob to the entry found
1231 * in the master label list.
1232 */
David Howells8f0cfa52008-04-29 00:59:41 -07001233static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
1234 const void *value, size_t size, int flags)
Casey Schauflere114e472008-02-04 22:29:50 -08001235{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001236 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001237 struct inode_smack *isp = smack_inode(d_backing_inode(dentry));
Casey Schaufler676dac42010-12-02 06:43:39 -08001238
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001239 if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1240 isp->smk_flags |= SMK_INODE_TRANSMUTE;
1241 return;
1242 }
1243
Casey Schaufler676dac42010-12-02 06:43:39 -08001244 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001245 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001246 if (!IS_ERR(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001247 isp->smk_inode = skp;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001248 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001249 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001250 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001251 isp->smk_task = skp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001252 } else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
José Bollo9598f4c2014-04-03 13:48:41 +02001253 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02001254 if (!IS_ERR(skp))
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001255 isp->smk_mmap = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001256 }
Casey Schauflere114e472008-02-04 22:29:50 -08001257
1258 return;
1259}
1260
Casey Schauflerce8a4322011-09-29 18:21:01 -07001261/**
Casey Schauflere114e472008-02-04 22:29:50 -08001262 * smack_inode_getxattr - Smack check on getxattr
1263 * @dentry: the object
1264 * @name: unused
1265 *
1266 * Returns 0 if access is permitted, an error code otherwise
1267 */
David Howells8f0cfa52008-04-29 00:59:41 -07001268static int smack_inode_getxattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001269{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001270 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001271 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001272
Eric Parisa2694342011-04-25 13:10:27 -04001273 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001274 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1275
David Howellsc6f493d2015-03-17 22:26:22 +00001276 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
1277 rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07001278 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001279}
1280
Casey Schauflerce8a4322011-09-29 18:21:01 -07001281/**
Casey Schauflere114e472008-02-04 22:29:50 -08001282 * smack_inode_removexattr - Smack check on removexattr
1283 * @dentry: the object
1284 * @name: name of the attribute
1285 *
1286 * Removing the Smack attribute requires CAP_MAC_ADMIN
1287 *
1288 * Returns 0 if access is permitted, an error code otherwise
1289 */
David Howells8f0cfa52008-04-29 00:59:41 -07001290static int smack_inode_removexattr(struct dentry *dentry, const char *name)
Casey Schauflere114e472008-02-04 22:29:50 -08001291{
Casey Schaufler676dac42010-12-02 06:43:39 -08001292 struct inode_smack *isp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001293 struct smk_audit_info ad;
Casey Schauflerbcdca222008-02-23 15:24:04 -08001294 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001295
Casey Schauflerbcdca222008-02-23 15:24:04 -08001296 if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
1297 strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
Casey Schaufler676dac42010-12-02 06:43:39 -08001298 strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02001299 strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001300 strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05301301 strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
Casey Schaufler1880eff2012-06-05 15:28:30 -07001302 if (!smack_privileged(CAP_MAC_ADMIN))
Casey Schauflerbcdca222008-02-23 15:24:04 -08001303 rc = -EPERM;
1304 } else
1305 rc = cap_inode_removexattr(dentry, name);
1306
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001307 if (rc != 0)
1308 return rc;
1309
Eric Parisa2694342011-04-25 13:10:27 -04001310 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001311 smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
Casey Schauflerbcdca222008-02-23 15:24:04 -08001312
David Howellsc6f493d2015-03-17 22:26:22 +00001313 rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
1314 rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001315 if (rc != 0)
1316 return rc;
1317
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001318 isp = smack_inode(d_backing_inode(dentry));
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001319 /*
1320 * Don't do anything special for these.
1321 * XATTR_NAME_SMACKIPIN
1322 * XATTR_NAME_SMACKIPOUT
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001323 */
José Bollo80124952016-01-12 21:23:40 +01001324 if (strcmp(name, XATTR_NAME_SMACK) == 0) {
Al Virofc640052016-04-10 01:33:30 -04001325 struct super_block *sbp = dentry->d_sb;
José Bollo80124952016-01-12 21:23:40 +01001326 struct superblock_smack *sbsp = sbp->s_security;
1327
1328 isp->smk_inode = sbsp->smk_default;
1329 } else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
Casey Schaufler676dac42010-12-02 06:43:39 -08001330 isp->smk_task = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001331 else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001332 isp->smk_mmap = NULL;
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001333 else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
1334 isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
Casey Schaufler676dac42010-12-02 06:43:39 -08001335
Casey Schauflerf59bdfb2014-04-10 16:35:36 -07001336 return 0;
Casey Schauflere114e472008-02-04 22:29:50 -08001337}
1338
1339/**
1340 * smack_inode_getsecurity - get smack xattrs
1341 * @inode: the object
1342 * @name: attribute name
1343 * @buffer: where to put the result
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001344 * @alloc: duplicate memory
Casey Schauflere114e472008-02-04 22:29:50 -08001345 *
1346 * Returns the size of the attribute or an error code
1347 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05001348static int smack_inode_getsecurity(struct inode *inode,
Casey Schauflere114e472008-02-04 22:29:50 -08001349 const char *name, void **buffer,
1350 bool alloc)
1351{
1352 struct socket_smack *ssp;
1353 struct socket *sock;
1354 struct super_block *sbp;
1355 struct inode *ip = (struct inode *)inode;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001356 struct smack_known *isp;
Casey Schauflere114e472008-02-04 22:29:50 -08001357
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001358 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
Casey Schauflere114e472008-02-04 22:29:50 -08001359 isp = smk_of_inode(inode);
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001360 else {
1361 /*
1362 * The rest of the Smack xattrs are only on sockets.
1363 */
1364 sbp = ip->i_sb;
1365 if (sbp->s_magic != SOCKFS_MAGIC)
1366 return -EOPNOTSUPP;
1367
1368 sock = SOCKET_I(ip);
1369 if (sock == NULL || sock->sk == NULL)
1370 return -EOPNOTSUPP;
1371
1372 ssp = sock->sk->sk_security;
1373
1374 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1375 isp = ssp->smk_in;
1376 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1377 isp = ssp->smk_out;
1378 else
1379 return -EOPNOTSUPP;
Casey Schauflere114e472008-02-04 22:29:50 -08001380 }
1381
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001382 if (alloc) {
1383 *buffer = kstrdup(isp->smk_known, GFP_KERNEL);
1384 if (*buffer == NULL)
1385 return -ENOMEM;
Casey Schauflere114e472008-02-04 22:29:50 -08001386 }
1387
Casey Schaufler57e7ba02017-09-19 09:39:08 -07001388 return strlen(isp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08001389}
1390
1391
1392/**
1393 * smack_inode_listsecurity - list the Smack attributes
1394 * @inode: the object
1395 * @buffer: where they go
1396 * @buffer_size: size of buffer
Casey Schauflere114e472008-02-04 22:29:50 -08001397 */
1398static int smack_inode_listsecurity(struct inode *inode, char *buffer,
1399 size_t buffer_size)
1400{
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001401 int len = sizeof(XATTR_NAME_SMACK);
Casey Schauflere114e472008-02-04 22:29:50 -08001402
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001403 if (buffer != NULL && len <= buffer_size)
Casey Schauflere114e472008-02-04 22:29:50 -08001404 memcpy(buffer, XATTR_NAME_SMACK, len);
Konstantin Khlebnikovfd5c9d22014-08-07 20:52:33 +04001405
1406 return len;
Casey Schauflere114e472008-02-04 22:29:50 -08001407}
1408
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001409/**
1410 * smack_inode_getsecid - Extract inode's security id
1411 * @inode: inode to extract the info from
1412 * @secid: where result will be saved
1413 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001414static void smack_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001415{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001416 struct smack_known *skp = smk_of_inode(inode);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001417
Casey Schaufler0f8983c2018-06-01 10:45:12 -07001418 *secid = skp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10001419}
1420
Casey Schauflere114e472008-02-04 22:29:50 -08001421/*
1422 * File Hooks
1423 */
1424
Casey Schaufler491a0b02016-01-26 15:08:35 -08001425/*
1426 * There is no smack_file_permission hook
Casey Schauflere114e472008-02-04 22:29:50 -08001427 *
1428 * Should access checks be done on each read or write?
1429 * UNICOS and SELinux say yes.
1430 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
1431 *
1432 * I'll say no for now. Smack does not do the frequent
1433 * label changing that SELinux does.
1434 */
Casey Schauflere114e472008-02-04 22:29:50 -08001435
1436/**
1437 * smack_file_alloc_security - assign a file security blob
1438 * @file: the object
1439 *
1440 * The security blob for a file is a pointer to the master
1441 * label list, so no allocation is done.
1442 *
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001443 * f_security is the owner security information. It
1444 * isn't used on file access checks, it's for send_sigio.
1445 *
Casey Schauflere114e472008-02-04 22:29:50 -08001446 * Returns 0
1447 */
1448static int smack_file_alloc_security(struct file *file)
1449{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001450 struct smack_known **blob = smack_file(file);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001451
Casey Schauflerf28952a2018-11-12 09:38:53 -08001452 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001453 return 0;
1454}
1455
1456/**
Casey Schauflere114e472008-02-04 22:29:50 -08001457 * smack_file_ioctl - Smack check on ioctls
1458 * @file: the object
1459 * @cmd: what to do
1460 * @arg: unused
1461 *
1462 * Relies heavily on the correct use of the ioctl command conventions.
1463 *
1464 * Returns 0 if allowed, error code otherwise
1465 */
1466static int smack_file_ioctl(struct file *file, unsigned int cmd,
1467 unsigned long arg)
1468{
1469 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001470 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001471 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001472
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001473 if (unlikely(IS_PRIVATE(inode)))
1474 return 0;
1475
Eric Parisf48b7392011-04-25 12:54:27 -04001476 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001477 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schauflere114e472008-02-04 22:29:50 -08001478
Casey Schauflerd166c802014-08-27 14:51:27 -07001479 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001480 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001481 rc = smk_bu_file(file, MAY_WRITE, rc);
1482 }
Casey Schauflere114e472008-02-04 22:29:50 -08001483
Casey Schauflerd166c802014-08-27 14:51:27 -07001484 if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001485 rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001486 rc = smk_bu_file(file, MAY_READ, rc);
1487 }
Casey Schauflere114e472008-02-04 22:29:50 -08001488
1489 return rc;
1490}
1491
1492/**
1493 * smack_file_lock - Smack check on file locking
1494 * @file: the object
Randy Dunlap251a2a92009-02-18 11:42:33 -08001495 * @cmd: unused
Casey Schauflere114e472008-02-04 22:29:50 -08001496 *
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001497 * Returns 0 if current has lock access, error code otherwise
Casey Schauflere114e472008-02-04 22:29:50 -08001498 */
1499static int smack_file_lock(struct file *file, unsigned int cmd)
1500{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001501 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07001502 int rc;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001503 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001504
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001505 if (unlikely(IS_PRIVATE(inode)))
1506 return 0;
1507
Eric Paris92f42502011-04-25 13:15:55 -04001508 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1509 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001510 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001511 rc = smk_bu_file(file, MAY_LOCK, rc);
1512 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001513}
1514
1515/**
1516 * smack_file_fcntl - Smack check on fcntl
1517 * @file: the object
1518 * @cmd: what action to check
1519 * @arg: unused
1520 *
Casey Schaufler531f1d42011-09-19 12:41:42 -07001521 * Generally these operations are harmless.
1522 * File locking operations present an obvious mechanism
1523 * for passing information, so they require write access.
1524 *
Casey Schauflere114e472008-02-04 22:29:50 -08001525 * Returns 0 if current has access, error code otherwise
1526 */
1527static int smack_file_fcntl(struct file *file, unsigned int cmd,
1528 unsigned long arg)
1529{
Etienne Bassetecfcc532009-04-08 20:40:06 +02001530 struct smk_audit_info ad;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001531 int rc = 0;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001532 struct inode *inode = file_inode(file);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001533
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001534 if (unlikely(IS_PRIVATE(inode)))
1535 return 0;
1536
Casey Schauflere114e472008-02-04 22:29:50 -08001537 switch (cmd) {
Casey Schauflere114e472008-02-04 22:29:50 -08001538 case F_GETLK:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001539 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001540 case F_SETLK:
1541 case F_SETLKW:
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001542 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1543 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001544 rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001545 rc = smk_bu_file(file, MAY_LOCK, rc);
Casey Schauflerc0ab6e52013-10-11 18:06:39 -07001546 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001547 case F_SETOWN:
1548 case F_SETSIG:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001549 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1550 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001551 rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001552 rc = smk_bu_file(file, MAY_WRITE, rc);
Casey Schauflere114e472008-02-04 22:29:50 -08001553 break;
1554 default:
Casey Schaufler531f1d42011-09-19 12:41:42 -07001555 break;
Casey Schauflere114e472008-02-04 22:29:50 -08001556 }
1557
1558 return rc;
1559}
1560
1561/**
Al Viroe5467852012-05-30 13:30:51 -04001562 * smack_mmap_file :
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001563 * Check permissions for a mmap operation. The @file may be NULL, e.g.
1564 * if mapping anonymous memory.
1565 * @file contains the file structure for file to map (may be NULL).
1566 * @reqprot contains the protection requested by the application.
1567 * @prot contains the protection that will be applied by the kernel.
1568 * @flags contains the operational flags.
1569 * Return 0 if permission is granted.
1570 */
Al Viroe5467852012-05-30 13:30:51 -04001571static int smack_mmap_file(struct file *file,
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001572 unsigned long reqprot, unsigned long prot,
Al Viroe5467852012-05-30 13:30:51 -04001573 unsigned long flags)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001574{
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001575 struct smack_known *skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001576 struct smack_known *mkp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001577 struct smack_rule *srp;
1578 struct task_smack *tsp;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001579 struct smack_known *okp;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001580 struct inode_smack *isp;
Seth Forshee809c02e2016-04-26 14:36:22 -05001581 struct superblock_smack *sbsp;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001582 int may;
1583 int mmay;
1584 int tmay;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001585 int rc;
1586
Al Viro496ad9a2013-01-23 17:07:38 -05001587 if (file == NULL)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001588 return 0;
1589
Seung-Woo Kim83a1e532016-12-12 17:35:26 +09001590 if (unlikely(IS_PRIVATE(file_inode(file))))
1591 return 0;
1592
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001593 isp = smack_inode(file_inode(file));
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001594 if (isp->smk_mmap == NULL)
1595 return 0;
Seth Forshee809c02e2016-04-26 14:36:22 -05001596 sbsp = file_inode(file)->i_sb->s_security;
1597 if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
1598 isp->smk_mmap != sbsp->smk_root)
1599 return -EACCES;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001600 mkp = isp->smk_mmap;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001601
Casey Schauflerb17103a2018-11-09 16:12:56 -08001602 tsp = smack_cred(current_cred());
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001603 skp = smk_of_current();
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001604 rc = 0;
1605
1606 rcu_read_lock();
1607 /*
1608 * For each Smack rule associated with the subject
1609 * label verify that the SMACK64MMAP also has access
1610 * to that rule's object label.
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001611 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07001612 list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001613 okp = srp->smk_object;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001614 /*
1615 * Matching labels always allows access.
1616 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001617 if (mkp->smk_known == okp->smk_known)
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001618 continue;
1619 /*
1620 * If there is a matching local rule take
1621 * that into account as well.
1622 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001623 may = smk_access_entry(srp->smk_subject->smk_known,
1624 okp->smk_known,
1625 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001626 if (may == -ENOENT)
1627 may = srp->smk_access;
1628 else
1629 may &= srp->smk_access;
1630 /*
1631 * If may is zero the SMACK64MMAP subject can't
1632 * possibly have less access.
1633 */
1634 if (may == 0)
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001635 continue;
1636
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001637 /*
1638 * Fetch the global list entry.
1639 * If there isn't one a SMACK64MMAP subject
1640 * can't have as much access as current.
1641 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001642 mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1643 &mkp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001644 if (mmay == -ENOENT) {
1645 rc = -EACCES;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001646 break;
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001647 }
1648 /*
1649 * If there is a local entry it modifies the
1650 * potential access, too.
1651 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001652 tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
1653 &tsp->smk_rules);
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001654 if (tmay != -ENOENT)
1655 mmay &= tmay;
1656
1657 /*
1658 * If there is any access available to current that is
1659 * not available to a SMACK64MMAP subject
1660 * deny access.
1661 */
Casey Schaufler75a25632011-02-09 19:58:42 -08001662 if ((may | mmay) != mmay) {
Casey Schaufler0e0a0702011-02-08 16:36:24 -08001663 rc = -EACCES;
1664 break;
1665 }
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001666 }
1667
1668 rcu_read_unlock();
1669
1670 return rc;
1671}
1672
1673/**
Casey Schauflere114e472008-02-04 22:29:50 -08001674 * smack_file_set_fowner - set the file security blob value
1675 * @file: object in question
1676 *
Casey Schauflere114e472008-02-04 22:29:50 -08001677 */
Jeff Laytone0b93ed2014-08-22 11:27:32 -04001678static void smack_file_set_fowner(struct file *file)
Casey Schauflere114e472008-02-04 22:29:50 -08001679{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001680 struct smack_known **blob = smack_file(file);
1681
1682 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08001683}
1684
1685/**
1686 * smack_file_send_sigiotask - Smack on sigio
1687 * @tsk: The target task
1688 * @fown: the object the signal come from
1689 * @signum: unused
1690 *
1691 * Allow a privileged task to get signals even if it shouldn't
1692 *
1693 * Returns 0 if a subject with the object's smack could
1694 * write to the task, an error code otherwise.
1695 */
1696static int smack_file_send_sigiotask(struct task_struct *tsk,
1697 struct fown_struct *fown, int signum)
1698{
Casey Schauflerf28952a2018-11-12 09:38:53 -08001699 struct smack_known **blob;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001700 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001701 struct smack_known *tkp = smk_of_task(smack_cred(tsk->cred));
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001702 const struct cred *tcred;
Casey Schauflere114e472008-02-04 22:29:50 -08001703 struct file *file;
1704 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001705 struct smk_audit_info ad;
Casey Schauflere114e472008-02-04 22:29:50 -08001706
1707 /*
1708 * struct fown_struct is never outside the context of a struct file
1709 */
1710 file = container_of(fown, struct file, f_owner);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001711
Etienne Bassetecfcc532009-04-08 20:40:06 +02001712 /* we don't log here as rc can be overriden */
Casey Schauflerf28952a2018-11-12 09:38:53 -08001713 blob = smack_file(file);
1714 skp = *blob;
Casey Schauflerc60b9062016-08-30 10:31:39 -07001715 rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
1716 rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001717
1718 rcu_read_lock();
1719 tcred = __task_cred(tsk);
1720 if (rc != 0 && smack_privileged_cred(CAP_MAC_OVERRIDE, tcred))
Etienne Bassetecfcc532009-04-08 20:40:06 +02001721 rc = 0;
Casey Schauflerdcb569c2018-09-18 16:09:16 -07001722 rcu_read_unlock();
Etienne Bassetecfcc532009-04-08 20:40:06 +02001723
1724 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
1725 smk_ad_setfield_u_tsk(&ad, tsk);
Casey Schauflerc60b9062016-08-30 10:31:39 -07001726 smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
Casey Schauflere114e472008-02-04 22:29:50 -08001727 return rc;
1728}
1729
1730/**
1731 * smack_file_receive - Smack file receive check
1732 * @file: the object
1733 *
1734 * Returns 0 if current has access, error code otherwise
1735 */
1736static int smack_file_receive(struct file *file)
1737{
Casey Schauflerd166c802014-08-27 14:51:27 -07001738 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001739 int may = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001740 struct smk_audit_info ad;
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001741 struct inode *inode = file_inode(file);
Casey Schaufler79be0932015-12-07 14:34:32 -08001742 struct socket *sock;
1743 struct task_smack *tsp;
1744 struct socket_smack *ssp;
Casey Schauflere114e472008-02-04 22:29:50 -08001745
Seung-Woo Kim97775822015-04-17 15:25:04 +09001746 if (unlikely(IS_PRIVATE(inode)))
1747 return 0;
1748
Casey Schaufler4482a442013-12-30 17:37:45 -08001749 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001750 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Casey Schaufler79be0932015-12-07 14:34:32 -08001751
Casey Schaufler51d59af2017-05-31 08:53:42 -07001752 if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
Casey Schaufler79be0932015-12-07 14:34:32 -08001753 sock = SOCKET_I(inode);
1754 ssp = sock->sk->sk_security;
Casey Schauflerb17103a2018-11-09 16:12:56 -08001755 tsp = smack_cred(current_cred());
Casey Schaufler79be0932015-12-07 14:34:32 -08001756 /*
1757 * If the receiving process can't write to the
1758 * passed socket or if the passed socket can't
1759 * write to the receiving process don't accept
1760 * the passed socket.
1761 */
1762 rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
1763 rc = smk_bu_file(file, may, rc);
1764 if (rc < 0)
1765 return rc;
1766 rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
1767 rc = smk_bu_file(file, may, rc);
1768 return rc;
1769 }
Casey Schauflere114e472008-02-04 22:29:50 -08001770 /*
1771 * This code relies on bitmasks.
1772 */
1773 if (file->f_mode & FMODE_READ)
1774 may = MAY_READ;
1775 if (file->f_mode & FMODE_WRITE)
1776 may |= MAY_WRITE;
1777
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001778 rc = smk_curacc(smk_of_inode(inode), may, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001779 rc = smk_bu_file(file, may, rc);
1780 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08001781}
1782
Casey Schaufler531f1d42011-09-19 12:41:42 -07001783/**
Eric Paris83d49852012-04-04 13:45:40 -04001784 * smack_file_open - Smack dentry open processing
Casey Schaufler531f1d42011-09-19 12:41:42 -07001785 * @file: the object
Casey Schauflera6834c02014-04-21 11:10:26 -07001786 * @cred: task credential
Casey Schaufler531f1d42011-09-19 12:41:42 -07001787 *
1788 * Set the security blob in the file structure.
Casey Schauflera6834c02014-04-21 11:10:26 -07001789 * Allow the open only if the task has read access. There are
1790 * many read operations (e.g. fstat) that you can do with an
1791 * fd even if you have the file open write-only.
Casey Schaufler531f1d42011-09-19 12:41:42 -07001792 *
1793 * Returns 0
1794 */
Al Viro94817692018-07-10 14:13:18 -04001795static int smack_file_open(struct file *file)
Casey Schaufler531f1d42011-09-19 12:41:42 -07001796{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001797 struct task_smack *tsp = smack_cred(file->f_cred);
Casey Schaufler5e7270a2014-12-12 17:19:19 -08001798 struct inode *inode = file_inode(file);
Casey Schauflera6834c02014-04-21 11:10:26 -07001799 struct smk_audit_info ad;
1800 int rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001801
Casey Schauflera6834c02014-04-21 11:10:26 -07001802 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1803 smk_ad_setfield_u_fs_path(&ad, file->f_path);
Himanshu Shuklac9d238a2016-11-23 11:59:45 +05301804 rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
Al Viro94817692018-07-10 14:13:18 -04001805 rc = smk_bu_credfile(file->f_cred, file, MAY_READ, rc);
Casey Schauflera6834c02014-04-21 11:10:26 -07001806
1807 return rc;
Casey Schaufler531f1d42011-09-19 12:41:42 -07001808}
1809
Casey Schauflere114e472008-02-04 22:29:50 -08001810/*
1811 * Task hooks
1812 */
1813
1814/**
David Howellsee18d642009-09-02 09:14:21 +01001815 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
1816 * @new: the new credentials
1817 * @gfp: the atomicity of any memory allocations
1818 *
1819 * Prepare a blank set of credentials for modification. This must allocate all
1820 * the memory the LSM module might require such that cred_transfer() can
1821 * complete without error.
1822 */
1823static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
1824{
Casey Schauflerbbd36622018-11-12 09:30:56 -08001825 init_task_smack(smack_cred(cred), NULL, NULL);
David Howellsee18d642009-09-02 09:14:21 +01001826 return 0;
1827}
1828
1829
1830/**
David Howellsf1752ee2008-11-14 10:39:17 +11001831 * smack_cred_free - "free" task-level security credentials
1832 * @cred: the credentials in question
Casey Schauflere114e472008-02-04 22:29:50 -08001833 *
Casey Schauflere114e472008-02-04 22:29:50 -08001834 */
David Howellsf1752ee2008-11-14 10:39:17 +11001835static void smack_cred_free(struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08001836{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001837 struct task_smack *tsp = smack_cred(cred);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001838 struct smack_rule *rp;
1839 struct list_head *l;
1840 struct list_head *n;
1841
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001842 smk_destroy_label_list(&tsp->smk_relabel);
1843
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001844 list_for_each_safe(l, n, &tsp->smk_rules) {
1845 rp = list_entry(l, struct smack_rule, list);
1846 list_del(&rp->list);
1847 kfree(rp);
1848 }
Casey Schauflere114e472008-02-04 22:29:50 -08001849}
1850
1851/**
David Howellsd84f4f92008-11-14 10:39:23 +11001852 * smack_cred_prepare - prepare new set of credentials for modification
1853 * @new: the new credentials
1854 * @old: the original credentials
1855 * @gfp: the atomicity of any memory allocations
1856 *
1857 * Prepare a new set of credentials for modification.
1858 */
1859static int smack_cred_prepare(struct cred *new, const struct cred *old,
1860 gfp_t gfp)
1861{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001862 struct task_smack *old_tsp = smack_cred(old);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001863 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001864 int rc;
Casey Schaufler676dac42010-12-02 06:43:39 -08001865
Casey Schauflerbbd36622018-11-12 09:30:56 -08001866 init_task_smack(new_tsp, old_tsp->smk_task, old_tsp->smk_task);
Himanshu Shuklab437aba2016-11-10 16:17:02 +05301867
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001868 rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
1869 if (rc != 0)
1870 return rc;
1871
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02001872 rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
1873 gfp);
Casey Schauflerbbd36622018-11-12 09:30:56 -08001874 return rc;
David Howellsd84f4f92008-11-14 10:39:23 +11001875}
1876
Randy Dunlap251a2a92009-02-18 11:42:33 -08001877/**
David Howellsee18d642009-09-02 09:14:21 +01001878 * smack_cred_transfer - Transfer the old credentials to the new credentials
1879 * @new: the new credentials
1880 * @old: the original credentials
1881 *
1882 * Fill in a set of blank credentials from another set of credentials.
1883 */
1884static void smack_cred_transfer(struct cred *new, const struct cred *old)
1885{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001886 struct task_smack *old_tsp = smack_cred(old);
1887 struct task_smack *new_tsp = smack_cred(new);
Casey Schaufler676dac42010-12-02 06:43:39 -08001888
1889 new_tsp->smk_task = old_tsp->smk_task;
1890 new_tsp->smk_forked = old_tsp->smk_task;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001891 mutex_init(&new_tsp->smk_rules_lock);
1892 INIT_LIST_HEAD(&new_tsp->smk_rules);
1893
Casey Schaufler7898e1f2011-01-17 08:05:27 -08001894 /* cbs copy rule list */
David Howellsee18d642009-09-02 09:14:21 +01001895}
1896
1897/**
Matthew Garrett3ec30112018-01-08 13:36:19 -08001898 * smack_cred_getsecid - get the secid corresponding to a creds structure
1899 * @c: the object creds
1900 * @secid: where to put the result
1901 *
1902 * Sets the secid to contain a u32 version of the smack label.
1903 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08001904static void smack_cred_getsecid(const struct cred *cred, u32 *secid)
Matthew Garrett3ec30112018-01-08 13:36:19 -08001905{
1906 struct smack_known *skp;
1907
1908 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -08001909 skp = smk_of_task(smack_cred(cred));
Matthew Garrett3ec30112018-01-08 13:36:19 -08001910 *secid = skp->smk_secid;
1911 rcu_read_unlock();
1912}
1913
1914/**
David Howells3a3b7ce2008-11-14 10:39:28 +11001915 * smack_kernel_act_as - Set the subjective context in a set of credentials
Randy Dunlap251a2a92009-02-18 11:42:33 -08001916 * @new: points to the set of credentials to be modified.
1917 * @secid: specifies the security ID to be set
David Howells3a3b7ce2008-11-14 10:39:28 +11001918 *
1919 * Set the security data for a kernel service.
1920 */
1921static int smack_kernel_act_as(struct cred *new, u32 secid)
1922{
Casey Schauflerb17103a2018-11-09 16:12:56 -08001923 struct task_smack *new_tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11001924
Casey Schaufler152f91d2016-11-14 09:38:15 -08001925 new_tsp->smk_task = smack_from_secid(secid);
David Howells3a3b7ce2008-11-14 10:39:28 +11001926 return 0;
1927}
1928
1929/**
1930 * smack_kernel_create_files_as - Set the file creation label in a set of creds
Randy Dunlap251a2a92009-02-18 11:42:33 -08001931 * @new: points to the set of credentials to be modified
1932 * @inode: points to the inode to use as a reference
David Howells3a3b7ce2008-11-14 10:39:28 +11001933 *
1934 * Set the file creation context in a set of credentials to the same
1935 * as the objective context of the specified inode
1936 */
1937static int smack_kernel_create_files_as(struct cred *new,
1938 struct inode *inode)
1939{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08001940 struct inode_smack *isp = smack_inode(inode);
Casey Schauflerb17103a2018-11-09 16:12:56 -08001941 struct task_smack *tsp = smack_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11001942
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001943 tsp->smk_forked = isp->smk_inode;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07001944 tsp->smk_task = tsp->smk_forked;
David Howells3a3b7ce2008-11-14 10:39:28 +11001945 return 0;
1946}
1947
1948/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02001949 * smk_curacc_on_task - helper to log task related access
1950 * @p: the task object
Casey Schaufler531f1d42011-09-19 12:41:42 -07001951 * @access: the access requested
1952 * @caller: name of the calling function for audit
Etienne Bassetecfcc532009-04-08 20:40:06 +02001953 *
1954 * Return 0 if access is permitted
1955 */
Casey Schaufler531f1d42011-09-19 12:41:42 -07001956static int smk_curacc_on_task(struct task_struct *p, int access,
1957 const char *caller)
Etienne Bassetecfcc532009-04-08 20:40:06 +02001958{
1959 struct smk_audit_info ad;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03001960 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07001961 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001962
Casey Schaufler531f1d42011-09-19 12:41:42 -07001963 smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
Etienne Bassetecfcc532009-04-08 20:40:06 +02001964 smk_ad_setfield_u_tsk(&ad, p);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02001965 rc = smk_curacc(skp, access, &ad);
Casey Schauflerd166c802014-08-27 14:51:27 -07001966 rc = smk_bu_task(p, access, rc);
1967 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02001968}
1969
1970/**
Casey Schauflere114e472008-02-04 22:29:50 -08001971 * smack_task_setpgid - Smack check on setting pgid
1972 * @p: the task object
1973 * @pgid: unused
1974 *
1975 * Return 0 if write access is permitted
1976 */
1977static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
1978{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001979 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001980}
1981
1982/**
1983 * smack_task_getpgid - Smack access check for getpgid
1984 * @p: the object task
1985 *
1986 * Returns 0 if current can read the object task, error code otherwise
1987 */
1988static int smack_task_getpgid(struct task_struct *p)
1989{
Casey Schaufler531f1d42011-09-19 12:41:42 -07001990 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08001991}
1992
1993/**
1994 * smack_task_getsid - Smack access check for getsid
1995 * @p: the object task
1996 *
1997 * Returns 0 if current can read the object task, error code otherwise
1998 */
1999static int smack_task_getsid(struct task_struct *p)
2000{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002001 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002002}
2003
2004/**
2005 * smack_task_getsecid - get the secid of the task
2006 * @p: the object task
2007 * @secid: where to put the result
2008 *
2009 * Sets the secid to contain a u32 version of the smack label.
2010 */
2011static void smack_task_getsecid(struct task_struct *p, u32 *secid)
2012{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002013 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002014
2015 *secid = skp->smk_secid;
Casey Schauflere114e472008-02-04 22:29:50 -08002016}
2017
2018/**
2019 * smack_task_setnice - Smack check on setting nice
2020 * @p: the task object
2021 * @nice: unused
2022 *
2023 * Return 0 if write access is permitted
2024 */
2025static int smack_task_setnice(struct task_struct *p, int nice)
2026{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002027 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002028}
2029
2030/**
2031 * smack_task_setioprio - Smack check on setting ioprio
2032 * @p: the task object
2033 * @ioprio: unused
2034 *
2035 * Return 0 if write access is permitted
2036 */
2037static int smack_task_setioprio(struct task_struct *p, int ioprio)
2038{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002039 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002040}
2041
2042/**
2043 * smack_task_getioprio - Smack check on reading ioprio
2044 * @p: the task object
2045 *
2046 * Return 0 if read access is permitted
2047 */
2048static int smack_task_getioprio(struct task_struct *p)
2049{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002050 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002051}
2052
2053/**
2054 * smack_task_setscheduler - Smack check on setting scheduler
2055 * @p: the task object
2056 * @policy: unused
2057 * @lp: unused
2058 *
2059 * Return 0 if read access is permitted
2060 */
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09002061static int smack_task_setscheduler(struct task_struct *p)
Casey Schauflere114e472008-02-04 22:29:50 -08002062{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002063 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002064}
2065
2066/**
2067 * smack_task_getscheduler - Smack check on reading scheduler
2068 * @p: the task object
2069 *
2070 * Return 0 if read access is permitted
2071 */
2072static int smack_task_getscheduler(struct task_struct *p)
2073{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002074 return smk_curacc_on_task(p, MAY_READ, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002075}
2076
2077/**
2078 * smack_task_movememory - Smack check on moving memory
2079 * @p: the task object
2080 *
2081 * Return 0 if write access is permitted
2082 */
2083static int smack_task_movememory(struct task_struct *p)
2084{
Casey Schaufler531f1d42011-09-19 12:41:42 -07002085 return smk_curacc_on_task(p, MAY_WRITE, __func__);
Casey Schauflere114e472008-02-04 22:29:50 -08002086}
2087
2088/**
2089 * smack_task_kill - Smack check on signal delivery
2090 * @p: the task object
2091 * @info: unused
2092 * @sig: unused
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002093 * @cred: identifies the cred to use in lieu of current's
Casey Schauflere114e472008-02-04 22:29:50 -08002094 *
2095 * Return 0 if write access is permitted
2096 *
Casey Schauflere114e472008-02-04 22:29:50 -08002097 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002098static int smack_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002099 int sig, const struct cred *cred)
Casey Schauflere114e472008-02-04 22:29:50 -08002100{
Etienne Bassetecfcc532009-04-08 20:40:06 +02002101 struct smk_audit_info ad;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002102 struct smack_known *skp;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002103 struct smack_known *tkp = smk_of_task_struct(p);
Casey Schauflerd166c802014-08-27 14:51:27 -07002104 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002105
Rafal Krypa18d872f2016-04-04 11:14:53 +02002106 if (!sig)
2107 return 0; /* null signal; existence test */
2108
Etienne Bassetecfcc532009-04-08 20:40:06 +02002109 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
2110 smk_ad_setfield_u_tsk(&ad, p);
Casey Schauflere114e472008-02-04 22:29:50 -08002111 /*
Casey Schauflere114e472008-02-04 22:29:50 -08002112 * Sending a signal requires that the sender
2113 * can write the receiver.
2114 */
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002115 if (cred == NULL) {
Casey Schauflerc60b9062016-08-30 10:31:39 -07002116 rc = smk_curacc(tkp, MAY_DELIVER, &ad);
2117 rc = smk_bu_task(p, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002118 return rc;
2119 }
Casey Schauflere114e472008-02-04 22:29:50 -08002120 /*
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04002121 * If the cred isn't NULL we're dealing with some USB IO
Casey Schauflere114e472008-02-04 22:29:50 -08002122 * specific behavior. This is not clean. For one thing
2123 * we can't take privilege into account.
2124 */
Casey Schauflerb17103a2018-11-09 16:12:56 -08002125 skp = smk_of_task(smack_cred(cred));
Casey Schauflerc60b9062016-08-30 10:31:39 -07002126 rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
2127 rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07002128 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002129}
2130
2131/**
Casey Schauflere114e472008-02-04 22:29:50 -08002132 * smack_task_to_inode - copy task smack into the inode blob
2133 * @p: task to copy from
Randy Dunlap251a2a92009-02-18 11:42:33 -08002134 * @inode: inode to copy to
Casey Schauflere114e472008-02-04 22:29:50 -08002135 *
2136 * Sets the smack pointer in the inode security blob
2137 */
2138static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
2139{
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002140 struct inode_smack *isp = smack_inode(inode);
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03002141 struct smack_known *skp = smk_of_task_struct(p);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002142
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002143 isp->smk_inode = skp;
Casey Schaufler7b4e8842018-06-22 10:54:45 -07002144 isp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002145}
2146
2147/*
2148 * Socket hooks.
2149 */
2150
2151/**
2152 * smack_sk_alloc_security - Allocate a socket blob
2153 * @sk: the socket
2154 * @family: unused
Randy Dunlap251a2a92009-02-18 11:42:33 -08002155 * @gfp_flags: memory allocation flags
Casey Schauflere114e472008-02-04 22:29:50 -08002156 *
2157 * Assign Smack pointers to current
2158 *
2159 * Returns 0 on success, -ENOMEM is there's no memory
2160 */
2161static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
2162{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002163 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002164 struct socket_smack *ssp;
2165
2166 ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
2167 if (ssp == NULL)
2168 return -ENOMEM;
2169
jooseong lee08382c92016-11-03 11:54:39 +01002170 /*
2171 * Sockets created by kernel threads receive web label.
2172 */
2173 if (unlikely(current->flags & PF_KTHREAD)) {
2174 ssp->smk_in = &smack_known_web;
2175 ssp->smk_out = &smack_known_web;
2176 } else {
2177 ssp->smk_in = skp;
2178 ssp->smk_out = skp;
2179 }
Casey Schaufler272cd7a2011-09-20 12:24:36 -07002180 ssp->smk_packet = NULL;
Casey Schauflere114e472008-02-04 22:29:50 -08002181
2182 sk->sk_security = ssp;
2183
2184 return 0;
2185}
2186
2187/**
2188 * smack_sk_free_security - Free a socket blob
2189 * @sk: the socket
2190 *
2191 * Clears the blob pointer
2192 */
2193static void smack_sk_free_security(struct sock *sk)
2194{
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302195#ifdef SMACK_IPV6_PORT_LABELING
2196 struct smk_port_label *spp;
2197
2198 if (sk->sk_family == PF_INET6) {
2199 rcu_read_lock();
2200 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2201 if (spp->smk_sock != sk)
2202 continue;
2203 spp->smk_can_reuse = 1;
2204 break;
2205 }
2206 rcu_read_unlock();
2207 }
2208#endif
Casey Schauflere114e472008-02-04 22:29:50 -08002209 kfree(sk->sk_security);
2210}
2211
2212/**
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002213* smack_ipv4host_label - check host based restrictions
Paul Moore07feee82009-03-27 17:10:54 -04002214* @sip: the object end
2215*
2216* looks for host based access restrictions
2217*
2218* This version will only be appropriate for really small sets of single label
2219* hosts. The caller is responsible for ensuring that the RCU read lock is
2220* taken before calling this function.
2221*
2222* Returns the label of the far end or NULL if it's not special.
2223*/
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002224static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
Paul Moore07feee82009-03-27 17:10:54 -04002225{
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002226 struct smk_net4addr *snp;
Paul Moore07feee82009-03-27 17:10:54 -04002227 struct in_addr *siap = &sip->sin_addr;
2228
2229 if (siap->s_addr == 0)
2230 return NULL;
2231
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002232 list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
2233 /*
2234 * we break after finding the first match because
2235 * the list is sorted from longest to shortest mask
2236 * so we have found the most specific match
2237 */
2238 if (snp->smk_host.s_addr ==
2239 (siap->s_addr & snp->smk_mask.s_addr))
2240 return snp->smk_label;
2241
2242 return NULL;
2243}
2244
2245#if IS_ENABLED(CONFIG_IPV6)
2246/*
2247 * smk_ipv6_localhost - Check for local ipv6 host address
2248 * @sip: the address
2249 *
2250 * Returns boolean true if this is the localhost address
2251 */
2252static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
2253{
2254 __be16 *be16p = (__be16 *)&sip->sin6_addr;
2255 __be32 *be32p = (__be32 *)&sip->sin6_addr;
2256
2257 if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
2258 ntohs(be16p[7]) == 1)
2259 return true;
2260 return false;
2261}
2262
2263/**
2264* smack_ipv6host_label - check host based restrictions
2265* @sip: the object end
2266*
2267* looks for host based access restrictions
2268*
2269* This version will only be appropriate for really small sets of single label
2270* hosts. The caller is responsible for ensuring that the RCU read lock is
2271* taken before calling this function.
2272*
2273* Returns the label of the far end or NULL if it's not special.
2274*/
2275static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
2276{
2277 struct smk_net6addr *snp;
2278 struct in6_addr *sap = &sip->sin6_addr;
2279 int i;
2280 int found = 0;
2281
2282 /*
2283 * It's local. Don't look for a host label.
2284 */
2285 if (smk_ipv6_localhost(sip))
2286 return NULL;
2287
2288 list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
Paul Moore07feee82009-03-27 17:10:54 -04002289 /*
Casey Schaufler2e4939f2016-11-07 19:01:09 -08002290 * If the label is NULL the entry has
2291 * been renounced. Ignore it.
2292 */
2293 if (snp->smk_label == NULL)
2294 continue;
2295 /*
Paul Moore07feee82009-03-27 17:10:54 -04002296 * we break after finding the first match because
2297 * the list is sorted from longest to shortest mask
2298 * so we have found the most specific match
2299 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002300 for (found = 1, i = 0; i < 8; i++) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002301 if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
2302 snp->smk_host.s6_addr16[i]) {
2303 found = 0;
2304 break;
2305 }
Etienne Basset43031542009-03-27 17:11:01 -04002306 }
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002307 if (found)
2308 return snp->smk_label;
2309 }
Paul Moore07feee82009-03-27 17:10:54 -04002310
2311 return NULL;
2312}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002313#endif /* CONFIG_IPV6 */
Paul Moore07feee82009-03-27 17:10:54 -04002314
2315/**
Casey Schauflere114e472008-02-04 22:29:50 -08002316 * smack_netlabel - Set the secattr on a socket
2317 * @sk: the socket
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002318 * @labeled: socket label scheme
Casey Schauflere114e472008-02-04 22:29:50 -08002319 *
2320 * Convert the outbound smack value (smk_out) to a
2321 * secattr and attach it to the socket.
2322 *
2323 * Returns 0 on success or an error code
2324 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002325static int smack_netlabel(struct sock *sk, int labeled)
Casey Schauflere114e472008-02-04 22:29:50 -08002326{
Casey Schauflerf7112e62012-05-06 15:22:02 -07002327 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002328 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002329 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002330
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002331 /*
2332 * Usually the netlabel code will handle changing the
2333 * packet labeling based on the label.
2334 * The case of a single label host is different, because
2335 * a single label host should never get a labeled packet
2336 * even though the label is usually associated with a packet
2337 * label.
2338 */
2339 local_bh_disable();
2340 bh_lock_sock_nested(sk);
2341
2342 if (ssp->smk_out == smack_net_ambient ||
2343 labeled == SMACK_UNLABELED_SOCKET)
2344 netlbl_sock_delattr(sk);
2345 else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002346 skp = ssp->smk_out;
Casey Schauflerf7112e62012-05-06 15:22:02 -07002347 rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002348 }
2349
2350 bh_unlock_sock(sk);
2351 local_bh_enable();
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002352
Casey Schauflere114e472008-02-04 22:29:50 -08002353 return rc;
2354}
2355
2356/**
Paul Moore07feee82009-03-27 17:10:54 -04002357 * smack_netlbel_send - Set the secattr on a socket and perform access checks
2358 * @sk: the socket
2359 * @sap: the destination address
2360 *
2361 * Set the correct secattr for the given socket based on the destination
2362 * address and perform any outbound access checks needed.
2363 *
2364 * Returns 0 on success or an error code.
2365 *
2366 */
2367static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
2368{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002369 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04002370 int rc;
2371 int sk_lbl;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002372 struct smack_known *hkp;
Paul Moore07feee82009-03-27 17:10:54 -04002373 struct socket_smack *ssp = sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002374 struct smk_audit_info ad;
Paul Moore07feee82009-03-27 17:10:54 -04002375
2376 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002377 hkp = smack_ipv4host_label(sap);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002378 if (hkp != NULL) {
Etienne Bassetecfcc532009-04-08 20:40:06 +02002379#ifdef CONFIG_AUDIT
Kees Cook923e9a12012-04-10 13:26:44 -07002380 struct lsm_network_audit net;
2381
Eric Paris48c62af2012-04-02 13:15:44 -04002382 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2383 ad.a.u.net->family = sap->sin_family;
2384 ad.a.u.net->dport = sap->sin_port;
2385 ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002386#endif
Kees Cook923e9a12012-04-10 13:26:44 -07002387 sk_lbl = SMACK_UNLABELED_SOCKET;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002388 skp = ssp->smk_out;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002389 rc = smk_access(skp, hkp, MAY_WRITE, &ad);
2390 rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04002391 } else {
2392 sk_lbl = SMACK_CIPSO_SOCKET;
2393 rc = 0;
2394 }
2395 rcu_read_unlock();
2396 if (rc != 0)
2397 return rc;
2398
2399 return smack_netlabel(sk, sk_lbl);
2400}
2401
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002402#if IS_ENABLED(CONFIG_IPV6)
2403/**
2404 * smk_ipv6_check - check Smack access
2405 * @subject: subject Smack label
2406 * @object: object Smack label
2407 * @address: address
2408 * @act: the action being taken
2409 *
2410 * Check an IPv6 access
2411 */
2412static int smk_ipv6_check(struct smack_known *subject,
2413 struct smack_known *object,
2414 struct sockaddr_in6 *address, int act)
2415{
2416#ifdef CONFIG_AUDIT
2417 struct lsm_network_audit net;
2418#endif
2419 struct smk_audit_info ad;
2420 int rc;
2421
2422#ifdef CONFIG_AUDIT
2423 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
2424 ad.a.u.net->family = PF_INET6;
2425 ad.a.u.net->dport = ntohs(address->sin6_port);
2426 if (act == SMK_RECEIVING)
2427 ad.a.u.net->v6info.saddr = address->sin6_addr;
2428 else
2429 ad.a.u.net->v6info.daddr = address->sin6_addr;
2430#endif
2431 rc = smk_access(subject, object, MAY_WRITE, &ad);
2432 rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
2433 return rc;
2434}
2435#endif /* CONFIG_IPV6 */
2436
2437#ifdef SMACK_IPV6_PORT_LABELING
Paul Moore07feee82009-03-27 17:10:54 -04002438/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002439 * smk_ipv6_port_label - Smack port access table management
2440 * @sock: socket
2441 * @address: address
2442 *
2443 * Create or update the port list entry
2444 */
2445static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
2446{
2447 struct sock *sk = sock->sk;
2448 struct sockaddr_in6 *addr6;
2449 struct socket_smack *ssp = sock->sk->sk_security;
2450 struct smk_port_label *spp;
2451 unsigned short port = 0;
2452
2453 if (address == NULL) {
2454 /*
2455 * This operation is changing the Smack information
2456 * on the bound socket. Take the changes to the port
2457 * as well.
2458 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302459 rcu_read_lock();
2460 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Casey Schauflerc6739442013-05-22 18:42:56 -07002461 if (sk != spp->smk_sock)
2462 continue;
2463 spp->smk_in = ssp->smk_in;
2464 spp->smk_out = ssp->smk_out;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302465 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002466 return;
2467 }
2468 /*
2469 * A NULL address is only used for updating existing
2470 * bound entries. If there isn't one, it's OK.
2471 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302472 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002473 return;
2474 }
2475
2476 addr6 = (struct sockaddr_in6 *)address;
2477 port = ntohs(addr6->sin6_port);
2478 /*
2479 * This is a special case that is safely ignored.
2480 */
2481 if (port == 0)
2482 return;
2483
2484 /*
2485 * Look for an existing port list entry.
2486 * This is an indication that a port is getting reused.
2487 */
Vishal Goel3c7ce342016-11-23 10:31:08 +05302488 rcu_read_lock();
2489 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302490 if (spp->smk_port != port || spp->smk_sock_type != sock->type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002491 continue;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302492 if (spp->smk_can_reuse != 1) {
2493 rcu_read_unlock();
2494 return;
2495 }
Casey Schauflerc6739442013-05-22 18:42:56 -07002496 spp->smk_port = port;
2497 spp->smk_sock = sk;
2498 spp->smk_in = ssp->smk_in;
2499 spp->smk_out = ssp->smk_out;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302500 spp->smk_can_reuse = 0;
Vishal Goel3c7ce342016-11-23 10:31:08 +05302501 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002502 return;
2503 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302504 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002505 /*
2506 * A new port entry is required.
2507 */
2508 spp = kzalloc(sizeof(*spp), GFP_KERNEL);
2509 if (spp == NULL)
2510 return;
2511
2512 spp->smk_port = port;
2513 spp->smk_sock = sk;
2514 spp->smk_in = ssp->smk_in;
2515 spp->smk_out = ssp->smk_out;
Vishal Goel9d44c972016-11-23 10:31:59 +05302516 spp->smk_sock_type = sock->type;
Vishal Goel0c96d1f2016-11-23 10:32:54 +05302517 spp->smk_can_reuse = 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002518
Vishal Goel3c7ce342016-11-23 10:31:08 +05302519 mutex_lock(&smack_ipv6_lock);
2520 list_add_rcu(&spp->list, &smk_ipv6_port_list);
2521 mutex_unlock(&smack_ipv6_lock);
Casey Schauflerc6739442013-05-22 18:42:56 -07002522 return;
2523}
2524
2525/**
2526 * smk_ipv6_port_check - check Smack port access
2527 * @sock: socket
2528 * @address: address
2529 *
2530 * Create or update the port list entry
2531 */
Casey Schaufler6ea06242013-08-05 13:21:22 -07002532static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
Casey Schauflerc6739442013-05-22 18:42:56 -07002533 int act)
2534{
Casey Schauflerc6739442013-05-22 18:42:56 -07002535 struct smk_port_label *spp;
2536 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002537 struct smack_known *skp = NULL;
2538 unsigned short port;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002539 struct smack_known *object;
Casey Schauflerc6739442013-05-22 18:42:56 -07002540
2541 if (act == SMK_RECEIVING) {
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002542 skp = smack_ipv6host_label(address);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002543 object = ssp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002544 } else {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002545 skp = ssp->smk_out;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002546 object = smack_ipv6host_label(address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002547 }
2548
2549 /*
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002550 * The other end is a single label host.
Casey Schauflerc6739442013-05-22 18:42:56 -07002551 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002552 if (skp != NULL && object != NULL)
2553 return smk_ipv6_check(skp, object, address, act);
2554 if (skp == NULL)
2555 skp = smack_net_ambient;
2556 if (object == NULL)
2557 object = smack_net_ambient;
Casey Schauflerc6739442013-05-22 18:42:56 -07002558
2559 /*
2560 * It's remote, so port lookup does no good.
2561 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002562 if (!smk_ipv6_localhost(address))
2563 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002564
2565 /*
2566 * It's local so the send check has to have passed.
2567 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002568 if (act == SMK_RECEIVING)
2569 return 0;
Casey Schauflerc6739442013-05-22 18:42:56 -07002570
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002571 port = ntohs(address->sin6_port);
Vishal Goel3c7ce342016-11-23 10:31:08 +05302572 rcu_read_lock();
2573 list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
Vishal Goel9d44c972016-11-23 10:31:59 +05302574 if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
Casey Schauflerc6739442013-05-22 18:42:56 -07002575 continue;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002576 object = spp->smk_in;
Casey Schauflerc6739442013-05-22 18:42:56 -07002577 if (act == SMK_CONNECTING)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002578 ssp->smk_packet = spp->smk_out;
Casey Schauflerc6739442013-05-22 18:42:56 -07002579 break;
2580 }
Vishal Goel3c7ce342016-11-23 10:31:08 +05302581 rcu_read_unlock();
Casey Schauflerc6739442013-05-22 18:42:56 -07002582
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002583 return smk_ipv6_check(skp, object, address, act);
Casey Schauflerc6739442013-05-22 18:42:56 -07002584}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002585#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002586
2587/**
Casey Schauflere114e472008-02-04 22:29:50 -08002588 * smack_inode_setsecurity - set smack xattrs
2589 * @inode: the object
2590 * @name: attribute name
2591 * @value: attribute value
2592 * @size: size of the attribute
2593 * @flags: unused
2594 *
2595 * Sets the named attribute in the appropriate blob
2596 *
2597 * Returns 0 on success, or an error code
2598 */
2599static int smack_inode_setsecurity(struct inode *inode, const char *name,
2600 const void *value, size_t size, int flags)
2601{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002602 struct smack_known *skp;
Casey Schauflerfb4021b2018-11-12 12:43:01 -08002603 struct inode_smack *nsp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08002604 struct socket_smack *ssp;
2605 struct socket *sock;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08002606 int rc = 0;
Casey Schauflere114e472008-02-04 22:29:50 -08002607
Casey Schauflerf7112e62012-05-06 15:22:02 -07002608 if (value == NULL || size > SMK_LONGLABEL || size == 0)
Pankaj Kumar5e9ab592013-12-13 15:12:22 +05302609 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08002610
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002611 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02002612 if (IS_ERR(skp))
2613 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08002614
2615 if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02002616 nsp->smk_inode = skp;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002617 nsp->smk_flags |= SMK_INODE_INSTANT;
Casey Schauflere114e472008-02-04 22:29:50 -08002618 return 0;
2619 }
2620 /*
2621 * The rest of the Smack xattrs are only on sockets.
2622 */
2623 if (inode->i_sb->s_magic != SOCKFS_MAGIC)
2624 return -EOPNOTSUPP;
2625
2626 sock = SOCKET_I(inode);
Ahmed S. Darwish2e1d1462008-02-13 15:03:34 -08002627 if (sock == NULL || sock->sk == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08002628 return -EOPNOTSUPP;
2629
2630 ssp = sock->sk->sk_security;
2631
2632 if (strcmp(name, XATTR_SMACK_IPIN) == 0)
Casey Schaufler54e70ec2014-04-10 16:37:08 -07002633 ssp->smk_in = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08002634 else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002635 ssp->smk_out = skp;
Casey Schauflerc6739442013-05-22 18:42:56 -07002636 if (sock->sk->sk_family == PF_INET) {
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08002637 rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2638 if (rc != 0)
2639 printk(KERN_WARNING
2640 "Smack: \"%s\" netlbl error %d.\n",
2641 __func__, -rc);
2642 }
Casey Schauflere114e472008-02-04 22:29:50 -08002643 } else
2644 return -EOPNOTSUPP;
2645
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002646#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07002647 if (sock->sk->sk_family == PF_INET6)
2648 smk_ipv6_port_label(sock, NULL);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002649#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002650
Casey Schauflere114e472008-02-04 22:29:50 -08002651 return 0;
2652}
2653
2654/**
2655 * smack_socket_post_create - finish socket setup
2656 * @sock: the socket
2657 * @family: protocol family
2658 * @type: unused
2659 * @protocol: unused
2660 * @kern: unused
2661 *
2662 * Sets the netlabel information on the socket
2663 *
2664 * Returns 0 on success, and error code otherwise
2665 */
2666static int smack_socket_post_create(struct socket *sock, int family,
2667 int type, int protocol, int kern)
2668{
Marcin Lis74123012015-01-22 15:40:33 +01002669 struct socket_smack *ssp;
2670
2671 if (sock->sk == NULL)
2672 return 0;
2673
2674 /*
2675 * Sockets created by kernel threads receive web label.
2676 */
2677 if (unlikely(current->flags & PF_KTHREAD)) {
2678 ssp = sock->sk->sk_security;
2679 ssp->smk_in = &smack_known_web;
2680 ssp->smk_out = &smack_known_web;
2681 }
2682
2683 if (family != PF_INET)
Casey Schauflere114e472008-02-04 22:29:50 -08002684 return 0;
2685 /*
2686 * Set the outbound netlbl.
2687 */
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002688 return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
2689}
2690
Tom Gundersen5859cdf2018-05-04 16:28:22 +02002691/**
2692 * smack_socket_socketpair - create socket pair
2693 * @socka: one socket
2694 * @sockb: another socket
2695 *
2696 * Cross reference the peer labels for SO_PEERSEC
2697 *
2698 * Returns 0 on success, and error code otherwise
2699 */
2700static int smack_socket_socketpair(struct socket *socka,
2701 struct socket *sockb)
2702{
2703 struct socket_smack *asp = socka->sk->sk_security;
2704 struct socket_smack *bsp = sockb->sk->sk_security;
2705
2706 asp->smk_packet = bsp->smk_out;
2707 bsp->smk_packet = asp->smk_out;
2708
2709 return 0;
2710}
2711
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002712#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002713/**
Casey Schauflerc6739442013-05-22 18:42:56 -07002714 * smack_socket_bind - record port binding information.
2715 * @sock: the socket
2716 * @address: the port address
2717 * @addrlen: size of the address
2718 *
2719 * Records the label bound to a port.
2720 *
2721 * Returns 0
2722 */
2723static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
2724 int addrlen)
2725{
2726 if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
2727 smk_ipv6_port_label(sock, address);
Casey Schauflerc6739442013-05-22 18:42:56 -07002728 return 0;
2729}
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002730#endif /* SMACK_IPV6_PORT_LABELING */
Casey Schauflerc6739442013-05-22 18:42:56 -07002731
2732/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002733 * smack_socket_connect - connect access check
2734 * @sock: the socket
2735 * @sap: the other end
2736 * @addrlen: size of sap
2737 *
2738 * Verifies that a connection may be possible
2739 *
2740 * Returns 0 on success, and error code otherwise
2741 */
2742static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
2743 int addrlen)
2744{
Casey Schauflerc6739442013-05-22 18:42:56 -07002745 int rc = 0;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002746#if IS_ENABLED(CONFIG_IPV6)
2747 struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
2748#endif
2749#ifdef SMACK_IPV6_SECMARK_LABELING
2750 struct smack_known *rsp;
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002751 struct socket_smack *ssp;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002752#endif
Casey Schaufler6d3dc072008-12-31 12:54:12 -05002753
Casey Schauflerc6739442013-05-22 18:42:56 -07002754 if (sock->sk == NULL)
2755 return 0;
2756
Vasyl Gomonovychda49b5d2017-12-21 16:57:52 +01002757#ifdef SMACK_IPV6_SECMARK_LABELING
2758 ssp = sock->sk->sk_security;
2759#endif
2760
Casey Schauflerc6739442013-05-22 18:42:56 -07002761 switch (sock->sk->sk_family) {
2762 case PF_INET:
2763 if (addrlen < sizeof(struct sockaddr_in))
2764 return -EINVAL;
2765 rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
2766 break;
2767 case PF_INET6:
2768 if (addrlen < sizeof(struct sockaddr_in6))
2769 return -EINVAL;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002770#ifdef SMACK_IPV6_SECMARK_LABELING
2771 rsp = smack_ipv6host_label(sip);
2772 if (rsp != NULL)
2773 rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
Casey Schaufler6ea06242013-08-05 13:21:22 -07002774 SMK_CONNECTING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07002775#endif
2776#ifdef SMACK_IPV6_PORT_LABELING
2777 rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
2778#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07002779 break;
2780 }
2781 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08002782}
2783
2784/**
2785 * smack_flags_to_may - convert S_ to MAY_ values
2786 * @flags: the S_ value
2787 *
2788 * Returns the equivalent MAY_ value
2789 */
2790static int smack_flags_to_may(int flags)
2791{
2792 int may = 0;
2793
2794 if (flags & S_IRUGO)
2795 may |= MAY_READ;
2796 if (flags & S_IWUGO)
2797 may |= MAY_WRITE;
2798 if (flags & S_IXUGO)
2799 may |= MAY_EXEC;
2800
2801 return may;
2802}
2803
2804/**
2805 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
2806 * @msg: the object
2807 *
2808 * Returns 0
2809 */
2810static int smack_msg_msg_alloc_security(struct msg_msg *msg)
2811{
Casey Schauflerecd5f822018-11-20 11:55:02 -08002812 struct smack_known **blob = smack_msg_msg(msg);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07002813
Casey Schauflerecd5f822018-11-20 11:55:02 -08002814 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002815 return 0;
2816}
2817
2818/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002819 * smack_of_ipc - the smack pointer for the ipc
2820 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002821 *
2822 * Returns a pointer to the smack value
2823 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002824static struct smack_known *smack_of_ipc(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002825{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002826 struct smack_known **blob = smack_ipc(isp);
2827
2828 return *blob;
Casey Schauflere114e472008-02-04 22:29:50 -08002829}
2830
2831/**
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002832 * smack_ipc_alloc_security - Set the security blob for ipc
2833 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002834 *
2835 * Returns 0
2836 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002837static int smack_ipc_alloc_security(struct kern_ipc_perm *isp)
Casey Schauflere114e472008-02-04 22:29:50 -08002838{
Casey Schaufler019bcca2018-09-21 17:19:54 -07002839 struct smack_known **blob = smack_ipc(isp);
Casey Schauflere114e472008-02-04 22:29:50 -08002840
Casey Schaufler019bcca2018-09-21 17:19:54 -07002841 *blob = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08002842 return 0;
2843}
2844
2845/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002846 * smk_curacc_shm : check if current has access on shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002847 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002848 * @access : access requested
2849 *
2850 * Returns 0 if current has the requested access, error code otherwise
2851 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002852static int smk_curacc_shm(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002853{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002854 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002855 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002856 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002857
2858#ifdef CONFIG_AUDIT
2859 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002860 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002861#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002862 rc = smk_curacc(ssp, access, &ad);
2863 rc = smk_bu_current("shm", ssp, access, rc);
2864 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002865}
2866
2867/**
Casey Schauflere114e472008-02-04 22:29:50 -08002868 * smack_shm_associate - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002869 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002870 * @shmflg: access requested
2871 *
2872 * Returns 0 if current has the requested access, error code otherwise
2873 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002874static int smack_shm_associate(struct kern_ipc_perm *isp, int shmflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002875{
Casey Schauflere114e472008-02-04 22:29:50 -08002876 int may;
2877
2878 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002879 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002880}
2881
2882/**
2883 * smack_shm_shmctl - Smack access check for shm
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002884 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002885 * @cmd: what it wants to do
2886 *
2887 * Returns 0 if current has the requested access, error code otherwise
2888 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002889static int smack_shm_shmctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002890{
Casey Schauflere114e472008-02-04 22:29:50 -08002891 int may;
2892
2893 switch (cmd) {
2894 case IPC_STAT:
2895 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07002896 case SHM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002897 may = MAY_READ;
2898 break;
2899 case IPC_SET:
2900 case SHM_LOCK:
2901 case SHM_UNLOCK:
2902 case IPC_RMID:
2903 may = MAY_READWRITE;
2904 break;
2905 case IPC_INFO:
2906 case SHM_INFO:
2907 /*
2908 * System level information.
2909 */
2910 return 0;
2911 default:
2912 return -EINVAL;
2913 }
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002914 return smk_curacc_shm(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002915}
2916
2917/**
2918 * smack_shm_shmat - Smack access for shmat
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002919 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002920 * @shmaddr: unused
2921 * @shmflg: access requested
2922 *
2923 * Returns 0 if current has the requested access, error code otherwise
2924 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002925static int smack_shm_shmat(struct kern_ipc_perm *ipc, char __user *shmaddr,
Casey Schauflere114e472008-02-04 22:29:50 -08002926 int shmflg)
2927{
Casey Schauflere114e472008-02-04 22:29:50 -08002928 int may;
2929
2930 may = smack_flags_to_may(shmflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002931 return smk_curacc_shm(ipc, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002932}
2933
2934/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02002935 * smk_curacc_sem : check if current has access on sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002936 * @isp : the object
Etienne Bassetecfcc532009-04-08 20:40:06 +02002937 * @access : access requested
2938 *
2939 * Returns 0 if current has the requested access, error code otherwise
2940 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002941static int smk_curacc_sem(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02002942{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002943 struct smack_known *ssp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02002944 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07002945 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002946
2947#ifdef CONFIG_AUDIT
2948 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002949 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002950#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07002951 rc = smk_curacc(ssp, access, &ad);
2952 rc = smk_bu_current("sem", ssp, access, rc);
2953 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02002954}
2955
2956/**
Casey Schauflere114e472008-02-04 22:29:50 -08002957 * smack_sem_associate - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002958 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002959 * @semflg: access requested
2960 *
2961 * Returns 0 if current has the requested access, error code otherwise
2962 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002963static int smack_sem_associate(struct kern_ipc_perm *isp, int semflg)
Casey Schauflere114e472008-02-04 22:29:50 -08002964{
Casey Schauflere114e472008-02-04 22:29:50 -08002965 int may;
2966
2967 may = smack_flags_to_may(semflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002968 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08002969}
2970
2971/**
2972 * smack_sem_shmctl - Smack access check for sem
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002973 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08002974 * @cmd: what it wants to do
2975 *
2976 * Returns 0 if current has the requested access, error code otherwise
2977 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05002978static int smack_sem_semctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08002979{
Casey Schauflere114e472008-02-04 22:29:50 -08002980 int may;
2981
2982 switch (cmd) {
2983 case GETPID:
2984 case GETNCNT:
2985 case GETZCNT:
2986 case GETVAL:
2987 case GETALL:
2988 case IPC_STAT:
2989 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07002990 case SEM_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08002991 may = MAY_READ;
2992 break;
2993 case SETVAL:
2994 case SETALL:
2995 case IPC_RMID:
2996 case IPC_SET:
2997 may = MAY_READWRITE;
2998 break;
2999 case IPC_INFO:
3000 case SEM_INFO:
3001 /*
3002 * System level information
3003 */
3004 return 0;
3005 default:
3006 return -EINVAL;
3007 }
3008
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003009 return smk_curacc_sem(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003010}
3011
3012/**
3013 * smack_sem_semop - Smack checks of semaphore operations
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003014 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003015 * @sops: unused
3016 * @nsops: unused
3017 * @alter: unused
3018 *
3019 * Treated as read and write in all cases.
3020 *
3021 * Returns 0 if access is allowed, error code otherwise
3022 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003023static int smack_sem_semop(struct kern_ipc_perm *isp, struct sembuf *sops,
Casey Schauflere114e472008-02-04 22:29:50 -08003024 unsigned nsops, int alter)
3025{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003026 return smk_curacc_sem(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003027}
3028
3029/**
Etienne Bassetecfcc532009-04-08 20:40:06 +02003030 * smk_curacc_msq : helper to check if current has access on msq
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003031 * @isp : the msq
Etienne Bassetecfcc532009-04-08 20:40:06 +02003032 * @access : access requested
3033 *
3034 * return 0 if current has access, error otherwise
3035 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003036static int smk_curacc_msq(struct kern_ipc_perm *isp, int access)
Etienne Bassetecfcc532009-04-08 20:40:06 +02003037{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003038 struct smack_known *msp = smack_of_ipc(isp);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003039 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003040 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003041
3042#ifdef CONFIG_AUDIT
3043 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003044 ad.a.u.ipc_id = isp->id;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003045#endif
Casey Schauflerd166c802014-08-27 14:51:27 -07003046 rc = smk_curacc(msp, access, &ad);
3047 rc = smk_bu_current("msq", msp, access, rc);
3048 return rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003049}
3050
3051/**
Casey Schauflere114e472008-02-04 22:29:50 -08003052 * smack_msg_queue_associate - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003053 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003054 * @msqflg: access requested
3055 *
3056 * Returns 0 if current has the requested access, error code otherwise
3057 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003058static int smack_msg_queue_associate(struct kern_ipc_perm *isp, int msqflg)
Casey Schauflere114e472008-02-04 22:29:50 -08003059{
Casey Schauflere114e472008-02-04 22:29:50 -08003060 int may;
3061
3062 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003063 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003064}
3065
3066/**
3067 * smack_msg_queue_msgctl - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003068 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003069 * @cmd: what it wants to do
3070 *
3071 * Returns 0 if current has the requested access, error code otherwise
3072 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003073static int smack_msg_queue_msgctl(struct kern_ipc_perm *isp, int cmd)
Casey Schauflere114e472008-02-04 22:29:50 -08003074{
Casey Schauflere114e472008-02-04 22:29:50 -08003075 int may;
3076
3077 switch (cmd) {
3078 case IPC_STAT:
3079 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07003080 case MSG_STAT_ANY:
Casey Schauflere114e472008-02-04 22:29:50 -08003081 may = MAY_READ;
3082 break;
3083 case IPC_SET:
3084 case IPC_RMID:
3085 may = MAY_READWRITE;
3086 break;
3087 case IPC_INFO:
3088 case MSG_INFO:
3089 /*
3090 * System level information
3091 */
3092 return 0;
3093 default:
3094 return -EINVAL;
3095 }
3096
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003097 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003098}
3099
3100/**
3101 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003102 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003103 * @msg: unused
3104 * @msqflg: access requested
3105 *
3106 * Returns 0 if current has the requested access, error code otherwise
3107 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003108static int smack_msg_queue_msgsnd(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003109 int msqflg)
3110{
Etienne Bassetecfcc532009-04-08 20:40:06 +02003111 int may;
Casey Schauflere114e472008-02-04 22:29:50 -08003112
Etienne Bassetecfcc532009-04-08 20:40:06 +02003113 may = smack_flags_to_may(msqflg);
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003114 return smk_curacc_msq(isp, may);
Casey Schauflere114e472008-02-04 22:29:50 -08003115}
3116
3117/**
3118 * smack_msg_queue_msgsnd - Smack access check for msg_queue
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003119 * @isp: the object
Casey Schauflere114e472008-02-04 22:29:50 -08003120 * @msg: unused
3121 * @target: unused
3122 * @type: unused
3123 * @mode: unused
3124 *
3125 * Returns 0 if current has read and write access, error code otherwise
3126 */
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003127static int smack_msg_queue_msgrcv(struct kern_ipc_perm *isp, struct msg_msg *msg,
Casey Schauflere114e472008-02-04 22:29:50 -08003128 struct task_struct *target, long type, int mode)
3129{
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05003130 return smk_curacc_msq(isp, MAY_READWRITE);
Casey Schauflere114e472008-02-04 22:29:50 -08003131}
3132
3133/**
3134 * smack_ipc_permission - Smack access for ipc_permission()
3135 * @ipp: the object permissions
3136 * @flag: access requested
3137 *
3138 * Returns 0 if current has read and write access, error code otherwise
3139 */
3140static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
3141{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003142 struct smack_known **blob = smack_ipc(ipp);
3143 struct smack_known *iskp = *blob;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003144 int may = smack_flags_to_may(flag);
3145 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003146 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003147
Etienne Bassetecfcc532009-04-08 20:40:06 +02003148#ifdef CONFIG_AUDIT
3149 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
3150 ad.a.u.ipc_id = ipp->id;
3151#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003152 rc = smk_curacc(iskp, may, &ad);
3153 rc = smk_bu_current("svipc", iskp, may, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003154 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003155}
3156
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003157/**
3158 * smack_ipc_getsecid - Extract smack security id
Randy Dunlap251a2a92009-02-18 11:42:33 -08003159 * @ipp: the object permissions
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003160 * @secid: where result will be saved
3161 */
3162static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
3163{
Casey Schaufler019bcca2018-09-21 17:19:54 -07003164 struct smack_known **blob = smack_ipc(ipp);
3165 struct smack_known *iskp = *blob;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003166
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003167 *secid = iskp->smk_secid;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10003168}
3169
Casey Schauflere114e472008-02-04 22:29:50 -08003170/**
3171 * smack_d_instantiate - Make sure the blob is correct on an inode
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003172 * @opt_dentry: dentry where inode will be attached
Casey Schauflere114e472008-02-04 22:29:50 -08003173 * @inode: the object
3174 *
3175 * Set the inode's security blob if it hasn't been done already.
3176 */
3177static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
3178{
3179 struct super_block *sbp;
3180 struct superblock_smack *sbsp;
3181 struct inode_smack *isp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003182 struct smack_known *skp;
3183 struct smack_known *ckp = smk_of_current();
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003184 struct smack_known *final;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003185 char trattr[TRANS_TRUE_SIZE];
3186 int transflag = 0;
Casey Schaufler2267b132012-03-13 19:14:19 -07003187 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003188 struct dentry *dp;
3189
3190 if (inode == NULL)
3191 return;
3192
Casey Schauflerfb4021b2018-11-12 12:43:01 -08003193 isp = smack_inode(inode);
Casey Schauflere114e472008-02-04 22:29:50 -08003194
3195 mutex_lock(&isp->smk_lock);
3196 /*
3197 * If the inode is already instantiated
3198 * take the quick way out
3199 */
3200 if (isp->smk_flags & SMK_INODE_INSTANT)
3201 goto unlockandout;
3202
3203 sbp = inode->i_sb;
3204 sbsp = sbp->s_security;
3205 /*
3206 * We're going to use the superblock default label
3207 * if there's no label on the file.
3208 */
3209 final = sbsp->smk_default;
3210
3211 /*
Casey Schauflere97dcb02008-06-02 10:04:32 -07003212 * If this is the root inode the superblock
3213 * may be in the process of initialization.
3214 * If that is the case use the root value out
3215 * of the superblock.
3216 */
3217 if (opt_dentry->d_parent == opt_dentry) {
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003218 switch (sbp->s_magic) {
3219 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003220 case CGROUP2_SUPER_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003221 /*
3222 * The cgroup filesystem is never mounted,
3223 * so there's no opportunity to set the mount
3224 * options.
3225 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003226 sbsp->smk_root = &smack_known_star;
3227 sbsp->smk_default = &smack_known_star;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003228 isp->smk_inode = sbsp->smk_root;
3229 break;
3230 case TMPFS_MAGIC:
3231 /*
3232 * What about shmem/tmpfs anonymous files with dentry
3233 * obtained from d_alloc_pseudo()?
3234 */
3235 isp->smk_inode = smk_of_current();
3236 break;
Roman Kubiak8da4aba2015-10-05 12:27:16 +02003237 case PIPEFS_MAGIC:
3238 isp->smk_inode = smk_of_current();
3239 break;
Rafal Krypa805b65a2016-12-09 14:03:04 +01003240 case SOCKFS_MAGIC:
3241 /*
3242 * Socket access is controlled by the socket
3243 * structures associated with the task involved.
3244 */
3245 isp->smk_inode = &smack_known_star;
3246 break;
Łukasz Stelmach1d8c2322014-12-16 16:53:08 +01003247 default:
3248 isp->smk_inode = sbsp->smk_root;
3249 break;
Casey Schaufler36ea7352014-04-28 15:23:01 -07003250 }
Casey Schauflere97dcb02008-06-02 10:04:32 -07003251 isp->smk_flags |= SMK_INODE_INSTANT;
3252 goto unlockandout;
3253 }
3254
3255 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003256 * This is pretty hackish.
3257 * Casey says that we shouldn't have to do
3258 * file system specific code, but it does help
3259 * with keeping it simple.
3260 */
3261 switch (sbp->s_magic) {
3262 case SMACK_MAGIC:
Casey Schaufler36ea7352014-04-28 15:23:01 -07003263 case CGROUP_SUPER_MAGIC:
José Bollo58c442f2018-02-27 17:06:21 +01003264 case CGROUP2_SUPER_MAGIC:
Casey Schauflere114e472008-02-04 22:29:50 -08003265 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003266 * Casey says that it's a little embarrassing
Casey Schauflere114e472008-02-04 22:29:50 -08003267 * that the smack file system doesn't do
3268 * extended attributes.
Casey Schaufler36ea7352014-04-28 15:23:01 -07003269 *
Casey Schaufler36ea7352014-04-28 15:23:01 -07003270 * Cgroupfs is special
Casey Schauflere114e472008-02-04 22:29:50 -08003271 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003272 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003273 break;
3274 case DEVPTS_SUPER_MAGIC:
3275 /*
3276 * devpts seems content with the label of the task.
3277 * Programs that change smack have to treat the
3278 * pty with respect.
3279 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003280 final = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003281 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003282 case PROC_SUPER_MAGIC:
3283 /*
3284 * Casey says procfs appears not to care.
3285 * The superblock default suffices.
3286 */
3287 break;
3288 case TMPFS_MAGIC:
3289 /*
3290 * Device labels should come from the filesystem,
3291 * but watch out, because they're volitile,
3292 * getting recreated on every reboot.
3293 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003294 final = &smack_known_star;
Casey Schauflere114e472008-02-04 22:29:50 -08003295 /*
Gustavo A. R. Silvab1fed3e2018-08-01 17:38:54 -05003296 * Fall through.
Casey Schauflere114e472008-02-04 22:29:50 -08003297 *
3298 * If a smack value has been set we want to use it,
3299 * but since tmpfs isn't giving us the opportunity
3300 * to set mount options simulate setting the
3301 * superblock default.
3302 */
3303 default:
3304 /*
3305 * This isn't an understood special case.
3306 * Get the value from the xattr.
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003307 */
3308
3309 /*
3310 * UNIX domain sockets use lower level socket data.
3311 */
3312 if (S_ISSOCK(inode->i_mode)) {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003313 final = &smack_known_star;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003314 break;
3315 }
3316 /*
Casey Schauflere114e472008-02-04 22:29:50 -08003317 * No xattr support means, alas, no SMACK label.
3318 * Use the aforeapplied default.
3319 * It would be curious if the label of the task
3320 * does not match that assigned.
3321 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003322 if (!(inode->i_opflags & IOP_XATTR))
3323 break;
Casey Schauflere114e472008-02-04 22:29:50 -08003324 /*
3325 * Get the dentry for xattr.
3326 */
Dan Carpenter3e62cbb2010-06-01 09:14:04 +02003327 dp = dget(opt_dentry);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003328 skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003329 if (!IS_ERR_OR_NULL(skp))
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003330 final = skp;
Casey Schaufler2267b132012-03-13 19:14:19 -07003331
3332 /*
3333 * Transmuting directory
3334 */
3335 if (S_ISDIR(inode->i_mode)) {
3336 /*
3337 * If this is a new directory and the label was
3338 * transmuted when the inode was initialized
3339 * set the transmute attribute on the directory
3340 * and mark the inode.
3341 *
3342 * If there is a transmute attribute on the
3343 * directory mark the inode.
3344 */
3345 if (isp->smk_flags & SMK_INODE_CHANGED) {
3346 isp->smk_flags &= ~SMK_INODE_CHANGED;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003347 rc = __vfs_setxattr(dp, inode,
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003348 XATTR_NAME_SMACKTRANSMUTE,
Casey Schaufler2267b132012-03-13 19:14:19 -07003349 TRANS_TRUE, TRANS_TRUE_SIZE,
3350 0);
3351 } else {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02003352 rc = __vfs_getxattr(dp, inode,
Casey Schaufler2267b132012-03-13 19:14:19 -07003353 XATTR_NAME_SMACKTRANSMUTE, trattr,
3354 TRANS_TRUE_SIZE);
3355 if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
3356 TRANS_TRUE_SIZE) != 0)
3357 rc = -EINVAL;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003358 }
Casey Schaufler2267b132012-03-13 19:14:19 -07003359 if (rc >= 0)
3360 transflag = SMK_INODE_TRANSMUTE;
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003361 }
Seth Forshee809c02e2016-04-26 14:36:22 -05003362 /*
3363 * Don't let the exec or mmap label be "*" or "@".
3364 */
3365 skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
3366 if (IS_ERR(skp) || skp == &smack_known_star ||
3367 skp == &smack_known_web)
3368 skp = NULL;
3369 isp->smk_task = skp;
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003370
Casey Schaufler19760ad2013-12-16 16:27:26 -08003371 skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003372 if (IS_ERR(skp) || skp == &smack_known_star ||
3373 skp == &smack_known_web)
Casey Schaufler19760ad2013-12-16 16:27:26 -08003374 skp = NULL;
3375 isp->smk_mmap = skp;
Casey Schaufler676dac42010-12-02 06:43:39 -08003376
Casey Schauflere114e472008-02-04 22:29:50 -08003377 dput(dp);
3378 break;
3379 }
3380
3381 if (final == NULL)
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003382 isp->smk_inode = ckp;
Casey Schauflere114e472008-02-04 22:29:50 -08003383 else
3384 isp->smk_inode = final;
3385
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +02003386 isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
Casey Schauflere114e472008-02-04 22:29:50 -08003387
3388unlockandout:
3389 mutex_unlock(&isp->smk_lock);
3390 return;
3391}
3392
3393/**
3394 * smack_getprocattr - Smack process attribute access
3395 * @p: the object task
3396 * @name: the name of the attribute in /proc/.../attr
3397 * @value: where to put the result
3398 *
3399 * Places a copy of the task Smack into value
3400 *
3401 * Returns the length of the smack label or an error code
3402 */
3403static int smack_getprocattr(struct task_struct *p, char *name, char **value)
3404{
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +03003405 struct smack_known *skp = smk_of_task_struct(p);
Casey Schauflere114e472008-02-04 22:29:50 -08003406 char *cp;
3407 int slen;
3408
3409 if (strcmp(name, "current") != 0)
3410 return -EINVAL;
3411
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003412 cp = kstrdup(skp->smk_known, GFP_KERNEL);
Casey Schauflere114e472008-02-04 22:29:50 -08003413 if (cp == NULL)
3414 return -ENOMEM;
3415
3416 slen = strlen(cp);
3417 *value = cp;
3418 return slen;
3419}
3420
3421/**
3422 * smack_setprocattr - Smack process attribute setting
Casey Schauflere114e472008-02-04 22:29:50 -08003423 * @name: the name of the attribute in /proc/.../attr
3424 * @value: the value to set
3425 * @size: the size of the value
3426 *
3427 * Sets the Smack value of the task. Only setting self
3428 * is permitted and only with privilege
3429 *
3430 * Returns the length of the smack label or an error code
3431 */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05003432static int smack_setprocattr(const char *name, void *value, size_t size)
Casey Schauflere114e472008-02-04 22:29:50 -08003433{
Casey Schauflerb17103a2018-11-09 16:12:56 -08003434 struct task_smack *tsp = smack_cred(current_cred());
David Howellsd84f4f92008-11-14 10:39:23 +11003435 struct cred *new;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003436 struct smack_known *skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003437 struct smack_known_list_elem *sklep;
3438 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003439
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003440 if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
David Howells5cd9c582008-08-14 11:37:28 +01003441 return -EPERM;
3442
Casey Schauflerf7112e62012-05-06 15:22:02 -07003443 if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
Casey Schauflere114e472008-02-04 22:29:50 -08003444 return -EINVAL;
3445
3446 if (strcmp(name, "current") != 0)
3447 return -EINVAL;
3448
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003449 skp = smk_import_entry(value, size);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02003450 if (IS_ERR(skp))
3451 return PTR_ERR(skp);
Casey Schauflere114e472008-02-04 22:29:50 -08003452
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003453 /*
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303454 * No process is ever allowed the web ("@") label
3455 * and the star ("*") label.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003456 */
Himanshu Shukla7128ea12016-11-10 16:17:49 +05303457 if (skp == &smack_known_web || skp == &smack_known_star)
3458 return -EINVAL;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003459
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003460 if (!smack_privileged(CAP_MAC_ADMIN)) {
3461 rc = -EPERM;
3462 list_for_each_entry(sklep, &tsp->smk_relabel, list)
3463 if (sklep->smk_label == skp) {
3464 rc = 0;
3465 break;
3466 }
3467 if (rc)
3468 return rc;
3469 }
3470
David Howellsd84f4f92008-11-14 10:39:23 +11003471 new = prepare_creds();
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003472 if (new == NULL)
David Howellsd84f4f92008-11-14 10:39:23 +11003473 return -ENOMEM;
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003474
Casey Schauflerb17103a2018-11-09 16:12:56 -08003475 tsp = smack_cred(new);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003476 tsp->smk_task = skp;
Zbigniew Jasinski38416e52015-10-19 18:23:53 +02003477 /*
3478 * process can change its label only once
3479 */
3480 smk_destroy_label_list(&tsp->smk_relabel);
Casey Schaufler7898e1f2011-01-17 08:05:27 -08003481
David Howellsd84f4f92008-11-14 10:39:23 +11003482 commit_creds(new);
Casey Schauflere114e472008-02-04 22:29:50 -08003483 return size;
3484}
3485
3486/**
3487 * smack_unix_stream_connect - Smack access on UDS
David S. Miller3610cda2011-01-05 15:38:53 -08003488 * @sock: one sock
3489 * @other: the other sock
Casey Schauflere114e472008-02-04 22:29:50 -08003490 * @newsk: unused
3491 *
3492 * Return 0 if a subject with the smack of sock could access
3493 * an object with the smack of other, otherwise an error code
3494 */
David S. Miller3610cda2011-01-05 15:38:53 -08003495static int smack_unix_stream_connect(struct sock *sock,
3496 struct sock *other, struct sock *newsk)
Casey Schauflere114e472008-02-04 22:29:50 -08003497{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003498 struct smack_known *skp;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003499 struct smack_known *okp;
James Morrisd2e7ad12011-01-10 09:46:24 +11003500 struct socket_smack *ssp = sock->sk_security;
3501 struct socket_smack *osp = other->sk_security;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003502 struct socket_smack *nsp = newsk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003503 struct smk_audit_info ad;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003504 int rc = 0;
Kees Cook923e9a12012-04-10 13:26:44 -07003505#ifdef CONFIG_AUDIT
3506 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003507#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003508
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003509 if (!smack_privileged(CAP_MAC_OVERRIDE)) {
3510 skp = ssp->smk_out;
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003511 okp = osp->smk_in;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003512#ifdef CONFIG_AUDIT
3513 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
3514 smk_ad_setfield_u_net_sk(&ad, other);
3515#endif
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003516 rc = smk_access(skp, okp, MAY_WRITE, &ad);
3517 rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003518 if (rc == 0) {
Zbigniew Jasinski96be7b52014-12-29 15:34:58 +01003519 okp = osp->smk_out;
3520 skp = ssp->smk_in;
Rafal Krypa138a8682015-01-08 18:52:45 +01003521 rc = smk_access(okp, skp, MAY_WRITE, &ad);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003522 rc = smk_bu_note("UDS connect", okp, skp,
Casey Schauflerd166c802014-08-27 14:51:27 -07003523 MAY_WRITE, rc);
3524 }
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003525 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003526
Casey Schaufler975d5e52011-09-26 14:43:39 -07003527 /*
3528 * Cross reference the peer labels for SO_PEERSEC.
3529 */
3530 if (rc == 0) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003531 nsp->smk_packet = ssp->smk_out;
3532 ssp->smk_packet = osp->smk_out;
Casey Schaufler975d5e52011-09-26 14:43:39 -07003533 }
3534
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003535 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003536}
3537
3538/**
3539 * smack_unix_may_send - Smack access on UDS
3540 * @sock: one socket
3541 * @other: the other socket
3542 *
3543 * Return 0 if a subject with the smack of sock could access
3544 * an object with the smack of other, otherwise an error code
3545 */
3546static int smack_unix_may_send(struct socket *sock, struct socket *other)
3547{
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003548 struct socket_smack *ssp = sock->sk->sk_security;
3549 struct socket_smack *osp = other->sk->sk_security;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003550 struct smk_audit_info ad;
Casey Schauflerd166c802014-08-27 14:51:27 -07003551 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003552
Kees Cook923e9a12012-04-10 13:26:44 -07003553#ifdef CONFIG_AUDIT
3554 struct lsm_network_audit net;
3555
Eric Paris48c62af2012-04-02 13:15:44 -04003556 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003557 smk_ad_setfield_u_net_sk(&ad, other->sk);
Kees Cook923e9a12012-04-10 13:26:44 -07003558#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003559
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003560 if (smack_privileged(CAP_MAC_OVERRIDE))
3561 return 0;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003562
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003563 rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
3564 rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
Casey Schauflerd166c802014-08-27 14:51:27 -07003565 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003566}
3567
3568/**
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003569 * smack_socket_sendmsg - Smack check based on destination host
3570 * @sock: the socket
Randy Dunlap251a2a92009-02-18 11:42:33 -08003571 * @msg: the message
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003572 * @size: the size of the message
3573 *
Casey Schauflerc6739442013-05-22 18:42:56 -07003574 * Return 0 if the current subject can write to the destination host.
3575 * For IPv4 this is only a question if the destination is a single label host.
3576 * For IPv6 this is a check against the label of the port.
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003577 */
3578static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3579 int size)
3580{
3581 struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003582#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003583 struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003584#endif
3585#ifdef SMACK_IPV6_SECMARK_LABELING
3586 struct socket_smack *ssp = sock->sk->sk_security;
3587 struct smack_known *rsp;
3588#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003589 int rc = 0;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003590
3591 /*
3592 * Perfectly reasonable for this to be NULL
3593 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003594 if (sip == NULL)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003595 return 0;
3596
Roman Kubiak81bd0d52015-12-17 13:24:35 +01003597 switch (sock->sk->sk_family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003598 case AF_INET:
3599 rc = smack_netlabel_send(sock->sk, sip);
3600 break;
3601 case AF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003602#ifdef SMACK_IPV6_SECMARK_LABELING
3603 rsp = smack_ipv6host_label(sap);
3604 if (rsp != NULL)
3605 rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
3606 SMK_CONNECTING);
3607#endif
3608#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflerc6739442013-05-22 18:42:56 -07003609 rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003610#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003611 break;
3612 }
3613 return rc;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003614}
3615
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003616/**
Randy Dunlap251a2a92009-02-18 11:42:33 -08003617 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
Casey Schauflere114e472008-02-04 22:29:50 -08003618 * @sap: netlabel secattr
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003619 * @ssp: socket security information
Casey Schauflere114e472008-02-04 22:29:50 -08003620 *
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003621 * Returns a pointer to a Smack label entry found on the label list.
Casey Schauflere114e472008-02-04 22:29:50 -08003622 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003623static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
3624 struct socket_smack *ssp)
Casey Schauflere114e472008-02-04 22:29:50 -08003625{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003626 struct smack_known *skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003627 int found = 0;
Casey Schaufler677264e2013-06-28 13:47:07 -07003628 int acat;
3629 int kcat;
Casey Schauflere114e472008-02-04 22:29:50 -08003630
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003631 if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
Casey Schauflere114e472008-02-04 22:29:50 -08003632 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003633 * Looks like a CIPSO packet.
Casey Schauflere114e472008-02-04 22:29:50 -08003634 * If there are flags but no level netlabel isn't
3635 * behaving the way we expect it to.
3636 *
Casey Schauflerf7112e62012-05-06 15:22:02 -07003637 * Look it up in the label table
Casey Schauflere114e472008-02-04 22:29:50 -08003638 * Without guidance regarding the smack value
3639 * for the packet fall back on the network
3640 * ambient value.
3641 */
Casey Schauflerf7112e62012-05-06 15:22:02 -07003642 rcu_read_lock();
Vishal Goel348dc282016-11-23 10:45:31 +05303643 list_for_each_entry_rcu(skp, &smack_known_list, list) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003644 if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
Casey Schauflerf7112e62012-05-06 15:22:02 -07003645 continue;
Casey Schaufler677264e2013-06-28 13:47:07 -07003646 /*
3647 * Compare the catsets. Use the netlbl APIs.
3648 */
3649 if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
3650 if ((skp->smk_netlabel.flags &
3651 NETLBL_SECATTR_MLS_CAT) == 0)
3652 found = 1;
3653 break;
3654 }
3655 for (acat = -1, kcat = -1; acat == kcat; ) {
Paul Moore4fbe63d2014-08-01 11:17:37 -04003656 acat = netlbl_catmap_walk(sap->attr.mls.cat,
3657 acat + 1);
3658 kcat = netlbl_catmap_walk(
Casey Schaufler677264e2013-06-28 13:47:07 -07003659 skp->smk_netlabel.attr.mls.cat,
3660 kcat + 1);
3661 if (acat < 0 || kcat < 0)
3662 break;
3663 }
3664 if (acat == kcat) {
3665 found = 1;
3666 break;
3667 }
Casey Schauflere114e472008-02-04 22:29:50 -08003668 }
Casey Schauflerf7112e62012-05-06 15:22:02 -07003669 rcu_read_unlock();
3670
3671 if (found)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003672 return skp;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003673
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003674 if (ssp != NULL && ssp->smk_in == &smack_known_star)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003675 return &smack_known_web;
3676 return &smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003677 }
Casey Schaufler152f91d2016-11-14 09:38:15 -08003678 if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003679 /*
3680 * Looks like a fallback, which gives us a secid.
3681 */
Casey Schaufler152f91d2016-11-14 09:38:15 -08003682 return smack_from_secid(sap->attr.secid);
Casey Schauflere114e472008-02-04 22:29:50 -08003683 /*
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003684 * Without guidance regarding the smack value
3685 * for the packet fall back on the network
3686 * ambient value.
Casey Schauflere114e472008-02-04 22:29:50 -08003687 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003688 return smack_net_ambient;
Casey Schauflere114e472008-02-04 22:29:50 -08003689}
3690
Casey Schaufler69f287a2014-12-12 17:08:40 -08003691#if IS_ENABLED(CONFIG_IPV6)
Casey Schaufler6ea06242013-08-05 13:21:22 -07003692static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
Casey Schauflerc6739442013-05-22 18:42:56 -07003693{
Casey Schauflerc6739442013-05-22 18:42:56 -07003694 u8 nexthdr;
3695 int offset;
3696 int proto = -EINVAL;
3697 struct ipv6hdr _ipv6h;
3698 struct ipv6hdr *ip6;
3699 __be16 frag_off;
3700 struct tcphdr _tcph, *th;
3701 struct udphdr _udph, *uh;
3702 struct dccp_hdr _dccph, *dh;
3703
3704 sip->sin6_port = 0;
3705
3706 offset = skb_network_offset(skb);
3707 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3708 if (ip6 == NULL)
3709 return -EINVAL;
3710 sip->sin6_addr = ip6->saddr;
3711
3712 nexthdr = ip6->nexthdr;
3713 offset += sizeof(_ipv6h);
3714 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3715 if (offset < 0)
3716 return -EINVAL;
3717
3718 proto = nexthdr;
3719 switch (proto) {
3720 case IPPROTO_TCP:
3721 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3722 if (th != NULL)
3723 sip->sin6_port = th->source;
3724 break;
3725 case IPPROTO_UDP:
Piotr Sawickia07ef952018-07-19 11:45:16 +02003726 case IPPROTO_UDPLITE:
Casey Schauflerc6739442013-05-22 18:42:56 -07003727 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3728 if (uh != NULL)
3729 sip->sin6_port = uh->source;
3730 break;
3731 case IPPROTO_DCCP:
3732 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3733 if (dh != NULL)
3734 sip->sin6_port = dh->dccph_sport;
3735 break;
3736 }
3737 return proto;
3738}
Casey Schaufler69f287a2014-12-12 17:08:40 -08003739#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003740
Casey Schauflere114e472008-02-04 22:29:50 -08003741/**
3742 * smack_socket_sock_rcv_skb - Smack packet delivery access check
3743 * @sk: socket
3744 * @skb: packet
3745 *
3746 * Returns 0 if the packet should be delivered, an error code otherwise
3747 */
3748static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3749{
3750 struct netlbl_lsm_secattr secattr;
3751 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003752 struct smack_known *skp = NULL;
Casey Schauflerc6739442013-05-22 18:42:56 -07003753 int rc = 0;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003754 struct smk_audit_info ad;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003755 u16 family = sk->sk_family;
Kees Cook923e9a12012-04-10 13:26:44 -07003756#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003757 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003758#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003759#if IS_ENABLED(CONFIG_IPV6)
3760 struct sockaddr_in6 sadd;
3761 int proto;
Piotr Sawicki129a9982018-07-19 11:42:58 +02003762
3763 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
3764 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003765#endif /* CONFIG_IPV6 */
3766
Piotr Sawicki129a9982018-07-19 11:42:58 +02003767 switch (family) {
Casey Schauflerc6739442013-05-22 18:42:56 -07003768 case PF_INET:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003769#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3770 /*
3771 * If there is a secmark use it rather than the CIPSO label.
3772 * If there is no secmark fall back to CIPSO.
3773 * The secmark is assumed to reflect policy better.
3774 */
3775 if (skb && skb->secmark != 0) {
3776 skp = smack_from_secid(skb->secmark);
3777 goto access_check;
3778 }
3779#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
Casey Schauflerc6739442013-05-22 18:42:56 -07003780 /*
3781 * Translate what netlabel gave us.
3782 */
3783 netlbl_secattr_init(&secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08003784
Piotr Sawicki129a9982018-07-19 11:42:58 +02003785 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflerc6739442013-05-22 18:42:56 -07003786 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003787 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflerc6739442013-05-22 18:42:56 -07003788 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003789 skp = smack_net_ambient;
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003790
Casey Schauflerc6739442013-05-22 18:42:56 -07003791 netlbl_secattr_destroy(&secattr);
Casey Schaufler6d3dc072008-12-31 12:54:12 -05003792
Casey Schaufler69f287a2014-12-12 17:08:40 -08003793#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3794access_check:
3795#endif
Etienne Bassetecfcc532009-04-08 20:40:06 +02003796#ifdef CONFIG_AUDIT
Casey Schauflerc6739442013-05-22 18:42:56 -07003797 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003798 ad.a.u.net->family = family;
Casey Schauflerc6739442013-05-22 18:42:56 -07003799 ad.a.u.net->netif = skb->skb_iif;
3800 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
Etienne Bassetecfcc532009-04-08 20:40:06 +02003801#endif
Casey Schauflerc6739442013-05-22 18:42:56 -07003802 /*
3803 * Receiving a packet requires that the other end
3804 * be able to write here. Read access is not required.
3805 * This is the simplist possible security model
3806 * for networking.
3807 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003808 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3809 rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
Casey Schauflerd166c802014-08-27 14:51:27 -07003810 MAY_WRITE, rc);
Casey Schauflerc6739442013-05-22 18:42:56 -07003811 if (rc != 0)
Piotr Sawicki129a9982018-07-19 11:42:58 +02003812 netlbl_skbuff_err(skb, family, rc, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003813 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003814#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07003815 case PF_INET6:
Casey Schaufler69f287a2014-12-12 17:08:40 -08003816 proto = smk_skb_to_addr_ipv6(skb, &sadd);
Piotr Sawickia07ef952018-07-19 11:45:16 +02003817 if (proto != IPPROTO_UDP && proto != IPPROTO_UDPLITE &&
3818 proto != IPPROTO_TCP && proto != IPPROTO_DCCP)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003819 break;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003820#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003821 if (skb && skb->secmark != 0)
3822 skp = smack_from_secid(skb->secmark);
Casey Schauflerc6739442013-05-22 18:42:56 -07003823 else
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003824 skp = smack_ipv6host_label(&sadd);
3825 if (skp == NULL)
Casey Schaufler69f287a2014-12-12 17:08:40 -08003826 skp = smack_net_ambient;
3827#ifdef CONFIG_AUDIT
3828 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
Piotr Sawicki129a9982018-07-19 11:42:58 +02003829 ad.a.u.net->family = family;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003830 ad.a.u.net->netif = skb->skb_iif;
3831 ipv6_skb_to_auditdata(skb, &ad.a, NULL);
3832#endif /* CONFIG_AUDIT */
3833 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
3834 rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
3835 MAY_WRITE, rc);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003836#endif /* SMACK_IPV6_SECMARK_LABELING */
3837#ifdef SMACK_IPV6_PORT_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003838 rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003839#endif /* SMACK_IPV6_PORT_LABELING */
Piotr Sawickid66a8ac2018-07-19 11:47:31 +02003840 if (rc != 0)
3841 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
3842 ICMPV6_ADM_PROHIBITED, 0);
Casey Schauflerc6739442013-05-22 18:42:56 -07003843 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003844#endif /* CONFIG_IPV6 */
Casey Schauflerc6739442013-05-22 18:42:56 -07003845 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08003846
Paul Moorea8134292008-10-10 10:16:31 -04003847 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08003848}
3849
3850/**
3851 * smack_socket_getpeersec_stream - pull in packet label
3852 * @sock: the socket
3853 * @optval: user's destination
3854 * @optlen: size thereof
Randy Dunlap251a2a92009-02-18 11:42:33 -08003855 * @len: max thereof
Casey Schauflere114e472008-02-04 22:29:50 -08003856 *
3857 * returns zero on success, an error code otherwise
3858 */
3859static int smack_socket_getpeersec_stream(struct socket *sock,
3860 char __user *optval,
3861 int __user *optlen, unsigned len)
3862{
3863 struct socket_smack *ssp;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003864 char *rcp = "";
3865 int slen = 1;
Casey Schauflere114e472008-02-04 22:29:50 -08003866 int rc = 0;
3867
3868 ssp = sock->sk->sk_security;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003869 if (ssp->smk_packet != NULL) {
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003870 rcp = ssp->smk_packet->smk_known;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003871 slen = strlen(rcp) + 1;
3872 }
Casey Schauflere114e472008-02-04 22:29:50 -08003873
3874 if (slen > len)
3875 rc = -ERANGE;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003876 else if (copy_to_user(optval, rcp, slen) != 0)
Casey Schauflere114e472008-02-04 22:29:50 -08003877 rc = -EFAULT;
3878
3879 if (put_user(slen, optlen) != 0)
3880 rc = -EFAULT;
3881
3882 return rc;
3883}
3884
3885
3886/**
3887 * smack_socket_getpeersec_dgram - pull in packet label
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003888 * @sock: the peer socket
Casey Schauflere114e472008-02-04 22:29:50 -08003889 * @skb: packet data
3890 * @secid: pointer to where to put the secid of the packet
3891 *
3892 * Sets the netlabel socket state on sk from parent
3893 */
3894static int smack_socket_getpeersec_dgram(struct socket *sock,
3895 struct sk_buff *skb, u32 *secid)
3896
3897{
3898 struct netlbl_lsm_secattr secattr;
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003899 struct socket_smack *ssp = NULL;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003900 struct smack_known *skp;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003901 int family = PF_UNSPEC;
3902 u32 s = 0; /* 0 is the invalid secid */
Casey Schauflere114e472008-02-04 22:29:50 -08003903 int rc;
3904
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003905 if (skb != NULL) {
3906 if (skb->protocol == htons(ETH_P_IP))
3907 family = PF_INET;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003908#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003909 else if (skb->protocol == htons(ETH_P_IPV6))
3910 family = PF_INET6;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003911#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08003912 }
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003913 if (family == PF_UNSPEC && sock != NULL)
3914 family = sock->sk->sk_family;
Casey Schauflere114e472008-02-04 22:29:50 -08003915
Casey Schaufler69f287a2014-12-12 17:08:40 -08003916 switch (family) {
3917 case PF_UNIX:
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003918 ssp = sock->sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003919 s = ssp->smk_out->smk_secid;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003920 break;
3921 case PF_INET:
3922#ifdef CONFIG_SECURITY_SMACK_NETFILTER
3923 s = skb->secmark;
3924 if (s != 0)
3925 break;
3926#endif
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003927 /*
3928 * Translate what netlabel gave us.
3929 */
Casey Schaufler272cd7a2011-09-20 12:24:36 -07003930 if (sock != NULL && sock->sk != NULL)
3931 ssp = sock->sk->sk_security;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003932 netlbl_secattr_init(&secattr);
3933 rc = netlbl_skbuff_getattr(skb, family, &secattr);
3934 if (rc == 0) {
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003935 skp = smack_from_secattr(&secattr, ssp);
3936 s = skp->smk_secid;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003937 }
3938 netlbl_secattr_destroy(&secattr);
Casey Schaufler69f287a2014-12-12 17:08:40 -08003939 break;
Casey Schaufler69f287a2014-12-12 17:08:40 -08003940 case PF_INET6:
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003941#ifdef SMACK_IPV6_SECMARK_LABELING
Casey Schaufler69f287a2014-12-12 17:08:40 -08003942 s = skb->secmark;
Casey Schaufler21abb1e2015-07-22 14:25:31 -07003943#endif
Casey Schaufler69f287a2014-12-12 17:08:40 -08003944 break;
Casey Schauflerb4e0d5f2010-11-24 17:12:10 -08003945 }
3946 *secid = s;
Casey Schauflere114e472008-02-04 22:29:50 -08003947 if (s == 0)
3948 return -EINVAL;
Casey Schauflere114e472008-02-04 22:29:50 -08003949 return 0;
3950}
3951
3952/**
Paul Moore07feee82009-03-27 17:10:54 -04003953 * smack_sock_graft - Initialize a newly created socket with an existing sock
3954 * @sk: child sock
3955 * @parent: parent socket
Casey Schauflere114e472008-02-04 22:29:50 -08003956 *
Paul Moore07feee82009-03-27 17:10:54 -04003957 * Set the smk_{in,out} state of an existing sock based on the process that
3958 * is creating the new socket.
Casey Schauflere114e472008-02-04 22:29:50 -08003959 */
3960static void smack_sock_graft(struct sock *sk, struct socket *parent)
3961{
3962 struct socket_smack *ssp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003963 struct smack_known *skp = smk_of_current();
Casey Schauflere114e472008-02-04 22:29:50 -08003964
Paul Moore07feee82009-03-27 17:10:54 -04003965 if (sk == NULL ||
3966 (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
Casey Schauflere114e472008-02-04 22:29:50 -08003967 return;
3968
3969 ssp = sk->sk_security;
Casey Schaufler54e70ec2014-04-10 16:37:08 -07003970 ssp->smk_in = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07003971 ssp->smk_out = skp;
Paul Moore07feee82009-03-27 17:10:54 -04003972 /* cssp->smk_packet is already set in smack_inet_csk_clone() */
Casey Schauflere114e472008-02-04 22:29:50 -08003973}
3974
3975/**
3976 * smack_inet_conn_request - Smack access check on connect
3977 * @sk: socket involved
3978 * @skb: packet
3979 * @req: unused
3980 *
3981 * Returns 0 if a task with the packet label could write to
3982 * the socket, otherwise an error code
3983 */
3984static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
3985 struct request_sock *req)
3986{
Paul Moore07feee82009-03-27 17:10:54 -04003987 u16 family = sk->sk_family;
Casey Schauflerf7112e62012-05-06 15:22:02 -07003988 struct smack_known *skp;
Casey Schauflere114e472008-02-04 22:29:50 -08003989 struct socket_smack *ssp = sk->sk_security;
Paul Moore07feee82009-03-27 17:10:54 -04003990 struct netlbl_lsm_secattr secattr;
3991 struct sockaddr_in addr;
3992 struct iphdr *hdr;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02003993 struct smack_known *hskp;
Casey Schauflere114e472008-02-04 22:29:50 -08003994 int rc;
Etienne Bassetecfcc532009-04-08 20:40:06 +02003995 struct smk_audit_info ad;
Kees Cook923e9a12012-04-10 13:26:44 -07003996#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04003997 struct lsm_network_audit net;
Kees Cook923e9a12012-04-10 13:26:44 -07003998#endif
Casey Schauflere114e472008-02-04 22:29:50 -08003999
Casey Schaufler69f287a2014-12-12 17:08:40 -08004000#if IS_ENABLED(CONFIG_IPV6)
Casey Schauflerc6739442013-05-22 18:42:56 -07004001 if (family == PF_INET6) {
4002 /*
4003 * Handle mapped IPv4 packets arriving
4004 * via IPv6 sockets. Don't set up netlabel
4005 * processing on IPv6.
4006 */
4007 if (skb->protocol == htons(ETH_P_IP))
4008 family = PF_INET;
4009 else
4010 return 0;
4011 }
Casey Schaufler69f287a2014-12-12 17:08:40 -08004012#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -08004013
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004014#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4015 /*
4016 * If there is a secmark use it rather than the CIPSO label.
4017 * If there is no secmark fall back to CIPSO.
4018 * The secmark is assumed to reflect policy better.
4019 */
4020 if (skb && skb->secmark != 0) {
4021 skp = smack_from_secid(skb->secmark);
4022 goto access_check;
4023 }
4024#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
4025
Paul Moore07feee82009-03-27 17:10:54 -04004026 netlbl_secattr_init(&secattr);
4027 rc = netlbl_skbuff_getattr(skb, family, &secattr);
Casey Schauflere114e472008-02-04 22:29:50 -08004028 if (rc == 0)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004029 skp = smack_from_secattr(&secattr, ssp);
Casey Schauflere114e472008-02-04 22:29:50 -08004030 else
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004031 skp = &smack_known_huh;
Paul Moore07feee82009-03-27 17:10:54 -04004032 netlbl_secattr_destroy(&secattr);
4033
Casey Schaufler7f368ad2015-02-11 12:52:32 -08004034#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4035access_check:
4036#endif
4037
Etienne Bassetecfcc532009-04-08 20:40:06 +02004038#ifdef CONFIG_AUDIT
Eric Paris48c62af2012-04-02 13:15:44 -04004039 smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
4040 ad.a.u.net->family = family;
4041 ad.a.u.net->netif = skb->skb_iif;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004042 ipv4_skb_to_auditdata(skb, &ad.a, NULL);
4043#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004044 /*
Paul Moore07feee82009-03-27 17:10:54 -04004045 * Receiving a packet requires that the other end be able to write
4046 * here. Read access is not required.
Casey Schauflere114e472008-02-04 22:29:50 -08004047 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004048 rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
4049 rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
Paul Moore07feee82009-03-27 17:10:54 -04004050 if (rc != 0)
4051 return rc;
4052
4053 /*
4054 * Save the peer's label in the request_sock so we can later setup
4055 * smk_packet in the child socket so that SO_PEERCRED can report it.
4056 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004057 req->peer_secid = skp->smk_secid;
Paul Moore07feee82009-03-27 17:10:54 -04004058
4059 /*
4060 * We need to decide if we want to label the incoming connection here
4061 * if we do we only need to label the request_sock and the stack will
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004062 * propagate the wire-label to the sock when it is created.
Paul Moore07feee82009-03-27 17:10:54 -04004063 */
4064 hdr = ip_hdr(skb);
4065 addr.sin_addr.s_addr = hdr->saddr;
4066 rcu_read_lock();
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004067 hskp = smack_ipv4host_label(&addr);
Casey Schauflerf7112e62012-05-06 15:22:02 -07004068 rcu_read_unlock();
4069
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004070 if (hskp == NULL)
Casey Schauflerf7112e62012-05-06 15:22:02 -07004071 rc = netlbl_req_setattr(req, &skp->smk_netlabel);
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004072 else
Paul Moore07feee82009-03-27 17:10:54 -04004073 netlbl_req_delattr(req);
Casey Schauflere114e472008-02-04 22:29:50 -08004074
4075 return rc;
4076}
4077
Paul Moore07feee82009-03-27 17:10:54 -04004078/**
4079 * smack_inet_csk_clone - Copy the connection information to the new socket
4080 * @sk: the new socket
4081 * @req: the connection's request_sock
4082 *
4083 * Transfer the connection's peer label to the newly created socket.
4084 */
4085static void smack_inet_csk_clone(struct sock *sk,
4086 const struct request_sock *req)
4087{
4088 struct socket_smack *ssp = sk->sk_security;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004089 struct smack_known *skp;
Paul Moore07feee82009-03-27 17:10:54 -04004090
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004091 if (req->peer_secid != 0) {
4092 skp = smack_from_secid(req->peer_secid);
Casey Schaufler54e70ec2014-04-10 16:37:08 -07004093 ssp->smk_packet = skp;
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004094 } else
Casey Schaufler272cd7a2011-09-20 12:24:36 -07004095 ssp->smk_packet = NULL;
Paul Moore07feee82009-03-27 17:10:54 -04004096}
4097
Casey Schauflere114e472008-02-04 22:29:50 -08004098/*
4099 * Key management security hooks
4100 *
4101 * Casey has not tested key support very heavily.
4102 * The permission check is most likely too restrictive.
4103 * If you care about keys please have a look.
4104 */
4105#ifdef CONFIG_KEYS
4106
4107/**
4108 * smack_key_alloc - Set the key security blob
4109 * @key: object
David Howellsd84f4f92008-11-14 10:39:23 +11004110 * @cred: the credentials to use
Casey Schauflere114e472008-02-04 22:29:50 -08004111 * @flags: unused
4112 *
4113 * No allocation required
4114 *
4115 * Returns 0
4116 */
David Howellsd84f4f92008-11-14 10:39:23 +11004117static int smack_key_alloc(struct key *key, const struct cred *cred,
Casey Schauflere114e472008-02-04 22:29:50 -08004118 unsigned long flags)
4119{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004120 struct smack_known *skp = smk_of_task(smack_cred(cred));
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004121
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004122 key->security = skp;
Casey Schauflere114e472008-02-04 22:29:50 -08004123 return 0;
4124}
4125
4126/**
4127 * smack_key_free - Clear the key security blob
4128 * @key: the object
4129 *
4130 * Clear the blob pointer
4131 */
4132static void smack_key_free(struct key *key)
4133{
4134 key->security = NULL;
4135}
4136
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004137/**
Casey Schauflere114e472008-02-04 22:29:50 -08004138 * smack_key_permission - Smack access on a key
4139 * @key_ref: gets to the object
David Howellsd84f4f92008-11-14 10:39:23 +11004140 * @cred: the credentials to use
Lukasz Pawelczyk1a289792014-11-26 15:31:06 +01004141 * @perm: requested key permissions
Casey Schauflere114e472008-02-04 22:29:50 -08004142 *
4143 * Return 0 if the task has read and write to the object,
4144 * an error code otherwise
4145 */
4146static int smack_key_permission(key_ref_t key_ref,
David Howellsf5895942014-03-14 17:44:49 +00004147 const struct cred *cred, unsigned perm)
Casey Schauflere114e472008-02-04 22:29:50 -08004148{
4149 struct key *keyp;
Etienne Bassetecfcc532009-04-08 20:40:06 +02004150 struct smk_audit_info ad;
Casey Schauflerb17103a2018-11-09 16:12:56 -08004151 struct smack_known *tkp = smk_of_task(smack_cred(cred));
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004152 int request = 0;
Casey Schauflerd166c802014-08-27 14:51:27 -07004153 int rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004154
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004155 /*
4156 * Validate requested permissions
4157 */
4158 if (perm & ~KEY_NEED_ALL)
4159 return -EINVAL;
4160
Casey Schauflere114e472008-02-04 22:29:50 -08004161 keyp = key_ref_to_ptr(key_ref);
4162 if (keyp == NULL)
4163 return -EINVAL;
4164 /*
4165 * If the key hasn't been initialized give it access so that
4166 * it may do so.
4167 */
4168 if (keyp->security == NULL)
4169 return 0;
4170 /*
4171 * This should not occur
4172 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004173 if (tkp == NULL)
Casey Schauflere114e472008-02-04 22:29:50 -08004174 return -EACCES;
Casey Schauflerd19dfe52018-01-08 10:25:32 -08004175
4176 if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
4177 return 0;
4178
Etienne Bassetecfcc532009-04-08 20:40:06 +02004179#ifdef CONFIG_AUDIT
4180 smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
4181 ad.a.u.key_struct.key = keyp->serial;
4182 ad.a.u.key_struct.key_desc = keyp->description;
4183#endif
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004184 if (perm & (KEY_NEED_READ | KEY_NEED_SEARCH | KEY_NEED_VIEW))
4185 request |= MAY_READ;
Dmitry Kasatkinfffea212014-03-14 17:44:49 +00004186 if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
Zoran Markovic5b841bf2018-10-17 16:25:44 -07004187 request |= MAY_WRITE;
Casey Schauflerd166c802014-08-27 14:51:27 -07004188 rc = smk_access(tkp, keyp->security, request, &ad);
4189 rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
4190 return rc;
Casey Schauflere114e472008-02-04 22:29:50 -08004191}
José Bollo7fc5f362015-02-17 15:41:22 +01004192
4193/*
4194 * smack_key_getsecurity - Smack label tagging the key
4195 * @key points to the key to be queried
4196 * @_buffer points to a pointer that should be set to point to the
4197 * resulting string (if no label or an error occurs).
4198 * Return the length of the string (including terminating NUL) or -ve if
4199 * an error.
4200 * May also return 0 (and a NULL buffer pointer) if there is no label.
4201 */
4202static int smack_key_getsecurity(struct key *key, char **_buffer)
4203{
4204 struct smack_known *skp = key->security;
4205 size_t length;
4206 char *copy;
4207
4208 if (key->security == NULL) {
4209 *_buffer = NULL;
4210 return 0;
4211 }
4212
4213 copy = kstrdup(skp->smk_known, GFP_KERNEL);
4214 if (copy == NULL)
4215 return -ENOMEM;
4216 length = strlen(copy) + 1;
4217
4218 *_buffer = copy;
4219 return length;
4220}
4221
Casey Schauflere114e472008-02-04 22:29:50 -08004222#endif /* CONFIG_KEYS */
4223
4224/*
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004225 * Smack Audit hooks
4226 *
4227 * Audit requires a unique representation of each Smack specific
4228 * rule. This unique representation is used to distinguish the
4229 * object to be audited from remaining kernel objects and also
4230 * works as a glue between the audit hooks.
4231 *
4232 * Since repository entries are added but never deleted, we'll use
4233 * the smack_known label address related to the given audit rule as
4234 * the needed unique representation. This also better fits the smack
4235 * model where nearly everything is a label.
4236 */
4237#ifdef CONFIG_AUDIT
4238
4239/**
4240 * smack_audit_rule_init - Initialize a smack audit rule
4241 * @field: audit rule fields given from user-space (audit.h)
4242 * @op: required testing operator (=, !=, >, <, ...)
4243 * @rulestr: smack label to be audited
4244 * @vrule: pointer to save our own audit rule representation
4245 *
4246 * Prepare to audit cases where (@field @op @rulestr) is true.
4247 * The label to be audited is created if necessay.
4248 */
4249static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
4250{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004251 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004252 char **rule = (char **)vrule;
4253 *rule = NULL;
4254
4255 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4256 return -EINVAL;
4257
Al Viro5af75d82008-12-16 05:59:26 -05004258 if (op != Audit_equal && op != Audit_not_equal)
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004259 return -EINVAL;
4260
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004261 skp = smk_import_entry(rulestr, 0);
Lukasz Pawelczyke774ad62015-04-20 17:12:54 +02004262 if (IS_ERR(skp))
4263 return PTR_ERR(skp);
4264
4265 *rule = skp->smk_known;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004266
4267 return 0;
4268}
4269
4270/**
4271 * smack_audit_rule_known - Distinguish Smack audit rules
4272 * @krule: rule of interest, in Audit kernel representation format
4273 *
4274 * This is used to filter Smack rules from remaining Audit ones.
4275 * If it's proved that this rule belongs to us, the
4276 * audit_rule_match hook will be called to do the final judgement.
4277 */
4278static int smack_audit_rule_known(struct audit_krule *krule)
4279{
4280 struct audit_field *f;
4281 int i;
4282
4283 for (i = 0; i < krule->field_count; i++) {
4284 f = &krule->fields[i];
4285
4286 if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
4287 return 1;
4288 }
4289
4290 return 0;
4291}
4292
4293/**
4294 * smack_audit_rule_match - Audit given object ?
4295 * @secid: security id for identifying the object to test
4296 * @field: audit rule flags given from user-space
4297 * @op: required testing operator
4298 * @vrule: smack internal rule presentation
4299 * @actx: audit context associated with the check
4300 *
4301 * The core Audit hook. It's used to take the decision of
4302 * whether to audit or not to audit a given object.
4303 */
4304static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
4305 struct audit_context *actx)
4306{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004307 struct smack_known *skp;
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004308 char *rule = vrule;
4309
Richard Guy Briggs4eb0f4a2013-11-21 13:57:33 -05004310 if (unlikely(!rule)) {
4311 WARN_ONCE(1, "Smack: missing rule\n");
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004312 return -ENOENT;
4313 }
4314
4315 if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
4316 return 0;
4317
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004318 skp = smack_from_secid(secid);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004319
4320 /*
4321 * No need to do string comparisons. If a match occurs,
4322 * both pointers will point to the same smack_known
4323 * label.
4324 */
Al Viro5af75d82008-12-16 05:59:26 -05004325 if (op == Audit_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004326 return (rule == skp->smk_known);
Al Viro5af75d82008-12-16 05:59:26 -05004327 if (op == Audit_not_equal)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004328 return (rule != skp->smk_known);
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004329
4330 return 0;
4331}
4332
Casey Schaufler491a0b02016-01-26 15:08:35 -08004333/*
4334 * There is no need for a smack_audit_rule_free hook.
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004335 * No memory was allocated.
4336 */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004337
4338#endif /* CONFIG_AUDIT */
4339
Randy Dunlap251a2a92009-02-18 11:42:33 -08004340/**
David Quigley746df9b2013-05-22 12:50:35 -04004341 * smack_ismaclabel - check if xattr @name references a smack MAC label
4342 * @name: Full xattr name to check.
4343 */
4344static int smack_ismaclabel(const char *name)
4345{
4346 return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
4347}
4348
4349
4350/**
Casey Schauflere114e472008-02-04 22:29:50 -08004351 * smack_secid_to_secctx - return the smack label for a secid
4352 * @secid: incoming integer
4353 * @secdata: destination
4354 * @seclen: how long it is
4355 *
4356 * Exists for networking code.
4357 */
4358static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
4359{
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004360 struct smack_known *skp = smack_from_secid(secid);
Casey Schauflere114e472008-02-04 22:29:50 -08004361
Eric Parisd5630b92010-10-13 16:24:48 -04004362 if (secdata)
Casey Schaufler2f823ff2013-05-22 18:43:03 -07004363 *secdata = skp->smk_known;
4364 *seclen = strlen(skp->smk_known);
Casey Schauflere114e472008-02-04 22:29:50 -08004365 return 0;
4366}
4367
Randy Dunlap251a2a92009-02-18 11:42:33 -08004368/**
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004369 * smack_secctx_to_secid - return the secid for a smack label
4370 * @secdata: smack label
4371 * @seclen: how long result is
4372 * @secid: outgoing integer
4373 *
4374 * Exists for audit and networking code.
4375 */
David Howellse52c17642008-04-29 20:52:51 +01004376static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004377{
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +02004378 struct smack_known *skp = smk_find_entry(secdata);
4379
4380 if (skp)
4381 *secid = skp->smk_secid;
4382 else
4383 *secid = 0;
Casey Schaufler4bc87e62008-02-15 15:24:25 -08004384 return 0;
4385}
4386
Casey Schaufler491a0b02016-01-26 15:08:35 -08004387/*
4388 * There used to be a smack_release_secctx hook
4389 * that did nothing back when hooks were in a vector.
4390 * Now that there's a list such a hook adds cost.
Casey Schauflere114e472008-02-04 22:29:50 -08004391 */
Casey Schauflere114e472008-02-04 22:29:50 -08004392
David P. Quigley1ee65e32009-09-03 14:25:57 -04004393static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
4394{
4395 return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
4396}
4397
4398static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
4399{
4400 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
4401}
4402
4403static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
4404{
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004405 struct smack_known *skp = smk_of_inode(inode);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004406
Casey Schaufler0f8983c2018-06-01 10:45:12 -07004407 *ctx = skp->smk_known;
4408 *ctxlen = strlen(skp->smk_known);
David P. Quigley1ee65e32009-09-03 14:25:57 -04004409 return 0;
4410}
4411
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004412static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
4413{
4414
4415 struct task_smack *tsp;
4416 struct smack_known *skp;
4417 struct inode_smack *isp;
4418 struct cred *new_creds = *new;
4419
4420 if (new_creds == NULL) {
4421 new_creds = prepare_creds();
4422 if (new_creds == NULL)
4423 return -ENOMEM;
4424 }
4425
Casey Schauflerb17103a2018-11-09 16:12:56 -08004426 tsp = smack_cred(new_creds);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004427
4428 /*
4429 * Get label from overlay inode and set it in create_sid
4430 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004431 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004432 skp = isp->smk_inode;
4433 tsp->smk_task = skp;
4434 *new = new_creds;
4435 return 0;
4436}
4437
4438static int smack_inode_copy_up_xattr(const char *name)
4439{
4440 /*
4441 * Return 1 if this is the smack access Smack attribute.
4442 */
4443 if (strcmp(name, XATTR_NAME_SMACK) == 0)
4444 return 1;
4445
4446 return -EOPNOTSUPP;
4447}
4448
4449static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
4450 struct qstr *name,
4451 const struct cred *old,
4452 struct cred *new)
4453{
Casey Schauflerb17103a2018-11-09 16:12:56 -08004454 struct task_smack *otsp = smack_cred(old);
4455 struct task_smack *ntsp = smack_cred(new);
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004456 struct inode_smack *isp;
4457 int may;
4458
4459 /*
4460 * Use the process credential unless all of
4461 * the transmuting criteria are met
4462 */
4463 ntsp->smk_task = otsp->smk_task;
4464
4465 /*
4466 * the attribute of the containing directory
4467 */
Casey Schauflerfb4021b2018-11-12 12:43:01 -08004468 isp = smack_inode(d_inode(dentry->d_parent));
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004469
4470 if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
4471 rcu_read_lock();
4472 may = smk_access_entry(otsp->smk_task->smk_known,
4473 isp->smk_inode->smk_known,
4474 &otsp->smk_task->smk_rules);
4475 rcu_read_unlock();
4476
4477 /*
4478 * If the directory is transmuting and the rule
4479 * providing access is transmuting use the containing
4480 * directory label instead of the process label.
4481 */
4482 if (may > 0 && (may & MAY_TRANSMUTE))
4483 ntsp->smk_task = isp->smk_inode;
4484 }
4485 return 0;
4486}
4487
Casey Schauflerbbd36622018-11-12 09:30:56 -08004488struct lsm_blob_sizes smack_blob_sizes __lsm_ro_after_init = {
4489 .lbs_cred = sizeof(struct task_smack),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08004490 .lbs_file = sizeof(struct smack_known *),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07004491 .lbs_inode = sizeof(struct inode_smack),
Casey Schauflerecd5f822018-11-20 11:55:02 -08004492 .lbs_ipc = sizeof(struct smack_known *),
4493 .lbs_msg_msg = sizeof(struct smack_known *),
Casey Schauflerbbd36622018-11-12 09:30:56 -08004494};
4495
James Morrisca97d932017-02-15 00:18:51 +11004496static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07004497 LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
4498 LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
4499 LSM_HOOK_INIT(syslog, smack_syslog),
Casey Schauflere114e472008-02-04 22:29:50 -08004500
Casey Schauflere20b0432015-05-02 15:11:36 -07004501 LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
4502 LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05004503 LSM_HOOK_INIT(sb_free_mnt_opts, smack_free_mnt_opts),
Al Viro5b400232018-12-12 20:13:29 -05004504 LSM_HOOK_INIT(sb_eat_lsm_opts, smack_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07004505 LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
Vivek Trivedi3bf27892015-06-22 15:36:06 +05304506 LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
Casey Schauflere114e472008-02-04 22:29:50 -08004507
Casey Schauflere20b0432015-05-02 15:11:36 -07004508 LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),
Casey Schaufler676dac42010-12-02 06:43:39 -08004509
Casey Schauflere20b0432015-05-02 15:11:36 -07004510 LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004511 LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
4512 LSM_HOOK_INIT(inode_link, smack_inode_link),
4513 LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
4514 LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
4515 LSM_HOOK_INIT(inode_rename, smack_inode_rename),
4516 LSM_HOOK_INIT(inode_permission, smack_inode_permission),
4517 LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
4518 LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
4519 LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
4520 LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
4521 LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
4522 LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
4523 LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
4524 LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
4525 LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
4526 LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004527
Casey Schauflere20b0432015-05-02 15:11:36 -07004528 LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004529 LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
4530 LSM_HOOK_INIT(file_lock, smack_file_lock),
4531 LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
4532 LSM_HOOK_INIT(mmap_file, smack_mmap_file),
4533 LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
4534 LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
4535 LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
4536 LSM_HOOK_INIT(file_receive, smack_file_receive),
Casey Schauflere114e472008-02-04 22:29:50 -08004537
Casey Schauflere20b0432015-05-02 15:11:36 -07004538 LSM_HOOK_INIT(file_open, smack_file_open),
Casey Schaufler531f1d42011-09-19 12:41:42 -07004539
Casey Schauflere20b0432015-05-02 15:11:36 -07004540 LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
4541 LSM_HOOK_INIT(cred_free, smack_cred_free),
4542 LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
4543 LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08004544 LSM_HOOK_INIT(cred_getsecid, smack_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004545 LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
4546 LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
4547 LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
4548 LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
4549 LSM_HOOK_INIT(task_getsid, smack_task_getsid),
4550 LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
4551 LSM_HOOK_INIT(task_setnice, smack_task_setnice),
4552 LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
4553 LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
4554 LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
4555 LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
4556 LSM_HOOK_INIT(task_movememory, smack_task_movememory),
4557 LSM_HOOK_INIT(task_kill, smack_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07004558 LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),
Casey Schauflere114e472008-02-04 22:29:50 -08004559
Casey Schauflere20b0432015-05-02 15:11:36 -07004560 LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
4561 LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),
Casey Schauflere114e472008-02-04 22:29:50 -08004562
Casey Schauflere20b0432015-05-02 15:11:36 -07004563 LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
Casey Schauflere114e472008-02-04 22:29:50 -08004564
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004565 LSM_HOOK_INIT(msg_queue_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004566 LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
4567 LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
4568 LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
4569 LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),
Casey Schauflere114e472008-02-04 22:29:50 -08004570
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004571 LSM_HOOK_INIT(shm_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004572 LSM_HOOK_INIT(shm_associate, smack_shm_associate),
4573 LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
4574 LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),
Casey Schauflere114e472008-02-04 22:29:50 -08004575
Eric W. Biederman0d79cbf2018-03-23 23:56:19 -05004576 LSM_HOOK_INIT(sem_alloc_security, smack_ipc_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07004577 LSM_HOOK_INIT(sem_associate, smack_sem_associate),
4578 LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
4579 LSM_HOOK_INIT(sem_semop, smack_sem_semop),
Casey Schauflere114e472008-02-04 22:29:50 -08004580
Casey Schauflere20b0432015-05-02 15:11:36 -07004581 LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),
Casey Schauflere114e472008-02-04 22:29:50 -08004582
Casey Schauflere20b0432015-05-02 15:11:36 -07004583 LSM_HOOK_INIT(getprocattr, smack_getprocattr),
4584 LSM_HOOK_INIT(setprocattr, smack_setprocattr),
Casey Schauflere114e472008-02-04 22:29:50 -08004585
Casey Schauflere20b0432015-05-02 15:11:36 -07004586 LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
4587 LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),
Casey Schauflere114e472008-02-04 22:29:50 -08004588
Casey Schauflere20b0432015-05-02 15:11:36 -07004589 LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
Tom Gundersen5859cdf2018-05-04 16:28:22 +02004590 LSM_HOOK_INIT(socket_socketpair, smack_socket_socketpair),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004591#ifdef SMACK_IPV6_PORT_LABELING
Casey Schauflere20b0432015-05-02 15:11:36 -07004592 LSM_HOOK_INIT(socket_bind, smack_socket_bind),
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004593#endif
Casey Schauflere20b0432015-05-02 15:11:36 -07004594 LSM_HOOK_INIT(socket_connect, smack_socket_connect),
4595 LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
4596 LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
4597 LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
4598 LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
4599 LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
4600 LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
4601 LSM_HOOK_INIT(sock_graft, smack_sock_graft),
4602 LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
4603 LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004604
Casey Schauflere114e472008-02-04 22:29:50 -08004605 /* key management security hooks */
4606#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07004607 LSM_HOOK_INIT(key_alloc, smack_key_alloc),
4608 LSM_HOOK_INIT(key_free, smack_key_free),
4609 LSM_HOOK_INIT(key_permission, smack_key_permission),
4610 LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
Casey Schauflere114e472008-02-04 22:29:50 -08004611#endif /* CONFIG_KEYS */
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004612
4613 /* Audit hooks */
4614#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07004615 LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
4616 LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
4617 LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
Ahmed S. Darwishd20bdda2008-04-30 08:34:10 +10004618#endif /* CONFIG_AUDIT */
4619
Casey Schauflere20b0432015-05-02 15:11:36 -07004620 LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
4621 LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
4622 LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
Casey Schauflere20b0432015-05-02 15:11:36 -07004623 LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
4624 LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
4625 LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
Casey Schauflerd6d80cb2017-09-28 14:54:50 -07004626 LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
4627 LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
4628 LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
Casey Schauflere114e472008-02-04 22:29:50 -08004629};
4630
Etienne Basset7198e2e2009-03-24 20:53:24 +01004631
Casey Schaufler86812bb2012-04-17 18:55:46 -07004632static __init void init_smack_known_list(void)
Etienne Basset7198e2e2009-03-24 20:53:24 +01004633{
Casey Schaufler86812bb2012-04-17 18:55:46 -07004634 /*
Casey Schaufler86812bb2012-04-17 18:55:46 -07004635 * Initialize rule list locks
4636 */
4637 mutex_init(&smack_known_huh.smk_rules_lock);
4638 mutex_init(&smack_known_hat.smk_rules_lock);
4639 mutex_init(&smack_known_floor.smk_rules_lock);
4640 mutex_init(&smack_known_star.smk_rules_lock);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004641 mutex_init(&smack_known_web.smk_rules_lock);
4642 /*
4643 * Initialize rule lists
4644 */
4645 INIT_LIST_HEAD(&smack_known_huh.smk_rules);
4646 INIT_LIST_HEAD(&smack_known_hat.smk_rules);
4647 INIT_LIST_HEAD(&smack_known_star.smk_rules);
4648 INIT_LIST_HEAD(&smack_known_floor.smk_rules);
Casey Schaufler86812bb2012-04-17 18:55:46 -07004649 INIT_LIST_HEAD(&smack_known_web.smk_rules);
4650 /*
4651 * Create the known labels list
4652 */
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004653 smk_insert_entry(&smack_known_huh);
4654 smk_insert_entry(&smack_known_hat);
4655 smk_insert_entry(&smack_known_star);
4656 smk_insert_entry(&smack_known_floor);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +02004657 smk_insert_entry(&smack_known_web);
Etienne Basset7198e2e2009-03-24 20:53:24 +01004658}
4659
Casey Schauflere114e472008-02-04 22:29:50 -08004660/**
4661 * smack_init - initialize the smack system
4662 *
4663 * Returns 0
4664 */
4665static __init int smack_init(void)
4666{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004667 struct cred *cred = (struct cred *) current->cred;
Casey Schaufler676dac42010-12-02 06:43:39 -08004668 struct task_smack *tsp;
David Howellsd84f4f92008-11-14 10:39:23 +11004669
Rohit1a5b4722014-10-15 17:40:41 +05304670 smack_inode_cache = KMEM_CACHE(inode_smack, 0);
4671 if (!smack_inode_cache)
4672 return -ENOMEM;
4673
Casey Schauflerbbd36622018-11-12 09:30:56 -08004674 lsm_early_cred(cred);
Casey Schaufler676dac42010-12-02 06:43:39 -08004675
Casey Schauflerbbd36622018-11-12 09:30:56 -08004676 /*
4677 * Set the security state for the initial task.
4678 */
4679 tsp = smack_cred(cred);
4680 init_task_smack(tsp, &smack_known_floor, &smack_known_floor);
4681
4682 /*
4683 * Register with LSM
4684 */
4685 security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
José Bollod21b7b02015-10-02 15:15:56 +02004686 smack_enabled = 1;
4687
Casey Schaufler21abb1e2015-07-22 14:25:31 -07004688 pr_info("Smack: Initializing.\n");
4689#ifdef CONFIG_SECURITY_SMACK_NETFILTER
4690 pr_info("Smack: Netfilter enabled.\n");
4691#endif
4692#ifdef SMACK_IPV6_PORT_LABELING
4693 pr_info("Smack: IPv6 port labeling enabled.\n");
4694#endif
4695#ifdef SMACK_IPV6_SECMARK_LABELING
4696 pr_info("Smack: IPv6 Netfilter enabled.\n");
4697#endif
Casey Schauflere114e472008-02-04 22:29:50 -08004698
Casey Schaufler86812bb2012-04-17 18:55:46 -07004699 /* initialize the smack_known_list */
4700 init_smack_known_list();
Casey Schauflere114e472008-02-04 22:29:50 -08004701
Casey Schauflere114e472008-02-04 22:29:50 -08004702 return 0;
4703}
4704
4705/*
4706 * Smack requires early initialization in order to label
4707 * all processes and objects when they are created.
4708 */
Kees Cook3d6e5f62018-10-10 17:18:23 -07004709DEFINE_LSM(smack) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07004710 .name = "smack",
Kees Cook14bd99c2018-09-19 19:57:06 -07004711 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Casey Schauflerbbd36622018-11-12 09:30:56 -08004712 .blobs = &smack_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07004713 .init = smack_init,
4714};