veriexec_check.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2011, 2012, 2013, 2015, Juniper Networks, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/types.h>
  29. #include <sys/errno.h>
  30. #include <sys/mac.h>
  31. #include <sys/stat.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <paths.h>
  37. #include <security/mac_veriexec/mac_veriexec.h>
  38. #include "libveriexec.h"
  39. static int
  40. check_fd_mode(int fd, unsigned int mask)
  41. {
  42. struct stat st;
  43. if (fstat(fd, &st) < 0)
  44. return errno;
  45. if ((st.st_mode & mask) == 0)
  46. return EAUTH;
  47. return 0;
  48. }
  49. int
  50. veriexec_check_fd_mode(int fd, unsigned int mask)
  51. {
  52. int error;
  53. if (fd < 0) {
  54. errno = EINVAL;
  55. return -1;
  56. }
  57. error = mac_syscall(MAC_VERIEXEC_NAME, MAC_VERIEXEC_CHECK_FD_SYSCALL,
  58. (void *)(intptr_t)fd);
  59. if (error == -1) {
  60. switch (errno) {
  61. case ENOSYS: /* veriexec not loaded */
  62. error = 0; /* ignore */
  63. break;
  64. }
  65. }
  66. if (mask && error == 0)
  67. error = check_fd_mode(fd, mask);
  68. return (error);
  69. }
  70. int
  71. veriexec_check_path_mode(const char *file, unsigned int mask)
  72. {
  73. int error;
  74. if (!file) {
  75. errno = EINVAL;
  76. return -1;
  77. }
  78. if (mask) {
  79. int fd;
  80. if ((fd = open(file, O_RDONLY)) < 0)
  81. return errno;
  82. error = veriexec_check_fd_mode(fd, mask);
  83. close(fd);
  84. return error;
  85. }
  86. error = mac_syscall(MAC_VERIEXEC_NAME, MAC_VERIEXEC_CHECK_PATH_SYSCALL,
  87. __DECONST(void *, file));
  88. if (error == -1) {
  89. switch (errno) {
  90. case ENOSYS: /* veriexec not loaded */
  91. error = 0; /* ignore */
  92. break;
  93. }
  94. }
  95. return (error);
  96. }
  97. int
  98. veriexec_check_fd(int fd)
  99. {
  100. return veriexec_check_fd_mode(fd, 0);
  101. }
  102. int
  103. veriexec_check_path(const char *file)
  104. {
  105. return veriexec_check_path_mode(file, 0);
  106. }
  107. #if defined(MAIN) || defined(UNIT_TEST)
  108. int
  109. main(int argc __unused, char *argv[] __unused)
  110. {
  111. int error;
  112. int rc = 0;
  113. while (*++argv) {
  114. error = veriexec_check_path(*argv);
  115. if (error == -1) {
  116. rc = 1;
  117. warn("%s", *argv);
  118. }
  119. }
  120. exit(rc);
  121. }
  122. #endif