x86/fpu/xstate: Clear uninitialized xstate areas in core dump
Subsystems (2)
PATCH 1/1 x86/fpu/xstate: Clear uninitialized xstate areas in core dump
In a core dump, copy_xstate_to_kernel() copies only enabled user xfeatures to a kernel buffer without touching areas for disabled xfeatures. However, those uninitialized areas may contain random data, which is then written to the core dump file and can be read by a non-privileged user.
Fix it by clearing uninitialized areas.
--- arch/x86/kernel/fpu/xstate.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
arch/x86/kernel/fpu/xstate.c +16 -0
@@ -983,6 +983,7 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of {unsigned int offset, size;struct xstate_header header; +int last_off;int i;/*
@@ -1006,7 +1007,17 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of__copy_xstate_to_kernel (kbuf, &header, offset, size, size_total); + last_off =0 ; +for (i =0 ; i <XFEATURE_MAX ; i++) { +/* +* Clear uninitialized area before XSAVE header. +*/ +if (i ==FIRST_EXTENDED_XFEATURE ) { +memset (kbuf + last_off,0 ,XSAVE_HDR_OFFSET - last_off); + last_off =XSAVE_HDR_OFFSET +XSAVE_HDR_SIZE ; + } +/* * Copy only in-use xstates: */
@@ -1020,11 +1031,16 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int ofif (offset + size > size_total)break ; +memset (kbuf + last_off,0 , offset - last_off); + last_off = offset + size; +__copy_xstate_to_kernel (kbuf, src, offset, size, size_total); } } +memset (kbuf + last_off,0 , size_total - last_off); +if (xfeatures_mxcsr_quirk (header.xfeatures)) { offset = offsetof(struct fxregs_state , mxcsr); size =MXCSR_AND_FLAGS_SIZE ;
Signature · 1 line
2.21.0
Quoted 6 lines · Yu-cheng Yu
In a core dump,
copy_xstate_to_kernel()copies only enabled user xfeatures to a kernel buffer without touching areas for disabled xfeatures. However, those uninitialized areas may contain random data, which is then written to the core dump file and can be read by a non-privileged user.Fix it by clearing uninitialized areas.
Do you have a Fixes: tag for this, or some background on where this issue originated that might be helpful for backports?
Quoted 10 lines · Dave Hansen
Quoted 6 lines · Yu-cheng Yu
In a core dump,
copy_xstate_to_kernel()copies only enabled user xfeatures to a kernel buffer without touching areas for disabled xfeatures. However, those uninitialized areas may contain random data, which is then written to the core dump file and can be read by a non-privileged user.Fix it by clearing uninitialized areas.
Do you have a Fixes: tag for this, or some background on where this issue originated that might be helpful for backports?
I will add that.
Quoted 6 lines · Yu-cheng Yu
In a core dump,
copy_xstate_to_kernel()copies only enabled user xfeatures to a kernel buffer without touching areas for disabled xfeatures. However, those uninitialized areas may contain random data, which is then written to the core dump file and can be read by a non-privileged user.Fix it by clearing uninitialized areas.
Is the problem that copy_xstate_to_kernel() gets `kbuf' passed which isn't zeroed? If so, would it work clean that upfront?
Sebastian
Quoted 10 lines · Sebastian Andrzej Siewior
Quoted 6 lines · Yu-cheng Yu
In a core dump,
copy_xstate_to_kernel()copies only enabled user xfeatures to a kernel buffer without touching areas for disabled xfeatures. However, those uninitialized areas may contain random data, which is then written to the core dump file and can be read by a non-privileged user.Fix it by clearing uninitialized areas.
Is the problem that
copy_xstate_to_kernel()gets `kbuf' passed which isn't zeroed? If so, would it work clean that upfront?
Alexander Potapenko's patch (in the Link:) fixes the buffer in fill_thread_core_info(). My patch prevents the same issue if this function is called from somewhere else in the future.
Yu-cheng
Quoted 29 lines · Yu-cheng Yu
arch/x86/kernel/fpu/xstate.c +11
-0@@ -983,6 +983,7 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of {unsigned int offset, size;struct xstate_header header; +int last_off;int i;/* @@ -1006,7 +1007,17 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of__copy_xstate_to_kernel (kbuf, &header, offset, size, size_total); + last_off =0 ; +for (i =0 ; i <XFEATURE_MAX ; i++) { +/* +* Clear uninitialized area before XSAVE header. +*/ +if (i ==FIRST_EXTENDED_XFEATURE ) { +memset (kbuf + last_off,0 ,XSAVE_HDR_OFFSET - last_off); + last_off =XSAVE_HDR_OFFSET +XSAVE_HDR_SIZE ; + } +/* * Copy only in-use xstates: */ @@ -1020,11 +1031,16 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int ofif (offset + size > size_total)break ;
arch/x86/kernel/fpu/xstate.c int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of · line 1042
Quoted 10 lines
arch/x86/kernel/fpu/xstate.c +4
-0+memset (kbuf + last_off,0 , offset - last_off); + last_off = offset + size; +__copy_xstate_to_kernel (kbuf, src, offset, size, size_total); } } +memset (kbuf + last_off,0 , size_total - last_off);
Why doing all this partial zeroing? There is absolutely no point.
Either the caller clears the buffer or this function clears it right at the beginning with:
memset(kbuf, 0, min(size_total, XSAVE_MAX_SIZE));
Thanks,
tglx
Quoted 39 lines · Thomas Gleixner
Quoted 38 lines · Yu-cheng Yu
arch/x86/kernel/fpu/xstate.c +14
-0@@ -983,6 +983,7 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of {unsigned int offset, size;struct xstate_header header; +int last_off;int i;/* @@ -1006,7 +1007,17 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of__copy_xstate_to_kernel (kbuf, &header, offset, size, size_total); + last_off =0 ; +for (i =0 ; i <XFEATURE_MAX ; i++) { +/* +* Clear uninitialized area before XSAVE header. +*/ +if (i ==FIRST_EXTENDED_XFEATURE ) { +memset (kbuf + last_off,0 ,XSAVE_HDR_OFFSET - last_off); + last_off =XSAVE_HDR_OFFSET +XSAVE_HDR_SIZE ; + } +/* * Copy only in-use xstates: */ @@ -1020,11 +1031,16 @@ int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int ofif (offset + size > size_total)break ; +memset (kbuf + last_off,0 , offset - last_off); + last_off = offset + size; +__copy_xstate_to_kernel (kbuf, src, offset, size, size_total); } }
Quoted 8 lines
arch/x86/kernel/fpu/xstate.c int copy_xstate_to_kernel(void *kbuf, struct xregs_state *xsave, unsigned int of · line 1042
Quoted 1 line
arch/x86/kernel/fpu/xstate.c +1
-0+memset (kbuf + last_off,0 , size_total - last_off);Why doing all this partial zeroing? There is absolutely no point.
Either the caller clears the buffer or this function clears it right at the beginning with:
memset(kbuf, 0, min(size_total, XSAVE_MAX_SIZE));
I was concerned that the XSAVES buffer can be large, but this is not in a performance-critical path. Yes, clear it in the beginning is simpler.
Yu-cheng