diff options
author | garga <garga@FreeBSD.org> | 2015-07-02 17:30:59 +0000 |
---|---|---|
committer | garga <garga@FreeBSD.org> | 2015-07-02 17:30:59 +0000 |
commit | 43727d164bcc91ce9ad967eb9ee136419df192d6 (patch) | |
tree | 117a7f93582c746c2d9f1203b60edb19ede05ae6 /lib/libutil/gr_util.c | |
parent | 774c959e8c30079eb72afe189b6ec55ecec6aa69 (diff) | |
download | FreeBSD-src-43727d164bcc91ce9ad967eb9ee136419df192d6.zip FreeBSD-src-43727d164bcc91ce9ad967eb9ee136419df192d6.tar.gz |
When passwd or group information is changed (by pw, vipw, chpass, ...)
temporary file is created and then a rename() call move it to official file.
This operation didn't have any check to make sure data was written to disk
and if a power cycle happens system could end up with a 0 length passwd
or group database.
There is a pfSense bug with more infor about it:
https://redmine.pfsense.org/issues/4523
The following changes were made to protect passwd and group operations:
* lib/libutil/gr_util.c:
- Replace mkstemp() by mkostemp() with O_SYNC flag to create temp file
- After rename(), fsync() call on directory for faster result
* lib/libutil/pw_util.c
- Replace mkstemp() by mkostemp() with O_SYNC flag to create temp file
* usr.sbin/pwd_mkdb/pwd_mkdb.c
- Added O_SYNC flag on dbopen() calls
- After rename(), fsync() call on directory for faster result
* lib/libutil/pw_util.3
- pw_lock() returns a file descriptor to master password file on success
Differential Revision: https://reviews.freebsd.org/D2978
Approved by: bapt
Sponsored by: Netgate
Diffstat (limited to 'lib/libutil/gr_util.c')
-rw-r--r-- | lib/libutil/gr_util.c | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/lib/libutil/gr_util.c b/lib/libutil/gr_util.c index b0b0b36f..93b3eb2 100644 --- a/lib/libutil/gr_util.c +++ b/lib/libutil/gr_util.c @@ -141,7 +141,7 @@ gr_tmp(int mfd) errno = ENAMETOOLONG; return (-1); } - if ((tfd = mkstemp(tempname)) == -1) + if ((tfd = mkostemp(tempname, O_SYNC)) == -1) return (-1); if (mfd != -1) { while ((nr = read(mfd, buf, sizeof(buf))) > 0) @@ -318,10 +318,28 @@ gr_copy(int ffd, int tfd, const struct group *gr, struct group *old_gr) int gr_mkdb(void) { + int fd; + if (chmod(tempname, 0644) != 0) return (-1); - return (rename(tempname, group_file)); + if (rename(tempname, group_file) != 0) + return (-1); + + /* + * Make sure new group file is safe on disk. To improve performance we + * will call fsync() to the directory where file lies + */ + if ((fd = open(group_dir, O_RDONLY|O_DIRECTORY)) == -1) + return (-1); + + if (fsync(fd) != 0) { + close(fd); + return (-1); + } + + close(fd); + return(0); } /* |