Skip to contentSkip to conversation

secilc: Use fstat instead of stat to avoid TOCTOU issues

PATCH In review James Carter 2 participants 3 messages Jun 16 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 secilc: Use fstat instead of stat to avoid TOCTOU issues

For secilc, secilcheck, secil2tree, and secil2conf use fstat instead of stat to avoid TOCTOU issues.

---
 secilc/secil2conf.c | 2 +-
 secilc/secil2tree.c | 2 +-
 secilc/secilc.c     | 2 +-
 secilc/secilcheck.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
secilc/secil2conf.c +1 -1
index 4046e603..9c028656 100644
@@ -147,7 +147,7 @@ int main(int argc, char *argv[])
 			rc = SEPOL_ERR;
 			goto exit;
 		}
-		rc = stat(argv[i], &filedata);
+		rc = fstat(fileno(file), &filedata);
 		if (rc == -1) {
 			fprintf(stderr, "Could not stat file: %s\n", argv[i]);
 			goto exit;
secilc/secil2tree.c +1 -1
index f0f02eb3..039d1524 100644
@@ -155,7 +155,7 @@ int main(int argc, char *argv[])
 			rc = SEPOL_ERR;
 			goto exit;
 		}
-		rc = stat(argv[i], &filedata);
+		rc = fstat(fileno(file), &filedata);
 		if (rc == -1) {
 			fprintf(stderr, "Could not stat file: %s\n", argv[i]);
 			goto exit;
secilc/secilc.c +1 -1
index 70359b75..016ffef3 100644
@@ -282,7 +282,7 @@ int main(int argc, char *argv[])
 			rc = SEPOL_ERR;
 			goto exit;
 		}
-		rc = stat(argv[i], &filedata);
+		rc = fstat(fileno(file), &filedata);
 		if (rc == -1) {
 			fprintf(stderr, "Could not stat file: %s\n", argv[i]);
 			rc = SEPOL_ERR;
secilc/secilcheck.c +1 -1
index 45176df4..ee141bc0 100644
@@ -148,7 +148,7 @@ static int add_cil_file(const char *filename, struct cil_db *cdb)
 		fprintf(stderr, "Could not open file: %s\n", filename);
 		goto exit;
 	}
-	if (stat(filename, &filedata) < 0) {
+	if (fstat(fileno(file), &filedata) < 0) {
 		fprintf(stderr, "Could not stat file: %s\n", filename);
 		goto exit;
 	}
Signature · 1 line
2.54.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · James Carter

For secilc, secilcheck, secil2tree, and secil2conf use fstat instead of stat to avoid TOCTOU issues.

Quoted 62 lines
---
 secilc/secil2conf.c | 2 +-
 secilc/secil2tree.c | 2 +-
 secilc/secilc.c     | 2 +-
 secilc/secilcheck.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
secilc/secil2conf.c +1 -1
index 4046e603..9c028656 100644
@@ -147,7 +147,7 @@ int main(int argc, char *argv[])
                        rc = SEPOL_ERR;
                        goto exit;
                }
-               rc = stat(argv[i], &filedata);
+               rc = fstat(fileno(file), &filedata);
                if (rc == -1) {
                        fprintf(stderr, "Could not stat file: %s\n", argv[i]);
                        goto exit;
secilc/secil2tree.c +1 -1
index f0f02eb3..039d1524 100644
@@ -155,7 +155,7 @@ int main(int argc, char *argv[])
                        rc = SEPOL_ERR;
                        goto exit;
                }
-               rc = stat(argv[i], &filedata);
+               rc = fstat(fileno(file), &filedata);
                if (rc == -1) {
                        fprintf(stderr, "Could not stat file: %s\n", argv[i]);
                        goto exit;
secilc/secilc.c +1 -1
index 70359b75..016ffef3 100644
@@ -282,7 +282,7 @@ int main(int argc, char *argv[])
                        rc = SEPOL_ERR;
                        goto exit;
                }
-               rc = stat(argv[i], &filedata);
+               rc = fstat(fileno(file), &filedata);
                if (rc == -1) {
                        fprintf(stderr, "Could not stat file: %s\n", argv[i]);
                        rc = SEPOL_ERR;
secilc/secilcheck.c +1 -2
index 45176df4..ee141bc0 100644
@@ -148,7 +148,7 @@ static int add_cil_file(const char *filename, struct cil_db *cdb)
                fprintf(stderr, "Could not open file: %s\n", filename);
                goto exit;
        }
-       if (stat(filename, &filedata) < 0) {
+       if (fstat(fileno(file), &filedata) < 0) {
                fprintf(stderr, "Could not stat file: %s\n", filename);
                goto exit;
        }
--

2.54.0