Skip to content

Commit 9c36e59

Browse files
Brian Gerstsuryasaimadhu
authored andcommitted
x86/signal: Merge get_sigframe()
Adapt the native get_sigframe() function so that the compat signal code can use it. Signed-off-by: Brian Gerst <brgerst@gmail.com> Signed-off-by: Borislav Petkov <bp@suse.de> Acked-by: "Eric W. Biederman" <ebiederm@xmission.com> Link: https://lore.kernel.org/r/20220606203802.158958-6-brgerst@gmail.com Signed-off-by: Borislav Petkov <bp@suse.de>
1 parent 695c39b commit 9c36e59

3 files changed

Lines changed: 42 additions & 76 deletions

File tree

arch/x86/ia32/ia32_signal.c

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -196,40 +196,6 @@ do { \
196196
goto label; \
197197
} while(0)
198198

199-
/*
200-
* Determine which stack to use..
201-
*/
202-
static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
203-
size_t frame_size,
204-
void __user **fpstate)
205-
{
206-
unsigned long sp, fx_aligned, math_size;
207-
208-
/* Default to using normal stack */
209-
sp = regs->sp;
210-
211-
/* This is the X/Open sanctioned signal stack switching. */
212-
if (ksig->ka.sa.sa_flags & SA_ONSTACK)
213-
sp = sigsp(sp, ksig);
214-
/* This is the legacy signal stack switching. */
215-
else if (regs->ss != __USER_DS &&
216-
!(ksig->ka.sa.sa_flags & SA_RESTORER) &&
217-
ksig->ka.sa.sa_restorer)
218-
sp = (unsigned long) ksig->ka.sa.sa_restorer;
219-
220-
sp = fpu__alloc_mathframe(sp, 1, &fx_aligned, &math_size);
221-
*fpstate = (struct _fpstate_32 __user *) sp;
222-
if (!copy_fpstate_to_sigframe(*fpstate, (void __user *)fx_aligned,
223-
math_size))
224-
return (void __user *) -1L;
225-
226-
sp -= frame_size;
227-
/* Align the stack pointer according to the i386 ABI,
228-
* i.e. so that on function entry ((sp + 4) & 15) == 0. */
229-
sp = ((sp + 4) & -16ul) - 4;
230-
return (void __user *) sp;
231-
}
232-
233199
int ia32_setup_frame(struct ksignal *ksig, struct pt_regs *regs)
234200
{
235201
compat_sigset_t *set = (compat_sigset_t *) sigmask_to_save();

arch/x86/include/asm/sighandling.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@
1515

1616
void signal_fault(struct pt_regs *regs, void __user *frame, char *where);
1717

18+
void __user *
19+
get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size,
20+
void __user **fpstate);
21+
1822
#endif /* _ASM_X86_SIGHANDLING_H */

arch/x86/kernel/signal.c

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@
4848
#include <asm/sigframe.h>
4949
#include <asm/signal.h>
5050

51+
static inline int is_ia32_compat_frame(struct ksignal *ksig)
52+
{
53+
return IS_ENABLED(CONFIG_IA32_EMULATION) &&
54+
ksig->ka.sa.sa_flags & SA_IA32_ABI;
55+
}
56+
57+
static inline int is_ia32_frame(struct ksignal *ksig)
58+
{
59+
return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
60+
}
61+
62+
static inline int is_x32_frame(struct ksignal *ksig)
63+
{
64+
return IS_ENABLED(CONFIG_X86_X32_ABI) &&
65+
ksig->ka.sa.sa_flags & SA_X32_ABI;
66+
}
67+
5168
#ifdef CONFIG_X86_64
5269
/*
5370
* If regs->ss will cause an IRET fault, change it. Otherwise leave it
@@ -223,24 +240,12 @@ do { \
223240
/*
224241
* Determine which stack to use..
225242
*/
226-
static unsigned long align_sigframe(unsigned long sp)
227-
{
228-
#ifdef CONFIG_X86_32
229-
/*
230-
* Align the stack pointer according to the i386 ABI,
231-
* i.e. so that on function entry ((sp + 4) & 15) == 0.
232-
*/
233-
sp = ((sp + 4) & -FRAME_ALIGNMENT) - 4;
234-
#else /* !CONFIG_X86_32 */
235-
sp = round_down(sp, FRAME_ALIGNMENT) - 8;
236-
#endif
237-
return sp;
238-
}
239-
240-
static void __user *
241-
get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
243+
void __user *
244+
get_sigframe(struct ksignal *ksig, struct pt_regs *regs, size_t frame_size,
242245
void __user **fpstate)
243246
{
247+
struct k_sigaction *ka = &ksig->ka;
248+
int ia32_frame = is_ia32_frame(ksig);
244249
/* Default to using normal stack */
245250
bool nested_altstack = on_sig_stack(regs->sp);
246251
bool entering_altstack = false;
@@ -249,7 +254,7 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
249254
unsigned long buf_fx = 0;
250255

251256
/* redzone */
252-
if (IS_ENABLED(CONFIG_X86_64))
257+
if (!ia32_frame)
253258
sp -= 128;
254259

255260
/* This is the X/Open sanctioned signal stack switching. */
@@ -263,7 +268,7 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
263268
sp = current->sas_ss_sp + current->sas_ss_size;
264269
entering_altstack = true;
265270
}
266-
} else if (IS_ENABLED(CONFIG_X86_32) &&
271+
} else if (ia32_frame &&
267272
!nested_altstack &&
268273
regs->ss != __USER_DS &&
269274
!(ka->sa.sa_flags & SA_RESTORER) &&
@@ -273,11 +278,19 @@ get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
273278
entering_altstack = true;
274279
}
275280

276-
sp = fpu__alloc_mathframe(sp, IS_ENABLED(CONFIG_X86_32),
277-
&buf_fx, &math_size);
281+
sp = fpu__alloc_mathframe(sp, ia32_frame, &buf_fx, &math_size);
278282
*fpstate = (void __user *)sp;
279283

280-
sp = align_sigframe(sp - frame_size);
284+
sp -= frame_size;
285+
286+
if (ia32_frame)
287+
/*
288+
* Align the stack pointer according to the i386 ABI,
289+
* i.e. so that on function entry ((sp + 4) & 15) == 0.
290+
*/
291+
sp = ((sp + 4) & -FRAME_ALIGNMENT) - 4;
292+
else
293+
sp = round_down(sp, FRAME_ALIGNMENT) - 8;
281294

282295
/*
283296
* If we are on the alternate signal stack and would overflow it, don't.
@@ -331,7 +344,7 @@ __setup_frame(struct ksignal *ksig, struct pt_regs *regs)
331344
void __user *restorer;
332345
void __user *fp = NULL;
333346

334-
frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
347+
frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
335348

336349
if (!user_access_begin(frame, sizeof(*frame)))
337350
return -EFAULT;
@@ -386,7 +399,7 @@ static int __setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
386399
void __user *restorer;
387400
void __user *fp = NULL;
388401

389-
frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
402+
frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
390403

391404
if (!user_access_begin(frame, sizeof(*frame)))
392405
return -EFAULT;
@@ -469,7 +482,7 @@ static int __setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
469482
if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
470483
return -EFAULT;
471484

472-
frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
485+
frame = get_sigframe(ksig, regs, sizeof(struct rt_sigframe), &fp);
473486
uc_flags = frame_uc_flags(regs);
474487

475488
if (!user_access_begin(frame, sizeof(*frame)))
@@ -572,7 +585,7 @@ static int x32_setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
572585
if (!(ksig->ka.sa.sa_flags & SA_RESTORER))
573586
return -EFAULT;
574587

575-
frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fp);
588+
frame = get_sigframe(ksig, regs, sizeof(*frame), &fp);
576589

577590
uc_flags = frame_uc_flags(regs);
578591

@@ -742,23 +755,6 @@ unsigned long get_sigframe_size(void)
742755
return max_frame_size;
743756
}
744757

745-
static inline int is_ia32_compat_frame(struct ksignal *ksig)
746-
{
747-
return IS_ENABLED(CONFIG_IA32_EMULATION) &&
748-
ksig->ka.sa.sa_flags & SA_IA32_ABI;
749-
}
750-
751-
static inline int is_ia32_frame(struct ksignal *ksig)
752-
{
753-
return IS_ENABLED(CONFIG_X86_32) || is_ia32_compat_frame(ksig);
754-
}
755-
756-
static inline int is_x32_frame(struct ksignal *ksig)
757-
{
758-
return IS_ENABLED(CONFIG_X86_X32_ABI) &&
759-
ksig->ka.sa.sa_flags & SA_X32_ABI;
760-
}
761-
762758
static int
763759
setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
764760
{

0 commit comments

Comments
 (0)