3
0

selinux_common.c 1.2 KB

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