Unix Technical Forum

Re: Don't let mg create backupfiles in world-writable dirs.

This is a discussion on Re: Don't let mg create backupfiles in world-writable dirs. within the mailing.openbsd.tech forums, part of the OpenBSD category; --> I don't think this is the best way to do it. I think what we really want is to ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > mailing.openbsd.tech

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-18-2008, 07:33 AM
Todd C. Miller
 
Posts: n/a
Default Re: Don't let mg create backupfiles in world-writable dirs.

I don't think this is the best way to do it. I think what we really
want is to use mkstemp() to create the temp file and then just
rename() it to the predictable name. If the rename fails, we know
someone is trying to attach us. If not, we are safe.

- todd

Index: fileio.c
================================================== =================
RCS file: /home/cvs/openbsd/src/usr.bin/mg/fileio.c,v
retrieving revision 1.43
diff -u -r1.43 fileio.c
--- fileio.c 11 Jan 2005 17:19:16 -0000 1.43
+++ fileio.c 30 Jan 2005 21:11:47 -0000
@@ -160,7 +160,7 @@
int from, to, serrno;
ssize_t nread;
char buf[BUFSIZ];
- char *nname;
+ char *nname, *tname;

if (stat(fn, &sb) == -1) {
ewprintf("Can't stat %s : %s", fn, strerror(errno));
@@ -168,20 +168,27 @@
}

if (asprintf(&nname, "%s~", fn) == -1) {
- ewprintf("Can't allocate temp file name : %s",
- strerror(errno));
+ ewprintf("Can't allocate temp file name : %s", strerror(errno));
+ return (ABORT);
+ }
+
+ if (asprintf(&tname, "%s.XXXXXXXXXX", fn) == -1) {
+ ewprintf("Can't allocate temp file name : %s", strerror(errno));
+ free(nname);
return (ABORT);
}

if ((from = open(fn, O_RDONLY)) == -1) {
free(nname);
+ free(tname);
return (FALSE);
}
- to = open(nname, O_WRONLY|O_CREAT|O_TRUNC, (sb.st_mode & 0777));
+ to = mkstemp(tname);
if (to == -1) {
serrno = errno;
close(from);
free(nname);
+ free(tname);
errno = serrno;
return (FALSE);
}
@@ -192,13 +199,20 @@
}
}
serrno = errno;
+ (void) fchmod(to, (sb.st_mode & 0777));
close(from);
close(to);
if (nread == -1) {
- if (unlink(nname) == -1)
+ if (unlink(tname) == -1)
ewprintf("Can't unlink temp : %s", strerror(errno));
+ } else {
+ if (rename(tname, nname) == -1) {
+ ewprintf("Can't rename temp : %s", strerror(errno));
+ (void) unlink(tname);
+ }
}
free(nname);
+ free(tname);
errno = serrno;

return (nread == -1 ? FALSE : TRUE);

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:59 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com