]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
src/config: Fix data race reported by Coverity
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Sun, 19 Jan 2025 05:48:02 +0000 (11:18 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 24 Jan 2025 14:27:08 +0000 (07:27 -0700)
Fix the following data race issue reported by Coverity:

CID 465888: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_namespace_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 namespace_table_index value check too under the
namespace_table_lock

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

index b489b668c282bf147c54aad4bbcf05ddb0e31959..b47a44d60784753884497cb4ca4e7f6a0b73efd3 100644 (file)
@@ -599,11 +599,13 @@ int cgroup_config_insert_into_namespace_table(char *name, char *nspath)
 {
        char *ns_tbl_name, *ns_tbl_path;
 
-       if (namespace_table_index >= CG_CONTROLLER_MAX)
-               return 0;
-
        pthread_rwlock_wrlock(&namespace_table_lock);
 
+       if (namespace_table_index >= CG_CONTROLLER_MAX) {
+               pthread_rwlock_unlock(&namespace_table_lock);
+               return 0;
+       }
+
        ns_tbl_name = config_namespace_table[namespace_table_index].name;
        strncpy(ns_tbl_name, name, CONTROL_NAMELEN_MAX - 1);
        ns_tbl_name[CONTROL_NAMELEN_MAX - 1] = '\0';