From: Daniel Verkamp <dverkamp@chromium.org>
To: jeffxu@chromium.org
Cc: skhan@linuxfoundation.org, keescook@chromium.org,
akpm@linux-foundation.org, dmitry.torokhov@gmail.com,
hughd@google.com, jeffxu@google.com, jorgelo@chromium.org,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-mm@kvack.org, mnissler@chromium.org, jannh@google.com,
linux-hardening@vger.kernel.org
Subject: Re: [PATCH v3] mm/memfd: Add write seals when apply SEAL_EXEC to executable memfd
Date: Fri, 2 Dec 2022 15:23:41 -0800 [thread overview]
Message-ID: <CABVzXAkoGoypAs86EG5RsJZ=CXPu3NtTHb7_2=byQt7A7p7krQ@mail.gmail.com> (raw)
In-Reply-To: <20221202013404.163143-7-jeffxu@google.com>
On Thu, Dec 1, 2022 at 5:36 PM <jeffxu@chromium.org> wrote:
>
> From: Jeff Xu <jeffxu@chromium.org>
>
> When apply F_SEAL_EXEC to an executable memfd, add write seals also to
> prevent modification of memfd.
>
> Signed-off-by: Jeff Xu <jeffxu@chromium.org>
> ---
> mm/memfd.c | 3 +++
> tools/testing/selftests/memfd/memfd_test.c | 25 ++++++++++++++++++++++
> 2 files changed, 28 insertions(+)
>
> diff --git a/mm/memfd.c b/mm/memfd.c
> index 96dcfbfed09e..3a04c0698957 100644
> --- a/mm/memfd.c
> +++ b/mm/memfd.c
> @@ -222,6 +222,9 @@ static int memfd_add_seals(struct file *file, unsigned int seals)
> }
> }
>
> + if (seals & F_SEAL_EXEC && inode->i_mode & 0111)
> + seals |= F_ALL_SEALS;
> +
> *file_seals |= seals;
> error = 0;
>
Hi Jeff,
(Following up on some discussion on the original review, sorry for any
duplicate comments.)
Making F_SEAL_EXEC imply all seals (including F_SEAL_SEAL) seems a bit
confusing. This at least needs documentation to make it clear.
Rather than silently adding other seals, perhaps we could return an
error if the caller requests F_SEAL_EXEC but not the write seals, so
the other seals would have to be explicitly listed in the application
code. This would have the same net effect without making the
F_SEAL_EXEC operation too magical.
Additionally, if the goal is to enforce W^X, I don't think this
completely closes the gap. There will always be a period where it is
both writable and executable with this API:
1. memfd_create(MFD_EXEC). Can't use MFD_NOEXEC since that would seal
chmod(+x), so the memfd is W + X here.
2. write() code to the memfd.
3. fcntl(F_ADD_SEALS, F_SEAL_EXEC) to convert the memfd to !W + X.
I think one of the attack vectors involved the attacker waiting for
another process to create a memfd, pausing/delaying the victim
process, overwriting the memfd with their own code, and calling exec()
on it, which is still possible in the window between steps 1 and 3
with this design.
Thanks,
-- Daniel
next prev parent reply other threads:[~2022-12-02 23:24 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20221202013404.163143-1-jeffxu@google.com>
[not found] ` <20221202013404.163143-6-jeffxu@google.com>
2022-12-02 10:11 ` [PATCH v3] mm/memfd: security hook for memfd_create kernel test robot
2022-12-02 12:33 ` kernel test robot
2022-12-02 22:58 ` Kees Cook
2022-12-02 23:23 ` Jeff Xu
[not found] ` <20221202013404.163143-3-jeffxu@google.com>
2022-12-02 11:32 ` [PATCH v3] mm/memfd: add MFD_NOEXEC_SEAL and MFD_EXEC kernel test robot
2022-12-02 13:33 ` kernel test robot
2022-12-02 13:43 ` kernel test robot
2022-12-02 22:56 ` Kees Cook
2022-12-02 23:32 ` Jeff Xu
2022-12-02 22:41 ` [PATCH v3] mm/memfd: " Kees Cook
[not found] ` <20221202013404.163143-2-jeffxu@google.com>
2022-12-02 22:43 ` [PATCH v3] mm/memfd: add F_SEAL_EXEC Kees Cook
2022-12-02 22:45 ` Kees Cook
[not found] ` <20221202013404.163143-7-jeffxu@google.com>
2022-12-02 23:23 ` Daniel Verkamp [this message]
2022-12-03 2:29 ` [PATCH v3] mm/memfd: Add write seals when apply SEAL_EXEC to executable memfd Jeff Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CABVzXAkoGoypAs86EG5RsJZ=CXPu3NtTHb7_2=byQt7A7p7krQ@mail.gmail.com' \
--to=dverkamp@chromium.org \
--cc=akpm@linux-foundation.org \
--cc=dmitry.torokhov@gmail.com \
--cc=hughd@google.com \
--cc=jannh@google.com \
--cc=jeffxu@chromium.org \
--cc=jeffxu@google.com \
--cc=jorgelo@chromium.org \
--cc=keescook@chromium.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mnissler@chromium.org \
--cc=skhan@linuxfoundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox