xref: /linux/security/selinux/include/objsec.h (revision 66de33a0bbb59ef3909d2c65dbbb7fc503d573bd)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
21da177e4SLinus Torvalds /*
390aa4f5eSStephen Smalley  *  Security-Enhanced Linux (SELinux) security module
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  This file contains the SELinux security data structures for kernel objects.
61da177e4SLinus Torvalds  *
70fe53224SStephen Smalley  *  Author(s):  Stephen Smalley, <stephen.smalley.work@gmail.com>
81da177e4SLinus Torvalds  *		Chris Vance, <cvance@nai.com>
91da177e4SLinus Torvalds  *		Wayne Salamon, <wsalamon@nai.com>
101da177e4SLinus Torvalds  *		James Morris <jmorris@redhat.com>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Copyright (C) 2001,2002 Networks Associates Technology, Inc.
131da177e4SLinus Torvalds  *  Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
143a976fa6SDaniel Jurgens  *  Copyright (C) 2016 Mellanox Technologies
151da177e4SLinus Torvalds  */
16c7870220SPaul Moore 
171da177e4SLinus Torvalds #ifndef _SELINUX_OBJSEC_H_
181da177e4SLinus Torvalds #define _SELINUX_OBJSEC_H_
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/list.h>
211da177e4SLinus Torvalds #include <linux/sched.h>
221da177e4SLinus Torvalds #include <linux/fs.h>
231da177e4SLinus Torvalds #include <linux/binfmts.h>
241da177e4SLinus Torvalds #include <linux/in.h>
259f2ad665SPaul Moore #include <linux/spinlock.h>
26bbd3662aSCasey Schaufler #include <linux/lsm_hooks.h>
277c653828SCasey Schaufler #include <linux/msg.h>
28cbe0d6e8SPaul Moore #include <net/net_namespace.h>
291da177e4SLinus Torvalds #include "flask.h"
301da177e4SLinus Torvalds #include "avc.h"
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds struct task_security_struct {
331da177e4SLinus Torvalds 	u32 osid; /* SID prior to last execve */
341da177e4SLinus Torvalds 	u32 sid; /* current SID */
351da177e4SLinus Torvalds 	u32 exec_sid; /* exec SID */
361da177e4SLinus Torvalds 	u32 create_sid; /* fscreate SID */
374eb582cfSMichael LeMay 	u32 keycreate_sid; /* keycreate SID */
3842c3e03eSEric Paris 	u32 sockcreate_sid; /* fscreate SID */
395c108d4eSStephen Smalley } __randomize_layout;
401da177e4SLinus Torvalds 
416f3be9f5SAndreas Gruenbacher enum label_initialized {
424b57d6bcSPaul Moore 	LABEL_INVALID, /* invalid or not initialized */
439287aed2SAndreas Gruenbacher 	LABEL_INITIALIZED, /* initialized */
449287aed2SAndreas Gruenbacher 	LABEL_PENDING
456f3be9f5SAndreas Gruenbacher };
466f3be9f5SAndreas Gruenbacher 
471da177e4SLinus Torvalds struct inode_security_struct {
481da177e4SLinus Torvalds 	struct inode *inode; /* back pointer to inode object */
491da177e4SLinus Torvalds 	struct list_head list; /* list of inode_security_struct */
501da177e4SLinus Torvalds 	u32 task_sid; /* SID of creating task */
511da177e4SLinus Torvalds 	u32 sid; /* SID of this object */
521da177e4SLinus Torvalds 	u16 sclass; /* security class of this object */
531da177e4SLinus Torvalds 	unsigned char initialized; /* initialization flag */
549287aed2SAndreas Gruenbacher 	spinlock_t lock;
551da177e4SLinus Torvalds };
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds struct file_security_struct {
581da177e4SLinus Torvalds 	u32 sid; /* SID of open file description */
591da177e4SLinus Torvalds 	u32 fown_sid; /* SID of file owner (for SIGIO) */
60788e7dd4SYuichi Nakamura 	u32 isid; /* SID of inode at the time of file open */
61788e7dd4SYuichi Nakamura 	u32 pseqno; /* Policy seqno at the time of file open */
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds struct superblock_security_struct {
65c312feb2SEric Paris 	u32 sid; /* SID of file system superblock */
661da177e4SLinus Torvalds 	u32 def_sid; /* default SID for labeling */
67c312feb2SEric Paris 	u32 mntpoint_sid; /* SECURITY_FS_USE_MNTPOINT context for files */
68f936c6e5SEric Paris 	unsigned short behavior; /* labeling behavior */
69cfca0303SEric Paris 	unsigned short flags; /* which mount options were specified */
70bc7e982bSEric Paris 	struct mutex lock;
711da177e4SLinus Torvalds 	struct list_head isec_head;
721da177e4SLinus Torvalds 	spinlock_t isec_lock;
731da177e4SLinus Torvalds };
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds struct msg_security_struct {
761da177e4SLinus Torvalds 	u32 sid; /* SID of message */
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds struct ipc_security_struct {
801da177e4SLinus Torvalds 	u16 sclass; /* security class of this object */
811da177e4SLinus Torvalds 	u32 sid; /* SID of IPC resource */
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds struct netif_security_struct {
85cbe0d6e8SPaul Moore 	struct net *ns; /* network namespace */
86e8bfdb9dSPaul Moore 	int ifindex; /* device index */
87e8bfdb9dSPaul Moore 	u32 sid; /* SID for this interface */
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds 
90224dfbd8SPaul Moore struct netnode_security_struct {
91224dfbd8SPaul Moore 	union {
92224dfbd8SPaul Moore 		__be32 ipv4; /* IPv4 node address */
93224dfbd8SPaul Moore 		struct in6_addr ipv6; /* IPv6 node address */
94224dfbd8SPaul Moore 	} addr;
95224dfbd8SPaul Moore 	u32 sid; /* SID for this node */
96224dfbd8SPaul Moore 	u16 family; /* address family */
97224dfbd8SPaul Moore };
98224dfbd8SPaul Moore 
993e112172SPaul Moore struct netport_security_struct {
1003e112172SPaul Moore 	u32 sid; /* SID for this node */
1013e112172SPaul Moore 	u16 port; /* port number */
1023e112172SPaul Moore 	u8 protocol; /* transport protocol */
1033e112172SPaul Moore };
1043e112172SPaul Moore 
1051da177e4SLinus Torvalds struct sk_security_struct {
106220deb96SPaul Moore #ifdef CONFIG_NETLABEL
1077420ed23SVenkat Yekkirala 	enum { /* NetLabel state */
1087420ed23SVenkat Yekkirala 	       NLBL_UNSET = 0,
1097420ed23SVenkat Yekkirala 	       NLBL_REQUIRE,
1107420ed23SVenkat Yekkirala 	       NLBL_LABELED,
111948bf85cSPaul Moore 	       NLBL_REQSKB,
112014ab19aSPaul Moore 	       NLBL_CONNLABELED,
1137420ed23SVenkat Yekkirala 	} nlbl_state;
1146c5b3fc0SPaul Moore 	struct netlbl_lsm_secattr *nlbl_secattr; /* NetLabel sec attributes */
1157420ed23SVenkat Yekkirala #endif
1166c5b3fc0SPaul Moore 	u32 sid; /* SID of this object */
1176c5b3fc0SPaul Moore 	u32 peer_sid; /* SID of peer */
1186c5b3fc0SPaul Moore 	u16 sclass; /* sock security class */
119d452930fSRichard Haines 	enum { /* SCTP association state */
120d452930fSRichard Haines 	       SCTP_ASSOC_UNSET = 0,
121d452930fSRichard Haines 	       SCTP_ASSOC_SET,
122d452930fSRichard Haines 	} sctp_assoc_state;
1231da177e4SLinus Torvalds };
1241da177e4SLinus Torvalds 
1255dbbaf2dSPaul Moore struct tun_security_struct {
1265dbbaf2dSPaul Moore 	u32 sid; /* SID for the tun device sockets */
1275dbbaf2dSPaul Moore };
1285dbbaf2dSPaul Moore 
129d720024eSMichael LeMay struct key_security_struct {
130d720024eSMichael LeMay 	u32 sid; /* SID of key */
131d720024eSMichael LeMay };
132d720024eSMichael LeMay 
1333a976fa6SDaniel Jurgens struct ib_security_struct {
1343a976fa6SDaniel Jurgens 	u32 sid; /* SID of the queue pair or MAD agent */
1353a976fa6SDaniel Jurgens };
1363a976fa6SDaniel Jurgens 
137409dcf31SDaniel Jurgens struct pkey_security_struct {
138409dcf31SDaniel Jurgens 	u64 subnet_prefix; /* Port subnet prefix */
139409dcf31SDaniel Jurgens 	u16 pkey; /* PKey number */
140409dcf31SDaniel Jurgens 	u32 sid; /* SID of pkey */
141409dcf31SDaniel Jurgens };
142409dcf31SDaniel Jurgens 
143ec27c356SChenbo Feng struct bpf_security_struct {
144da97e184SJoel Fernandes (Google) 	u32 sid; /* SID of bpf obj creator */
145da97e184SJoel Fernandes (Google) };
146da97e184SJoel Fernandes (Google) 
147da97e184SJoel Fernandes (Google) struct perf_event_security_struct {
148da97e184SJoel Fernandes (Google) 	u32 sid; /* SID of perf_event obj creator */
149ec27c356SChenbo Feng };
150ec27c356SChenbo Feng 
151bbd3662aSCasey Schaufler extern struct lsm_blob_sizes selinux_blob_sizes;
1520c6cfa62SCasey Schaufler static inline struct task_security_struct *selinux_cred(const struct cred *cred)
1530c6cfa62SCasey Schaufler {
154bbd3662aSCasey Schaufler 	return cred->security + selinux_blob_sizes.lbs_cred;
1550c6cfa62SCasey Schaufler }
1560c6cfa62SCasey Schaufler 
157bb6c6b02SCasey Schaufler static inline struct file_security_struct *selinux_file(const struct file *file)
158bb6c6b02SCasey Schaufler {
15933bf60caSCasey Schaufler 	return file->f_security + selinux_blob_sizes.lbs_file;
160bb6c6b02SCasey Schaufler }
161bb6c6b02SCasey Schaufler 
162c7870220SPaul Moore static inline struct inode_security_struct *
163c7870220SPaul Moore selinux_inode(const struct inode *inode)
16480788c22SCasey Schaufler {
165afb1cbe3SCasey Schaufler 	if (unlikely(!inode->i_security))
166afb1cbe3SCasey Schaufler 		return NULL;
167afb1cbe3SCasey Schaufler 	return inode->i_security + selinux_blob_sizes.lbs_inode;
16880788c22SCasey Schaufler }
16980788c22SCasey Schaufler 
170c7870220SPaul Moore static inline struct msg_security_struct *
171c7870220SPaul Moore selinux_msg_msg(const struct msg_msg *msg_msg)
1727c653828SCasey Schaufler {
173ecd5f82eSCasey Schaufler 	return msg_msg->security + selinux_blob_sizes.lbs_msg_msg;
1747c653828SCasey Schaufler }
1757c653828SCasey Schaufler 
176c7870220SPaul Moore static inline struct ipc_security_struct *
177c7870220SPaul Moore selinux_ipc(const struct kern_ipc_perm *ipc)
1787c653828SCasey Schaufler {
179ecd5f82eSCasey Schaufler 	return ipc->security + selinux_blob_sizes.lbs_ipc;
1807c653828SCasey Schaufler }
1817c653828SCasey Schaufler 
182169ce0c0SStephen Smalley /*
183169ce0c0SStephen Smalley  * get the subjective security ID of the current task
184169ce0c0SStephen Smalley  */
185169ce0c0SStephen Smalley static inline u32 current_sid(void)
186169ce0c0SStephen Smalley {
187169ce0c0SStephen Smalley 	const struct task_security_struct *tsec = selinux_cred(current_cred());
188169ce0c0SStephen Smalley 
189169ce0c0SStephen Smalley 	return tsec->sid;
190169ce0c0SStephen Smalley }
191169ce0c0SStephen Smalley 
192c7870220SPaul Moore static inline struct superblock_security_struct *
193c7870220SPaul Moore selinux_superblock(const struct super_block *superblock)
1941aea7808SCasey Schaufler {
1951aea7808SCasey Schaufler 	return superblock->s_security + selinux_blob_sizes.lbs_superblock;
1961aea7808SCasey Schaufler }
1971aea7808SCasey Schaufler 
1985f8d28f6SCasey Schaufler #ifdef CONFIG_KEYS
1995f8d28f6SCasey Schaufler static inline struct key_security_struct *selinux_key(const struct key *key)
2005f8d28f6SCasey Schaufler {
2015f8d28f6SCasey Schaufler 	return key->security + selinux_blob_sizes.lbs_key;
2025f8d28f6SCasey Schaufler }
2035f8d28f6SCasey Schaufler #endif /* CONFIG_KEYS */
2045f8d28f6SCasey Schaufler 
2052aff9d20SCasey Schaufler static inline struct sk_security_struct *selinux_sock(const struct sock *sock)
2062aff9d20SCasey Schaufler {
2072aff9d20SCasey Schaufler 	return sock->sk_security + selinux_blob_sizes.lbs_sock;
2082aff9d20SCasey Schaufler }
2092aff9d20SCasey Schaufler 
210a39c0f77SCasey Schaufler static inline struct tun_security_struct *selinux_tun_dev(void *security)
211a39c0f77SCasey Schaufler {
212a39c0f77SCasey Schaufler 	return security + selinux_blob_sizes.lbs_tun_dev;
213a39c0f77SCasey Schaufler }
214a39c0f77SCasey Schaufler 
215*66de33a0SCasey Schaufler static inline struct ib_security_struct *selinux_ib(void *ib_sec)
216*66de33a0SCasey Schaufler {
217*66de33a0SCasey Schaufler 	return ib_sec + selinux_blob_sizes.lbs_ib;
218*66de33a0SCasey Schaufler }
219*66de33a0SCasey Schaufler 
2201da177e4SLinus Torvalds #endif /* _SELINUX_OBJSEC_H_ */
221