Restricts the mounting of tmpfs: mount -t tmpfs -o memcg=<cgroup> Only if the mounting task is allowed to open <cgroup>/cgroup.procs file and allowed to enter the cgroup. Thus, processes are allowed to direct tmpfs changes to a cgroup that they themselves can enter and allocate memory in. Signed-off-by: Mina Almasry <almasrymina@xxxxxxxxxx> Cc: Michal Hocko <mhocko@xxxxxxxx> Cc: Theodore Ts'o <tytso@xxxxxxx> Cc: Greg Thelen <gthelen@xxxxxxxxxx> Cc: Shakeel Butt <shakeelb@xxxxxxxxxx> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> Cc: Hugh Dickins <hughd@xxxxxxxxxx> Cc: Roman Gushchin <songmuchun@xxxxxxxxxxxxx> Cc: Johannes Weiner <hannes@xxxxxxxxxxx> Cc: Hugh Dickins <hughd@xxxxxxxxxx> Cc: Tejun Heo <tj@xxxxxxxxxx> Cc: Vladimir Davydov <vdavydov.dev@xxxxxxxxx> Cc: Muchun Song <songmuchun@xxxxxxxxxxxxx> Cc: riel@xxxxxxxxxxx Cc: linux-mm@xxxxxxxxx Cc: linux-fsdevel@xxxxxxxxxxxxxxx Cc: cgroups@xxxxxxxxxxxxxxx --- mm/memcontrol.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 389d2f2be9674..2e4c20d09f959 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -62,6 +62,7 @@ #include <linux/tracehook.h> #include <linux/psi.h> #include <linux/seq_buf.h> +#include <linux/string.h> #include "internal.h" #include <net/sock.h> #include <net/ip.h> @@ -2585,9 +2586,32 @@ void mem_cgroup_handle_over_high(void) */ struct mem_cgroup *mem_cgroup_get_from_path(const char *path) { - struct file *file; + static const char procs_filename[] = "/cgroup.procs"; + struct file *file, *procs; struct cgroup_subsys_state *css; struct mem_cgroup *memcg; + char *procs_path = + kmalloc(strlen(path) + sizeof(procs_filename), GFP_KERNEL); + + if (procs_path == NULL) + return ERR_PTR(-ENOMEM); + strcpy(procs_path, path); + strcat(procs_path, procs_filename); + + procs = filp_open(procs_path, O_WRONLY, 0); + kfree(procs_path); + + /* + * Restrict the capability for tasks to mount with memcg charging to the + * cgroup they could not join. For example, disallow: + * + * mount -t tmpfs -o memcg=root-cgroup nodev <MOUNT_DIR> + * + * if it is a non-root task. + */ + if (IS_ERR(procs)) + return (struct mem_cgroup *)procs; + fput(procs); file = filp_open(path, O_DIRECTORY | O_RDONLY, 0); if (IS_ERR(file)) -- 2.34.0.rc0.344.g81b53c2807-goog