Skip to contentSkip to conversation

sysctl: fix proc_dobool() usability

PATCH Applied Ondrej Mosnacek 5 participants 9 messages Feb 10, 2023 – Mar 6, 2023

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sysctl: fix proc_dobool() usability

Currently proc_dobool expects a (bool *) in table->data, but sizeof(int) in table->maxsize, because it uses do_proc_dointvec() directly.

This is unsafe for at least two reasons:
1. A sysctl table definition may use { .data = &variable, .maxsize = sizeof(variable) }, not realizing that this makes the sysctl unusable (see the Fixes: tag) and that they need to use the completely counterintuitive sizeof(int) instead.
2. proc_dobool() will currently try to parse an array of values if given .maxsize >= 2*sizeof(int), but will try to write values of type bool by offsets of sizeof(int), so it will not work correctly with neither an (int *) nor a (bool *). There is no .maxsize validation to prevent this.

Fix this by:
1. Constraining proc_dobool() to allow only one value and .maxsize == sizeof(bool).
2. Wrapping the original struct ctl_table in a temporary one with .data pointing to a local int variable and .maxsize set to sizeof(int) and passing this one to proc_dointvec(), converting the value to/from bool as needed (using proc_dou8vec_minmax() as an example).
3. Extending sysctl_check_table() to enforce proc_dobool() expectations.
4. Fixing the proc_dobool() docstring (it was just copy-pasted from proc_douintvec, apparently...).
5. Converting all existing proc_dobool() users to set .maxsize to sizeof(bool) instead of sizeof(int).

Fixes 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled")
Fixes a2071573d634 ("sysctl: introduce new proc handler proc_dobool")
---
 fs/lockd/svc.c        |  2 +-
 fs/proc/proc_sysctl.c |  6 ++++++
 kernel/sysctl.c       | 43 ++++++++++++++++++++++++-------------------
 mm/hugetlb_vmemmap.c  |  2 +-
 4 files changed, 32 insertions(+), 21 deletions(-)
fs/lockd/svc.c +1 -1
index 59ef8a1f843f3..914ea1c3537d1 100644
@@ -496,7 +496,7 @@ static struct ctl_table nlm_sysctls[] = {
 	{
 		.procname	= "nsm_use_hostnames",
 		.data		= &nsm_use_hostnames,
-		.maxlen		= sizeof(int),
+		.maxlen		= sizeof(bool),
 		.mode		= 0644,
 		.proc_handler	= proc_dobool,
 	},
fs/proc/proc_sysctl.c +6 -0
index 48f2d60bd78a2..436025e0f77a6 100644
@@ -1124,6 +1124,11 @@ static int sysctl_check_table_array(const char *path, struct ctl_table *table)
 			err |= sysctl_err(path, table, "array not allowed");
 	}
 
+	if (table->proc_handler == proc_dobool) {
+		if (table->maxlen != sizeof(bool))
+			err |= sysctl_err(path, table, "array not allowed");
+	}
+
 	return err;
 }
 
@@ -1136,6 +1141,7 @@ static int sysctl_check_table(const char *path, struct ctl_table *table)
 			err |= sysctl_err(path, entry, "Not a file");
 
 		if ((entry->proc_handler == proc_dostring) ||
+		    (entry->proc_handler == proc_dobool) ||
 		    (entry->proc_handler == proc_dointvec) ||
 		    (entry->proc_handler == proc_douintvec) ||
 		    (entry->proc_handler == proc_douintvec_minmax) ||
kernel/sysctl.c +24 -19

61 lines not on this page · raw message

mm/hugetlb_vmemmap.c +1 -1
index 45e93a545dd7e..a559037cce00c 100644
@@ -581,7 +581,7 @@ static struct ctl_table hugetlb_vmemmap_sysctls[] = {
 	{
 		.procname	= "hugetlb_optimize_vmemmap",
 		.data		= &vmemmap_optimize_enabled,
-		.maxlen		= sizeof(int),
+		.maxlen		= sizeof(vmemmap_optimize_enabled),
 		.mode		= 0644,
 		.proc_handler	= proc_dobool,
 	},
Signature · 1 line
2.39.1
#1 lore raw
Quoted 30 lines · Ondrej Mosnacek

Currently proc_dobool expects a (bool *) in table->data, but sizeof(int) in table->maxsize, because it uses do_proc_dointvec() directly.

This is unsafe for at least two reasons:
1. A sysctl table definition may use { .data = &variable, .maxsize = sizeof(variable) }, not realizing that this makes the sysctl unusable (see the Fixes: tag) and that they need to use the completely counterintuitive sizeof(int) instead.
2. proc_dobool() will currently try to parse an array of values if given .maxsize >= 2*sizeof(int), but will try to write values of type bool by offsets of sizeof(int), so it will not work correctly with neither an (int *) nor a (bool *). There is no .maxsize validation to prevent this.

Fix this by:
1. Constraining proc_dobool() to allow only one value and .maxsize == sizeof(bool).
2. Wrapping the original struct ctl_table in a temporary one with .data pointing to a local int variable and .maxsize set to sizeof(int) and passing this one to proc_dointvec(), converting the value to/from bool as needed (using proc_dou8vec_minmax() as an example).
3. Extending sysctl_check_table() to enforce proc_dobool() expectations.
4. Fixing the proc_dobool() docstring (it was just copy-pasted from proc_douintvec, apparently...).
5. Converting all existing proc_dobool() users to set .maxsize to sizeof(bool) instead of sizeof(int).

Fixes 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled")
Fixes a2071573d634 ("sysctl: introduce new proc handler proc_dobool")
Quoted 6 lines
---
 fs/lockd/svc.c        |  2 +-
 fs/proc/proc_sysctl.c |  6 ++++++
 kernel/sysctl.c       | 43 ++++++++++++++++++++++++-------------------
 mm/hugetlb_vmemmap.c  |  2 +-
 4 files changed, 32 insertions(+), 21 deletions(-)

Gentle ping... Without this patch the new "dev.tty.legacy_tiocsti" sysctl is unusable. This is blocking me from making selinux-testsuite work with CONFIG_LEGACY_TIOCSTI=n:
https://lore.kernel.org/selinux/CAHC9VhQwrjwdW27+ktcT_9q-N7AmuUK8GYgoYbPXGVAcjwA4nQ@mail.gmail.com/T/

Signature · 3 lines
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.
Acked-by on v1 1/1
#2 lore raw
In reply to Ondrej Mosnacek
Quoted 27 lines · Ondrej Mosnacek

Currently proc_dobool expects a (bool *) in table->data, but sizeof(int) in table->maxsize, because it uses do_proc_dointvec() directly.

This is unsafe for at least two reasons:
1. A sysctl table definition may use { .data = &variable, .maxsize = sizeof(variable) }, not realizing that this makes the sysctl unusable (see the Fixes: tag) and that they need to use the completely counterintuitive sizeof(int) instead.
2. proc_dobool() will currently try to parse an array of values if given .maxsize >= 2*sizeof(int), but will try to write values of type bool by offsets of sizeof(int), so it will not work correctly with neither an (int *) nor a (bool *). There is no .maxsize validation to prevent this.

Fix this by:
1. Constraining proc_dobool() to allow only one value and .maxsize == sizeof(bool).
2. Wrapping the original struct ctl_table in a temporary one with .data pointing to a local int variable and .maxsize set to sizeof(int) and passing this one to proc_dointvec(), converting the value to/from bool as needed (using proc_dou8vec_minmax() as an example).
3. Extending sysctl_check_table() to enforce proc_dobool() expectations.
4. Fixing the proc_dobool() docstring (it was just copy-pasted from proc_douintvec, apparently...).
5. Converting all existing proc_dobool() users to set .maxsize to sizeof(bool) instead of sizeof(int).

Quoted 3 lines
Fixes 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled")
Fixes a2071573d634 ("sysctl: introduce new proc handler proc_dobool")

Ah nice, thanks for tracking this down.

I've long wanted to replace all the open-coded sysctl initializers with a helper macro so it's hard to make mistakes. e.g.

#define _SYSCTL_HANDLER(var)					\
		_Generic((var),					\
			default:	proc_dointvec_minmax,	\
			unsigned int:	proc_douintvec_minmax,	\
			unsigned long:	proc_doulongvec_minmax,	\
			char:		proc_dou8vec_minmax,	\
			bool:		proc_dobool,		\
			char *:		proc_dostring)
#define _SYSCTL_MINVAL(var)				\
		_Generic((var),				\
			default:	0,		\
			int:		INT_MIN,	\
			unsigned int:	UINT_MIN,	\
			unsigned long:	ULONG_MIN,	\
			char:		U8_MIN,		\
			bool:		0)
#define _SYSCTL_MAXVAL(var)				\
		_Generic((var),				\
			default:	0,		\
			int:		INT_MAX,	\
			unsigned int:	UINT_MAX,	\
			unsigned long:	ULONG_MAX,	\
			char:		U8_MAX,		\
			bool:		1)
#define _SYSCTL_VAR(_name, _var, _mode, _handler, _len, _min, _max)	\
	{								\
		.procname	= _name,				\
		.data		= &(_var),				\
		.maxlen		= _len,					\
		.mode		= _mode,				\
		.proc_handler	= _handler,				\
		.minlen		= _min,					\
		.maxlen		= _min,					\
	}

/* single value */

#define SYSCTL_VAL(var)							\
	_SYSCTL_VAR(#var, var, 0644, _SYSCTL_HANDLER(var), sizeof(var),	\
		    _SYSCTL_MINVAL(var), _SYSCTL_MAXVAL(var))

/* single value with min/max */

#define SYSCTL_VAL_MINMAX(_var, _min, _max)				\
	_SYSCTL_VAR(#var, var, 0644, _SYSCTL_HANDLER(var), sizeof(var),	\
		    _min, _max)

/* Strings... */

#define SYSCTL_STR(_var, _len)						\
	_SYSCTL_VAR(#var, var, 0644, _SYSCTL_HANDLER(var), _len, 0, 0)

/* Arrays... */

#define SYSCTL_ARRAY(_var)						\
	_SYSCTL_VAR(#var, var, 0644, _SYSCTL_HANDLER(var),		\
		    ARRAY_SIZE(_var), _SYSCTL_MINVAL(var),		\
		    _SYSCTL_MAXVAL(var)

Totally untested. I think this would cover the vast majority of sysctl initializers.

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 28 lines · Kees Cook
Quoted 27 lines · Ondrej Mosnacek

Currently proc_dobool expects a (bool *) in table->data, but sizeof(int) in table->maxsize, because it uses do_proc_dointvec() directly.

This is unsafe for at least two reasons:
1. A sysctl table definition may use { .data = &variable, .maxsize = sizeof(variable) }, not realizing that this makes the sysctl unusable (see the Fixes: tag) and that they need to use the completely counterintuitive sizeof(int) instead.
2. proc_dobool() will currently try to parse an array of values if given .maxsize >= 2*sizeof(int), but will try to write values of type bool by offsets of sizeof(int), so it will not work correctly with neither an (int *) nor a (bool *). There is no .maxsize validation to prevent this.

Fix this by:
1. Constraining proc_dobool() to allow only one value and .maxsize == sizeof(bool).
2. Wrapping the original struct ctl_table in a temporary one with .data pointing to a local int variable and .maxsize set to sizeof(int) and passing this one to proc_dointvec(), converting the value to/from bool as needed (using proc_dou8vec_minmax() as an example).
3. Extending sysctl_check_table() to enforce proc_dobool() expectations.
4. Fixing the proc_dobool() docstring (it was just copy-pasted from proc_douintvec, apparently...).
5. Converting all existing proc_dobool() users to set .maxsize to sizeof(bool) instead of sizeof(int).

Quoted 7 lines
Quoted 3 lines
Fixes 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled")
Fixes a2071573d634 ("sysctl: introduce new proc handler proc_dobool")

Ah nice, thanks for tracking this down.

Queued onto sysctl-next, will send to Linus as this is a fix too.

Luis

#4 lore raw
Quoted 33 lines · Luis Chamberlain
Quoted 32 lines · Kees Cook
Quoted 30 lines · Ondrej Mosnacek

Currently proc_dobool expects a (bool *) in table->data, but sizeof(int) in table->maxsize, because it uses do_proc_dointvec() directly.

This is unsafe for at least two reasons:
1. A sysctl table definition may use { .data = &variable, .maxsize = sizeof(variable) }, not realizing that this makes the sysctl unusable (see the Fixes: tag) and that they need to use the completely counterintuitive sizeof(int) instead.
2. proc_dobool() will currently try to parse an array of values if given .maxsize >= 2*sizeof(int), but will try to write values of type bool by offsets of sizeof(int), so it will not work correctly with neither an (int *) nor a (bool *). There is no .maxsize validation to prevent this.

Fix this by:
1. Constraining proc_dobool() to allow only one value and .maxsize == sizeof(bool).
2. Wrapping the original struct ctl_table in a temporary one with .data pointing to a local int variable and .maxsize set to sizeof(int) and passing this one to proc_dointvec(), converting the value to/from bool as needed (using proc_dou8vec_minmax() as an example).
3. Extending sysctl_check_table() to enforce proc_dobool() expectations.
4. Fixing the proc_dobool() docstring (it was just copy-pasted from proc_douintvec, apparently...).
5. Converting all existing proc_dobool() users to set .maxsize to sizeof(bool) instead of sizeof(int).

Fixes 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled")
Fixes a2071573d634 ("sysctl: introduce new proc handler proc_dobool")
Quoted 5 lines
Quoted 3 lines

Ah nice, thanks for tracking this down.

Queued onto sysctl-next, will send to Linus as this is a fix too.

Thanks, Luis!

Signature · 3 lines
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.
#5 lore raw

Request to backport "sysctl: fix proc_dobool() usability" to stable kernels

In reply to Ondrej Mosnacek

Hi -stable team,

please backport the commit f1aa2eb5ea05 ("sysctl: fix proc_dobool() usability") to the stable kernels containing commit 83efeeeb3d04 ("tty: Allow TIOCSTI to be disabled"). (Which seems only to be the 6.2 branch only at the moment)

Without this backport the sysctl dev.net.legacy_tiocsti to enable ioctl(TIOCSTI) is not functional. So on kernels that don't enable CONFIG_LEGACY_TIOCSTI, ioctl(TIOCSTI) is not usable at all.

This ioctl is used for the copy-and-paste functionality of the screenreader "fenrir".
( https://github.com/chrys87/fenrir )

#7 lore raw

Re: Request to backport "sysctl: fix proc_dobool() usability" to stable kernels

Quoted 5 lines · Storm Dragon
Quoted 4 lines · Thomas Weißschuh

This ioctl is used for the copy-and-paste functionality of the screenreader "fenrir".
( https://github.com/chrys87/fenrir )

Quoted 7 lines
Quoted 2 lines

I believe this will also cause some loss of functionality in brltty as well:

https://brltty.app

The documentation of brltty indicates that they only use TIOCSTI as fallback. By default a virtual keyboard device is used to simulate typing.

Maybe it would also make sense to open a ticket to ArchLinux to enable CONFIG_LEGACY_TIOCSTI again, as per the kernel default.

In accordance with the options help text:

"Say 'Y here only if you have confirmed that yout system's userspace depends on this functionality to continue operating normally"

Could you create such a ticket if think it's necessary?

#8 lore raw

Re: Request to backport "sysctl: fix proc_dobool() usability" to stable kernels

Quoted 9 lines · Thomas Weißschuh

Maybe it would also make sense to open a ticket to ArchLinux to enable CONFIG_LEGACY_TIOCSTI again, as per the kernel default.

In accordance with the options help text:

"Say 'Y here only if you have confirmed that yout system's userspace depends on this functionality to continue operating normally"

Could you create such a ticket if think it's necessary?

The ticket has been created. The link is:

https://bugs.archlinux.org/task/77745

Thanks,
Storm