module: Make srcu_struct ptr array as read-only
authorJoel Fernandes (Google) <joel@joelfernandes.org>
Thu, 11 Apr 2019 20:24:21 +0000 (16:24 -0400)
committerPaul E. McKenney <paulmck@linux.ibm.com>
Tue, 28 May 2019 16:03:35 +0000 (09:03 -0700)
commit056b89e7e699742cc060ce722d3f26effe51b4aa
treed2aac68d6dcbca74f3e39a1b246035d775173721
parent54e6c11b9e74fd780e52f95c3eed313e0f4e6b7e
module: Make srcu_struct ptr array as read-only

Since commit title ("srcu: Allocate per-CPU data for DEFINE_SRCU() in
modules"), modules that call DEFINE_{STATIC,}SRCU will have a new array
of srcu_struct pointers, which is used by srcu code to initialize and
clean up these structures and save valuable per-cpu reserved space.

There is no reason for this array of pointers to be writable, and can
cause security or other hidden bugs. Mark these are read-only after the
module init has completed.

Tested with the following diff to ensure array not writable:

(diff is a bit reduced to avoid patch command getting confused)
 a/kernel/module.c
 b/kernel/module.c
  -3506,6 +3506,14  static noinline int do_init_module [snip]
  rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms);
 #endif
  module_enable_ro(mod, true);
+
+ if (mod->srcu_struct_ptrs) {
+ // Check if srcu_struct_ptrs access is possible
+ char x = *(char *)mod->srcu_struct_ptrs;
+ *(char *)mod->srcu_struct_ptrs = 0;
+ *(char *)mod->srcu_struct_ptrs = x;
+ }
+
  mod_tree_remove_init(mod);
  disable_ro_nx(&mod->init_layout);
  module_arch_freeing_init(mod);

Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: paulmck@linux.vnet.ibm.com
Cc: rostedt@goodmis.org
Cc: mathieu.desnoyers@efficios.com
Cc: rcu@vger.kernel.org
Cc: kernel-hardening@lists.openwall.com
Cc: kernel-team@android.com
Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com>
include/linux/srcutree.h