Skip to content

Commit ce184a0

Browse files
Dave MartinRussell King
authored andcommitted
ARM: 8687/1: signal: Fix unparseable iwmmxt_sigframe in uc_regspace[]
In kernels with CONFIG_IWMMXT=y running on non-iWMMXt hardware, the signal frame can be left partially uninitialised in such a way that userspace cannot parse uc_regspace[] safely. In particular, this means that the VFP registers cannot be located reliably in the signal frame when a multi_v7_defconfig kernel is run on the majority of platforms. The cause is that the uc_regspace[] is laid out statically based on the kernel config, but the decision of whether to save/restore the iWMMXt registers must be a runtime decision. To minimise breakage of software that may assume a fixed layout, this patch emits a dummy block of the same size as iwmmxt_sigframe, for non-iWMMXt threads. However, the magic and size of this block are now filled in to help parsers skip over it. A new DUMMY_MAGIC is defined for this purpose. It is probably legitimate (if non-portable) for userspace to manufacture its own sigframe for sigreturn, and there is no obvious reason why userspace should be required to insert a DUMMY_MAGIC block when running on non-iWMMXt hardware, when omitting it has worked just fine forever in other configurations. So in this case, sigreturn does not require this block to be present. Reported-by: Edmund Grimley-Evans <Edmund.Grimley-Evans@arm.com> Signed-off-by: Dave Martin <Dave.Martin@arm.com> Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
1 parent 2695835 commit ce184a0

2 files changed

Lines changed: 65 additions & 17 deletions

File tree

‎arch/arm/include/asm/ucontext.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ struct ucontext {
3535
* bytes, to prevent unpredictable padding in the signal frame.
3636
*/
3737

38+
/*
39+
* Dummy padding block: if this magic is encountered, the block should
40+
* be skipped using the corresponding size field.
41+
*/
42+
#define DUMMY_MAGIC 0xb0d9ed01
43+
3844
#ifdef CONFIG_CRUNCH
3945
#define CRUNCH_MAGIC 0x5065cf03
4046
#define CRUNCH_STORAGE_SIZE (CRUNCH_SIZE + 8)

‎arch/arm/kernel/signal.c‎

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ static int preserve_crunch_context(struct crunch_sigframe __user *frame)
4040
return __copy_to_user(frame, kframe, sizeof(*frame));
4141
}
4242

43-
static int restore_crunch_context(struct crunch_sigframe __user *frame)
43+
static int restore_crunch_context(char __user **auxp)
4444
{
45+
struct crunch_sigframe __user *frame =
46+
(struct crunch_sigframe __user *)*auxp;
4547
char kbuf[sizeof(*frame) + 8];
4648
struct crunch_sigframe *kframe;
4749

@@ -52,6 +54,7 @@ static int restore_crunch_context(struct crunch_sigframe __user *frame)
5254
if (kframe->magic != CRUNCH_MAGIC ||
5355
kframe->size != CRUNCH_STORAGE_SIZE)
5456
return -1;
57+
*auxp += CRUNCH_STORAGE_SIZE;
5558
crunch_task_restore(current_thread_info(), &kframe->storage);
5659
return 0;
5760
}
@@ -63,28 +66,64 @@ static int preserve_iwmmxt_context(struct iwmmxt_sigframe __user *frame)
6366
{
6467
char kbuf[sizeof(*frame) + 8];
6568
struct iwmmxt_sigframe *kframe;
69+
int err = 0;
6670

6771
/* the iWMMXt context must be 64 bit aligned */
6872
kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
69-
kframe->magic = IWMMXT_MAGIC;
70-
kframe->size = IWMMXT_STORAGE_SIZE;
71-
iwmmxt_task_copy(current_thread_info(), &kframe->storage);
72-
return __copy_to_user(frame, kframe, sizeof(*frame));
73+
74+
if (test_thread_flag(TIF_USING_IWMMXT)) {
75+
kframe->magic = IWMMXT_MAGIC;
76+
kframe->size = IWMMXT_STORAGE_SIZE;
77+
iwmmxt_task_copy(current_thread_info(), &kframe->storage);
78+
79+
err = __copy_to_user(frame, kframe, sizeof(*frame));
80+
} else {
81+
/*
82+
* For bug-compatibility with older kernels, some space
83+
* has to be reserved for iWMMXt even if it's not used.
84+
* Set the magic and size appropriately so that properly
85+
* written userspace can skip it reliably:
86+
*/
87+
__put_user_error(DUMMY_MAGIC, &frame->magic, err);
88+
__put_user_error(IWMMXT_STORAGE_SIZE, &frame->size, err);
89+
}
90+
91+
return err;
7392
}
7493

75-
static int restore_iwmmxt_context(struct iwmmxt_sigframe __user *frame)
94+
static int restore_iwmmxt_context(char __user **auxp)
7695
{
96+
struct iwmmxt_sigframe __user *frame =
97+
(struct iwmmxt_sigframe __user *)*auxp;
7798
char kbuf[sizeof(*frame) + 8];
7899
struct iwmmxt_sigframe *kframe;
79100

80101
/* the iWMMXt context must be 64 bit aligned */
81102
kframe = (struct iwmmxt_sigframe *)((unsigned long)(kbuf + 8) & ~7);
82103
if (__copy_from_user(kframe, frame, sizeof(*frame)))
83104
return -1;
84-
if (kframe->magic != IWMMXT_MAGIC ||
85-
kframe->size != IWMMXT_STORAGE_SIZE)
105+
106+
/*
107+
* For non-iWMMXt threads: a single iwmmxt_sigframe-sized dummy
108+
* block is discarded for compatibility with setup_sigframe() if
109+
* present, but we don't mandate its presence. If some other
110+
* magic is here, it's not for us:
111+
*/
112+
if (!test_thread_flag(TIF_USING_IWMMXT) &&
113+
kframe->magic != DUMMY_MAGIC)
114+
return 0;
115+
116+
if (kframe->size != IWMMXT_STORAGE_SIZE)
86117
return -1;
87-
iwmmxt_task_restore(current_thread_info(), &kframe->storage);
118+
119+
if (test_thread_flag(TIF_USING_IWMMXT)) {
120+
if (kframe->magic != IWMMXT_MAGIC)
121+
return -1;
122+
123+
iwmmxt_task_restore(current_thread_info(), &kframe->storage);
124+
}
125+
126+
*auxp += IWMMXT_STORAGE_SIZE;
88127
return 0;
89128
}
90129

@@ -107,8 +146,10 @@ static int preserve_vfp_context(struct vfp_sigframe __user *frame)
107146
return vfp_preserve_user_clear_hwstate(&frame->ufp, &frame->ufp_exc);
108147
}
109148

110-
static int restore_vfp_context(struct vfp_sigframe __user *frame)
149+
static int restore_vfp_context(char __user **auxp)
111150
{
151+
struct vfp_sigframe __user *frame =
152+
(struct vfp_sigframe __user *)*auxp;
112153
unsigned long magic;
113154
unsigned long size;
114155
int err = 0;
@@ -121,6 +162,7 @@ static int restore_vfp_context(struct vfp_sigframe __user *frame)
121162
if (magic != VFP_MAGIC || size != VFP_STORAGE_SIZE)
122163
return -EINVAL;
123164

165+
*auxp += size;
124166
return vfp_restore_user_hwstate(&frame->ufp, &frame->ufp_exc);
125167
}
126168

@@ -141,7 +183,7 @@ struct rt_sigframe {
141183

142184
static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
143185
{
144-
struct aux_sigframe __user *aux;
186+
char __user *aux;
145187
sigset_t set;
146188
int err;
147189

@@ -169,18 +211,18 @@ static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
169211

170212
err |= !valid_user_regs(regs);
171213

172-
aux = (struct aux_sigframe __user *) sf->uc.uc_regspace;
214+
aux = (char __user *) sf->uc.uc_regspace;
173215
#ifdef CONFIG_CRUNCH
174216
if (err == 0)
175-
err |= restore_crunch_context(&aux->crunch);
217+
err |= restore_crunch_context(&aux);
176218
#endif
177219
#ifdef CONFIG_IWMMXT
178-
if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
179-
err |= restore_iwmmxt_context(&aux->iwmmxt);
220+
if (err == 0)
221+
err |= restore_iwmmxt_context(&aux);
180222
#endif
181223
#ifdef CONFIG_VFP
182224
if (err == 0)
183-
err |= restore_vfp_context(&aux->vfp);
225+
err |= restore_vfp_context(&aux);
184226
#endif
185227

186228
return err;
@@ -286,7 +328,7 @@ setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
286328
err |= preserve_crunch_context(&aux->crunch);
287329
#endif
288330
#ifdef CONFIG_IWMMXT
289-
if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
331+
if (err == 0)
290332
err |= preserve_iwmmxt_context(&aux->iwmmxt);
291333
#endif
292334
#ifdef CONFIG_VFP

0 commit comments

Comments
 (0)