Skip to contentSkip to conversation

Create environment block safely

PATCH In review Colin Watson 2 participants 5 messages Sep 30, 2009 – Oct 2, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Create environment block safely

If grub-editenv create is interrupted, it's theoretically possible for it to create an incomplete block that future grub-editenv calls won't recognise. It would be better for it to atomically create either a complete block or nothing. Any objections?

2009-09-30 Colin Watson <[email protected]>

* util/grub-editenv.c (create_envblk_file): Write new block with a .new suffix and then rename it into place, to ensure atomic creation.

grub-editenv.c +9 -3
Index: util/grub-editenv.c
===================================================================
@@ -95,25 +95,31 @@ create_envblk_file (const char *name)
 {
   FILE *fp;
   char *buf;
+  char *namenew;
 
   buf = malloc (DEFAULT_ENVBLK_SIZE);
   if (! buf)
     grub_util_error ("out of memory");
 
-  fp = fopen (name, "wb");
+  asprintf (&namenew, "%s.new", name);
+  fp = fopen (namenew, "wb");
   if (! fp)
-    grub_util_error ("cannot open the file %s", name);
+    grub_util_error ("cannot open the file %s", namenew);
 
   memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
   memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#',
           DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
 
   if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
-    grub_util_error ("cannot write to the file %s", name);
+    grub_util_error ("cannot write to the file %s", namenew);
 
   fsync (fileno (fp));
   free (buf);
   fclose (fp);
+
+  if (rename (namenew, name) < 0)
+    grub_util_error ("cannot rename the file %s to %s", namenew, name);
+  free (namenew);
 }
 
 static grub_envblk_t
Signature · 1 line
Colin Watson                                       [[email protected]]
#1 lore raw
Quoted 10 lines · Colin Watson

If grub-editenv create is interrupted, it's theoretically possible for it to create an incomplete block that future grub-editenv calls won't recognise. It would be better for it to atomically create either a complete block or nothing. Any objections?

2009-09-30 Colin Watson <[email protected]>

* util/grub-editenv.c (create_envblk_file): Write new block with a .new suffix and then rename it into place, to ensure atomic creation.

I've gone ahead and committed this, since it's straightforward enough.

Signature · 1 line
Colin Watson                                       [[email protected]]
#2 lore raw
In reply to Colin Watson
Quoted 4 lines · Colin Watson

If grub-editenv create is interrupted, it's theoretically possible for it to create an incomplete block that future grub-editenv calls won't recognise. It would be better for it to atomically create either a complete block or nothing. Any objections?

Be aware that this may lose security settings. Write-in-place is also different wrt hard links, but it would be pretty odd for that file to have additional hard links.

You might also need to check for the alternate name, in case the operation failed after writing the new content and unlinking the old file, but before renaming the new file.

#3 lore raw
Quoted 9 lines · On Thu, Oct 01, 2009 at 09:31:55PM -0500, [email protected] wrote:
Quoted 4 lines · Colin Watson

If grub-editenv create is interrupted, it's theoretically possible for it to create an incomplete block that future grub-editenv calls won't recognise. It would be better for it to atomically create either a complete block or nothing. Any objections?

Be aware that this may lose security settings. Write-in-place is also different wrt hard links, but it would be pretty odd for that file to have additional hard links.

While this would be true for writing files in general, this is explicitly a "create" operation and so I don't think that matters. We use different code paths when writing to an existing environment block file.

Quoted 3 lines

You might also need to check for the alternate name, in case the operation failed after writing the new content and unlinking the old file, but before renaming the new file.

There is no old file here, or if there is then the user has already explicitly indicated that they don't care about it. There is no unlink call in my code (and wouldn't be even if this weren't a create operation; no need to bother when you're renaming over the top anyway).

Signature · 1 line
Colin Watson                                       [[email protected]]
#4 lore raw
Quoted 8 lines
Quoted 3 lines

You might also need to check for the alternate name, in case the operation failed after writing the new content and unlinking the old file, but before renaming the new file.

There is no old file here, or if there is then the user has already explicitly indicated that they don't care about it. There is no unlink call in my code (and wouldn't be even if this weren't a create operation; no need to bother when you're renaming over the top anyway).

I guess I didn't look closely enough at the rename docs:

The rename() system call causes the link named old to be renamed as new.
If new exists, it is first removed. Both old and new must be of the same type (that is, both must be either directories or non-directories) and must reside on the same file system.

The rename() system call guarantees that an instance of new will always exist, even if the system should crash in the middle of the operation.

So my unease was unfounded.