Similar to how security_memcg_charge_task adds an access-control point for pidfd for the charge_fd of a DMA_HEAP_IOCTL_ALLOC or DMA_BUF_IOCTL_XFER_CHARGE ioctl, we need a new access-control point for the case where charge_fd represents a cgroupfd.
Add security_memcg_charge_cgroup(), called in get_obj_cgroup_from_fd() when charge_fd refers to a cgroup2. The hook receives the credentials of the requesting process (cred) and the open file of the cgroup directory to charge (file), giving security modules a controlled enforcement point for cross-cgroup memcg attribution policy.
The file is passed instead of the cgroup so that a module can label-check the object the cgroupfd actually names. get_obj_cgroup_from_fd() resolves the memory css from that same file with css_tryget_online_from_dir(), so the fd cannot be swapped between the check and the charge.
When CONFIG_SECURITY is not set the hook compiles to an inline returning 0, adding no overhead to the fast path.
Signed-off-by: Albert Esteve aesteve@redhat.com --- include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 8 ++++++++ mm/memcontrol.c | 9 ++++++++- security/security.c | 20 ++++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 93c8bb5642f5..3b254b4e60ec 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -44,6 +44,7 @@ LSM_HOOK(int, 0, capset, struct cred *new, const struct cred *old, LSM_HOOK(int, 0, capable, const struct cred *cred, struct user_namespace *ns, int cap, unsigned int opts) LSM_HOOK(int, 0, memcg_charge_task, const struct cred *from, const struct cred *to) +LSM_HOOK(int, 0, memcg_charge_cgroup, const struct cred *cred, const struct file *file) LSM_HOOK(int, 0, quotactl, int cmds, int type, int id, const struct super_block *sb) LSM_HOOK(int, 0, quota_on, struct dentry *dentry) LSM_HOOK(int, 0, syslog, int type) diff --git a/include/linux/security.h b/include/linux/security.h index b21823225ed0..d1cd6c324ffd 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -353,6 +353,8 @@ int security_capable(const struct cred *cred, int cap, unsigned int opts); int security_memcg_charge_task(const struct cred *from, const struct cred *to); +int security_memcg_charge_cgroup(const struct cred *cred, + const struct file *file); int security_quotactl(int cmds, int type, int id, const struct super_block *sb); int security_quota_on(struct dentry *dentry); int security_syslog(int type); @@ -717,6 +719,12 @@ static inline int security_memcg_charge_task(const struct cred *from, return 0; }
+static inline int security_memcg_charge_cgroup(const struct cred *cred, + const struct file *file) +{ + return 0; +} + static inline int security_quotactl(int cmds, int type, int id, const struct super_block *sb) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 2e6fcbe14d43..693f0b2877bf 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3220,7 +3220,14 @@ struct obj_cgroup *get_obj_cgroup_from_fd(int fd) css = ERR_PTR(-EBADF); }
- if (IS_ERR(css)) { + if (!IS_ERR(css)) { + /* fd is a cgroupfd */ + ret = security_memcg_charge_cgroup(current_cred(), fd_file(f)); + if (ret) { + css_put(css); + return ERR_PTR(ret); + } + } else { if (PTR_ERR(css) != -EBADF) return ERR_CAST(css);
diff --git a/security/security.c b/security/security.c index e198e00b6a40..925b6c003059 100644 --- a/security/security.c +++ b/security/security.c @@ -689,6 +689,26 @@ int security_memcg_charge_task(const struct cred *from, const struct cred *to) return call_int_hook(memcg_charge_task, from, to); }
+/** + * security_memcg_charge_cgroup() - Check if charging memory to a cgroup is allowed + * @cred: credentials of the process requesting the charge + * @file: open file of the cgroup directory to charge + * + * Check whether the process with credentials @cred is allowed to charge + * memory to the cgroup referenced by @file. Called when userspace passes a + * cgroupfd to identify the charge target instead of a pidfd. @file is the + * file the cgroup was resolved from, so the check applies to the object + * that is actually charged. It must have been already validated by the + * caller. + * + * Return: Returns 0 if permission is granted. + */ +int security_memcg_charge_cgroup(const struct cred *cred, + const struct file *file) +{ + return call_int_hook(memcg_charge_cgroup, cred, file); +} + /** * security_quotactl() - Check if a quotactl() syscall is allowed for this fs * @cmds: commands