context.h 4.0 KB

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