From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59927) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aaiaM-00064c-HE for qemu-devel@nongnu.org; Tue, 01 Mar 2016 06:35:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aaiaJ-0004As-6N for qemu-devel@nongnu.org; Tue, 01 Mar 2016 06:35:26 -0500 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:34006) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aaiaI-0004Ak-So for qemu-devel@nongnu.org; Tue, 01 Mar 2016 06:35:23 -0500 Received: by mail-wm0-x244.google.com with SMTP id p65so3605094wmp.1 for ; Tue, 01 Mar 2016 03:35:22 -0800 (PST) Sender: Paolo Bonzini References: <1456771254-17511-1-git-send-email-armbru@redhat.com> <1456771254-17511-2-git-send-email-armbru@redhat.com> From: Paolo Bonzini Message-ID: <56D57E76.1080402@redhat.com> Date: Tue, 1 Mar 2016 12:35:18 +0100 MIME-Version: 1.0 In-Reply-To: <1456771254-17511-2-git-send-email-armbru@redhat.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [PATCH 01/38] exec: Fix memory allocation when memory path names new file List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster , qemu-devel@nongnu.org Cc: mlureau@redhat.com, cam@cs.ualberta.ca, claudio.fontana@huawei.com, david.marchand@6wind.com On 29/02/2016 19:40, Markus Armbruster wrote: > - if (!stat(path, &st) && S_ISDIR(st.st_mode)) { > + ret = stat(path, &st); > + if (!ret && S_ISDIR(st.st_mode)) { > + /* path names a directory -> create a temporary file there */ > /* Make name safe to use with mkstemp by replacing '/' with '_'. */ > sanitized_name = g_strdup(memory_region_name(block->mr)); > for (c = sanitized_name; *c != '\0'; c++) { > @@ -1282,13 +1271,32 @@ static void *file_ram_alloc(RAMBlock *block, > unlink(filename); > } > g_free(filename); > + } else if (!ret) { > + /* path names an existing file -> use it */ > + fd = open(path, O_RDWR); > } else { > + /* create a new file */ > fd = open(path, O_RDWR | O_CREAT, 0644); > + unlink_on_error = true; > } While at it, let's avoid TOCTTOU conditions: for (;;) { fd = open(path, O_RDWR); if (fd != -1) { break; } if (errno == ENOENT) { fd = open(path, O_RDWR | O_CREAT | O_EXCL, 0644); if (fd != -1) { unlink_on_error = true; break; } } else if (errno == EISDIR) { ... mkstemp ... if (fd != -1) { unlink_on_error = true; break; } } if (errno != EEXIST && errno != EINTR) { goto error; } } and use fstatfs in gethugepagesize. Paolo