• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * A security context is a set of security attributes
4  * associated with each subject and object controlled
5  * by the security policy.  Security contexts are
6   * externally represented as variable-length strings
7  * that can be interpreted by a user or application
8  * with an understanding of the security policy.
9  * Internally, the security server uses a simple
10  * structure.  This structure is private to the
11  * security server and can be changed without affecting
12  * clients of the security server.
13  *
14  * Author : Stephen Smalley, <sds@tycho.nsa.gov>
15  */
16 #ifndef _SS_CONTEXT_H_
17 #define _SS_CONTEXT_H_
18 
19 #include "ebitmap.h"
20 #include "mls_types.h"
21 #include "security.h"
22 
23 /*
24  * A security context consists of an authenticated user
25  * identity, a role, a type and a MLS range.
26  */
27 struct context {
28 	u32 user;
29 	u32 role;
30 	u32 type;
31 	u32 len;        /* length of string in bytes */
32 	struct mls_range range;
33 	char *str;	/* string representation if context cannot be mapped. */
34 	u32 hash;	/* a hash of the string representation */
35 };
36 
mls_context_init(struct context * c)37 static inline void mls_context_init(struct context *c)
38 {
39 	memset(&c->range, 0, sizeof(c->range));
40 }
41 
mls_context_cpy(struct context * dst,struct context * src)42 static inline int mls_context_cpy(struct context *dst, struct context *src)
43 {
44 	int rc;
45 
46 	dst->range.level[0].sens = src->range.level[0].sens;
47 	rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
48 	if (rc)
49 		goto out;
50 
51 	dst->range.level[1].sens = src->range.level[1].sens;
52 	rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat);
53 	if (rc)
54 		ebitmap_destroy(&dst->range.level[0].cat);
55 out:
56 	return rc;
57 }
58 
59 /*
60  * Sets both levels in the MLS range of 'dst' to the low level of 'src'.
61  */
mls_context_cpy_low(struct context * dst,struct context * src)62 static inline int mls_context_cpy_low(struct context *dst, struct context *src)
63 {
64 	int rc;
65 
66 	dst->range.level[0].sens = src->range.level[0].sens;
67 	rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
68 	if (rc)
69 		goto out;
70 
71 	dst->range.level[1].sens = src->range.level[0].sens;
72 	rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[0].cat);
73 	if (rc)
74 		ebitmap_destroy(&dst->range.level[0].cat);
75 out:
76 	return rc;
77 }
78 
79 /*
80  * Sets both levels in the MLS range of 'dst' to the high level of 'src'.
81  */
mls_context_cpy_high(struct context * dst,struct context * src)82 static inline int mls_context_cpy_high(struct context *dst, struct context *src)
83 {
84 	int rc;
85 
86 	dst->range.level[0].sens = src->range.level[1].sens;
87 	rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[1].cat);
88 	if (rc)
89 		goto out;
90 
91 	dst->range.level[1].sens = src->range.level[1].sens;
92 	rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat);
93 	if (rc)
94 		ebitmap_destroy(&dst->range.level[0].cat);
95 out:
96 	return rc;
97 }
98 
mls_context_cmp(struct context * c1,struct context * c2)99 static inline int mls_context_cmp(struct context *c1, struct context *c2)
100 {
101 	return ((c1->range.level[0].sens == c2->range.level[0].sens) &&
102 		ebitmap_cmp(&c1->range.level[0].cat, &c2->range.level[0].cat) &&
103 		(c1->range.level[1].sens == c2->range.level[1].sens) &&
104 		ebitmap_cmp(&c1->range.level[1].cat, &c2->range.level[1].cat));
105 }
106 
mls_context_destroy(struct context * c)107 static inline void mls_context_destroy(struct context *c)
108 {
109 	ebitmap_destroy(&c->range.level[0].cat);
110 	ebitmap_destroy(&c->range.level[1].cat);
111 	mls_context_init(c);
112 }
113 
context_init(struct context * c)114 static inline void context_init(struct context *c)
115 {
116 	memset(c, 0, sizeof(*c));
117 }
118 
context_cpy(struct context * dst,struct context * src)119 static inline int context_cpy(struct context *dst, struct context *src)
120 {
121 	int rc;
122 
123 	dst->user = src->user;
124 	dst->role = src->role;
125 	dst->type = src->type;
126 	if (src->str) {
127 		dst->str = kstrdup(src->str, GFP_ATOMIC);
128 		if (!dst->str)
129 			return -ENOMEM;
130 		dst->len = src->len;
131 	} else {
132 		dst->str = NULL;
133 		dst->len = 0;
134 	}
135 	rc = mls_context_cpy(dst, src);
136 	if (rc) {
137 		kfree(dst->str);
138 		return rc;
139 	}
140 	dst->hash = src->hash;
141 	return 0;
142 }
143 
context_destroy(struct context * c)144 static inline void context_destroy(struct context *c)
145 {
146 	c->user = c->role = c->type = c->hash = 0;
147 	kfree(c->str);
148 	c->str = NULL;
149 	c->len = 0;
150 	mls_context_destroy(c);
151 }
152 
context_cmp(struct context * c1,struct context * c2)153 static inline int context_cmp(struct context *c1, struct context *c2)
154 {
155 	if (c1->hash && c2->hash && (c1->hash != c2->hash))
156 		return 0;
157 	if (c1->len && c2->len)
158 		return (c1->len == c2->len && !strcmp(c1->str, c2->str));
159 	if (c1->len || c2->len)
160 		return 0;
161 	return ((c1->user == c2->user) &&
162 		(c1->role == c2->role) &&
163 		(c1->type == c2->type) &&
164 		mls_context_cmp(c1, c2));
165 }
166 
context_compute_hash(const char * s)167 static inline unsigned int context_compute_hash(const char *s)
168 {
169 	return full_name_hash(NULL, s, strlen(s));
170 }
171 
172 #endif	/* _SS_CONTEXT_H_ */
173 
174