Skip to contentSkip to conversation

sandbox/seunshare: switch seunshare_mount_file() to use open()

PATCH Applied Stephen Smalley 2 participants 4 messages May 12 – May 15 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 sandbox/seunshare: switch seunshare_mount_file() to use open()

seunshare_mount_file() currently uses fopen() to create the dst if it doesn't already exist. Switch to using open() with explicitly specified flags including O_NOFOLLOW and an explicitly specified mode for the new file.

---
 sandbox/seunshare.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
sandbox/seunshare.c +8 -6
index a1900eaa..17a727e7 100644
@@ -304,18 +304,20 @@ static int seunshare_mount(const char *src, const char *dst, struct stat *src_st
  */
 static int seunshare_mount_file(const char *src, const char *dst)
 {
-	int flags = 0;
-
 	if (verbose)
 		printf(_("Mounting %s on %s\n"), src, dst);
 
 	if (access(dst, F_OK) == -1) {
-		 FILE *fptr;
-         fptr = fopen(dst, "w");
-		 fclose(fptr);
+		int fd = open(dst, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600);
+		if (fd < 0) {
+			fprintf(stderr, _("Failed to create mount point %s: %m\n"), dst);
+			return -1;
+		}
+		close(fd);
 	}
+
 	/* mount file */
-	if (mount(src, dst, NULL, MS_BIND | flags, NULL) < 0) {
+	if (mount(src, dst, NULL, MS_BIND, NULL) < 0) {
 		fprintf(stderr, _("Failed to mount %s on %s: %s\n"), src, dst, strerror(errno));
 		return -1;
 	}
Signature · 1 line
2.54.0
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Stephen Smalley

seunshare_mount_file() currently uses fopen() to create the dst if it doesn't already exist. Switch to using open() with explicitly specified flags including O_NOFOLLOW and an explicitly specified mode for the new file.

Quoted 37 lines
---
 sandbox/seunshare.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
sandbox/seunshare.c +8 -6
index a1900eaa..17a727e7 100644
@@ -304,18 +304,20 @@ static int seunshare_mount(const char *src, const char *dst, struct stat *src_st
  */
 static int seunshare_mount_file(const char *src, const char *dst)
 {
-	int flags = 0;
-
 	if (verbose)
 		printf(_("Mounting %s on %s\n"), src, dst);
 
 	if (access(dst, F_OK) == -1) {
-		 FILE *fptr;
-         fptr = fopen(dst, "w");
-		 fclose(fptr);
+		int fd = open(dst, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600);
+		if (fd < 0) {
+			fprintf(stderr, _("Failed to create mount point %s: %m\n"), dst);
+			return -1;
+		}
+		close(fd);
 	}
+
 	/* mount file */
-	if (mount(src, dst, NULL, MS_BIND | flags, NULL) < 0) {
+	if (mount(src, dst, NULL, MS_BIND, NULL) < 0) {
 		fprintf(stderr, _("Failed to mount %s on %s: %s\n"), src, dst, strerror(errno));
 		return -1;
 	}
Signature · 1 line
2.54.0
author maintainer
#2 lore raw
Quoted 11 lines · Petr Lautrbach
Quoted 6 lines · Stephen Smalley

seunshare_mount_file() currently uses fopen() to create the dst if it doesn't already exist. Switch to using open() with explicitly specified flags including O_NOFOLLOW and an explicitly specified mode for the new file.

Thanks for the Acks; let me know if you plan to merge or if I should. Also, I was wondering what if any validation we want to do for seunshare_mount_file src/dst files before calling mount() on them; there aren't currently verify_directory/check_owner_uid() calls for them unlike for the directories passed to seunshare_mount().

Quoted 39 lines
Quoted 38 lines
---
 sandbox/seunshare.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
sandbox/seunshare.c +8 -7
index a1900eaa..17a727e7 100644
@@ -304,18 +304,20 @@ static int seunshare_mount(const char *src, const char *dst, struct stat *src_st
  */
 static int seunshare_mount_file(const char *src, const char *dst)
 {
-     int flags = 0;
-
      if (verbose)
              printf(_("Mounting %s on %s\n"), src, dst);
 
      if (access(dst, F_OK) == -1) {
-              FILE *fptr;
-         fptr = fopen(dst, "w");
-              fclose(fptr);
+             int fd = open(dst, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600);
+             if (fd < 0) {
+                     fprintf(stderr, _("Failed to create mount point %s: %m\n"), dst);
+                     return -1;
+             }
+             close(fd);
      }
+
      /* mount file */
-     if (mount(src, dst, NULL, MS_BIND | flags, NULL) < 0) {
+     if (mount(src, dst, NULL, MS_BIND, NULL) < 0) {
              fprintf(stderr, _("Failed to mount %s on %s: %s\n"), src, dst, strerror(errno));
              return -1;
      }
--

2.54.0

#3 lore raw
In reply to Petr Lautrbach
Quoted 10 lines · Petr Lautrbach
Quoted 6 lines · Stephen Smalley

seunshare_mount_file() currently uses fopen() to create the dst if it doesn't already exist. Switch to using open() with explicitly specified flags including O_NOFOLLOW and an explicitly specified mode for the new file.

Merged, thanks!

Quoted 37 lines
Quoted 37 lines
---
 sandbox/seunshare.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
sandbox/seunshare.c +8 -6
index a1900eaa..17a727e7 100644
@@ -304,18 +304,20 @@ static int seunshare_mount(const char *src, const char *dst, struct stat *src_st
  */
 static int seunshare_mount_file(const char *src, const char *dst)
 {
-	int flags = 0;
-
 	if (verbose)
 		printf(_("Mounting %s on %s\n"), src, dst);
 
 	if (access(dst, F_OK) == -1) {
-		 FILE *fptr;
-         fptr = fopen(dst, "w");
-		 fclose(fptr);
+		int fd = open(dst, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600);
+		if (fd < 0) {
+			fprintf(stderr, _("Failed to create mount point %s: %m\n"), dst);
+			return -1;
+		}
+		close(fd);
 	}
+
 	/* mount file */
-	if (mount(src, dst, NULL, MS_BIND | flags, NULL) < 0) {
+	if (mount(src, dst, NULL, MS_BIND, NULL) < 0) {
 		fprintf(stderr, _("Failed to mount %s on %s: %s\n"), src, dst, strerror(errno));
 		return -1;
 	}
Signature · 1 line
2.54.0