All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sysctl: handle table->maxlen properly for proc_dobool
@ 2022-05-19 12:55 Muchun Song
  2022-05-19 13:09 ` Matthew Wilcox
  0 siblings, 1 reply; 3+ messages in thread
From: Muchun Song @ 2022-05-19 12:55 UTC (permalink / raw)
  To: linux-kernel, linux-fsdevel
  Cc: duanxiongchun, smuchun, Muchun Song, Luis Chamberlain, Kees Cook,
	Iurii Zaikin

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(-)

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] sysctl: handle table->maxlen properly for proc_dobool
  2022-05-19 12:55 [PATCH] sysctl: handle table->maxlen properly for proc_dobool Muchun Song
@ 2022-05-19 13:09 ` Matthew Wilcox
  2022-05-19 14:10   ` Muchun Song
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Wilcox @ 2022-05-19 13:09 UTC (permalink / raw)
  To: Muchun Song
  Cc: linux-kernel, linux-fsdevel, duanxiongchun, smuchun,
	Luis Chamberlain, Kees Cook, Iurii Zaikin

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)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] sysctl: handle table->maxlen properly for proc_dobool
  2022-05-19 13:09 ` Matthew Wilcox
@ 2022-05-19 14:10   ` Muchun Song
  0 siblings, 0 replies; 3+ messages in thread
From: Muchun Song @ 2022-05-19 14:10 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: LKML, linux-fsdevel, Xiongchun duan, Muchun Song,
	Luis Chamberlain, Kees Cook, Iurii Zaikin

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-05-19 14:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-19 12:55 [PATCH] sysctl: handle table->maxlen properly for proc_dobool Muchun Song
2022-05-19 13:09 ` Matthew Wilcox
2022-05-19 14:10   ` Muchun Song

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.