Skip to contentSkip to conversation

rseq: Validate read-only fields under DEBUG_RSEQ config

RFC PATCH New Mathieu Desnoyers 2 participants 2 messages Oct 18, 2024 – Oct 23, 2024

Subsystems (2)
lore
Share & tools
lore

Mathieu Desnoyers

author maintainer
#0 lore raw

RFC PATCH 1/1 rseq: Validate read-only fields under DEBUG_RSEQ config

The rseq uapi requires cooperation between users of the rseq fields to ensure that all libraries and applications using rseq within a process do not interfere with each other.

This is especially important for fields which are meant to be read-only from user-space, as documented in uapi/linux/rseq.h:

- cpu_id_start,
- cpu_id,
- node_id,
- mm_cid.

Storing to those fields from a user-space library prevents any sharing of the rseq ABI with other libraries and applications, as other users are not aware that the content of those fields has been altered by a third-party library.

This is unfortunately the current behavior of tcmalloc: it purposefully overlaps part of a cached value with the cpu_id_start upper bits to get notified about preemption, because the kernel clears those upper bits before returning to user-space. This behavior does not conform to the rseq uapi header ABI.

This prevents tcmalloc from using rseq when rseq is registered by the GNU C library 2.35+. It requires tcmalloc users to disable glibc rseq registration with a glibc tunable, which is a sad state of affairs.

Considering that tcmalloc and the GNU C library are the two first upstream projects using rseq, and that they are already incompatible due to use of this hack, adding kernel-level validation of all read-only fields content is necessary to ensure future users of rseq abide by the rseq ABI requirements.

Validate that user-space does not corrupt the read-only fields and conform to the rseq uapi header ABI when the kernel is built with CONFIG_DEBUG_RSEQ=y. This is done by storing a copy of the read-only fields in the task_struct, and validating the prior values present in user-space before updating them. If the values do not match, print a warning on the console (printk_ratelimited()).

This is a first step to identify misuses of the rseq ABI by printing a warning on the console. After a giving some time to userspace to correct its use of rseq, the plan is to eventually terminate offending processes with SIGSEGV.

This change is expected to produce warnings for the upstream tcmalloc implementation, but tcmalloc developers mentioned they were open to adapt their implementation to kernel-level change.

---
 include/linux/sched.h |  3 ++
 kernel/rseq.c         | 87 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+)
include/linux/sched.h +3 -0
index f8d150343d42..041b7cd96423 100644
@@ -1340,6 +1340,9 @@ struct task_struct {
 	 * with respect to preemption.
 	 */
 	unsigned long rseq_event_mask;
+# ifdef CONFIG_DEBUG_RSEQ
+	struct rseq			rseq_fields;
+# endif
 #endif
 
 #ifdef CONFIG_SCHED_MM_CID
kernel/rseq.c +87 -0

123 lines not on this page · raw message

Signature · 1 line
2.39.5
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Mathieu,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on tip/sched/core]
[also build test WARNING on akpm-mm/mm-everything linus/master v6.12-rc4 next-20241022] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mathieu-Desnoyers/rseq-Validate-read-only-fields-under-DEBUG_RSEQ-config/20241019-011655
base:   tip/sched/core
patch link:    https://lore.kernel.org/r/20241018171219.256647-1-mathieu.desnoyers%40efficios.com
patch subject: [RFC PATCH] rseq: Validate read-only fields under DEBUG_RSEQ config
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20241023/[email protected]/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 5886454669c3c9026f7f27eab13509dd0241f2d6)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241023/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from arch/s390/purgatory/purgatory.c:10:
In file included from include/linux/kexec.h:18:
In file included from include/linux/vmcore_info.h:6:
In file included from include/linux/elfcore.h:7:
In file included from include/linux/sched/task_stack.h:9:

>> include/linux/sched.h:1368:16: warning: field 'rseq_fields' with variable sized type 'struct rseq' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
    1368 |         struct rseq                     rseq_fields;
         |                                         ^

1 warning generated.

vim +1368 include/linux/sched.h

1357
1358 #ifdef CONFIG_RSEQ
1359 struct rseq __user *rseq;
1360 u32 rseq_len;
1361 u32 rseq_sig;
1362 /*
1363 * RmW on rseq_event_mask must be performed atomically
1364 * with respect to preemption.
1365 */
1366 unsigned long rseq_event_mask;
1367 # ifdef CONFIG_DEBUG_RSEQ

> 1368		struct rseq			rseq_fields;

1369 # endif
1370 #endif
1371

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki