context.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "ebitmap.h"
  19. #include "mls_types.h"
  20. #include "security.h"
  21. /*
  22. * A security context consists of an authenticated user
  23. * identity, a role, a type and a MLS range.
  24. */
  25. struct context {
  26. u32 user;
  27. u32 role;
  28. u32 type;
  29. u32 len; /* length of string in bytes */
  30. struct mls_range range;
  31. char *str; /* string representation if context cannot be mapped. */
  32. u32 hash; /* a hash of the string representation */
  33. };
  34. static inline void mls_context_init(struct context *c)
  35. {
  36. memset(&c->range, 0, sizeof(c->range));
  37. }
  38. static inline int mls_context_cpy(struct context *dst, struct context *src)
  39. {
  40. int rc;
  41. dst->range.level[0].sens = src->range.level[0].sens;
  42. rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
  43. if (rc)
  44. goto out;
  45. dst->range.level[1].sens = src->range.level[1].sens;
  46. rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat);
  47. if (rc)
  48. ebitmap_destroy(&dst->range.level[0].cat);
  49. out:
  50. return rc;
  51. }
  52. /*
  53. * Sets both levels in the MLS range of 'dst' to the low level of 'src'.
  54. */
  55. static inline int mls_context_cpy_low(struct context *dst, struct context *src)
  56. {
  57. int rc;
  58. dst->range.level[0].sens = src->range.level[0].sens;
  59. rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[0].cat);
  60. if (rc)
  61. goto out;
  62. dst->range.level[1].sens = src->range.level[0].sens;
  63. rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[0].cat);
  64. if (rc)
  65. ebitmap_destroy(&dst->range.level[0].cat);
  66. out:
  67. return rc;
  68. }
  69. /*
  70. * Sets both levels in the MLS range of 'dst' to the high level of 'src'.
  71. */
  72. static inline int mls_context_cpy_high(struct context *dst, struct context *src)
  73. {
  74. int rc;
  75. dst->range.level[0].sens = src->range.level[1].sens;
  76. rc = ebitmap_cpy(&dst->range.level[0].cat, &src->range.level[1].cat);
  77. if (rc)
  78. goto out;
  79. dst->range.level[1].sens = src->range.level[1].sens;
  80. rc = ebitmap_cpy(&dst->range.level[1].cat, &src->range.level[1].cat);
  81. if (rc)
  82. ebitmap_destroy(&dst->range.level[0].cat);
  83. out:
  84. return rc;
  85. }
  86. static inline int mls_context_cmp(struct context *c1, struct context *c2)
  87. {
  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));
  92. }
  93. static inline void mls_context_destroy(struct context *c)
  94. {
  95. ebitmap_destroy(&c->range.level[0].cat);
  96. ebitmap_destroy(&c->range.level[1].cat);
  97. mls_context_init(c);
  98. }
  99. static inline void context_init(struct context *c)
  100. {
  101. memset(c, 0, sizeof(*c));
  102. }
  103. static inline int context_cpy(struct context *dst, struct context *src)
  104. {
  105. int rc;
  106. dst->user = src->user;
  107. dst->role = src->role;
  108. dst->type = src->type;
  109. if (src->str) {
  110. dst->str = kstrdup(src->str, GFP_ATOMIC);
  111. if (!dst->str)
  112. return -ENOMEM;
  113. dst->len = src->len;
  114. } else {
  115. dst->str = NULL;
  116. dst->len = 0;
  117. }
  118. rc = mls_context_cpy(dst, src);
  119. if (rc) {
  120. kfree(dst->str);
  121. return rc;
  122. }
  123. dst->hash = src->hash;
  124. return 0;
  125. }
  126. static inline void context_destroy(struct context *c)
  127. {
  128. c->user = c->role = c->type = c->hash = 0;
  129. kfree(c->str);
  130. c->str = NULL;
  131. c->len = 0;
  132. mls_context_destroy(c);
  133. }
  134. static inline int context_cmp(struct context *c1, struct context *c2)
  135. {
  136. if (c1->hash && c2->hash && (c1->hash != c2->hash))
  137. return 0;
  138. if (c1->len && c2->len)
  139. return (c1->len == c2->len && !strcmp(c1->str, c2->str));
  140. if (c1->len || c2->len)
  141. return 0;
  142. return ((c1->user == c2->user) &&
  143. (c1->role == c2->role) &&
  144. (c1->type == c2->type) &&
  145. mls_context_cmp(c1, c2));
  146. }
  147. static inline unsigned int context_compute_hash(const char *s)
  148. {
  149. return full_name_hash(NULL, s, strlen(s));
  150. }
  151. #endif /* _SS_CONTEXT_H_ */