From patchwork Wed Dec 7 15:49:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 30907 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp260927wrr; Wed, 7 Dec 2022 07:52:42 -0800 (PST) X-Google-Smtp-Source: AA0mqf4yUl5EZ4Hhanol6+GbzVgPNoGYcVLo0Nt6xAeI9AE3WYgAt+ZqA28j+cahuPCfjnJ73VOl X-Received: by 2002:a17:907:a06e:b0:7c0:dfb7:3525 with SMTP id ia14-20020a170907a06e00b007c0dfb73525mr15418471ejc.205.1670428362026; Wed, 07 Dec 2022 07:52:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670428362; cv=none; d=google.com; s=arc-20160816; b=kFQdcohfQYfNdO8r1NrlsxqdCdGfoWL6/jKi1em4IOpTrdezlmLIZSVAgKWNdl74Tt xp18g9+9XTBsulzCvatr18w1FuUKlhT/xqZzFqu5Jz2eJMnBu1fKwgSTysDAFZ5sBWgp IZv03uUZCrNz5jI+lioMkPSEV2iAtmQbB2pL6rx5WjvL/DOAcCx2a/OsUvhGlAicxNUF rO6ljGi0OOXky1uw+GqWZpu+GpNjO2e8dIPbT1gGy8hnRIHjcAzVC3m5e6tg8U8sHYNF Yh1q/M8h9JI2SNMFz0OmpEfIZcAP2XaEjCYHTW0nJxv0LKaaLYA5N47+Z1hMUn591vpo 9hWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=71ca0yPxFF6EaiLKMWlrSqitNHDHlqW3RCkGeX1vy+A=; b=vYjUBkBkoR7sxyXSKuEF7FRuCC80YJOWHyxTehTQ55c3C42JTdB1CgjARcdQPChwYy af46Erpzy75t1I2l5kmjrC2p8Y6uZFeYKjOUIcRfqDPDWzsRe2VABHc3UkKJguxnUwaG keqSyGRgUh0spb41nXl9+t3RoGHIgFxU7AZ9RGxkIy1e6jd/C2fuiSBie9yorDOaEiD9 QY/j5838HlnUKq0VR4v0M1dlN3Pl3ewc7AGNL8lKAcxKaogLNYLBBe6QPy54zzpvSecx Wks82m5ngUV1Nv/9e6ou0f/QDv4XDQZlpE78Gf1565s0G1JG3BklmArZ5tjWs+RTKcA+ eZ9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PhU557wY; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r24-20020aa7c158000000b0046ca3c3df50si4313939edp.211.2022.12.07.07.52.18; Wed, 07 Dec 2022 07:52:42 -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=PhU557wY; 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 S229811AbiLGPty (ORCPT + 99 others); Wed, 7 Dec 2022 10:49:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229705AbiLGPtq (ORCPT ); Wed, 7 Dec 2022 10:49:46 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 034B3326DF for ; Wed, 7 Dec 2022 07:49:46 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id t11-20020a17090a024b00b0021932afece4so1814391pje.5 for ; Wed, 07 Dec 2022 07:49:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=71ca0yPxFF6EaiLKMWlrSqitNHDHlqW3RCkGeX1vy+A=; b=PhU557wYsKo/iDLZ/eBYG10j3SUNufkud5V4AzaP2ICyOtQc995aeL12t/n/DCDOjS NMogyAHS/VfnZLMBDSu6Di4KmFIVVQ0uOhTFzgcacngKlg82FwY5QBCqc7jvGtP83Lsg 0oNoHBlwcyoa3/tfXpapegc2TgwcvzzxnMc4Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=71ca0yPxFF6EaiLKMWlrSqitNHDHlqW3RCkGeX1vy+A=; b=1W32KPTrcOjMjc0ED7xQynRIaX3Lbf+wCf8jWMDrS5QnGC9Pvg63giTpSy7kpqCrcX Y0UsT6dwFpLl1eabbNNo3OGegAj7nh0Bwp3NOTMlR7HADy9waUToxRT3+f/qrhLj3s9n sDTYMPHDVjEeWngP59iQRsaC2TM+/k/xJ/PX34PfvKsoai0eQjEyQVj3y/NPuzpZM6eK VM+bT5FuRI3DN4Z/7Tpts14Hi8GRQXbCnnelCelPURIHWtjnJVDl0rcxPR1K9NCwowm/ PpjuC5bUS4439ohj271GKxHWJjL10iVDIAhb4girxrqx0cD/W1olYSMNq+MmdmVvlb1H HnpA== X-Gm-Message-State: ANoB5plXZXIxWPL95K3FX9vDXn480oHhvTKJCqVzaSPC/pwbaI88TV5z PR47XEFn7DjAQr8dxaMD9oO4bQ== X-Received: by 2002:a17:902:8343:b0:187:1f0:4579 with SMTP id z3-20020a170902834300b0018701f04579mr748942pln.61.1670428185700; Wed, 07 Dec 2022 07:49:45 -0800 (PST) Received: from jeffxud.c.googlers.com.com (30.202.168.34.bc.googleusercontent.com. [34.168.202.30]) by smtp.gmail.com with ESMTPSA id a9-20020a170902ecc900b0017f7628cbddsm14920934plh.30.2022.12.07.07.49.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 07:49:44 -0800 (PST) From: jeffxu@chromium.org To: skhan@linuxfoundation.org, keescook@chromium.org Cc: akpm@linux-foundation.org, dmitry.torokhov@gmail.com, dverkamp@chromium.org, hughd@google.com, jeffxu@google.com, jorgelo@chromium.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, jannh@google.com, linux-hardening@vger.kernel.org Subject: [PATCH v6 1/6] mm/memfd: add F_SEAL_EXEC Date: Wed, 7 Dec 2022 15:49:34 +0000 Message-Id: <20221207154939.2532830-2-jeffxu@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <20221207154939.2532830-1-jeffxu@google.com> References: <20221207154939.2532830-1-jeffxu@google.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751571089842539072?= X-GMAIL-MSGID: =?utf-8?q?1751571089842539072?= From: Daniel Verkamp The new F_SEAL_EXEC flag will prevent modification of the exec bits: written as traditional octal mask, 0111, or as named flags, S_IXUSR | S_IXGRP | S_IXOTH. Any chmod(2) or similar call that attempts to modify any of these bits after the seal is applied will fail with errno EPERM. This will preserve the execute bits as they are at the time of sealing, so the memfd will become either permanently executable or permanently un-executable. Signed-off-by: Daniel Verkamp Co-developed-by: Jeff Xu Signed-off-by: Jeff Xu Reviewed-by: Kees Cook --- include/uapi/linux/fcntl.h | 1 + mm/memfd.c | 2 ++ mm/shmem.c | 6 ++++++ 3 files changed, 9 insertions(+) diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index 2f86b2ad6d7e..e8c07da58c9f 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -43,6 +43,7 @@ #define F_SEAL_GROW 0x0004 /* prevent file from growing */ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #define F_SEAL_FUTURE_WRITE 0x0010 /* prevent future writes while mapped */ +#define F_SEAL_EXEC 0x0020 /* prevent chmod modifying exec bits */ /* (1U << 31) is reserved for signed error codes */ /* diff --git a/mm/memfd.c b/mm/memfd.c index 08f5f8304746..4ebeab94aa74 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -147,6 +147,7 @@ static unsigned int *memfd_file_seals_ptr(struct file *file) } #define F_ALL_SEALS (F_SEAL_SEAL | \ + F_SEAL_EXEC | \ F_SEAL_SHRINK | \ F_SEAL_GROW | \ F_SEAL_WRITE | \ @@ -175,6 +176,7 @@ static int memfd_add_seals(struct file *file, unsigned int seals) * SEAL_SHRINK: Prevent the file from shrinking * SEAL_GROW: Prevent the file from growing * SEAL_WRITE: Prevent write access to the file + * SEAL_EXEC: Prevent modification of the exec bits in the file mode * * As we don't require any trust relationship between two parties, we * must prevent seals from being removed. Therefore, sealing a file diff --git a/mm/shmem.c b/mm/shmem.c index c1d8b8a1aa3b..e18a9cf9d937 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1085,6 +1085,12 @@ static int shmem_setattr(struct user_namespace *mnt_userns, if (error) return error; + if ((info->seals & F_SEAL_EXEC) && (attr->ia_valid & ATTR_MODE)) { + if ((inode->i_mode ^ attr->ia_mode) & 0111) { + return -EPERM; + } + } + if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { loff_t oldsize = inode->i_size; loff_t newsize = attr->ia_size;