162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Author:
662306a36Sopenharmony_ci *      Casey Schaufler <casey@schaufler-ca.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef _SECURITY_SMACK_H
1062306a36Sopenharmony_ci#define _SECURITY_SMACK_H
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/capability.h>
1362306a36Sopenharmony_ci#include <linux/spinlock.h>
1462306a36Sopenharmony_ci#include <linux/lsm_hooks.h>
1562306a36Sopenharmony_ci#include <linux/in.h>
1662306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
1762306a36Sopenharmony_ci#include <linux/in6.h>
1862306a36Sopenharmony_ci#endif /* CONFIG_IPV6 */
1962306a36Sopenharmony_ci#include <net/netlabel.h>
2062306a36Sopenharmony_ci#include <linux/list.h>
2162306a36Sopenharmony_ci#include <linux/rculist.h>
2262306a36Sopenharmony_ci#include <linux/lsm_audit.h>
2362306a36Sopenharmony_ci#include <linux/msg.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * Use IPv6 port labeling if IPv6 is enabled and secmarks
2762306a36Sopenharmony_ci * are not being used.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
3062306a36Sopenharmony_ci#define SMACK_IPV6_PORT_LABELING 1
3162306a36Sopenharmony_ci#endif
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) && defined(CONFIG_SECURITY_SMACK_NETFILTER)
3462306a36Sopenharmony_ci#define SMACK_IPV6_SECMARK_LABELING 1
3562306a36Sopenharmony_ci#endif
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/*
3862306a36Sopenharmony_ci * Smack labels were limited to 23 characters for a long time.
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_ci#define SMK_LABELLEN	24
4162306a36Sopenharmony_ci#define SMK_LONGLABEL	256
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/*
4462306a36Sopenharmony_ci * This is the repository for labels seen so that it is
4562306a36Sopenharmony_ci * not necessary to keep allocating tiny chuncks of memory
4662306a36Sopenharmony_ci * and so that they can be shared.
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * Labels are never modified in place. Anytime a label
4962306a36Sopenharmony_ci * is imported (e.g. xattrset on a file) the list is checked
5062306a36Sopenharmony_ci * for it and it is added if it doesn't exist. The address
5162306a36Sopenharmony_ci * is passed out in either case. Entries are added, but
5262306a36Sopenharmony_ci * never deleted.
5362306a36Sopenharmony_ci *
5462306a36Sopenharmony_ci * Since labels are hanging around anyway it doesn't
5562306a36Sopenharmony_ci * hurt to maintain a secid for those awkward situations
5662306a36Sopenharmony_ci * where kernel components that ought to use LSM independent
5762306a36Sopenharmony_ci * interfaces don't. The secid should go away when all of
5862306a36Sopenharmony_ci * these components have been repaired.
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * The cipso value associated with the label gets stored here, too.
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * Keep the access rules for this subject label here so that
6362306a36Sopenharmony_ci * the entire set of rules does not need to be examined every
6462306a36Sopenharmony_ci * time.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_cistruct smack_known {
6762306a36Sopenharmony_ci	struct list_head		list;
6862306a36Sopenharmony_ci	struct hlist_node		smk_hashed;
6962306a36Sopenharmony_ci	char				*smk_known;
7062306a36Sopenharmony_ci	u32				smk_secid;
7162306a36Sopenharmony_ci	struct netlbl_lsm_secattr	smk_netlabel;	/* on wire labels */
7262306a36Sopenharmony_ci	struct list_head		smk_rules;	/* access rules */
7362306a36Sopenharmony_ci	struct mutex			smk_rules_lock;	/* lock for rules */
7462306a36Sopenharmony_ci};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/*
7762306a36Sopenharmony_ci * Maximum number of bytes for the levels in a CIPSO IP option.
7862306a36Sopenharmony_ci * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
7962306a36Sopenharmony_ci * bigger than can be used, and 24 is the next lower multiple
8062306a36Sopenharmony_ci * of 8, and there are too many issues if there isn't space set
8162306a36Sopenharmony_ci * aside for the terminating null byte.
8262306a36Sopenharmony_ci */
8362306a36Sopenharmony_ci#define SMK_CIPSOLEN	24
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistruct superblock_smack {
8662306a36Sopenharmony_ci	struct smack_known	*smk_root;
8762306a36Sopenharmony_ci	struct smack_known	*smk_floor;
8862306a36Sopenharmony_ci	struct smack_known	*smk_hat;
8962306a36Sopenharmony_ci	struct smack_known	*smk_default;
9062306a36Sopenharmony_ci	int			smk_flags;
9162306a36Sopenharmony_ci};
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/*
9462306a36Sopenharmony_ci * Superblock flags
9562306a36Sopenharmony_ci */
9662306a36Sopenharmony_ci#define SMK_SB_INITIALIZED	0x01
9762306a36Sopenharmony_ci#define SMK_SB_UNTRUSTED	0x02
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistruct socket_smack {
10062306a36Sopenharmony_ci	struct smack_known	*smk_out;	/* outbound label */
10162306a36Sopenharmony_ci	struct smack_known	*smk_in;	/* inbound label */
10262306a36Sopenharmony_ci	struct smack_known	*smk_packet;	/* TCP peer label */
10362306a36Sopenharmony_ci	int			smk_state;	/* netlabel socket states */
10462306a36Sopenharmony_ci};
10562306a36Sopenharmony_ci#define	SMK_NETLBL_UNSET	0
10662306a36Sopenharmony_ci#define	SMK_NETLBL_UNLABELED	1
10762306a36Sopenharmony_ci#define	SMK_NETLBL_LABELED	2
10862306a36Sopenharmony_ci#define	SMK_NETLBL_REQSKB	3
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/*
11162306a36Sopenharmony_ci * Inode smack data
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_cistruct inode_smack {
11462306a36Sopenharmony_ci	struct smack_known	*smk_inode;	/* label of the fso */
11562306a36Sopenharmony_ci	struct smack_known	*smk_task;	/* label of the task */
11662306a36Sopenharmony_ci	struct smack_known	*smk_mmap;	/* label of the mmap domain */
11762306a36Sopenharmony_ci	int			smk_flags;	/* smack inode flags */
11862306a36Sopenharmony_ci};
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistruct task_smack {
12162306a36Sopenharmony_ci	struct smack_known	*smk_task;	/* label for access control */
12262306a36Sopenharmony_ci	struct smack_known	*smk_forked;	/* label when forked */
12362306a36Sopenharmony_ci	struct smack_known	*smk_transmuted;/* label when transmuted */
12462306a36Sopenharmony_ci	struct list_head	smk_rules;	/* per task access rules */
12562306a36Sopenharmony_ci	struct mutex		smk_rules_lock;	/* lock for the rules */
12662306a36Sopenharmony_ci	struct list_head	smk_relabel;	/* transit allowed labels */
12762306a36Sopenharmony_ci};
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci#define	SMK_INODE_INSTANT	0x01	/* inode is instantiated */
13062306a36Sopenharmony_ci#define	SMK_INODE_TRANSMUTE	0x02	/* directory is transmuting */
13162306a36Sopenharmony_ci#define	SMK_INODE_CHANGED	0x04	/* smack was transmuted (unused) */
13262306a36Sopenharmony_ci#define	SMK_INODE_IMPURE	0x08	/* involved in an impure transaction */
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/*
13562306a36Sopenharmony_ci * A label access rule.
13662306a36Sopenharmony_ci */
13762306a36Sopenharmony_cistruct smack_rule {
13862306a36Sopenharmony_ci	struct list_head	list;
13962306a36Sopenharmony_ci	struct smack_known	*smk_subject;
14062306a36Sopenharmony_ci	struct smack_known	*smk_object;
14162306a36Sopenharmony_ci	int			smk_access;
14262306a36Sopenharmony_ci};
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * An entry in the table identifying IPv4 hosts.
14662306a36Sopenharmony_ci */
14762306a36Sopenharmony_cistruct smk_net4addr {
14862306a36Sopenharmony_ci	struct list_head	list;
14962306a36Sopenharmony_ci	struct in_addr		smk_host;	/* network address */
15062306a36Sopenharmony_ci	struct in_addr		smk_mask;	/* network mask */
15162306a36Sopenharmony_ci	int			smk_masks;	/* mask size */
15262306a36Sopenharmony_ci	struct smack_known	*smk_label;	/* label */
15362306a36Sopenharmony_ci};
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci/*
15662306a36Sopenharmony_ci * An entry in the table identifying IPv6 hosts.
15762306a36Sopenharmony_ci */
15862306a36Sopenharmony_cistruct smk_net6addr {
15962306a36Sopenharmony_ci	struct list_head	list;
16062306a36Sopenharmony_ci	struct in6_addr		smk_host;	/* network address */
16162306a36Sopenharmony_ci	struct in6_addr		smk_mask;	/* network mask */
16262306a36Sopenharmony_ci	int			smk_masks;	/* mask size */
16362306a36Sopenharmony_ci	struct smack_known	*smk_label;	/* label */
16462306a36Sopenharmony_ci};
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci/*
16762306a36Sopenharmony_ci * An entry in the table identifying ports.
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_cistruct smk_port_label {
17062306a36Sopenharmony_ci	struct list_head	list;
17162306a36Sopenharmony_ci	struct sock		*smk_sock;	/* socket initialized on */
17262306a36Sopenharmony_ci	unsigned short		smk_port;	/* the port number */
17362306a36Sopenharmony_ci	struct smack_known	*smk_in;	/* inbound label */
17462306a36Sopenharmony_ci	struct smack_known	*smk_out;	/* outgoing label */
17562306a36Sopenharmony_ci	short			smk_sock_type;	/* Socket type */
17662306a36Sopenharmony_ci	short			smk_can_reuse;
17762306a36Sopenharmony_ci};
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cistruct smack_known_list_elem {
18062306a36Sopenharmony_ci	struct list_head	list;
18162306a36Sopenharmony_ci	struct smack_known	*smk_label;
18262306a36Sopenharmony_ci};
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_cienum {
18562306a36Sopenharmony_ci	Opt_error = -1,
18662306a36Sopenharmony_ci	Opt_fsdefault = 0,
18762306a36Sopenharmony_ci	Opt_fsfloor = 1,
18862306a36Sopenharmony_ci	Opt_fshat = 2,
18962306a36Sopenharmony_ci	Opt_fsroot = 3,
19062306a36Sopenharmony_ci	Opt_fstransmute = 4,
19162306a36Sopenharmony_ci};
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci#define SMACK_DELETE_OPTION	"-DELETE"
19462306a36Sopenharmony_ci#define SMACK_CIPSO_OPTION 	"-CIPSO"
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci/*
19762306a36Sopenharmony_ci * CIPSO defaults.
19862306a36Sopenharmony_ci */
19962306a36Sopenharmony_ci#define SMACK_CIPSO_DOI_DEFAULT		3	/* Historical */
20062306a36Sopenharmony_ci#define SMACK_CIPSO_DOI_INVALID		-1	/* Not a DOI */
20162306a36Sopenharmony_ci#define SMACK_CIPSO_DIRECT_DEFAULT	250	/* Arbitrary */
20262306a36Sopenharmony_ci#define SMACK_CIPSO_MAPPED_DEFAULT	251	/* Also arbitrary */
20362306a36Sopenharmony_ci#define SMACK_CIPSO_MAXLEVEL            255     /* CIPSO 2.2 standard */
20462306a36Sopenharmony_ci/*
20562306a36Sopenharmony_ci * CIPSO 2.2 standard is 239, but Smack wants to use the
20662306a36Sopenharmony_ci * categories in a structured way that limits the value to
20762306a36Sopenharmony_ci * the bits in 23 bytes, hence the unusual number.
20862306a36Sopenharmony_ci */
20962306a36Sopenharmony_ci#define SMACK_CIPSO_MAXCATNUM           184     /* 23 * 8 */
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci/*
21262306a36Sopenharmony_ci * Ptrace rules
21362306a36Sopenharmony_ci */
21462306a36Sopenharmony_ci#define SMACK_PTRACE_DEFAULT	0
21562306a36Sopenharmony_ci#define SMACK_PTRACE_EXACT	1
21662306a36Sopenharmony_ci#define SMACK_PTRACE_DRACONIAN	2
21762306a36Sopenharmony_ci#define SMACK_PTRACE_MAX	SMACK_PTRACE_DRACONIAN
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci/*
22062306a36Sopenharmony_ci * Flags for untraditional access modes.
22162306a36Sopenharmony_ci * It shouldn't be necessary to avoid conflicts with definitions
22262306a36Sopenharmony_ci * in fs.h, but do so anyway.
22362306a36Sopenharmony_ci */
22462306a36Sopenharmony_ci#define MAY_TRANSMUTE	0x00001000	/* Controls directory labeling */
22562306a36Sopenharmony_ci#define MAY_LOCK	0x00002000	/* Locks should be writes, but ... */
22662306a36Sopenharmony_ci#define MAY_BRINGUP	0x00004000	/* Report use of this rule */
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci/*
22962306a36Sopenharmony_ci * The policy for delivering signals is configurable.
23062306a36Sopenharmony_ci * It is usually "write", but can be "append".
23162306a36Sopenharmony_ci */
23262306a36Sopenharmony_ci#ifdef CONFIG_SECURITY_SMACK_APPEND_SIGNALS
23362306a36Sopenharmony_ci#define MAY_DELIVER	MAY_APPEND	/* Signal delivery requires append */
23462306a36Sopenharmony_ci#else
23562306a36Sopenharmony_ci#define MAY_DELIVER	MAY_WRITE	/* Signal delivery requires write */
23662306a36Sopenharmony_ci#endif
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci#define SMACK_BRINGUP_ALLOW		1	/* Allow bringup mode */
23962306a36Sopenharmony_ci#define SMACK_UNCONFINED_SUBJECT	2	/* Allow unconfined label */
24062306a36Sopenharmony_ci#define SMACK_UNCONFINED_OBJECT		3	/* Allow unconfined label */
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci/*
24362306a36Sopenharmony_ci * Just to make the common cases easier to deal with
24462306a36Sopenharmony_ci */
24562306a36Sopenharmony_ci#define MAY_ANYREAD	(MAY_READ | MAY_EXEC)
24662306a36Sopenharmony_ci#define MAY_READWRITE	(MAY_READ | MAY_WRITE)
24762306a36Sopenharmony_ci#define MAY_NOT		0
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/*
25062306a36Sopenharmony_ci * Number of access types used by Smack (rwxatlb)
25162306a36Sopenharmony_ci */
25262306a36Sopenharmony_ci#define SMK_NUM_ACCESS_TYPE 7
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci/* SMACK data */
25562306a36Sopenharmony_cistruct smack_audit_data {
25662306a36Sopenharmony_ci	const char *function;
25762306a36Sopenharmony_ci	char *subject;
25862306a36Sopenharmony_ci	char *object;
25962306a36Sopenharmony_ci	char *request;
26062306a36Sopenharmony_ci	int result;
26162306a36Sopenharmony_ci};
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci/*
26462306a36Sopenharmony_ci * Smack audit data; is empty if CONFIG_AUDIT not set
26562306a36Sopenharmony_ci * to save some stack
26662306a36Sopenharmony_ci */
26762306a36Sopenharmony_cistruct smk_audit_info {
26862306a36Sopenharmony_ci#ifdef CONFIG_AUDIT
26962306a36Sopenharmony_ci	struct common_audit_data a;
27062306a36Sopenharmony_ci	struct smack_audit_data sad;
27162306a36Sopenharmony_ci#endif
27262306a36Sopenharmony_ci};
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci/*
27562306a36Sopenharmony_ci * These functions are in smack_access.c
27662306a36Sopenharmony_ci */
27762306a36Sopenharmony_ciint smk_access_entry(char *, char *, struct list_head *);
27862306a36Sopenharmony_ciint smk_access(struct smack_known *, struct smack_known *,
27962306a36Sopenharmony_ci	       int, struct smk_audit_info *);
28062306a36Sopenharmony_ciint smk_tskacc(struct task_smack *, struct smack_known *,
28162306a36Sopenharmony_ci	       u32, struct smk_audit_info *);
28262306a36Sopenharmony_ciint smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
28362306a36Sopenharmony_cistruct smack_known *smack_from_secid(const u32);
28462306a36Sopenharmony_cichar *smk_parse_smack(const char *string, int len);
28562306a36Sopenharmony_ciint smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
28662306a36Sopenharmony_cistruct smack_known *smk_import_entry(const char *, int);
28762306a36Sopenharmony_civoid smk_insert_entry(struct smack_known *skp);
28862306a36Sopenharmony_cistruct smack_known *smk_find_entry(const char *);
28962306a36Sopenharmony_cibool smack_privileged(int cap);
29062306a36Sopenharmony_cibool smack_privileged_cred(int cap, const struct cred *cred);
29162306a36Sopenharmony_civoid smk_destroy_label_list(struct list_head *list);
29262306a36Sopenharmony_ciint smack_populate_secattr(struct smack_known *skp);
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci/*
29562306a36Sopenharmony_ci * Shared data.
29662306a36Sopenharmony_ci */
29762306a36Sopenharmony_ciextern int smack_enabled __initdata;
29862306a36Sopenharmony_ciextern int smack_cipso_direct;
29962306a36Sopenharmony_ciextern int smack_cipso_mapped;
30062306a36Sopenharmony_ciextern struct smack_known *smack_net_ambient;
30162306a36Sopenharmony_ciextern struct smack_known *smack_syslog_label;
30262306a36Sopenharmony_ci#ifdef CONFIG_SECURITY_SMACK_BRINGUP
30362306a36Sopenharmony_ciextern struct smack_known *smack_unconfined;
30462306a36Sopenharmony_ci#endif
30562306a36Sopenharmony_ciextern int smack_ptrace_rule;
30662306a36Sopenharmony_ciextern struct lsm_blob_sizes smack_blob_sizes;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ciextern struct smack_known smack_known_floor;
30962306a36Sopenharmony_ciextern struct smack_known smack_known_hat;
31062306a36Sopenharmony_ciextern struct smack_known smack_known_huh;
31162306a36Sopenharmony_ciextern struct smack_known smack_known_star;
31262306a36Sopenharmony_ciextern struct smack_known smack_known_web;
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ciextern struct mutex	smack_known_lock;
31562306a36Sopenharmony_ciextern struct list_head smack_known_list;
31662306a36Sopenharmony_ciextern struct list_head smk_net4addr_list;
31762306a36Sopenharmony_ciextern struct list_head smk_net6addr_list;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ciextern struct mutex     smack_onlycap_lock;
32062306a36Sopenharmony_ciextern struct list_head smack_onlycap_list;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci#define SMACK_HASH_SLOTS 16
32362306a36Sopenharmony_ciextern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
32462306a36Sopenharmony_ciextern struct kmem_cache *smack_rule_cache;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_cistatic inline struct task_smack *smack_cred(const struct cred *cred)
32762306a36Sopenharmony_ci{
32862306a36Sopenharmony_ci	return cred->security + smack_blob_sizes.lbs_cred;
32962306a36Sopenharmony_ci}
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_cistatic inline struct smack_known **smack_file(const struct file *file)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci	return (struct smack_known **)(file->f_security +
33462306a36Sopenharmony_ci				       smack_blob_sizes.lbs_file);
33562306a36Sopenharmony_ci}
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_cistatic inline struct inode_smack *smack_inode(const struct inode *inode)
33862306a36Sopenharmony_ci{
33962306a36Sopenharmony_ci	return inode->i_security + smack_blob_sizes.lbs_inode;
34062306a36Sopenharmony_ci}
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_cistatic inline struct smack_known **smack_msg_msg(const struct msg_msg *msg)
34362306a36Sopenharmony_ci{
34462306a36Sopenharmony_ci	return msg->security + smack_blob_sizes.lbs_msg_msg;
34562306a36Sopenharmony_ci}
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_cistatic inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc)
34862306a36Sopenharmony_ci{
34962306a36Sopenharmony_ci	return ipc->security + smack_blob_sizes.lbs_ipc;
35062306a36Sopenharmony_ci}
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_cistatic inline struct superblock_smack *smack_superblock(
35362306a36Sopenharmony_ci					const struct super_block *superblock)
35462306a36Sopenharmony_ci{
35562306a36Sopenharmony_ci	return superblock->s_security + smack_blob_sizes.lbs_superblock;
35662306a36Sopenharmony_ci}
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci/*
35962306a36Sopenharmony_ci * Is the directory transmuting?
36062306a36Sopenharmony_ci */
36162306a36Sopenharmony_cistatic inline int smk_inode_transmutable(const struct inode *isp)
36262306a36Sopenharmony_ci{
36362306a36Sopenharmony_ci	struct inode_smack *sip = smack_inode(isp);
36462306a36Sopenharmony_ci	return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci/*
36862306a36Sopenharmony_ci * Present a pointer to the smack label entry in an inode blob.
36962306a36Sopenharmony_ci */
37062306a36Sopenharmony_cistatic inline struct smack_known *smk_of_inode(const struct inode *isp)
37162306a36Sopenharmony_ci{
37262306a36Sopenharmony_ci	struct inode_smack *sip = smack_inode(isp);
37362306a36Sopenharmony_ci	return sip->smk_inode;
37462306a36Sopenharmony_ci}
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci/*
37762306a36Sopenharmony_ci * Present a pointer to the smack label entry in an task blob.
37862306a36Sopenharmony_ci */
37962306a36Sopenharmony_cistatic inline struct smack_known *smk_of_task(const struct task_smack *tsp)
38062306a36Sopenharmony_ci{
38162306a36Sopenharmony_ci	return tsp->smk_task;
38262306a36Sopenharmony_ci}
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_cistatic inline struct smack_known *smk_of_task_struct_obj(
38562306a36Sopenharmony_ci						const struct task_struct *t)
38662306a36Sopenharmony_ci{
38762306a36Sopenharmony_ci	struct smack_known *skp;
38862306a36Sopenharmony_ci	const struct cred *cred;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	rcu_read_lock();
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	cred = __task_cred(t);
39362306a36Sopenharmony_ci	skp = smk_of_task(smack_cred(cred));
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci	rcu_read_unlock();
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci	return skp;
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci/*
40162306a36Sopenharmony_ci * Present a pointer to the forked smack label entry in an task blob.
40262306a36Sopenharmony_ci */
40362306a36Sopenharmony_cistatic inline struct smack_known *smk_of_forked(const struct task_smack *tsp)
40462306a36Sopenharmony_ci{
40562306a36Sopenharmony_ci	return tsp->smk_forked;
40662306a36Sopenharmony_ci}
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci/*
40962306a36Sopenharmony_ci * Present a pointer to the smack label in the current task blob.
41062306a36Sopenharmony_ci */
41162306a36Sopenharmony_cistatic inline struct smack_known *smk_of_current(void)
41262306a36Sopenharmony_ci{
41362306a36Sopenharmony_ci	return smk_of_task(smack_cred(current_cred()));
41462306a36Sopenharmony_ci}
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci/*
41762306a36Sopenharmony_ci * logging functions
41862306a36Sopenharmony_ci */
41962306a36Sopenharmony_ci#define SMACK_AUDIT_DENIED 0x1
42062306a36Sopenharmony_ci#define SMACK_AUDIT_ACCEPT 0x2
42162306a36Sopenharmony_ciextern int log_policy;
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_civoid smack_log(char *subject_label, char *object_label,
42462306a36Sopenharmony_ci		int request,
42562306a36Sopenharmony_ci		int result, struct smk_audit_info *auditdata);
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci#ifdef CONFIG_AUDIT
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci/*
43062306a36Sopenharmony_ci * some inline functions to set up audit data
43162306a36Sopenharmony_ci * they do nothing if CONFIG_AUDIT is not set
43262306a36Sopenharmony_ci *
43362306a36Sopenharmony_ci */
43462306a36Sopenharmony_cistatic inline void smk_ad_init(struct smk_audit_info *a, const char *func,
43562306a36Sopenharmony_ci			       char type)
43662306a36Sopenharmony_ci{
43762306a36Sopenharmony_ci	memset(&a->sad, 0, sizeof(a->sad));
43862306a36Sopenharmony_ci	a->a.type = type;
43962306a36Sopenharmony_ci	a->a.smack_audit_data = &a->sad;
44062306a36Sopenharmony_ci	a->a.smack_audit_data->function = func;
44162306a36Sopenharmony_ci}
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_cistatic inline void smk_ad_init_net(struct smk_audit_info *a, const char *func,
44462306a36Sopenharmony_ci				   char type, struct lsm_network_audit *net)
44562306a36Sopenharmony_ci{
44662306a36Sopenharmony_ci	smk_ad_init(a, func, type);
44762306a36Sopenharmony_ci	memset(net, 0, sizeof(*net));
44862306a36Sopenharmony_ci	a->a.u.net = net;
44962306a36Sopenharmony_ci}
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
45262306a36Sopenharmony_ci					 struct task_struct *t)
45362306a36Sopenharmony_ci{
45462306a36Sopenharmony_ci	a->a.u.tsk = t;
45562306a36Sopenharmony_ci}
45662306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
45762306a36Sopenharmony_ci						    struct dentry *d)
45862306a36Sopenharmony_ci{
45962306a36Sopenharmony_ci	a->a.u.dentry = d;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
46262306a36Sopenharmony_ci					      struct inode *i)
46362306a36Sopenharmony_ci{
46462306a36Sopenharmony_ci	a->a.u.inode = i;
46562306a36Sopenharmony_ci}
46662306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
46762306a36Sopenharmony_ci					     struct path p)
46862306a36Sopenharmony_ci{
46962306a36Sopenharmony_ci	a->a.u.path = p;
47062306a36Sopenharmony_ci}
47162306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
47262306a36Sopenharmony_ci					    struct sock *sk)
47362306a36Sopenharmony_ci{
47462306a36Sopenharmony_ci	a->a.u.net->sk = sk;
47562306a36Sopenharmony_ci}
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci#else /* no AUDIT */
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_cistatic inline void smk_ad_init(struct smk_audit_info *a, const char *func,
48062306a36Sopenharmony_ci			       char type)
48162306a36Sopenharmony_ci{
48262306a36Sopenharmony_ci}
48362306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
48462306a36Sopenharmony_ci					 struct task_struct *t)
48562306a36Sopenharmony_ci{
48662306a36Sopenharmony_ci}
48762306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
48862306a36Sopenharmony_ci						    struct dentry *d)
48962306a36Sopenharmony_ci{
49062306a36Sopenharmony_ci}
49162306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
49262306a36Sopenharmony_ci					      struct inode *i)
49362306a36Sopenharmony_ci{
49462306a36Sopenharmony_ci}
49562306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
49662306a36Sopenharmony_ci					     struct path p)
49762306a36Sopenharmony_ci{
49862306a36Sopenharmony_ci}
49962306a36Sopenharmony_cistatic inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
50062306a36Sopenharmony_ci					    struct sock *sk)
50162306a36Sopenharmony_ci{
50262306a36Sopenharmony_ci}
50362306a36Sopenharmony_ci#endif
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci#endif  /* _SECURITY_SMACK_H */
506