selinux.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * dpkg - main program for package management
  3. * selinux.c - SE Linux support
  4. *
  5. * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <dpkg/i18n.h>
  27. #include <dpkg/dpkg.h>
  28. #include <dpkg/dpkg-db.h>
  29. #ifdef WITH_SELINUX
  30. #include <selinux/selinux.h>
  31. #include <selinux/avc.h>
  32. #include <selinux/label.h>
  33. #endif
  34. #include "main.h"
  35. #ifdef WITH_SELINUX
  36. static struct selabel_handle *dpkg_sehandle;
  37. static struct selabel_handle *
  38. dpkg_selabel_get_handle(void)
  39. {
  40. return dpkg_sehandle;
  41. }
  42. #endif
  43. void
  44. dpkg_selabel_load(void)
  45. {
  46. #ifdef WITH_SELINUX
  47. static int selinux_enabled = -1;
  48. if (selinux_enabled < 0) {
  49. int rc;
  50. /* Set selinux_enabled if it is not already set (singleton). */
  51. selinux_enabled = (is_selinux_enabled() > 0);
  52. if (!selinux_enabled)
  53. return;
  54. /* Open the SELinux status notification channel, with fallback
  55. * enabled for older kernels. */
  56. rc = selinux_status_open(1);
  57. if (rc < 0)
  58. ohshit(_("cannot open security status notification channel"));
  59. /* XXX: We could use selinux_set_callback() to redirect the
  60. * errors from the other SELinux calls, but that does not seem
  61. * worth it right now. */
  62. } else if (selinux_enabled && selinux_status_updated()) {
  63. /* The SELinux policy got updated in the kernel, usually after
  64. * upgrading the package shipping it, we need to reload. */
  65. selabel_close(dpkg_sehandle);
  66. } else {
  67. /* SELinux is either disabled or it does not need a reload. */
  68. return;
  69. }
  70. dpkg_sehandle = selabel_open(SELABEL_CTX_FILE, NULL, 0);
  71. if (dpkg_sehandle == NULL)
  72. ohshite(_("cannot get security labeling handle"));
  73. #endif
  74. }
  75. void
  76. dpkg_selabel_set_context(const char *matchpath, const char *path, mode_t mode)
  77. {
  78. #ifdef WITH_SELINUX
  79. struct selabel_handle *sehandle;
  80. security_context_t scontext = NULL;
  81. int ret;
  82. /* If there's no file type, just give up. */
  83. if ((mode & S_IFMT) == 0)
  84. return;
  85. /* If SELinux is not enabled just do nothing. */
  86. sehandle = dpkg_selabel_get_handle();
  87. if (sehandle == NULL)
  88. return;
  89. /*
  90. * We use the _raw function variants here so that no translation
  91. * happens from computer to human readable forms, to avoid issues
  92. * when mcstransd has disappeared during the unpack process.
  93. */
  94. /* Do nothing if we can't figure out what the context is, or if it has
  95. * no context; in which case the default context shall be applied. */
  96. ret = selabel_lookup_raw(sehandle, &scontext, matchpath, mode & S_IFMT);
  97. if (ret == -1 || (ret == 0 && scontext == NULL))
  98. return;
  99. ret = lsetfilecon_raw(path, scontext);
  100. if (ret < 0 && errno != ENOTSUP)
  101. ohshite(_("cannot set security context for file object '%s'"),
  102. path);
  103. freecon(scontext);
  104. #endif /* WITH_SELINUX */
  105. }
  106. void
  107. dpkg_selabel_close(void)
  108. {
  109. #ifdef WITH_SELINUX
  110. if (dpkg_sehandle == NULL)
  111. return;
  112. selinux_status_close();
  113. selabel_close(dpkg_sehandle);
  114. dpkg_sehandle = NULL;
  115. #endif
  116. }