[PATCH v3 3/5] sysctl: add KUnit test code to check for encoding min/max in table entries
From: Wen Yang
Date: Sat Sep 14 2024 - 22:10:15 EST
Add KUnit test code to check the impact of encoding the min/max values
directly in table entries on functions such as proc_rointvec,
proc_rou8vectminmax, proc_rouintvectminmax, and proc_roulongvectminmax,
including basic parsing testing and min/max overflow testing.
Signed-off-by: Wen Yang <wen.yang@xxxxxxxxx>
Cc: Luis Chamberlain <mcgrof@xxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Cc: Joel Granados <j.granados@xxxxxxxxxxx>
Cc: Eric W. Biederman <ebiederm@xxxxxxxxxxxx>
Cc: Christian Brauner <brauner@xxxxxxxxxx>
Cc: Dave Young <dyoung@xxxxxxxxxx>
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
kernel/sysctl-test.c | 581 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 581 insertions(+)
diff --git a/kernel/sysctl-test.c b/kernel/sysctl-test.c
index 3ac98bb7fb82..486240787103 100644
--- a/kernel/sysctl-test.c
+++ b/kernel/sysctl-test.c
@@ -415,6 +415,575 @@ static void sysctl_test_register_sysctl_sz_invalid_extra_value(
KUNIT_EXPECT_NOT_NULL(test, register_sysctl("foo", table_qux));
}
+static void sysctl_test_api_dointvec_write_with_minmax(
+ struct kunit *test)
+{
+ int data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(int),
+ .mode = 0644 | SYSCTL_FLAG_MIN | SYSCTL_FLAG_MAX,
+ .proc_handler = proc_dointvec_minmax,
+ .min = SYSCTL_NUM_NEG_ONE,
+ .max = SYSCTL_NUM_ONE_HUNDRED,
+ };
+ size_t max_len = 32, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "10";
+ char input2[] = "-5";
+ char input3[] = "200";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 10, *((int *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 10, *((int *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 10, *((int *)table.data));
+}
+
+static void sysctl_test_api_dointvec_write_with_min(
+ struct kunit *test)
+{
+ int data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(int),
+ .mode = 0644 | SYSCTL_FLAG_MIN,
+ .proc_handler = proc_dointvec_minmax,
+ .min = SYSCTL_NUM_NEG_ONE,
+ };
+ size_t max_len = 32, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "10";
+ char input2[] = "-5";
+ char input3[] = "2147483647";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 10, *((int *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 10, *((int *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input3) - 1, len);
+ KUNIT_EXPECT_EQ(test, 2147483647, *((int *)table.data));
+}
+
+static void sysctl_test_api_dointvec_write_with_max(
+ struct kunit *test)
+{
+ int data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(int),
+ .mode = 0644 | SYSCTL_FLAG_MAX,
+ .proc_handler = proc_dointvec_minmax,
+ .max = SYSCTL_NUM_ONE_HUNDRED,
+ };
+ size_t max_len = 32, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "10";
+ char input2[] = "2147483647";
+ char input3[] = "-2147483648";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 10, *((int *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 10, *((int *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dointvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input3) - 1, len);
+ KUNIT_EXPECT_EQ(test, -2147483648, *((int *)table.data));
+}
+
+static void sysctl_test_api_douintvec_write_with_minmax(
+ struct kunit *test)
+{
+ unsigned int data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(int),
+ .mode = 0644 | SYSCTL_FLAG_MIN | SYSCTL_FLAG_MAX,
+ .proc_handler = proc_douintvec_minmax,
+ .min = SYSCTL_NUM_FOUR,
+ .max = SYSCTL_NUM_TWO_HUNDRED,
+ };
+ size_t max_len = 32, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "100";
+ char input2[] = "3";
+ char input3[] = "1000";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 100, *((unsigned int *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 100, *((unsigned int *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 100, *((unsigned int *)table.data));
+}
+
+static void sysctl_test_api_douintvec_write_with_min(
+ struct kunit *test)
+{
+ unsigned int data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(int),
+ .mode = 0644 | SYSCTL_FLAG_MIN,
+ .proc_handler = proc_douintvec_minmax,
+ .min = SYSCTL_NUM_FOUR,
+ };
+ size_t max_len = 32, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "100";
+ char input2[] = "3";
+ char input3[] = "4294967295";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 100, *((unsigned int *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 100, *((unsigned int *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input3) - 1, len);
+ KUNIT_EXPECT_EQ(test, 4294967295, *((unsigned int *)table.data));
+}
+
+static void sysctl_test_api_douintvec_write_with_max(
+ struct kunit *test)
+{
+ unsigned int data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(int),
+ .mode = 0644 | SYSCTL_FLAG_MAX,
+ .proc_handler = proc_douintvec_minmax,
+ .max = SYSCTL_NUM_ONE_THOUSAND,
+ };
+ size_t max_len = 32, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "900";
+ char input2[] = "10000";
+ char input3[] = "0";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 900, *((unsigned int *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 900, *((unsigned int *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_douintvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input3) - 1, len);
+ KUNIT_EXPECT_EQ(test, 0, *((unsigned int *)table.data));
+}
+
+static void sysctl_test_api_dou8vec_write_with_minmax(
+ struct kunit *test)
+{
+ unsigned char data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(unsigned char),
+ .mode = 0644 | SYSCTL_FLAG_MIN | SYSCTL_FLAG_MAX,
+ .proc_handler = proc_dou8vec_minmax,
+ .min = SYSCTL_NUM_THREE,
+ .max = SYSCTL_NUM_ONE_HUNDRED,
+ };
+ size_t max_len = 8, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "32";
+ char input2[] = "1";
+ char input3[] = "200";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 32, *((unsigned char *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 32, *((unsigned char *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 32, *((unsigned char *)table.data));
+}
+
+static void sysctl_test_api_dou8vec_write_with_min(
+ struct kunit *test)
+{
+ unsigned char data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(unsigned char),
+ .mode = 0644 | SYSCTL_FLAG_MIN,
+ .proc_handler = proc_dou8vec_minmax,
+ .min = SYSCTL_NUM_THREE,
+ };
+ size_t max_len = 8, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "32";
+ char input2[] = "1";
+ char input3[] = "255";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 32, *((unsigned char *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 32, *((unsigned char *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input3) - 1, len);
+ KUNIT_EXPECT_EQ(test, 255, *((unsigned char *)table.data));
+}
+
+static void sysctl_test_api_dou8vec_write_with_max(
+ struct kunit *test)
+{
+ unsigned char data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(unsigned char),
+ .mode = 0644 | SYSCTL_FLAG_MAX,
+ .proc_handler = proc_dou8vec_minmax,
+ .max = SYSCTL_NUM_TWO_HUNDRED,
+ };
+ size_t max_len = 8, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "32";
+ char input2[] = "0";
+ char input3[] = "255";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 32, *((unsigned char *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input2) - 1, len);
+ KUNIT_EXPECT_EQ(test, 0, *((unsigned char *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_dou8vec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 0, *((unsigned char *)table.data));
+}
+
+static void sysctl_test_api_doulongvec_write_with_minmax(
+ struct kunit *test)
+{
+ unsigned long data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(unsigned long),
+ .mode = 0644 | SYSCTL_FLAG_MIN | SYSCTL_FLAG_MAX,
+ .proc_handler = proc_doulongvec_minmax,
+ .min = SYSCTL_NUM_ONE_THOUSAND,
+ .max = SYSCTL_NUM_THREE_THOUSAND,
+ };
+ size_t max_len = 64, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "1024";
+ char input2[] = "100";
+ char input3[] = "4096";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 1024, *((unsigned long *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 1024, *((unsigned long *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 1024, *((unsigned long *)table.data));
+}
+
+static void sysctl_test_api_doulongvec_write_with_min(
+ struct kunit *test)
+{
+ unsigned long data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(unsigned long),
+ .mode = 0644 | SYSCTL_FLAG_MIN,
+ .proc_handler = proc_doulongvec_minmax,
+ .min = SYSCTL_NUM_ONE_THOUSAND,
+ };
+ size_t max_len = 64, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "1000";
+ char input2[] = "10";
+ char input3[64] = {0};
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 1000, *((unsigned long *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 1000, *((unsigned long *)table.data));
+
+ // test for input3
+ snprintf(input3, sizeof(input3), "%lu", ULONG_MAX);
+ len = strlen(input3);
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, strlen(input3), len);
+ KUNIT_EXPECT_EQ(test, ULONG_MAX, *((unsigned long *)table.data));
+}
+
+static void sysctl_test_api_doulongvec_write_with_max(
+ struct kunit *test)
+{
+ unsigned long data = 0;
+ struct ctl_table table = {
+ .procname = "foo",
+ .data = &data,
+ .maxlen = sizeof(unsigned long),
+ .mode = 0644 | SYSCTL_FLAG_MAX,
+ .proc_handler = proc_doulongvec_minmax,
+ .max = SYSCTL_NUM_THREE_THOUSAND,
+ };
+ size_t max_len = 64, len = max_len;
+ loff_t pos = 0;
+ char *buffer = kunit_kzalloc(test, max_len, GFP_USER);
+ char __user *user_buffer = (char __user *)buffer;
+ char input1[] = "1024";
+ char input2[] = "4096";
+ char input3[] = "0";
+
+ // test for input1
+ len = sizeof(input1) - 1;
+ memcpy(buffer, input1, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input1) - 1, len);
+ KUNIT_EXPECT_EQ(test, 1024, *((unsigned long *)table.data));
+
+ // test for input2
+ len = sizeof(input2) - 1;
+ pos = 0;
+ memcpy(buffer, input2, len);
+ KUNIT_EXPECT_EQ(test, -EINVAL, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, 0, pos);
+ KUNIT_EXPECT_EQ(test, 1024, *((unsigned long *)table.data));
+
+ // test for input3
+ len = sizeof(input3) - 1;
+ pos = 0;
+ memcpy(buffer, input3, len);
+ KUNIT_EXPECT_EQ(test, 0, proc_doulongvec_minmax(&table, KUNIT_PROC_WRITE,
+ user_buffer, &len, &pos));
+ KUNIT_EXPECT_EQ(test, sizeof(input3) - 1, len);
+ KUNIT_EXPECT_EQ(test, 0, *((unsigned long *)table.data));
+}
+
static struct kunit_case sysctl_test_cases[] = {
KUNIT_CASE(sysctl_test_api_dointvec_null_tbl_data),
KUNIT_CASE(sysctl_test_api_dointvec_table_maxlen_unset),
@@ -427,6 +996,18 @@ static struct kunit_case sysctl_test_cases[] = {
KUNIT_CASE(sysctl_test_api_dointvec_write_single_less_int_min),
KUNIT_CASE(sysctl_test_api_dointvec_write_single_greater_int_max),
KUNIT_CASE(sysctl_test_register_sysctl_sz_invalid_extra_value),
+ KUNIT_CASE(sysctl_test_api_dointvec_write_with_minmax),
+ KUNIT_CASE(sysctl_test_api_dointvec_write_with_min),
+ KUNIT_CASE(sysctl_test_api_dointvec_write_with_max),
+ KUNIT_CASE(sysctl_test_api_douintvec_write_with_minmax),
+ KUNIT_CASE(sysctl_test_api_douintvec_write_with_min),
+ KUNIT_CASE(sysctl_test_api_douintvec_write_with_max),
+ KUNIT_CASE(sysctl_test_api_dou8vec_write_with_minmax),
+ KUNIT_CASE(sysctl_test_api_dou8vec_write_with_min),
+ KUNIT_CASE(sysctl_test_api_dou8vec_write_with_max),
+ KUNIT_CASE(sysctl_test_api_doulongvec_write_with_minmax),
+ KUNIT_CASE(sysctl_test_api_doulongvec_write_with_min),
+ KUNIT_CASE(sysctl_test_api_doulongvec_write_with_max),
{}
};
--
2.25.1