Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1288810rwr; Fri, 5 May 2023 11:45:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4Bb0aO4UPv8tdgzPW7EIjf4j3kqaOagBinoOlOLmzgdCr4nuMWRWB4jEEl929st/xi/oA9 X-Received: by 2002:a05:6a20:a586:b0:f1:377a:b762 with SMTP id bc6-20020a056a20a58600b000f1377ab762mr2863413pzb.33.1683312307605; Fri, 05 May 2023 11:45:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683312307; cv=none; d=google.com; s=arc-20160816; b=CYJHLtRcORRfKFe1ieo3NOZ92mXoTNCGRk9mZVxsqhXTpj36h0eBX/+Q0rCI77PZIk XvlT3ET51v/VPk9wde4yTkNAO7yyb/467I055EQJ4lW/bOn9lR1jcRfT0zS2hG+F0v7o kSGx+UIt0Z8BCGPtjvj0TSiNB5wv41UNE/yiI0fxW6YOFYtzod7T80csqW7J4DtAfLAZ 9LFp4bSA7w14Pd3/uq9/4OGbdjbc4SGQ7EuZ70zeRwN8YSYvy4Oys12AHoFFk6TkORKc SgGzo8v2aJtsm27klmzonucFQklYPBkxGpCd+oEtQAVTcU2wSTJnf2asT8KTZzToICQH iD1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=m6nJBP6OFS8qeAfvl5bcoKbHo59PNNS9O0BzUFi/oF0=; b=zmb/83kzPw8eh3YiH6wCeZTik3CzR/Xb20el2caqYfKMtv1sIiI2tOY3KFbw1ICGy2 K4mqpP3nU2lC4qUW/Wvt1qSfk2nEPWWwUTBqq+jloJOpohaVUAaAKD3pQjQU9cA4S89W Qt2u0O72qUctojBvoud8FaRmXWzWbbABseY31+9lcVIxIQhfbmxpNUtZ4x0KoLeCEDuC sLCjnnzhMmEHgn1Lac4JRNYqi8Wdn1psb8BDoKx3/4vhd832guwC+WIolEI91gQqqMNm z00wI9IAwOa0UPseAmk5NKbFbuJVVZ3k4/CtWGXGBaLlBIkQpITXfZjjKtdlSs2UB0kR HlmA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g10-20020a17090adb0a00b0024e41806376si6766404pjv.180.2023.05.05.11.44.55; Fri, 05 May 2023 11:45:07 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230473AbjEESeb (ORCPT + 99 others); Fri, 5 May 2023 14:34:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229795AbjEESe3 (ORCPT ); Fri, 5 May 2023 14:34:29 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 672801816B for ; Fri, 5 May 2023 11:34:28 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id F0EE963CDA for ; Fri, 5 May 2023 18:34:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D1D7BC433EF; Fri, 5 May 2023 18:34:24 +0000 (UTC) Date: Fri, 5 May 2023 19:34:21 +0100 From: Catalin Marinas To: Florent Revest Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, anshuman.khandual@arm.com, joey.gouly@arm.com, mhocko@suse.com, keescook@chromium.org, david@redhat.com, peterx@redhat.com, izbyshev@ispras.ru, nd@arm.com, broonie@kernel.org, szabolcs.nagy@arm.com Subject: Re: [PATCH 3/4] mm: Add a NO_INHERIT flag to the PR_SET_MDWE prctl Message-ID: References: <20230504170942.822147-1-revest@chromium.org> <20230504170942.822147-4-revest@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230504170942.822147-4-revest@chromium.org> X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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, May 04, 2023 at 07:09:41PM +0200, Florent Revest wrote: > This extends the current PR_SET_MDWE prctl arg with a bit to indicate > that the process doesn't want MDWE protection to propagate to children. > > To implement this no-inherit mode, the tag in current->mm->flags must be > absent from MMF_INIT_MASK. This means that the encoding for "MDWE but > without inherit" is different in the prctl than in the mm flags. This > leads to a bit of bit-mangling in the prctl implementation. That bit mangling is not that bad but it complicates the code a bit, especially if we'll add new bits in the future. We also need to check both the original and the no-inherit bits for each feature. Another question is whether we want to support more fine-grained inheriting or just a big knob that disables inheriting for all the (future) MDWE flags. I think a somewhat simpler way would be to clear the flags on fork(), either based on a big MMF_HAS_MDWE_NO_INHERIT knob or individual ones. Something like below (completely untested): diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h index 0ee96ea7a0e9..ca83a0c8d19c 100644 --- a/include/linux/sched/coredump.h +++ b/include/linux/sched/coredump.h @@ -91,4 +91,12 @@ static inline int get_dumpable(struct mm_struct *mm) MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK) #define MMF_VM_MERGE_ANY 29 + +#define MMF_INIT_FLAGS(flags) ({ \ + unsigned long new_flags = flags; \ + if (new_flags & (1UL << MMF_HAS_MDWE_NO_INHERIT)) \ + new_flags &= ~(1UL << MMF_HAS_MDWE_MASK); \ + new_flags & MMF_INIT_MASK; \ +}) + #endif /* _LINUX_SCHED_COREDUMP_H */ diff --git a/kernel/fork.c b/kernel/fork.c index ed4e01daccaa..53f0b68a5451 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1288,7 +1288,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, hugetlb_count_init(mm); if (current->mm) { - mm->flags = current->mm->flags & MMF_INIT_MASK; + mm->flags = MMF_INIT_FLAGS(current->mm->flags); mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; } else { mm->flags = default_dump_filter; The checks in MMF_INIT_FLAGS() can grow in time if we add more bits in there but we still only keep a single flag that determines whether the feature is enabled (maybe that's more like bikeshedding at this moment when we have a single bit). (fun remark: I see you cc'ed nd@arm.com'; that's not a real person, it's what our IT folk asked us to add on cc so that the Exchange server doesn't append the legal disclaimer; most lists are covered already without such cc but I guess people feel safer to add it, just in case) -- Catalin