2020-10-22 10:14:26

by Topi Miettinen

[permalink] [raw]
Subject: Re: [systemd-devel] BTI interaction between seccomp filters in systemd and glibc mprotect calls, causing service failures

On 22.10.2020 12.31, Catalin Marinas wrote:
> On Thu, Oct 22, 2020 at 10:38:23AM +0200, Lennart Poettering wrote:
>> On Do, 22.10.20 09:29, Szabolcs Nagy ([email protected]) wrote:
>>>>> The dynamic loader has to process the LOAD segments to get to the ELF
>>>>> note that says to enable BTI. Maybe we could do a first pass and load
>>>>> only the segments that cover notes. But that requires lots of changes
>>>>> to generic code in the loader.
>>>>
>>>> What if the loader always enabled BTI for PROT_EXEC pages, but then when
>>>> discovering that this was a mistake, mprotect() the pages without BTI? Then
>>>> both BTI and MDWX would work and the penalty of not getting MDWX would fall
>>>> to non-BTI programs. What's the expected proportion of BTI enabled code vs.
>>>> disabled in the future, is it perhaps expected that a distro would enable
>>>> the flag globally so eventually only a few legacy programs might be
>>>> unprotected?
>>>
>>> i thought mprotect(PROT_EXEC) would get filtered
>>> with or without bti, is that not the case?
>>
>> We can adjust the filter in systemd to match any combination of
>> flags to allow and to deny.
>
> Yes but Szabolcs' point to Topi was that if we can adjust the filters to
> allow mprotect(PROT_EXEC), why not allow mprotect(PROT_EXEC|PROT_BTI)
> instead? Anyway, I see the MDWX and BTI as complementary policies so
> ideally we shouldn't have to choose between one or the other. If we
> allow mprotect(PROT_EXEC), that would override MDWX and also disable
> BTI.

Allowing mprotect(PROT_EXEC|PROT_BTI) would mean that all you need to
circumvent MDWX is to add PROT_BTI flag. I'd suggest getting the flags
right at mmap() time or failing that, reverting the PROT_BTI for legacy
programs later.

Could the kernel tell the loader of the BTI situation with auxiliary
vectors? Then it would be easy for the loader to always use the best
mmap() flags without ever needing to mprotect().

-Topi


2020-10-22 10:29:02

by Florian Weimer

[permalink] [raw]
Subject: Re: [systemd-devel] BTI interaction between seccomp filters in systemd and glibc mprotect calls, causing service failures

* Topi Miettinen:

> Allowing mprotect(PROT_EXEC|PROT_BTI) would mean that all you need to
> circumvent MDWX is to add PROT_BTI flag. I'd suggest getting the flags
> right at mmap() time or failing that, reverting the PROT_BTI for
> legacy programs later.
>
> Could the kernel tell the loader of the BTI situation with auxiliary
> vectors? Then it would be easy for the loader to always use the best
> mmap() flags without ever needing to mprotect().

I think what we want is a mprotect2 call with a flags argument (separate
from protection flags) that tells the kernel that the request *removes*
protection flags and should fail otherwise. seccomp could easily filter
that then.

But like the other proposals, the migration story isn't great. You
would need kernel and seccomp/systemd etc. updates before glibc starts
working, even if glibc has a fallback from mprotect2 to mprotect
(because the latter would be blocked).

Thanks,
Florian
--
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill