]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
config: Fix data race reported in cgroup_config_insert_into_mount_table
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Sun, 26 Jan 2025 06:01:25 +0000 (11:31 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 27 Jan 2025 15:07:25 +0000 (08:07 -0700)
Fix the following data race issue reported by Coverity:

CID 465888: (#1 of 1): Check of thread-shared field evades lock
acquisition (LOCK_EVASION):

"The data guarded by this critical section may be read while in an
inconsistent state or modified by multiple racing threads.

In cgroup_config_insert_into_mount_table: Checking the value of a
thread-shared field outside of a locked region to determine if a locked
operation involving that thread shared field has completed."

Fix it by moving the config_table_index value check too under the
config_table_lock.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/config.c

index b47a44d60784753884497cb4ca4e7f6a0b73efd3..afaf9aad604658d31ce97b8fb91ebf34dc92643a 100644 (file)
@@ -551,11 +551,13 @@ int cgroup_config_insert_into_mount_table(char *name, char *mount_point)
 {
        int i;
 
-       if (config_table_index >= CG_CONTROLLER_MAX)
-               return 0;
-
        pthread_rwlock_wrlock(&config_table_lock);
 
+       if (config_table_index >= CG_CONTROLLER_MAX) {
+               pthread_rwlock_unlock(&config_table_lock);
+               return 0;
+       }
+
        /* Merge controller names with the same mount point */
        for (i = 0; i < config_table_index; i++) {
                if (strcmp(config_mount_table[i].mount.path, mount_point) == 0) {