device_cgroup: fix RCU imbalance in error case [Linux 3.16.72]

This Linux kernel change "device_cgroup: fix RCU imbalance in error case" is included in the Linux 3.16.72 release. This change is authored by Jann Horn <jannh [at] google.com> on Tue Mar 19 02:36:59 2019 +0100. The commit for this change in Linux stable tree is 198f512 (patch) which is from upstream commit 0fcc4c8. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 0fcc4c8.

device_cgroup: fix RCU imbalance in error case

commit 0fcc4c8c044e117ac126ab6df4138ea9a67fa2a9 upstream.

When dev_exception_add() returns an error (due to a failed memory
allocation), make sure that we move the RCU preemption count back to where
it was before we were called. We dropped the RCU read lock inside the loop
body, so we can't just "break".

sparse complains about this, too:

$ make -s C=2 security/device_cgroup.o
./include/linux/rcupdate.h:647:9: warning: context imbalance in
'propagate_exception' - unexpected unlock

Fixes: d591fb56618f ("device_cgroup: simplify cgroup tree walk in propagate_exception()")
Signed-off-by: Jann Horn <[email protected]>
Acked-by: Michal Hocko <[email protected]>
Signed-off-by: Tejun Heo <[email protected]>
Signed-off-by: Ben Hutchings <[email protected]>

There are 2 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 security/device_cgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/security/device_cgroup.c b/security/device_cgroup.c
index d9d69e6..8fee5f7 100644
--- a/security/device_cgroup.c
+++ b/security/device_cgroup.c
@@ -568,7 +568,7 @@ static int propagate_exception(struct dev_cgroup *devcg_root,
            devcg->behavior == DEVCG_DEFAULT_ALLOW) {
            rc = dev_exception_add(devcg, ex);
            if (rc)
-               break;
+               return rc;
        } else {
            /*
             * in the other possible cases:

Leave a Reply

Your email address will not be published. Required fields are marked *