Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1iP4ns-0007T2-PV for mharc-grub-devel@gnu.org; Mon, 28 Oct 2019 09:11:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57497) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iP4nn-0007Ji-Fa for grub-devel@gnu.org; Mon, 28 Oct 2019 09:11:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iP4nm-00055f-2L for grub-devel@gnu.org; Mon, 28 Oct 2019 09:11:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59170) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iP4nl-00055L-P1 for grub-devel@gnu.org; Mon, 28 Oct 2019 09:11:18 -0400 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9A22D8535C for ; Mon, 28 Oct 2019 13:11:16 +0000 (UTC) Received: by mail-wr1-f72.google.com with SMTP id t2so2909589wri.18 for ; Mon, 28 Oct 2019 06:11:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4nicBA75z/j4h9oSbEjBJCc5Gev7tg08g/4qFOZj1s4=; b=uVwj02Oe/UnLVkM42/+QCywxieIaFx0Xm2pRBxo6YgXyGIH2zndZ5evV1505zjzXYD 6Ho2ZXqeBOxW9hPpfzivK0adgk4reMgGhbsVYmrCvIiPG2TjfRUYKNMVkHZl67Rp6BDM Sa4bpWEVjz6RnUNmBBXz7wiUY3KSQ7Jt/ShTCidv8sYGoVanWKFBkkabUSidoiJiSLQY 7Q0i23l8X4Vb2+8O8G8PcCMHp0wtHDCp1qorQ4rGQaWtmrjpnllWcDq2Wgd7VvJgY5bk 7hYYAHQ02Imdq3iHl7FUv8jSpyv2qrXDV9f+nAwHbGsxVyXrk1u551/Xw4gSVj/xFxIN wdQg== X-Gm-Message-State: APjAAAXxhk5qUEFiokAevfLdV8am9XXrmGtF7VYtVtkaOGzdf/CyyB4r 1TeTY8OPAyiykcTm7zvSab2j8EdjXvcsdOCshhBXUrll0+P+ZNX6/x6Pai/n/WDSYvRRM47nGD/ idAmfdWL/cow= X-Received: by 2002:a1c:7313:: with SMTP id d19mr16139179wmb.16.1572268274841; Mon, 28 Oct 2019 06:11:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqzUFgf+nsYF8f6K9LaT3lAykRHYY0mAzQoX1dpMgREJ4o4WSZoTaBWkxcyDXj67MEVlH+Cehg== X-Received: by 2002:a1c:7313:: with SMTP id d19mr16139157wmb.16.1572268274616; Mon, 28 Oct 2019 06:11:14 -0700 (PDT) Received: from minerva.redhat.com ([90.168.169.92]) by smtp.gmail.com with ESMTPSA id i18sm11679082wrx.14.2019.10.28.06.11.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Oct 2019 06:11:13 -0700 (PDT) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Peter Jones , Jonathan Lebon , Adam Jackson , Javier Martinez Canillas Subject: [PATCH v2 2/2] Make editenv chase symlinks including those across devices Date: Mon, 28 Oct 2019 14:11:10 +0100 Message-Id: <20191028131110.17464-2-javierm@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191028131110.17464-1-javierm@redhat.com> References: <20191028131110.17464-1-javierm@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Oct 2019 13:11:21 -0000 From: Peter Jones The grub-editenv create command will wrongly overwrite /boot/grub2/gruben= v with a regular file if grubenv is a symbolic link. But instead, it should create a new file in the path the symlink points to. This lets /boot/grub2/grubenv be a symlink to /boot/efi/EFI/fedora/gruben= v even when they're different mount points, which allows grub2-editenv to b= e the same across platforms (i.e. UEFI vs BIOS). For example, in Fedora the GRUB EFI builds have prefix set to /EFI/fedora (on the EFI System Partition), but for BIOS machine it'll be /boot/grub2 (which may or may not be its own mountpoint). With this patch, on EFI machines we can make /boot/grub2/grubenv a symlin= k to /boot/efi/EFI/fedora/grubenv, and the same copy of grub-set-default wi= ll work on both kinds of systems. Signed-off-by: Peter Jones Signed-off-by: Jonathan Lebon Reviewed-by: Adam Jackson Signed-off-by: Javier Martinez Canillas --- Changes in v2: - Address issues pointed out by Daniel Kiper in the previous version. Makefile.util.def | 11 +++++++++ util/editenv.c | 59 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git Makefile.util.def Makefile.util.def index 969d32f0097..733a397cb2b 100644 --- Makefile.util.def +++ Makefile.util.def @@ -240,8 +240,19 @@ program =3D { =20 common =3D util/grub-editenv.c; common =3D util/editenv.c; + common =3D util/grub-install-common.c; common =3D grub-core/osdep/init.c; + common =3D grub-core/osdep/compress.c; + extra_dist =3D grub-core/osdep/unix/compress.c; + extra_dist =3D grub-core/osdep/basic/compress.c; + common =3D util/mkimage.c; + common =3D util/grub-mkimage32.c; + common =3D util/grub-mkimage64.c; + common =3D grub-core/osdep/config.c; + common =3D util/config.c; + common =3D util/resolve.c; =20 + ldadd =3D '$(LIBLZMA)'; ldadd =3D libgrubmods.a; ldadd =3D libgrubgcry.a; ldadd =3D libgrubkern.a; diff --git util/editenv.c util/editenv.c index eb2d0c03a98..accc08407d9 100644 --- util/editenv.c +++ util/editenv.c @@ -28,15 +28,19 @@ =20 #include #include +#include =20 #define DEFAULT_ENVBLK_SIZE 1024 =20 void grub_util_create_envblk_file (const char *name) { + int rc; FILE *fp; char *buf; char *namenew; + ssize_t size =3D 1; + char *rename_target =3D xstrdup (name); =20 buf =3D xmalloc (DEFAULT_ENVBLK_SIZE); =20 @@ -60,7 +64,58 @@ grub_util_create_envblk_file (const char *name) free (buf); fclose (fp); =20 - if (grub_util_rename (namenew, name) < 0) - grub_util_error (_("cannot rename the file %s to %s"), namenew, name= ); + while (1) + { + char *linkbuf; + ssize_t retsize; + + linkbuf =3D xmalloc (size+1); + retsize =3D grub_util_readlink (rename_target, linkbuf, size); + if (retsize < 0 && (errno =3D=3D ENOENT || errno =3D=3D EINVAL)) + { + free (linkbuf); + break; + } + else if (retsize < 0) + { + grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, n= ame); + free (linkbuf); + free (namenew); + return; + } + else if (retsize =3D=3D size) + { + free (linkbuf); + size +=3D 128; + continue; + } + + linkbuf[retsize] =3D '\0'; + if (linkbuf[0] =3D=3D '/') + { + free (rename_target); + rename_target =3D linkbuf; + } + else + { + char *dbuf =3D xstrdup (rename_target); + const char *dir =3D dirname (dbuf); + free (rename_target); + rename_target =3D xasprintf ("%s/%s", dir, linkbuf); + free (dbuf); + } + } + + rc =3D grub_util_rename (namenew, rename_target); + if (rc < 0 && errno =3D=3D EXDEV) + { + rc =3D grub_install_copy_file (namenew, rename_target, 1); + grub_util_unlink (namenew); + } + + if (rc < 0) + grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, = name); + free (namenew); + free (rename_target); } --=20 2.21.0