Skip to content

Commit 16c29d1

Browse files
mikeypaulusmack
authored andcommitted
powerpc: Fix swapcontext system for VSX + old ucontext size
Since VSX support was added, we now have two sizes of ucontext_t; the older, smaller size without the extra VSX state, and the new larger size with the extra VSX state. A program using the sys_swapcontext system call and supplying smaller ucontext_t structures will currently get an EINVAL error if the task has used VSX (e.g. because of calling library code that uses VSX) and the old_ctx argument is non-NULL (i.e. the program is asking for its current context to be saved). Thus the program will start getting EINVAL errors on calls that previously worked. This commit changes this behaviour so that we don't send an EINVAL in this case. It will now return the smaller context but the VSX MSR bit will always be cleared to indicate that the ucontext_t doesn't include the extra VSX state, even if the task has executed VSX instructions. Both 32 and 64 bit cases are updated. [paulus@samba.org - also fix some access_ok() and get_user() calls] Thanks to Ben Herrenschmidt for noticing this problem. Signed-off-by: Michael Neuling <mikey@neuling.org> Signed-off-by: Paul Mackerras <paulus@samba.org>
1 parent b160544 commit 16c29d1

2 files changed

Lines changed: 30 additions & 39 deletions

File tree

‎arch/powerpc/kernel/signal_32.c‎

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ inline unsigned long copy_fpr_from_user(struct task_struct *task,
410410
* altivec/spe instructions at some point.
411411
*/
412412
static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
413-
int sigret)
413+
int sigret, int ctx_has_vsx_region)
414414
{
415415
unsigned long msr = regs->msr;
416416

@@ -451,7 +451,7 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
451451
* the saved MSR value to indicate that frame->mc_vregs
452452
* contains valid data
453453
*/
454-
if (current->thread.used_vsr) {
454+
if (current->thread.used_vsr && ctx_has_vsx_region) {
455455
__giveup_vsx(current);
456456
if (copy_vsx_to_user(&frame->mc_vsregs, current))
457457
return 1;
@@ -858,11 +858,11 @@ int handle_rt_signal32(unsigned long sig, struct k_sigaction *ka,
858858
frame = &rt_sf->uc.uc_mcontext;
859859
addr = frame;
860860
if (vdso32_rt_sigtramp && current->mm->context.vdso_base) {
861-
if (save_user_regs(regs, frame, 0))
861+
if (save_user_regs(regs, frame, 0, 1))
862862
goto badframe;
863863
regs->link = current->mm->context.vdso_base + vdso32_rt_sigtramp;
864864
} else {
865-
if (save_user_regs(regs, frame, __NR_rt_sigreturn))
865+
if (save_user_regs(regs, frame, __NR_rt_sigreturn, 1))
866866
goto badframe;
867867
regs->link = (unsigned long) frame->tramp;
868868
}
@@ -936,12 +936,13 @@ long sys_swapcontext(struct ucontext __user *old_ctx,
936936
int ctx_size, int r6, int r7, int r8, struct pt_regs *regs)
937937
{
938938
unsigned char tmp;
939+
int ctx_has_vsx_region = 0;
939940

940941
#ifdef CONFIG_PPC64
941942
unsigned long new_msr = 0;
942943

943944
if (new_ctx &&
944-
__get_user(new_msr, &new_ctx->uc_mcontext.mc_gregs[PT_MSR]))
945+
get_user(new_msr, &new_ctx->uc_mcontext.mc_gregs[PT_MSR]))
945946
return -EFAULT;
946947
/*
947948
* Check that the context is not smaller than the original
@@ -956,16 +957,9 @@ long sys_swapcontext(struct ucontext __user *old_ctx,
956957
if ((ctx_size < sizeof(struct ucontext)) &&
957958
(new_msr & MSR_VSX))
958959
return -EINVAL;
959-
#ifdef CONFIG_VSX
960-
/*
961-
* If userspace doesn't provide enough room for VSX data,
962-
* but current thread has used VSX, we don't have anywhere
963-
* to store the full context back into.
964-
*/
965-
if ((ctx_size < sizeof(struct ucontext)) &&
966-
(current->thread.used_vsr && old_ctx))
967-
return -EINVAL;
968-
#endif
960+
/* Does the context have enough room to store VSX data? */
961+
if (ctx_size >= sizeof(struct ucontext))
962+
ctx_has_vsx_region = 1;
969963
#else
970964
/* Context size is for future use. Right now, we only make sure
971965
* we are passed something we understand
@@ -985,17 +979,17 @@ long sys_swapcontext(struct ucontext __user *old_ctx,
985979
*/
986980
mctx = (struct mcontext __user *)
987981
((unsigned long) &old_ctx->uc_mcontext & ~0xfUL);
988-
if (!access_ok(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
989-
|| save_user_regs(regs, mctx, 0)
982+
if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
983+
|| save_user_regs(regs, mctx, 0, ctx_has_vsx_region)
990984
|| put_sigset_t(&old_ctx->uc_sigmask, &current->blocked)
991985
|| __put_user(to_user_ptr(mctx), &old_ctx->uc_regs))
992986
return -EFAULT;
993987
}
994988
if (new_ctx == NULL)
995989
return 0;
996-
if (!access_ok(VERIFY_READ, new_ctx, sizeof(*new_ctx))
990+
if (!access_ok(VERIFY_READ, new_ctx, ctx_size)
997991
|| __get_user(tmp, (u8 __user *) new_ctx)
998-
|| __get_user(tmp, (u8 __user *) (new_ctx + 1) - 1))
992+
|| __get_user(tmp, (u8 __user *) new_ctx + ctx_size - 1))
999993
return -EFAULT;
1000994

1001995
/*
@@ -1196,11 +1190,11 @@ int handle_signal32(unsigned long sig, struct k_sigaction *ka,
11961190
goto badframe;
11971191

11981192
if (vdso32_sigtramp && current->mm->context.vdso_base) {
1199-
if (save_user_regs(regs, &frame->mctx, 0))
1193+
if (save_user_regs(regs, &frame->mctx, 0, 1))
12001194
goto badframe;
12011195
regs->link = current->mm->context.vdso_base + vdso32_sigtramp;
12021196
} else {
1203-
if (save_user_regs(regs, &frame->mctx, __NR_sigreturn))
1197+
if (save_user_regs(regs, &frame->mctx, __NR_sigreturn, 1))
12041198
goto badframe;
12051199
regs->link = (unsigned long) frame->mctx.tramp;
12061200
}

‎arch/powerpc/kernel/signal_64.c‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ static const char fmt64[] = KERN_INFO \
7474
*/
7575

7676
static long setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
77-
int signr, sigset_t *set, unsigned long handler)
77+
int signr, sigset_t *set, unsigned long handler,
78+
int ctx_has_vsx_region)
7879
{
7980
/* When CONFIG_ALTIVEC is set, we _always_ setup v_regs even if the
8081
* process never used altivec yet (MSR_VEC is zero in pt_regs of
@@ -121,7 +122,7 @@ static long setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
121122
* then out to userspace. Update v_regs to point after the
122123
* VMX data.
123124
*/
124-
if (current->thread.used_vsr) {
125+
if (current->thread.used_vsr && ctx_has_vsx_region) {
125126
__giveup_vsx(current);
126127
v_regs += ELF_NVRREG;
127128
err |= copy_vsx_to_user(v_regs, current);
@@ -282,9 +283,10 @@ int sys_swapcontext(struct ucontext __user *old_ctx,
282283
unsigned char tmp;
283284
sigset_t set;
284285
unsigned long new_msr = 0;
286+
int ctx_has_vsx_region = 0;
285287

286288
if (new_ctx &&
287-
__get_user(new_msr, &new_ctx->uc_mcontext.gp_regs[PT_MSR]))
289+
get_user(new_msr, &new_ctx->uc_mcontext.gp_regs[PT_MSR]))
288290
return -EFAULT;
289291
/*
290292
* Check that the context is not smaller than the original
@@ -299,28 +301,23 @@ int sys_swapcontext(struct ucontext __user *old_ctx,
299301
if ((ctx_size < sizeof(struct ucontext)) &&
300302
(new_msr & MSR_VSX))
301303
return -EINVAL;
302-
#ifdef CONFIG_VSX
303-
/*
304-
* If userspace doesn't provide enough room for VSX data,
305-
* but current thread has used VSX, we don't have anywhere
306-
* to store the full context back into.
307-
*/
308-
if ((ctx_size < sizeof(struct ucontext)) &&
309-
(current->thread.used_vsr && old_ctx))
310-
return -EINVAL;
311-
#endif
304+
/* Does the context have enough room to store VSX data? */
305+
if (ctx_size >= sizeof(struct ucontext))
306+
ctx_has_vsx_region = 1;
307+
312308
if (old_ctx != NULL) {
313-
if (!access_ok(VERIFY_WRITE, old_ctx, sizeof(*old_ctx))
314-
|| setup_sigcontext(&old_ctx->uc_mcontext, regs, 0, NULL, 0)
309+
if (!access_ok(VERIFY_WRITE, old_ctx, ctx_size)
310+
|| setup_sigcontext(&old_ctx->uc_mcontext, regs, 0, NULL, 0,
311+
ctx_has_vsx_region)
315312
|| __copy_to_user(&old_ctx->uc_sigmask,
316313
&current->blocked, sizeof(sigset_t)))
317314
return -EFAULT;
318315
}
319316
if (new_ctx == NULL)
320317
return 0;
321-
if (!access_ok(VERIFY_READ, new_ctx, sizeof(*new_ctx))
318+
if (!access_ok(VERIFY_READ, new_ctx, ctx_size)
322319
|| __get_user(tmp, (u8 __user *) new_ctx)
323-
|| __get_user(tmp, (u8 __user *) (new_ctx + 1) - 1))
320+
|| __get_user(tmp, (u8 __user *) new_ctx + ctx_size - 1))
324321
return -EFAULT;
325322

326323
/*
@@ -423,7 +420,7 @@ int handle_rt_signal64(int signr, struct k_sigaction *ka, siginfo_t *info,
423420
&frame->uc.uc_stack.ss_flags);
424421
err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
425422
err |= setup_sigcontext(&frame->uc.uc_mcontext, regs, signr, NULL,
426-
(unsigned long)ka->sa.sa_handler);
423+
(unsigned long)ka->sa.sa_handler, 1);
427424
err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
428425
if (err)
429426
goto badframe;

0 commit comments

Comments
 (0)