fw_namespace.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Test triggering of loading of firmware from different mount
  3. * namespaces. Expect firmware to be always loaded from the mount
  4. * namespace of PID 1. */
  5. #define _GNU_SOURCE
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <sched.h>
  9. #include <stdarg.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/mount.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <unistd.h>
  19. #ifndef CLONE_NEWNS
  20. # define CLONE_NEWNS 0x00020000
  21. #endif
  22. static char *fw_path = NULL;
  23. static void die(char *fmt, ...)
  24. {
  25. va_list ap;
  26. va_start(ap, fmt);
  27. vfprintf(stderr, fmt, ap);
  28. va_end(ap);
  29. if (fw_path)
  30. unlink(fw_path);
  31. umount("/lib/firmware");
  32. exit(EXIT_FAILURE);
  33. }
  34. static void trigger_fw(const char *fw_name, const char *sys_path)
  35. {
  36. int fd;
  37. fd = open(sys_path, O_WRONLY);
  38. if (fd < 0)
  39. die("open failed: %s\n",
  40. strerror(errno));
  41. if (write(fd, fw_name, strlen(fw_name)) != strlen(fw_name))
  42. exit(EXIT_FAILURE);
  43. close(fd);
  44. }
  45. static void setup_fw(const char *fw_path)
  46. {
  47. int fd;
  48. const char fw[] = "ABCD0123";
  49. fd = open(fw_path, O_WRONLY | O_CREAT, 0600);
  50. if (fd < 0)
  51. die("open failed: %s\n",
  52. strerror(errno));
  53. if (write(fd, fw, sizeof(fw) -1) != sizeof(fw) -1)
  54. die("write failed: %s\n",
  55. strerror(errno));
  56. close(fd);
  57. }
  58. static bool test_fw_in_ns(const char *fw_name, const char *sys_path, bool block_fw_in_parent_ns)
  59. {
  60. pid_t child;
  61. if (block_fw_in_parent_ns)
  62. if (mount("test", "/lib/firmware", "tmpfs", MS_RDONLY, NULL) == -1)
  63. die("blocking firmware in parent ns failed\n");
  64. child = fork();
  65. if (child == -1) {
  66. die("fork failed: %s\n",
  67. strerror(errno));
  68. }
  69. if (child != 0) { /* parent */
  70. pid_t pid;
  71. int status;
  72. pid = waitpid(child, &status, 0);
  73. if (pid == -1) {
  74. die("waitpid failed: %s\n",
  75. strerror(errno));
  76. }
  77. if (pid != child) {
  78. die("waited for %d got %d\n",
  79. child, pid);
  80. }
  81. if (!WIFEXITED(status)) {
  82. die("child did not terminate cleanly\n");
  83. }
  84. if (block_fw_in_parent_ns)
  85. umount("/lib/firmware");
  86. return WEXITSTATUS(status) == EXIT_SUCCESS;
  87. }
  88. if (unshare(CLONE_NEWNS) != 0) {
  89. die("unshare(CLONE_NEWNS) failed: %s\n",
  90. strerror(errno));
  91. }
  92. if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) == -1)
  93. die("remount root in child ns failed\n");
  94. if (!block_fw_in_parent_ns) {
  95. if (mount("test", "/lib/firmware", "tmpfs", MS_RDONLY, NULL) == -1)
  96. die("blocking firmware in child ns failed\n");
  97. } else
  98. umount("/lib/firmware");
  99. trigger_fw(fw_name, sys_path);
  100. exit(EXIT_SUCCESS);
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. const char *fw_name = "test-firmware.bin";
  105. char *sys_path;
  106. if (argc != 2)
  107. die("usage: %s sys_path\n", argv[0]);
  108. /* Mount tmpfs to /lib/firmware so we don't have to assume
  109. that it is writable for us.*/
  110. if (mount("test", "/lib/firmware", "tmpfs", 0, NULL) == -1)
  111. die("mounting tmpfs to /lib/firmware failed\n");
  112. sys_path = argv[1];
  113. if (asprintf(&fw_path, "/lib/firmware/%s", fw_name) < 0)
  114. die("error: failed to build full fw_path\n");
  115. setup_fw(fw_path);
  116. setvbuf(stdout, NULL, _IONBF, 0);
  117. /* Positive case: firmware in PID1 mount namespace */
  118. printf("Testing with firmware in parent namespace (assumed to be same file system as PID1)\n");
  119. if (!test_fw_in_ns(fw_name, sys_path, false))
  120. die("error: failed to access firmware\n");
  121. /* Negative case: firmware in child mount namespace, expected to fail */
  122. printf("Testing with firmware in child namespace\n");
  123. if (test_fw_in_ns(fw_name, sys_path, true))
  124. die("error: firmware access did not fail\n");
  125. unlink(fw_path);
  126. free(fw_path);
  127. umount("/lib/firmware");
  128. exit(EXIT_SUCCESS);
  129. }