From patchwork Fri Dec 9 16:04:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 31863 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp860681wrr; Fri, 9 Dec 2022 08:08:04 -0800 (PST) X-Google-Smtp-Source: AA0mqf5a6mmiEtLUiMtZFzaiWedVuFH4kvgvDJQ6FVmGi2mIgyGtnyKCm8hmNGwG8rwhO0Beupsj X-Received: by 2002:a05:6a20:d38e:b0:aa:5c2d:ae2a with SMTP id iq14-20020a056a20d38e00b000aa5c2dae2amr7784826pzb.61.1670602083900; Fri, 09 Dec 2022 08:08:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670602083; cv=none; d=google.com; s=arc-20160816; b=IJYs0v65mPBoPZNKYp7XG1PmCLHtMI0z65RI7XEy9TL/BJUsoQsRkpd0CXPXniAFgA JpzkywFsOuKH+ylp515qEoA4r+ElzN2wqR0SuVynnmsUU9t5fesj1cTobjfWAaEjRjQz VfL7LCZEV7HeDRHHyeAqGtUH5f1zXV5IDZKMROi8jutKPnIbW+pRZGaZG5C6ChaZhQYR q4//yApEAw0xsvk/TWg1nBudPtthl84ygSI4V7H+6k8ndIc4KHUoSstceDrOXyxWtfPm IYK60TE8zuH6BAXdWea2SKwDICP6yrf2r+KIcY1TAQeqPQDtV3lpfdgH0W0tbnFUKedx 9EOA== 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=1Z0pNLRJ0HO6mEsQsP4xR7y3AzecdWz1BUkrTkkGBPM=; b=GhPCNfiAliiIk0hrEuH0udU5rjp6OZPg/X2J0Vlw5gVZAYB55W0ArhTvo4rPbzqzXE aZz1o6AOk54nBi1HGdalJj9s9or3TjPKFaYJ4ViQUVBcSBHvL9l6DgRj1XFnInGdEIlU eMMpFK0KJZWVxkMki9cEGhi7pqhBJUGhCS38mhaFrb+Jr2ego8x3F45WGWTlUYPiSawc xQLDyLTnfN3LeO59r3gOsu/1GbHN4YqhWZrHaqwGD6Z/LPUk/UWlh8Bvehf14UDRAoM/ dEwQm3NlfQjltmQlE9e6Ym/OHcXpDk4RI4iT2F0uqkOAknMfHJtPA6D3t5IVun57RZJ1 gzMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NKb1GAp4; 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 e23-20020a656897000000b004772bd28105si1656272pgt.831.2022.12.09.08.07.50; Fri, 09 Dec 2022 08:08:03 -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=NKb1GAp4; 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 S229680AbiLIQFJ (ORCPT + 99 others); Fri, 9 Dec 2022 11:05:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229835AbiLIQFC (ORCPT ); Fri, 9 Dec 2022 11:05:02 -0500 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F7A351C04 for ; Fri, 9 Dec 2022 08:05:01 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id w37so3822652pga.5 for ; Fri, 09 Dec 2022 08:05:01 -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=1Z0pNLRJ0HO6mEsQsP4xR7y3AzecdWz1BUkrTkkGBPM=; b=NKb1GAp4o6SR8RAwURjuFDuN+zxhqHMcW5ssGhcZ+DU+PFAx+LUcL7yPaq/lio2rqx gsbeokPy4gQONsYdB+99EBxJJNsHClsPC4BjpU3IrUyEzRti9qGuEoFDz7cNCZF8sD6t PVp5eg0FzAb2GvkkjoCUfSPMbbq/LQxGCDCUg= 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=1Z0pNLRJ0HO6mEsQsP4xR7y3AzecdWz1BUkrTkkGBPM=; b=PdeS7xvyNC4mEzyH780yhXUUx3gvCyB/uhzgTiGYlSdy3xLKK7o6r1xuf0G8dxoD3J LVfMKePQV3rB3SH3E5wOSrE4gul6DidMayFJLMqs3WbX+JqkppO7ZRtNIEhD0nDCDiDW D8j6SgnJm8T75i7vSbrEw9+ZcQ+vkhr1qGbFnG75mxWgyEPtK1lvQTTLAJNA+agNg8DL 3SnwflF2eX3qwtpkZaMCqNOPFMEa4cee9hkEeM/wkalPm4/Drkj/8CJY0S3ruN8GV1QH DHdvFCbkmod23vtuBGJDbMpehF+9znGxKugk/hAEwEV+FWVSw3tVZC2EZh4GzN2yq3Z3 /RGA== X-Gm-Message-State: ANoB5pk5d6D10IDWxwCTXXcF2vvclEwSCaG9xxX0YUmvDGlY1GfGe4Vt EQUcigSjiuxAuPmd+yweT+hc6g== X-Received: by 2002:a62:870d:0:b0:576:f02e:d0ef with SMTP id i13-20020a62870d000000b00576f02ed0efmr5609008pfe.4.1670601900690; Fri, 09 Dec 2022 08:05:00 -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 a15-20020aa795af000000b00576670cc170sm1460504pfk.93.2022.12.09.08.04.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Dec 2022 08:05:00 -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, linux-security-module@vger.kernel.org Subject: [PATCH v7 1/6] mm/memfd: add F_SEAL_EXEC Date: Fri, 9 Dec 2022 16:04:48 +0000 Message-Id: <20221209160453.3246150-2-jeffxu@google.com> X-Mailer: git-send-email 2.39.0.rc1.256.g54fd8350bd-goog In-Reply-To: <20221209160453.3246150-1-jeffxu@google.com> References: <20221209160453.3246150-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=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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751753250919352668?= X-GMAIL-MSGID: =?utf-8?q?1751753250919352668?= 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;