policydb.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef _SEPOL_POLICYDB_H_
  2. #define _SEPOL_POLICYDB_H_
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. #include <sepol/handle.h>
  6. struct sepol_policy_file;
  7. typedef struct sepol_policy_file sepol_policy_file_t;
  8. struct sepol_policydb;
  9. typedef struct sepol_policydb sepol_policydb_t;
  10. /* Policy file public interfaces. */
  11. /* Create and free memory associated with a policy file. */
  12. extern int sepol_policy_file_create(sepol_policy_file_t ** pf);
  13. extern void sepol_policy_file_free(sepol_policy_file_t * pf);
  14. /*
  15. * Set the policy file to represent a binary policy memory image.
  16. * Subsequent operations using the policy file will read and write
  17. * the image located at the specified address with the specified length.
  18. * If 'len' is 0, then merely compute the necessary length upon
  19. * subsequent policydb write operations in order to determine the
  20. * necessary buffer size to allocate.
  21. */
  22. extern void sepol_policy_file_set_mem(sepol_policy_file_t * pf,
  23. char *data, size_t len);
  24. /*
  25. * Get the size of the buffer needed to store a policydb write
  26. * previously done on this policy file.
  27. */
  28. extern int sepol_policy_file_get_len(sepol_policy_file_t * pf, size_t * len);
  29. /*
  30. * Set the policy file to represent a FILE.
  31. * Subsequent operations using the policy file will read and write
  32. * to the FILE.
  33. */
  34. extern void sepol_policy_file_set_fp(sepol_policy_file_t * pf, FILE * fp);
  35. /*
  36. * Associate a handle with a policy file, for use in
  37. * error reporting from subsequent calls that take the
  38. * policy file as an argument.
  39. */
  40. extern void sepol_policy_file_set_handle(sepol_policy_file_t * pf,
  41. sepol_handle_t * handle);
  42. /* Policydb public interfaces. */
  43. /* Create and free memory associated with a policydb. */
  44. extern int sepol_policydb_create(sepol_policydb_t ** p);
  45. extern void sepol_policydb_free(sepol_policydb_t * p);
  46. /* Legal types of policies that the policydb can represent. */
  47. #define SEPOL_POLICY_KERN 0
  48. #define SEPOL_POLICY_BASE 1
  49. #define SEPOL_POLICY_MOD 2
  50. /*
  51. * Range of policy versions for the kernel policy type supported
  52. * by this library.
  53. */
  54. extern int sepol_policy_kern_vers_min(void);
  55. extern int sepol_policy_kern_vers_max(void);
  56. /*
  57. * Set the policy type as specified, and automatically initialize the
  58. * policy version accordingly to the maximum version supported for the
  59. * policy type.
  60. * Returns -1 if the policy type is not legal.
  61. */
  62. extern int sepol_policydb_set_typevers(sepol_policydb_t * p, unsigned int type);
  63. /*
  64. * Set the policy version to a different value.
  65. * Returns -1 if the policy version is not in the supported range for
  66. * the (previously set) policy type.
  67. */
  68. extern int sepol_policydb_set_vers(sepol_policydb_t * p, unsigned int vers);
  69. /* Set how to handle unknown class/perms. */
  70. #define SEPOL_DENY_UNKNOWN 0
  71. #define SEPOL_REJECT_UNKNOWN 2
  72. #define SEPOL_ALLOW_UNKNOWN 4
  73. extern int sepol_policydb_set_handle_unknown(sepol_policydb_t * p,
  74. unsigned int handle_unknown);
  75. /*
  76. * Read a policydb from a policy file.
  77. * This automatically sets the type and version based on the
  78. * image contents.
  79. */
  80. extern int sepol_policydb_read(sepol_policydb_t * p, sepol_policy_file_t * pf);
  81. /*
  82. * Write a policydb to a policy file.
  83. * The generated image will be in the binary format corresponding
  84. * to the policy version associated with the policydb.
  85. */
  86. extern int sepol_policydb_write(sepol_policydb_t * p, sepol_policy_file_t * pf);
  87. /*
  88. * Extract a policydb from a binary policy memory image.
  89. * This is equivalent to sepol_policydb_read with a policy file
  90. * set to refer to memory.
  91. */
  92. extern int sepol_policydb_from_image(sepol_handle_t * handle,
  93. void *data, size_t len,
  94. sepol_policydb_t * p);
  95. /*
  96. * Generate a binary policy memory image from a policydb.
  97. * This is equivalent to sepol_policydb_write with a policy file
  98. * set to refer to memory, but internally handles computing the
  99. * necessary length and allocating an appropriately sized memory
  100. * buffer for the caller.
  101. */
  102. extern int sepol_policydb_to_image(sepol_handle_t * handle,
  103. sepol_policydb_t * p,
  104. void **newdata, size_t * newlen);
  105. /*
  106. * Check whether the policydb has MLS enabled.
  107. */
  108. extern int sepol_policydb_mls_enabled(const sepol_policydb_t * p);
  109. /*
  110. * Check whether the compatibility mode for SELinux network
  111. * checks should be enabled when using this policy.
  112. */
  113. extern int sepol_policydb_compat_net(const sepol_policydb_t * p);
  114. #endif