lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220706052130.16368-3-kuniyu@amazon.com>
Date:   Tue, 5 Jul 2022 22:21:16 -0700
From:   Kuniyuki Iwashima <kuniyu@...zon.com>
To:     "David S. Miller" <davem@...emloft.net>,
        Eric Dumazet <edumazet@...gle.com>,
        Jakub Kicinski <kuba@...nel.org>,
        Paolo Abeni <pabeni@...hat.com>,
        Luis Chamberlain <mcgrof@...nel.org>,
        Kees Cook <keescook@...omium.org>,
        Iurii Zaikin <yzaikin@...gle.com>
CC:     Kuniyuki Iwashima <kuniyu@...zon.com>,
        Kuniyuki Iwashima <kuni1840@...il.com>,
        <netdev@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
        Jia He <hejianet@...il.com>
Subject: [PATCH v1 net 02/16] sysctl: Add proc_dobool_lockless().

A sysctl variable is accessed concurrently, and there is always a chance of
data-race.  So, all readers and writers need some basic protection to avoid
load/store-tearing.

This patch changes proc_dobool() to use READ_ONCE()/WRITE_ONCE() internally
to fix a data-race on the sysctl side.  For now, proc_dobool() itself is
tolerant to a data-race, but we still need to add annotations on the other
subsystem's side.

In case we miss such fixes, this patch converts proc_dobool() to a wrapper
of proc_dobool_lockless().  When we fix a data-race in the other subsystem,
we can explicitly set it as a handler.

Also, this patch removes proc_dobool()'s document and adds
proc_dobool_lockless()'s one so that no one will use proc_dobool() anymore.

Fixes: a2071573d634 ("sysctl: introduce new proc handler proc_dobool")
Signed-off-by: Kuniyuki Iwashima <kuniyu@...zon.com>
---
CC: Jia He <hejianet@...il.com>
---
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 23 ++++++++++++++++-------
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 9beab3a4de3d..fcafc16abbad 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -83,6 +83,8 @@ PROC_HANDLER(proc_dointvec_ms_jiffies);
 PROC_HANDLER(proc_do_large_bitmap);
 PROC_HANDLER(proc_do_static_key);
 
+PROC_HANDLER(proc_dobool_lockless);
+
 /*
  * Register a set of sysctl names by calling register_sysctl_table
  * with an initialised array of struct ctl_table's.  An entry with 
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 1082c8bc5ba5..bc6fcc64eeaf 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -424,13 +424,12 @@ 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)
+			       int *valp, int write, void *data)
 {
 	if (write) {
-		*(bool *)valp = *lvalp;
+		WRITE_ONCE(*(bool *)valp, *lvalp);
 	} else {
-		int val = *(bool *)valp;
+		int val = READ_ONCE(*(bool *)valp);
 
 		*lvalp = (unsigned long)val;
 		*negp = false;
@@ -701,7 +700,7 @@ int do_proc_douintvec(struct ctl_table *table, int write,
 }
 
 /**
- * proc_dobool - read/write a bool
+ * proc_dobool_lockless - read/write a bool locklessly
  * @table: the sysctl table
  * @write: %TRUE if this is a write to the sysctl file
  * @buffer: the user buffer
@@ -713,13 +712,19 @@ int do_proc_douintvec(struct ctl_table *table, int write,
  *
  * Returns 0 on success.
  */
-int proc_dobool(struct ctl_table *table, int write, void *buffer,
-		size_t *lenp, loff_t *ppos)
+int proc_dobool_lockless(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);
 }
 
+int proc_dobool(struct ctl_table *table, int write, void *buffer,
+		size_t *lenp, loff_t *ppos)
+{
+	return proc_dobool_lockless(table, write, buffer, lenp, ppos);
+}
+
 /**
  * proc_dointvec - read a vector of integers
  * @table: the sysctl table
@@ -1497,6 +1502,8 @@ PROC_HANDLER_ENOSYS(proc_dointvec_ms_jiffies);
 PROC_HANDLER_ENOSYS(proc_do_cad_pid);
 PROC_HANDLER_ENOSYS(proc_do_large_bitmap);
 
+PROC_HANDLER_ENOSYS(proc_dobool_lockless);
+
 #endif /* CONFIG_PROC_SYSCTL */
 
 #if defined(CONFIG_SYSCTL)
@@ -2405,3 +2412,5 @@ EXPORT_SYMBOL(proc_dointvec_jiffies);
 EXPORT_SYMBOL(proc_dointvec_userhz_jiffies);
 EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
 EXPORT_SYMBOL(proc_do_large_bitmap);
+
+EXPORT_SYMBOL(proc_dobool_lockless);
-- 
2.30.2

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ