From nobody Wed Nov 27 18:38:15 2024 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.179]) (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 A80DF144D21 for ; Tue, 8 Oct 2024 15:15:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728400525; cv=none; b=jel+xSy0ypBeYwnyh2tYJtJQP9MElI0i/fqWp1mcwnpL9+zldoH5+ZGXB/H6QxgWR8vrtgSEtd5n4B+U06SBItgMofmbcEcD/l+nJO13Q4TX1P/T1rkZCsQlakRxi8z07t7zejr8PN29wxiNMHSZWODuaYVkuzznUi7RyYjzNV8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728400525; c=relaxed/simple; bh=mPljEaaQMRYK8Bkq85PNqm6mGWLYeUNTBictaVSpePg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OI7GXofkO7HzbE6KoVlVYwWTr/6lctjz+jKUYs8wloWs07zS/bebWpuv9vBGpBKJ4nqYjE0F4ZQzdWDraE+QHWqp1tfkIvQyJnBtapYzxiQUNhQz/kJgIo6G/QscwfwIdqRyMzd5z0Onf+BnAnSD8KidNNqjnwr6voupFU7qt88= 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=kfvllnMg; arc=none smtp.client-ip=95.215.58.179 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="kfvllnMg" 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=1728400520; 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=3eBdW7rI2XiPDOIiaUyk9TyZESzVybH1oFyeHMcIxDM=; b=kfvllnMgM+YByeGEf4iqvRhzsiZ6hYpczG9Q74hQEUwBXU7xuUSblqvAC8Qqe9mEnLTfgg 3zYtDB7HWCF6ywdyRdS8l3HLSEY/tPJzO3jldtKguluJaKMLgRkTB9BXPbYWm4UFntG8WZ OuNr3I04PI6FU186WiyX6gWN1QonPUk= From: Wen Yang To: Joel Granados , Luis Chamberlain , Kees Cook Cc: "Eric W . Biederman" , Christian Brauner , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, Wen Yang , Dave Young Subject: [RESEND PATCH v4 3/5] sysctl: add kunit test code to check the min/max encoding of sysctl table entries Date: Tue, 8 Oct 2024 23:14:06 +0800 Message-Id: <20241008151444.12453-3-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..23439ce85f2d 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 -1, + .max =3D 100, + }; + 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 -1, + }; + 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 100, + }; + 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 4, + .max =3D 200, + }; + 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 4, + }; + 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 1000, + }; + 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 3, + .max =3D 100, + }; + 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 3, + }; + 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 200, + }; + 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 1000, + .max =3D 3000, + }; + 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 1000, + }; + 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 3000, + }; + 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