Skip to contentSkip to conversation

semodule: avoid toctou on output module

PATCH In review Christian Göttsche 3 participants 3 messages May 20, 2022 – Jun 2, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 semodule: avoid toctou on output module

Do not check for file existence and open afterwards, open with the exclusive flag (supported in Glibc and musl 0.9.6 and also standardized in C11).

Found by GitHub CodeQL.

---
 policycoreutils/semodule/semodule.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
policycoreutils/semodule/semodule.c +5 -8
index 1ed8e690..48bc28dd 100644
@@ -550,15 +550,12 @@ int main(int argc, char *argv[])
 					goto cleanup_extract;
 				}
 
-				if (access(output_path, F_OK) == 0) {
-					fprintf(stderr, "%s: %s is already extracted with extension %s.\n", argv[0], mode_arg, lang_ext);
-					result = -1;
-					goto cleanup_extract;
-				}
-
-				output_fd = fopen(output_path, "w");
+				output_fd = fopen(output_path, "wx");
 				if (output_fd == NULL) {
-					fprintf(stderr, "%s: Unable to open %s\n", argv[0], output_path);
+					if (errno == EEXIST)
+						fprintf(stderr, "%s: %s is already extracted with extension %s.\n", argv[0], mode_arg, lang_ext);
+					else
+						fprintf(stderr, "%s: Unable to open %s:  %s\n", argv[0], output_path, strerror(errno));
 					result = -1;
 					goto cleanup_extract;
 				}
Signature · 1 line
2.36.1
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Christian Göttsche

Do not check for file existence and open afterwards, open with the exclusive flag (supported in Glibc and musl 0.9.6 and also standardized in C11).

Found by GitHub CodeQL.

This looks good to me.

Thanks!

Quoted 32 lines
---
 policycoreutils/semodule/semodule.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
policycoreutils/semodule/semodule.c +5 -9
index 1ed8e690..48bc28dd 100644
@@ -550,15 +550,12 @@ int main(int argc, char *argv[])
                                        goto cleanup_extract;
                                }
 
-                               if (access(output_path, F_OK) == 0) {
-                                       fprintf(stderr, "%s: %s is already extracted with extension %s.\n", argv[0], mode_arg, lang_ext);
-                                       result = -1;
-                                       goto cleanup_extract;
-                               }
-
-                               output_fd = fopen(output_path, "w");
+                               output_fd = fopen(output_path, "wx");
                                if (output_fd == NULL) {
-                                       fprintf(stderr, "%s: Unable to open %s\n", argv[0], output_path);
+                                       if (errno == EEXIST)
+                                               fprintf(stderr, "%s: %s is already extracted with extension %s.\n", argv[0], mode_arg, lang_ext);
+                                       else
+                                               fprintf(stderr, "%s: Unable to open %s:  %s\n", argv[0], output_path, strerror(errno));
                                        result = -1;
                                        goto cleanup_extract;
                                }
--

2.36.1

#2 lore raw
Quoted 8 lines · Nicolas Iooss
Quoted 5 lines · Christian Göttsche

Do not check for file existence and open afterwards, open with the exclusive flag (supported in Glibc and musl 0.9.6 and also standardized in C11).

Quoted 8 lines
Quoted 3 lines

Found by GitHub CodeQL.

This looks good to me.

Merged.
Thanks,
Jim

Quoted 35 lines

Thanks!

Quoted 32 lines
---
 policycoreutils/semodule/semodule.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
policycoreutils/semodule/semodule.c +5 -9
index 1ed8e690..48bc28dd 100644
@@ -550,15 +550,12 @@ int main(int argc, char *argv[])
                                        goto cleanup_extract;
                                }
 
-                               if (access(output_path, F_OK) == 0) {
-                                       fprintf(stderr, "%s: %s is already extracted with extension %s.\n", argv[0], mode_arg, lang_ext);
-                                       result = -1;
-                                       goto cleanup_extract;
-                               }
-
-                               output_fd = fopen(output_path, "w");
+                               output_fd = fopen(output_path, "wx");
                                if (output_fd == NULL) {
-                                       fprintf(stderr, "%s: Unable to open %s\n", argv[0], output_path);
+                                       if (errno == EEXIST)
+                                               fprintf(stderr, "%s: %s is already extracted with extension %s.\n", argv[0], mode_arg, lang_ext);
+                                       else
+                                               fprintf(stderr, "%s: Unable to open %s:  %s\n", argv[0], output_path, strerror(errno));
                                        result = -1;
                                        goto cleanup_extract;
                                }
--

2.36.1