Back to home page

LXR

 
 

    


0001 /*
0002  * Process number limiting controller for cgroups.
0003  *
0004  * Used to allow a cgroup hierarchy to stop any new processes from fork()ing
0005  * after a certain limit is reached.
0006  *
0007  * Since it is trivial to hit the task limit without hitting any kmemcg limits
0008  * in place, PIDs are a fundamental resource. As such, PID exhaustion must be
0009  * preventable in the scope of a cgroup hierarchy by allowing resource limiting
0010  * of the number of tasks in a cgroup.
0011  *
0012  * In order to use the `pids` controller, set the maximum number of tasks in
0013  * pids.max (this is not available in the root cgroup for obvious reasons). The
0014  * number of processes currently in the cgroup is given by pids.current.
0015  * Organisational operations are not blocked by cgroup policies, so it is
0016  * possible to have pids.current > pids.max. However, it is not possible to
0017  * violate a cgroup policy through fork(). fork() will return -EAGAIN if forking
0018  * would cause a cgroup policy to be violated.
0019  *
0020  * To set a cgroup to have no limit, set pids.max to "max". This is the default
0021  * for all new cgroups (N.B. that PID limits are hierarchical, so the most
0022  * stringent limit in the hierarchy is followed).
0023  *
0024  * pids.current tracks all child cgroup hierarchies, so parent/pids.current is
0025  * a superset of parent/child/pids.current.
0026  *
0027  * Copyright (C) 2015 Aleksa Sarai <cyphar@cyphar.com>
0028  *
0029  * This file is subject to the terms and conditions of version 2 of the GNU
0030  * General Public License.  See the file COPYING in the main directory of the
0031  * Linux distribution for more details.
0032  */
0033 
0034 #include <linux/kernel.h>
0035 #include <linux/threads.h>
0036 #include <linux/atomic.h>
0037 #include <linux/cgroup.h>
0038 #include <linux/slab.h>
0039 
0040 #define PIDS_MAX (PID_MAX_LIMIT + 1ULL)
0041 #define PIDS_MAX_STR "max"
0042 
0043 struct pids_cgroup {
0044     struct cgroup_subsys_state  css;
0045 
0046     /*
0047      * Use 64-bit types so that we can safely represent "max" as
0048      * %PIDS_MAX = (%PID_MAX_LIMIT + 1).
0049      */
0050     atomic64_t          counter;
0051     int64_t             limit;
0052 
0053     /* Handle for "pids.events" */
0054     struct cgroup_file      events_file;
0055 
0056     /* Number of times fork failed because limit was hit. */
0057     atomic64_t          events_limit;
0058 };
0059 
0060 static struct pids_cgroup *css_pids(struct cgroup_subsys_state *css)
0061 {
0062     return container_of(css, struct pids_cgroup, css);
0063 }
0064 
0065 static struct pids_cgroup *parent_pids(struct pids_cgroup *pids)
0066 {
0067     return css_pids(pids->css.parent);
0068 }
0069 
0070 static struct cgroup_subsys_state *
0071 pids_css_alloc(struct cgroup_subsys_state *parent)
0072 {
0073     struct pids_cgroup *pids;
0074 
0075     pids = kzalloc(sizeof(struct pids_cgroup), GFP_KERNEL);
0076     if (!pids)
0077         return ERR_PTR(-ENOMEM);
0078 
0079     pids->limit = PIDS_MAX;
0080     atomic64_set(&pids->counter, 0);
0081     atomic64_set(&pids->events_limit, 0);
0082     return &pids->css;
0083 }
0084 
0085 static void pids_css_free(struct cgroup_subsys_state *css)
0086 {
0087     kfree(css_pids(css));
0088 }
0089 
0090 /**
0091  * pids_cancel - uncharge the local pid count
0092  * @pids: the pid cgroup state
0093  * @num: the number of pids to cancel
0094  *
0095  * This function will WARN if the pid count goes under 0, because such a case is
0096  * a bug in the pids controller proper.
0097  */
0098 static void pids_cancel(struct pids_cgroup *pids, int num)
0099 {
0100     /*
0101      * A negative count (or overflow for that matter) is invalid,
0102      * and indicates a bug in the `pids` controller proper.
0103      */
0104     WARN_ON_ONCE(atomic64_add_negative(-num, &pids->counter));
0105 }
0106 
0107 /**
0108  * pids_uncharge - hierarchically uncharge the pid count
0109  * @pids: the pid cgroup state
0110  * @num: the number of pids to uncharge
0111  */
0112 static void pids_uncharge(struct pids_cgroup *pids, int num)
0113 {
0114     struct pids_cgroup *p;
0115 
0116     for (p = pids; parent_pids(p); p = parent_pids(p))
0117         pids_cancel(p, num);
0118 }
0119 
0120 /**
0121  * pids_charge - hierarchically charge the pid count
0122  * @pids: the pid cgroup state
0123  * @num: the number of pids to charge
0124  *
0125  * This function does *not* follow the pid limit set. It cannot fail and the new
0126  * pid count may exceed the limit. This is only used for reverting failed
0127  * attaches, where there is no other way out than violating the limit.
0128  */
0129 static void pids_charge(struct pids_cgroup *pids, int num)
0130 {
0131     struct pids_cgroup *p;
0132 
0133     for (p = pids; parent_pids(p); p = parent_pids(p))
0134         atomic64_add(num, &p->counter);
0135 }
0136 
0137 /**
0138  * pids_try_charge - hierarchically try to charge the pid count
0139  * @pids: the pid cgroup state
0140  * @num: the number of pids to charge
0141  *
0142  * This function follows the set limit. It will fail if the charge would cause
0143  * the new value to exceed the hierarchical limit. Returns 0 if the charge
0144  * succeeded, otherwise -EAGAIN.
0145  */
0146 static int pids_try_charge(struct pids_cgroup *pids, int num)
0147 {
0148     struct pids_cgroup *p, *q;
0149 
0150     for (p = pids; parent_pids(p); p = parent_pids(p)) {
0151         int64_t new = atomic64_add_return(num, &p->counter);
0152 
0153         /*
0154          * Since new is capped to the maximum number of pid_t, if
0155          * p->limit is %PIDS_MAX then we know that this test will never
0156          * fail.
0157          */
0158         if (new > p->limit)
0159             goto revert;
0160     }
0161 
0162     return 0;
0163 
0164 revert:
0165     for (q = pids; q != p; q = parent_pids(q))
0166         pids_cancel(q, num);
0167     pids_cancel(p, num);
0168 
0169     return -EAGAIN;
0170 }
0171 
0172 static int pids_can_attach(struct cgroup_taskset *tset)
0173 {
0174     struct task_struct *task;
0175     struct cgroup_subsys_state *dst_css;
0176 
0177     cgroup_taskset_for_each(task, dst_css, tset) {
0178         struct pids_cgroup *pids = css_pids(dst_css);
0179         struct cgroup_subsys_state *old_css;
0180         struct pids_cgroup *old_pids;
0181 
0182         /*
0183          * No need to pin @old_css between here and cancel_attach()
0184          * because cgroup core protects it from being freed before
0185          * the migration completes or fails.
0186          */
0187         old_css = task_css(task, pids_cgrp_id);
0188         old_pids = css_pids(old_css);
0189 
0190         pids_charge(pids, 1);
0191         pids_uncharge(old_pids, 1);
0192     }
0193 
0194     return 0;
0195 }
0196 
0197 static void pids_cancel_attach(struct cgroup_taskset *tset)
0198 {
0199     struct task_struct *task;
0200     struct cgroup_subsys_state *dst_css;
0201 
0202     cgroup_taskset_for_each(task, dst_css, tset) {
0203         struct pids_cgroup *pids = css_pids(dst_css);
0204         struct cgroup_subsys_state *old_css;
0205         struct pids_cgroup *old_pids;
0206 
0207         old_css = task_css(task, pids_cgrp_id);
0208         old_pids = css_pids(old_css);
0209 
0210         pids_charge(old_pids, 1);
0211         pids_uncharge(pids, 1);
0212     }
0213 }
0214 
0215 /*
0216  * task_css_check(true) in pids_can_fork() and pids_cancel_fork() relies
0217  * on threadgroup_change_begin() held by the copy_process().
0218  */
0219 static int pids_can_fork(struct task_struct *task)
0220 {
0221     struct cgroup_subsys_state *css;
0222     struct pids_cgroup *pids;
0223     int err;
0224 
0225     css = task_css_check(current, pids_cgrp_id, true);
0226     pids = css_pids(css);
0227     err = pids_try_charge(pids, 1);
0228     if (err) {
0229         /* Only log the first time events_limit is incremented. */
0230         if (atomic64_inc_return(&pids->events_limit) == 1) {
0231             pr_info("cgroup: fork rejected by pids controller in ");
0232             pr_cont_cgroup_path(task_cgroup(current, pids_cgrp_id));
0233             pr_cont("\n");
0234         }
0235         cgroup_file_notify(&pids->events_file);
0236     }
0237     return err;
0238 }
0239 
0240 static void pids_cancel_fork(struct task_struct *task)
0241 {
0242     struct cgroup_subsys_state *css;
0243     struct pids_cgroup *pids;
0244 
0245     css = task_css_check(current, pids_cgrp_id, true);
0246     pids = css_pids(css);
0247     pids_uncharge(pids, 1);
0248 }
0249 
0250 static void pids_free(struct task_struct *task)
0251 {
0252     struct pids_cgroup *pids = css_pids(task_css(task, pids_cgrp_id));
0253 
0254     pids_uncharge(pids, 1);
0255 }
0256 
0257 static ssize_t pids_max_write(struct kernfs_open_file *of, char *buf,
0258                   size_t nbytes, loff_t off)
0259 {
0260     struct cgroup_subsys_state *css = of_css(of);
0261     struct pids_cgroup *pids = css_pids(css);
0262     int64_t limit;
0263     int err;
0264 
0265     buf = strstrip(buf);
0266     if (!strcmp(buf, PIDS_MAX_STR)) {
0267         limit = PIDS_MAX;
0268         goto set_limit;
0269     }
0270 
0271     err = kstrtoll(buf, 0, &limit);
0272     if (err)
0273         return err;
0274 
0275     if (limit < 0 || limit >= PIDS_MAX)
0276         return -EINVAL;
0277 
0278 set_limit:
0279     /*
0280      * Limit updates don't need to be mutex'd, since it isn't
0281      * critical that any racing fork()s follow the new limit.
0282      */
0283     pids->limit = limit;
0284     return nbytes;
0285 }
0286 
0287 static int pids_max_show(struct seq_file *sf, void *v)
0288 {
0289     struct cgroup_subsys_state *css = seq_css(sf);
0290     struct pids_cgroup *pids = css_pids(css);
0291     int64_t limit = pids->limit;
0292 
0293     if (limit >= PIDS_MAX)
0294         seq_printf(sf, "%s\n", PIDS_MAX_STR);
0295     else
0296         seq_printf(sf, "%lld\n", limit);
0297 
0298     return 0;
0299 }
0300 
0301 static s64 pids_current_read(struct cgroup_subsys_state *css,
0302                  struct cftype *cft)
0303 {
0304     struct pids_cgroup *pids = css_pids(css);
0305 
0306     return atomic64_read(&pids->counter);
0307 }
0308 
0309 static int pids_events_show(struct seq_file *sf, void *v)
0310 {
0311     struct pids_cgroup *pids = css_pids(seq_css(sf));
0312 
0313     seq_printf(sf, "max %lld\n", (s64)atomic64_read(&pids->events_limit));
0314     return 0;
0315 }
0316 
0317 static struct cftype pids_files[] = {
0318     {
0319         .name = "max",
0320         .write = pids_max_write,
0321         .seq_show = pids_max_show,
0322         .flags = CFTYPE_NOT_ON_ROOT,
0323     },
0324     {
0325         .name = "current",
0326         .read_s64 = pids_current_read,
0327         .flags = CFTYPE_NOT_ON_ROOT,
0328     },
0329     {
0330         .name = "events",
0331         .seq_show = pids_events_show,
0332         .file_offset = offsetof(struct pids_cgroup, events_file),
0333         .flags = CFTYPE_NOT_ON_ROOT,
0334     },
0335     { } /* terminate */
0336 };
0337 
0338 struct cgroup_subsys pids_cgrp_subsys = {
0339     .css_alloc  = pids_css_alloc,
0340     .css_free   = pids_css_free,
0341     .can_attach     = pids_can_attach,
0342     .cancel_attach  = pids_cancel_attach,
0343     .can_fork   = pids_can_fork,
0344     .cancel_fork    = pids_cancel_fork,
0345     .free       = pids_free,
0346     .legacy_cftypes = pids_files,
0347     .dfl_cftypes    = pids_files,
0348 };