Skip to contentSkip to conversation

libsemanage: Fix USE_AFTER_FREE (CWE-672) in semanage_direct_write_langext()

PATCH In review Petr Lautrbach 2 participants 3 messages Jul 28, 2021 – Aug 3, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsemanage: Fix USE_AFTER_FREE (CWE-672) in semanage_direct_write_langext()

From fclose(3):
Upon successful completion, 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case, any further access (including another call to fclose()) to the stream results in undefined behavior.

Fixes:

    Error: USE_AFTER_FREE (CWE-672): [#def1]
    libsemanage-3.2/src/direct_api.c:1023: freed_arg: "fclose" frees "fp".
    libsemanage-3.2/src/direct_api.c:1034: use_closed_file: Calling "fclose" uses file handle "fp" after closing it.
    # 1032|
    # 1033|   cleanup:
    # 1034|-> 	if (fp != NULL) fclose(fp);
    # 1035|
    # 1036|   	return ret;
---
 libsemanage/src/direct_api.c | 1 +
 1 file changed, 1 insertion(+)
libsemanage/src/direct_api.c +1 -0
index 9a4e79385b69..f0e2300a2f58 100644
@@ -1022,6 +1022,7 @@ static int semanage_direct_write_langext(semanage_handle_t *sh,
 
 	if (fclose(fp) != 0) {
 		ERR(sh, "Unable to close %s module ext file.", modinfo->name);
+		fp = NULL;
 		ret = -1;
 		goto cleanup;
 	}
Signature · 1 line
2.32.0
Acked-by on v1 1/1
#1 lore raw
Quoted 17 lines · Petr Lautrbach

From fclose(3):
Upon successful completion, 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case, any further access (including another call to fclose()) to the stream results in undefined behavior.

Fixes:

    Error: USE_AFTER_FREE (CWE-672): [#def1]
    libsemanage-3.2/src/direct_api.c:1023: freed_arg: "fclose" frees "fp".
    libsemanage-3.2/src/direct_api.c:1034: use_closed_file: Calling "fclose" uses file handle "fp" after closing it.
    # 1032|
    # 1033|   cleanup:
    # 1034|->   if (fp != NULL) fclose(fp);
    # 1035|
    # 1036|     return ret;
Quoted 1 line
Quoted 19 lines
---
 libsemanage/src/direct_api.c | 1 +
 1 file changed, 1 insertion(+)
libsemanage/src/direct_api.c +1 -1
index 9a4e79385b69..f0e2300a2f58 100644
@@ -1022,6 +1022,7 @@ static int semanage_direct_write_langext(semanage_handle_t *sh,
 
        if (fclose(fp) != 0) {
                ERR(sh, "Unable to close %s module ext file.", modinfo->name);
+               fp = NULL;
                ret = -1;
                goto cleanup;
        }
--

2.32.0

#2 lore raw
Quoted 19 lines · James Carter
Quoted 17 lines · Petr Lautrbach

From fclose(3):
Upon successful completion, 0 is returned. Otherwise, EOF is returned and errno is set to indicate the error. In either case, any further access (including another call to fclose()) to the stream results in undefined behavior.

Fixes:

    Error: USE_AFTER_FREE (CWE-672): [#def1]
    libsemanage-3.2/src/direct_api.c:1023: freed_arg: "fclose" frees "fp".
    libsemanage-3.2/src/direct_api.c:1034: use_closed_file: Calling "fclose" uses file handle "fp" after closing it.
    # 1032|
    # 1033|   cleanup:
    # 1034|->   if (fp != NULL) fclose(fp);
    # 1035|
    # 1036|     return ret;
Quoted 4 lines
Quoted 1 line

Merged.
Thanks,
Jim

Quoted 19 lines
Quoted 19 lines
---
 libsemanage/src/direct_api.c | 1 +
 1 file changed, 1 insertion(+)
libsemanage/src/direct_api.c +1 -1
index 9a4e79385b69..f0e2300a2f58 100644
@@ -1022,6 +1022,7 @@ static int semanage_direct_write_langext(semanage_handle_t *sh,
 
        if (fclose(fp) != 0) {
                ERR(sh, "Unable to close %s module ext file.", modinfo->name);
+               fp = NULL;
                ret = -1;
                goto cleanup;
        }
--

2.32.0