From: Serge Hallyn <serge@hallyn.com>
To: Xu Kuohai <xukuohai@huaweicloud.com>
Cc: bpf@vger.kernel.org, netdev@vger.kernel.org,
linux-security-module@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-integrity@vger.kernel.org,
apparmor@lists.ubuntu.com, selinux@vger.kernel.org,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Jiri Olsa <jolsa@kernel.org>,
Matt Bobrowski <mattbobrowski@google.com>,
Brendan Jackman <jackmanb@chromium.org>,
Paul Moore <paul@paul-moore.com>,
James Morris <jmorris@namei.org>,
Khadija Kamran <kamrankhadijadj@gmail.com>,
Casey Schaufler <casey@schaufler-ca.com>,
Ondrej Mosnacek <omosnace@redhat.com>,
Kees Cook <keescook@chromium.org>,
John Johansen <john.johansen@canonical.com>,
Lukas Bulwahn <lukas.bulwahn@gmail.com>,
Roberto Sassu <roberto.sassu@huawei.com>,
Shung-Hsi Yu <shung-hsi.yu@suse.com>,
Edward Cree <ecree.xilinx@gmail.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>,
Trond Myklebust <trond.myklebust@hammerspace.com>,
Anna Schumaker <anna@kernel.org>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Stephen Smalley <stephen.smalley.work@gmail.com>
Subject: Re: [PATCH bpf-next v4 01/20] lsm: Refactor return value of LSM hook vm_enough_memory
Date: Thu, 11 Jul 2024 08:46:51 -0500 (CDT) [thread overview]
Message-ID: <d1a7de68-1e36-42e6-bdc8-4046ea52338a@hallyn.com> (raw)
In-Reply-To: <20240711111908.3817636-2-xukuohai@huaweicloud.com>
Jul 11, 2024 06:14:08 Xu Kuohai <xukuohai@huaweicloud.com>:
> From: Xu Kuohai <xukuohai@huawei.com>
>
> To be consistent with most LSM hooks, convert the return value of
> hook vm_enough_memory to 0 or a negative error code.
>
> Before:
> - Hook vm_enough_memory returns 1 if permission is granted, 0 if not.
> - LSM_RET_DEFAULT(vm_enough_memory_mm) is 1.
>
> After:
> - Hook vm_enough_memory reutrns 0 if permission is granted, negative
> error code if not.
> - LSM_RET_DEFAULT(vm_enough_memory_mm) is 0.
I haven't done a detailed review yet, but based on the description this is definitely a good change. Thank you.
> Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
> ---
> include/linux/lsm_hook_defs.h | 2 +-
> include/linux/security.h | 2 +-
> security/commoncap.c | 11 +++--------
> security/security.c | 11 +++++------
> security/selinux/hooks.c | 15 ++++-----------
> 5 files changed, 14 insertions(+), 27 deletions(-)
>
> diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
> index 44488b1ab9a9..e6e6f8473955 100644
> --- a/include/linux/lsm_hook_defs.h
> +++ b/include/linux/lsm_hook_defs.h
> @@ -48,7 +48,7 @@ LSM_HOOK(int, 0, quota_on, struct dentry *dentry)
> LSM_HOOK(int, 0, syslog, int type)
> LSM_HOOK(int, 0, settime, const struct timespec64 *ts,
> const struct timezone *tz)
> -LSM_HOOK(int, 1, vm_enough_memory, struct mm_struct *mm, long pages)
> +LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages)
> LSM_HOOK(int, 0, bprm_creds_for_exec, struct linux_binprm *bprm)
> LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, const struct file *file)
> LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
> diff --git a/include/linux/security.h b/include/linux/security.h
> index de3af33e6ff5..454f96307cb9 100644
> --- a/include/linux/security.h
> +++ b/include/linux/security.h
> @@ -634,7 +634,7 @@ static inline int security_settime64(const struct timespec64 *ts,
>
> static inline int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
> {
> - return __vm_enough_memory(mm, pages, cap_vm_enough_memory(mm, pages));
> + return __vm_enough_memory(mm, pages, !cap_vm_enough_memory(mm, pages));
> }
>
> static inline int security_bprm_creds_for_exec(struct linux_binprm *bprm)
> diff --git a/security/commoncap.c b/security/commoncap.c
> index 162d96b3a676..cefad323a0b1 100644
> --- a/security/commoncap.c
> +++ b/security/commoncap.c
> @@ -1396,17 +1396,12 @@ int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
> * Determine whether the allocation of a new virtual mapping by the current
> * task is permitted.
> *
> - * Return: 1 if permission is granted, 0 if not.
> + * Return: 0 if permission granted, negative error code if not.
> */
> int cap_vm_enough_memory(struct mm_struct *mm, long pages)
> {
> - int cap_sys_admin = 0;
> -
> - if (cap_capable(current_cred(), &init_user_ns,
> - CAP_SYS_ADMIN, CAP_OPT_NOAUDIT) == 0)
> - cap_sys_admin = 1;
> -
> - return cap_sys_admin;
> + return cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
> + CAP_OPT_NOAUDIT);
> }
>
> /**
> diff --git a/security/security.c b/security/security.c
> index e5ca08789f74..3475f0cab3da 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -1115,15 +1115,14 @@ int security_vm_enough_memory_mm(struct mm_struct *mm, long pages)
> int rc;
>
> /*
> - * The module will respond with a positive value if
> - * it thinks the __vm_enough_memory() call should be
> - * made with the cap_sys_admin set. If all of the modules
> - * agree that it should be set it will. If any module
> - * thinks it should not be set it won't.
> + * The module will respond with 0 if it thinks the __vm_enough_memory()
> + * call should be made with the cap_sys_admin set. If all of the modules
> + * agree that it should be set it will. If any module thinks it should
> + * not be set it won't.
> */
> hlist_for_each_entry(hp, &security_hook_heads.vm_enough_memory, list) {
> rc = hp->hook.vm_enough_memory(mm, pages);
> - if (rc <= 0) {
> + if (rc < 0) {
> cap_sys_admin = 0;
> break;
> }
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 7eed331e90f0..9cd5a8f1f6a3 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -2202,23 +2202,16 @@ static int selinux_syslog(int type)
> }
>
> /*
> - * Check that a process has enough memory to allocate a new virtual
> - * mapping. 0 means there is enough memory for the allocation to
> - * succeed and -ENOMEM implies there is not.
> + * Check permission for allocating a new virtual mapping. Returns
> + * 0 if permission is granted, negative error code if not.
> *
> * Do not audit the selinux permission check, as this is applied to all
> * processes that allocate mappings.
> */
> static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
> {
> - int rc, cap_sys_admin = 0;
> -
> - rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
> - CAP_OPT_NOAUDIT, true);
> - if (rc == 0)
> - cap_sys_admin = 1;
> -
> - return cap_sys_admin;
> + return cred_has_capability(current_cred(), CAP_SYS_ADMIN,
> + CAP_OPT_NOAUDIT, true);
> }
>
> /* binprm security operations */
> --
> 2.30.2
next prev parent reply other threads:[~2024-07-11 13:55 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-11 11:18 [PATCH bpf-next v4 00/20] Add return value range check for BPF LSM Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 01/20] lsm: Refactor return value of LSM hook vm_enough_memory Xu Kuohai
2024-07-11 13:46 ` Serge Hallyn [this message]
2024-07-19 2:07 ` [PATCH v4 1/20] " Paul Moore
2024-07-11 11:18 ` [PATCH bpf-next v4 02/20] lsm: Refactor return value of LSM hook inode_need_killpriv Xu Kuohai
2024-07-11 14:15 ` Serge Hallyn
2024-07-13 8:06 ` Xu Kuohai
2024-07-19 2:08 ` [PATCH v4 2/20] " Paul Moore
2024-07-20 9:27 ` Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 03/20] lsm: Refactor return value of LSM hook inode_getsecurity Xu Kuohai
2024-07-12 13:31 ` Simon Horman
2024-07-13 8:07 ` Xu Kuohai
2024-07-19 2:08 ` [PATCH v4 3/20] " Paul Moore
2024-07-20 9:28 ` Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 04/20] lsm: Refactor return value of LSM hook inode_listsecurity Xu Kuohai
2024-07-19 2:08 ` [PATCH v4 4/20] " Paul Moore
2024-07-20 9:29 ` Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 05/20] lsm: Refactor return value of LSM hook inode_copy_up_xattr Xu Kuohai
2024-07-19 2:08 ` [PATCH v4 5/20] " Paul Moore
2024-07-20 9:29 ` Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 06/20] lsm: Refactor return value of LSM hook getselfattr Xu Kuohai
2024-07-19 2:08 ` [PATCH v4 6/20] " Paul Moore
2024-07-20 9:30 ` Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 07/20] lsm: Refactor return value of LSM hook setprocattr Xu Kuohai
2024-07-19 2:08 ` [PATCH v4 7/20] " Paul Moore
2024-07-20 9:31 ` Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 08/20] lsm: Refactor return value of LSM hook getprocattr Xu Kuohai
2024-07-19 2:08 ` [PATCH v4 8/20] " Paul Moore
2024-07-20 9:30 ` Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 09/20] lsm: Refactor return value of LSM hook key_getsecurity Xu Kuohai
2024-07-19 2:08 ` [PATCH v4 9/20] " Paul Moore
2024-07-20 9:31 ` Xu Kuohai
2024-07-22 21:35 ` Paul Moore
2024-07-23 7:04 ` Xu Kuohai
2024-07-23 18:34 ` Paul Moore
2024-07-11 11:18 ` [PATCH bpf-next v4 10/20] lsm: Refactor return value of LSM hook audit_rule_match Xu Kuohai
2024-07-19 2:08 ` [PATCH " Paul Moore
2024-07-20 9:31 ` Xu Kuohai
2024-07-11 11:18 ` [PATCH bpf-next v4 11/20] bpf, lsm: Add disabled BPF LSM hook list Xu Kuohai
2024-07-12 17:56 ` Alexei Starovoitov
2024-07-13 8:11 ` Xu Kuohai
2024-07-11 11:19 ` [PATCH bpf-next v4 12/20] bpf, lsm: Enable BPF LSM prog to read/write return value parameters Xu Kuohai
2024-07-12 15:56 ` [PATCH bpf-next v4 00/20] Add return value range check for BPF LSM Paul Moore
2024-07-12 16:00 ` Paul Moore
2024-07-12 21:44 ` Paul Moore
2024-07-19 2:13 ` Paul Moore
2024-07-19 3:55 ` Xu Kuohai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d1a7de68-1e36-42e6-bdc8-4046ea52338a@hallyn.com \
--to=serge@hallyn.com \
--cc=andrii@kernel.org \
--cc=anna@kernel.org \
--cc=apparmor@lists.ubuntu.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=casey@schaufler-ca.com \
--cc=daniel@iogearbox.net \
--cc=ecree.xilinx@gmail.com \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=haoluo@google.com \
--cc=jackmanb@chromium.org \
--cc=jmorris@namei.org \
--cc=john.fastabend@gmail.com \
--cc=john.johansen@canonical.com \
--cc=jolsa@kernel.org \
--cc=kamrankhadijadj@gmail.com \
--cc=keescook@chromium.org \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=lukas.bulwahn@gmail.com \
--cc=martin.lau@linux.dev \
--cc=mattbobrowski@google.com \
--cc=netdev@vger.kernel.org \
--cc=omosnace@redhat.com \
--cc=pabeni@redhat.com \
--cc=paul@paul-moore.com \
--cc=roberto.sassu@huawei.com \
--cc=sdf@google.com \
--cc=selinux@vger.kernel.org \
--cc=shung-hsi.yu@suse.com \
--cc=song@kernel.org \
--cc=stephen.smalley.work@gmail.com \
--cc=trond.myklebust@hammerspace.com \
--cc=viro@zeniv.linux.org.uk \
--cc=xukuohai@huaweicloud.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox