Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1isUD9-000743-K6 for mharc-grub-devel@gnu.org; Fri, 17 Jan 2020 11:11:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:43321) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1isUD4-00070u-B7 for grub-devel@gnu.org; Fri, 17 Jan 2020 11:11:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1isUCz-0002MO-Uu for grub-devel@gnu.org; Fri, 17 Jan 2020 11:10:58 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:38895 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 1isUCz-0002M3-Qx for grub-devel@gnu.org; Fri, 17 Jan 2020 11:10:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579277453; 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=QMHS/ZlH2f4V6Nq9WJcWFwe1Q219hwJXf9gwNJFy+LY=; b=IWhErG8e+faOWhUYXMUgmYihJCATYb0BHTj7FRm0j+WqMLdbmXfPZymvfclDdzuL2BVo7q WqPvxCV0PT0TEBy3kCXU4As1+ZVoMJt+K36XezQ/ZEDRjnlakgt2mi7e5ONHX8ndYODuku X5ImQk1WJhV8sHb54MV9KRZLH7aVzPE= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-186-6LhU61whPo-d5vSVCwSvbg-1; Fri, 17 Jan 2020 11:10:51 -0500 Received: by mail-wr1-f72.google.com with SMTP id w6so10620459wrm.16 for ; Fri, 17 Jan 2020 08:10:51 -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=hzKjykOQske4TBc7C3t3Y9fuuqduP3t6dMic2uz1taA=; b=HA0/riOUce5FIwtCoFXXzSLjC352ufpB/8c025Q6QbAyacO97wWjP1+pIyUfkwjHyB Lmq81pVAkXYuUEz+hVUHSJ9BIV/usz1hzN1i0zfKj8Uw5433+XwDy0/lo08QiL7Y+Q1a 8vb0lmAjFKQ2zC8JtpQBWtmcxdR7gMBXLpur+emEO62HFL8l9v7Yzx95wG0V8i3gEhay nEuzdsIDkDo3GlWEDf0qVUP86UshI2eCjITuLT/L+CY16Kk3dGVHXNmSKsVLo6icb4FQ P2qQ802eMeI7G372aUaudZOtNIzn9a+KnE6MBiUX8OAYAnXTZWDmybY/NBviSo5likOc oV/w== X-Gm-Message-State: APjAAAVihEVpH/6tVubzd1nTR7JZNhA6RIu3CyKXCkQ3NLQ+B4+H5t9x /j6eWTcP9B4ZA48jWD4mpU3p0h/jQTvd1LfFpycQxMO8JAwExGvqBZ2vbiK3DRmPVn/xSxd2Jc0 FNtsxCIESNEQ= X-Received: by 2002:adf:f80b:: with SMTP id s11mr4057845wrp.12.1579277449485; Fri, 17 Jan 2020 08:10:49 -0800 (PST) X-Google-Smtp-Source: APXvYqz3LK7/Jt2/aCgi7PKMvRJIwlczLh2K9x3l0lNPSOdQ7Dx9XW90DPhztrXhs8eVEVrjsSYg3Q== X-Received: by 2002:adf:f80b:: with SMTP id s11mr4057826wrp.12.1579277449218; Fri, 17 Jan 2020 08:10:49 -0800 (PST) Received: from minerva.redhat.com ([195.166.127.210]) by smtp.gmail.com with ESMTPSA id n1sm32924704wrw.52.2020.01.17.08.10.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jan 2020 08:10:48 -0800 (PST) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Peter Jones , Jonathan Lebon , Adam Jackson , Javier Martinez Canillas Subject: [RESEND PATCH v4 2/2] Make editenv chase symlinks including those across devices Date: Fri, 17 Jan 2020 17:10:43 +0100 Message-Id: <20200117161043.3474202-2-javierm@redhat.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200117161043.3474202-1-javierm@redhat.com> References: <20200117161043.3474202-1-javierm@redhat.com> MIME-Version: 1.0 X-MC-Unique: 6LhU61whPo-d5vSVCwSvbg-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: 205.139.110.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 16:11:02 -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 94336392b9e..51a254f1a6f 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -242,8 +242,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 81f68bd107b..c532b046f0e 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 #define GRUB_ENVBLK_MESSAGE=09"# WARNING: Do not edit this file by tools o= ther than "PACKAGE"-editenv!!!\n" @@ -37,6 +40,11 @@ grub_util_create_envblk_file (const char *name) { FILE *fp; char *buf, *pbuf, *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 @@ -64,7 +72,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