Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp41195lfv; Tue, 12 Apr 2022 16:27:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2hnAIzJ0K5RbiHndBkyHUoFPU63R+p4JGscSwAkRAURHLhLwD+wj+VhIUopwUHsT2Fi8X X-Received: by 2002:a17:902:f608:b0:158:29e6:c88 with SMTP id n8-20020a170902f60800b0015829e60c88mr21941148plg.174.1649806020309; Tue, 12 Apr 2022 16:27:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649806020; cv=none; d=google.com; s=arc-20160816; b=W40eKjEchaENEqMK2SAQbYdxtLesi9O9mR4vhYBmRcw2KloVAA0lkEF8MWy4kPclHK G+8A3CKg+E+Yzfrf4C8mwsX/aWoUvuWa7TsTLCizcaRxaYbeFgz/5nHky1mIWmXwOKYw VA+kqmA9zuol0Ezqievvml9cKYN5U7WBD/KahPZFIPLvuX52Wt+aS1nZHalR1f6ZylRs VkEAkMi0yH3s7uXx6mrym7EqaRjlFyBAPY+gyrJcDmw9Ptj4ANhFeCiDIq+T7TBJbvH8 hkthkBBTR/Se1Xpxz/AK5149twfiueo8KjG+pv6OvXybJAAdTp33bPVu9XCR/8En4Tg3 9b+Q== 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=gAM9gOX3/seVYT3D196x7EB5gQUOOgJXAlu9K1sLJ9M=; b=w4r3Uoumj2P15InzsEkHbd5mIiP4hyYZJVTkbeJRam3S/53QyMUXCBigJzWuaVjSEg B3RV5P8Fz8tJa+N/6BWt0w09J08oMLphMA0ydQZJF7aUwk+yedTpgdkYJwvKtQSiug+E wULjVUsI5fYuRX7DFpaAbY03M0f43iYPq1bifsw5tPVaf+pC5EXSEi+H7CaALGCsa0N7 aNBc3Ph6vzu98WStw/P5DH1E/iyPdx15UQIKgJYDdHhH8nDiDcpbgfVQv48EUsT3Z0aD MmsT9Ua7D9hYPt66GZ/F2RmBMGiKPeO4ilmQ4NHt+o46JRIN+F5opJdQ3h+KYlkgLGng qRAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Bx7JFS3T; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id 31-20020a63125f000000b0039d90cc655dsi3560801pgs.216.2022.04.12.16.26.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 16:27:00 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Bx7JFS3T; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CE377FE415; Tue, 12 Apr 2022 14:19:28 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244464AbiDLG7z (ORCPT + 99 others); Tue, 12 Apr 2022 02:59:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351461AbiDLGxk (ORCPT ); Tue, 12 Apr 2022 02:53:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E57CF37A28; Mon, 11 Apr 2022 23:41:04 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 802DD60B2F; Tue, 12 Apr 2022 06:41:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F83AC385A6; Tue, 12 Apr 2022 06:41:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649745663; bh=izc9W8FO0fiUBBEMSdWN6Sq6d8J779YYw3wNNUEbC3w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Bx7JFS3TgcWmfcqhGR3O/BSfTfH3p1yNH0Y8QD9EmkR9khBq0b0YuBj9rnTui7jFc TvVTRtpOPEMAJqfr84WYNko8EQGYOG0BaCqttpG7WG7c+cPJbxZA0ndPR+el4KXOOE A4e+MXPXc0BBy9zkkHdPg5EzNJMvgNIvZwz1+rUY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Eric W. Biederman" , Linus Torvalds , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Tejun Heo , Ovidiu Panait Subject: [PATCH 5.10 162/171] cgroup: Use open-time credentials for process migraton perm checks Date: Tue, 12 Apr 2022 08:30:53 +0200 Message-Id: <20220412062932.586417436@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412062927.870347203@linuxfoundation.org> References: <20220412062927.870347203@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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: apply original __cgroup_procs_write() changes to cgroup_threads_write() and cgroup_procs_write(), as the refactoring commit da70862efe006 ("cgroup: cgroup.{procs,threads} factor out common parts") is not present in 5.10-stable] 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 @@ -505,10 +505,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 @@ -4788,6 +4788,7 @@ static ssize_t cgroup_procs_write(struct struct cgroup_file_ctx *ctx = of->priv; struct cgroup *src_cgrp, *dst_cgrp; struct task_struct *task; + const struct cred *saved_cred; ssize_t ret; bool locked; @@ -4805,9 +4806,16 @@ 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_attach_permissions(src_cgrp, dst_cgrp, of->file->f_path.dentry->d_sb, true, ctx->ns); + revert_creds(saved_cred); if (ret) goto out_finish; @@ -4832,6 +4840,7 @@ static ssize_t cgroup_threads_write(stru struct cgroup_file_ctx *ctx = of->priv; struct cgroup *src_cgrp, *dst_cgrp; struct task_struct *task; + const struct cred *saved_cred; ssize_t ret; bool locked; @@ -4851,10 +4860,16 @@ 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_attach_permissions(src_cgrp, dst_cgrp, of->file->f_path.dentry->d_sb, false, ctx->ns); + revert_creds(saved_cred); if (ret) goto out_finish;