Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp469914pxx; Mon, 26 Oct 2020 12:41:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy5yXZxoPj1WGJzhQrr5XugatOu6dOK4JBrF2juiit+3/d4HlyDbK1FfkRVfx3Hu5d8Awd2 X-Received: by 2002:a17:906:141b:: with SMTP id p27mr17967294ejc.205.1603741273971; Mon, 26 Oct 2020 12:41:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603741273; cv=none; d=google.com; s=arc-20160816; b=Qq4ynfl1SVlddR4mnuOZU3POjgRk0nJLfEvwL0tDHZcIgnW217pXoCKSk1edBvEn9h E4HzbX3PYikvhaAlv3WWwYuZl+D63gCYE+NjthiyMJah1NoKGEEnAtqr+l5asLTUO3xn gBmJHVCfOeYIMMGC1v06FY0VX8mdZphXw+pvnpypQoSiLNMlxSja67+E2n/gdb6TkQWs PLXR+EBWhs483V5JskFatkfBK1ci5NR10ptblnri/7GocM/fRoYQmC7Qg4n63VVx/fmz QfbJUCZvt/oDPyzKflyqjUN+aptaHaxXWS7AgS93EcJXaf+k50mCS4CsIJitKUhokVSq Ijbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=bYYpq/ODtlAp6N9v/jOmvwi4a5DwPQlK76zjEaH5y0U=; b=BN1MEvvcVFXmut/6dKdO5/9GZZMUdi1o6m8I9SBPy7Dnb+SOFS+jRKoAtnvtsGkzkp zpG7Gx8/NVu9j1oXL0o8pUedKJh/WZ5f9j5TczR+z7HS3GDHyTntQ5g2igqVUdF2k/Cm Ww1xK4TGuv6nWNycf2qDkSfx68PqzTtfBgeSKV/DHiX+mrN4LoZay8xgEf7nzVlc73lj 2nQPWXrNtl6G2dBaF58sXgiLCyxJ5A/znt+FNyw4Xjv8PwEjQepn2Wl25zVhShx1qP8b tja2tf8uK3eH5bqeN41UlMYwINO3wKWBtPy/7B1z5q2c8aKAqbpIBvmmLvK7nNkqkOvh CPMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=hmCYvw9T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n9si7598452edy.248.2020.10.26.12.40.51; Mon, 26 Oct 2020 12:41:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=hmCYvw9T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1786231AbgJZQkV (ORCPT + 99 others); Mon, 26 Oct 2020 12:40:21 -0400 Received: from mail-lf1-f67.google.com ([209.85.167.67]:38597 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1741277AbgJZQkV (ORCPT ); Mon, 26 Oct 2020 12:40:21 -0400 Received: by mail-lf1-f67.google.com with SMTP id c141so12916429lfg.5 for ; Mon, 26 Oct 2020 09:40:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=bYYpq/ODtlAp6N9v/jOmvwi4a5DwPQlK76zjEaH5y0U=; b=hmCYvw9TPflPd9H/DK+zYCiLVpY5wiSnYliiGK03r7yuumwzjDfiWUfaETFJkuFXlL QHuf3ENiQc9ybhv0U9lUsM06Seadp7EyAGOaVTxST1ou+WDqigqW997mKZ+cvV1uKUPP e9e/u9S4WZl/5w277vCvBfQHhABwox7LdWIN5koZP7tYuaA5zLWQN9uidAWWVvoLUJYW +w54kOvfmg0XpZYA3gMbNkug0gaWI0CMGdRGtc4zxGxSP8+d6YiFb39REgpsudDcnjBd Ju4exspOqQOM1aJ+gMqmHnyYonF/xDUYbq0piU/yNKW0E67Amo6MdYhnN7/PnKmB9mQG WRAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=bYYpq/ODtlAp6N9v/jOmvwi4a5DwPQlK76zjEaH5y0U=; b=R6RbDX9Qqpqir7krevRZECCX83z+vv9zMcaGlfFFIBUIf7azWqzx9BV8hn09wfBdZ/ DzkQqQn6c1q4bb0LLapp7I5cQHFmqIIxPdEZl5CaoEPGrjRsjwyf7frSIwNp0qK2YJD2 1SkzEbvnPMuMNWCzNu9mgb9+GxjsolWFqz7j0c+E/ljpqAPkE7FDANrmqJfTlHK0/9c/ Oh11PN5GgOtLJDDH3G6Q19MSLbvc87XvYSyl0CwrlkhlQQ5T+pFxzc98kO67zZzPnFDF gkXwECex9zcVr7AZoG10ojbe/uBRZ+ScHvdrB6c0yku+yPi+i/3IAf/tixifFNY25gU5 V0nQ== X-Gm-Message-State: AOAM530UHQ9vOdo9XM8XyNzzKKDeFboaiJLEvNBetTCnTAZfThGuRbEe sRtMabFlbBTnySOHy5aBqwA= X-Received: by 2002:a19:c3cd:: with SMTP id t196mr4934402lff.501.1603730418621; Mon, 26 Oct 2020 09:40:18 -0700 (PDT) Received: from [192.168.1.112] (88-114-211-119.elisa-laajakaista.fi. [88.114.211.119]) by smtp.gmail.com with ESMTPSA id m22sm1091484lfq.12.2020.10.26.09.40.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 26 Oct 2020 09:40:17 -0700 (PDT) Subject: Re: BTI interaction between seccomp filters in systemd and glibc mprotect calls, causing service failures To: Dave Martin , Jeremy Linton Cc: "linux-arm-kernel@lists.infradead.org" , libc-alpha@sourceware.org, systemd-devel@lists.freedesktop.org, "linux-kernel@vger.kernel.org" , Mark Rutland , Kees Cook , Catalin Marinas , Will Deacon , Mark Brown References: <8584c14f-5c28-9d70-c054-7c78127d84ea@arm.com> <20201026162410.GB27285@arm.com> From: Topi Miettinen Message-ID: Date: Mon, 26 Oct 2020 18:39:55 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20201026162410.GB27285@arm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 26.10.2020 18.24, Dave Martin wrote: > On Wed, Oct 21, 2020 at 10:44:46PM -0500, Jeremy Linton via Libc-alpha wrote: >> Hi, >> >> There is a problem with glibc+systemd on BTI enabled systems. Systemd >> has a service flag "MemoryDenyWriteExecute" which uses seccomp to deny >> PROT_EXEC changes. Glibc enables BTI only on segments which are marked as >> being BTI compatible by calling mprotect PROT_EXEC|PROT_BTI. That call is >> caught by the seccomp filter, resulting in service failures. >> >> So, at the moment one has to pick either denying PROT_EXEC changes, or BTI. >> This is obviously not desirable. >> >> Various changes have been suggested, replacing the mprotect with mmap calls >> having PROT_BTI set on the original mapping, re-mmapping the segments, >> implying PROT_EXEC on mprotect PROT_BTI calls when VM_EXEC is already set, >> and various modification to seccomp to allow particular mprotect cases to >> bypass the filters. In each case there seems to be an undesirable attribute >> to the solution. >> >> So, whats the best solution? > > Unrolling this discussion a bit, this problem comes from a few sources: > > 1) systemd is trying to implement a policy that doesn't fit SECCOMP > syscall filtering very well. > > 2) The program is trying to do something not expressible through the > syscall interface: really the intent is to set PROT_BTI on the page, > with no intent to set PROT_EXEC on any page that didn't already have it > set. > > > This limitation of mprotect() was known when I originally added PROT_BTI, > but at that time we weren't aware of a clear use case that would fail. > > > Would it now help to add something like: > > int mchangeprot(void *addr, size_t len, int old_flags, int new_flags) > { > int ret = -EINVAL; > mmap_write_lock(current->mm); > if (all vmas in [addr .. addr + len) have > their mprotect flags set to old_flags) { > > ret = mprotect(addr, len, new_flags); > } > > mmap_write_unlock(current->mm); > return ret; > } > > > libc would now be able to do > > mchangeprot(addr, len, PROT_EXEC | PROT_READ, > PROT_EXEC | PROT_READ | PROT_BTI); > > while systemd's MDWX filter would reject the call if > > (new_flags & PROT_EXEC) && > (!(old_flags & PROT_EXEC) || (new_flags & PROT_WRITE) > > > > This won't magically fix current code, but something along these lines > might be better going forward. > > > Thoughts? Looks good to me. -Topi