Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1iPlX0-00042D-V9 for mharc-grub-devel@gnu.org; Wed, 30 Oct 2019 06:48:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45199) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iPlWx-0003yx-FI for grub-devel@gnu.org; Wed, 30 Oct 2019 06:48:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iPlWw-0004ZJ-5B for grub-devel@gnu.org; Wed, 30 Oct 2019 06:48:47 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60434) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iPlWv-0004Xk-SY for grub-devel@gnu.org; Wed, 30 Oct 2019 06:48:46 -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 D937A4E83E for ; Wed, 30 Oct 2019 10:48:44 +0000 (UTC) Received: by mail-wr1-f72.google.com with SMTP id i10so1100044wrp.7 for ; Wed, 30 Oct 2019 03:48:44 -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=rhyRd6dHRu9B+x+GtTLsodqja/KMaIqBu4+GozWYUQE=; b=XQo6aYOaVQUDlwzLd1g2lR/NTwHM5fDIEzo9aD717IJJu6O06FffNml8IpGCs+4jJ4 L/o9j5AZxekZrdvuyUYQvL4O3WuBhpwNpBX2YBUdLs6vhJQJlO7ceuxrd2qeFpD+6abs 1DKwB3zHMZajoT0xNedbtfcdsnWvvG+uiU5GyPACNAkD5kQOD3F/W/S4EGbXVjY5Wwtg HsOOZApANdrGKwp77bGEB+4HJ7UyEMjzYbz1ACx8uUBoej096h9O7dBZ/Z3huPedGrJF p6MHoekUyfWa3mgYcBe57qDe2uATNJ9BA6Hg1xzo/6Ds4SDuz+NNnDuMOkggpWnXH38j ERIw== X-Gm-Message-State: APjAAAXIRnL9v2KySnygmfXNehjJvxMzAv5/W3D3P/QJNh0ykCofLJHW lE0qtbLVqTeGSHVhR6+wBj/fSferiwBu4Yf2jTTMJWEplqH66v8wMkZbRtSrRo5Tze4hSM4qXma pPX3JcMWvNIg= X-Received: by 2002:a05:6000:44:: with SMTP id k4mr24900855wrx.170.1572432523269; Wed, 30 Oct 2019 03:48:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqwKi28g1EqFb+wK6SGeybOF/m6r8TzG/6B/DUZ066lRvgBFSUlVNCK1Jxgb5kOzOG37BysFSQ== X-Received: by 2002:a05:6000:44:: with SMTP id k4mr24900846wrx.170.1572432523048; Wed, 30 Oct 2019 03:48:43 -0700 (PDT) Received: from minerva.redhat.com ([90.168.169.92]) by smtp.gmail.com with ESMTPSA id b3sm323341wma.13.2019.10.30.03.48.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Oct 2019 03:48:42 -0700 (PDT) From: Javier Martinez Canillas To: grub-devel@gnu.org Cc: Peter Jones , Jonathan Lebon , Adam Jackson , Javier Martinez Canillas Subject: [PATCH v3 2/2] Make editenv chase symlinks including those across devices Date: Wed, 30 Oct 2019 11:48:14 +0100 Message-Id: <20191030104815.25837-2-javierm@redhat.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191030104815.25837-1-javierm@redhat.com> References: <20191030104815.25837-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: Wed, 30 Oct 2019 10:48:48 -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 --- Daniel, I didn't add a check for x* allocation functions since as mentioned in my previous email these are not needed. The functions already check for NULL and call exit(3) if that's the case. 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 | 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..ce00da3fc1b 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) + { + free (linkbuf); + grub_util_error (_("cannot rename the file %s to %s: %m"), nam= enew, 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); + } + + 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