selinux.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_LIBSELINUX
  30. #include <selinux/selinux.h>
  31. #include <selinux/avc.h>
  32. #include <selinux/label.h>
  33. #endif
  34. #include "main.h"
  35. #ifdef WITH_LIBSELINUX
  36. static struct selabel_handle *sehandle;
  37. #endif
  38. void
  39. dpkg_selabel_load(void)
  40. {
  41. #ifdef WITH_LIBSELINUX
  42. static int selinux_enabled = -1;
  43. if (selinux_enabled < 0) {
  44. int rc;
  45. /* Set selinux_enabled if it is not already set (singleton). */
  46. selinux_enabled = (is_selinux_enabled() > 0);
  47. if (!selinux_enabled)
  48. return;
  49. /* Open the SELinux status notification channel, with fallback
  50. * enabled for older kernels. */
  51. rc = selinux_status_open(1);
  52. if (rc < 0)
  53. ohshit(_("cannot open security status notification channel"));
  54. /* XXX: We could use selinux_set_callback() to redirect the
  55. * errors from the other SELinux calls, but that does not seem
  56. * worth it right now. */
  57. } else if (selinux_enabled && selinux_status_updated()) {
  58. /* The SELinux policy got updated in the kernel, usually after
  59. * upgrading the package shipping it, we need to reload. */
  60. selabel_close(sehandle);
  61. } else {
  62. /* SELinux is either disabled or it does not need a reload. */
  63. return;
  64. }
  65. sehandle = selabel_open(SELABEL_CTX_FILE, NULL, 0);
  66. if (sehandle == NULL && security_getenforce() == 1)
  67. ohshite(_("cannot get security labeling handle"));
  68. #endif
  69. }
  70. void
  71. dpkg_selabel_set_context(const char *matchpath, const char *path, mode_t mode)
  72. {
  73. #ifdef WITH_LIBSELINUX
  74. security_context_t scontext = NULL;
  75. int ret;
  76. /* If SELinux is not enabled just do nothing. */
  77. if (sehandle == NULL)
  78. return;
  79. /*
  80. * We use the _raw function variants here so that no translation
  81. * happens from computer to human readable forms, to avoid issues
  82. * when mcstransd has disappeared during the unpack process.
  83. */
  84. /* Do nothing if we can't figure out what the context is, or if it has
  85. * no context; in which case the default context shall be applied. */
  86. ret = selabel_lookup_raw(sehandle, &scontext, matchpath, mode & S_IFMT);
  87. if (ret == -1 || (ret == 0 && scontext == NULL))
  88. return;
  89. ret = lsetfilecon_raw(path, scontext);
  90. if (ret < 0 && errno != ENOTSUP)
  91. ohshite(_("cannot set security context for file object '%s'"),
  92. path);
  93. freecon(scontext);
  94. #endif /* WITH_LIBSELINUX */
  95. }
  96. void
  97. dpkg_selabel_close(void)
  98. {
  99. #ifdef WITH_LIBSELINUX
  100. if (sehandle == NULL)
  101. return;
  102. selinux_status_close();
  103. selabel_close(sehandle);
  104. sehandle = NULL;
  105. #endif
  106. }