fw_namespace.c 3.5 KB

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