avc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Access vector cache interface for object managers.
  3. *
  4. * Author : Eamon Walsh <ewalsh@epoch.ncsc.mil>
  5. */
  6. #ifndef _SELINUX_AVC_H_
  7. #define _SELINUX_AVC_H_
  8. #include <stdint.h>
  9. #include <errno.h>
  10. #include <stdlib.h>
  11. #include <selinux/selinux.h>
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /*
  16. * SID format and operations
  17. */
  18. struct security_id {
  19. security_context_t ctx;
  20. unsigned int refcnt;
  21. };
  22. typedef struct security_id *security_id_t;
  23. #define SECSID_WILD (security_id_t)NULL /* unspecified SID */
  24. /**
  25. * avc_sid_to_context - get copy of context corresponding to SID.
  26. * @sid: input SID
  27. * @ctx: pointer to context reference
  28. *
  29. * Return a copy of the security context corresponding to the input
  30. * @sid in the memory referenced by @ctx. The caller is expected to
  31. * free the context with freecon(). Return %0 on success, -%1 on
  32. * failure, with @errno set to %ENOMEM if insufficient memory was
  33. * available to make the copy, or %EINVAL if the input SID is invalid.
  34. */
  35. int avc_sid_to_context(security_id_t sid, security_context_t * ctx);
  36. int avc_sid_to_context_raw(security_id_t sid, security_context_t * ctx);
  37. /**
  38. * avc_context_to_sid - get SID for context.
  39. * @ctx: input security context
  40. * @sid: pointer to SID reference
  41. *
  42. * Look up security context @ctx in SID table, making
  43. * a new entry if @ctx is not found. Increment the
  44. * reference counter for the SID. Store a pointer
  45. * to the SID structure into the memory referenced by @sid,
  46. * returning %0 on success or -%1 on error with @errno set.
  47. */
  48. int avc_context_to_sid(security_context_t ctx, security_id_t * sid);
  49. int avc_context_to_sid_raw(security_context_t ctx, security_id_t * sid);
  50. /**
  51. * sidget - increment SID reference counter.
  52. * @sid: SID reference
  53. *
  54. * Increment the reference counter for @sid, indicating that
  55. * @sid is in use by an (additional) object. Return the
  56. * new reference count, or zero if @sid is invalid (has zero
  57. * reference count). Note that avc_context_to_sid() also
  58. * increments reference counts.
  59. */
  60. int sidget(security_id_t sid);
  61. /**
  62. * sidput - decrement SID reference counter.
  63. * @sid: SID reference
  64. *
  65. * Decrement the reference counter for @sid, indicating that
  66. * a reference to @sid is no longer in use. Return the
  67. * new reference count. When the reference count reaches
  68. * zero, the SID is invalid, and avc_context_to_sid() must
  69. * be called to obtain a new SID for the security context.
  70. */
  71. int sidput(security_id_t sid);
  72. /**
  73. * avc_get_initial_sid - get SID for an initial kernel security identifier
  74. * @name: input name of initial kernel security identifier
  75. * @sid: pointer to a SID reference
  76. *
  77. * Get the context for an initial kernel security identifier specified by
  78. * @name using security_get_initial_context() and then call
  79. * avc_context_to_sid() to get the corresponding SID.
  80. */
  81. int avc_get_initial_sid(const char *name, security_id_t * sid);
  82. /*
  83. * AVC entry
  84. */
  85. struct avc_entry;
  86. struct avc_entry_ref {
  87. struct avc_entry *ae;
  88. };
  89. /**
  90. * avc_entry_ref_init - initialize an AVC entry reference.
  91. * @aeref: pointer to avc entry reference structure
  92. *
  93. * Use this macro to initialize an avc entry reference structure
  94. * before first use. These structures are passed to avc_has_perm(),
  95. * which stores cache entry references in them. They can increase
  96. * performance on repeated queries.
  97. */
  98. #define avc_entry_ref_init(aeref) ((aeref)->ae = NULL)
  99. /*
  100. * User-provided callbacks for memory, auditing, and locking
  101. */
  102. /* These structures are passed by reference to avc_init(). Passing
  103. * a NULL reference will cause the AVC to use a default. The default
  104. * memory callbacks are malloc() and free(). The default logging method
  105. * is to print on stderr. If no thread callbacks are passed, a separate
  106. * listening thread won't be started for kernel policy change messages.
  107. * If no locking callbacks are passed, no locking will take place.
  108. */
  109. struct avc_memory_callback {
  110. /* malloc() equivalent. */
  111. void *(*func_malloc) (size_t size);
  112. /* free() equivalent. */
  113. void (*func_free) (void *ptr);
  114. /* Note that these functions should set errno on failure.
  115. If not, some avc routines may return -1 without errno set. */
  116. };
  117. struct avc_log_callback {
  118. /* log the printf-style format and arguments. */
  119. void (*func_log) (const char *fmt, ...);
  120. /* store a string representation of auditdata (corresponding
  121. to the given security class) into msgbuf. */
  122. void (*func_audit) (void *auditdata, security_class_t cls,
  123. char *msgbuf, size_t msgbufsize);
  124. };
  125. struct avc_thread_callback {
  126. /* create and start a thread, returning an opaque pointer to it;
  127. the thread should run the given function. */
  128. void *(*func_create_thread) (void (*run) (void));
  129. /* cancel a given thread and free its resources. */
  130. void (*func_stop_thread) (void *thread);
  131. };
  132. struct avc_lock_callback {
  133. /* create a lock and return an opaque pointer to it. */
  134. void *(*func_alloc_lock) (void);
  135. /* obtain a given lock, blocking if necessary. */
  136. void (*func_get_lock) (void *lock);
  137. /* release a given lock. */
  138. void (*func_release_lock) (void *lock);
  139. /* destroy a given lock (free memory, etc.) */
  140. void (*func_free_lock) (void *lock);
  141. };
  142. /*
  143. * Available options
  144. */
  145. /* no-op option, useful for unused slots in an array of options */
  146. #define AVC_OPT_UNUSED 0
  147. /* override kernel enforcing mode (boolean value) */
  148. #define AVC_OPT_SETENFORCE 1
  149. /*
  150. * AVC operations
  151. */
  152. /**
  153. * avc_init - Initialize the AVC.
  154. * @msgprefix: prefix for log messages
  155. * @mem_callbacks: user-supplied memory callbacks
  156. * @log_callbacks: user-supplied logging callbacks
  157. * @thread_callbacks: user-supplied threading callbacks
  158. * @lock_callbacks: user-supplied locking callbacks
  159. *
  160. * Initialize the access vector cache. Return %0 on
  161. * success or -%1 with @errno set on failure.
  162. * If @msgprefix is NULL, use "uavc". If any callback
  163. * structure references are NULL, use default methods
  164. * for those callbacks (see the definition of the callback
  165. * structures above).
  166. */
  167. int avc_init(const char *msgprefix,
  168. const struct avc_memory_callback *mem_callbacks,
  169. const struct avc_log_callback *log_callbacks,
  170. const struct avc_thread_callback *thread_callbacks,
  171. const struct avc_lock_callback *lock_callbacks);
  172. /**
  173. * avc_open - Initialize the AVC.
  174. * @opts: array of selabel_opt structures specifying AVC options or NULL.
  175. * @nopts: number of elements in opts array or zero for no options.
  176. *
  177. * This function is identical to avc_init(), except the message prefix
  178. * is set to "avc" and any callbacks desired should be specified via
  179. * selinux_set_callback(). Available options are listed above.
  180. */
  181. int avc_open(struct selinux_opt *opts, unsigned nopts);
  182. /**
  183. * avc_cleanup - Remove unused SIDs and AVC entries.
  184. *
  185. * Search the SID table for SID structures with zero
  186. * reference counts, and remove them along with all
  187. * AVC entries that reference them. This can be used
  188. * to return memory to the system.
  189. */
  190. void avc_cleanup(void);
  191. /**
  192. * avc_reset - Flush the cache and reset statistics.
  193. *
  194. * Remove all entries from the cache and reset all access
  195. * statistics (as returned by avc_cache_stats()) to zero.
  196. * The SID mapping is not affected. Return %0 on success,
  197. * -%1 with @errno set on error.
  198. */
  199. int avc_reset(void);
  200. /**
  201. * avc_destroy - Free all AVC structures.
  202. *
  203. * Destroy all AVC structures and free all allocated
  204. * memory. User-supplied locking, memory, and audit
  205. * callbacks will be retained, but security-event
  206. * callbacks will not. All SID's will be invalidated.
  207. * User must call avc_init() if further use of AVC is desired.
  208. */
  209. void avc_destroy(void);
  210. /**
  211. * avc_has_perm_noaudit - Check permissions but perform no auditing.
  212. * @ssid: source security identifier
  213. * @tsid: target security identifier
  214. * @tclass: target security class
  215. * @requested: requested permissions, interpreted based on @tclass
  216. * @aeref: AVC entry reference
  217. * @avd: access vector decisions
  218. *
  219. * Check the AVC to determine whether the @requested permissions are granted
  220. * for the SID pair (@ssid, @tsid), interpreting the permissions
  221. * based on @tclass, and call the security server on a cache miss to obtain
  222. * a new decision and add it to the cache. Update @aeref to refer to an AVC
  223. * entry with the resulting decisions, and return a copy of the decisions
  224. * in @avd. Return %0 if all @requested permissions are granted, -%1 with
  225. * @errno set to %EACCES if any permissions are denied, or to another value
  226. * upon other errors. This function is typically called by avc_has_perm(),
  227. * but may also be called directly to separate permission checking from
  228. * auditing, e.g. in cases where a lock must be held for the check but
  229. * should be released for the auditing.
  230. */
  231. int avc_has_perm_noaudit(security_id_t ssid,
  232. security_id_t tsid,
  233. security_class_t tclass,
  234. access_vector_t requested,
  235. struct avc_entry_ref *aeref, struct av_decision *avd);
  236. /**
  237. * avc_has_perm - Check permissions and perform any appropriate auditing.
  238. * @ssid: source security identifier
  239. * @tsid: target security identifier
  240. * @tclass: target security class
  241. * @requested: requested permissions, interpreted based on @tclass
  242. * @aeref: AVC entry reference
  243. * @auditdata: auxiliary audit data
  244. *
  245. * Check the AVC to determine whether the @requested permissions are granted
  246. * for the SID pair (@ssid, @tsid), interpreting the permissions
  247. * based on @tclass, and call the security server on a cache miss to obtain
  248. * a new decision and add it to the cache. Update @aeref to refer to an AVC
  249. * entry with the resulting decisions. Audit the granting or denial of
  250. * permissions in accordance with the policy. Return %0 if all @requested
  251. * permissions are granted, -%1 with @errno set to %EACCES if any permissions
  252. * are denied or to another value upon other errors.
  253. */
  254. int avc_has_perm(security_id_t ssid, security_id_t tsid,
  255. security_class_t tclass, access_vector_t requested,
  256. struct avc_entry_ref *aeref, void *auditdata);
  257. /**
  258. * avc_audit - Audit the granting or denial of permissions.
  259. * @ssid: source security identifier
  260. * @tsid: target security identifier
  261. * @tclass: target security class
  262. * @requested: requested permissions
  263. * @avd: access vector decisions
  264. * @result: result from avc_has_perm_noaudit
  265. * @auditdata: auxiliary audit data
  266. *
  267. * Audit the granting or denial of permissions in accordance
  268. * with the policy. This function is typically called by
  269. * avc_has_perm() after a permission check, but can also be
  270. * called directly by callers who use avc_has_perm_noaudit()
  271. * in order to separate the permission check from the auditing.
  272. * For example, this separation is useful when the permission check must
  273. * be performed under a lock, to allow the lock to be released
  274. * before calling the auditing code.
  275. */
  276. void avc_audit(security_id_t ssid, security_id_t tsid,
  277. security_class_t tclass, access_vector_t requested,
  278. struct av_decision *avd, int result, void *auditdata);
  279. /**
  280. * avc_compute_create - Compute SID for labeling a new object.
  281. * @ssid: source security identifier
  282. * @tsid: target security identifier
  283. * @tclass: target security class
  284. * @newsid: pointer to SID reference
  285. *
  286. * Call the security server to obtain a context for labeling a
  287. * new object. Look up the context in the SID table, making
  288. * a new entry if not found. Increment the reference counter
  289. * for the SID. Store a pointer to the SID structure into the
  290. * memory referenced by @newsid, returning %0 on success or -%1 on
  291. * error with @errno set.
  292. */
  293. int avc_compute_create(security_id_t ssid,
  294. security_id_t tsid,
  295. security_class_t tclass, security_id_t * newsid);
  296. /**
  297. * avc_compute_member - Compute SID for polyinstantation.
  298. * @ssid: source security identifier
  299. * @tsid: target security identifier
  300. * @tclass: target security class
  301. * @newsid: pointer to SID reference
  302. *
  303. * Call the security server to obtain a context for labeling an
  304. * object instance. Look up the context in the SID table, making
  305. * a new entry if not found. Increment the reference counter
  306. * for the SID. Store a pointer to the SID structure into the
  307. * memory referenced by @newsid, returning %0 on success or -%1 on
  308. * error with @errno set.
  309. */
  310. int avc_compute_member(security_id_t ssid,
  311. security_id_t tsid,
  312. security_class_t tclass, security_id_t * newsid);
  313. /*
  314. * security event callback facility
  315. */
  316. /* security events */
  317. #define AVC_CALLBACK_GRANT 1
  318. #define AVC_CALLBACK_TRY_REVOKE 2
  319. #define AVC_CALLBACK_REVOKE 4
  320. #define AVC_CALLBACK_RESET 8
  321. #define AVC_CALLBACK_AUDITALLOW_ENABLE 16
  322. #define AVC_CALLBACK_AUDITALLOW_DISABLE 32
  323. #define AVC_CALLBACK_AUDITDENY_ENABLE 64
  324. #define AVC_CALLBACK_AUDITDENY_DISABLE 128
  325. /**
  326. * avc_add_callback - Register a callback for security events.
  327. * @callback: callback function
  328. * @events: bitwise OR of desired security events
  329. * @ssid: source security identifier or %SECSID_WILD
  330. * @tsid: target security identifier or %SECSID_WILD
  331. * @tclass: target security class
  332. * @perms: permissions
  333. *
  334. * Register a callback function for events in the set @events
  335. * related to the SID pair (@ssid, @tsid) and
  336. * and the permissions @perms, interpreting
  337. * @perms based on @tclass. Returns %0 on success or
  338. * -%1 if insufficient memory exists to add the callback.
  339. */
  340. int avc_add_callback(int (*callback)
  341. (uint32_t event, security_id_t ssid,
  342. security_id_t tsid, security_class_t tclass,
  343. access_vector_t perms,
  344. access_vector_t * out_retained),
  345. uint32_t events, security_id_t ssid,
  346. security_id_t tsid, security_class_t tclass,
  347. access_vector_t perms);
  348. /*
  349. * AVC statistics
  350. */
  351. /* If set, cache statistics are tracked. This may
  352. * become a compile-time option in the future.
  353. */
  354. #define AVC_CACHE_STATS 1
  355. struct avc_cache_stats {
  356. unsigned entry_lookups;
  357. unsigned entry_hits;
  358. unsigned entry_misses;
  359. unsigned entry_discards;
  360. unsigned cav_lookups;
  361. unsigned cav_hits;
  362. unsigned cav_probes;
  363. unsigned cav_misses;
  364. };
  365. /**
  366. * avc_cache_stats - get cache access statistics.
  367. * @stats: reference to statistics structure
  368. *
  369. * Fill the supplied structure with information about AVC
  370. * activity since the last call to avc_init() or
  371. * avc_reset(). See the structure definition for
  372. * details.
  373. */
  374. void avc_cache_stats(struct avc_cache_stats *stats);
  375. /**
  376. * avc_av_stats - log av table statistics.
  377. *
  378. * Log a message with information about the size and
  379. * distribution of the access vector table. The audit
  380. * callback is used to print the message.
  381. */
  382. void avc_av_stats(void);
  383. /**
  384. * avc_sid_stats - log SID table statistics.
  385. *
  386. * Log a message with information about the size and
  387. * distribution of the SID table. The audit callback
  388. * is used to print the message.
  389. */
  390. void avc_sid_stats(void);
  391. /**
  392. * avc_netlink_open - Create a netlink socket and connect to the kernel.
  393. */
  394. int avc_netlink_open(int blocking);
  395. /**
  396. * avc_netlink_loop - Wait for netlink messages from the kernel
  397. */
  398. void avc_netlink_loop(void);
  399. /**
  400. * avc_netlink_close - Close the netlink socket
  401. */
  402. void avc_netlink_close(void);
  403. /**
  404. * avc_netlink_acquire_fd - Acquire netlink socket fd.
  405. *
  406. * Allows the application to manage messages from the netlink socket in
  407. * its own main loop.
  408. */
  409. int avc_netlink_acquire_fd(void);
  410. /**
  411. * avc_netlink_release_fd - Release netlink socket fd.
  412. *
  413. * Returns ownership of the netlink socket to the library.
  414. */
  415. void avc_netlink_release_fd(void);
  416. /**
  417. * avc_netlink_check_nb - Check netlink socket for new messages.
  418. *
  419. * Called by the application when using avc_netlink_acquire_fd() to
  420. * process kernel netlink events.
  421. */
  422. int avc_netlink_check_nb(void);
  423. #ifdef __cplusplus
  424. }
  425. #endif
  426. #endif /* _SELINUX_AVC_H_ */