Skip to contentSkip to conversation

Fix /proc/sys/fs/nfs/nsm_use_hostnames on big endian machines

PATCH In review Thomas Huth 5 participants 11 messages Aug 3, 2021 – Sep 16, 2021

Subsystems (5)
VFS NFS client NFS server s390 +1 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Fix /proc/sys/fs/nfs/nsm_use_hostnames on big endian machines

There is an endianess problem with /proc/sys/fs/nfs/nsm_use_hostnames (which can e.g. be seen on an s390x host) :

 # modprobe lockd nsm_use_hostnames=1
 # cat /proc/sys/fs/nfs/nsm_use_hostnames
 16777216

The nsm_use_hostnames variable is declared as "bool" which is required for the correct type for the module parameter. However, this does not work correctly with the entry in the /proc filesystem since this currently requires "int".

Jia He already provided patches for this problem a couple of years ago, but apparently they felt through the cracks and never got merged. So here's a rebased version to finally fix this issue.

Jia He (2):
sysctl: introduce new proc handler proc_dobool
lockd: change the proc_handler for nsm_use_hostnames

 fs/lockd/svc.c         |  2 +-
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/2 sysctl: introduce new proc handler proc_dobool

From: Jia He <[email protected]>

This is to let bool variable could be correctly displayed in big/little endian sysctl procfs. sizeof(bool) is arch dependent, proc_dobool should work in all arches.

[thuth: rebased the patch to the current kernel version]
---
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/linux/sysctl.h +2 -0
index d99ca99837de..1fa2b69c6fc3 100644
@@ -48,6 +48,8 @@ typedef int proc_handler(struct ctl_table *ctl, int write, void *buffer,
 		size_t *lenp, loff_t *ppos);
 
 int proc_dostring(struct ctl_table *, int, void *, size_t *, loff_t *);
+int proc_dobool(struct ctl_table *table, int write, void *buffer,
+		size_t *lenp, loff_t *ppos);
 int proc_dointvec(struct ctl_table *, int, void *, size_t *, loff_t *);
 int proc_douintvec(struct ctl_table *, int, void *, size_t *, loff_t *);
 int proc_dointvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
kernel/sysctl.c +42 -0

66 lines not on this page · raw message

Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH 1/2] sysctl: introduce new proc handler proc_dobool

Quoted 96 lines · Thomas Huth

From: Jia He <[email protected]>

This is to let bool variable could be correctly displayed in big/little endian sysctl procfs. sizeof(bool) is arch dependent, proc_dobool should work in all arches.

[thuth: rebased the patch to the current kernel version]
---
  include/linux/sysctl.h |  2 ++
  kernel/sysctl.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 44 insertions(+)
include/linux/sysctl.h +2 -0
index d99ca99837de..1fa2b69c6fc3 100644
@@ -48,6 +48,8 @@ typedef int proc_handler(struct ctl_table *ctl, int write, void *buffer,
  		size_t *lenp, loff_t *ppos);
 
  int proc_dostring(struct ctl_table *, int, void *, size_t *, loff_t *);
+int proc_dobool(struct ctl_table *table, int write, void *buffer,
+		size_t *lenp, loff_t *ppos);
  int proc_dointvec(struct ctl_table *, int, void *, size_t *, loff_t *);
  int proc_douintvec(struct ctl_table *, int, void *, size_t *, loff_t *);
  int proc_dointvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
kernel/sysctl.c +41 -0
index 272f4a272f8c..25e49b4d8049 100644
@@ -536,6 +536,21 @@ static void proc_put_char(void **buf, size_t *size, char c)
  	}
  }
 
+static int do_proc_dobool_conv(bool *negp, unsigned long *lvalp,
+				int *valp,
+				int write, void *data)
+{
+	if (write) {
+		*(bool *)valp = *lvalp;
+	} else {
+		int val = *(bool *)valp;
+
+		*lvalp = (unsigned long)val;
+		*negp = false;
+	}
+	return 0;
+}
+
  static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
  				 int *valp,
  				 int write, void *data)
@@ -798,6 +813,26 @@ static int do_proc_douintvec(struct ctl_table *table, int write,
  				   buffer, lenp, ppos, conv, data);
  }
 
+/**
+ * proc_dobool - read/write a bool
+ * @table: the sysctl table
+ * @write: %TRUE if this is a write to the sysctl file
+ * @buffer: the user buffer
+ * @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.
+ *
+ * Returns 0 on success.
+ */
+int proc_dobool(struct ctl_table *table, int write, void *buffer,
+		size_t *lenp, loff_t *ppos)
+{
+	return do_proc_dointvec(table, write, buffer, lenp, ppos,
+				do_proc_dobool_conv, NULL);
+}
+
  /**
   * proc_dointvec - read a vector of integers
   * @table: the sysctl table
@@ -1630,6 +1665,12 @@ int proc_dostring(struct ctl_table *table, int write,
  	return -ENOSYS;
  }
 
+int proc_dobool(struct ctl_table *table, int write,
+		void *buffer, size_t *lenp, loff_t *ppos)
+{
+	return -ENOSYS;
+}
+
  int proc_dointvec(struct ctl_table *table, int write,
  		  void *buffer, size_t *lenp, loff_t *ppos)
  {

kernel/sysctl.c int __init sysctl_init(void) · line 3472

Quoted 7 lines
kernel/sysctl.c +1 -0
@@ -3425,6 +3466,7 @@ int __init sysctl_init(void)
   * No sense putting this after each symbol definition, twice,
   * exception granted :-)
   */
+EXPORT_SYMBOL(proc_dobool);
  EXPORT_SYMBOL(proc_dointvec);
  EXPORT_SYMBOL(proc_douintvec);
  EXPORT_SYMBOL(proc_dointvec_jiffies);

Friendly ping!

Luis, Kees, Iurii, could you please have a look and provide an Ack if this looks ok to you?

Thanks,
Thomas

#3 lore raw

Re: [PATCH 1/2] sysctl: introduce new proc handler proc_dobool

Quoted 106 lines · Thomas Huth
Quoted 95 lines · Thomas Huth

From: Jia He <[email protected]>

This is to let bool variable could be correctly displayed in big/little endian sysctl procfs. sizeof(bool) is arch dependent, proc_dobool should work in all arches.

[thuth: rebased the patch to the current kernel version]

---
  include/linux/sysctl.h |  2 ++
  kernel/sysctl.c        | 42 ++++++++++++++++++++++++++++++++++++++++++

  2 files changed, 44 insertions(+)
include/linux/sysctl.h +2 -0
index d99ca99837de..1fa2b69c6fc3 100644
@@ -48,6 +48,8 @@ typedef int proc_handler(struct ctl_table *ctl, int 
 write, void *buffer,
           size_t *lenp, loff_t *ppos);
   int proc_dostring(struct ctl_table *, int, void *, size_t *, loff_t *);
+int proc_dobool(struct ctl_table *table, int write, void *buffer,
+        size_t *lenp, loff_t *ppos);
   int proc_dointvec(struct ctl_table *, int, void *, size_t *, loff_t *);
   int proc_douintvec(struct ctl_table *, int, void *, size_t *, loff_t *);
   int proc_dointvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
kernel/sysctl.c +41 -0
index 272f4a272f8c..25e49b4d8049 100644
@@ -536,6 +536,21 @@ static void proc_put_char(void **buf, size_t *size, 
 char c)
       }
   }
+static int do_proc_dobool_conv(bool *negp, unsigned long *lvalp,
+                int *valp,
+                int write, void *data)
+{
+    if (write) {
+        *(bool *)valp = *lvalp;
+    } else {
+        int val = *(bool *)valp;
+
+        *lvalp = (unsigned long)val;
+        *negp = false;
+    }
+    return 0;
+}
+
   static int do_proc_dointvec_conv(bool *negp, unsigned long *lvalp,
                    int *valp,
                    int write, void *data)
@@ -798,6 +813,26 @@ static int do_proc_douintvec(struct ctl_table *table, 
 int write,
                      buffer, lenp, ppos, conv, data);
   }
+/**
+ * proc_dobool - read/write a bool
+ * @table: the sysctl table
+ * @write: %TRUE if this is a write to the sysctl file
+ * @buffer: the user buffer
+ * @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.
+ *
+ * Returns 0 on success.
+ */
+int proc_dobool(struct ctl_table *table, int write, void *buffer,
+        size_t *lenp, loff_t *ppos)
+{
+    return do_proc_dointvec(table, write, buffer, lenp, ppos,
+                do_proc_dobool_conv, NULL);
+}
+
   /**
    * proc_dointvec - read a vector of integers
    * @table: the sysctl table
@@ -1630,6 +1665,12 @@ int proc_dostring(struct ctl_table *table, int write,
       return -ENOSYS;
   }
+int proc_dobool(struct ctl_table *table, int write,
+        void *buffer, size_t *lenp, loff_t *ppos)
+{
+    return -ENOSYS;
+}
+
   int proc_dointvec(struct ctl_table *table, int write,
             void *buffer, size_t *lenp, loff_t *ppos)
   {

kernel/sysctl.c int __init sysctl_init(void) · line 3472

Quoted 7 lines
kernel/sysctl.c +1 -0
@@ -3425,6 +3466,7 @@ int __init sysctl_init(void)
    * No sense putting this after each symbol definition, twice,
    * exception granted :-)
    */
+EXPORT_SYMBOL(proc_dobool);
   EXPORT_SYMBOL(proc_dointvec);
   EXPORT_SYMBOL(proc_douintvec);
   EXPORT_SYMBOL(proc_dointvec_jiffies);

Friendly ping!

Quoted 1 line

Luis, Kees, Iurii, could you please have a look and provide an Ack if this looks ok to you?

Ping again!

Could anybody please provide an Ack?

Thanks,
Thomas

Acked-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] sysctl: introduce new proc handler proc_dobool

In reply to Thomas Huth
Quoted 8 lines · Thomas Huth

From: Jia He <[email protected]>

This is to let bool variable could be correctly displayed in big/little endian sysctl procfs. sizeof(bool) is arch dependent, proc_dobool should work in all arches.

Hi! I apologize for the delay. Yes, this looks good to me; thanks!

Signature · 1 line
Kees Cook
#5 lore raw

PATCH 2/2 lockd: change the proc_handler for nsm_use_hostnames

In reply to Thomas Huth

From: Jia He <[email protected]>

nsm_use_hostnames is a module parameter and it will be exported to sysctl procfs. This is to let user sometimes change it from userspace. But the minimal unit for sysctl procfs read/write it sizeof(int).
In big endian system, the converting from/to bool to/from int will cause error for proc items.

This patch use a new proc_handler proc_dobool to fix it.

[thuth: Fix typo in commit message]
---
 fs/lockd/svc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/lockd/svc.c +1 -1
index 2de048f80eb8..0ab9756ed235 100644
@@ -584,7 +584,7 @@ static struct ctl_table nlm_sysctls[] = {
 		.data		= &nsm_use_hostnames,
 		.maxlen		= sizeof(int),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dobool,
 	},
 	{
 		.procname	= "nsm_local_state",
Signature · 1 line
2.27.0
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] lockd: change the proc_handler for nsm_use_hostnames

Quoted 23 lines · Thomas Huth

From: Jia He <[email protected]>

nsm_use_hostnames is a module parameter and it will be exported to sysctl procfs. This is to let user sometimes change it from userspace. But the minimal unit for sysctl procfs read/write it sizeof(int).
In big endian system, the converting from/to bool to/from int will cause error for proc items.

This patch use a new proc_handler proc_dobool to fix it.

[thuth: Fix typo in commit message]
---
 fs/lockd/svc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/lockd/svc.c +0 -0
index 2de048f80eb8..0ab9756ed235 100644

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

Quoted 2 lines
fs/lockd/svc.c +0 -0
@@ -584,7 +584,7 @@ static struct ctl_table nlm_sysctls[] = {
 		.data		= &nsm_use_hostnames,
 		.maxlen		= sizeof(int),

For robustness, maybe this should be:

.maxlen = sizeof(nsm_use_hostnames),

Quoted 9 lines
fs/lockd/svc.c +1 -1
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec,
+		.proc_handler	= proc_dobool,
 	},
 	{
 		.procname	= "nsm_local_state",
Signature · 1 line
2.27.0

Also, I wonder what other BE-corrupted bools are out there?

Signature · 1 line
Kees Cook
#7 lore raw
In reply to Thomas Huth

Looks good to me. Could Chuck take it with nfsd stuff if somebody could ACK the sysctl part?

--b.

Quoted 29 lines · Thomas Huth

There is an endianess problem with /proc/sys/fs/nfs/nsm_use_hostnames (which can e.g. be seen on an s390x host) :

 # modprobe lockd nsm_use_hostnames=1
 # cat /proc/sys/fs/nfs/nsm_use_hostnames
 16777216

The nsm_use_hostnames variable is declared as "bool" which is required for the correct type for the module parameter. However, this does not work correctly with the entry in the /proc filesystem since this currently requires "int".

Jia He already provided patches for this problem a couple of years ago, but apparently they felt through the cracks and never got merged. So here's a rebased version to finally fix this issue.

Jia He (2):
sysctl: introduce new proc handler proc_dobool
lockd: change the proc_handler for nsm_use_hostnames

 fs/lockd/svc.c         |  2 +-
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)
Signature · 1 line
2.27.0
#8 lore raw

Thanks for your review, Bruce. I'll watch for the additional Ack.

Quoted 37 lines · J. Bruce Fields

Looks good to me. Could Chuck take it with nfsd stuff if somebody could ACK the sysctl part?

--b.

Quoted 29 lines · Thomas Huth

There is an endianess problem with /proc/sys/fs/nfs/nsm_use_hostnames (which can e.g. be seen on an s390x host) :

# modprobe lockd nsm_use_hostnames=1
# cat /proc/sys/fs/nfs/nsm_use_hostnames
16777216

The nsm_use_hostnames variable is declared as "bool" which is required for the correct type for the module parameter. However, this does not work correctly with the entry in the /proc filesystem since this currently requires "int".

Jia He already provided patches for this problem a couple of years ago, but apparently they felt through the cracks and never got merged. So here's a rebased version to finally fix this issue.

Jia He (2):
sysctl: introduce new proc handler proc_dobool
lockd: change the proc_handler for nsm_use_hostnames

fs/lockd/svc.c         |  2 +-
include/linux/sysctl.h |  2 ++
kernel/sysctl.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 45 insertions(+), 1 deletion(-)
Signature · 1 line
2.27.0

--
Chuck Lever

#9 lore raw
In reply to Thomas Huth
Quoted 16 lines · Thomas Huth

There is an endianess problem with /proc/sys/fs/nfs/nsm_use_hostnames (which can e.g. be seen on an s390x host) :

  # modprobe lockd nsm_use_hostnames=1
  # cat /proc/sys/fs/nfs/nsm_use_hostnames
  16777216

The nsm_use_hostnames variable is declared as "bool" which is required for the correct type for the module parameter. However, this does not work correctly with the entry in the /proc filesystem since this currently requires "int".

Jia He already provided patches for this problem a couple of years ago, but apparently they felt through the cracks and never got merged. So here's a rebased version to finally fix this issue.

Quoted 5 lines

Jia He (2):
sysctl: introduce new proc handler proc_dobool
lockd: change the proc_handler for nsm_use_hostnames

Thanks for picking them up ;-)

---
Cheers,
Justin (Jia He)

#10 lore raw
In reply to Thomas Huth
Quoted 22 lines · Thomas Huth

There is an endianess problem with /proc/sys/fs/nfs/nsm_use_hostnames (which can e.g. be seen on an s390x host) :

# modprobe lockd nsm_use_hostnames=1
# cat /proc/sys/fs/nfs/nsm_use_hostnames
16777216

The nsm_use_hostnames variable is declared as "bool" which is required for the correct type for the module parameter. However, this does not work correctly with the entry in the /proc filesystem since this currently requires "int".

Jia He already provided patches for this problem a couple of years ago, but apparently they felt through the cracks and never got merged. So here's a rebased version to finally fix this issue.

Quoted 11 lines

Jia He (2):
sysctl: introduce new proc handler proc_dobool
lockd: change the proc_handler for nsm_use_hostnames

fs/lockd/svc.c         |  2 +-
include/linux/sysctl.h |  2 ++
kernel/sysctl.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 45 insertions(+), 1 deletion(-)
Signature · 1 line
2.27.0

To get these patches in front of our zero-day apparatus, I've applied them to the for-next topic branch here:

https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux.git/log/?h=for-next

However I haven't seen an Ack for the kernel/sysctl.c change yet.

--
Chuck Lever