From patchwork Mon May 16 10:22:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12850551 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 273DFC433F5 for ; Mon, 16 May 2022 10:23:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B91108D0001; Mon, 16 May 2022 06:23:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B3FEB6B007E; Mon, 16 May 2022 06:23:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E0798D0001; Mon, 16 May 2022 06:23:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8F8E76B007D for ; Mon, 16 May 2022 06:23:39 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6B02E1319 for ; Mon, 16 May 2022 10:23:39 +0000 (UTC) X-FDA: 79471219758.25.4C99329 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf09.hostedemail.com (Postfix) with ESMTP id 432C91400B8 for ; Mon, 16 May 2022 10:23:29 +0000 (UTC) Received: by mail-pl1-f175.google.com with SMTP id i1so13989663plg.7 for ; Mon, 16 May 2022 03:23:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=66QHju611WH53dYegSIeTLMF0HCCMVlkCTvmtXu5zP0=; b=dbOdnoBhv8yDK+GYyHYeN5G+d7/MHd9XVwc4tEPW7W3O+cZRT3Wv7x+znTXvNtO+f3 6bEsAy40OaeqAaep5aSPNUgrcDvjiV7Mbt0ptchb8A3pYElKJt1bNlTdgx7zFZ4MxyHX 0B5xxhVpkjVQjpaCkqHkLxuE/ntC47wRcsxOZZAg+3yzJVnK0NBoVC5kQbXHy99msudO Oe6PF6U9kalMK2v1ZjOxyE9Jl0fXH67dfu4LtcJmdbOW8f8piy081NEqSkdF/H+wgM4B qYkLF9/8nR7Xdej7zfxtkgHwip0sZnt3tx+uk8zKb9pebOPiv1hSqyGFpMK/ACJwdU4+ eKoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=66QHju611WH53dYegSIeTLMF0HCCMVlkCTvmtXu5zP0=; b=dNddc1XxhW8ezhLbC6DogwDs8iMtm09CZD7upBlT7l5WMm++TT9eqiaYqTPAlbBPAJ ZKkJ9Q38XyXfdjfnvmoufFhLRz85hNbu5x+8Z07jO3PXPD2sYumt5zhWxlIFBfQ4lHtL tYs0PTh6EWfwuAKfcRUnFJgmV+oPQkkwPRy14Ly/6CBFmfYn4llPcQhyygqbV8zRCrUf 3igt6dM5OvwI4nIDH5xaTEfU9M4LSn0mgoXfPeWejx1Xj9d8t4eJepaRwtFpTPRbaB/l 1YF1s7REqrj9pZSo/t4PLbEfNfuZ3l2qdd/Bzbtg0naeM64m1USWIXALMpuxdTY8e9Lx vuDA== X-Gm-Message-State: AOAM532v9rcYiaMQfyADfOqLkXZhP2NdeQR7gmtPe6FE5JGkEXhDBdF9 zygRrjU0fZzsE7LxRPGdHdD8WQ== X-Google-Smtp-Source: ABdhPJx7T73yVTTd72pWQdg0tme4uuKLleJpMJUhliphuOjg/2KAtHyrnRxxoAQV31EyZlOMr2VLWg== X-Received: by 2002:a17:902:ab8c:b0:15e:fd9f:3f39 with SMTP id f12-20020a170902ab8c00b0015efd9f3f39mr16805245plr.103.1652696618000; Mon, 16 May 2022 03:23:38 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.234]) by smtp.gmail.com with ESMTPSA id i9-20020aa79089000000b0050dc76281e4sm6472731pfa.190.2022.05.16.03.23.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 03:23:37 -0700 (PDT) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, akpm@linux-foundation.org, mcgrof@kernel.org, keescook@chromium.org, yzaikin@google.com, osalvador@suse.de, david@redhat.com, masahiroy@kernel.org Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v12 6/7] sysctl: handle table->maxlen properly for proc_dobool Date: Mon, 16 May 2022 18:22:10 +0800 Message-Id: <20220516102211.41557-7-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.1 (Apple Git-133) In-Reply-To: <20220516102211.41557-1-songmuchun@bytedance.com> References: <20220516102211.41557-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Stat-Signature: egjz44xzzk3rf7nzwd4nxb375obr6b4s X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 432C91400B8 X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=dbOdnoBh; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf09.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-HE-Tag: 1652696609-774496 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Setting ->proc_handler to proc_dobool at the same time setting ->maxlen to sizeof(int) is counter-intuitive, it is easy to make mistakes. For robustness, fix it by handling able->maxlen properly for proc_dobool in __do_proc_dointvec(). In the next patch, we will use proc_dobool which depends on this change. Signed-off-by: Muchun Song Cc: Luis Chamberlain Cc: Kees Cook Cc: Iurii Zaikin --- fs/lockd/svc.c | 2 +- kernel/sysctl.c | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/fs/lockd/svc.c b/fs/lockd/svc.c index 59ef8a1f843f..6e48ee787f49 100644 --- a/fs/lockd/svc.c +++ b/fs/lockd/svc.c @@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = { { .procname = "nsm_use_hostnames", .data = &nsm_use_hostnames, - .maxlen = sizeof(int), + .maxlen = sizeof(nsm_use_hostnames), .mode = 0644, .proc_handler = proc_dobool, }, diff --git a/kernel/sysctl.c b/kernel/sysctl.c index e52b6e372c60..353fb9093012 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -428,6 +428,8 @@ static int do_proc_dobool_conv(bool *negp, unsigned long *lvalp, int write, void *data) { if (write) { + if (*negp || (*lvalp != 0 && *lvalp != 1)) + return -EINVAL; *(bool *)valp = *lvalp; } else { int val = *(bool *)valp; @@ -489,17 +491,17 @@ static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table, int write, void *data), void *data) { - int *i, vleft, first = 1, err = 0; + int vleft, first = 1, err = 0, size; size_t left; char *p; - + if (!tbl_data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } - - i = (int *) tbl_data; - vleft = table->maxlen / sizeof(*i); + + size = conv == do_proc_dobool_conv ? sizeof(bool) : sizeof(int); + vleft = table->maxlen / size; left = *lenp; if (!conv) @@ -514,7 +516,7 @@ static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table, p = buffer; } - for (; left && vleft--; i++, first=0) { + for (; left && vleft--; tbl_data = (char *)tbl_data + size, first=0) { unsigned long lval; bool neg; @@ -528,12 +530,12 @@ static int __do_proc_dointvec(void *tbl_data, struct ctl_table *table, sizeof(proc_wspace_sep), NULL); if (err) break; - if (conv(&neg, &lval, i, 1, data)) { + if (conv(&neg, &lval, tbl_data, 1, data)) { err = -EINVAL; break; } } else { - if (conv(&neg, &lval, i, 0, data)) { + if (conv(&neg, &lval, tbl_data, 0, data)) { err = -EINVAL; break; } @@ -708,8 +710,8 @@ int do_proc_douintvec(struct ctl_table *table, int write, * @lenp: the size of the user buffer * @ppos: file position * - * Reads/writes up to table->maxlen/sizeof(unsigned int) integer - * values from/to the user buffer, treated as an ASCII string. + * Reads/writes up to table->maxlen/sizeof(bool) bool values from/to + * the user buffer, treated as an ASCII string. * * Returns 0 on success. */