context.h (4935B)
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 */ 27struct 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}; 35 36static inline void mls_context_init(struct context *c) 37{ 38 memset(&c->range, 0, sizeof(c->range)); 39} 40 41static inline int mls_context_cpy(struct context *dst, struct context *src) 42{ 43 int rc; 44 45 dst->range.level[0].sens = src->range.level[0].sens; 46 rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat); 47 if (rc) 48 goto out; 49 50 dst->range.level[1].sens = src->range.level[1].sens; 51 rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat); 52 if (rc) 53 ebitmap_destroy(&dst->range.level[0].cat); 54out: 55 return rc; 56} 57 58/* 59 * Sets both levels in the MLS range of 'dst' to the low level of 'src'. 60 */ 61static inline int mls_context_cpy_low(struct context *dst, struct context *src) 62{ 63 int rc; 64 65 dst->range.level[0].sens = src->range.level[0].sens; 66 rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat); 67 if (rc) 68 goto out; 69 70 dst->range.level[1].sens = src->range.level[0].sens; 71 rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[0].cat); 72 if (rc) 73 ebitmap_destroy(&dst->range.level[0].cat); 74out: 75 return rc; 76} 77 78/* 79 * Sets both levels in the MLS range of 'dst' to the high level of 'src'. 80 */ 81static inline int mls_context_cpy_high(struct context *dst, struct context *src) 82{ 83 int rc; 84 85 dst->range.level[0].sens = src->range.level[1].sens; 86 rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[1].cat); 87 if (rc) 88 goto out; 89 90 dst->range.level[1].sens = src->range.level[1].sens; 91 rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat); 92 if (rc) 93 ebitmap_destroy(&dst->range.level[0].cat); 94out: 95 return rc; 96} 97 98 99static inline int mls_context_glblub(struct context *dst, 100 struct context *c1, struct context *c2) 101{ 102 struct mls_range *dr = &dst->range, *r1 = &c1->range, *r2 = &c2->range; 103 int rc = 0; 104 105 if (r1->level[1].sens < r2->level[0].sens || 106 r2->level[1].sens < r1->level[0].sens) 107 /* These ranges have no common sensitivities */ 108 return -EINVAL; 109 110 /* Take the greatest of the low */ 111 dr->level[0].sens = max(r1->level[0].sens, r2->level[0].sens); 112 113 /* Take the least of the high */ 114 dr->level[1].sens = min(r1->level[1].sens, r2->level[1].sens); 115 116 rc = ebitmap_and(&dr->level[0].cat, 117 &r1->level[0].cat, &r2->level[0].cat); 118 if (rc) 119 goto out; 120 121 rc = ebitmap_and(&dr->level[1].cat, 122 &r1->level[1].cat, &r2->level[1].cat); 123 if (rc) 124 goto out; 125 126out: 127 return rc; 128} 129 130static inline int mls_context_cmp(struct context *c1, struct context *c2) 131{ 132 return ((c1->range.level[0].sens == c2->range.level[0].sens) && 133 ebitmap_cmp(&c1->range.level[0].cat, &c2->range.level[0].cat) && 134 (c1->range.level[1].sens == c2->range.level[1].sens) && 135 ebitmap_cmp(&c1->range.level[1].cat, &c2->range.level[1].cat)); 136} 137 138static inline void mls_context_destroy(struct context *c) 139{ 140 ebitmap_destroy(&c->range.level[0].cat); 141 ebitmap_destroy(&c->range.level[1].cat); 142 mls_context_init(c); 143} 144 145static inline void context_init(struct context *c) 146{ 147 memset(c, 0, sizeof(*c)); 148} 149 150static inline int context_cpy(struct context *dst, struct context *src) 151{ 152 int rc; 153 154 dst->user = src->user; 155 dst->role = src->role; 156 dst->type = src->type; 157 if (src->str) { 158 dst->str = kstrdup(src->str, GFP_ATOMIC); 159 if (!dst->str) 160 return -ENOMEM; 161 dst->len = src->len; 162 } else { 163 dst->str = NULL; 164 dst->len = 0; 165 } 166 rc = mls_context_cpy(dst, src); 167 if (rc) { 168 kfree(dst->str); 169 return rc; 170 } 171 return 0; 172} 173 174static inline void context_destroy(struct context *c) 175{ 176 c->user = c->role = c->type = 0; 177 kfree(c->str); 178 c->str = NULL; 179 c->len = 0; 180 mls_context_destroy(c); 181} 182 183static inline int context_cmp(struct context *c1, struct context *c2) 184{ 185 if (c1->len && c2->len) 186 return (c1->len == c2->len && !strcmp(c1->str, c2->str)); 187 if (c1->len || c2->len) 188 return 0; 189 return ((c1->user == c2->user) && 190 (c1->role == c2->role) && 191 (c1->type == c2->type) && 192 mls_context_cmp(c1, c2)); 193} 194 195u32 context_compute_hash(const struct context *c); 196 197#endif /* _SS_CONTEXT_H_ */ 198