sandbox/seunshare: switch seunshare_mount_file() to use open()
Stephen Smalley
maintainerPATCH 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
@@ -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
Quoted 6 lines · Stephen Smalley
seunshare_mount_file()currently usesfopen()to create the dst if it doesn't already exist. Switch to usingopen()with explicitly specified flags includingO_NOFOLLOWand an explicitly specified mode for the new file.Signed-off-by Stephen Smalley <[email protected]>
Quoted 37 lines
--- sandbox/seunshare.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)sandbox/seunshare.c +8
-6@@ -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
Stephen Smalley
maintainerQuoted 11 lines · Petr Lautrbach
Quoted 6 lines · Stephen Smalley
seunshare_mount_file()currently usesfopen()to create the dst if it doesn't already exist. Switch to usingopen()with explicitly specified flags includingO_NOFOLLOWand an explicitly specified mode for the new file.Signed-off-by Stephen Smalley <[email protected]>Acked-by Petr Lautrbach <[email protected]>
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@@ -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
Petr Lautrbach
AppliedQuoted 10 lines · Petr Lautrbach
Quoted 6 lines · Stephen Smalley
seunshare_mount_file()currently usesfopen()to create the dst if it doesn't already exist. Switch to usingopen()with explicitly specified flags includingO_NOFOLLOWand an explicitly specified mode for the new file.Signed-off-by Stephen Smalley <[email protected]>Acked-by Petr Lautrbach <[email protected]>
Merged, thanks!
Quoted 37 lines
Quoted 37 lines
--- sandbox/seunshare.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)sandbox/seunshare.c +8
-6@@ -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