context.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Author : Stephen Smalley, <sds@epoch.ncsc.mil> */
  2. /* FLASK */
  3. /*
  4. * A security context is a set of security attributes
  5. * associated with each subject and object controlled
  6. * by the security policy. Security contexts are
  7. * externally represented as variable-length strings
  8. * that can be interpreted by a user or application
  9. * with an understanding of the security policy.
  10. * Internally, the security server uses a simple
  11. * structure. This structure is private to the
  12. * security server and can be changed without affecting
  13. * clients of the security server.
  14. */
  15. #ifndef _SEPOL_POLICYDB_CONTEXT_H_
  16. #define _SEPOL_POLICYDB_CONTEXT_H_
  17. #include <stddef.h>
  18. #include <sepol/policydb/ebitmap.h>
  19. #include <sepol/policydb/mls_types.h>
  20. /*
  21. * A security context consists of an authenticated user
  22. * identity, a role, a type and a MLS range.
  23. */
  24. typedef struct context_struct {
  25. uint32_t user;
  26. uint32_t role;
  27. uint32_t type;
  28. mls_range_t range;
  29. } context_struct_t;
  30. static inline void mls_context_init(context_struct_t * c)
  31. {
  32. mls_range_init(&c->range);
  33. }
  34. static inline int mls_context_cpy(context_struct_t * dst,
  35. context_struct_t * src)
  36. {
  37. if (mls_range_cpy(&dst->range, &src->range) < 0)
  38. return -1;
  39. return 0;
  40. }
  41. static inline int mls_context_cmp(context_struct_t * c1, context_struct_t * c2)
  42. {
  43. return (mls_level_eq(&c1->range.level[0], &c2->range.level[0]) &&
  44. mls_level_eq(&c1->range.level[1], &c2->range.level[1]));
  45. }
  46. static inline void mls_context_destroy(context_struct_t * c)
  47. {
  48. if (c == NULL)
  49. return;
  50. mls_range_destroy(&c->range);
  51. mls_context_init(c);
  52. }
  53. static inline void context_init(context_struct_t * c)
  54. {
  55. memset(c, 0, sizeof(*c));
  56. }
  57. static inline int context_cpy(context_struct_t * dst, context_struct_t * src)
  58. {
  59. dst->user = src->user;
  60. dst->role = src->role;
  61. dst->type = src->type;
  62. return mls_context_cpy(dst, src);
  63. }
  64. static inline void context_destroy(context_struct_t * c)
  65. {
  66. if (c == NULL)
  67. return;
  68. c->user = c->role = c->type = 0;
  69. mls_context_destroy(c);
  70. }
  71. static inline int context_cmp(context_struct_t * c1, context_struct_t * c2)
  72. {
  73. return ((c1->user == c2->user) &&
  74. (c1->role == c2->role) &&
  75. (c1->type == c2->type) && mls_context_cmp(c1, c2));
  76. }
  77. #endif