[PATCH] memcg: Limit the number of events registered on oom_control

From: Michal Hocko
Date: Wed Aug 07 2013 - 12:51:40 EST


There is no limit for the maximum number of oom_control events
registered per memcg. This might lead to an user triggered memory
depletion if a regular user is allowed to register events because one
file descriptor might be used for arbitrary number of registrations.

Let's be more strict and cap the number of events to the number of
open file descriptors. Teach mem_cgroup_oom_register_event to check
the registered events and fail if the given cftype (target file file
descriptor) has been already registered. This will cap the number of
events to the maximum number of allowed open files so no user who is
allowed to register event can runaway the an existing limit.

Signed-off-by: Michal Hocko <mhocko@xxxxxxx>
---
mm/memcontrol.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 8247db3..f893a39 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -212,6 +212,7 @@ struct mem_cgroup_thresholds {
struct mem_cgroup_eventfd_list {
struct list_head list;
struct eventfd_ctx *eventfd;
+ struct cftype *cft;
};

static void mem_cgroup_threshold(struct mem_cgroup *memcg);
@@ -5577,6 +5578,7 @@ static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
struct mem_cgroup_eventfd_list *event;
enum res_type type = MEMFILE_TYPE(cft->private);
+ struct mem_cgroup_eventfd_list *ev, *tmp;

BUG_ON(type != _OOM_TYPE);
event = kmalloc(sizeof(*event), GFP_KERNEL);
@@ -5585,7 +5587,20 @@ static int mem_cgroup_oom_register_event(struct cgroup *cgrp,

spin_lock(&memcg_oom_lock);

+ /*
+ * Make sure that only one event is registered for the given
+ * cftype - aka file descriptor. This caps the number of events
+ * to number of file descriptors per process so the userspace doesn't
+ * escape and depleate the memory.
+ */
+ list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
+ if (ev->cft == cft) {
+ kfree(event);
+ return -EINVAL;
+ }
+ }
event->eventfd = eventfd;
+ event->cft = cft;
list_add(&event->list, &memcg->oom_notify);

/* already in OOM ? */
--
1.7.10.4

--
Michal Hocko
SUSE Labs
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/