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