2 * A security context is a set of security attributes
3 * associated with each subject and object controlled
4 * by the security policy. Security contexts are
5 * externally represented as variable-length strings
6 * that can be interpreted by a user or application
7 * with an understanding of the security policy.
8 * Internally, the security server uses a simple
9 * structure. This structure is private to the
10 * security server and can be changed without affecting
11 * clients of the security server.
13 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
15 #ifndef _SS_CONTEXT_H_
16 #define _SS_CONTEXT_H_
19 #include "mls_types.h"
23 * A security context consists of an authenticated user
24 * identity, a role, a type and a MLS range.
30 struct mls_range range;
31 char *str; /* string representation if context cannot be mapped. */
32 u32 len; /* length of string in bytes */
35 static inline void mls_context_init(struct context *c)
37 memset(&c->range, 0, sizeof(c->range));
40 static inline int mls_context_cpy(struct context *dst, struct context *src)
44 if (!selinux_mls_enabled)
47 dst->range.level[0].sens = src->range.level[0].sens;
48 rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
52 dst->range.level[1].sens = src->range.level[1].sens;
53 rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat);
55 ebitmap_destroy(&dst->range.level[0].cat);
61 * Sets both levels in the MLS range of 'dst' to the low level of 'src'.
63 static inline int mls_context_cpy_low(struct context *dst, struct context *src)
67 if (!selinux_mls_enabled)
70 dst->range.level[0].sens = src->range.level[0].sens;
71 rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
75 dst->range.level[1].sens = src->range.level[0].sens;
76 rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[0].cat);
78 ebitmap_destroy(&dst->range.level[0].cat);
83 static inline int mls_context_cmp(struct context *c1, struct context *c2)
85 if (!selinux_mls_enabled)
88 return ((c1->range.level[0].sens == c2->range.level[0].sens) &&
89 ebitmap_cmp(&c1->range.level[0].cat, &c2->range.level[0].cat) &&
90 (c1->range.level[1].sens == c2->range.level[1].sens) &&
91 ebitmap_cmp(&c1->range.level[1].cat, &c2->range.level[1].cat));
94 static inline void mls_context_destroy(struct context *c)
96 if (!selinux_mls_enabled)
99 ebitmap_destroy(&c->range.level[0].cat);
100 ebitmap_destroy(&c->range.level[1].cat);
104 static inline void context_init(struct context *c)
106 memset(c, 0, sizeof(*c));
109 static inline int context_cpy(struct context *dst, struct context *src)
113 dst->user = src->user;
114 dst->role = src->role;
115 dst->type = src->type;
117 dst->str = kstrdup(src->str, GFP_ATOMIC);
125 rc = mls_context_cpy(dst, src);
133 static inline void context_destroy(struct context *c)
135 c->user = c->role = c->type = 0;
139 mls_context_destroy(c);
142 static inline int context_cmp(struct context *c1, struct context *c2)
144 if (c1->len && c2->len)
145 return (c1->len == c2->len && !strcmp(c1->str, c2->str));
146 if (c1->len || c2->len)
148 return ((c1->user == c2->user) &&
149 (c1->role == c2->role) &&
150 (c1->type == c2->type) &&
151 mls_context_cmp(c1, c2));
154 #endif /* _SS_CONTEXT_H_ */