Re: [PATCH 1/2] sigaltstack: implement SS_AUTODISARM flag

From: Andy Lutomirski
Date: Sun Mar 06 2016 - 15:10:53 EST


On Sun, Mar 6, 2016 at 12:07 PM, Andy Lutomirski <luto@xxxxxxxxxxxxxx> wrote:
> On Mon, Feb 29, 2016 at 1:29 PM, Stas Sergeev <stsp@xxxxxxx> wrote:
>> This patch implements the SS_AUTODISARM flag that can be ORed with
>> SS_ONSTACK when forming ss_flags.
>> When this flag is set, sigaltstack will be disabled when entering
>> the signal handler; more precisely, after saving sas to uc_stack.
>> When leaving the signal handler, the sigaltstack is restored by
>> uc_stack.
>> When this flag is used, it is safe to switch from sighandler with
>> swapcontext(). Without this flag, the subsequent signal will corrupt
>> the state of the switched-away sighandler.
>>
>> CC: Ingo Molnar <mingo@xxxxxxxxxx>
>> CC: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
>> CC: Richard Weinberger <richard@xxxxxx>
>> CC: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>> CC: Oleg Nesterov <oleg@xxxxxxxxxx>
>> CC: Tejun Heo <tj@xxxxxxxxxx>
>> CC: Heinrich Schuchardt <xypron.glpk@xxxxxx>
>> CC: Jason Low <jason.low2@xxxxxx>
>> CC: Andrea Arcangeli <aarcange@xxxxxxxxxx>
>> CC: Frederic Weisbecker <fweisbec@xxxxxxxxx>
>> CC: Konstantin Khlebnikov <khlebnikov@xxxxxxxxxxxxxx>
>> CC: Josh Triplett <josh@xxxxxxxxxxxxxxxx>
>> CC: "Eric W. Biederman" <ebiederm@xxxxxxxxxxxx>
>> CC: Aleksa Sarai <cyphar@xxxxxxxxxx>
>> CC: "Amanieu d'Antras" <amanieu@xxxxxxxxx>
>> CC: Paul Moore <pmoore@xxxxxxxxxx>
>> CC: Sasha Levin <sasha.levin@xxxxxxxxxx>
>> CC: Palmer Dabbelt <palmer@xxxxxxxxxxx>
>> CC: Vladimir Davydov <vdavydov@xxxxxxxxxxxxx>
>> CC: linux-kernel@xxxxxxxxxxxxxxx
>> CC: linux-api@xxxxxxxxxxxxxxx
>> CC: Andy Lutomirski <luto@xxxxxxxxxxxxxx>
>>
>> Signed-off-by: Stas Sergeev <stsp@xxxxxxxxxxxxxxxxxxxxx>
>> ---
>> include/linux/sched.h | 8 ++++++++
>> include/linux/signal.h | 4 +++-
>> include/uapi/linux/signal.h | 3 +++
>> kernel/fork.c | 2 +-
>> kernel/signal.c | 23 ++++++++++++-----------
>> 5 files changed, 27 insertions(+), 13 deletions(-)
>>
>> diff --git a/include/linux/sched.h b/include/linux/sched.h
>> index a10494a..26201cd 100644
>> --- a/include/linux/sched.h
>> +++ b/include/linux/sched.h
>> @@ -1587,6 +1587,7 @@ struct task_struct {
>>
>> unsigned long sas_ss_sp;
>> size_t sas_ss_size;
>> + unsigned sas_ss_flags;
>>
>> struct callback_head *task_works;
>>
>> @@ -2573,6 +2574,13 @@ static inline int sas_ss_flags(unsigned long sp)
>> return on_sig_stack(sp) ? SS_ONSTACK : 0;
>> }
>>
>> +static inline void sas_ss_reset(struct task_struct *p)
>> +{
>> + p->sas_ss_sp = 0;
>> + p->sas_ss_size = 0;
>> + p->sas_ss_flags = SS_DISABLE;
>> +}
>> +
>> static inline unsigned long sigsp(unsigned long sp, struct ksignal *ksig)
>> {
>> if (unlikely((ksig->ka.sa.sa_flags & SA_ONSTACK)) && ! sas_ss_flags(sp))
>> diff --git a/include/linux/signal.h b/include/linux/signal.h
>> index 92557bb..3fbe814 100644
>> --- a/include/linux/signal.h
>> +++ b/include/linux/signal.h
>> @@ -432,8 +432,10 @@ int __save_altstack(stack_t __user *, unsigned long);
>> stack_t __user *__uss = uss; \
>> struct task_struct *t = current; \
>> put_user_ex((void __user *)t->sas_ss_sp, &__uss->ss_sp); \
>> - put_user_ex(sas_ss_flags(sp), &__uss->ss_flags); \
>> + put_user_ex(t->sas_ss_flags, &__uss->ss_flags); \
>> put_user_ex(t->sas_ss_size, &__uss->ss_size); \
>> + if (t->sas_ss_flags & SS_AUTODISARM) \
>> + sas_ss_reset(t); \
>> } while (0);
>>
>> #ifdef CONFIG_PROC_FS
>> diff --git a/include/uapi/linux/signal.h b/include/uapi/linux/signal.h
>> index e1bd50c2..4691bc5 100644
>> --- a/include/uapi/linux/signal.h
>> +++ b/include/uapi/linux/signal.h
>> @@ -6,5 +6,8 @@
>>
>> #define SS_ONSTACK 1
>> #define SS_DISABLE 2
>> +#define SS_VALMASK 0xf
>
> SS_MODE_MASK, perhaps?

Actually, let's invert that.

#define SS_AUTODISARM (1U << 31)
#define SS_FLAG_BITS SS_AUTODISARM

ss_mode = ss_flags & ~SS_FLAG_BITS;

this way flag bits that are currently undefined will continue to trigger EINVAL.

--Andy