* [Qemu-devel] [PATCH] block: Don't forget to delete temporary file @ 2012-09-05 13:26 riegamaths 2012-09-05 15:40 ` Paolo Bonzini 2012-09-11 10:23 ` Kevin Wolf 0 siblings, 2 replies; 9+ messages in thread From: riegamaths @ 2012-09-05 13:26 UTC (permalink / raw) To: qemu-devel; +Cc: qemu-trivial, Dunrong Huang From: Dunrong Huang <riegamaths@gmail.com> The caller would not delete temporary file after failed get_tmp_filename(). Signed-off-by: Dunrong Huang <riegamaths@gmail.com> --- block.c | 6 +++++- 1 个文件被修改,插入 5 行(+),删除 1 行(-) diff --git a/block.c b/block.c index 074987e..2bc9f75 100644 --- a/block.c +++ b/block.c @@ -433,7 +433,11 @@ int get_tmp_filename(char *filename, int size) return -EOVERFLOW; } fd = mkstemp(filename); - if (fd < 0 || close(fd)) { + if (fd < 0) { + return -errno; + } + if (close(fd) != 0) { + unlink(filename); return -errno; } return 0; -- 1.7.12 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] block: Don't forget to delete temporary file 2012-09-05 13:26 [Qemu-devel] [PATCH] block: Don't forget to delete temporary file riegamaths @ 2012-09-05 15:40 ` Paolo Bonzini 2012-09-05 15:51 ` Dunrong Huang 2012-09-05 16:02 ` Markus Armbruster 2012-09-11 10:23 ` Kevin Wolf 1 sibling, 2 replies; 9+ messages in thread From: Paolo Bonzini @ 2012-09-05 15:40 UTC (permalink / raw) To: riegamaths; +Cc: qemu-trivial, qemu-devel Il 05/09/2012 15:26, riegamaths@gmail.com ha scritto: > From: Dunrong Huang <riegamaths@gmail.com> > > The caller would not delete temporary file after failed get_tmp_filename(). > > Signed-off-by: Dunrong Huang <riegamaths@gmail.com> > --- > block.c | 6 +++++- > 1 个文件被修改,插入 5 行(+),删除 1 行(-) > > diff --git a/block.c b/block.c > index 074987e..2bc9f75 100644 > --- a/block.c > +++ b/block.c > @@ -433,7 +433,11 @@ int get_tmp_filename(char *filename, int size) > return -EOVERFLOW; > } > fd = mkstemp(filename); > - if (fd < 0 || close(fd)) { > + if (fd < 0) { > + return -errno; > + } > + if (close(fd) != 0) { > + unlink(filename); > return -errno; > } > return 0; > Not necessary, mkstemp will not create a file if it returns an error. Paolo ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] block: Don't forget to delete temporary file 2012-09-05 15:40 ` Paolo Bonzini @ 2012-09-05 15:51 ` Dunrong Huang 2012-09-05 16:02 ` Markus Armbruster 1 sibling, 0 replies; 9+ messages in thread From: Dunrong Huang @ 2012-09-05 15:51 UTC (permalink / raw) To: Paolo Bonzini; +Cc: qemu-trivial, qemu-devel Hi, thanks for you reply. 2012/9/5 Paolo Bonzini <pbonzini@redhat.com>: > Il 05/09/2012 15:26, riegamaths@gmail.com ha scritto: >> From: Dunrong Huang <riegamaths@gmail.com> >> >> The caller would not delete temporary file after failed get_tmp_filename(). >> >> Signed-off-by: Dunrong Huang <riegamaths@gmail.com> >> --- >> block.c | 6 +++++- >> 1 个文件被修改,插入 5 行(+),删除 1 行(-) >> >> diff --git a/block.c b/block.c >> index 074987e..2bc9f75 100644 >> --- a/block.c >> +++ b/block.c >> @@ -433,7 +433,11 @@ int get_tmp_filename(char *filename, int size) >> return -EOVERFLOW; >> } >> fd = mkstemp(filename); >> - if (fd < 0 || close(fd)) { >> + if (fd < 0) { >> + return -errno; >> + } >> + if (close(fd) != 0) { >> + unlink(filename); >> return -errno; >> } >> return 0; >> > > Not necessary, mkstemp will not create a file if it returns an error. > If we call mkstemp() successfully, but failed to close(fd), in this case, the temporafy file will not be deleted even if QEMU exits. > Paolo -- Best Regards, Dunrong Huang ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] block: Don't forget to delete temporary file 2012-09-05 15:40 ` Paolo Bonzini 2012-09-05 15:51 ` Dunrong Huang @ 2012-09-05 16:02 ` Markus Armbruster 2012-09-05 16:23 ` Paolo Bonzini 1 sibling, 1 reply; 9+ messages in thread From: Markus Armbruster @ 2012-09-05 16:02 UTC (permalink / raw) To: Paolo Bonzini; +Cc: qemu-trivial, qemu-devel, riegamaths Paolo Bonzini <pbonzini@redhat.com> writes: > Il 05/09/2012 15:26, riegamaths@gmail.com ha scritto: >> From: Dunrong Huang <riegamaths@gmail.com> >> >> The caller would not delete temporary file after failed get_tmp_filename(). >> >> Signed-off-by: Dunrong Huang <riegamaths@gmail.com> >> --- >> block.c | 6 +++++- >> 1 个文件被修改,插入 5 行(+),删除 1 行(-) >> >> diff --git a/block.c b/block.c >> index 074987e..2bc9f75 100644 >> --- a/block.c >> +++ b/block.c >> @@ -433,7 +433,11 @@ int get_tmp_filename(char *filename, int size) >> return -EOVERFLOW; >> } >> fd = mkstemp(filename); >> - if (fd < 0 || close(fd)) { >> + if (fd < 0) { >> + return -errno; >> + } >> + if (close(fd) != 0) { >> + unlink(filename); >> return -errno; >> } >> return 0; >> > > Not necessary, mkstemp will not create a file if it returns an error. Read the patch once more :) ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] block: Don't forget to delete temporary file 2012-09-05 16:02 ` Markus Armbruster @ 2012-09-05 16:23 ` Paolo Bonzini 2012-09-05 16:28 ` Eric Blake 0 siblings, 1 reply; 9+ messages in thread From: Paolo Bonzini @ 2012-09-05 16:23 UTC (permalink / raw) To: Markus Armbruster; +Cc: qemu-trivial, Eric Blake, qemu-devel, riegamaths Il 05/09/2012 18:02, Markus Armbruster ha scritto: > Paolo Bonzini <pbonzini@redhat.com> writes: > >> Il 05/09/2012 15:26, riegamaths@gmail.com ha scritto: >>> From: Dunrong Huang <riegamaths@gmail.com> >>> >>> The caller would not delete temporary file after failed get_tmp_filename(). >>> >>> Signed-off-by: Dunrong Huang <riegamaths@gmail.com> >>> --- >>> block.c | 6 +++++- >>> 1 个文件被修改,插入 5 行(+),删除 1 行(-) >>> >>> diff --git a/block.c b/block.c >>> index 074987e..2bc9f75 100644 >>> --- a/block.c >>> +++ b/block.c >>> @@ -433,7 +433,11 @@ int get_tmp_filename(char *filename, int size) >>> return -EOVERFLOW; >>> } >>> fd = mkstemp(filename); >>> - if (fd < 0 || close(fd)) { >>> + if (fd < 0) { >>> + return -errno; >>> + } >>> + if (close(fd) != 0) { >>> + unlink(filename); >>> return -errno; >>> } >>> return 0; >>> >> >> Not necessary, mkstemp will not create a file if it returns an error. > > Read the patch once more :) Oops. :) I wondered about that check for close() errors, perhaps an error in close could just be swallowed? Since we don't care about the content of the file after close (it's empty), we also don't care about errors closing it. However, perhaps there were errors writing the directory entry... Should any errors writing the directory entry be reported by open(), i.e. by mkstemp()? If not, and the dcache entry is evicted, someone could create a different file with the same name as ours. But then, not even a successful close() guarantees that the data has reached the disk. And finally, the whole get_tmp_filename is unsafe because there is a race window between closing and reopening the file, if the directory is writable and does not have the sticky bit. So the patch is an improvement, but there is still something unpleasing in this code... Paolo ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] block: Don't forget to delete temporary file 2012-09-05 16:23 ` Paolo Bonzini @ 2012-09-05 16:28 ` Eric Blake 2012-09-06 3:47 ` Dunrong Huang 0 siblings, 1 reply; 9+ messages in thread From: Eric Blake @ 2012-09-05 16:28 UTC (permalink / raw) To: Paolo Bonzini; +Cc: qemu-trivial, Markus Armbruster, riegamaths, qemu-devel [-- Attachment #1: Type: text/plain, Size: 1086 bytes --] On 09/05/2012 10:23 AM, Paolo Bonzini wrote: > And finally, the whole get_tmp_filename is unsafe because there is a > race window between closing and reopening the file, if the directory is > writable and does not have the sticky bit. > > So the patch is an improvement, but there is still something unpleasing > in this code... I absolutely agree that there is a nasty race here. If you aren't going to use the fd, then mktemp() is sufficient (and just as racy, but then you are at least honest that you don't care about the race); in all other situations, if you want a temporary file name but want to avoid a race, then it feels like you should be returning the fd from mkstemp() still open (or at a bare minimum, auditing ALL callers to make sure they only use the temporary name with O_CREAT|O_EXCL, and that they retry in a loop in case they lose the race, at which point they are reinventing the loop already done on their behalf by mkstemp()...). -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org [-- Attachment #2: OpenPGP digital signature --] [-- Type: application/pgp-signature, Size: 617 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] block: Don't forget to delete temporary file 2012-09-05 16:28 ` Eric Blake @ 2012-09-06 3:47 ` Dunrong Huang 0 siblings, 0 replies; 9+ messages in thread From: Dunrong Huang @ 2012-09-06 3:47 UTC (permalink / raw) To: Eric Blake; +Cc: qemu-trivial, Paolo Bonzini, Markus Armbruster, qemu-devel 2012/9/6 Eric Blake <eblake@redhat.com>: > On 09/05/2012 10:23 AM, Paolo Bonzini wrote: >> And finally, the whole get_tmp_filename is unsafe because there is a >> race window between closing and reopening the file, if the directory is >> writable and does not have the sticky bit. >> >> So the patch is an improvement, but there is still something unpleasing >> in this code... > > I absolutely agree that there is a nasty race here. If you aren't going > to use the fd, then mktemp() is sufficient (and just as racy, but then > you are at least honest that you don't care about the race); in all Yes, using mktemp() in get_tmp_filename() is ok because we dont care about race, but for old gcc version, e.g. for version 4.4, we will get a annoying unsecure warning "warning: the use of `mktemp' is dangerous, better use `mkstemp'", which breaks build. > other situations, if you want a temporary file name but want to avoid a > race, then it feels like you should be returning the fd from mkstemp() > still open (or at a bare minimum, auditing ALL callers to make sure they > only use the temporary name with O_CREAT|O_EXCL, and that they retry in > a loop in case they lose the race, at which point they are reinventing > the loop already done on their behalf by mkstemp()...). > > -- > Eric Blake eblake@redhat.com +1-919-301-3266 > Libvirt virtualization library http://libvirt.org > -- Best Regards, Dunrong Huang ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] block: Don't forget to delete temporary file 2012-09-05 13:26 [Qemu-devel] [PATCH] block: Don't forget to delete temporary file riegamaths 2012-09-05 15:40 ` Paolo Bonzini @ 2012-09-11 10:23 ` Kevin Wolf 2012-09-14 7:45 ` Stefan Hajnoczi 1 sibling, 1 reply; 9+ messages in thread From: Kevin Wolf @ 2012-09-11 10:23 UTC (permalink / raw) To: riegamaths; +Cc: qemu-trivial, qemu-devel Am 05.09.2012 15:26, schrieb riegamaths@gmail.com: > From: Dunrong Huang <riegamaths@gmail.com> > > The caller would not delete temporary file after failed get_tmp_filename(). > > Signed-off-by: Dunrong Huang <riegamaths@gmail.com> Thanks, applied to the block branch. Kevin ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Qemu-devel] [PATCH] block: Don't forget to delete temporary file 2012-09-11 10:23 ` Kevin Wolf @ 2012-09-14 7:45 ` Stefan Hajnoczi 0 siblings, 0 replies; 9+ messages in thread From: Stefan Hajnoczi @ 2012-09-14 7:45 UTC (permalink / raw) To: Kevin Wolf; +Cc: qemu-trivial, qemu-devel, riegamaths On Tue, Sep 11, 2012 at 12:23:37PM +0200, Kevin Wolf wrote: > Am 05.09.2012 15:26, schrieb riegamaths@gmail.com: > > From: Dunrong Huang <riegamaths@gmail.com> > > > > The caller would not delete temporary file after failed get_tmp_filename(). > > > > Signed-off-by: Dunrong Huang <riegamaths@gmail.com> > > Thanks, applied to the block branch. For the record, using the close(2) errno after calling unlink(2) isn't a good idea. We should follow the docs and preserve errno carefully. It's the exact issue I pointed out on the original patch. Stefan ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2012-09-14 7:45 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2012-09-05 13:26 [Qemu-devel] [PATCH] block: Don't forget to delete temporary file riegamaths 2012-09-05 15:40 ` Paolo Bonzini 2012-09-05 15:51 ` Dunrong Huang 2012-09-05 16:02 ` Markus Armbruster 2012-09-05 16:23 ` Paolo Bonzini 2012-09-05 16:28 ` Eric Blake 2012-09-06 3:47 ` Dunrong Huang 2012-09-11 10:23 ` Kevin Wolf 2012-09-14 7:45 ` Stefan Hajnoczi
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).