Skip to contentSkip to conversation

use mkstemp instead of mktemp

PATCH In review Elia Pinto 3 participants 4 messages Apr 26, 2012 – May 11, 2012 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Elia Pinto

author
#0 lore raw

PATCH 1/1 use mkstemp instead of mktemp

As described in the manual page mktemp(3) the use of this feature is strongly discouraged in favor of mkstemp(3).

In fact the mkstemp() function generates a unique temporary file name from the supplied template,
opens a file of that name using the O_EXCL flag (guaranteeing the current process to be the only user) and returns a file descriptor.

But the POSIX specification does not say anything about file modes,
so the application should make sure its umask
is set appropriately before calling mkstemp.
( ref. https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/781-BSI.html)

---
 mount.cifs.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
mount.cifs.c +13 -5
index c90ce3e..bdd9d28 100644
@@ -1627,6 +1627,9 @@ del_mtab(char *mountpoint)
 	FILE *mnttmp, *mntmtab;
 	struct mntent *mountent;
 	char *mtabfile, *mtabdir, *mtabtmpfile;
+	mode_t mode;
+	FILE *spf;
+	int fd = -1 ;
 
 	mtabfile = strdup(MOUNTED);
 	mtabdir = dirname(mtabfile);
@@ -1652,12 +1655,17 @@ del_mtab(char *mountpoint)
 		goto del_mtab_exit;
 	}
 
-	mtabtmpfile = mktemp(mtabtmpfile);
-	if (!mtabtmpfile) {
-		fprintf(stderr, "del_mtab: cannot setup tmp file destination");
-		rc = EX_FILEIO;
-		goto del_mtab_exit;
+	mode = umask(0077);
+	(void) umask(mode);
+	if ((fd = mkstemp(mtabtmpfile)) == -1 ||
+		(spf = fdopen(fd, "w+")) == NULL) {
+		if (fd != -1) {
+			fprintf(stderr, "del_mtab: cannot setup tmp file destination");
+			rc = EX_FILEIO;
+			goto del_mtab_exit;
+                }
 	}
+	(void) fclose(spf);
 
 	mntmtab = setmntent(MOUNTED, "r");
 	if (!mntmtab) {
Signature · 1 line
1.7.1

A message that is not in the archive (sent off-list or not yet received): 1335454598-27812-1-git-send-email-gitter.spiros-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw
Quoted 33 lines · On Thu, 26 Apr 2012 11:36:38 -0400 Elia Pinto…

As described in the manual page mktemp(3) the use of this feature is strongly discouraged in favor of mkstemp(3).

In fact the mkstemp() function generates a unique temporary file name from the supplied template,
opens a file of that name using the O_EXCL flag (guaranteeing the current process to be the only user) and returns a file descriptor.

But the POSIX specification does not say anything about file modes,
so the application should make sure its umask
is set appropriately before calling mkstemp.
( ref. https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/781-BSI.html)

---
 mount.cifs.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)
mount.cifs.c +3 -0
index c90ce3e..bdd9d28 100644
@@ -1627,6 +1627,9 @@ del_mtab(char *mountpoint)
 	FILE *mnttmp, *mntmtab;
 	struct mntent *mountent;
 	char *mtabfile, *mtabdir, *mtabtmpfile;
+	mode_t mode;
+	FILE *spf;
+	int fd = -1 ;
 
 	mtabfile = strdup(MOUNTED);
 	mtabdir = dirname(mtabfile);

mount.cifs.c del_mtab(char *mountpoint) · line 1658

Quoted 9 lines
mount.cifs.c +1 -5
@@ -1652,12 +1655,17 @@ del_mtab(char *mountpoint)
 		goto del_mtab_exit;
 	}
 
-	mtabtmpfile = mktemp(mtabtmpfile);
-	if (!mtabtmpfile) {
-		fprintf(stderr, "del_mtab: cannot setup tmp file destination");
-		rc = EX_FILEIO;
-		goto del_mtab_exit;
+	mode = umask(0077);

(cc'ing Carlos since he did most of this work originally)

I think we want the final mtab to be group and world-readable, right? So maybe umask(0033) would be better? We should probably also reset the umask afterward too just to be safe...

mount.cifs.c del_mtab(char *mountpoint) · line 1669

Quoted 11 lines
mount.cifs.c +9 -0
+	(void) umask(mode);
+	if ((fd = mkstemp(mtabtmpfile)) == -1 ||
+		(spf = fdopen(fd, "w+")) == NULL) {
+		if (fd != -1) {
+			fprintf(stderr, "del_mtab: cannot setup tmp file destination");
+			rc = EX_FILEIO;
+			goto del_mtab_exit;
+                }
 	}
+	(void) fclose(spf);
 

Looks reasonable to change to mkstemp, but what's the point of doing the the fdopen there if you're just going to immediately close it afterward? Why not just close(fd) there?

...or, is it legit to call fdopen on fd and and then just pretend that's equivalent to a setmntent and get rid of the later setmntent to open the file?

Quoted 2 lines
mount.cifs.c +0 -0
 	mntmtab = setmntent(MOUNTED, "r");
 	if (!mntmtab) {
Signature · 1 line
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): 20120426142215.59d29cb6-4QP7MXygkU+dMjc06nkz3ljfA9RmPOcC@public.gmane.org

#4 lore raw

Hi,

Quoted 26 lines
Quoted 10 lines
mount.cifs.c +3 -0
 	FILE *mnttmp, *mntmtab;
 	struct mntent *mountent;
 	char *mtabfile, *mtabdir, *mtabtmpfile;
+	mode_t mode;
+	FILE *spf;
+	int fd = -1 ;
 
 	mtabfile = strdup(MOUNTED);
 	mtabdir = dirname(mtabfile);

mount.cifs.c del_mtab(char *mountpoint) · line 1658

Quoted 9 lines
mount.cifs.c +1 -5
@@ -1652,12 +1655,17 @@ del_mtab(char *mountpoint)
 		goto del_mtab_exit;
 	}
 
-	mtabtmpfile = mktemp(mtabtmpfile);
-	if (!mtabtmpfile) {
-		fprintf(stderr, "del_mtab: cannot setup tmp file destination");
-		rc = EX_FILEIO;
-		goto del_mtab_exit;
+	mode = umask(0077);

(cc'ing Carlos since he did most of this work originally)

I think we want the final mtab to be group and world-readable, right? So maybe umask(0033) would be better? We should probably also reset the umask afterward too just to be safe...

Quoted 16 lines

mount.cifs.c del_mtab(char *mountpoint) · line 1669

Quoted 11 lines
mount.cifs.c +9 -0
+	(void) umask(mode);
+	if ((fd = mkstemp(mtabtmpfile)) == -1 ||
+		(spf = fdopen(fd, "w+")) == NULL) {
+		if (fd != -1) {
+			fprintf(stderr, "del_mtab: cannot setup tmp file destination");
+			rc = EX_FILEIO;
+			goto del_mtab_exit;
+                }
 	}
+	(void) fclose(spf);
 

Looks reasonable to change to mkstemp, but what's the point of doing the the fdopen there if you're just going to immediately close it afterward? Why not just close(fd) there?

The mainly reason I chose mktemp() instead of mkstemp was due its return value. The mktemp() returns a template, while mkstemp returns a file descriptor, which makes the need to handle the file descriptor.
Due our usage of mktemp() here, I didn't think we were subjected to both risks it implies (the limit of at most 26 different names and the race), and also, mktemp() made it easier to implement. But I have no objection to change it to mkstemp if anyone believes it's a risk.

Sorry for my delay though, I was enjoying some vacations :)

Signature · 1 line
--Carlos

A message that is not in the archive (sent off-list or not yet received): 20120509125611.GA16402-PWLG29+z7hGDShSEFzyI5WNt5VayBDZ4VpNB7YpNyf8@public.gmane.org

#6 lore raw
Quoted 53 lines · On Wed, 9 May 2012 09:56:11 -0300 Carlos Maiolino…

Hi,

Quoted 26 lines
Quoted 10 lines
mount.cifs.c +3 -0
 	FILE *mnttmp, *mntmtab;
 	struct mntent *mountent;
 	char *mtabfile, *mtabdir, *mtabtmpfile;
+	mode_t mode;
+	FILE *spf;
+	int fd = -1 ;
 
 	mtabfile = strdup(MOUNTED);
 	mtabdir = dirname(mtabfile);

mount.cifs.c del_mtab(char *mountpoint) · line 1658

Quoted 9 lines
mount.cifs.c +1 -5
@@ -1652,12 +1655,17 @@ del_mtab(char *mountpoint)
 		goto del_mtab_exit;
 	}
 
-	mtabtmpfile = mktemp(mtabtmpfile);
-	if (!mtabtmpfile) {
-		fprintf(stderr, "del_mtab: cannot setup tmp file destination");
-		rc = EX_FILEIO;
-		goto del_mtab_exit;
+	mode = umask(0077);

(cc'ing Carlos since he did most of this work originally)

I think we want the final mtab to be group and world-readable, right? So maybe umask(0033) would be better? We should probably also reset the umask afterward too just to be safe...

Quoted 16 lines

mount.cifs.c del_mtab(char *mountpoint) · line 1669

Quoted 11 lines
mount.cifs.c +9 -0
+	(void) umask(mode);
+	if ((fd = mkstemp(mtabtmpfile)) == -1 ||
+		(spf = fdopen(fd, "w+")) == NULL) {
+		if (fd != -1) {
+			fprintf(stderr, "del_mtab: cannot setup tmp file destination");
+			rc = EX_FILEIO;
+			goto del_mtab_exit;
+                }
 	}
+	(void) fclose(spf);
 

Looks reasonable to change to mkstemp, but what's the point of doing the the fdopen there if you're just going to immediately close it afterward? Why not just close(fd) there?

The mainly reason I chose mktemp() instead of mkstemp was due its return value. The mktemp() returns a template, while mkstemp returns a file descriptor, which makes the need to handle the file descriptor.
Due our usage of mktemp() here, I didn't think we were subjected to both risks it implies (the limit of at most 26 different names and the race), and also, mktemp() made it easier to implement. But I have no objection to change it to mkstemp if anyone believes it's a risk.

Quoted 2 lines

Sorry for my delay though, I was enjoying some vacations :)

Well, there is a minor risk here. We don't open the file with O_EXCL and we should. It's possible someone could guess the filename and be able to create it during the race window. Presumably they'd already need to be root to do that in which case they could already clobber /etc/mtab, but it doesn't hurt to be safe there I guess...

Signature · 1 line
Jeff Layton <[email protected]>