Skip to contentSkip to conversation

libsepol/sepol_compute_sid: Do not destroy uninitialized context

PATCH Applied Vit Mojzis 3 participants 4 messages Jul 19, 2024 – Jul 30, 2024 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

PATCH 1/1 libsepol/sepol_compute_sid: Do not destroy uninitialized context

Avoid context_destroy() on "newcontext" before context_init() is called.

Fixes:
libsepol-3.6/src/services.c:1335: var_decl: Declaring variable "newcontext" without initializer. libsepol-3.6/src/services.c:1462: uninit_use_in_call: Using uninitialized value "newcontext.range.level[0].cat.node" when calling "context_destroy".

  \# 1460|   	rc = sepol_sidtab_context_to_sid(sidtab, &newcontext, out_sid);
  \# 1461|         out:
  \# 1462|-> 	context_destroy(&newcontext);
  \# 1463|   	return rc;
  \# 1464|   }
---
 libsepol/src/services.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
libsepol/src/services.c +2 -4
index 36e2368f..f3231f17 100644
@@ -1362,14 +1362,12 @@ static int sepol_compute_sid(sepol_security_id_t ssid,
 	scontext = sepol_sidtab_search(sidtab, ssid);
 	if (!scontext) {
 		ERR(NULL, "unrecognized SID %d", ssid);
-		rc = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 	tcontext = sepol_sidtab_search(sidtab, tsid);
 	if (!tcontext) {
 		ERR(NULL, "unrecognized SID %d", tsid);
-		rc = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 
 	if (tclass && tclass <= policydb->p_classes.nprim)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Vit Mojzis

Avoid context_destroy() on "newcontext" before context_init() is called.

Fixes:
libsepol-3.6/src/services.c:1335: var_decl: Declaring variable "newcontext" without initializer. libsepol-3.6/src/services.c:1462: uninit_use_in_call: Using uninitialized value "newcontext.range.level[0].cat.node" when calling "context_destroy".

Quoted 7 lines
  \# 1460|      rc = sepol_sidtab_context_to_sid(sidtab, &newcontext, out_sid);
  \# 1461|         out:
  \# 1462|->    context_destroy(&newcontext);
  \# 1463|      return rc;
  \# 1464|   }

LGTM.

Quoted 29 lines
---
 libsepol/src/services.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
libsepol/src/services.c +2 -5
index 36e2368f..f3231f17 100644
@@ -1362,14 +1362,12 @@ static int sepol_compute_sid(sepol_security_id_t ssid,
        scontext = sepol_sidtab_search(sidtab, ssid);
        if (!scontext) {
                ERR(NULL, "unrecognized SID %d", ssid);
-               rc = -EINVAL;
-               goto out;
+               return -EINVAL;
        }
        tcontext = sepol_sidtab_search(sidtab, tsid);
        if (!tcontext) {
                ERR(NULL, "unrecognized SID %d", tsid);
-               rc = -EINVAL;
-               goto out;
+               return -EINVAL;
        }
 
        if (tclass && tclass <= policydb->p_classes.nprim)
--

2.43.0

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Vit Mojzis
Quoted 6 lines · Vit Mojzis

Avoid context_destroy() on "newcontext" before context_init() is called.

Fixes:
libsepol-3.6/src/services.c:1335: var_decl: Declaring variable "newcontext" without initializer. libsepol-3.6/src/services.c:1462: uninit_use_in_call: Using uninitialized value "newcontext.range.level[0].cat.node" when calling "context_destroy".

Quoted 7 lines
  \# 1460|      rc = sepol_sidtab_context_to_sid(sidtab, &newcontext, out_sid);
  \# 1461|         out:
  \# 1462|->    context_destroy(&newcontext);
  \# 1463|      return rc;
  \# 1464|   }

BTW, this function has long since diverged from the corresponding kernel function security_compute_sid; originally they were identical and even built from the same sources but we forked them long ago to specialize the kernel code. Don't believe anything is using it except for checkpolicy (via the -d option for the transition/member/change_sid commands) but no one should be relying on it matching the kernel's behavior.

maintainer Applied
#3 lore raw
Quoted 18 lines · Stephen Smalley
Quoted 6 lines · Vit Mojzis

Avoid context_destroy() on "newcontext" before context_init() is called.

Fixes:
libsepol-3.6/src/services.c:1335: var_decl: Declaring variable "newcontext" without initializer. libsepol-3.6/src/services.c:1462: uninit_use_in_call: Using uninitialized value "newcontext.range.level[0].cat.node" when calling "context_destroy".

Quoted 7 lines
  \# 1460|      rc = sepol_sidtab_context_to_sid(sidtab, &newcontext, out_sid);
  \# 1461|         out:
  \# 1462|->    context_destroy(&newcontext);
  \# 1463|      return rc;
  \# 1464|   }
Quoted 7 lines

BTW, this function has long since diverged from the corresponding kernel function security_compute_sid; originally they were identical and even built from the same sources but we forked them long ago to specialize the kernel code. Don't believe anything is using it except for checkpolicy (via the -d option for the transition/member/change_sid commands) but no one should be relying on it matching the kernel's behavior.

Applied.