diff -Naurp util-linux-ng-2.17.1/mount/fstab.c util-linux-ng-2.17.1.oden/mount/fstab.c --- util-linux-ng-2.17.1/mount/fstab.c 2010-02-16 08:58:57.000000000 +0000 +++ util-linux-ng-2.17.1.oden/mount/fstab.c 2012-05-29 08:30:23.000000000 +0000 @@ -773,7 +773,7 @@ update_mtab (const char *dir, struct my_ mntFILE *mfp, *mftmp; const char *fnam = _PATH_MOUNTED; struct mntentchn mtabhead; /* dummy */ - struct mntentchn *mc, *mc0, *absent = NULL; + struct mntentchn *mc, *mc0 = NULL, *absent = NULL; struct stat sbuf; int fd; @@ -797,10 +797,12 @@ update_mtab (const char *dir, struct my_ read_mntentchn(mfp, fnam, mc); /* find last occurrence of dir */ - for (mc = mc0->prev; mc && mc != mc0; mc = mc->prev) - if (streq(mc->m.mnt_dir, dir)) - break; - if (mc && mc != mc0) { + if (dir) { + for (mc = mc0->prev; mc && mc != mc0; mc = mc->prev) + if (streq(mc->m.mnt_dir, dir)) + break; + } + if (dir && mc && mc != mc0) { if (instead == NULL) { /* An umount - remove entry */ if (mc && mc != mc0) { @@ -846,19 +848,32 @@ update_mtab (const char *dir, struct my_ int errsv = errno; error (_("cannot open %s (%s) - mtab not updated"), _PATH_MOUNTED_TMP, strerror (errsv)); - discard_mntentchn(mc0); goto leave; } for (mc = mc0->nxt; mc && mc != mc0; mc = mc->nxt) { if (my_addmntent(mftmp, &(mc->m)) == 1) { int errsv = errno; - die (EX_FILEIO, _("error writing %s: %s"), + error(_("error writing %s: %s"), _PATH_MOUNTED_TMP, strerror (errsv)); + goto leave; } } discard_mntentchn(mc0); + mc0 = NULL; + + /* + * We have to be paranoid with write() to avoid incomplete + * /etc/mtab. Users are able to control writing by RLIMIT_FSIZE. + */ + if (fflush(mftmp->mntent_fp) != 0) { + int errsv = errno; + error (_("%s: cannot fflush changes: %s"), + _PATH_MOUNTED_TMP, strerror (errsv)); + goto leave; + } + fd = fileno(mftmp->mntent_fp); /* @@ -900,6 +915,9 @@ update_mtab (const char *dir, struct my_ } leave: + if (mc0) + discard_mntentchn(mc0); + unlink(_PATH_MOUNTED_TMP); unlock_mtab(); } diff -Naurp util-linux-ng-2.17.1/mount/mount.c util-linux-ng-2.17.1.oden/mount/mount.c --- util-linux-ng-2.17.1/mount/mount.c 2012-05-29 08:28:47.000000000 +0000 +++ util-linux-ng-2.17.1.oden/mount/mount.c 2012-05-29 08:30:23.000000000 +0000 @@ -1275,6 +1275,7 @@ update_mtab_entry(const char *spec, cons printf(_("mount: no %s found - creating it..\n"), _PATH_MOUNTED); create_mtab (); + } if (!nomtab && mtab_is_writable()) { @@ -1282,25 +1283,8 @@ update_mtab_entry(const char *spec, cons update_mtab (mnt.mnt_dir, &mnt); else if (flags & MS_MOVE) update_mtab(mnt.mnt_fsname, &mnt); - else { - mntFILE *mfp; - - lock_mtab(); - mfp = my_setmntent(_PATH_MOUNTED, "a+"); - if (mfp == NULL || mfp->mntent_fp == NULL) { - int errsv = errno; - error(_("mount: can't open %s: %s"), _PATH_MOUNTED, - strerror (errsv)); - } else { - if ((my_addmntent (mfp, &mnt)) == 1) { - int errsv = errno; - error(_("mount: error writing %s: %s"), - _PATH_MOUNTED, strerror (errsv)); - } - } - my_endmntent(mfp); - unlock_mtab(); - } + else + update_mtab(NULL, &mnt); } my_free(mnt.mnt_fsname); my_free(mnt.mnt_dir);