sysctl-test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test of proc sysctl.
  4. */
  5. #include <kunit/test.h>
  6. #include <linux/sysctl.h>
  7. #define KUNIT_PROC_READ 0
  8. #define KUNIT_PROC_WRITE 1
  9. static int i_zero;
  10. static int i_one_hundred = 100;
  11. /*
  12. * Test that proc_dointvec will not try to use a NULL .data field even when the
  13. * length is non-zero.
  14. */
  15. static void sysctl_test_api_dointvec_null_tbl_data(struct kunit *test)
  16. {
  17. struct ctl_table null_data_table = {
  18. .procname = "foo",
  19. /*
  20. * Here we are testing that proc_dointvec behaves correctly when
  21. * we give it a NULL .data field. Normally this would point to a
  22. * piece of memory where the value would be stored.
  23. */
  24. .data = NULL,
  25. .maxlen = sizeof(int),
  26. .mode = 0644,
  27. .proc_handler = proc_dointvec,
  28. .extra1 = &i_zero,
  29. .extra2 = &i_one_hundred,
  30. };
  31. /*
  32. * proc_dointvec expects a buffer in user space, so we allocate one. We
  33. * also need to cast it to __user so sparse doesn't get mad.
  34. */
  35. void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
  36. GFP_USER);
  37. size_t len;
  38. loff_t pos;
  39. /*
  40. * We don't care what the starting length is since proc_dointvec should
  41. * not try to read because .data is NULL.
  42. */
  43. len = 1234;
  44. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&null_data_table,
  45. KUNIT_PROC_READ, buffer, &len,
  46. &pos));
  47. KUNIT_EXPECT_EQ(test, (size_t)0, len);
  48. /*
  49. * See above.
  50. */
  51. len = 1234;
  52. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&null_data_table,
  53. KUNIT_PROC_WRITE, buffer, &len,
  54. &pos));
  55. KUNIT_EXPECT_EQ(test, (size_t)0, len);
  56. }
  57. /*
  58. * Similar to the previous test, we create a struct ctrl_table that has a .data
  59. * field that proc_dointvec cannot do anything with; however, this time it is
  60. * because we tell proc_dointvec that the size is 0.
  61. */
  62. static void sysctl_test_api_dointvec_table_maxlen_unset(struct kunit *test)
  63. {
  64. int data = 0;
  65. struct ctl_table data_maxlen_unset_table = {
  66. .procname = "foo",
  67. .data = &data,
  68. /*
  69. * So .data is no longer NULL, but we tell proc_dointvec its
  70. * length is 0, so it still shouldn't try to use it.
  71. */
  72. .maxlen = 0,
  73. .mode = 0644,
  74. .proc_handler = proc_dointvec,
  75. .extra1 = &i_zero,
  76. .extra2 = &i_one_hundred,
  77. };
  78. void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
  79. GFP_USER);
  80. size_t len;
  81. loff_t pos;
  82. /*
  83. * As before, we don't care what buffer length is because proc_dointvec
  84. * cannot do anything because its internal .data buffer has zero length.
  85. */
  86. len = 1234;
  87. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&data_maxlen_unset_table,
  88. KUNIT_PROC_READ, buffer, &len,
  89. &pos));
  90. KUNIT_EXPECT_EQ(test, (size_t)0, len);
  91. /*
  92. * See previous comment.
  93. */
  94. len = 1234;
  95. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&data_maxlen_unset_table,
  96. KUNIT_PROC_WRITE, buffer, &len,
  97. &pos));
  98. KUNIT_EXPECT_EQ(test, (size_t)0, len);
  99. }
  100. /*
  101. * Here we provide a valid struct ctl_table, but we try to read and write from
  102. * it using a buffer of zero length, so it should still fail in a similar way as
  103. * before.
  104. */
  105. static void sysctl_test_api_dointvec_table_len_is_zero(struct kunit *test)
  106. {
  107. int data = 0;
  108. /* Good table. */
  109. struct ctl_table table = {
  110. .procname = "foo",
  111. .data = &data,
  112. .maxlen = sizeof(int),
  113. .mode = 0644,
  114. .proc_handler = proc_dointvec,
  115. .extra1 = &i_zero,
  116. .extra2 = &i_one_hundred,
  117. };
  118. void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
  119. GFP_USER);
  120. /*
  121. * However, now our read/write buffer has zero length.
  122. */
  123. size_t len = 0;
  124. loff_t pos;
  125. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_READ, buffer,
  126. &len, &pos));
  127. KUNIT_EXPECT_EQ(test, (size_t)0, len);
  128. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_WRITE, buffer,
  129. &len, &pos));
  130. KUNIT_EXPECT_EQ(test, (size_t)0, len);
  131. }
  132. /*
  133. * Test that proc_dointvec refuses to read when the file position is non-zero.
  134. */
  135. static void sysctl_test_api_dointvec_table_read_but_position_set(
  136. struct kunit *test)
  137. {
  138. int data = 0;
  139. /* Good table. */
  140. struct ctl_table table = {
  141. .procname = "foo",
  142. .data = &data,
  143. .maxlen = sizeof(int),
  144. .mode = 0644,
  145. .proc_handler = proc_dointvec,
  146. .extra1 = &i_zero,
  147. .extra2 = &i_one_hundred,
  148. };
  149. void __user *buffer = (void __user *)kunit_kzalloc(test, sizeof(int),
  150. GFP_USER);
  151. /*
  152. * We don't care about our buffer length because we start off with a
  153. * non-zero file position.
  154. */
  155. size_t len = 1234;
  156. /*
  157. * proc_dointvec should refuse to read into the buffer since the file
  158. * pos is non-zero.
  159. */
  160. loff_t pos = 1;
  161. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_READ, buffer,
  162. &len, &pos));
  163. KUNIT_EXPECT_EQ(test, (size_t)0, len);
  164. }
  165. /*
  166. * Test that we can read a two digit number in a sufficiently size buffer.
  167. * Nothing fancy.
  168. */
  169. static void sysctl_test_dointvec_read_happy_single_positive(struct kunit *test)
  170. {
  171. int data = 0;
  172. /* Good table. */
  173. struct ctl_table table = {
  174. .procname = "foo",
  175. .data = &data,
  176. .maxlen = sizeof(int),
  177. .mode = 0644,
  178. .proc_handler = proc_dointvec,
  179. .extra1 = &i_zero,
  180. .extra2 = &i_one_hundred,
  181. };
  182. size_t len = 4;
  183. loff_t pos = 0;
  184. char *buffer = kunit_kzalloc(test, len, GFP_USER);
  185. char __user *user_buffer = (char __user *)buffer;
  186. /* Store 13 in the data field. */
  187. *((int *)table.data) = 13;
  188. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_READ,
  189. user_buffer, &len, &pos));
  190. KUNIT_ASSERT_EQ(test, (size_t)3, len);
  191. buffer[len] = '\0';
  192. /* And we read 13 back out. */
  193. KUNIT_EXPECT_STREQ(test, "13\n", buffer);
  194. }
  195. /*
  196. * Same as previous test, just now with negative numbers.
  197. */
  198. static void sysctl_test_dointvec_read_happy_single_negative(struct kunit *test)
  199. {
  200. int data = 0;
  201. /* Good table. */
  202. struct ctl_table table = {
  203. .procname = "foo",
  204. .data = &data,
  205. .maxlen = sizeof(int),
  206. .mode = 0644,
  207. .proc_handler = proc_dointvec,
  208. .extra1 = &i_zero,
  209. .extra2 = &i_one_hundred,
  210. };
  211. size_t len = 5;
  212. loff_t pos = 0;
  213. char *buffer = kunit_kzalloc(test, len, GFP_USER);
  214. char __user *user_buffer = (char __user *)buffer;
  215. *((int *)table.data) = -16;
  216. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_READ,
  217. user_buffer, &len, &pos));
  218. KUNIT_ASSERT_EQ(test, (size_t)4, len);
  219. buffer[len] = '\0';
  220. KUNIT_EXPECT_STREQ(test, "-16\n", (char *)buffer);
  221. }
  222. /*
  223. * Test that a simple positive write works.
  224. */
  225. static void sysctl_test_dointvec_write_happy_single_positive(struct kunit *test)
  226. {
  227. int data = 0;
  228. /* Good table. */
  229. struct ctl_table table = {
  230. .procname = "foo",
  231. .data = &data,
  232. .maxlen = sizeof(int),
  233. .mode = 0644,
  234. .proc_handler = proc_dointvec,
  235. .extra1 = &i_zero,
  236. .extra2 = &i_one_hundred,
  237. };
  238. char input[] = "9";
  239. size_t len = sizeof(input) - 1;
  240. loff_t pos = 0;
  241. char *buffer = kunit_kzalloc(test, len, GFP_USER);
  242. char __user *user_buffer = (char __user *)buffer;
  243. memcpy(buffer, input, len);
  244. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_WRITE,
  245. user_buffer, &len, &pos));
  246. KUNIT_EXPECT_EQ(test, sizeof(input) - 1, len);
  247. KUNIT_EXPECT_EQ(test, sizeof(input) - 1, (size_t)pos);
  248. KUNIT_EXPECT_EQ(test, 9, *((int *)table.data));
  249. }
  250. /*
  251. * Same as previous test, but now with negative numbers.
  252. */
  253. static void sysctl_test_dointvec_write_happy_single_negative(struct kunit *test)
  254. {
  255. int data = 0;
  256. struct ctl_table table = {
  257. .procname = "foo",
  258. .data = &data,
  259. .maxlen = sizeof(int),
  260. .mode = 0644,
  261. .proc_handler = proc_dointvec,
  262. .extra1 = &i_zero,
  263. .extra2 = &i_one_hundred,
  264. };
  265. char input[] = "-9";
  266. size_t len = sizeof(input) - 1;
  267. loff_t pos = 0;
  268. char *buffer = kunit_kzalloc(test, len, GFP_USER);
  269. char __user *user_buffer = (char __user *)buffer;
  270. memcpy(buffer, input, len);
  271. KUNIT_EXPECT_EQ(test, 0, proc_dointvec(&table, KUNIT_PROC_WRITE,
  272. user_buffer, &len, &pos));
  273. KUNIT_EXPECT_EQ(test, sizeof(input) - 1, len);
  274. KUNIT_EXPECT_EQ(test, sizeof(input) - 1, (size_t)pos);
  275. KUNIT_EXPECT_EQ(test, -9, *((int *)table.data));
  276. }
  277. /*
  278. * Test that writing a value smaller than the minimum possible value is not
  279. * allowed.
  280. */
  281. static void sysctl_test_api_dointvec_write_single_less_int_min(
  282. struct kunit *test)
  283. {
  284. int data = 0;
  285. struct ctl_table table = {
  286. .procname = "foo",
  287. .data = &data,
  288. .maxlen = sizeof(int),
  289. .mode = 0644,
  290. .proc_handler = proc_dointvec,
  291. .extra1 = &i_zero,
  292. .extra2 = &i_one_hundred,
  293. };
  294. size_t max_len = 32, len = max_len;
  295. loff_t pos = 0;
  296. char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
  297. char __user *user_buffer = (char __user *)buffer;
  298. unsigned long abs_of_less_than_min = (unsigned long)INT_MAX
  299. - (INT_MAX + INT_MIN) + 1;
  300. /*
  301. * We use this rigmarole to create a string that contains a value one
  302. * less than the minimum accepted value.
  303. */
  304. KUNIT_ASSERT_LT(test,
  305. (size_t)snprintf(buffer, max_len, "-%lu",
  306. abs_of_less_than_min),
  307. max_len);
  308. KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec(&table, KUNIT_PROC_WRITE,
  309. user_buffer, &len, &pos));
  310. KUNIT_EXPECT_EQ(test, max_len, len);
  311. KUNIT_EXPECT_EQ(test, 0, *((int *)table.data));
  312. }
  313. /*
  314. * Test that writing the maximum possible value works.
  315. */
  316. static void sysctl_test_api_dointvec_write_single_greater_int_max(
  317. struct kunit *test)
  318. {
  319. int data = 0;
  320. struct ctl_table table = {
  321. .procname = "foo",
  322. .data = &data,
  323. .maxlen = sizeof(int),
  324. .mode = 0644,
  325. .proc_handler = proc_dointvec,
  326. .extra1 = &i_zero,
  327. .extra2 = &i_one_hundred,
  328. };
  329. size_t max_len = 32, len = max_len;
  330. loff_t pos = 0;
  331. char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
  332. char __user *user_buffer = (char __user *)buffer;
  333. unsigned long greater_than_max = (unsigned long)INT_MAX + 1;
  334. KUNIT_ASSERT_GT(test, greater_than_max, (unsigned long)INT_MAX);
  335. KUNIT_ASSERT_LT(test, (size_t)snprintf(buffer, max_len, "%lu",
  336. greater_than_max),
  337. max_len);
  338. KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec(&table, KUNIT_PROC_WRITE,
  339. user_buffer, &len, &pos));
  340. KUNIT_ASSERT_EQ(test, max_len, len);
  341. KUNIT_EXPECT_EQ(test, 0, *((int *)table.data));
  342. }
  343. static struct kunit_case sysctl_test_cases[] = {
  344. KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data),
  345. KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset),
  346. KUNIT_CASE(sysctl_test_api_dointvec_table_len_is_zero),
  347. KUNIT_CASE(sysctl_test_api_dointvec_table_read_but_position_set),
  348. KUNIT_CASE(sysctl_test_dointvec_read_happy_single_positive),
  349. KUNIT_CASE(sysctl_test_dointvec_read_happy_single_negative),
  350. KUNIT_CASE(sysctl_test_dointvec_write_happy_single_positive),
  351. KUNIT_CASE(sysctl_test_dointvec_write_happy_single_negative),
  352. KUNIT_CASE(sysctl_test_api_dointvec_write_single_less_int_min),
  353. KUNIT_CASE(sysctl_test_api_dointvec_write_single_greater_int_max),
  354. {}
  355. };
  356. static struct kunit_suite sysctl_test_suite = {
  357. .name = "sysctl_test",
  358. .test_cases = sysctl_test_cases,
  359. };
  360. kunit_test_suites(&sysctl_test_suite);
  361. MODULE_LICENSE("GPL v2");