From patchwork Wed Dec 7 15:49:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 30906 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp260833wrr; Wed, 7 Dec 2022 07:52:27 -0800 (PST) X-Google-Smtp-Source: AA0mqf5uXGRL88KQw2r5mzC5lo3/m3wJ2Ee7xehMfbv1WgdvANytgoijgbV56BUNYfbkm8z6VCIh X-Received: by 2002:a05:6402:35cf:b0:469:5ee9:4a76 with SMTP id z15-20020a05640235cf00b004695ee94a76mr62975803edc.405.1670428346885; Wed, 07 Dec 2022 07:52:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670428346; cv=none; d=google.com; s=arc-20160816; b=VgIYZXo2L3HCJVB6/B4L/yhLyztMbBwDAr2JiCnVdojSpq2ZoueNWY+HMgDJ/Z7S2z +3ELdOBnwwdIK3H+SjBkU6WHM20MTcBFw+C5wa7oYt3Yw+fpbouWhedFw/v6TlRimjTh C4xNNRLcpgpEG7Dn9EO+YOvZMsFV0r/tLzxerDQRBdMrm9JmoyFr3dR/Jr/SNc3bPUhQ MThzMfV2jF1kYNkhU+T4RrGkm3qUCGkh8BGA7vy12eKfIb9YLKPdAnldDfxB07sZ5kX1 CgBQvX8nOipJrbevMdKDrpL5qn52a82WuoFnaoeC/tRIHxUbQrB6HwaMl35xeNVO74dL Q6qQ== 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=br+NzT6Ekl7un/bONAJTTP+zVNWLoPX0fUe5M313tYk=; b=iajEGv536cFIpE6Gc443ldwMXeXQuCyE57SKDoziRP7wxmFC/1cF+HdZluQMjGZxBx 62YWF8WvGU85BhytR/PGoD3O3FVRpfDLcKN17oUF10L01jPZ/dwD1QIEeq0/XJ+7NcA6 xLWI8Wp4YJ/LiFpgfth3Jm5yQqf+gJ5pfWhB5CT3bU+nnbqEdWk/M5/nS15WgHaE/AND fD8EzFAL3+VEVwdQi1q4TNa6lCToT6wXGN2AfM+4IMsZsSWY7gnld4p33FnNb7y10VRs zEQmjuAaKkp22Lt0Mqa42VPm5KeHtSym6Ter3HdbNAJ2TLNnWQQVw+Et9gbTgpulro7h nDdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KveNxSwr; 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 ji13-20020a170907980d00b007c098f5e5f4si19057922ejc.233.2022.12.07.07.52.02; Wed, 07 Dec 2022 07:52:26 -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=KveNxSwr; 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 S229942AbiLGPuX (ORCPT + 99 others); Wed, 7 Dec 2022 10:50:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229760AbiLGPtv (ORCPT ); Wed, 7 Dec 2022 10:49:51 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0692A63BBB for ; Wed, 7 Dec 2022 07:49:51 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id b11so18005781pjp.2 for ; Wed, 07 Dec 2022 07:49:51 -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=br+NzT6Ekl7un/bONAJTTP+zVNWLoPX0fUe5M313tYk=; b=KveNxSwrAsaP96yq/2ARVH0FCPenY0jXv1lv5clhnkzL7NYz4dsA1XuERs7Zl08c5t 8vCBKRTn59G+ilGEelru03ZLrBZQfDmbySTIZfw8NCC1D/7Z0wESKY45SOkudNKxEzZ8 +FtkzZYTl3DL+21FEiiiK6YesMBG9HrOrbn8g= 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=br+NzT6Ekl7un/bONAJTTP+zVNWLoPX0fUe5M313tYk=; b=q0UrhfYV9W5x+cjqNTO16E7y+saN9IMvwxeq5UxJZPGYz7WSwCgVbPRPFqqxeyYR3m ALnyTgusg1Bm/r+sH3mEZ1aIOgsR2NFesJb7v3vFo/JI4/iuzd9hz8YMmyvX4CY2OQt4 aYlBu5R9tUvUN1TfTTLBqAlUXgxniaaX2pFziDnnSWq3s6TtVrU7922K+lPboWIGztV8 x46KjbWreDRZJqiK1Xc4NRh9VW67S6cQMVyHmIVUTtPNJZBi9Q394qSZvlX3xGrCqX66 M/EJp2Id0P4zORgui0JdARQiTzdcQ3Qaj3NZcpyCIgr/SBP1CLF6lRVUWYeO6eOdl9x4 ItDA== X-Gm-Message-State: ANoB5pl41eotylrsENiMdxgo9G1fKt9S9XikkGIgTmS+Xv6TcpUgeeg/ r2HyJdNyq/KB8X4x/Vhg/vHB+Q== X-Received: by 2002:a17:902:b611:b0:188:f570:7bdf with SMTP id b17-20020a170902b61100b00188f5707bdfmr682078pls.40.1670428190523; Wed, 07 Dec 2022 07:49:50 -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.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 07:49:49 -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, kernel test robot Subject: [PATCH v6 6/6] mm/memfd: security hook for memfd_create Date: Wed, 7 Dec 2022 15:49:39 +0000 Message-Id: <20221207154939.2532830-7-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=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?1751571073828688925?= X-GMAIL-MSGID: =?utf-8?q?1751571073828688925?= From: Jeff Xu The new security_memfd_create allows lsm to check flags of memfd_create. The security by default system (such as chromeos) can use this to implement system wide lsm to allow only non-executable memfd being created. Signed-off-by: Jeff Xu Reported-by: kernel test robot --- include/linux/lsm_hook_defs.h | 1 + include/linux/lsm_hooks.h | 4 ++++ include/linux/security.h | 6 ++++++ mm/memfd.c | 5 +++++ security/security.c | 13 +++++++++++++ 5 files changed, 29 insertions(+) diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index ec119da1d89b..fd40840927c8 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -164,6 +164,7 @@ LSM_HOOK(int, 0, file_alloc_security, struct file *file) LSM_HOOK(void, LSM_RET_VOID, file_free_security, struct file *file) LSM_HOOK(int, 0, file_ioctl, struct file *file, unsigned int cmd, unsigned long arg) +LSM_HOOK(int, 0, memfd_create, char *name, unsigned int flags) LSM_HOOK(int, 0, mmap_addr, unsigned long addr) LSM_HOOK(int, 0, mmap_file, struct file *file, unsigned long reqprot, unsigned long prot, unsigned long flags) diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h index 4ec80b96c22e..5a18a6552278 100644 --- a/include/linux/lsm_hooks.h +++ b/include/linux/lsm_hooks.h @@ -543,6 +543,10 @@ * simple integer value. When @arg represents a user space pointer, it * should never be used by the security module. * Return 0 if permission is granted. + * @memfd_create: + * @name is the name of memfd file. + * @flags is the flags used in memfd_create. + * Return 0 if permission is granted. * @mmap_addr : * Check permissions for a mmap operation at @addr. * @addr contains virtual address that will be used for the operation. diff --git a/include/linux/security.h b/include/linux/security.h index ca1b7109c0db..5b87a780822a 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -384,6 +384,7 @@ int security_file_permission(struct file *file, int mask); int security_file_alloc(struct file *file); void security_file_free(struct file *file); int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg); +int security_memfd_create(char *name, unsigned int flags); int security_mmap_file(struct file *file, unsigned long prot, unsigned long flags); int security_mmap_addr(unsigned long addr); @@ -963,6 +964,11 @@ static inline int security_file_ioctl(struct file *file, unsigned int cmd, return 0; } +static inline int security_memfd_create(char *name, unsigned int flags) +{ + return 0; +} + static inline int security_mmap_file(struct file *file, unsigned long prot, unsigned long flags) { diff --git a/mm/memfd.c b/mm/memfd.c index 92f0a5765f7c..f04ed5f0474f 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -356,6 +356,11 @@ SYSCALL_DEFINE2(memfd_create, goto err_name; } + /* security hook for memfd_create */ + error = security_memfd_create(name, flags); + if (error) + return error; + if (flags & MFD_HUGETLB) { file = hugetlb_file_setup(name, 0, VM_NORESERVE, HUGETLB_ANONHUGE_INODE, diff --git a/security/security.c b/security/security.c index 79d82cb6e469..5c018e080923 100644 --- a/security/security.c +++ b/security/security.c @@ -1010,6 +1010,19 @@ int security_sb_clone_mnt_opts(const struct super_block *oldsb, } EXPORT_SYMBOL(security_sb_clone_mnt_opts); +int security_add_mnt_opt(const char *option, const char *val, int len, + void **mnt_opts) +{ + return call_int_hook(sb_add_mnt_opt, -EINVAL, + option, val, len, mnt_opts); +} +EXPORT_SYMBOL(security_add_mnt_opt); + +int security_memfd_create(char *name, unsigned int flags) +{ + return call_int_hook(memfd_create, 0, name, flags); +} + int security_move_mount(const struct path *from_path, const struct path *to_path) { return call_int_hook(move_mount, 0, from_path, to_path);