From nobody Wed Apr 8 10:17:02 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4DC09C04A95 for ; Tue, 25 Oct 2022 11:02:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231184AbiJYLCc (ORCPT ); Tue, 25 Oct 2022 07:02:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230245AbiJYLC2 (ORCPT ); Tue, 25 Oct 2022 07:02:28 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 957F3DFB4B; Tue, 25 Oct 2022 04:02:25 -0700 (PDT) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4MxTV22n9xzJn7j; Tue, 25 Oct 2022 18:59:38 +0800 (CST) Received: from dggpemm500001.china.huawei.com (7.185.36.107) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 25 Oct 2022 19:02:23 +0800 Received: from octopus.huawei.com (10.67.174.191) by dggpemm500001.china.huawei.com (7.185.36.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 25 Oct 2022 19:02:23 +0800 From: Wang Weiyang To: , , , , , CC: , Subject: [PATCH] device_cgroup: Roll back to original exceptions after copy failure Date: Tue, 25 Oct 2022 19:31:01 +0800 Message-ID: <20221025113101.41132-1-wangweiyang2@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.67.174.191] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm500001.china.huawei.com (7.185.36.107) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When add the 'a *:* rwm' entry to devcgroup A's whitelist, at first A's exceptions will be cleaned and A's behavior is changed to DEVCG_DEFAULT_ALLOW. Then parent's exceptions will be copyed to A's whitelist. If copy failure occurs, just return leaving A to grant permissions to all devices. And A may grant more permissions than parent. Backup A's whitelist and recover original exceptions after copy failure. Fixes: 4cef7299b478 ("device_cgroup: add proper checking when changing defa= ult behavior") Signed-off-by: Wang Weiyang Reviewed-by: Aristeu Rozanski --- security/device_cgroup.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/security/device_cgroup.c b/security/device_cgroup.c index a9f8c63a96d1..bef2b9285fb3 100644 --- a/security/device_cgroup.c +++ b/security/device_cgroup.c @@ -82,6 +82,17 @@ static int dev_exceptions_copy(struct list_head *dest, s= truct list_head *orig) return -ENOMEM; } =20 +static void dev_exceptions_move(struct list_head *dest, struct list_head *= orig) +{ + struct dev_exception_item *ex, *tmp; + + lockdep_assert_held(&devcgroup_mutex); + + list_for_each_entry_safe(ex, tmp, orig, list) { + list_move_tail(&ex->list, dest); + } +} + /* * called under devcgroup_mutex */ @@ -604,11 +615,13 @@ static int devcgroup_update_access(struct dev_cgroup = *devcgroup, int count, rc =3D 0; struct dev_exception_item ex; struct dev_cgroup *parent =3D css_to_devcgroup(devcgroup->css.parent); + struct dev_cgroup tmp_devcgrp; =20 if (!capable(CAP_SYS_ADMIN)) return -EPERM; =20 memset(&ex, 0, sizeof(ex)); + memset(&tmp_devcgrp, 0, sizeof(tmp_devcgrp)); b =3D buffer; =20 switch (*b) { @@ -620,15 +633,27 @@ static int devcgroup_update_access(struct dev_cgroup = *devcgroup, =20 if (!may_allow_all(parent)) return -EPERM; - dev_exception_clean(devcgroup); - devcgroup->behavior =3D DEVCG_DEFAULT_ALLOW; - if (!parent) + if (!parent) { + devcgroup->behavior =3D DEVCG_DEFAULT_ALLOW; + dev_exception_clean(devcgroup); break; + } =20 + INIT_LIST_HEAD(&tmp_devcgrp.exceptions); + rc =3D dev_exceptions_copy(&tmp_devcgrp.exceptions, + &devcgroup->exceptions); + if (rc) + return rc; + dev_exception_clean(devcgroup); rc =3D dev_exceptions_copy(&devcgroup->exceptions, &parent->exceptions); - if (rc) + if (rc) { + dev_exceptions_move(&devcgroup->exceptions, + &tmp_devcgrp.exceptions); return rc; + } + devcgroup->behavior =3D DEVCG_DEFAULT_ALLOW; + dev_exception_clean(&tmp_devcgrp); break; case DEVCG_DENY: if (css_has_online_children(&devcgroup->css)) --=20 2.17.1