diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2001-06-18 21:39:27 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2001-06-18 21:39:27 +0000 |
commit | 1c4e6cd26420fee6e835171ffffb3b8b1e6dc3cd (patch) | |
tree | bd74a103f58a556a1b4c2091fd4d16b566267cc7 /usr.bin | |
parent | 36a1298f5bf20a09cb892234b987be86a778120b (diff) |
When creating temp files, use fchmod() to set the perms to be what we
expect since the mode mkstemp() uses can be modified by the umask.
This fixes a problem where vi would spin trying to create temp
files, eating up inodes; reported by xyntrix@bitz.org
This fix has the side effect of letting you create files with
silly modes (like 0000), but that is probably OK.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/vi/common/exf.c | 16 | ||||
-rw-r--r-- | usr.bin/vi/common/recover.c | 17 |
2 files changed, 23 insertions, 10 deletions
diff --git a/usr.bin/vi/common/exf.c b/usr.bin/vi/common/exf.c index 333c495d27f..d2b361d9f8b 100644 --- a/usr.bin/vi/common/exf.c +++ b/usr.bin/vi/common/exf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: exf.c,v 1.12 2001/01/29 01:58:29 niklas Exp $ */ +/* $OpenBSD: exf.c,v 1.13 2001/06/18 21:39:25 millert Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -189,9 +189,14 @@ file_init(sp, frp, rcv_name, flags) goto err; (void)snprintf(tname, sizeof(tname), "%s/vi.XXXXXX", O_STR(sp, O_DIRECTORY)); - if ((fd = mkstemp(tname)) == -1) { + fd = mkstemp(tname); + if (fd == -1 || fchmod(fd, S_IRUSR | S_IWUSR) == -1) { msgq(sp, M_SYSERR, "237|Unable to create temporary file"); + if (fd != -1) { + close(fd); + (void)unlink(tname); + } goto err; } (void)close(fd); @@ -1129,7 +1134,12 @@ file_backup(sp, name, bname) flags = O_TRUNC; } else flags = O_CREAT | O_EXCL; - if ((wfd = open(wfname, flags | O_WRONLY, S_IRUSR | S_IWUSR)) < 0) { + if ((wfd = open(wfname, flags | O_WRONLY, S_IRUSR | S_IWUSR)) < 0 || + fchmod(wfd, S_IRUSR | S_IWUSR) < 0) { + if (wfd != -1) { + close(wfd); + (void)unlink(wfname); + } estr = bname; goto err; } diff --git a/usr.bin/vi/common/recover.c b/usr.bin/vi/common/recover.c index a61685dd2b2..2b47562da48 100644 --- a/usr.bin/vi/common/recover.c +++ b/usr.bin/vi/common/recover.c @@ -1,4 +1,4 @@ -/* $OpenBSD: recover.c,v 1.6 2001/05/28 22:41:35 pvalchev Exp $ */ +/* $OpenBSD: recover.c,v 1.7 2001/06/18 21:39:26 millert Exp $ */ /*- * Copyright (c) 1993, 1994 @@ -837,16 +837,19 @@ rcv_mktemp(sp, path, dname, perms) * !!! * We expect mkstemp(3) to set the permissions correctly. On * historic System V systems, mkstemp didn't. Do it here, on - * GP's. + * GP's. This also protects us from users with stupid umasks. * * XXX - * The variable perms should really be a mode_t, and it would - * be nice to use fchmod(2) instead of chmod(2), here. + * The variable perms should really be a mode_t. */ - if ((fd = mkstemp(path)) == -1) + if ((fd = mkstemp(path)) == -1 || fchmod(fd, perms) == -1) { msgq_str(sp, M_SYSERR, dname, "%s"); - else - (void)chmod(path, perms); + if (fd != -1) { + close(fd); + unlink(path); + fd = -1; + } + } return (fd); } |