Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2804519rwb; Fri, 2 Dec 2022 15:37:37 -0800 (PST) X-Google-Smtp-Source: AA0mqf6YtALmwA0JZNrMC6vUjax/rC9rhzogrF3qH/TyuRMicyqfQBaUOFwhuHzDkMrpE6yRbayY X-Received: by 2002:a17:906:dfe8:b0:7c0:8711:7a4 with SMTP id lc8-20020a170906dfe800b007c0871107a4mr17558778ejc.667.1670024257317; Fri, 02 Dec 2022 15:37:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670024257; cv=none; d=google.com; s=arc-20160816; b=gAbBanOFjkAJJeTGY3XUwJgxCz4By8WM8t5X+zw0z2xigZcXgkgR9w/RqgJgTkkHRF ixbyMwy2fpk3ECMZZct1Rj299l8bmdDNXWZ+xZ4tcDfCWcCayljIWNLKTdMNs+AbRDai yHAY3CrTEt/r+EV1UtZ6GRVDgyXfSDgMwrIGvdNGYr4qLBV3pf1z5SdFzsX8FDDnqdu1 UGbvKuToDMrDMhSzQOk2azC4QNlFLWCWcXU8D0kc4NK63SjTEuUnfu3bWosH1b0n4Nkw yMYHkMAQ5KixqYCep1EDmHPePY9rwvlXMFF4wXGg9EKuXr5Rarxl6fyyepEk5svg5tLr /izA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ofSHfJEl53nBxTKL7kEN4Zg0gZG24eP3ZO/twC4u+1k=; b=ld3ASBJFefFdVQkn6hCS5SiNPV3EKkSB8PiZIGB3ucSjHRNnmyBwlhk0yWeo9G1Tc1 GEKR1qpMyN5UtZDiOhLDgD7OwR9Xrr1n+5sBKout4+7a2jwAD8wIAVY0XuDGn2TwdUhm 6TgQLWWg4+Tkp9/QLEND6OhJ7LFGSPkP/7kZ+40lf6mCkjiiNZdkOigJt8X3d+Qu01Yy bLa6O+iRdmpLr+UB1tqXFjctD3lCexnbUeL3DEKPJlC/LBBGuIYig4iI1SITuSFgcWjv /TU4+wvLoJdUWgglR2zBszUhnIoOXtT0q8OkpfhQ9SQbAy2jzlSYYpYKrl4Msp5QyYEa zNkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HeigGMHv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hg2-20020a1709072cc200b007c0d4287966si676966ejc.423.2022.12.02.15.37.17; Fri, 02 Dec 2022 15:37:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HeigGMHv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234508AbiLBXYN (ORCPT + 82 others); Fri, 2 Dec 2022 18:24:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234204AbiLBXYL (ORCPT ); Fri, 2 Dec 2022 18:24:11 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D03CDC2D33 for ; Fri, 2 Dec 2022 15:24:10 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id k5so6268415pjo.5 for ; Fri, 02 Dec 2022 15:24:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ofSHfJEl53nBxTKL7kEN4Zg0gZG24eP3ZO/twC4u+1k=; b=HeigGMHv29ce9DCaTPYVM3dw6G+hA6RazCRkjT3KQZLg25j/kXIJzVk1CREUf4Tnjy ObHsN/+LfiA+cHa5FGu5yq1n8lo5ofkI8FlaEx18YFuhsuonRWe5rGWezpeJ+mAErWcz bONfAWoJMb2gN0IP5FjGges9W5cH/wdhI0L3s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ofSHfJEl53nBxTKL7kEN4Zg0gZG24eP3ZO/twC4u+1k=; b=A+yIRsxRM+G3X44Se45X2dXU6fW8Avfq2POtPp+xzpImTS02OUoLoZsYkt8ZgypBsD IE4wC5lPjYBBcBaigv5j/waJDz7ea0DsDweKRBAtJc7QnFRWN5vhfY2Tsp8hpm3VV67w 8VLX3I4jrNBXNBAFSPlc20xtqjMMHJKFv1XRgqILkPnT5Py6xX970gd2Gueyrz14DTEK 3+rbHZ/OnlTikaLbvrHD/V1H7LCHgucWu7YtqJ/+J7uoP4GC4eHC+DoTmxC4d2U72pW0 Y7BRIVXB4n2s7PwU4GDspGb+CgrKW75YRUuXFy16yLfOvFkEfXzb6f+3KR+HKAWqrUf0 28uw== X-Gm-Message-State: ANoB5pmJmNZDLZHjAIY2nNqIycYA+qEDO77MOM381145qmHscAisdDc3 mX8qObSGuYyVDdULzgfFwYRGZfBXKrv9HPMJ X-Received: by 2002:a17:90b:258:b0:219:396f:8a09 with SMTP id fz24-20020a17090b025800b00219396f8a09mr27633434pjb.20.1670023450256; Fri, 02 Dec 2022 15:24:10 -0800 (PST) Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com. [209.85.216.53]) by smtp.gmail.com with ESMTPSA id q4-20020aa78424000000b00575bab45644sm5610286pfn.61.2022.12.02.15.24.08 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 Dec 2022 15:24:08 -0800 (PST) Received: by mail-pj1-f53.google.com with SMTP id b13-20020a17090a5a0d00b0021906102d05so6419972pjd.5 for ; Fri, 02 Dec 2022 15:24:08 -0800 (PST) X-Received: by 2002:a17:902:e807:b0:189:117c:fcfe with SMTP id u7-20020a170902e80700b00189117cfcfemr54885077plg.124.1670023447808; Fri, 02 Dec 2022 15:24:07 -0800 (PST) MIME-Version: 1.0 References: <20221202013404.163143-1-jeffxu@google.com> <20221202013404.163143-7-jeffxu@google.com> In-Reply-To: <20221202013404.163143-7-jeffxu@google.com> From: Daniel Verkamp Date: Fri, 2 Dec 2022 15:23:41 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3] mm/memfd: Add write seals when apply SEAL_EXEC to executable memfd 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Dec 1, 2022 at 5:36 PM wrote: > > From: Jeff Xu > > When apply F_SEAL_EXEC to an executable memfd, add write seals also to > prevent modification of memfd. > > Signed-off-by: Jeff Xu > --- > 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