From patchwork Mon Oct 24 20:37:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 79075 Delivered-To: patches@linaro.org Received: by 10.140.97.247 with SMTP id m110csp2775113qge; Mon, 24 Oct 2016 13:37:04 -0700 (PDT) X-Received: by 10.98.87.79 with SMTP id l76mr31973155pfb.174.1477341424565; Mon, 24 Oct 2016 13:37:04 -0700 (PDT) Return-Path: Received: from mail-pf0-x22a.google.com (mail-pf0-x22a.google.com. [2607:f8b0:400e:c00::22a]) by mx.google.com with ESMTPS id n5si600933pac.62.2016.10.24.13.37.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Oct 2016 13:37:04 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22a as permitted sender) client-ip=2607:f8b0:400e:c00::22a; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22a as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by mail-pf0-x22a.google.com with SMTP id s8so104690761pfj.2 for ; Mon, 24 Oct 2016 13:37:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=3jV6/y9J+dFYuY3fcD8gcYrRGiJqO5R8lVl66P/iRb0=; b=gZoU2NrIheau/8uX46xl1KE0KjBlZvKaPytPbFBHuQYenUhH6xmJ0OueRyG1OViDEJ jZfBafrXPTap+3u+314yjQOu7lYA1woFMOhPffVvvKer+DjUcsvyxMxxuhNRV/cxOYGm T3SJ2k/jVmivoeHz881RRAaNHedLmTnGwfDZw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=3jV6/y9J+dFYuY3fcD8gcYrRGiJqO5R8lVl66P/iRb0=; b=UcurPzQsSNCoAwQhUOYXGHJwTnrj2FUeEeHpRF7r0qzbaMF9FN93dWypVIMxvNVFit d8jk+GgK07JUQ5IhJkGwUlYx1uRAWbv/KhikytSlPZQNljDD5RrOXVTlYHLA4RqngQxp VloVcdOJoLq5S5uQ3zYVwbTxPnqbuRrAIn+/vaeU8nvUevv+qJw+Gi17FAEEsJQE4IHy 0shVkx7VWOJSNo0YTPpUjiFHl71zGVikGQpSADeMEINa+kGLi8yk/fdf6Srtfptk39vn UzgpUVpbXhyv9s61UC7ASE7/8dg3Ar0yO1yPFE1NknPn5QdLUzr6S79qUqbSUyDQEdbA +mqA== X-Gm-Message-State: ABUngvcLMDRYFTvnttiHAdr1/4Ee9w7LkidBH8wwwuw1JmXkqggerMiIhIHfR2gS6Dcq4KrQMeA= X-Received: by 10.99.150.10 with SMTP id c10mr26365504pge.94.1477341424227; Mon, 24 Oct 2016 13:37:04 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:1002:83f0:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id bx5sm27468006pad.6.2016.10.24.13.37.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 24 Oct 2016 13:37:03 -0700 (PDT) From: John Stultz To: lkml Cc: John Stultz , Tejun Heo , Li Zefan , Jonathan Corbet , cgroups@vger.kernel.org, Android Kernel Team , Rom Lemarchand , Colin Cross , Dmitry Shmidt , Todd Kjos , Christian Poetzsch , Amit Pundir , Dmitry Torokhov , Kees Cook , "Serge E . Hallyn" , linux-api@vger.kernel.org Subject: [PATCH v4] cgroup: Use CAP_SYS_RESOURCE to allow a process to migrate other tasks between cgroups Date: Mon, 24 Oct 2016 13:37:00 -0700 Message-Id: <1477341420-21857-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 This patch adds logic to allows a process to migrate other tasks between cgroups if they have CAP_SYS_RESOURCE. In Android (where this feature originated), the ActivityManager tracks various application states (TOP_APP, FOREGROUND, BACKGROUND, SYSTEM, etc), and then as applications change states, the SchedPolicy logic will migrate the application tasks between different cgroups used to control the different application states (for example, there is a background cpuset cgroup which can limit background tasks to stay on one low-power cpu, and the bg_non_interactive cpuctrl cgroup can then further limit those background tasks to a small percentage of that one cpu's cpu time). However, for security reasons, Android doesn't want to make the system_server (the process that runs the ActivityManager and SchedPolicy logic), run as root. So in the Android common.git kernel, they have some logic to allow cgroups to loosen their permissions so CAP_SYS_NICE tasks can migrate other tasks between cgroups. I feel the approach taken there overloads CAP_SYS_NICE a bit much for non-android environments. So this patch, as suggested by Michael Kerrisk, simply adds a check for CAP_SYS_RESOURCE. I've tested this with AOSP master, and this seems to work well as Zygote and system_server already use CAP_SYS_RESOURCE. I've also submitted patches against the android-4.4 kernel to change it to use CAP_SYS_RESOURCE, and the Android developers just merged it. Thouhts and feedback would be appreciated! Cc: Tejun Heo Cc: Li Zefan Cc: Jonathan Corbet Cc: cgroups@vger.kernel.org Cc: Android Kernel Team Cc: Rom Lemarchand Cc: Colin Cross Cc: Dmitry Shmidt Cc: Todd Kjos Cc: Christian Poetzsch Cc: Amit Pundir Cc: Dmitry Torokhov Cc: Kees Cook Cc: Serge E. Hallyn Cc: linux-api@vger.kernel.org Acked-by: Serge Hallyn Signed-off-by: John Stultz --- v2: Renamed to just CAP_CGROUP_MIGRATE as reccomended by Tejun v3: Switched to just using CAP_SYS_RESOURCE as suggested by Michael v4: Send out properly folded down version of the patch. :P --- kernel/cgroup.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) -- 2.7.4 diff --git a/kernel/cgroup.c b/kernel/cgroup.c index 85bc9be..866059a 100644 --- a/kernel/cgroup.c +++ b/kernel/cgroup.c @@ -2856,7 +2856,8 @@ static int cgroup_procs_write_permission(struct task_struct *task, */ if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && !uid_eq(cred->euid, tcred->uid) && - !uid_eq(cred->euid, tcred->suid)) + !uid_eq(cred->euid, tcred->suid) && + !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE)) ret = -EACCES; if (!ret && cgroup_on_dfl(dst_cgrp)) {