18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author:
68c2ecf20Sopenharmony_ci *      Casey Schaufler <casey@schaufler-ca.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef _SECURITY_SMACK_H
108c2ecf20Sopenharmony_ci#define _SECURITY_SMACK_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/capability.h>
138c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
148c2ecf20Sopenharmony_ci#include <linux/lsm_hooks.h>
158c2ecf20Sopenharmony_ci#include <linux/in.h>
168c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
178c2ecf20Sopenharmony_ci#include <linux/in6.h>
188c2ecf20Sopenharmony_ci#endif /* CONFIG_IPV6 */
198c2ecf20Sopenharmony_ci#include <net/netlabel.h>
208c2ecf20Sopenharmony_ci#include <linux/list.h>
218c2ecf20Sopenharmony_ci#include <linux/rculist.h>
228c2ecf20Sopenharmony_ci#include <linux/lsm_audit.h>
238c2ecf20Sopenharmony_ci#include <linux/msg.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * Use IPv6 port labeling if IPv6 is enabled and secmarks
278c2ecf20Sopenharmony_ci * are not being used.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) && !defined(CONFIG_SECURITY_SMACK_NETFILTER)
308c2ecf20Sopenharmony_ci#define SMACK_IPV6_PORT_LABELING 1
318c2ecf20Sopenharmony_ci#endif
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) && defined(CONFIG_SECURITY_SMACK_NETFILTER)
348c2ecf20Sopenharmony_ci#define SMACK_IPV6_SECMARK_LABELING 1
358c2ecf20Sopenharmony_ci#endif
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * Smack labels were limited to 23 characters for a long time.
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci#define SMK_LABELLEN	24
418c2ecf20Sopenharmony_ci#define SMK_LONGLABEL	256
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * This is the repository for labels seen so that it is
458c2ecf20Sopenharmony_ci * not necessary to keep allocating tiny chuncks of memory
468c2ecf20Sopenharmony_ci * and so that they can be shared.
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * Labels are never modified in place. Anytime a label
498c2ecf20Sopenharmony_ci * is imported (e.g. xattrset on a file) the list is checked
508c2ecf20Sopenharmony_ci * for it and it is added if it doesn't exist. The address
518c2ecf20Sopenharmony_ci * is passed out in either case. Entries are added, but
528c2ecf20Sopenharmony_ci * never deleted.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * Since labels are hanging around anyway it doesn't
558c2ecf20Sopenharmony_ci * hurt to maintain a secid for those awkward situations
568c2ecf20Sopenharmony_ci * where kernel components that ought to use LSM independent
578c2ecf20Sopenharmony_ci * interfaces don't. The secid should go away when all of
588c2ecf20Sopenharmony_ci * these components have been repaired.
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * The cipso value associated with the label gets stored here, too.
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * Keep the access rules for this subject label here so that
638c2ecf20Sopenharmony_ci * the entire set of rules does not need to be examined every
648c2ecf20Sopenharmony_ci * time.
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_cistruct smack_known {
678c2ecf20Sopenharmony_ci	struct list_head		list;
688c2ecf20Sopenharmony_ci	struct hlist_node		smk_hashed;
698c2ecf20Sopenharmony_ci	char				*smk_known;
708c2ecf20Sopenharmony_ci	u32				smk_secid;
718c2ecf20Sopenharmony_ci	struct netlbl_lsm_secattr	smk_netlabel;	/* on wire labels */
728c2ecf20Sopenharmony_ci	struct list_head		smk_rules;	/* access rules */
738c2ecf20Sopenharmony_ci	struct mutex			smk_rules_lock;	/* lock for rules */
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/*
778c2ecf20Sopenharmony_ci * Maximum number of bytes for the levels in a CIPSO IP option.
788c2ecf20Sopenharmony_ci * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is
798c2ecf20Sopenharmony_ci * bigger than can be used, and 24 is the next lower multiple
808c2ecf20Sopenharmony_ci * of 8, and there are too many issues if there isn't space set
818c2ecf20Sopenharmony_ci * aside for the terminating null byte.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_ci#define SMK_CIPSOLEN	24
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistruct superblock_smack {
868c2ecf20Sopenharmony_ci	struct smack_known	*smk_root;
878c2ecf20Sopenharmony_ci	struct smack_known	*smk_floor;
888c2ecf20Sopenharmony_ci	struct smack_known	*smk_hat;
898c2ecf20Sopenharmony_ci	struct smack_known	*smk_default;
908c2ecf20Sopenharmony_ci	int			smk_flags;
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/*
948c2ecf20Sopenharmony_ci * Superblock flags
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_ci#define SMK_SB_INITIALIZED	0x01
978c2ecf20Sopenharmony_ci#define SMK_SB_UNTRUSTED	0x02
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistruct socket_smack {
1008c2ecf20Sopenharmony_ci	struct smack_known	*smk_out;	/* outbound label */
1018c2ecf20Sopenharmony_ci	struct smack_known	*smk_in;	/* inbound label */
1028c2ecf20Sopenharmony_ci	struct smack_known	*smk_packet;	/* TCP peer label */
1038c2ecf20Sopenharmony_ci	int			smk_state;	/* netlabel socket states */
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci#define	SMK_NETLBL_UNSET	0
1068c2ecf20Sopenharmony_ci#define	SMK_NETLBL_UNLABELED	1
1078c2ecf20Sopenharmony_ci#define	SMK_NETLBL_LABELED	2
1088c2ecf20Sopenharmony_ci#define	SMK_NETLBL_REQSKB	3
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/*
1118c2ecf20Sopenharmony_ci * Inode smack data
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistruct inode_smack {
1148c2ecf20Sopenharmony_ci	struct smack_known	*smk_inode;	/* label of the fso */
1158c2ecf20Sopenharmony_ci	struct smack_known	*smk_task;	/* label of the task */
1168c2ecf20Sopenharmony_ci	struct smack_known	*smk_mmap;	/* label of the mmap domain */
1178c2ecf20Sopenharmony_ci	int			smk_flags;	/* smack inode flags */
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistruct task_smack {
1218c2ecf20Sopenharmony_ci	struct smack_known	*smk_task;	/* label for access control */
1228c2ecf20Sopenharmony_ci	struct smack_known	*smk_forked;	/* label when forked */
1238c2ecf20Sopenharmony_ci	struct smack_known	*smk_transmuted;/* label when transmuted */
1248c2ecf20Sopenharmony_ci	struct list_head	smk_rules;	/* per task access rules */
1258c2ecf20Sopenharmony_ci	struct mutex		smk_rules_lock;	/* lock for the rules */
1268c2ecf20Sopenharmony_ci	struct list_head	smk_relabel;	/* transit allowed labels */
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#define	SMK_INODE_INSTANT	0x01	/* inode is instantiated */
1308c2ecf20Sopenharmony_ci#define	SMK_INODE_TRANSMUTE	0x02	/* directory is transmuting */
1318c2ecf20Sopenharmony_ci#define	SMK_INODE_CHANGED	0x04	/* smack was transmuted */
1328c2ecf20Sopenharmony_ci#define	SMK_INODE_IMPURE	0x08	/* involved in an impure transaction */
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/*
1358c2ecf20Sopenharmony_ci * A label access rule.
1368c2ecf20Sopenharmony_ci */
1378c2ecf20Sopenharmony_cistruct smack_rule {
1388c2ecf20Sopenharmony_ci	struct list_head	list;
1398c2ecf20Sopenharmony_ci	struct smack_known	*smk_subject;
1408c2ecf20Sopenharmony_ci	struct smack_known	*smk_object;
1418c2ecf20Sopenharmony_ci	int			smk_access;
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/*
1458c2ecf20Sopenharmony_ci * An entry in the table identifying IPv4 hosts.
1468c2ecf20Sopenharmony_ci */
1478c2ecf20Sopenharmony_cistruct smk_net4addr {
1488c2ecf20Sopenharmony_ci	struct list_head	list;
1498c2ecf20Sopenharmony_ci	struct in_addr		smk_host;	/* network address */
1508c2ecf20Sopenharmony_ci	struct in_addr		smk_mask;	/* network mask */
1518c2ecf20Sopenharmony_ci	int			smk_masks;	/* mask size */
1528c2ecf20Sopenharmony_ci	struct smack_known	*smk_label;	/* label */
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/*
1568c2ecf20Sopenharmony_ci * An entry in the table identifying IPv6 hosts.
1578c2ecf20Sopenharmony_ci */
1588c2ecf20Sopenharmony_cistruct smk_net6addr {
1598c2ecf20Sopenharmony_ci	struct list_head	list;
1608c2ecf20Sopenharmony_ci	struct in6_addr		smk_host;	/* network address */
1618c2ecf20Sopenharmony_ci	struct in6_addr		smk_mask;	/* network mask */
1628c2ecf20Sopenharmony_ci	int			smk_masks;	/* mask size */
1638c2ecf20Sopenharmony_ci	struct smack_known	*smk_label;	/* label */
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/*
1678c2ecf20Sopenharmony_ci * An entry in the table identifying ports.
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_cistruct smk_port_label {
1708c2ecf20Sopenharmony_ci	struct list_head	list;
1718c2ecf20Sopenharmony_ci	struct sock		*smk_sock;	/* socket initialized on */
1728c2ecf20Sopenharmony_ci	unsigned short		smk_port;	/* the port number */
1738c2ecf20Sopenharmony_ci	struct smack_known	*smk_in;	/* inbound label */
1748c2ecf20Sopenharmony_ci	struct smack_known	*smk_out;	/* outgoing label */
1758c2ecf20Sopenharmony_ci	short			smk_sock_type;	/* Socket type */
1768c2ecf20Sopenharmony_ci	short			smk_can_reuse;
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistruct smack_known_list_elem {
1808c2ecf20Sopenharmony_ci	struct list_head	list;
1818c2ecf20Sopenharmony_ci	struct smack_known	*smk_label;
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/* Super block security struct flags for mount options */
1858c2ecf20Sopenharmony_ci#define FSDEFAULT_MNT	0x01
1868c2ecf20Sopenharmony_ci#define FSFLOOR_MNT	0x02
1878c2ecf20Sopenharmony_ci#define FSHAT_MNT	0x04
1888c2ecf20Sopenharmony_ci#define FSROOT_MNT	0x08
1898c2ecf20Sopenharmony_ci#define FSTRANS_MNT	0x10
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#define NUM_SMK_MNT_OPTS	5
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cienum {
1948c2ecf20Sopenharmony_ci	Opt_error = -1,
1958c2ecf20Sopenharmony_ci	Opt_fsdefault = 0,
1968c2ecf20Sopenharmony_ci	Opt_fsfloor = 1,
1978c2ecf20Sopenharmony_ci	Opt_fshat = 2,
1988c2ecf20Sopenharmony_ci	Opt_fsroot = 3,
1998c2ecf20Sopenharmony_ci	Opt_fstransmute = 4,
2008c2ecf20Sopenharmony_ci};
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci#define SMACK_DELETE_OPTION	"-DELETE"
2038c2ecf20Sopenharmony_ci#define SMACK_CIPSO_OPTION 	"-CIPSO"
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/*
2068c2ecf20Sopenharmony_ci * CIPSO defaults.
2078c2ecf20Sopenharmony_ci */
2088c2ecf20Sopenharmony_ci#define SMACK_CIPSO_DOI_DEFAULT		3	/* Historical */
2098c2ecf20Sopenharmony_ci#define SMACK_CIPSO_DOI_INVALID		-1	/* Not a DOI */
2108c2ecf20Sopenharmony_ci#define SMACK_CIPSO_DIRECT_DEFAULT	250	/* Arbitrary */
2118c2ecf20Sopenharmony_ci#define SMACK_CIPSO_MAPPED_DEFAULT	251	/* Also arbitrary */
2128c2ecf20Sopenharmony_ci#define SMACK_CIPSO_MAXLEVEL            255     /* CIPSO 2.2 standard */
2138c2ecf20Sopenharmony_ci/*
2148c2ecf20Sopenharmony_ci * CIPSO 2.2 standard is 239, but Smack wants to use the
2158c2ecf20Sopenharmony_ci * categories in a structured way that limits the value to
2168c2ecf20Sopenharmony_ci * the bits in 23 bytes, hence the unusual number.
2178c2ecf20Sopenharmony_ci */
2188c2ecf20Sopenharmony_ci#define SMACK_CIPSO_MAXCATNUM           184     /* 23 * 8 */
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/*
2218c2ecf20Sopenharmony_ci * Ptrace rules
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_ci#define SMACK_PTRACE_DEFAULT	0
2248c2ecf20Sopenharmony_ci#define SMACK_PTRACE_EXACT	1
2258c2ecf20Sopenharmony_ci#define SMACK_PTRACE_DRACONIAN	2
2268c2ecf20Sopenharmony_ci#define SMACK_PTRACE_MAX	SMACK_PTRACE_DRACONIAN
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/*
2298c2ecf20Sopenharmony_ci * Flags for untraditional access modes.
2308c2ecf20Sopenharmony_ci * It shouldn't be necessary to avoid conflicts with definitions
2318c2ecf20Sopenharmony_ci * in fs.h, but do so anyway.
2328c2ecf20Sopenharmony_ci */
2338c2ecf20Sopenharmony_ci#define MAY_TRANSMUTE	0x00001000	/* Controls directory labeling */
2348c2ecf20Sopenharmony_ci#define MAY_LOCK	0x00002000	/* Locks should be writes, but ... */
2358c2ecf20Sopenharmony_ci#define MAY_BRINGUP	0x00004000	/* Report use of this rule */
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci/*
2388c2ecf20Sopenharmony_ci * The policy for delivering signals is configurable.
2398c2ecf20Sopenharmony_ci * It is usually "write", but can be "append".
2408c2ecf20Sopenharmony_ci */
2418c2ecf20Sopenharmony_ci#ifdef CONFIG_SECURITY_SMACK_APPEND_SIGNALS
2428c2ecf20Sopenharmony_ci#define MAY_DELIVER	MAY_APPEND	/* Signal delivery requires append */
2438c2ecf20Sopenharmony_ci#else
2448c2ecf20Sopenharmony_ci#define MAY_DELIVER	MAY_WRITE	/* Signal delivery requires write */
2458c2ecf20Sopenharmony_ci#endif
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#define SMACK_BRINGUP_ALLOW		1	/* Allow bringup mode */
2488c2ecf20Sopenharmony_ci#define SMACK_UNCONFINED_SUBJECT	2	/* Allow unconfined label */
2498c2ecf20Sopenharmony_ci#define SMACK_UNCONFINED_OBJECT		3	/* Allow unconfined label */
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/*
2528c2ecf20Sopenharmony_ci * Just to make the common cases easier to deal with
2538c2ecf20Sopenharmony_ci */
2548c2ecf20Sopenharmony_ci#define MAY_ANYREAD	(MAY_READ | MAY_EXEC)
2558c2ecf20Sopenharmony_ci#define MAY_READWRITE	(MAY_READ | MAY_WRITE)
2568c2ecf20Sopenharmony_ci#define MAY_NOT		0
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci/*
2598c2ecf20Sopenharmony_ci * Number of access types used by Smack (rwxatlb)
2608c2ecf20Sopenharmony_ci */
2618c2ecf20Sopenharmony_ci#define SMK_NUM_ACCESS_TYPE 7
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/* SMACK data */
2648c2ecf20Sopenharmony_cistruct smack_audit_data {
2658c2ecf20Sopenharmony_ci	const char *function;
2668c2ecf20Sopenharmony_ci	char *subject;
2678c2ecf20Sopenharmony_ci	char *object;
2688c2ecf20Sopenharmony_ci	char *request;
2698c2ecf20Sopenharmony_ci	int result;
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci/*
2738c2ecf20Sopenharmony_ci * Smack audit data; is empty if CONFIG_AUDIT not set
2748c2ecf20Sopenharmony_ci * to save some stack
2758c2ecf20Sopenharmony_ci */
2768c2ecf20Sopenharmony_cistruct smk_audit_info {
2778c2ecf20Sopenharmony_ci#ifdef CONFIG_AUDIT
2788c2ecf20Sopenharmony_ci	struct common_audit_data a;
2798c2ecf20Sopenharmony_ci	struct smack_audit_data sad;
2808c2ecf20Sopenharmony_ci#endif
2818c2ecf20Sopenharmony_ci};
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci/*
2848c2ecf20Sopenharmony_ci * These functions are in smack_access.c
2858c2ecf20Sopenharmony_ci */
2868c2ecf20Sopenharmony_ciint smk_access_entry(char *, char *, struct list_head *);
2878c2ecf20Sopenharmony_ciint smk_access(struct smack_known *, struct smack_known *,
2888c2ecf20Sopenharmony_ci	       int, struct smk_audit_info *);
2898c2ecf20Sopenharmony_ciint smk_tskacc(struct task_smack *, struct smack_known *,
2908c2ecf20Sopenharmony_ci	       u32, struct smk_audit_info *);
2918c2ecf20Sopenharmony_ciint smk_curacc(struct smack_known *, u32, struct smk_audit_info *);
2928c2ecf20Sopenharmony_cistruct smack_known *smack_from_secid(const u32);
2938c2ecf20Sopenharmony_cichar *smk_parse_smack(const char *string, int len);
2948c2ecf20Sopenharmony_ciint smk_netlbl_mls(int, char *, struct netlbl_lsm_secattr *, int);
2958c2ecf20Sopenharmony_cistruct smack_known *smk_import_entry(const char *, int);
2968c2ecf20Sopenharmony_civoid smk_insert_entry(struct smack_known *skp);
2978c2ecf20Sopenharmony_cistruct smack_known *smk_find_entry(const char *);
2988c2ecf20Sopenharmony_cibool smack_privileged(int cap);
2998c2ecf20Sopenharmony_cibool smack_privileged_cred(int cap, const struct cred *cred);
3008c2ecf20Sopenharmony_civoid smk_destroy_label_list(struct list_head *list);
3018c2ecf20Sopenharmony_ciint smack_populate_secattr(struct smack_known *skp);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci/*
3048c2ecf20Sopenharmony_ci * Shared data.
3058c2ecf20Sopenharmony_ci */
3068c2ecf20Sopenharmony_ciextern int smack_enabled;
3078c2ecf20Sopenharmony_ciextern int smack_cipso_direct;
3088c2ecf20Sopenharmony_ciextern int smack_cipso_mapped;
3098c2ecf20Sopenharmony_ciextern struct smack_known *smack_net_ambient;
3108c2ecf20Sopenharmony_ciextern struct smack_known *smack_syslog_label;
3118c2ecf20Sopenharmony_ci#ifdef CONFIG_SECURITY_SMACK_BRINGUP
3128c2ecf20Sopenharmony_ciextern struct smack_known *smack_unconfined;
3138c2ecf20Sopenharmony_ci#endif
3148c2ecf20Sopenharmony_ciextern int smack_ptrace_rule;
3158c2ecf20Sopenharmony_ciextern struct lsm_blob_sizes smack_blob_sizes;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ciextern struct smack_known smack_known_floor;
3188c2ecf20Sopenharmony_ciextern struct smack_known smack_known_hat;
3198c2ecf20Sopenharmony_ciextern struct smack_known smack_known_huh;
3208c2ecf20Sopenharmony_ciextern struct smack_known smack_known_star;
3218c2ecf20Sopenharmony_ciextern struct smack_known smack_known_web;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ciextern struct mutex	smack_known_lock;
3248c2ecf20Sopenharmony_ciextern struct list_head smack_known_list;
3258c2ecf20Sopenharmony_ciextern struct list_head smk_net4addr_list;
3268c2ecf20Sopenharmony_ciextern struct list_head smk_net6addr_list;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ciextern struct mutex     smack_onlycap_lock;
3298c2ecf20Sopenharmony_ciextern struct list_head smack_onlycap_list;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci#define SMACK_HASH_SLOTS 16
3328c2ecf20Sopenharmony_ciextern struct hlist_head smack_known_hash[SMACK_HASH_SLOTS];
3338c2ecf20Sopenharmony_ciextern struct kmem_cache *smack_rule_cache;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic inline struct task_smack *smack_cred(const struct cred *cred)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	return cred->security + smack_blob_sizes.lbs_cred;
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_cistatic inline struct smack_known **smack_file(const struct file *file)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	return (struct smack_known **)(file->f_security +
3438c2ecf20Sopenharmony_ci				       smack_blob_sizes.lbs_file);
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic inline struct inode_smack *smack_inode(const struct inode *inode)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	return inode->i_security + smack_blob_sizes.lbs_inode;
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic inline struct smack_known **smack_msg_msg(const struct msg_msg *msg)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	return msg->security + smack_blob_sizes.lbs_msg_msg;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic inline struct smack_known **smack_ipc(const struct kern_ipc_perm *ipc)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	return ipc->security + smack_blob_sizes.lbs_ipc;
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci/*
3628c2ecf20Sopenharmony_ci * Is the directory transmuting?
3638c2ecf20Sopenharmony_ci */
3648c2ecf20Sopenharmony_cistatic inline int smk_inode_transmutable(const struct inode *isp)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	struct inode_smack *sip = smack_inode(isp);
3678c2ecf20Sopenharmony_ci	return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci/*
3718c2ecf20Sopenharmony_ci * Present a pointer to the smack label entry in an inode blob.
3728c2ecf20Sopenharmony_ci */
3738c2ecf20Sopenharmony_cistatic inline struct smack_known *smk_of_inode(const struct inode *isp)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct inode_smack *sip = smack_inode(isp);
3768c2ecf20Sopenharmony_ci	return sip->smk_inode;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci/*
3808c2ecf20Sopenharmony_ci * Present a pointer to the smack label entry in an task blob.
3818c2ecf20Sopenharmony_ci */
3828c2ecf20Sopenharmony_cistatic inline struct smack_known *smk_of_task(const struct task_smack *tsp)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	return tsp->smk_task;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic inline struct smack_known *smk_of_task_struct(
3888c2ecf20Sopenharmony_ci						const struct task_struct *t)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	struct smack_known *skp;
3918c2ecf20Sopenharmony_ci	const struct cred *cred;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	rcu_read_lock();
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	cred = __task_cred(t);
3968c2ecf20Sopenharmony_ci	skp = smk_of_task(smack_cred(cred));
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	rcu_read_unlock();
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return skp;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci/*
4048c2ecf20Sopenharmony_ci * Present a pointer to the forked smack label entry in an task blob.
4058c2ecf20Sopenharmony_ci */
4068c2ecf20Sopenharmony_cistatic inline struct smack_known *smk_of_forked(const struct task_smack *tsp)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	return tsp->smk_forked;
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci/*
4128c2ecf20Sopenharmony_ci * Present a pointer to the smack label in the current task blob.
4138c2ecf20Sopenharmony_ci */
4148c2ecf20Sopenharmony_cistatic inline struct smack_known *smk_of_current(void)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	return smk_of_task(smack_cred(current_cred()));
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci/*
4208c2ecf20Sopenharmony_ci * logging functions
4218c2ecf20Sopenharmony_ci */
4228c2ecf20Sopenharmony_ci#define SMACK_AUDIT_DENIED 0x1
4238c2ecf20Sopenharmony_ci#define SMACK_AUDIT_ACCEPT 0x2
4248c2ecf20Sopenharmony_ciextern int log_policy;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_civoid smack_log(char *subject_label, char *object_label,
4278c2ecf20Sopenharmony_ci		int request,
4288c2ecf20Sopenharmony_ci		int result, struct smk_audit_info *auditdata);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci#ifdef CONFIG_AUDIT
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci/*
4338c2ecf20Sopenharmony_ci * some inline functions to set up audit data
4348c2ecf20Sopenharmony_ci * they do nothing if CONFIG_AUDIT is not set
4358c2ecf20Sopenharmony_ci *
4368c2ecf20Sopenharmony_ci */
4378c2ecf20Sopenharmony_cistatic inline void smk_ad_init(struct smk_audit_info *a, const char *func,
4388c2ecf20Sopenharmony_ci			       char type)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	memset(&a->sad, 0, sizeof(a->sad));
4418c2ecf20Sopenharmony_ci	a->a.type = type;
4428c2ecf20Sopenharmony_ci	a->a.smack_audit_data = &a->sad;
4438c2ecf20Sopenharmony_ci	a->a.smack_audit_data->function = func;
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic inline void smk_ad_init_net(struct smk_audit_info *a, const char *func,
4478c2ecf20Sopenharmony_ci				   char type, struct lsm_network_audit *net)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	smk_ad_init(a, func, type);
4508c2ecf20Sopenharmony_ci	memset(net, 0, sizeof(*net));
4518c2ecf20Sopenharmony_ci	a->a.u.net = net;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
4558c2ecf20Sopenharmony_ci					 struct task_struct *t)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	a->a.u.tsk = t;
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
4608c2ecf20Sopenharmony_ci						    struct dentry *d)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	a->a.u.dentry = d;
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
4658c2ecf20Sopenharmony_ci					      struct inode *i)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	a->a.u.inode = i;
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
4708c2ecf20Sopenharmony_ci					     struct path p)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	a->a.u.path = p;
4738c2ecf20Sopenharmony_ci}
4748c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
4758c2ecf20Sopenharmony_ci					    struct sock *sk)
4768c2ecf20Sopenharmony_ci{
4778c2ecf20Sopenharmony_ci	a->a.u.net->sk = sk;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci#else /* no AUDIT */
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_cistatic inline void smk_ad_init(struct smk_audit_info *a, const char *func,
4838c2ecf20Sopenharmony_ci			       char type)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a,
4878c2ecf20Sopenharmony_ci					 struct task_struct *t)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a,
4918c2ecf20Sopenharmony_ci						    struct dentry *d)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a,
4958c2ecf20Sopenharmony_ci					      struct inode *i)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a,
4998c2ecf20Sopenharmony_ci					     struct path p)
5008c2ecf20Sopenharmony_ci{
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_cistatic inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a,
5038c2ecf20Sopenharmony_ci					    struct sock *sk)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci}
5068c2ecf20Sopenharmony_ci#endif
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci#endif  /* _SECURITY_SMACK_H */
509