diff mbox series

sysctl: handle table->maxlen properly for proc_dobool

Message ID 20220519125505.92400-1-songmuchun@bytedance.com (mailing list archive)
State New, archived
Headers show
Series sysctl: handle table->maxlen properly for proc_dobool | expand

Commit Message

Muchun Song May 19, 2022, 12:55 p.m. UTC
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().

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Cc: Luis Chamberlain <mcgrof@kernel.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Iurii Zaikin <yzaikin@google.com>
---
 fs/lockd/svc.c  |  2 +-
 kernel/sysctl.c | 22 ++++++++++++----------
 2 files changed, 13 insertions(+), 11 deletions(-)

Comments

Matthew Wilcox May 19, 2022, 1:09 p.m. UTC | #1
On Thu, May 19, 2022 at 08:55:05PM +0800, Muchun Song wrote:
> @@ -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;

Is this the right approach?  Or should we do as C does and interpret
writing non-zero as true?  ie:

		*(bool *)valp = (bool)*lvalp;

(is that cast needed?  It wouldn't be if it were an int, but bool is a
bit weird)
Muchun Song May 19, 2022, 2:10 p.m. UTC | #2
On Thu, May 19, 2022 at 9:09 PM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Thu, May 19, 2022 at 08:55:05PM +0800, Muchun Song wrote:
> > @@ -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;
>
> Is this the right approach?  Or should we do as C does and interpret
> writing non-zero as true?  ie:

All right. We could obey the C rule here.

>
>                 *(bool *)valp = (bool)*lvalp;
>
> (is that cast needed?  It wouldn't be if it were an int, but bool is a
> bit weird)
>

If the cast is weird. How about:

        *(bool *)valp = *lvalp ? true : false;

Thanks.
diff mbox series

Patch

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.
  */