From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AFA4C4332F for ; Thu, 8 Dec 2022 16:32:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACD698E0005; Thu, 8 Dec 2022 11:32:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A7DB88E0001; Thu, 8 Dec 2022 11:32:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 945258E0005; Thu, 8 Dec 2022 11:32:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 859FE8E0001 for ; Thu, 8 Dec 2022 11:32:24 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5C45F1C5C5D for ; Thu, 8 Dec 2022 16:32:24 +0000 (UTC) X-FDA: 80219681808.24.A5EFD3A Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf15.hostedemail.com (Postfix) with ESMTP id 1B8DFA0002 for ; Thu, 8 Dec 2022 16:32:21 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=KGXTAc0X; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf15.hostedemail.com: domain of keescook@chromium.org designates 209.85.215.170 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670517142; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=57Ddcfru2LvUQsSfd0q8Xr1c5dKIodEjUxeNkx/mxX0=; b=6dzfArxVjUpGUh2lF6SZL9Og1QQr4FtVYrvPKtoHlTkbN05p7eBkjwP6CeIcc1GI3nY9IK 6MiPr4NX9QBXbJWqAKxJNOzWH5oJRfVsooeO6AkWQQ0yVBuHLoiESx0Dr4VFkEQS9ZY/U1 JstnfX37/z2tm56P/SHBvhAsl87Umng= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=KGXTAc0X; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf15.hostedemail.com: domain of keescook@chromium.org designates 209.85.215.170 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670517142; a=rsa-sha256; cv=none; b=QiIcPSrkB0q7x5dgxzfrWfmqee3PPJbm8FXv2YKwADm6bm5NR/zjtVYtJnr2F0LgxLQlxF 5Wiem7oXhiOSLmKft7HqjyhNlYxmZPHYeaj/SVsRlhzq8v1OBlKiEN0R6xJxOgevuAKxKw 0aKdDBIUml5+uNYldQmAOyR14khLrMI= Received: by mail-pg1-f170.google.com with SMTP id v3so1589406pgh.4 for ; Thu, 08 Dec 2022 08:32:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=57Ddcfru2LvUQsSfd0q8Xr1c5dKIodEjUxeNkx/mxX0=; b=KGXTAc0X86Oo6sdnfCoLnqgKb5/YNtxmCXPjMyJjXMlnm2HJcButMWPYzawWdOta/N cSs0YGuCxdtkeovYlpHj3CV+CQEvO/hbxrQx1u6l8gMF21cfbWsfXnVMQQi/LwiZ+Pau DELp5r470Ahb/RW/lEDJ84wtTsNeWiUz+jKmY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=57Ddcfru2LvUQsSfd0q8Xr1c5dKIodEjUxeNkx/mxX0=; b=KCv0ucitxiFIjgMCNVShNS2d9S70WCiIuMUzgQGwv9uHtK1LcVt7N9PneT6uYPgwJ9 n6MQlgK+6btfGOKMs9mVNs/OcfPUbzbsBqQ0akd0ZBhSxZ0NWceSkP/tVx5Y/e8JBUeV 3N8+UlgiZZ8deF3EIln61vfQQWslZquMB3B66ieTE7gpR+GO0Z3jt/nQPS1qRc0oEF3a PXtNeMlep38ma1Buwh6sbntqwZrNYwmt50naQ952a+20HYbVDTBZ4ikfSicudj+hnu1T ajETBDn9yfEXdAFu06WK/hIsd+tjlKM3s2bo+knlD8mysQrqrN1flEPnCqOTSl+cDdnR EDVg== X-Gm-Message-State: ANoB5plQHHluiAEsnHaYogedcoHlscjScACYLQ/x/plM3Ysb/p4MmGNg ftQHCK48e3ggOzixibTwYzFQdg== X-Google-Smtp-Source: AA0mqf4wxnc/rqiR7541frAh3SkYVsc69VXIbiMFJy68m5JROZIV5OyvOZr8KZI5wI6EPDwLWc4BDQ== X-Received: by 2002:a62:3882:0:b0:56b:9ce2:891f with SMTP id f124-20020a623882000000b0056b9ce2891fmr83105047pfa.43.1670517140886; Thu, 08 Dec 2022 08:32:20 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id 6-20020a170902c20600b00186ff402508sm16742741pll.281.2022.12.08.08.32.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 08:32:20 -0800 (PST) Date: Thu, 8 Dec 2022 08:32:19 -0800 From: Kees Cook To: jeffxu@chromium.org Cc: skhan@linuxfoundation.org, 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: Re: [PATCH v6 6/6] mm/memfd: security hook for memfd_create Message-ID: <202212080831.9010D8A@keescook> References: <20221207154939.2532830-1-jeffxu@google.com> <20221207154939.2532830-7-jeffxu@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221207154939.2532830-7-jeffxu@google.com> X-Rspamd-Queue-Id: 1B8DFA0002 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: qhbf34xhkfsneepmrp5tegm1k1rhbh3p X-HE-Tag: 1670517141-803987 X-HE-Meta: U2FsdGVkX19EHTNBUVXVo/VkZE0uCcSETGJIPNhJwThYKT7eMPe7AQuO4yjL/U4x53/GzrMmuzqkz4kzkW1nSG06MaorqXfXq/InYiOh5J41DL2XZzc+m1mYnWPytvRIhDMePXfhx71Ib5/SHr9OKUE+N3YpTlwmbrgs6LPb/iudXlTaQeYPoOluclooYhYRu7R7uluuQlCRZBqzimCmjdySLpAJgiUlLu51VYs67f1KrA1elHuchIwb5ebrQ5+8L4Apy+OioWI58eczMzXVbcuAQvjVv6jfpgS2wsCJDXyc8VMC1DTysOUOV/zKVGd78PVUVK9zVgZ96HuMOJ0YxSxZ5D3hxFQpTdR/88BdbgKn5z/JWqxKrMW7pS5o3BEssjsJk5EnfLCnOLuoLhUie49eeGbOyUu/CDRVhWf+lzYNebB0WFKKxLKWUk1ugwP9bNWwbth96Y5UCALpc++/nXhiFdHnobqS9ymhtmXqiV4cu102Y7kuaZCvfsx7ncOCCz268cyypiq1SawEBxPqBkd5M3oxzIVmaghrAhGkBhVGFBxIljJvL1+mcrHWS2le0/HrfwUuZPv+P5RPW9RhmtCpY89sQZ/ecqg5N0WV4Hlu0Oc0ewinvuW8fGRw7hSez9C8U1RQT7ZsVFaSTcsJ5GoNMlRKqQ3ogpixY9BFsmgKJn/nMfAtNvirLD52PCblztpEAlONJjBSfPqcaB9VFvWVBaxP4QmYAfVR1konIxECLwswKYo8E1swXXgUfl6NI/pEZrnWy0ZFdpoM73MBP1P9GWOZoiF5GaicwgPOkDh+NX40UvsNwkTjZxEjEfXShysyI8eRS6P28EpiAREucNS96eNu8tPCTNQaT6f64ZsPrgK7td2ZAUA4jbrkV52Lew6+cuv8x1qw8mEIiHgV9GZSKOUss1nVIehFZDFojFup9P1ODVAFWxg9wY3O7kS4GynDVvp2EKNyJtb6FMT ai6aSEQl JuuU+BfuAd3pBxHWAe1pazIsamiVuzg+10inDi6votVM2dQfxFEyeej0iP909L9Q1zDv2m13+S2J63wa1vQO7GIENvEJa9JWYPZdsdiIt6WT7a6MuPtFFRgTB3cm5MELnZQh5naCx4D466hfrPq43NYOvSvfHpSU75/sxU+Bd+tA+1o31DtQ0r0x6LcXzEQBhQoapMmdPwjo68x83MCBR5R8bMA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Dec 07, 2022 at 03:49:39PM +0000, jeffxu@chromium.org wrote: > 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); I think security_add_mnt_opt() isn't supposed to be in here. :) > + > +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); > -- > 2.39.0.rc0.267.gcb52ba06e7-goog > Otherwise looks good. Thanks! -Kees -- Kees Cook