Skip to content

Commit 6589654

Browse files
Dave Martinwildea01
authored andcommitted
arm64: uaccess: Fix omissions from usercopy whitelist
When the hardend usercopy support was added for arm64, it was concluded that all cases of usercopy into and out of thread_struct were statically sized and so didn't require explicit whitelisting of the appropriate fields in thread_struct. Testing with usercopy hardening enabled has revealed that this is not the case for certain ptrace regset manipulation calls on arm64. This occurs because the sizes of usercopies associated with the regset API are dynamic by construction, and because arm64 does not always stage such copies via the stack: indeed the regset API is designed to avoid the need for that by adding some bounds checking. This is currently believed to affect only the fpsimd and TLS registers. Because the whitelisted fields in thread_struct must be contiguous, this patch groups them together in a nested struct. It is also necessary to be able to determine the location and size of that struct, so rather than making the struct anonymous (which would save on edits elsewhere) or adding an anonymous union containing named and unnamed instances of the same struct (gross), this patch gives the struct a name and makes the necessary edits to code that references it (noisy but simple). Care is needed to ensure that the new struct does not contain padding (which the usercopy hardening would fail to protect). For this reason, the presence of tp2_value is made unconditional, since a padding field would be needed there in any case. This pads up to the 16-byte alignment required by struct user_fpsimd_state. Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Mark Rutland <mark.rutland@arm.com> Fixes: 9e8084d ("arm64: Implement thread_struct whitelist for hardened usercopy") Signed-off-by: Dave Martin <Dave.Martin@arm.com> Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 20b8547 commit 6589654

7 files changed

Lines changed: 72 additions & 57 deletions

File tree

‎arch/arm64/include/asm/processor.h‎

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#ifdef __KERNEL__
3636

37+
#include <linux/build_bug.h>
38+
#include <linux/stddef.h>
3739
#include <linux/string.h>
3840

3941
#include <asm/alternative.h>
@@ -103,11 +105,18 @@ struct cpu_context {
103105

104106
struct thread_struct {
105107
struct cpu_context cpu_context; /* cpu context */
106-
unsigned long tp_value; /* TLS register */
107-
#ifdef CONFIG_COMPAT
108-
unsigned long tp2_value;
109-
#endif
110-
struct user_fpsimd_state fpsimd_state;
108+
109+
/*
110+
* Whitelisted fields for hardened usercopy:
111+
* Maintainers must ensure manually that this contains no
112+
* implicit padding.
113+
*/
114+
struct {
115+
unsigned long tp_value; /* TLS register */
116+
unsigned long tp2_value;
117+
struct user_fpsimd_state fpsimd_state;
118+
} uw;
119+
111120
unsigned int fpsimd_cpu;
112121
void *sve_state; /* SVE registers, if any */
113122
unsigned int sve_vl; /* SVE vector length */
@@ -117,28 +126,31 @@ struct thread_struct {
117126
struct debug_info debug; /* debugging */
118127
};
119128

120-
/*
121-
* Everything usercopied to/from thread_struct is statically-sized, so
122-
* no hardened usercopy whitelist is needed.
123-
*/
124129
static inline void arch_thread_struct_whitelist(unsigned long *offset,
125130
unsigned long *size)
126131
{
127-
*offset = *size = 0;
132+
/* Verify that there is no padding among the whitelisted fields: */
133+
BUILD_BUG_ON(sizeof_field(struct thread_struct, uw) !=
134+
sizeof_field(struct thread_struct, uw.tp_value) +
135+
sizeof_field(struct thread_struct, uw.tp2_value) +
136+
sizeof_field(struct thread_struct, uw.fpsimd_state));
137+
138+
*offset = offsetof(struct thread_struct, uw);
139+
*size = sizeof_field(struct thread_struct, uw);
128140
}
129141

130142
#ifdef CONFIG_COMPAT
131143
#define task_user_tls(t) \
132144
({ \
133145
unsigned long *__tls; \
134146
if (is_compat_thread(task_thread_info(t))) \
135-
__tls = &(t)->thread.tp2_value; \
147+
__tls = &(t)->thread.uw.tp2_value; \
136148
else \
137-
__tls = &(t)->thread.tp_value; \
149+
__tls = &(t)->thread.uw.tp_value; \
138150
__tls; \
139151
})
140152
#else
141-
#define task_user_tls(t) (&(t)->thread.tp_value)
153+
#define task_user_tls(t) (&(t)->thread.uw.tp_value)
142154
#endif
143155

144156
/* Sync TPIDR_EL0 back to thread_struct for current */

‎arch/arm64/kernel/fpsimd.c‎

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static void sve_user_enable(void)
224224
* sets TIF_SVE.
225225
*
226226
* When stored, FPSIMD registers V0-V31 are encoded in
227-
* task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are
227+
* task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
228228
* logically zero but not stored anywhere; P0-P15 and FFR are not
229229
* stored and have unspecified values from userspace's point of
230230
* view. For hygiene purposes, the kernel zeroes them on next use,
@@ -233,9 +233,9 @@ static void sve_user_enable(void)
233233
* task->thread.sve_state does not need to be non-NULL, valid or any
234234
* particular size: it must not be dereferenced.
235235
*
236-
* * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of
237-
* whether TIF_SVE is clear or set, since these are not vector length
238-
* dependent.
236+
* * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
237+
* irrespective of whether TIF_SVE is clear or set, since these are
238+
* not vector length dependent.
239239
*/
240240

241241
/*
@@ -253,10 +253,10 @@ static void task_fpsimd_load(void)
253253

254254
if (system_supports_sve() && test_thread_flag(TIF_SVE))
255255
sve_load_state(sve_pffr(current),
256-
&current->thread.fpsimd_state.fpsr,
256+
&current->thread.uw.fpsimd_state.fpsr,
257257
sve_vq_from_vl(current->thread.sve_vl) - 1);
258258
else
259-
fpsimd_load_state(&current->thread.fpsimd_state);
259+
fpsimd_load_state(&current->thread.uw.fpsimd_state);
260260

261261
if (system_supports_sve()) {
262262
/* Toggle SVE trapping for userspace if needed */
@@ -292,9 +292,9 @@ static void task_fpsimd_save(void)
292292
}
293293

294294
sve_save_state(sve_pffr(current),
295-
&current->thread.fpsimd_state.fpsr);
295+
&current->thread.uw.fpsimd_state.fpsr);
296296
} else
297-
fpsimd_save_state(&current->thread.fpsimd_state);
297+
fpsimd_save_state(&current->thread.uw.fpsimd_state);
298298
}
299299
}
300300

@@ -405,20 +405,21 @@ static int __init sve_sysctl_init(void) { return 0; }
405405
(SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
406406

407407
/*
408-
* Transfer the FPSIMD state in task->thread.fpsimd_state to
408+
* Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
409409
* task->thread.sve_state.
410410
*
411411
* Task can be a non-runnable task, or current. In the latter case,
412412
* softirqs (and preemption) must be disabled.
413413
* task->thread.sve_state must point to at least sve_state_size(task)
414414
* bytes of allocated kernel memory.
415-
* task->thread.fpsimd_state must be up to date before calling this function.
415+
* task->thread.uw.fpsimd_state must be up to date before calling this
416+
* function.
416417
*/
417418
static void fpsimd_to_sve(struct task_struct *task)
418419
{
419420
unsigned int vq;
420421
void *sst = task->thread.sve_state;
421-
struct user_fpsimd_state const *fst = &task->thread.fpsimd_state;
422+
struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
422423
unsigned int i;
423424

424425
if (!system_supports_sve())
@@ -432,7 +433,7 @@ static void fpsimd_to_sve(struct task_struct *task)
432433

433434
/*
434435
* Transfer the SVE state in task->thread.sve_state to
435-
* task->thread.fpsimd_state.
436+
* task->thread.uw.fpsimd_state.
436437
*
437438
* Task can be a non-runnable task, or current. In the latter case,
438439
* softirqs (and preemption) must be disabled.
@@ -444,7 +445,7 @@ static void sve_to_fpsimd(struct task_struct *task)
444445
{
445446
unsigned int vq;
446447
void const *sst = task->thread.sve_state;
447-
struct user_fpsimd_state *fst = &task->thread.fpsimd_state;
448+
struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
448449
unsigned int i;
449450

450451
if (!system_supports_sve())
@@ -511,7 +512,7 @@ void fpsimd_sync_to_sve(struct task_struct *task)
511512
}
512513

513514
/*
514-
* Ensure that task->thread.fpsimd_state is up to date with respect to
515+
* Ensure that task->thread.uw.fpsimd_state is up to date with respect to
515516
* the user task, irrespective of whether SVE is in use or not.
516517
*
517518
* This should only be called by ptrace. task must be non-runnable.
@@ -526,21 +527,21 @@ void sve_sync_to_fpsimd(struct task_struct *task)
526527

527528
/*
528529
* Ensure that task->thread.sve_state is up to date with respect to
529-
* the task->thread.fpsimd_state.
530+
* the task->thread.uw.fpsimd_state.
530531
*
531532
* This should only be called by ptrace to merge new FPSIMD register
532533
* values into a task for which SVE is currently active.
533534
* task must be non-runnable.
534535
* task->thread.sve_state must point to at least sve_state_size(task)
535536
* bytes of allocated kernel memory.
536-
* task->thread.fpsimd_state must already have been initialised with
537+
* task->thread.uw.fpsimd_state must already have been initialised with
537538
* the new FPSIMD register values to be merged in.
538539
*/
539540
void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
540541
{
541542
unsigned int vq;
542543
void *sst = task->thread.sve_state;
543-
struct user_fpsimd_state const *fst = &task->thread.fpsimd_state;
544+
struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
544545
unsigned int i;
545546

546547
if (!test_tsk_thread_flag(task, TIF_SVE))
@@ -910,7 +911,7 @@ void fpsimd_thread_switch(struct task_struct *next)
910911
* upon the next return to userland.
911912
*/
912913
if (__this_cpu_read(fpsimd_last_state.st) ==
913-
&next->thread.fpsimd_state
914+
&next->thread.uw.fpsimd_state
914915
&& next->thread.fpsimd_cpu == smp_processor_id())
915916
clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
916917
else
@@ -927,8 +928,8 @@ void fpsimd_flush_thread(void)
927928

928929
local_bh_disable();
929930

930-
memset(&current->thread.fpsimd_state, 0,
931-
sizeof(current->thread.fpsimd_state));
931+
memset(&current->thread.uw.fpsimd_state, 0,
932+
sizeof(current->thread.uw.fpsimd_state));
932933
fpsimd_flush_task_state(current);
933934

934935
if (system_supports_sve()) {
@@ -987,7 +988,7 @@ void fpsimd_preserve_current_state(void)
987988

988989
/*
989990
* Like fpsimd_preserve_current_state(), but ensure that
990-
* current->thread.fpsimd_state is updated so that it can be copied to
991+
* current->thread.uw.fpsimd_state is updated so that it can be copied to
991992
* the signal frame.
992993
*/
993994
void fpsimd_signal_preserve_current_state(void)
@@ -1006,7 +1007,7 @@ static void fpsimd_bind_to_cpu(void)
10061007
struct fpsimd_last_state_struct *last =
10071008
this_cpu_ptr(&fpsimd_last_state);
10081009

1009-
last->st = &current->thread.fpsimd_state;
1010+
last->st = &current->thread.uw.fpsimd_state;
10101011
last->sve_in_use = test_thread_flag(TIF_SVE);
10111012
current->thread.fpsimd_cpu = smp_processor_id();
10121013
}
@@ -1043,7 +1044,7 @@ void fpsimd_update_current_state(struct user_fpsimd_state const *state)
10431044

10441045
local_bh_disable();
10451046

1046-
current->thread.fpsimd_state = *state;
1047+
current->thread.uw.fpsimd_state = *state;
10471048
if (system_supports_sve() && test_thread_flag(TIF_SVE))
10481049
fpsimd_to_sve(current);
10491050

‎arch/arm64/kernel/process.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static void tls_thread_flush(void)
257257
write_sysreg(0, tpidr_el0);
258258

259259
if (is_compat_task()) {
260-
current->thread.tp_value = 0;
260+
current->thread.uw.tp_value = 0;
261261

262262
/*
263263
* We need to ensure ordering between the shadow state and the
@@ -351,7 +351,7 @@ int copy_thread(unsigned long clone_flags, unsigned long stack_start,
351351
* for the new thread.
352352
*/
353353
if (clone_flags & CLONE_SETTLS)
354-
p->thread.tp_value = childregs->regs[3];
354+
p->thread.uw.tp_value = childregs->regs[3];
355355
} else {
356356
memset(childregs, 0, sizeof(struct pt_regs));
357357
childregs->pstate = PSR_MODE_EL1h;
@@ -379,7 +379,7 @@ static void tls_thread_switch(struct task_struct *next)
379379
tls_preserve_current_state();
380380

381381
if (is_compat_thread(task_thread_info(next)))
382-
write_sysreg(next->thread.tp_value, tpidrro_el0);
382+
write_sysreg(next->thread.uw.tp_value, tpidrro_el0);
383383
else if (!arm64_kernel_unmapped_at_el0())
384384
write_sysreg(0, tpidrro_el0);
385385

‎arch/arm64/kernel/ptrace.c‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ static int __fpr_get(struct task_struct *target,
629629

630630
sve_sync_to_fpsimd(target);
631631

632-
uregs = &target->thread.fpsimd_state;
632+
uregs = &target->thread.uw.fpsimd_state;
633633

634634
return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
635635
start_pos, start_pos + sizeof(*uregs));
@@ -655,19 +655,19 @@ static int __fpr_set(struct task_struct *target,
655655
struct user_fpsimd_state newstate;
656656

657657
/*
658-
* Ensure target->thread.fpsimd_state is up to date, so that a
658+
* Ensure target->thread.uw.fpsimd_state is up to date, so that a
659659
* short copyin can't resurrect stale data.
660660
*/
661661
sve_sync_to_fpsimd(target);
662662

663-
newstate = target->thread.fpsimd_state;
663+
newstate = target->thread.uw.fpsimd_state;
664664

665665
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate,
666666
start_pos, start_pos + sizeof(newstate));
667667
if (ret)
668668
return ret;
669669

670-
target->thread.fpsimd_state = newstate;
670+
target->thread.uw.fpsimd_state = newstate;
671671

672672
return ret;
673673
}
@@ -692,7 +692,7 @@ static int tls_get(struct task_struct *target, const struct user_regset *regset,
692692
unsigned int pos, unsigned int count,
693693
void *kbuf, void __user *ubuf)
694694
{
695-
unsigned long *tls = &target->thread.tp_value;
695+
unsigned long *tls = &target->thread.uw.tp_value;
696696

697697
if (target == current)
698698
tls_preserve_current_state();
@@ -705,13 +705,13 @@ static int tls_set(struct task_struct *target, const struct user_regset *regset,
705705
const void *kbuf, const void __user *ubuf)
706706
{
707707
int ret;
708-
unsigned long tls = target->thread.tp_value;
708+
unsigned long tls = target->thread.uw.tp_value;
709709

710710
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
711711
if (ret)
712712
return ret;
713713

714-
target->thread.tp_value = tls;
714+
target->thread.uw.tp_value = tls;
715715
return ret;
716716
}
717717

@@ -842,7 +842,7 @@ static int sve_get(struct task_struct *target,
842842
start = end;
843843
end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
844844
ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
845-
&target->thread.fpsimd_state.fpsr,
845+
&target->thread.uw.fpsimd_state.fpsr,
846846
start, end);
847847
if (ret)
848848
return ret;
@@ -941,7 +941,7 @@ static int sve_set(struct task_struct *target,
941941
start = end;
942942
end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
943943
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
944-
&target->thread.fpsimd_state.fpsr,
944+
&target->thread.uw.fpsimd_state.fpsr,
945945
start, end);
946946

947947
out:
@@ -1169,7 +1169,7 @@ static int compat_vfp_get(struct task_struct *target,
11691169
compat_ulong_t fpscr;
11701170
int ret, vregs_end_pos;
11711171

1172-
uregs = &target->thread.fpsimd_state;
1172+
uregs = &target->thread.uw.fpsimd_state;
11731173

11741174
if (target == current)
11751175
fpsimd_preserve_current_state();
@@ -1202,7 +1202,7 @@ static int compat_vfp_set(struct task_struct *target,
12021202
compat_ulong_t fpscr;
12031203
int ret, vregs_end_pos;
12041204

1205-
uregs = &target->thread.fpsimd_state;
1205+
uregs = &target->thread.uw.fpsimd_state;
12061206

12071207
vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
12081208
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
@@ -1225,7 +1225,7 @@ static int compat_tls_get(struct task_struct *target,
12251225
const struct user_regset *regset, unsigned int pos,
12261226
unsigned int count, void *kbuf, void __user *ubuf)
12271227
{
1228-
compat_ulong_t tls = (compat_ulong_t)target->thread.tp_value;
1228+
compat_ulong_t tls = (compat_ulong_t)target->thread.uw.tp_value;
12291229
return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
12301230
}
12311231

@@ -1235,13 +1235,13 @@ static int compat_tls_set(struct task_struct *target,
12351235
const void __user *ubuf)
12361236
{
12371237
int ret;
1238-
compat_ulong_t tls = target->thread.tp_value;
1238+
compat_ulong_t tls = target->thread.uw.tp_value;
12391239

12401240
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
12411241
if (ret)
12421242
return ret;
12431243

1244-
target->thread.tp_value = tls;
1244+
target->thread.uw.tp_value = tls;
12451245
return ret;
12461246
}
12471247

@@ -1538,7 +1538,7 @@ long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
15381538
break;
15391539

15401540
case COMPAT_PTRACE_GET_THREAD_AREA:
1541-
ret = put_user((compat_ulong_t)child->thread.tp_value,
1541+
ret = put_user((compat_ulong_t)child->thread.uw.tp_value,
15421542
(compat_ulong_t __user *)datap);
15431543
break;
15441544

0 commit comments

Comments
 (0)