liveupdate.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2025, Google LLC.
  4. * Pasha Tatashin <pasha.tatashin@soleen.com>
  5. */
  6. /*
  7. * Selftests for the Live Update Orchestrator.
  8. * This test suite verifies the functionality and behavior of the
  9. * /dev/liveupdate character device and its session management capabilities.
  10. *
  11. * Tests include:
  12. * - Device access: basic open/close, and enforcement of exclusive access.
  13. * - Session management: creation of unique sessions, and duplicate name detection.
  14. * - Resource preservation: successfully preserving individual and multiple memfds,
  15. * verifying contents remain accessible.
  16. * - Complex multi-session scenarios involving mixed empty and populated files.
  17. */
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <string.h>
  21. #include <sys/ioctl.h>
  22. #include <unistd.h>
  23. #include <linux/liveupdate.h>
  24. #include "../kselftest.h"
  25. #include "../kselftest_harness.h"
  26. #define LIVEUPDATE_DEV "/dev/liveupdate"
  27. FIXTURE(liveupdate_device) {
  28. int fd1;
  29. int fd2;
  30. };
  31. FIXTURE_SETUP(liveupdate_device)
  32. {
  33. self->fd1 = -1;
  34. self->fd2 = -1;
  35. }
  36. FIXTURE_TEARDOWN(liveupdate_device)
  37. {
  38. if (self->fd1 >= 0)
  39. close(self->fd1);
  40. if (self->fd2 >= 0)
  41. close(self->fd2);
  42. }
  43. /*
  44. * Test Case: Basic Open and Close
  45. *
  46. * Verifies that the /dev/liveupdate device can be opened and subsequently
  47. * closed without errors. Skips if the device does not exist.
  48. */
  49. TEST_F(liveupdate_device, basic_open_close)
  50. {
  51. self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
  52. if (self->fd1 < 0 && errno == ENOENT)
  53. SKIP(return, "%s does not exist.", LIVEUPDATE_DEV);
  54. ASSERT_GE(self->fd1, 0);
  55. ASSERT_EQ(close(self->fd1), 0);
  56. self->fd1 = -1;
  57. }
  58. /*
  59. * Test Case: Exclusive Open Enforcement
  60. *
  61. * Verifies that the /dev/liveupdate device can only be opened by one process
  62. * at a time. It checks that a second attempt to open the device fails with
  63. * the EBUSY error code.
  64. */
  65. TEST_F(liveupdate_device, exclusive_open)
  66. {
  67. self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
  68. if (self->fd1 < 0 && errno == ENOENT)
  69. SKIP(return, "%s does not exist.", LIVEUPDATE_DEV);
  70. ASSERT_GE(self->fd1, 0);
  71. self->fd2 = open(LIVEUPDATE_DEV, O_RDWR);
  72. EXPECT_LT(self->fd2, 0);
  73. EXPECT_EQ(errno, EBUSY);
  74. }
  75. /* Helper function to create a LUO session via ioctl. */
  76. static int create_session(int lu_fd, const char *name)
  77. {
  78. struct liveupdate_ioctl_create_session args = {};
  79. args.size = sizeof(args);
  80. strncpy((char *)args.name, name, sizeof(args.name) - 1);
  81. if (ioctl(lu_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &args))
  82. return -errno;
  83. return args.fd;
  84. }
  85. /*
  86. * Test Case: Create Duplicate Session
  87. *
  88. * Verifies that attempting to create two sessions with the same name fails
  89. * on the second attempt with EEXIST.
  90. */
  91. TEST_F(liveupdate_device, create_duplicate_session)
  92. {
  93. int session_fd1, session_fd2;
  94. self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
  95. if (self->fd1 < 0 && errno == ENOENT)
  96. SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
  97. ASSERT_GE(self->fd1, 0);
  98. session_fd1 = create_session(self->fd1, "duplicate-session-test");
  99. ASSERT_GE(session_fd1, 0);
  100. session_fd2 = create_session(self->fd1, "duplicate-session-test");
  101. EXPECT_LT(session_fd2, 0);
  102. EXPECT_EQ(-session_fd2, EEXIST);
  103. ASSERT_EQ(close(session_fd1), 0);
  104. }
  105. /*
  106. * Test Case: Create Distinct Sessions
  107. *
  108. * Verifies that creating two sessions with different names succeeds.
  109. */
  110. TEST_F(liveupdate_device, create_distinct_sessions)
  111. {
  112. int session_fd1, session_fd2;
  113. self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
  114. if (self->fd1 < 0 && errno == ENOENT)
  115. SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
  116. ASSERT_GE(self->fd1, 0);
  117. session_fd1 = create_session(self->fd1, "distinct-session-1");
  118. ASSERT_GE(session_fd1, 0);
  119. session_fd2 = create_session(self->fd1, "distinct-session-2");
  120. ASSERT_GE(session_fd2, 0);
  121. ASSERT_EQ(close(session_fd1), 0);
  122. ASSERT_EQ(close(session_fd2), 0);
  123. }
  124. static int preserve_fd(int session_fd, int fd_to_preserve, __u64 token)
  125. {
  126. struct liveupdate_session_preserve_fd args = {};
  127. args.size = sizeof(args);
  128. args.fd = fd_to_preserve;
  129. args.token = token;
  130. if (ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &args))
  131. return -errno;
  132. return 0;
  133. }
  134. /*
  135. * Test Case: Preserve MemFD
  136. *
  137. * Verifies that a valid memfd can be successfully preserved in a session and
  138. * that its contents remain intact after the preservation call.
  139. */
  140. TEST_F(liveupdate_device, preserve_memfd)
  141. {
  142. const char *test_str = "hello liveupdate";
  143. char read_buf[64] = {};
  144. int session_fd, mem_fd;
  145. self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
  146. if (self->fd1 < 0 && errno == ENOENT)
  147. SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
  148. ASSERT_GE(self->fd1, 0);
  149. session_fd = create_session(self->fd1, "preserve-memfd-test");
  150. ASSERT_GE(session_fd, 0);
  151. mem_fd = memfd_create("test-memfd", 0);
  152. ASSERT_GE(mem_fd, 0);
  153. ASSERT_EQ(write(mem_fd, test_str, strlen(test_str)), strlen(test_str));
  154. ASSERT_EQ(preserve_fd(session_fd, mem_fd, 0x1234), 0);
  155. ASSERT_EQ(close(session_fd), 0);
  156. ASSERT_EQ(lseek(mem_fd, 0, SEEK_SET), 0);
  157. ASSERT_EQ(read(mem_fd, read_buf, sizeof(read_buf)), strlen(test_str));
  158. ASSERT_STREQ(read_buf, test_str);
  159. ASSERT_EQ(close(mem_fd), 0);
  160. }
  161. /*
  162. * Test Case: Preserve Multiple MemFDs
  163. *
  164. * Verifies that multiple memfds can be preserved in a single session,
  165. * each with a unique token, and that their contents remain distinct and
  166. * correct after preservation.
  167. */
  168. TEST_F(liveupdate_device, preserve_multiple_memfds)
  169. {
  170. const char *test_str1 = "data for memfd one";
  171. const char *test_str2 = "data for memfd two";
  172. char read_buf[64] = {};
  173. int session_fd, mem_fd1, mem_fd2;
  174. self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
  175. if (self->fd1 < 0 && errno == ENOENT)
  176. SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
  177. ASSERT_GE(self->fd1, 0);
  178. session_fd = create_session(self->fd1, "preserve-multi-memfd-test");
  179. ASSERT_GE(session_fd, 0);
  180. mem_fd1 = memfd_create("test-memfd-1", 0);
  181. ASSERT_GE(mem_fd1, 0);
  182. mem_fd2 = memfd_create("test-memfd-2", 0);
  183. ASSERT_GE(mem_fd2, 0);
  184. ASSERT_EQ(write(mem_fd1, test_str1, strlen(test_str1)), strlen(test_str1));
  185. ASSERT_EQ(write(mem_fd2, test_str2, strlen(test_str2)), strlen(test_str2));
  186. ASSERT_EQ(preserve_fd(session_fd, mem_fd1, 0xAAAA), 0);
  187. ASSERT_EQ(preserve_fd(session_fd, mem_fd2, 0xBBBB), 0);
  188. memset(read_buf, 0, sizeof(read_buf));
  189. ASSERT_EQ(lseek(mem_fd1, 0, SEEK_SET), 0);
  190. ASSERT_EQ(read(mem_fd1, read_buf, sizeof(read_buf)), strlen(test_str1));
  191. ASSERT_STREQ(read_buf, test_str1);
  192. memset(read_buf, 0, sizeof(read_buf));
  193. ASSERT_EQ(lseek(mem_fd2, 0, SEEK_SET), 0);
  194. ASSERT_EQ(read(mem_fd2, read_buf, sizeof(read_buf)), strlen(test_str2));
  195. ASSERT_STREQ(read_buf, test_str2);
  196. ASSERT_EQ(close(mem_fd1), 0);
  197. ASSERT_EQ(close(mem_fd2), 0);
  198. ASSERT_EQ(close(session_fd), 0);
  199. }
  200. /*
  201. * Test Case: Preserve Complex Scenario
  202. *
  203. * Verifies a more complex scenario with multiple sessions and a mix of empty
  204. * and non-empty memfds distributed across them.
  205. */
  206. TEST_F(liveupdate_device, preserve_complex_scenario)
  207. {
  208. const char *data1 = "data for session 1";
  209. const char *data2 = "data for session 2";
  210. char read_buf[64] = {};
  211. int session_fd1, session_fd2;
  212. int mem_fd_data1, mem_fd_empty1, mem_fd_data2, mem_fd_empty2;
  213. self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
  214. if (self->fd1 < 0 && errno == ENOENT)
  215. SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
  216. ASSERT_GE(self->fd1, 0);
  217. session_fd1 = create_session(self->fd1, "complex-session-1");
  218. ASSERT_GE(session_fd1, 0);
  219. session_fd2 = create_session(self->fd1, "complex-session-2");
  220. ASSERT_GE(session_fd2, 0);
  221. mem_fd_data1 = memfd_create("data1", 0);
  222. ASSERT_GE(mem_fd_data1, 0);
  223. ASSERT_EQ(write(mem_fd_data1, data1, strlen(data1)), strlen(data1));
  224. mem_fd_empty1 = memfd_create("empty1", 0);
  225. ASSERT_GE(mem_fd_empty1, 0);
  226. mem_fd_data2 = memfd_create("data2", 0);
  227. ASSERT_GE(mem_fd_data2, 0);
  228. ASSERT_EQ(write(mem_fd_data2, data2, strlen(data2)), strlen(data2));
  229. mem_fd_empty2 = memfd_create("empty2", 0);
  230. ASSERT_GE(mem_fd_empty2, 0);
  231. ASSERT_EQ(preserve_fd(session_fd1, mem_fd_data1, 0x1111), 0);
  232. ASSERT_EQ(preserve_fd(session_fd1, mem_fd_empty1, 0x2222), 0);
  233. ASSERT_EQ(preserve_fd(session_fd2, mem_fd_data2, 0x3333), 0);
  234. ASSERT_EQ(preserve_fd(session_fd2, mem_fd_empty2, 0x4444), 0);
  235. ASSERT_EQ(lseek(mem_fd_data1, 0, SEEK_SET), 0);
  236. ASSERT_EQ(read(mem_fd_data1, read_buf, sizeof(read_buf)), strlen(data1));
  237. ASSERT_STREQ(read_buf, data1);
  238. memset(read_buf, 0, sizeof(read_buf));
  239. ASSERT_EQ(lseek(mem_fd_data2, 0, SEEK_SET), 0);
  240. ASSERT_EQ(read(mem_fd_data2, read_buf, sizeof(read_buf)), strlen(data2));
  241. ASSERT_STREQ(read_buf, data2);
  242. ASSERT_EQ(lseek(mem_fd_empty1, 0, SEEK_SET), 0);
  243. ASSERT_EQ(read(mem_fd_empty1, read_buf, sizeof(read_buf)), 0);
  244. ASSERT_EQ(lseek(mem_fd_empty2, 0, SEEK_SET), 0);
  245. ASSERT_EQ(read(mem_fd_empty2, read_buf, sizeof(read_buf)), 0);
  246. ASSERT_EQ(close(mem_fd_data1), 0);
  247. ASSERT_EQ(close(mem_fd_empty1), 0);
  248. ASSERT_EQ(close(mem_fd_data2), 0);
  249. ASSERT_EQ(close(mem_fd_empty2), 0);
  250. ASSERT_EQ(close(session_fd1), 0);
  251. ASSERT_EQ(close(session_fd2), 0);
  252. }
  253. /*
  254. * Test Case: Preserve Unsupported File Descriptor
  255. *
  256. * Verifies that attempting to preserve a file descriptor that does not have
  257. * a registered Live Update handler fails gracefully.
  258. * Uses /dev/null as a representative of a file type (character device)
  259. * that is not supported by the orchestrator.
  260. */
  261. TEST_F(liveupdate_device, preserve_unsupported_fd)
  262. {
  263. int session_fd, unsupported_fd;
  264. int ret;
  265. self->fd1 = open(LIVEUPDATE_DEV, O_RDWR);
  266. if (self->fd1 < 0 && errno == ENOENT)
  267. SKIP(return, "%s does not exist", LIVEUPDATE_DEV);
  268. ASSERT_GE(self->fd1, 0);
  269. session_fd = create_session(self->fd1, "unsupported-fd-test");
  270. ASSERT_GE(session_fd, 0);
  271. unsupported_fd = open("/dev/null", O_RDWR);
  272. ASSERT_GE(unsupported_fd, 0);
  273. ret = preserve_fd(session_fd, unsupported_fd, 0xDEAD);
  274. EXPECT_EQ(ret, -ENOENT);
  275. ASSERT_EQ(close(unsupported_fd), 0);
  276. ASSERT_EQ(close(session_fd), 0);
  277. }
  278. TEST_HARNESS_MAIN