From nobody Fri Nov 29 23:47:25 2024 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BDCDF9DA for ; Sun, 15 Sep 2024 02:10:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726366211; cv=none; b=qAbhdladeIWPnH9QTkAqloGuP8B9+OKnj6+I+mXqdVFUlyyRr+aWpyyw9wNYTqunVAJsLCNZlN7e0cHwqQATugTLnWIWa2+Wv6B8eD/Ld36bebGuxcUVX22OYC0DMtZfS/A+8uLYwUq5NL6g547i+K71dktPcNhil23uhqpUI3o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726366211; c=relaxed/simple; bh=mx8S/PMGmEyP25ULKHhQjaUHnwckKKHa6YPwW0XZ1FU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eY0q9zExAiIbKrvtkgWBRIi5M245jcV4CtR0BIPU1g1JgNxFx148o/ExoS17fndLSiDiro6YUp1uHopVkZlyS4kUiyC7YjBmgy9J5GCj779eRLB18CvlMfkrpz491qZFiA/dL2MdE40s+BpU1kxngOrMwfTV7d2JqrVoroN59Bs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=v2UUO4tU; arc=none smtp.client-ip=95.215.58.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="v2UUO4tU" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1726366207; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eKhBMD9KZww0WhZ8G5CO8zfEECOwnyicqnrjfVKl/jc=; b=v2UUO4tU4QeFhBPBxYZUi63Uure6JD/4HAjTmli8gAYygw32sYnTkO9S6g3rhB4GG8S8mW eLMeNb4/pry4YJF2i9uRL8mLgDPsucmDchTS/dvPWhnchrhgAhcwx3RY9tX5Ywdb0x9c3E zIfKwYST5hFr+rpLjCpW7eGYPQ2DVgI= From: Wen Yang To: "Eric W . Biederman" , Luis Chamberlain , Kees Cook , Joel Granados Cc: Christian Brauner , linux-kernel@vger.kernel.org, Wen Yang , Dave Young Subject: [PATCH v3 3/5] sysctl: add KUnit test code to check for encoding min/max in table entries Date: Sun, 15 Sep 2024 10:08:29 +0800 Message-Id: <988454b2c7705283811c0bd70bfbf80052860479.1726365007.git.wen.yang@linux.dev> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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 Cc: Luis Chamberlain Cc: Kees Cook Cc: Joel Granados Cc: Eric W. Biederman Cc: Christian Brauner Cc: Dave Young Cc: linux-kernel@vger.kernel.org --- 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_ex= tra_value( KUNIT_EXPECT_NOT_NULL(test, register_sysctl("foo", table_qux)); } =20 +static void sysctl_test_api_dointvec_write_with_minmax( + struct kunit *test) +{ + int data =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(int), + .mode =3D 0644 | SYSCTL_FLAG_MIN | SYSCTL_FLAG_MAX, + .proc_handler =3D proc_dointvec_minmax, + .min =3D SYSCTL_NUM_NEG_ONE, + .max =3D SYSCTL_NUM_ONE_HUNDRED, + }; + size_t max_len =3D 32, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "10"; + char input2[] =3D "-5"; + char input3[] =3D "200"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 0; + memcpy(buffer, input2, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec_minmax(&table, KUNIT_PROC_WR= ITE, + user_buffer, &len, &pos)); + KUNIT_EXPECT_EQ(test, 0, pos); + KUNIT_EXPECT_EQ(test, 10, *((int *)table.data)); + + // test for input3 + len =3D sizeof(input3) - 1; + pos =3D 0; + memcpy(buffer, input3, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec_minmax(&table, KUNIT_PROC_WR= ITE, + 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(int), + .mode =3D 0644 | SYSCTL_FLAG_MIN, + .proc_handler =3D proc_dointvec_minmax, + .min =3D SYSCTL_NUM_NEG_ONE, + }; + size_t max_len =3D 32, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "10"; + char input2[] =3D "-5"; + char input3[] =3D "2147483647"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 0; + memcpy(buffer, input2, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec_minmax(&table, KUNIT_PROC_WR= ITE, + user_buffer, &len, &pos)); + KUNIT_EXPECT_EQ(test, 0, pos); + KUNIT_EXPECT_EQ(test, 10, *((int *)table.data)); + + // test for input3 + len =3D sizeof(input3) - 1; + pos =3D 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(int), + .mode =3D 0644 | SYSCTL_FLAG_MAX, + .proc_handler =3D proc_dointvec_minmax, + .max =3D SYSCTL_NUM_ONE_HUNDRED, + }; + size_t max_len =3D 32, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "10"; + char input2[] =3D "2147483647"; + char input3[] =3D "-2147483648"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 0; + memcpy(buffer, input2, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_dointvec_minmax(&table, KUNIT_PROC_WR= ITE, + user_buffer, &len, &pos)); + KUNIT_EXPECT_EQ(test, 0, pos); + KUNIT_EXPECT_EQ(test, 10, *((int *)table.data)); + + // test for input3 + len =3D sizeof(input3) - 1; + pos =3D 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(int), + .mode =3D 0644 | SYSCTL_FLAG_MIN | SYSCTL_FLAG_MAX, + .proc_handler =3D proc_douintvec_minmax, + .min =3D SYSCTL_NUM_FOUR, + .max =3D SYSCTL_NUM_TWO_HUNDRED, + }; + size_t max_len =3D 32, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "100"; + char input2[] =3D "3"; + char input3[] =3D "1000"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 0; + memcpy(buffer, input2, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_douintvec_minmax(&table, KUNIT_PROC_W= RITE, + user_buffer, &len, &pos)); + KUNIT_EXPECT_EQ(test, 0, pos); + KUNIT_EXPECT_EQ(test, 100, *((unsigned int *)table.data)); + + // test for input3 + len =3D sizeof(input3) - 1; + pos =3D 0; + memcpy(buffer, input3, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_douintvec_minmax(&table, KUNIT_PROC_W= RITE, + 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(int), + .mode =3D 0644 | SYSCTL_FLAG_MIN, + .proc_handler =3D proc_douintvec_minmax, + .min =3D SYSCTL_NUM_FOUR, + }; + size_t max_len =3D 32, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "100"; + char input2[] =3D "3"; + char input3[] =3D "4294967295"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 0; + memcpy(buffer, input2, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_douintvec_minmax(&table, KUNIT_PROC_W= RITE, + user_buffer, &len, &pos)); + KUNIT_EXPECT_EQ(test, 0, pos); + KUNIT_EXPECT_EQ(test, 100, *((unsigned int *)table.data)); + + // test for input3 + len =3D sizeof(input3) - 1; + pos =3D 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(int), + .mode =3D 0644 | SYSCTL_FLAG_MAX, + .proc_handler =3D proc_douintvec_minmax, + .max =3D SYSCTL_NUM_ONE_THOUSAND, + }; + size_t max_len =3D 32, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "900"; + char input2[] =3D "10000"; + char input3[] =3D "0"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 0; + memcpy(buffer, input2, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_douintvec_minmax(&table, KUNIT_PROC_W= RITE, + user_buffer, &len, &pos)); + KUNIT_EXPECT_EQ(test, 0, pos); + KUNIT_EXPECT_EQ(test, 900, *((unsigned int *)table.data)); + + // test for input3 + len =3D sizeof(input3) - 1; + pos =3D 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(unsigned char), + .mode =3D 0644 | SYSCTL_FLAG_MIN | SYSCTL_FLAG_MAX, + .proc_handler =3D proc_dou8vec_minmax, + .min =3D SYSCTL_NUM_THREE, + .max =3D SYSCTL_NUM_ONE_HUNDRED, + }; + size_t max_len =3D 8, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "32"; + char input2[] =3D "1"; + char input3[] =3D "200"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 0; + memcpy(buffer, input2, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_dou8vec_minmax(&table, KUNIT_PROC_WRI= TE, + user_buffer, &len, &pos)); + KUNIT_EXPECT_EQ(test, 0, pos); + KUNIT_EXPECT_EQ(test, 32, *((unsigned char *)table.data)); + + // test for input3 + len =3D sizeof(input3) - 1; + pos =3D 0; + memcpy(buffer, input3, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_dou8vec_minmax(&table, KUNIT_PROC_WRI= TE, + 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(unsigned char), + .mode =3D 0644 | SYSCTL_FLAG_MIN, + .proc_handler =3D proc_dou8vec_minmax, + .min =3D SYSCTL_NUM_THREE, + }; + size_t max_len =3D 8, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "32"; + char input2[] =3D "1"; + char input3[] =3D "255"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 0; + memcpy(buffer, input2, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_dou8vec_minmax(&table, KUNIT_PROC_WRI= TE, + user_buffer, &len, &pos)); + KUNIT_EXPECT_EQ(test, 0, pos); + KUNIT_EXPECT_EQ(test, 32, *((unsigned char *)table.data)); + + // test for input3 + len =3D sizeof(input3) - 1; + pos =3D 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(unsigned char), + .mode =3D 0644 | SYSCTL_FLAG_MAX, + .proc_handler =3D proc_dou8vec_minmax, + .max =3D SYSCTL_NUM_TWO_HUNDRED, + }; + size_t max_len =3D 8, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "32"; + char input2[] =3D "0"; + char input3[] =3D "255"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 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 =3D sizeof(input3) - 1; + pos =3D 0; + memcpy(buffer, input3, len); + KUNIT_EXPECT_EQ(test, -EINVAL, proc_dou8vec_minmax(&table, KUNIT_PROC_WRI= TE, + 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(unsigned long), + .mode =3D 0644 | SYSCTL_FLAG_MIN | SYSCTL_FLAG_MAX, + .proc_handler =3D proc_doulongvec_minmax, + .min =3D SYSCTL_NUM_ONE_THOUSAND, + .max =3D SYSCTL_NUM_THREE_THOUSAND, + }; + size_t max_len =3D 64, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "1024"; + char input2[] =3D "100"; + char input3[] =3D "4096"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 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 =3D sizeof(input3) - 1; + pos =3D 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(unsigned long), + .mode =3D 0644 | SYSCTL_FLAG_MIN, + .proc_handler =3D proc_doulongvec_minmax, + .min =3D SYSCTL_NUM_ONE_THOUSAND, + }; + size_t max_len =3D 64, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "1000"; + char input2[] =3D "10"; + char input3[64] =3D {0}; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 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 =3D strlen(input3); + pos =3D 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 =3D 0; + struct ctl_table table =3D { + .procname =3D "foo", + .data =3D &data, + .maxlen =3D sizeof(unsigned long), + .mode =3D 0644 | SYSCTL_FLAG_MAX, + .proc_handler =3D proc_doulongvec_minmax, + .max =3D SYSCTL_NUM_THREE_THOUSAND, + }; + size_t max_len =3D 64, len =3D max_len; + loff_t pos =3D 0; + char *buffer =3D kunit_kzalloc(test, max_len, GFP_USER); + char __user *user_buffer =3D (char __user *)buffer; + char input1[] =3D "1024"; + char input2[] =3D "4096"; + char input3[] =3D "0"; + + // test for input1 + len =3D 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 =3D sizeof(input2) - 1; + pos =3D 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 =3D sizeof(input3) - 1; + pos =3D 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[] =3D { 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[] =3D { 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), {} }; =20 --=20 2.25.1