proc: avoid integer type confusion in get_proc_long

commit e6cfaf34be9fcd1a8285a294e18986bfc41a409c upstream.

proc_get_long() is passed a size_t, but then assigns it to an 'int'
variable for the length.  Let's not do that, even if our IO paths are
limited to MAX_RW_COUNT (exactly because of these kinds of type errors).

So do the proper test in the right type.

BUG=b/261741918
TEST=presubmit
SOURCE=UPSTREAM e6cfaf34be9fcd1a8285a294e18986bfc41a409c
RELEASE_NOTE=Fixed a type error in proc_get_long. This resolves
CVE-2022-4378.

cos-patch: security-moderate
Reported-by: Kyle Zeng <zengyhkyle@gmail.com>
Change-Id: Icf3fbedf0d4a698b2a3e0607c6fdfadb14639beb
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-on: https://cos-review.googlesource.com/c/third_party/kernel/+/40009
Tested-by: Cusky Presubmit Bot <presubmit@cos-infra-prod.iam.gserviceaccount.com>
Reviewed-by: Oleksandr Tymoshenko <ovt@google.com>
Main-Branch-Verified: Cusky Presubmit Bot <presubmit@cos-infra-prod.iam.gserviceaccount.com>
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 8b3a01c..decabf5 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -2232,13 +2232,12 @@ static int proc_get_long(char **buf, size_t *size,
 			  unsigned long *val, bool *neg,
 			  const char *perm_tr, unsigned perm_tr_len, char *tr)
 {
-	int len;
 	char *p, tmp[TMPBUFLEN];
+	ssize_t len = *size;
 
-	if (!*size)
+	if (len <= 0)
 		return -EINVAL;
 
-	len = *size;
 	if (len > TMPBUFLEN - 1)
 		len = TMPBUFLEN - 1;