os-compatibility.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright © 2012 Collabora, Ltd.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #define _GNU_SOURCE
  26. #include "config.h"
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <signal.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #ifdef HAVE_MEMFD_CREATE
  36. #include <sys/mman.h>
  37. #endif
  38. /* Fallback to no flag when missing the definition */
  39. #ifndef MFD_NOEXEC_SEAL
  40. #define MFD_NOEXEC_SEAL 0
  41. #endif
  42. #include "os-compatibility.h"
  43. #ifndef HAVE_MKOSTEMP
  44. static int
  45. set_cloexec_or_close(int fd)
  46. {
  47. long flags;
  48. if (fd == -1)
  49. return -1;
  50. flags = fcntl(fd, F_GETFD);
  51. if (flags == -1)
  52. goto err;
  53. if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
  54. goto err;
  55. return fd;
  56. err:
  57. close(fd);
  58. return -1;
  59. }
  60. #endif
  61. static int
  62. create_tmpfile_cloexec(char *tmpname)
  63. {
  64. int fd;
  65. #ifdef HAVE_MKOSTEMP
  66. fd = mkostemp(tmpname, O_CLOEXEC);
  67. if (fd >= 0)
  68. unlink(tmpname);
  69. #else
  70. fd = mkstemp(tmpname);
  71. if (fd >= 0) {
  72. fd = set_cloexec_or_close(fd);
  73. unlink(tmpname);
  74. }
  75. #endif
  76. return fd;
  77. }
  78. /*
  79. * Create a new, unique, anonymous file of the given size, and
  80. * return the file descriptor for it. The file descriptor is set
  81. * CLOEXEC. The file is immediately suitable for mmap()'ing
  82. * the given size at offset zero.
  83. *
  84. * The file should not have a permanent backing store like a disk,
  85. * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
  86. *
  87. * The file name is deleted from the file system.
  88. *
  89. * The file is suitable for buffer sharing between processes by
  90. * transmitting the file descriptor over Unix sockets using the
  91. * SCM_RIGHTS methods.
  92. *
  93. * If the C library implements posix_fallocate(), it is used to
  94. * guarantee that disk space is available for the file at the
  95. * given size. If disk space is insufficient, errno is set to ENOSPC.
  96. * If posix_fallocate() is not supported, program may receive
  97. * SIGBUS on accessing mmap()'ed file contents instead.
  98. *
  99. * If the C library implements memfd_create(), it is used to create the
  100. * file purely in memory, without any backing file name on the file
  101. * system, and then sealing off the possibility of shrinking it. This
  102. * can then be checked before accessing mmap()'ed file contents, to
  103. * make sure SIGBUS can't happen. It also avoids requiring
  104. * XDG_RUNTIME_DIR.
  105. */
  106. int
  107. os_create_anonymous_file(off_t size)
  108. {
  109. static const char template[] = "/wayland-cursor-shared-XXXXXX";
  110. const char *path;
  111. char *name;
  112. size_t name_size;
  113. int fd;
  114. #ifdef HAVE_MEMFD_CREATE
  115. /*
  116. * Linux kernels older than 6.3 reject MFD_NOEXEC_SEAL with EINVAL.
  117. * Try first *with* it, and if that fails, try again *without* it.
  118. */
  119. errno = 0;
  120. fd = memfd_create(
  121. "wayland-cursor",
  122. MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC_SEAL);
  123. if (fd < 0 && errno == EINVAL && MFD_NOEXEC_SEAL != 0) {
  124. fd = memfd_create(
  125. "wayland-cursor",
  126. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  127. }
  128. if (fd >= 0) {
  129. /* We can add this seal before calling posix_fallocate(), as
  130. * the file is currently zero-sized anyway.
  131. *
  132. * There is also no need to check for the return value, we
  133. * couldn't do anything with it anyway.
  134. */
  135. fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL);
  136. } else
  137. #endif
  138. {
  139. path = getenv("XDG_RUNTIME_DIR");
  140. if (!path || path[0] != '/') {
  141. errno = ENOENT;
  142. return -1;
  143. }
  144. name_size = strlen(path) + sizeof(template);
  145. name = malloc(name_size);
  146. if (!name)
  147. return -1;
  148. snprintf(name, name_size, "%s%s", path, template);
  149. fd = create_tmpfile_cloexec(name);
  150. free(name);
  151. if (fd < 0)
  152. return -1;
  153. }
  154. if (os_resize_anonymous_file(fd, size) < 0) {
  155. close(fd);
  156. return -1;
  157. }
  158. return fd;
  159. }
  160. int
  161. os_resize_anonymous_file(int fd, off_t size)
  162. {
  163. #ifdef HAVE_POSIX_FALLOCATE
  164. sigset_t mask;
  165. sigset_t old_mask;
  166. /*
  167. * posix_fallocate() might be interrupted, so we need to check
  168. * for EINTR and retry in that case.
  169. * However, in the presence of an alarm, the interrupt may trigger
  170. * repeatedly and prevent a large posix_fallocate() to ever complete
  171. * successfully, so we need to first block SIGALRM to prevent
  172. * this.
  173. */
  174. sigemptyset(&mask);
  175. sigaddset(&mask, SIGALRM);
  176. sigprocmask(SIG_BLOCK, &mask, &old_mask);
  177. /*
  178. * Filesystems that do not support fallocate will return EINVAL or
  179. * EOPNOTSUPP. In this case we need to fall back to ftruncate
  180. */
  181. do {
  182. errno = posix_fallocate(fd, 0, size);
  183. } while (errno == EINTR);
  184. sigprocmask(SIG_SETMASK, &old_mask, NULL);
  185. if (errno == 0)
  186. return 0;
  187. else if (errno != EINVAL && errno != EOPNOTSUPP)
  188. return -1;
  189. #endif
  190. if (ftruncate(fd, size) < 0)
  191. return -1;
  192. return 0;
  193. }