blob: e9e817d097859e203b0a90cb2fa4460445ca7d0e [file] [log] [blame]
Thomas Gleixnera10e7632019-05-31 01:09:32 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Casey Schauflere114e472008-02-04 22:29:50 -08002/*
3 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
4 *
Casey Schauflere114e472008-02-04 22:29:50 -08005 * Author:
6 * Casey Schaufler <casey@schaufler-ca.com>
Casey Schauflere114e472008-02-04 22:29:50 -08007 */
8
9#ifndef _SECURITY_SMACK_H
10#define _SECURITY_SMACK_H
11
12#include <linux/capability.h>
13#include <linux/spinlock.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070014#include <linux/lsm_hooks.h>
Casey Schaufler6d3dc072008-12-31 12:54:12 -050015#include <linux/in.h>
Casey Schaufler21abb1e2015-07-22 14:25:31 -070016#if IS_ENABLED(CONFIG_IPV6)
17#include <linux/in6.h>
18#endif /* CONFIG_IPV6 */
Casey Schauflere114e472008-02-04 22:29:50 -080019#include <net/netlabel.h>
Etienne Basset7198e2e2009-03-24 20:53:24 +010020#include <linux/list.h>
21#include <linux/rculist.h>
Etienne Bassetecfcc532009-04-08 20:40:06 +020022#include <linux/lsm_audit.h>
Casey Schaufler019bcca2018-09-21 17:19:54 -070023#include <linux/msg.h>
Casey Schauflere114e472008-02-04 22:29:50 -080024
25/*
Casey Schaufler21abb1e2015-07-22 14:25:31 -070026 * Use IPv6 port labeling if IPv6 is enabled and secmarks
27 * are not being used.
28 */
29#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
30#define SMACK_IPV6_PORT_LABELING 1
31#endif
32
33#if IS_ENABLED(CONFIG_IPV6) && defined(CONFIG_SECURITY_SMACK_NETFILTER)
34#define SMACK_IPV6_SECMARK_LABELING 1
35#endif
36
37/*
Casey Schauflerf7112e62012-05-06 15:22:02 -070038 * Smack labels were limited to 23 characters for a long time.
39 */
40#define SMK_LABELLEN 24
41#define SMK_LONGLABEL 256
42
43/*
Casey Schauflere114e472008-02-04 22:29:50 -080044 * This is the repository for labels seen so that it is
45 * not necessary to keep allocating tiny chuncks of memory
46 * and so that they can be shared.
47 *
48 * Labels are never modified in place. Anytime a label
49 * is imported (e.g. xattrset on a file) the list is checked
50 * for it and it is added if it doesn't exist. The address
51 * is passed out in either case. Entries are added, but
52 * never deleted.
53 *
54 * Since labels are hanging around anyway it doesn't
55 * hurt to maintain a secid for those awkward situations
56 * where kernel components that ought to use LSM independent
57 * interfaces don't. The secid should go away when all of
58 * these components have been repaired.
59 *
Casey Schauflerf7112e62012-05-06 15:22:02 -070060 * The cipso value associated with the label gets stored here, too.
Casey Schaufler272cd7a2011-09-20 12:24:36 -070061 *
62 * Keep the access rules for this subject label here so that
63 * the entire set of rules does not need to be examined every
64 * time.
Casey Schauflere114e472008-02-04 22:29:50 -080065 */
66struct smack_known {
Casey Schauflerf7112e62012-05-06 15:22:02 -070067 struct list_head list;
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +020068 struct hlist_node smk_hashed;
Casey Schauflerf7112e62012-05-06 15:22:02 -070069 char *smk_known;
70 u32 smk_secid;
71 struct netlbl_lsm_secattr smk_netlabel; /* on wire labels */
72 struct list_head smk_rules; /* access rules */
73 struct mutex smk_rules_lock; /* lock for rules */
Casey Schauflere114e472008-02-04 22:29:50 -080074};
75
76/*
Casey Schaufler2f823ff2013-05-22 18:43:03 -070077 * Maximum number of bytes for the levels in a CIPSO IP option.
78 * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
79 * bigger than can be used, and 24 is the next lower multiple
80 * of 8, and there are too many issues if there isn't space set
81 * aside for the terminating null byte.
82 */
83#define SMK_CIPSOLEN 24
84
85struct superblock_smack {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +020086 struct smack_known *smk_root;
87 struct smack_known *smk_floor;
88 struct smack_known *smk_hat;
89 struct smack_known *smk_default;
Seth Forshee9f50eda2015-09-23 15:16:06 -050090 int smk_flags;
Casey Schaufler2f823ff2013-05-22 18:43:03 -070091};
92
Seth Forshee9f50eda2015-09-23 15:16:06 -050093/*
94 * Superblock flags
95 */
96#define SMK_SB_INITIALIZED 0x01
97#define SMK_SB_UNTRUSTED 0x02
98
Casey Schaufler2f823ff2013-05-22 18:43:03 -070099struct socket_smack {
100 struct smack_known *smk_out; /* outbound label */
Casey Schaufler54e70ec2014-04-10 16:37:08 -0700101 struct smack_known *smk_in; /* inbound label */
102 struct smack_known *smk_packet; /* TCP peer label */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700103};
104
105/*
106 * Inode smack data
107 */
108struct inode_smack {
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200109 struct smack_known *smk_inode; /* label of the fso */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700110 struct smack_known *smk_task; /* label of the task */
111 struct smack_known *smk_mmap; /* label of the mmap domain */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700112 int smk_flags; /* smack inode flags */
113};
114
115struct task_smack {
116 struct smack_known *smk_task; /* label for access control */
117 struct smack_known *smk_forked; /* label when forked */
118 struct list_head smk_rules; /* per task access rules */
119 struct mutex smk_rules_lock; /* lock for the rules */
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200120 struct list_head smk_relabel; /* transit allowed labels */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700121};
122
123#define SMK_INODE_INSTANT 0x01 /* inode is instantiated */
124#define SMK_INODE_TRANSMUTE 0x02 /* directory is transmuting */
125#define SMK_INODE_CHANGED 0x04 /* smack was transmuted */
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700126#define SMK_INODE_IMPURE 0x08 /* involved in an impure transaction */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700127
128/*
129 * A label access rule.
130 */
131struct smack_rule {
132 struct list_head list;
133 struct smack_known *smk_subject;
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200134 struct smack_known *smk_object;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700135 int smk_access;
136};
137
138/*
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700139 * An entry in the table identifying IPv4 hosts.
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700140 */
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700141struct smk_net4addr {
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700142 struct list_head list;
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700143 struct in_addr smk_host; /* network address */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700144 struct in_addr smk_mask; /* network mask */
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700145 int smk_masks; /* mask size */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200146 struct smack_known *smk_label; /* label */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700147};
148
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700149/*
150 * An entry in the table identifying IPv6 hosts.
151 */
152struct smk_net6addr {
153 struct list_head list;
154 struct in6_addr smk_host; /* network address */
155 struct in6_addr smk_mask; /* network mask */
156 int smk_masks; /* mask size */
157 struct smack_known *smk_label; /* label */
158};
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700159
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700160/*
161 * An entry in the table identifying ports.
162 */
163struct smk_port_label {
164 struct list_head list;
165 struct sock *smk_sock; /* socket initialized on */
166 unsigned short smk_port; /* the port number */
Casey Schaufler54e70ec2014-04-10 16:37:08 -0700167 struct smack_known *smk_in; /* inbound label */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700168 struct smack_known *smk_out; /* outgoing label */
Vishal Goel9d44c972016-11-23 10:31:59 +0530169 short smk_sock_type; /* Socket type */
Vishal Goel0c96d1f2016-11-23 10:32:54 +0530170 short smk_can_reuse;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700171};
172
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200173struct smack_known_list_elem {
Rafal Krypac0d77c82015-06-02 11:23:48 +0200174 struct list_head list;
175 struct smack_known *smk_label;
176};
177
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530178/* Super block security struct flags for mount options */
179#define FSDEFAULT_MNT 0x01
180#define FSFLOOR_MNT 0x02
181#define FSHAT_MNT 0x04
182#define FSROOT_MNT 0x08
183#define FSTRANS_MNT 0x10
184
185#define NUM_SMK_MNT_OPTS 5
186
187enum {
188 Opt_error = -1,
David Howells2febd252018-11-01 23:07:24 +0000189 Opt_fsdefault = 0,
190 Opt_fsfloor = 1,
191 Opt_fshat = 2,
192 Opt_fsroot = 3,
193 Opt_fstransmute = 4,
Vivek Trivedi3bf27892015-06-22 15:36:06 +0530194};
195
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700196#define SMACK_DELETE_OPTION "-DELETE"
Etienne Basset43031542009-03-27 17:11:01 -0400197#define SMACK_CIPSO_OPTION "-CIPSO"
198
Casey Schauflere114e472008-02-04 22:29:50 -0800199/*
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500200 * How communications on this socket are treated.
201 * Usually it's determined by the underlying netlabel code
202 * but there are certain cases, including single label hosts
203 * and potentially single label interfaces for which the
204 * treatment can not be known in advance.
205 *
206 * The possibility of additional labeling schemes being
207 * introduced in the future exists as well.
208 */
209#define SMACK_UNLABELED_SOCKET 0
210#define SMACK_CIPSO_SOCKET 1
211
212/*
Casey Schauflere114e472008-02-04 22:29:50 -0800213 * CIPSO defaults.
214 */
215#define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500216#define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */
Casey Schauflere114e472008-02-04 22:29:50 -0800217#define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */
Casey Schauflerf7112e62012-05-06 15:22:02 -0700218#define SMACK_CIPSO_MAPPED_DEFAULT 251 /* Also arbitrary */
Casey Schauflere114e472008-02-04 22:29:50 -0800219#define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */
Casey Schaufler677264e2013-06-28 13:47:07 -0700220/*
221 * CIPSO 2.2 standard is 239, but Smack wants to use the
222 * categories in a structured way that limits the value to
223 * the bits in 23 bytes, hence the unusual number.
224 */
225#define SMACK_CIPSO_MAXCATNUM 184 /* 23 * 8 */
Casey Schauflere114e472008-02-04 22:29:50 -0800226
227/*
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100228 * Ptrace rules
229 */
230#define SMACK_PTRACE_DEFAULT 0
231#define SMACK_PTRACE_EXACT 1
232#define SMACK_PTRACE_DRACONIAN 2
233#define SMACK_PTRACE_MAX SMACK_PTRACE_DRACONIAN
234
235/*
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700236 * Flags for untraditional access modes.
237 * It shouldn't be necessary to avoid conflicts with definitions
238 * in fs.h, but do so anyway.
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200239 */
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700240#define MAY_TRANSMUTE 0x00001000 /* Controls directory labeling */
241#define MAY_LOCK 0x00002000 /* Locks should be writes, but ... */
Casey Schauflerd166c802014-08-27 14:51:27 -0700242#define MAY_BRINGUP 0x00004000 /* Report use of this rule */
Casey Schauflerc0ab6e52013-10-11 18:06:39 -0700243
Casey Schauflerc60b9062016-08-30 10:31:39 -0700244/*
245 * The policy for delivering signals is configurable.
246 * It is usually "write", but can be "append".
247 */
248#ifdef CONFIG_SECURITY_SMACK_APPEND_SIGNALS
249#define MAY_DELIVER MAY_APPEND /* Signal delivery requires append */
250#else
251#define MAY_DELIVER MAY_WRITE /* Signal delivery requires write */
252#endif
253
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700254#define SMACK_BRINGUP_ALLOW 1 /* Allow bringup mode */
255#define SMACK_UNCONFINED_SUBJECT 2 /* Allow unconfined label */
256#define SMACK_UNCONFINED_OBJECT 3 /* Allow unconfined label */
257
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200258/*
Casey Schauflere114e472008-02-04 22:29:50 -0800259 * Just to make the common cases easier to deal with
260 */
Casey Schauflere114e472008-02-04 22:29:50 -0800261#define MAY_ANYREAD (MAY_READ | MAY_EXEC)
Casey Schauflere114e472008-02-04 22:29:50 -0800262#define MAY_READWRITE (MAY_READ | MAY_WRITE)
263#define MAY_NOT 0
264
265/*
Casey Schauflerd166c802014-08-27 14:51:27 -0700266 * Number of access types used by Smack (rwxatlb)
Etienne Bassetecfcc532009-04-08 20:40:06 +0200267 */
Casey Schauflerd166c802014-08-27 14:51:27 -0700268#define SMK_NUM_ACCESS_TYPE 7
Etienne Bassetecfcc532009-04-08 20:40:06 +0200269
Eric Paris3b3b0e42012-04-03 09:37:02 -0700270/* SMACK data */
271struct smack_audit_data {
272 const char *function;
273 char *subject;
274 char *object;
275 char *request;
276 int result;
277};
278
Etienne Bassetecfcc532009-04-08 20:40:06 +0200279/*
280 * Smack audit data; is empty if CONFIG_AUDIT not set
281 * to save some stack
282 */
283struct smk_audit_info {
284#ifdef CONFIG_AUDIT
285 struct common_audit_data a;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700286 struct smack_audit_data sad;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200287#endif
288};
Casey Schauflere114e472008-02-04 22:29:50 -0800289
290/*
291 * These functions are in smack_access.c
292 */
Casey Schaufler7898e1f2011-01-17 08:05:27 -0800293int smk_access_entry(char *, char *, struct list_head *);
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200294int smk_access(struct smack_known *, struct smack_known *,
295 int, struct smk_audit_info *);
296int smk_tskacc(struct task_smack *, struct smack_known *,
297 u32, struct smk_audit_info *);
298int smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700299struct smack_known *smack_from_secid(const u32);
Casey Schauflerf7112e62012-05-06 15:22:02 -0700300char *smk_parse_smack(const char *string, int len);
301int smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
Casey Schauflere114e472008-02-04 22:29:50 -0800302struct smack_known *smk_import_entry(const char *, int);
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +0200303void smk_insert_entry(struct smack_known *skp);
Casey Schaufler272cd7a2011-09-20 12:24:36 -0700304struct smack_known *smk_find_entry(const char *);
Casey Schauflerf28e7832017-05-31 13:23:41 -0700305bool smack_privileged(int cap);
Casey Schauflerd19dfe52018-01-08 10:25:32 -0800306bool smack_privileged_cred(int cap, const struct cred *cred);
Zbigniew Jasinski38416e52015-10-19 18:23:53 +0200307void smk_destroy_label_list(struct list_head *list);
Casey Schauflere114e472008-02-04 22:29:50 -0800308
309/*
310 * Shared data.
311 */
Casey Schaufler69f287a2014-12-12 17:08:40 -0800312extern int smack_enabled;
Casey Schauflere114e472008-02-04 22:29:50 -0800313extern int smack_cipso_direct;
Casey Schauflerf7112e62012-05-06 15:22:02 -0700314extern int smack_cipso_mapped;
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700315extern struct smack_known *smack_net_ambient;
Casey Schaufler00f84f32013-12-23 11:07:10 -0800316extern struct smack_known *smack_syslog_label;
Casey Schauflerbf4b2fe2015-03-21 18:26:40 -0700317#ifdef CONFIG_SECURITY_SMACK_BRINGUP
318extern struct smack_known *smack_unconfined;
319#endif
Lukasz Pawelczyk66867812014-03-11 17:07:06 +0100320extern int smack_ptrace_rule;
Casey Schauflerbbd36622018-11-12 09:30:56 -0800321extern struct lsm_blob_sizes smack_blob_sizes;
Casey Schauflere114e472008-02-04 22:29:50 -0800322
Casey Schauflere114e472008-02-04 22:29:50 -0800323extern struct smack_known smack_known_floor;
324extern struct smack_known smack_known_hat;
325extern struct smack_known smack_known_huh;
Casey Schauflere114e472008-02-04 22:29:50 -0800326extern struct smack_known smack_known_star;
Casey Schaufler6d3dc072008-12-31 12:54:12 -0500327extern struct smack_known smack_known_web;
Casey Schauflere114e472008-02-04 22:29:50 -0800328
Casey Schauflerf7112e62012-05-06 15:22:02 -0700329extern struct mutex smack_known_lock;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100330extern struct list_head smack_known_list;
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700331extern struct list_head smk_net4addr_list;
Casey Schaufler21abb1e2015-07-22 14:25:31 -0700332extern struct list_head smk_net6addr_list;
Etienne Basset7198e2e2009-03-24 20:53:24 +0100333
Rafal Krypac0d77c82015-06-02 11:23:48 +0200334extern struct mutex smack_onlycap_lock;
335extern struct list_head smack_onlycap_list;
336
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +0200337#define SMACK_HASH_SLOTS 16
338extern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
Casey Schaufler4e328b02019-04-02 11:37:12 -0700339extern struct kmem_cache *smack_rule_cache;
Tomasz Stanislawski4d7cf4a2013-06-11 14:55:13 +0200340
Casey Schauflerb17103a2018-11-09 16:12:56 -0800341static inline struct task_smack *smack_cred(const struct cred *cred)
342{
Casey Schauflerbbd36622018-11-12 09:30:56 -0800343 return cred->security + smack_blob_sizes.lbs_cred;
Casey Schauflerb17103a2018-11-09 16:12:56 -0800344}
345
Casey Schauflerf28952a2018-11-12 09:38:53 -0800346static inline struct smack_known **smack_file(const struct file *file)
347{
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800348 return (struct smack_known **)(file->f_security +
349 smack_blob_sizes.lbs_file);
Casey Schauflerf28952a2018-11-12 09:38:53 -0800350}
351
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800352static inline struct inode_smack *smack_inode(const struct inode *inode)
353{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700354 return inode->i_security + smack_blob_sizes.lbs_inode;
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800355}
356
Casey Schaufler019bcca2018-09-21 17:19:54 -0700357static inline struct smack_known **smack_msg_msg(const struct msg_msg *msg)
358{
Casey Schauflerecd5f822018-11-20 11:55:02 -0800359 return msg->security + smack_blob_sizes.lbs_msg_msg;
Casey Schaufler019bcca2018-09-21 17:19:54 -0700360}
361
362static inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc)
363{
Casey Schauflerecd5f822018-11-20 11:55:02 -0800364 return ipc->security + smack_blob_sizes.lbs_ipc;
Casey Schaufler019bcca2018-09-21 17:19:54 -0700365}
366
Casey Schauflere114e472008-02-04 22:29:50 -0800367/*
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200368 * Is the directory transmuting?
369 */
370static inline int smk_inode_transmutable(const struct inode *isp)
371{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800372 struct inode_smack *sip = smack_inode(isp);
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200373 return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
374}
375
376/*
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200377 * Present a pointer to the smack label entry in an inode blob.
Casey Schauflere114e472008-02-04 22:29:50 -0800378 */
Lukasz Pawelczyk21c7eae2014-08-29 17:02:55 +0200379static inline struct smack_known *smk_of_inode(const struct inode *isp)
Casey Schauflere114e472008-02-04 22:29:50 -0800380{
Casey Schauflerfb4021b2018-11-12 12:43:01 -0800381 struct inode_smack *sip = smack_inode(isp);
Casey Schauflere114e472008-02-04 22:29:50 -0800382 return sip->smk_inode;
383}
384
Etienne Bassetecfcc532009-04-08 20:40:06 +0200385/*
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700386 * Present a pointer to the smack label entry in an task blob.
Casey Schaufler676dac42010-12-02 06:43:39 -0800387 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700388static inline struct smack_known *smk_of_task(const struct task_smack *tsp)
Casey Schaufler676dac42010-12-02 06:43:39 -0800389{
390 return tsp->smk_task;
391}
392
Casey Schauflerb17103a2018-11-09 16:12:56 -0800393static inline struct smack_known *smk_of_task_struct(
394 const struct task_struct *t)
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300395{
396 struct smack_known *skp;
Casey Schauflerb17103a2018-11-09 16:12:56 -0800397 const struct cred *cred;
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300398
399 rcu_read_lock();
Casey Schauflerb17103a2018-11-09 16:12:56 -0800400
401 cred = __task_cred(t);
402 skp = smk_of_task(smack_cred(cred));
403
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300404 rcu_read_unlock();
Casey Schauflerb17103a2018-11-09 16:12:56 -0800405
Andrey Ryabinin6d1cff22015-01-13 18:52:40 +0300406 return skp;
407}
408
Casey Schaufler676dac42010-12-02 06:43:39 -0800409/*
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700410 * Present a pointer to the forked smack label entry in an task blob.
Casey Schaufler676dac42010-12-02 06:43:39 -0800411 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700412static inline struct smack_known *smk_of_forked(const struct task_smack *tsp)
Casey Schaufler676dac42010-12-02 06:43:39 -0800413{
414 return tsp->smk_forked;
415}
416
417/*
Jarkko Sakkinen5c6d1122010-12-07 13:34:01 +0200418 * Present a pointer to the smack label in the current task blob.
Casey Schaufler676dac42010-12-02 06:43:39 -0800419 */
Casey Schaufler2f823ff2013-05-22 18:43:03 -0700420static inline struct smack_known *smk_of_current(void)
Casey Schaufler676dac42010-12-02 06:43:39 -0800421{
Casey Schauflerb17103a2018-11-09 16:12:56 -0800422 return smk_of_task(smack_cred(current_cred()));
Casey Schaufler676dac42010-12-02 06:43:39 -0800423}
424
425/*
Etienne Bassetecfcc532009-04-08 20:40:06 +0200426 * logging functions
427 */
428#define SMACK_AUDIT_DENIED 0x1
429#define SMACK_AUDIT_ACCEPT 0x2
430extern int log_policy;
431
432void smack_log(char *subject_label, char *object_label,
433 int request,
434 int result, struct smk_audit_info *auditdata);
435
436#ifdef CONFIG_AUDIT
437
438/*
439 * some inline functions to set up audit data
440 * they do nothing if CONFIG_AUDIT is not set
441 *
442 */
443static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
444 char type)
445{
Eric Paris50c205f2012-04-04 15:01:43 -0400446 memset(&a->sad, 0, sizeof(a->sad));
Etienne Bassetecfcc532009-04-08 20:40:06 +0200447 a->a.type = type;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700448 a->a.smack_audit_data = &a->sad;
449 a->a.smack_audit_data->function = func;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200450}
451
Eric Paris48c62af2012-04-02 13:15:44 -0400452static inline void smk_ad_init_net(struct smk_audit_info *a, const char *func,
453 char type, struct lsm_network_audit *net)
454{
455 smk_ad_init(a, func, type);
456 memset(net, 0, sizeof(*net));
457 a->a.u.net = net;
458}
459
Etienne Bassetecfcc532009-04-08 20:40:06 +0200460static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
461 struct task_struct *t)
462{
463 a->a.u.tsk = t;
464}
465static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
466 struct dentry *d)
467{
Eric Parisa2694342011-04-25 13:10:27 -0400468 a->a.u.dentry = d;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200469}
470static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
471 struct inode *i)
472{
Eric Parisf48b7392011-04-25 12:54:27 -0400473 a->a.u.inode = i;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200474}
475static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
476 struct path p)
477{
Eric Parisf48b7392011-04-25 12:54:27 -0400478 a->a.u.path = p;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200479}
480static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
481 struct sock *sk)
482{
Eric Paris48c62af2012-04-02 13:15:44 -0400483 a->a.u.net->sk = sk;
Etienne Bassetecfcc532009-04-08 20:40:06 +0200484}
485
486#else /* no AUDIT */
487
488static inline void smk_ad_init(struct smk_audit_info *a, const char *func,
489 char type)
490{
491}
492static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
493 struct task_struct *t)
494{
495}
496static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
497 struct dentry *d)
498{
499}
Etienne Bassetecfcc532009-04-08 20:40:06 +0200500static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
501 struct inode *i)
502{
503}
504static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
505 struct path p)
506{
507}
508static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
509 struct sock *sk)
510{
511}
512#endif
513
Casey Schauflere114e472008-02-04 22:29:50 -0800514#endif /* _SECURITY_SMACK_H */