3
0

selinux_common.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * libbb/selinux_common.c
  3. * -- common SELinux utility functions
  4. *
  5. * Copyright 2007 KaiGai Kohei <kaigai@kaigai.gr.jp>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #include <selinux/context.h>
  11. context_t FAST_FUNC set_security_context_component(security_context_t cur_context,
  12. char *user, char *role, char *type, char *range)
  13. {
  14. context_t con = context_new(cur_context);
  15. if (!con)
  16. return NULL;
  17. if (user && context_user_set(con, user))
  18. goto error;
  19. if (type && context_type_set(con, type))
  20. goto error;
  21. if (range && context_range_set(con, range))
  22. goto error;
  23. if (role && context_role_set(con, role))
  24. goto error;
  25. return con;
  26. error:
  27. context_free(con);
  28. return NULL;
  29. }
  30. void FAST_FUNC setfscreatecon_or_die(security_context_t scontext)
  31. {
  32. if (setfscreatecon(scontext) < 0) {
  33. /* Can be NULL. All known printf implementations
  34. * display "(null)", "<null>" etc */
  35. bb_perror_msg_and_die("can't set default "
  36. "file creation context to %s", scontext);
  37. }
  38. }
  39. void FAST_FUNC selinux_preserve_fcontext(int fdesc)
  40. {
  41. security_context_t context;
  42. if (fgetfilecon(fdesc, &context) < 0) {
  43. if (errno == ENODATA || errno == ENOTSUP)
  44. return;
  45. bb_perror_msg_and_die("fgetfilecon failed");
  46. }
  47. setfscreatecon_or_die(context);
  48. freecon(context);
  49. }