Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2954067pxb; Mon, 18 Apr 2022 11:52:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzoUMJv/eiNyUnQhejkjWpjI01GewtF4vcXac6kND47nptBkYDuDpYlnq8FwnF6F/71ciCC X-Received: by 2002:a17:90a:2b0f:b0:1cb:a3e5:413b with SMTP id x15-20020a17090a2b0f00b001cba3e5413bmr14402084pjc.115.1650307942664; Mon, 18 Apr 2022 11:52:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650307942; cv=none; d=google.com; s=arc-20160816; b=dUXCQRfscHaj7k688XYQQq0voBVzRe/5EMXTzFJ6wQ9Pi6NPwvJu5IwsAZuzB0io/H nRRns3U5RwwB+erP7TZcbEiyshYHU9MXbmnp5tcROM1eQ/XRVGegy2cWzxdUPJdljEs8 Rw6L7PAvpY6+QCOktGWUrTbbJ/atDlc+KDiYNN9Xo48Y9SgyQzwzTAdHFGZU4YPLUwZE MOhl9GzblsnsNh6TnvNft+SbnJA/wg4F2lcAMpwLU3ZgA4QZGv3BgI7ozYoqgMwDPgoB 8IwEaeJxCnmKzeuRdjnJ/jqXzmCOqv+1WMkJOmqv+3jRZGEAVL64Tk4JlcFkdQ4WkUKF im6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Hz97p6jiLwzzHR1PP+f/CvOcwEujO9ZVgHkoX/gnmts=; b=ujmPMAJTnPjcFM4eIH7ht6MkKv758LzQkWKWEO4gxbdVILf4kZBBP26CYd18NlTu0G 8p+gP0P2Qzwnex5S484c2m+pc2gpgM4EDuSQ6RJb+ckUr+DNXKYGDTEP8SsVkk9eZrcE cJh24ZOOn0WnU8H0aAaygbES8OIfaoU+D/ybVn7SXAzqJ5zL+goiUvGrZqFCQpg8HHtn nzYqK9C7e5peZs+XkA5fzCM7UA/MlewBR7DnYF5dSgZ5a8tP3IwtAFXq6/mrXoUWuw3U MP+7W8Q4TKQqZtmsrRf17+g9u2OtjxOfDX6l5zCYxZQfjE1N5S7MD3OfXqZz0gmHE1My Sc4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yP1xb6P2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q10-20020a63bc0a000000b003a1e6570398si9616753pge.871.2022.04.18.11.52.07; Mon, 18 Apr 2022 11:52:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yP1xb6P2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245127AbiDROG6 (ORCPT + 99 others); Mon, 18 Apr 2022 10:06:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239972AbiDRNuh (ORCPT ); Mon, 18 Apr 2022 09:50:37 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8ABC943AFE; Mon, 18 Apr 2022 06:01:53 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 25285B80EC3; Mon, 18 Apr 2022 13:01:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F780C385A7; Mon, 18 Apr 2022 13:01:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286910; bh=GR6Dfnbb9uN0r2QYDYz1vhQwHWD3lOdfcpRoWQ+ksZ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yP1xb6P2ws+Uvo3C+XwyFqJDVA8Oof7xCet1o00FrZUlRWbRLp1dIs3Lliwkh12p0 h+RXuz2hrZsdB67zvKJu03t+0vv7woa7go9/9U5UEWU/sUPHN7Gu8SyNtmJ0Mp8q5O 9bNd0UpqBS66N49xnIDWNK9DONulvIjujcwhylr4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , "Eric W. Biederman" , Linus Torvalds , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Tejun Heo , Ovidiu Panait Subject: [PATCH 4.14 257/284] cgroup: Use open-time credentials for process migraton perm checks Date: Mon, 18 Apr 2022 14:13:58 +0200 Message-Id: <20220418121219.735989441@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tejun Heo commit 1756d7994ad85c2479af6ae5a9750b92324685af upstream. cgroup process migration permission checks are performed at write time as whether a given operation is allowed or not is dependent on the content of the write - the PID. This currently uses current's credentials which is a potential security weakness as it may allow scenarios where a less privileged process tricks a more privileged one into writing into a fd that it created. This patch makes both cgroup2 and cgroup1 process migration interfaces to use the credentials saved at the time of open (file->f_cred) instead of current's. Reported-by: "Eric W. Biederman" Suggested-by: Linus Torvalds Fixes: 187fe84067bd ("cgroup: require write perm on common ancestor when moving processes on the default hierarchy") Reviewed-by: Michal Koutný Signed-off-by: Tejun Heo [OP: backport to v4.14: apply original __cgroup_procs_write() changes to cgroup_threads_write() and cgroup_procs_write()] Signed-off-by: Ovidiu Panait Signed-off-by: Greg Kroah-Hartman --- kernel/cgroup/cgroup-v1.c | 7 ++++--- kernel/cgroup/cgroup.c | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) --- a/kernel/cgroup/cgroup-v1.c +++ b/kernel/cgroup/cgroup-v1.c @@ -535,10 +535,11 @@ static ssize_t __cgroup1_procs_write(str goto out_unlock; /* - * Even if we're attaching all tasks in the thread group, we only - * need to check permissions on one of them. + * Even if we're attaching all tasks in the thread group, we only need + * to check permissions on one of them. Check permissions using the + * credentials from file open to protect against inherited fd attacks. */ - cred = current_cred(); + cred = of->file->f_cred; tcred = get_task_cred(task); if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) && !uid_eq(cred->euid, tcred->uid) && --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -4381,6 +4381,7 @@ static ssize_t cgroup_procs_write(struct { struct cgroup *src_cgrp, *dst_cgrp; struct task_struct *task; + const struct cred *saved_cred; ssize_t ret; dst_cgrp = cgroup_kn_lock_live(of->kn, false); @@ -4397,8 +4398,15 @@ static ssize_t cgroup_procs_write(struct src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); spin_unlock_irq(&css_set_lock); + /* + * Process and thread migrations follow same delegation rule. Check + * permissions using the credentials from file open to protect against + * inherited fd attacks. + */ + saved_cred = override_creds(of->file->f_cred); ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, of->file->f_path.dentry->d_sb); + revert_creds(saved_cred); if (ret) goto out_finish; @@ -4422,6 +4430,7 @@ static ssize_t cgroup_threads_write(stru { struct cgroup *src_cgrp, *dst_cgrp; struct task_struct *task; + const struct cred *saved_cred; ssize_t ret; buf = strstrip(buf); @@ -4440,9 +4449,15 @@ static ssize_t cgroup_threads_write(stru src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); spin_unlock_irq(&css_set_lock); - /* thread migrations follow the cgroup.procs delegation rule */ + /* + * Process and thread migrations follow same delegation rule. Check + * permissions using the credentials from file open to protect against + * inherited fd attacks. + */ + saved_cred = override_creds(of->file->f_cred); ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, of->file->f_path.dentry->d_sb); + revert_creds(saved_cred); if (ret) goto out_finish;