Skip to contentSkip to conversation

Fixes multiple sysctl bound checks

PATCH Applied [email protected] 8 participants 29 messages Feb 24, 2025 – Mar 11, 2025

Subsystems (7)

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fixes multiple sysctl bound checks

From: Nicolas Bouchinet <[email protected]>

Hi,

This patchset adds some bound checks to sysctls to avoid negative value writes.

The patched sysctls were storing the result of the proc_dointvec proc_handler into an unsigned int data. proc_dointvec being able to parse negative value, and it return value being a signed int, this could lead to undefined behaviors.
This has led to kernel crash in the past as described in commit 3b3376f222e3 ("sysctl.c: fix underflow value setting risk in vm_table")

They are now bounded between SYSCTL_ZERO and SYSCTL_INT_MAX. The proc_handlers have thus been updated to proc_dointvec_minmax.

This patchset has been written over sysctl-testing branch [1]. See [2] for similar sysctl fixes currently in review.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctl.git/log/?h=sysctl-testing
[2]: https://lore.kernel.org/all/[email protected]/

Best regards,

Nicolas

---

Changes since v1:
https://lore.kernel.org/all/[email protected]/

* Detached patches 1/9, 2/9 [3] and 3/9 [4]
* Adapted the cover-letter message to match the reduced patchset

[3]: https://lore.kernel.org/all/[email protected]/
[4]: https://lore.kernel.org/all/[email protected]/

---

Nicolas Bouchinet (6):
sysctl: Fixes idmap_cache_timeout bounds
sysctl: Fixes nsm_local_state bounds sysctl/coda: Fixes timeout bounds
sysctl: Fixes scsi_logging_level bounds sysctl/infiniband: Fixes infiniband sysctl bounds
sysctl: Fixes max-user-freq bounds

 drivers/char/hpet.c            | 4 +++-
 drivers/infiniband/core/iwcm.c | 4 +++-
 drivers/infiniband/core/ucma.c | 4 +++-
 drivers/scsi/scsi_sysctl.c     | 4 +++-
 fs/coda/sysctl.c               | 4 +++-
 fs/lockd/svc.c                 | 4 +++-
 fs/nfs/nfs4sysctl.c            | 4 +++-
 7 files changed, 21 insertions(+), 7 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v2 1/6 sysctl: Fixes idmap_cache_timeout bounds

From: Nicolas Bouchinet <[email protected]>

Bound idmap_cache_timeout sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/nfs/nfs4sysctl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/nfs/nfs4sysctl.c +3 -1
index d1a92d8f8ba4c..c36d89afb13af 100644
@@ -32,7 +32,9 @@ static const struct ctl_table nfs4_cb_sysctls[] = {
 		.data = &nfs_idmap_cache_timeout,
 		.maxlen = sizeof(int),
 		.mode = 0644,
-		.proc_handler = proc_dointvec,
+		.proc_handler = proc_dointvec_minmax,
+		.extra1 = SYSCTL_ZERO,
+		.extra2 = SYSCTL_INT_MAX,
 	},
 };
 
Signature · 1 line
2.48.1
#2 lore raw

PATCH v2 2/6 sysctl: Fixes nsm_local_state bounds

In reply to [email protected]

From: Nicolas Bouchinet <[email protected]>

Bound nsm_local_state sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/lockd/svc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/lockd/svc.c +3 -1
index 2c8eedc6c2cc9..984ab233af8b6 100644
@@ -461,7 +461,9 @@ static const struct ctl_table nlm_sysctls[] = {
 		.data		= &nsm_local_state,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
 	},
 };
 
Signature · 1 line
2.48.1
#3 lore raw

Re: [PATCH v2 2/6] sysctl: Fixes nsm_local_state bounds

Quoted 17 lines · On 2/24/25 4:58 AM, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound nsm_local_state sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/lockd/svc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/lockd/svc.c +0 -0
index 2c8eedc6c2cc9..984ab233af8b6 100644

fs/lockd/svc.c static const struct ctl_table nlm_sysctls[] = { · line 469

Quoted 10 lines
fs/lockd/svc.c +3 -1
@@ -461,7 +461,9 @@ static const struct ctl_table nlm_sysctls[] = {
 		.data		= &nsm_local_state,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
 	},
 };
 

Hi Nicolas -

nsm_local_state is an unsigned 32-bit integer. The type of that value is defined by spec, because this value is exchanged between peers on the network.

Perhaps this patch should replace proc_dointvec with proc_douintvec instead.

Signature · 1 line
Chuck Lever
#4 lore raw

Re: [PATCH v2 2/6] sysctl: Fixes nsm_local_state bounds

Quoted 30 lines · Chuck Lever
Quoted 17 lines · On 2/24/25 4:58 AM, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound nsm_local_state sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
  fs/lockd/svc.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
fs/lockd/svc.c +0 -0
index 2c8eedc6c2cc9..984ab233af8b6 100644

fs/lockd/svc.c static const struct ctl_table nlm_sysctls[] = { · line 469

Quoted 10 lines
fs/lockd/svc.c +3 -1
@@ -461,7 +461,9 @@ static const struct ctl_table nlm_sysctls[] = {
  		.data		= &nsm_local_state,
  		.maxlen		= sizeof(int),
  		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
  	},
  };
 

Hi Nicolas -

Quoted 8 lines

nsm_local_state is an unsigned 32-bit integer. The type of that value is defined by spec, because this value is exchanged between peers on the network.

Perhaps this patch should replace proc_dointvec with proc_douintvec instead.

Hi Chuck,

Thank's for your review.

If nsm_local_state should be set to the
full range of an uint32_t by a user writing in the sysctl, then yes it should use proc_douintvec instead of limiting it to SYSCTL_INT_MAX value (INT_MAX).

I've used proc_dointvec_minmax since it already used proc_dointvec and thus was already capped at INT_MAX.

Best regards,

Nicolas

Reviewed-by
#5 lore raw

Re: [PATCH v2 2/6] sysctl: Fixes nsm_local_state bounds

In reply to Chuck Lever
Quoted 31 lines · Chuck Lever
Quoted 17 lines · On 2/24/25 4:58 AM, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound nsm_local_state sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/lockd/svc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/lockd/svc.c +0 -0
index 2c8eedc6c2cc9..984ab233af8b6 100644

fs/lockd/svc.c static const struct ctl_table nlm_sysctls[] = { · line 469

Quoted 10 lines
fs/lockd/svc.c +3 -1
@@ -461,7 +461,9 @@ static const struct ctl_table nlm_sysctls[] = {
 		.data		= &nsm_local_state,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
 	},
 };
 

Hi Nicolas -

Quoted 6 lines

nsm_local_state is an unsigned 32-bit integer. The type of that value is defined by spec, because this value is exchanged between peers on the network.

Perhaps this patch should replace proc_dointvec with proc_douintvec instead.

As Nicolas stated, that is completely up to how you used the variable.

Things to notice:
1. If you want the full range of a unsigned long, then you should stop using proc_dointvec as it will upper limit the value to INT_MAX.
2. If you want to keep using nsm_local_state as unsigned int, then please add SYSCTL_ZERO as a lower bound to avoid assigning negative values
3. Having SYSCTL_INT_MAX is not necessary as it is already capped by proc_dointvec{_minmax,}, but it is nice to have as it makes explicit what is happening.

Let me know if you take this through your trees so I can remove it from sysctl.

Best

Quoted 4 lines
Signature · 2 lines
-- 
Chuck Lever
Signature · 1 line
Joel Granados
#6 lore raw

Re: [PATCH v2 2/6] sysctl: Fixes nsm_local_state bounds

Quoted 50 lines · Joel Granados
Quoted 31 lines · Chuck Lever
Quoted 17 lines · On 2/24/25 4:58 AM, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound nsm_local_state sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/lockd/svc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/lockd/svc.c +0 -0
index 2c8eedc6c2cc9..984ab233af8b6 100644

fs/lockd/svc.c static const struct ctl_table nlm_sysctls[] = { · line 469

Quoted 10 lines
fs/lockd/svc.c +3 -1
@@ -461,7 +461,9 @@ static const struct ctl_table nlm_sysctls[] = {
 		.data		= &nsm_local_state,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
 	},
 };
 

Hi Nicolas -

Quoted 6 lines

nsm_local_state is an unsigned 32-bit integer. The type of that value is defined by spec, because this value is exchanged between peers on the network.

Perhaps this patch should replace proc_dointvec with proc_douintvec instead.

As Nicolas stated, that is completely up to how you used the variable.

Things to notice:
1. If you want the full range of a unsigned long, then you should stop using proc_dointvec as it will upper limit the value to INT_MAX.
2. If you want to keep using nsm_local_state as unsigned int, then please add SYSCTL_ZERO as a lower bound to avoid assigning negative values
3. Having SYSCTL_INT_MAX is not necessary as it is already capped by proc_dointvec{_minmax,}, but it is nice to have as it makes explicit what is happening.

Quoted 4 lines

Let me know if you take this through your trees so I can remove it from sysctl.

The variable "nsm_local_state" is declared as "u32".

The range of the value of nsm_local_state is supposed to be 0 - UINT_MAX (ie, an unsigned 32-bit integer), so I'm proposing:

.data = &nsm_local_state,
.maxlen = sizeof(int),
.mode = 0644,
- .proc_handler = proc_dointvec,
+ .proc_handler = proc_douintvec,
},
};

But I suspect that the "maxlen" field should be changed to something like "sizeof(nsm_local_state)".

Does that seem correct to you?

I can take this patch through the NFSD tree so it can get some NFS- specific testing.

Signature · 1 line
Chuck Lever
#7 lore raw

Re: [PATCH v2 2/6] sysctl: Fixes nsm_local_state bounds

In reply to [email protected]

From: Chuck Lever <[email protected]>

Quoted 6 lines · On Mon, 24 Feb 2025 10:58:17 +0100, [email protected] wrote:

Bound nsm_local_state sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

Applied to nfsd-testing with modifications, thanks!

[2/6] sysctl: Fixes nsm_local_state bounds

      commit: 8e6d33ea0159b39d670b7986324bd6135ee9d5f7

--
Chuck Lever

#8 lore raw

Re: [PATCH v2 2/6] sysctl: Fixes nsm_local_state bounds

Quoted 10 lines · On Mon, Mar 03, 2025 at 04:42:17PM -0500, [email protected] wrote:

From: Chuck Lever <[email protected]>

Quoted 6 lines · On Mon, 24 Feb 2025 10:58:17 +0100, [email protected] wrote:

Bound nsm_local_state sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

Quoted 7 lines

Applied to nfsd-testing with modifications, thanks!

[2/6] sysctl: Fixes nsm_local_state bounds

      commit: 8e6d33ea0159b39d670b7986324bd6135ee9d5f7

--
Chuck Lever

Thx!

@nicolas: remove this one from your next Version

Best

Signature · 1 line
Joel Granados
#9 lore raw

PATCH v2 3/6 sysctl/coda: Fixes timeout bounds

In reply to [email protected]

From: Nicolas Bouchinet <[email protected]>

Bound coda timeout sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/coda/sysctl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/coda/sysctl.c +3 -1
index 0df46f09b6cc5..d6f8206c51575 100644
@@ -20,7 +20,9 @@ static const struct ctl_table coda_table[] = {
 		.data		= &coda_timeout,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
 	},
 	{
 		.procname	= "hard",
Signature · 1 line
2.48.1
#10 lore raw

Re: [PATCH v2 3/6] sysctl/coda: Fixes timeout bounds

Quoted 17 lines · On Mon, Feb 24, 2025 at 10:58:18AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound coda timeout sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/coda/sysctl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/coda/sysctl.c +0 -0
index 0df46f09b6cc5..d6f8206c51575 100644

fs/coda/sysctl.c static const struct ctl_table coda_table[] = { · line 20

Quoted 1 line
fs/coda/sysctl.c +0 -0
@@ -20,7 +20,9 @@ static const struct ctl_table coda_table[] = {
 		.data		= &coda_timeout,

I noticed that coda_timeout is an unsigned long. With that in mind I would change it to unsigned int. It seems to be a value that can be ranged within [0,INT_MAX]

Best

Quoted 12 lines
fs/coda/sysctl.c +3 -1
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
 	},
 	{
 		.procname	= "hard",
Signature · 1 line
2.48.1
Signature · 1 line
Joel Granados
maintainer
#11 lore raw

Re: [PATCH v2 3/6] sysctl/coda: Fixes timeout bounds

Quoted 13 lines · Joel Granados
Quoted 12 lines · On Mon, Feb 24, 2025 at 10:58:18AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound coda timeout sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/coda/sysctl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 9 lines

fs/coda/sysctl.c static const struct ctl_table coda_table[] = { · line 20

Quoted 6 lines
fs/coda/sysctl.c +0 -0
index 0df46f09b6cc5..d6f8206c51575 100644
@@ -20,7 +20,9 @@ static const struct ctl_table coda_table[] = {
 		.data		= &coda_timeout,

I noticed that coda_timeout is an unsigned long. With that in mind I would change it to unsigned int. It seems to be a value that can be ranged within [0,INT_MAX]

That seems fine by me.

It is a timeout in seconds and it is typically set to some value well under a minute.

Jan

#12 lore raw

Re: [PATCH v2 3/6] sysctl/coda: Fixes timeout bounds

Quoted 24 lines · Jan Harkes
Quoted 13 lines · Joel Granados
Quoted 12 lines · On Mon, Feb 24, 2025 at 10:58:18AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound coda timeout sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 fs/coda/sysctl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 9 lines

fs/coda/sysctl.c static const struct ctl_table coda_table[] = { · line 20

Quoted 6 lines
fs/coda/sysctl.c +0 -0
index 0df46f09b6cc5..d6f8206c51575 100644
@@ -20,7 +20,9 @@ static const struct ctl_table coda_table[] = {
 		.data		= &coda_timeout,

I noticed that coda_timeout is an unsigned long. With that in mind I would change it to unsigned int. It seems to be a value that can be ranged within [0,INT_MAX]

Quoted 5 lines

That seems fine by me.

It is a timeout in seconds and it is typically set to some value well under a minute.

Thx for the confirmation. I'll let nicolas take care of the change Best

Signature · 1 line
Joel Granados
#13 lore raw

Re: [PATCH v2 3/6] sysctl/coda: Fixes timeout bounds

Hi Jan and Joel,

Thanks for your review,

I'll update coda_timeout type to an unsigned int and push back the patchset without patches that has moved in their subsystems.

Best regards,

Nicolas

Quoted 32 lines · Joel Granados
Quoted 23 lines · Jan Harkes
Quoted 13 lines · Joel Granados
Quoted 12 lines · On Mon, Feb 24, 2025 at 10:58:18AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound coda timeout sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
  fs/coda/sysctl.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 9 lines

fs/coda/sysctl.c static const struct ctl_table coda_table[] = { · line 20

Quoted 6 lines
fs/coda/sysctl.c +0 -0
index 0df46f09b6cc5..d6f8206c51575 100644
@@ -20,7 +20,9 @@ static const struct ctl_table coda_table[] = {
  		.data		= &coda_timeout,

I noticed that coda_timeout is an unsigned long. With that in mind I would change it to unsigned int. It seems to be a value that can be ranged within [0,INT_MAX]

Quoted 5 lines

That seems fine by me.

It is a timeout in seconds and it is typically set to some value well under a minute.

Thx for the confirmation. I'll let nicolas take care of the change Best

#14 lore raw

PATCH v2 4/6 sysctl: Fixes scsi_logging_level bounds

In reply to [email protected]

From: Nicolas Bouchinet <[email protected]>

Bound scsi_logging_level sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 drivers/scsi/scsi_sysctl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/scsi/scsi_sysctl.c +3 -1
index be4aef0f4f996..055a03a83ad68 100644
@@ -17,7 +17,9 @@ static const struct ctl_table scsi_table[] = {
 	  .data		= &scsi_logging_level,
 	  .maxlen	= sizeof(scsi_logging_level),
 	  .mode		= 0644,
-	  .proc_handler	= proc_dointvec },
+	  .proc_handler	= proc_dointvec_minmax,
+	  .extra1	= SYSCTL_ZERO,
+	  .extra2	= SYSCTL_INT_MAX },
 };
 
 static struct ctl_table_header *scsi_table_header;
Signature · 1 line
2.48.1
#15 lore raw

Re: [PATCH v2 4/6] sysctl: Fixes scsi_logging_level bounds

Hi Nicolas!

drivers/scsi/scsi_sysctl.c static const struct ctl_table scsi_table[] = { · line 22

Quoted 10 lines
drivers/scsi/scsi_sysctl.c +3 -1
@@ -17,7 +17,9 @@ static const struct ctl_table scsi_table[] = {
 	  .data		= &scsi_logging_level,
 	  .maxlen	= sizeof(scsi_logging_level),
 	  .mode		= 0644,
-	  .proc_handler	= proc_dointvec },
+	  .proc_handler	= proc_dointvec_minmax,
+	  .extra1	= SYSCTL_ZERO,
+	  .extra2	= SYSCTL_INT_MAX },

scsi_logging_level is a bitmask and should be unsigned.

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#16 lore raw

Re: [PATCH v2 4/6] sysctl: Fixes scsi_logging_level bounds

Quoted 14 lines · Martin K. Petersen

Hi Nicolas!

drivers/scsi/scsi_sysctl.c static const struct ctl_table scsi_table[] = { · line 22

Quoted 10 lines
drivers/scsi/scsi_sysctl.c +3 -1
@@ -17,7 +17,9 @@ static const struct ctl_table scsi_table[] = {
  	  .data		= &scsi_logging_level,
  	  .maxlen	= sizeof(scsi_logging_level),
  	  .mode		= 0644,
-	  .proc_handler	= proc_dointvec },
+	  .proc_handler	= proc_dointvec_minmax,
+	  .extra1	= SYSCTL_ZERO,
+	  .extra2	= SYSCTL_INT_MAX },

scsi_logging_level is a bitmask and should be unsigned.

Hi Martin,

Thank's for your review.

Does scsi_logging_level needs the full range of a unsigned 32-bit integer ? As it was using proc_dointvec, it was capped to an INT_MAX.

If it effectively need the full range of an unsigned 32-bit integer, the proc_handler could be changed to proc_douintvec as suggested by Chuck.

Best regards,

Nicolas

Reviewed-by
#17 lore raw

Re: [PATCH v2 4/6] sysctl: Fixes scsi_logging_level bounds

Quoted 19 lines · Nicolas Bouchinet
Quoted 14 lines · Martin K. Petersen

Hi Nicolas!

drivers/scsi/scsi_sysctl.c static const struct ctl_table scsi_table[] = { · line 22

Quoted 10 lines
drivers/scsi/scsi_sysctl.c +3 -1
@@ -17,7 +17,9 @@ static const struct ctl_table scsi_table[] = {
  	  .data		= &scsi_logging_level,
  	  .maxlen	= sizeof(scsi_logging_level),
  	  .mode		= 0644,
-	  .proc_handler	= proc_dointvec },
+	  .proc_handler	= proc_dointvec_minmax,
+	  .extra1	= SYSCTL_ZERO,
+	  .extra2	= SYSCTL_INT_MAX },

scsi_logging_level is a bitmask and should be unsigned.

Hi Martin,

Quoted 8 lines

Thank's for your review.

Does scsi_logging_level needs the full range of a unsigned 32-bit integer ?
As it was using proc_dointvec, it was capped to an INT_MAX.

If it effectively need the full range of an unsigned 32-bit integer, the proc_handler could be changed to proc_douintvec as suggested by Chuck.

And as mentioned in another patch in this series:
1. Having the upper bound be SYSCTL_INT_MAX is not necessary (as it is silently capped by proc_dointvec_minmax, but it is good to have as it adds to the understanding on what the range of the values are.

2. Having the lower bound capped by SYSCTL_ZERO is needed as it will prevent ppl trying to assigning negative values to the unsigned integer

Let me know if you take this through the scsi subsystem so I know to drop it from sysctl

Best

Quoted 5 lines

Best regards,

Nicolas

Signature · 1 line
Joel Granados
#18 lore raw

Re: [PATCH v2 4/6] sysctl: Fixes scsi_logging_level bounds

Joel,

Quoted 9 lines

1. Having the upper bound be SYSCTL_INT_MAX is not necessary (as it is silently capped by proc_dointvec_minmax, but it is good to have as it adds to the understanding on what the range of the values are.

2. Having the lower bound capped by SYSCTL_ZERO is needed as it will prevent ppl trying to assigning negative values to the unsigned integer

Let me know if you take this through the scsi subsystem so I know to drop it from sysctl

Applied to 6.15/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#19 lore raw

Re: [PATCH v2 4/6] sysctl: Fixes scsi_logging_level bounds

Quoted 7 lines · Martin K. Petersen

Joel,

Quoted 4 lines

1. Having the upper bound be SYSCTL_INT_MAX is not necessary (as it is silently capped by proc_dointvec_minmax, but it is good to have as it adds to the understanding on what the range of the values are.

Quoted 7 lines
Quoted 5 lines

2. Having the lower bound capped by SYSCTL_ZERO is needed as it will prevent ppl trying to assigning negative values to the unsigned integer

Let me know if you take this through the scsi subsystem so I know to drop it from sysctl

Applied to 6.15/scsi-staging, thanks!

Thx!!

@Nicolas: Please take this out of your next version as it is already going upstream.

Best

Quoted 3 lines
Signature · 2 lines
-- 
Martin K. Petersen	Oracle Linux Engineering
Signature · 1 line
Joel Granados
#20 lore raw

PATCH v2 5/6 sysctl/infiniband: Fixes infiniband sysctl bounds

In reply to [email protected]

From: Nicolas Bouchinet <[email protected]>

Bound infiniband iwcm and ucma sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 drivers/infiniband/core/iwcm.c | 4 +++-
 drivers/infiniband/core/ucma.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
drivers/infiniband/core/iwcm.c +3 -1
index 7e3a55349e107..f4486cbd8f45a 100644
@@ -109,7 +109,9 @@ static struct ctl_table iwcm_ctl_table[] = {
 		.data		= &default_backlog,
 		.maxlen		= sizeof(default_backlog),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
 	},
 };
 
drivers/infiniband/core/ucma.c +3 -1
index 02f1666f3cbab..6e700b9740331 100644
@@ -69,7 +69,9 @@ static struct ctl_table ucma_ctl_table[] = {
 		.data		= &max_backlog,
 		.maxlen		= sizeof max_backlog,
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
 	},
 };
 
Signature · 1 line
2.48.1
Acked-by on v2 5/6
#21 lore raw

Re: [PATCH v2 5/6] sysctl/infiniband: Fixes infiniband sysctl bounds

Quoted 5 lines · On Mon, Feb 24, 2025 at 10:58:20AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound infiniband iwcm and ucma sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

Quoted 8 lines

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 drivers/infiniband/core/iwcm.c | 4 +++-
 drivers/infiniband/core/ucma.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)

How do you want to proceed from here? Should I take to RDMA repository?

Thanks

Reviewed-by
#22 lore raw

Re: [PATCH v2 5/6] sysctl/infiniband: Fixes infiniband sysctl bounds

Quoted 15 lines · Leon Romanovsky
Quoted 5 lines · On Mon, Feb 24, 2025 at 10:58:20AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound infiniband iwcm and ucma sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

Quoted 8 lines

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 drivers/infiniband/core/iwcm.c | 4 +++-
 drivers/infiniband/core/ucma.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
Quoted 5 lines

How do you want to proceed from here? Should I take to RDMA repository?

Thanks

It would be great if we push this through RDMA. Here are a few comments:
1. Having the upper bound be SYSCTL_INT_MAX is not necessary (as it is silently capped by proc_dointvec_minmax, but it is good to have as it gives understanding on what the spread of the values are.

2. Having the lower bound capped by SYSCTL_ZERO is needed as it will prevent ppl trying to assing negative values to the unsigned integers

Please let me know if you will push this through RDMA, so I know to remove it from sysctl.

Best

Signature · 1 line
Joel Granados
#23 lore raw

Re: [PATCH v2 5/6] sysctl/infiniband: Fixes infiniband sysctl bounds

Quoted 26 lines · Joel Granados
Quoted 15 lines · Leon Romanovsky
Quoted 5 lines · On Mon, Feb 24, 2025 at 10:58:20AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound infiniband iwcm and ucma sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

Quoted 8 lines

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 drivers/infiniband/core/iwcm.c | 4 +++-
 drivers/infiniband/core/ucma.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
Quoted 5 lines

How do you want to proceed from here? Should I take to RDMA repository?

Thanks

It would be great if we push this through RDMA. Here are a few comments:
1. Having the upper bound be SYSCTL_INT_MAX is not necessary (as it is silently capped by proc_dointvec_minmax, but it is good to have as it gives understanding on what the spread of the values are.

Quoted 5 lines

2. Having the lower bound capped by SYSCTL_ZERO is needed as it will prevent ppl trying to assing negative values to the unsigned integers

Please let me know if you will push this through RDMA, so I know to remove it from sysctl.

Applied to RDMA tree.
https://web.git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git/commit/?h=wip/leon-for-next&id=f33cd9b3fd03a791296ab37550ffd26213a90c4e

Quoted 4 lines

Best

Thanks

Quoted 4 lines
Signature · 3 lines
-- 

Joel Granados
#24 lore raw

Re: [PATCH v2 5/6] sysctl/infiniband: Fixes infiniband sysctl bounds

Quoted 27 lines · Leon Romanovsky
Quoted 26 lines · Joel Granados
Quoted 15 lines · Leon Romanovsky
Quoted 5 lines · On Mon, Feb 24, 2025 at 10:58:20AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Bound infiniband iwcm and ucma sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

Quoted 8 lines

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 drivers/infiniband/core/iwcm.c | 4 +++-
 drivers/infiniband/core/ucma.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
Quoted 5 lines

How do you want to proceed from here? Should I take to RDMA repository?

Thanks

It would be great if we push this through RDMA. Here are a few comments:
1. Having the upper bound be SYSCTL_INT_MAX is not necessary (as it is silently capped by proc_dointvec_minmax, but it is good to have as it gives understanding on what the spread of the values are.

Quoted 8 lines
Quoted 5 lines

2. Having the lower bound capped by SYSCTL_ZERO is needed as it will prevent ppl trying to assing negative values to the unsigned integers

Please let me know if you will push this through RDMA, so I know to remove it from sysctl.

Applied to RDMA tree.
https://web.git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git/commit/?h=wip/leon-for-next&id=f33cd9b3fd03a791296ab37550ffd26213a90c4e

Perfect. Thx.

@Nicolas: pls take this one out of your next version as it is already on its way upstrea.

Best

Quoted 12 lines
Quoted 5 lines

Best

Thanks

Quoted 4 lines
Signature · 3 lines
-- 

Joel Granados
Signature · 1 line
Joel Granados
Reviewed-by on v2 5/6
#25 lore raw

Re: [PATCH v2 5/6] sysctl/infiniband: Fixes infiniband sysctl bounds

In reply to [email protected]
Quoted 6 lines · 在 2025/2/24 10:58, [email protected] 写道:

From: Nicolas Bouchinet <[email protected]>

Bound infiniband iwcm and ucma sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

In this commit, the minimum and maximum are added to the proc_handler.

It seems that this will not introduce any risk.

I am fine with it.

Thanks,

Zhu Yanjun

Quoted 37 lines
---
  drivers/infiniband/core/iwcm.c | 4 +++-
  drivers/infiniband/core/ucma.c | 4 +++-
  2 files changed, 6 insertions(+), 2 deletions(-)
drivers/infiniband/core/iwcm.c +3 -1
index 7e3a55349e107..f4486cbd8f45a 100644
@@ -109,7 +109,9 @@ static struct ctl_table iwcm_ctl_table[] = {
  		.data		= &default_backlog,
  		.maxlen		= sizeof(default_backlog),
  		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
  	},
  };
 
drivers/infiniband/core/ucma.c +3 -1
index 02f1666f3cbab..6e700b9740331 100644
@@ -69,7 +69,9 @@ static struct ctl_table ucma_ctl_table[] = {
  		.data		= &max_backlog,
  		.maxlen		= sizeof max_backlog,
  		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_INT_MAX,
  	},
  };
 
Signature · 2 lines
Best Regards,
Yanjun.Zhu
#26 lore raw

PATCH v2 6/6 sysctl: Fixes max-user-freq bounds

In reply to [email protected]

From: Nicolas Bouchinet <[email protected]>

Bound max-user-freq sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.

The proc_handler has thus been updated to proc_dointvec_minmax.

---
 drivers/char/hpet.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/char/hpet.c +3 -1
index e110857824fcb..528b43e893d49 100644
@@ -730,7 +730,9 @@ static const struct ctl_table hpet_table[] = {
 	 .data = &hpet_max_freq,
 	 .maxlen = sizeof(int),
 	 .mode = 0644,
-	 .proc_handler = proc_dointvec,
+	 .proc_handler = proc_dointvec_minmax,
+	 .extra1 = SYSCTL_ZERO,
+	 .extra2 = SYSCTL_INT_MAX,
 	 },
 };
 
Signature · 1 line
2.48.1
#27 lore raw
In reply to [email protected]
Quoted 7 lines · On Mon, Feb 24, 2025 at 10:58:15AM +0100, [email protected] wrote:

From: Nicolas Bouchinet <[email protected]>

Hi,

This patchset adds some bound checks to sysctls to avoid negative value writes.

Quoted 6 lines

The patched sysctls were storing the result of the proc_dointvec proc_handler into an unsigned int data. proc_dointvec being able to parse negative value, and it return value being a signed int, this could lead to undefined behaviors.
This has led to kernel crash in the past as described in commit 3b3376f222e3 ("sysctl.c: fix underflow value setting risk in vm_table")

This patch is slightly different then what you are trying to do here. Notice that the issue in 3b3376f222e3 ("sysctl.c: fix underflow value setting risk in vm_table") was that the extra1 value was getting ignored because it was calling proc_dointvec; replacing it with proc_dointvec_minmax properly forwards the extra1 value. What your series is trying to do is to avoid a assignment of a negative value to a unsigned int.

Quoted 7 lines

They are now bounded between SYSCTL_ZERO and SYSCTL_INT_MAX. The proc_handlers have thus been updated to proc_dointvec_minmax.

This patchset has been written over sysctl-testing branch [1]. See [2] for similar sysctl fixes currently in review.

Quoted 6 lines

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctl.git/log/?h=sysctl-testing
[2]: https://lore.kernel.org/all/[email protected]/

Best regards,

Nicolas

In general, I would like for these patches to go into mainline through their individual subsystem as they would know more about what type fits best for the ctl_table->data variable. With that said, I'll push the leftovers through sysctl if there are no takers.

Thx for the series

Quoted 34 lines

---

Changes since v1:
https://lore.kernel.org/all/[email protected]/

* Detached patches 1/9, 2/9 [3] and 3/9 [4]
* Adapted the cover-letter message to match the reduced patchset

[3]: https://lore.kernel.org/all/[email protected]/
[4]: https://lore.kernel.org/all/[email protected]/

---

Nicolas Bouchinet (6):
sysctl: Fixes idmap_cache_timeout bounds
sysctl: Fixes nsm_local_state bounds sysctl/coda: Fixes timeout bounds
sysctl: Fixes scsi_logging_level bounds sysctl/infiniband: Fixes infiniband sysctl bounds
sysctl: Fixes max-user-freq bounds

 drivers/char/hpet.c            | 4 +++-
 drivers/infiniband/core/iwcm.c | 4 +++-
 drivers/infiniband/core/ucma.c | 4 +++-
 drivers/scsi/scsi_sysctl.c     | 4 +++-
 fs/coda/sysctl.c               | 4 +++-
 fs/lockd/svc.c                 | 4 +++-
 fs/nfs/nfs4sysctl.c            | 4 +++-
 7 files changed, 21 insertions(+), 7 deletions(-)
Signature · 1 line
2.48.1
Signature · 1 line
Joel Granados
#28 lore raw

Re: (subset) [PATCH v2 0/6] Fixes multiple sysctl bound checks

In reply to [email protected]
Quoted 11 lines · On Mon, 24 Feb 2025 10:58:15 +0100, [email protected] wrote:

This patchset adds some bound checks to sysctls to avoid negative value writes.

The patched sysctls were storing the result of the proc_dointvec proc_handler into an unsigned int data. proc_dointvec being able to parse negative value, and it return value being a signed int, this could lead to undefined behaviors.
This has led to kernel crash in the past as described in commit 3b3376f222e3 ("sysctl.c: fix underflow value setting risk in vm_table")

[...]

Applied to 6.15/scsi-queue, thanks!

[4/6] sysctl: Fixes scsi_logging_level bounds

      https://git.kernel.org/mkp/scsi/c/2cef5b4472c6
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering