!269 Read /etc/default/grub.d/*.cfg after /etc/default/grub

From: @zhangqiumiao 
Reviewed-by: @t_feng 
Signed-off-by: @t_feng
This commit is contained in:
openeuler-ci-bot 2023-05-29 02:32:34 +00:00 committed by Gitee
commit 021c621d62
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 223 additions and 1 deletions

View File

@ -0,0 +1,213 @@
From c9198e80f1aa2e9ae9e2bdc8b6f9e9ef601f0971 Mon Sep 17 00:00:00 2001
From: Colin Watson <cjwatson@ubuntu.com>
Date: Mon, 13 Jan 2014 12:13:10 +0000
Subject: [PATCH] Read /etc/default/grub.d/*.cfg after /etc/default/grub
Bug-Ubuntu: https://bugs.launchpad.net/bugs/901600
Forwarded: no
Last-Update: 2021-09-24
Reference:https://sources.debian.org/src/grub2/2.06-8.1/debian/patches/default-grub-d.patch/
Conflict:NA
---
grub-core/osdep/unix/config.c | 128 ++++++++++++++++++++++++++++------
util/grub-mkconfig.in | 5 ++
2 files changed, 112 insertions(+), 21 deletions(-)
diff --git a/grub-core/osdep/unix/config.c b/grub-core/osdep/unix/config.c
index 46a8815..5a8c584 100644
--- a/grub-core/osdep/unix/config.c
+++ b/grub-core/osdep/unix/config.c
@@ -24,6 +24,9 @@
#include <grub/emu/config.h>
#include <grub/util/install.h>
#include <grub/util/misc.h>
+#include <grub/list.h>
+#include <grub/safemath.h>
+#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -61,13 +64,27 @@ grub_util_get_localedir (void)
return LOCALEDIR;
}
+struct cfglist
+{
+ struct cfglist *next;
+ struct cfglist *prev;
+ char *path;
+};
+
void
grub_util_load_config (struct grub_util_config *cfg)
{
pid_t pid;
const char *argv[4];
- char *script, *ptr;
+ char *script = NULL, *ptr;
const char *cfgfile, *iptr;
+ char *cfgdir;
+ grub_util_fd_dir_t d;
+ struct cfglist *cfgpaths = NULL, *cfgpath, *next_cfgpath;
+ int num_cfgpaths = 0;
+ size_t len_cfgpaths = 0;
+ char **sorted_cfgpaths = NULL;
+ int i;
FILE *f = NULL;
int fd;
const char *v;
@@ -96,29 +113,88 @@ grub_util_load_config (struct grub_util_config *cfg)
}
cfgfile = grub_util_get_config_filename ();
- if (!grub_util_is_regular (cfgfile))
- return;
+ if (grub_util_is_regular (cfgfile))
+ {
+ size_t sz;
+
+ ++num_cfgpaths;
+ sz = strlen (cfgfile);
+ if (grub_mul (sz, 4, &sz) ||
+ grub_add (sz, sizeof (". ''; ") - 1, &sz) ||
+ grub_add (len_cfgpaths, sz, &len_cfgpaths))
+ grub_util_error ("%s", _("overflow is detected"));
+ }
+
+ cfgdir = xasprintf ("%s.d", cfgfile);
+ d = grub_util_fd_opendir (cfgdir);
+ if (d)
+ {
+ grub_util_fd_dirent_t de;
+
+ while ((de = grub_util_fd_readdir (d)))
+ {
+ const char *ext = strrchr (de->d_name, '.');
+ size_t sz;
+
+ if (!ext || strcmp (ext, ".cfg") != 0)
+ continue;
+
+ cfgpath = xmalloc (sizeof (*cfgpath));
+ cfgpath->path = grub_util_path_concat (2, cfgdir, de->d_name);
+ grub_list_push (GRUB_AS_LIST_P (&cfgpaths), GRUB_AS_LIST (cfgpath));
+ ++num_cfgpaths;
+ sz = strlen (cfgpath->path);
+ if (grub_mul (sz, 4, &sz) ||
+ grub_add (sz, sizeof (". ''; ") - 1, &sz) ||
+ grub_add (len_cfgpaths, sz, &len_cfgpaths))
+ grub_util_error ("%s", _("overflow is detected"));
+ }
+ grub_util_fd_closedir (d);
+ }
+
+ if (num_cfgpaths == 0)
+ goto out;
+
+ sorted_cfgpaths = xcalloc (num_cfgpaths, sizeof (*sorted_cfgpaths));
+ i = 0;
+ if (grub_util_is_regular (cfgfile))
+ sorted_cfgpaths[i++] = xstrdup (cfgfile);
+ FOR_LIST_ELEMENTS_SAFE (cfgpath, next_cfgpath, cfgpaths)
+ {
+ sorted_cfgpaths[i++] = cfgpath->path;
+ free (cfgpath);
+ }
+ assert (i == num_cfgpaths);
+ qsort (sorted_cfgpaths + 1, num_cfgpaths - 1, sizeof (*sorted_cfgpaths),
+ (int (*) (const void *, const void *)) strcmp);
argv[0] = "sh";
argv[1] = "-c";
- script = xcalloc (4, strlen (cfgfile) + 300);
+ if (grub_add (len_cfgpaths, 300, &len_cfgpaths))
+ grub_util_error ("%s", _("overflow is detected"));
+ script = xmalloc (len_cfgpaths);
ptr = script;
- memcpy (ptr, ". '", 3);
- ptr += 3;
- for (iptr = cfgfile; *iptr; iptr++)
+ for (i = 0; i < num_cfgpaths; i++)
{
- if (*iptr == '\\')
+ memcpy (ptr, ". '", 3);
+ ptr += 3;
+ for (iptr = sorted_cfgpaths[i]; *iptr; iptr++)
{
- memcpy (ptr, "'\\''", 4);
- ptr += 4;
- continue;
+ if (*iptr == '\\')
+ {
+ memcpy (ptr, "'\\''", 4);
+ ptr += 4;
+ continue;
+ }
+ *ptr++ = *iptr;
}
- *ptr++ = *iptr;
+ memcpy (ptr, "'; ", 3);
+ ptr += 3;
}
- strcpy (ptr, "'; printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\nSUSE_BTRFS_SNAPSHOT_BOOTING=%s\\n\" "
+ strcpy (ptr, "printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\nSUSE_BTRFS_SNAPSHOT_BOOTING=%s\\n\" "
"\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\" \"$SUSE_BTRFS_SNAPSHOT_BOOTING\"");
argv[2] = script;
@@ -138,15 +214,25 @@ grub_util_load_config (struct grub_util_config *cfg)
waitpid (pid, NULL, 0);
}
if (f)
- return;
+ goto out;
- f = grub_util_fopen (cfgfile, "r");
- if (f)
+ for (i = 0; i < num_cfgpaths; i++)
{
- grub_util_parse_config (f, cfg, 0);
- fclose (f);
+ f = grub_util_fopen (sorted_cfgpaths[i], "r");
+ if (f)
+ {
+ grub_util_parse_config (f, cfg, 0);
+ fclose (f);
+ }
+ else
+ grub_util_warn (_("cannot open configuration file `%s': %s"),
+ cfgfile, strerror (errno));
}
- else
- grub_util_warn (_("cannot open configuration file `%s': %s"),
- cfgfile, strerror (errno));
+
+out:
+ free (script);
+ for (i = 0; i < num_cfgpaths; i++)
+ free (sorted_cfgpaths[i]);
+ free (sorted_cfgpaths);
+ free (cfgdir);
}
diff --git a/util/grub-mkconfig.in b/util/grub-mkconfig.in
index 520a672..e3f2ae8 100644
--- a/util/grub-mkconfig.in
+++ b/util/grub-mkconfig.in
@@ -164,6 +164,11 @@ fi
if test -f ${sysconfdir}/default/grub ; then
. ${sysconfdir}/default/grub
fi
+for x in ${sysconfdir}/default/grub.d/*.cfg ; do
+ if [ -e "${x}" ]; then
+ . "${x}"
+ fi
+done
eval "$("${grub_get_kernel_settings}")" || true
--
2.19.1

View File

@ -326,3 +326,4 @@ Patch0318: backport-fs-iso9660-Incorrect-check-for-entry-boundary.patch
Patch0319: backport-fs-iso9660-Avoid-reading-past-the-entry-boundary.patch
Patch0320: backport-net-bootp-Fix-unchecked-return-value.patch
Patch0321: backport-osdep-linux-hostdisk-Modify-sector-by-sysfs-as-disk-sector.patch
Patch0322: backport-Read-etc-default-grub.d-.cfg-after-etc-default-grub.patch

View File

@ -14,7 +14,7 @@
Name: grub2
Epoch: 1
Version: 2.06
Release: 28
Release: 29
Summary: Bootloader with support for Linux, Multiboot and more
License: GPLv3+
URL: http://www.gnu.org/software/grub/
@ -256,6 +256,7 @@ install -m 0644 docs/grub-boot-indeterminate.service %{buildroot}%{_unitdir}
ln -s ../grub-boot-indeterminate.service %{buildroot}%{_unitdir}/system-update.target.wants
find %{buildroot}%{_unitdir}/ -type f -exec chmod a-x {} \;
mkdir %{buildroot}%{_sysconfdir}/default/grub.d
%global finddebugroot "%{_builddir}/%{?buildsubdir}/debug"
@ -349,6 +350,7 @@ fi
%config %{_sysconfdir}/grub.d/??_*
%exclude %{_sysconfdir}/grub.d/01_fallback_counting
%attr(0644,root,root) %ghost %config(noreplace) %{_sysconfdir}/default/grub
%dir %config(noreplace) %{_sysconfdir}/default/grub.d
%{_sysconfdir}/grub.d/README
%{_userunitdir}/*
%{_unitdir}/grub-boot-indeterminate.service
@ -439,6 +441,12 @@ fi
%{_datadir}/man/man*
%changelog
* Sun Apr 23 2023 zhangqiumiao <zhangqiumiao1@huawei.com> - 1:2.06-29
- Type:requirement
- CVE:NA
- SUG:NA
- DESC:Read /etc/default/grub.d/*.cfg after /etc/default/grub
* Mon Apr 10 2023 zhangqiumiao <zhangqiumiao1@huawei.com> - 1:2.06-28
- Type:bugfix
- CVE:NA