Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1isTx9-0004EU-0A for mharc-grub-devel@gnu.org; Fri, 17 Jan 2020 10:54:31 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40594) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1isTx5-00048j-6U for grub-devel@gnu.org; Fri, 17 Jan 2020 10:54:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1isTx3-0005fJ-5e for grub-devel@gnu.org; Fri, 17 Jan 2020 10:54:26 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:57678 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1isTx3-0005ek-1d for grub-devel@gnu.org; Fri, 17 Jan 2020 10:54:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579276464; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Jf++/vdjoNdcgWDHdk3YG8qdGpUE/CURpwNJHviNSik=; b=LNGvADCWp4Sttu54XcMB8SdMWpZ9w+jdumwOwZ+7zT+VvEZ62zbxo/1Zaa7b/KrYaPEAcd Z12B6M0K9StKenHCezW8SQGLoSLT6iVFY/qU7CGHsq3XNeRW7YjNOJZBjK8RqJmw02iGVj K2p2EPbxNc0os/iLmV/HnVjPt5m4rAU= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-7-HXEw6JyYMte89WmTmX7hFQ-1; Fri, 17 Jan 2020 10:54:19 -0500 Received: by mail-wm1-f70.google.com with SMTP id n17so1288598wmk.1 for ; Fri, 17 Jan 2020 07:54:19 -0800 (PST) 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=GVL7aukBrK34VjhWxPw3aqSHTrGEkj4PqYhhbHd5ED4=; b=iehIp2BeN7uSbJrL8e48i0+NMNUGaEeiwIlCuB9R4KGAsfTHg5IFhU21BoU+0U268i Cm0h9fTNkF30ukCzWs8VuO5vckCT6K0cuQZK5fbXwIMl7XI0nV/bpMmtK+N/JCvvJnHw 6fTg479aKncgs4WpwUbq2sxkny/vjdtlpvbsSMyZuMTu5m+N5qQPGh23XQ5vm2oSzqH4 YLCeEi6cSkosgXJK6VAZBn/iTaMUJhvJqHdBXqVippxMSOZQTQz3UqojT63B5g068jIz nj/P/FxmXOeQ11P0pv28o4V/wvwvSw8Z36LJYJLECsHW0bs4ZKcl405Yg/ScPngV9Uri DvuA== X-Gm-Message-State: APjAAAWYdyv8izgc5T283pER5L4S3/aeP1IeUCnBUwT2I4AIaoWhMWHB zFGj9bQV6U3cDwNXPdieeWkO/wMxaQP+qVrCEUpqK6KuX4IuXtYfcS+x/TYbwcNjjUozrZ1L/IT afhEf9pcM0kk= X-Received: by 2002:a5d:55d1:: with SMTP id i17mr3751215wrw.165.1579276458487; Fri, 17 Jan 2020 07:54:18 -0800 (PST) X-Google-Smtp-Source: APXvYqxaTtRgN0AEhw2J2JQnOjbecn3MGqlY6y6bqyNHdODIxKXH7ucOaCDo35NZIOYTdtyEslK2aA== X-Received: by 2002:a5d:55d1:: with SMTP id i17mr3751196wrw.165.1579276458254; Fri, 17 Jan 2020 07:54:18 -0800 (PST) Received: from minerva.redhat.com ([195.166.127.210]) by smtp.gmail.com with ESMTPSA id b17sm35197405wrx.15.2020.01.17.07.54.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jan 2020 07:54:17 -0800 (PST) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Peter Jones , Jonathan Lebon , Adam Jackson , Javier Martinez Canillas Subject: [PATCH v4 2/2] Make editenv chase symlinks including those across devices Date: Fri, 17 Jan 2020 16:54:08 +0100 Message-Id: <20200117155408.3395811-2-javierm@redhat.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200117155408.3395811-1-javierm@redhat.com> References: <20200117155408.3395811-1-javierm@redhat.com> MIME-Version: 1.0 X-MC-Unique: HXEw6JyYMte89WmTmX7hFQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII 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: 207.211.31.120 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: Fri, 17 Jan 2020 15:54:28 -0000 From: Peter Jones The grub-editenv create command will wrongly overwrite /boot/grub2/grubenv 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/grubenv even when they're different mount points, which allows grub2-editenv to be 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 symlink to /boot/efi/EFI/fedora/grubenv, and the same copy of grub-set-default will work on both kinds of systems. Windows doesn't implement a readlink primitive, so the current behaviour is maintained for this operating system. Signed-off-by: Peter Jones Signed-off-by: Jonathan Lebon Reviewed-by: Adam Jackson Signed-off-by: Javier Martinez Canillas --- Changes in v4: - Only attempt following symlinks in operating sytems other than Windows. Changes in v3: - A couple of style cleanups. Changes in v2: - Address issues pointed out by Daniel Kiper in the previous version. Makefile.util.def | 11 ++++++++ util/editenv.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/Makefile.util.def b/Makefile.util.def index 969d32f0097..733a397cb2b 100644 --- a/Makefile.util.def +++ b/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 a/util/editenv.c b/util/editenv.c index eb2d0c03a98..d53d4d6c68e 100644 --- a/util/editenv.c +++ b/util/editenv.c @@ -28,6 +28,9 @@ =20 #include #include +#if !defined(_WIN32) +#include +#endif =20 #define DEFAULT_ENVBLK_SIZE=091024 =20 @@ -37,6 +40,11 @@ grub_util_create_envblk_file (const char *name) FILE *fp; char *buf; char *namenew; +#if !defined(_WIN32) + ssize_t size =3D 1; + char *rename_target =3D xstrdup (name); + int rc; +#endif =20 buf =3D xmalloc (DEFAULT_ENVBLK_SIZE); =20 @@ -60,7 +68,64 @@ grub_util_create_envblk_file (const char *name) free (buf); fclose (fp); =20 +#if defined(_WIN32) if (grub_util_rename (namenew, name) < 0) grub_util_error (_("cannot rename the file %s to %s"), namenew, name); +#else + 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) + { + free (linkbuf); + grub_util_error (_("cannot rename the file %s to %s: %m"), namen= ew, name); + } + 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); + free (linkbuf); + } + } + + 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); + } + + free (rename_target); + + if (rc < 0) + grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, na= me); +#endif + free (namenew); } --=20 2.23.0