tst-memfd_create.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Test for the memfd_create system call.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <support/check.h>
  22. #include <support/support.h>
  23. #include <support/test-driver.h>
  24. #include <support/xunistd.h>
  25. #include <sys/mman.h>
  26. /* Return true if the descriptor has the FD_CLOEXEC flag set. */
  27. static bool
  28. is_cloexec (int fd)
  29. {
  30. int flags = fcntl (fd, F_GETFD);
  31. TEST_VERIFY (flags >= 0);
  32. return flags & FD_CLOEXEC;
  33. }
  34. /* Return the seals set on FD. */
  35. static int
  36. get_seals (int fd)
  37. {
  38. int flags = fcntl (fd, F_GET_SEALS);
  39. TEST_VERIFY (flags >= 0);
  40. return flags;
  41. }
  42. /* Return true if the F_SEAL_SEAL flag is set on the descriptor. */
  43. static bool
  44. is_sealed (int fd)
  45. {
  46. return get_seals (fd) & F_SEAL_SEAL;
  47. }
  48. static int
  49. do_test (void)
  50. {
  51. /* Initialized by the first call to memfd_create to 0 (memfd_create
  52. unsupported) or 1 (memfd_create is implemented in the kernel).
  53. Subsequent iterations check that the success/failure state is
  54. consistent. */
  55. int supported = -1;
  56. for (int do_cloexec = 0; do_cloexec < 2; ++do_cloexec)
  57. for (int do_sealing = 0; do_sealing < 2; ++do_sealing)
  58. {
  59. int flags = 0;
  60. if (do_cloexec)
  61. flags |= MFD_CLOEXEC;
  62. if (do_sealing)
  63. flags |= MFD_ALLOW_SEALING;
  64. if (test_verbose > 0)
  65. printf ("info: memfd_create with flags=0x%x\n", flags);
  66. int fd = memfd_create ("tst-memfd_create", flags);
  67. if (fd < 0)
  68. {
  69. if (errno == ENOSYS)
  70. {
  71. if (supported < 0)
  72. {
  73. printf ("warning: memfd_create is unsupported\n");
  74. supported = 0;
  75. continue;
  76. }
  77. TEST_VERIFY (supported == 0);
  78. continue;
  79. }
  80. else
  81. FAIL_EXIT1 ("memfd_create: %m");
  82. }
  83. if (supported < 0)
  84. supported = 1;
  85. TEST_VERIFY (supported > 0);
  86. char *fd_path = xasprintf ("/proc/self/fd/%d", fd);
  87. char *link = xreadlink (fd_path);
  88. if (test_verbose > 0)
  89. printf ("info: memfd link: %s\n", link);
  90. TEST_VERIFY (strcmp (link, "memfd:tst-memfd_create (deleted)"));
  91. TEST_VERIFY (is_cloexec (fd) == do_cloexec);
  92. TEST_VERIFY (is_sealed (fd) == !do_sealing);
  93. if (do_sealing)
  94. {
  95. TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_WRITE) == 0);
  96. TEST_VERIFY (!is_sealed (fd));
  97. TEST_VERIFY (get_seals (fd) & F_SEAL_WRITE);
  98. TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_SEAL) == 0);
  99. TEST_VERIFY (is_sealed (fd));
  100. }
  101. xclose (fd);
  102. free (fd_path);
  103. free (link);
  104. }
  105. if (supported == 0)
  106. return EXIT_UNSUPPORTED;
  107. return 0;
  108. }
  109. #include <support/test-driver.c>