From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 21992 invoked from network); 10 Aug 2020 16:35:03 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 10 Aug 2020 16:35:03 -0000 Received: (qmail 5899 invoked by uid 550); 10 Aug 2020 16:35:01 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 5881 invoked from network); 10 Aug 2020 16:35:00 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1597077289; bh=IU8onlDLltESfzLxZ3js0TUyl23qq6rHCSYhVx9Zweo=; h=X-UI-Sender-Class:Date:From:To:Subject:References:In-Reply-To; b=N4MG86QP+IlLLrFYSbyXVfqMEo90fE+/BTS3vCZx5+wiB9cHpadjf5Af3Iy4w5PnN n8bJFgfLHGeQpqDvUykmcTIltSLsZDF/MOWghnPzu2E5n//ekf5wFsww69AWxks40J dqa7FrPffagNVM3/DIXG4GgLMkOQDabit8YmmhyE= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Date: Mon, 10 Aug 2020 18:34:48 +0200 From: Markus Wichmann To: musl@lists.openwall.com Message-ID: <20200810163448.GB10312@voyager> References: <20200809003958.GE3265@brightrain.aerifal.cx> <20200809075430.GA10312@voyager> <20200810000622.GF3265@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200810000622.GF3265@brightrain.aerifal.cx> User-Agent: Mutt/1.9.4 (2018-02-28) X-Provags-ID: V03:K1:c2w2nXWw5dVhPQ/UUAJ5mfDzBr13dF5rn85roiPiXMrdZ1TcnIj 1POPnaQjBOMySJpq0j2wW5ovxILk1SUPmWesAL9CuLJPEFfmJSmSxZtV5ybWs0jJSYMFaV1 Bz4LCUu3LK0X7MAKl2KiuPSuRvoJUCyjeAr3agaS0Pv1dndFzB2kbVt/hU2qCwpyX+0r7JI 4A5o/DI5vxcuYBSDE0jLA== X-UI-Out-Filterresults: notjunk:1;V03:K0:PCIYlpG3Tsw=:npPRkgYro8wr6zSXHi4wf+ c3ESPN6tUdJ02yeZiwCXZH+Lg1mP/VTjThzIKHLm371lcnHkiKZ2LBjpPjei6Z8qH+zsUE2HO Ykw6odTv5qz1C33LSpsMnygF8m8pszOJOXoRPOMJz+OEsV7pOhSPGP/zIlhELVrV3UkyGG2qz FJ509yXtLuBKeI+EVZegxBXJQMQ6xoWgAem5jOsNRUYxWLujElXFwtEO10jJvcI5Hx65jPLZU FyZUck7lino+1O434BEl1KljpEsi3iA2BOIMzWdPf/w01lqHMMP4L77PcRr6pTsHJtGnCvKVt 0Odr9/z4ltJcj85Y+/iPRQUcR235vYLp9zPpTyhIy/0eXvDn1Je8KqAJt2LH3GGUVVZmXDz5i KokwLeHdYwEmvNNXPHGD42Soo0XlOpeQj/BuAwJ84SaEVbXtEXn7XNAcFk/qr68p1nqwTmp/6 3MmIz/knZ17+h6CbWjUldEdpbgIAYW4sPVqFgrtZ4QKD/E5txgUcWZAeltklQwe5dt7HKJ3mb hK/IK/O30oXp+vSbQbArbTEEV003vWbCIv0fb0gbxpiAICeUD/auqbIyrF0fizLdYRgCdrrkI kVVYs3xPwlofAEVnAAP2D7XJBicSce/HPNiJWEf69x3eSDhtdKuoIoxnibxdXWKxhj3gvtchs W8q+uOiF5KsawHqwILdYkoimaUnsyIRuuA887d0LWxM6HCACD6uakoJa1g4LoDguZJi0LUuJQ UZIclJtDuDMWYpdR7qjUSHfiWh0SuEud/s36IR7f75+suVA1xqLt/7o8bJLjS7tyEs+vr6T/F rkIh0ZN1vxi8oDh8zhsJi5TZBX7lKgfLc6QohOQRg/FVdOQwtp76UBLfmBsBGEsx4gxlarQ4m gyB+uwxw1NGBrKyo2JFRdKriQ8SAPtlxiV7dpty29UnKI5EfSYAEtCXgMaFLN0YInGWX8IFdD ByjDhkAJ/HykT9yukq7d/z1uPpbx6Iy6K3olboU3mjTMLT0WS5E61K55XeQ7KAp/2W5WcyAyV lKxxrRPEQxHj51A7rGlxUwixVwlkjKS72SIctSDki1B6IMVOJa5eCZkQwX5JkPrYmdPYH2N8y Wa/mTXDdKpicYMSnEY8xpEHNbtmoIACu1HUJRG3azkXCm28TRVHEBoseRPV5WQ/LrKoW2uPNf zMf7aS3lX7NDHSMT9p4Bm69bibIqBVuJdUMJo3NXqfvKaz26wDl0sN/sDVHgWnKXT/uRfTNQG e/Uv423WLASiJGB6+ Content-Transfer-Encoding: quoted-printable Subject: Re: [musl] Revisiting sigaltstack and implementation-internal signals On Sun, Aug 09, 2020 at 08:06:22PM -0400, Rich Felker wrote: > My understanding is that SA_ONSTACK is just reported by the kernel if > the current stack pointer is inside the alternate stack. If the > application has moved off that stack and a signal arrives, it has > nowhere to know "where in the alternate stack it was" or that the > alternate stack was even already in use, and clobbers it from the > beginning if a new signal arrives that is to execute on the alternate > stack. > > If you think this understanding is incorrect, we should research/test. > > Rich I thought the kernel would set the flag SS_ONSTACK in the sigaltstack flags when switching to the altstack, and only remove it through sigaltstack() or sigreturn(). However, this was merely based on what understanding I managed to absorb from the manpages. So I had a look at the Linux kernel code, specifically signal delivery. Now, that is arch-specific code. But I had a look at two architectures and in both of them you are right, so I guess the other architectures will handle it the same way. There is a function called get_sigframe(), that is present both in arch/x86/kernel/signal.c and arch/powerpc/kernel/signal.c. It determines where the signal frame is. In the x86 version, we can see that it will choose the current stack by default, but it will choose the top of the altstack if SA_ONSTACK is set in the sigaction flags and the current ss_flags are 0. How are those calculated? Partly by calling on_sig_stack(), which will test if the current SP is on the signal stack. So it is not a sticky note in the kernel, it is indeed calculated from SP. In the PowerPC version, get_sigframe() calls sigsp(), which basically does the same thing as above, but streamlined. So yeah, if you are executing on altstack, and then you call a coroutine on another stack, you just lost your altstack contents. And if another signal arrives, the altstack will be clobbered again. Therefore mixing coroutines and altstack is not safe, unless all signals with SA_ONSTACK are blocked while the altstack is live but not in active use. Which is impossible once musl uses altstack as well. Ciao, Markus