Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1Mt84F-0000Ui-JC for mharc-grub-devel@gnu.org; Wed, 30 Sep 2009 18:54:39 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Mt84E-0000S1-4S for grub-devel@gnu.org; Wed, 30 Sep 2009 18:54:38 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Mt849-0000Gv-EX for grub-devel@gnu.org; Wed, 30 Sep 2009 18:54:37 -0400 Received: from [199.232.76.173] (port=46267 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Mt849-0000Gn-Ar for grub-devel@gnu.org; Wed, 30 Sep 2009 18:54:33 -0400 Received: from smarthost03.mail.zen.net.uk ([212.23.3.142]:34142) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Mt848-00043E-Vc for grub-devel@gnu.org; Wed, 30 Sep 2009 18:54:33 -0400 Received: from [82.69.40.219] (helo=riva.pelham.vpn.ucam.org) by smarthost03.mail.zen.net.uk with esmtp (Exim 4.63) (envelope-from ) id 1Mt847-0002BC-3U for grub-devel@gnu.org; Wed, 30 Sep 2009 22:54:31 +0000 Received: from cjwatson by riva.pelham.vpn.ucam.org with local (Exim 3.36 #1 (Debian)) for grub-devel@gnu.org id 1Mt846-0002uj-00; Wed, 30 Sep 2009 23:54:30 +0100 Date: Wed, 30 Sep 2009 23:54:30 +0100 From: Colin Watson To: grub-devel@gnu.org Message-ID: <20090930225430.GX13423@riva.ucam.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.18 (2008-05-17) X-Originating-Smarthost03-IP: [82.69.40.219] X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: [PATCH] Create environment block safely X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Sep 2009 22:54:38 -0000 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 * util/grub-editenv.c (create_envblk_file): Write new block with a .new suffix and then rename it into place, to ensure atomic creation. Index: util/grub-editenv.c =================================================================== --- util/grub-editenv.c (revision 2614) +++ util/grub-editenv.c (working copy) @@ -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 -- Colin Watson [cjwatson@ubuntu.com]