]> git.proxmox.com Git - grub2.git/blob - util/editenv.c
fs/ntfs: Fix various OOB reads and writes (CVE-2023-4692, CVE-2023-4693)
[grub2.git] / util / editenv.c
1 /* editenv.c - tool to edit environment block. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008,2009,2010,2013 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <grub/types.h>
22 #include <grub/emu/misc.h>
23 #include <grub/util/misc.h>
24 #include <grub/util/install.h>
25 #include <grub/lib/envblk.h>
26 #include <grub/i18n.h>
27 #include <grub/emu/hostfile.h>
28
29 #include <errno.h>
30 #include <string.h>
31 #if !defined(_WIN32)
32 #include <libgen.h>
33 #endif
34
35 #define DEFAULT_ENVBLK_SIZE 1024
36 #define GRUB_ENVBLK_MESSAGE "# WARNING: Do not edit this file by tools other than "PACKAGE"-editenv!!!\n"
37
38 void
39 grub_util_create_envblk_file (const char *name)
40 {
41 FILE *fp;
42 char *buf, *pbuf, *namenew;
43 #if !defined(_WIN32)
44 ssize_t size = 1;
45 char *rename_target = xstrdup (name);
46 int rc;
47 #endif
48
49 buf = xmalloc (DEFAULT_ENVBLK_SIZE);
50
51 namenew = xasprintf ("%s.new", name);
52 fp = grub_util_fopen (namenew, "wb");
53 if (! fp)
54 grub_util_error (_("cannot open `%s': %s"), namenew,
55 strerror (errno));
56
57 pbuf = buf;
58 memcpy (pbuf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
59 pbuf += sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
60 memcpy (pbuf, GRUB_ENVBLK_MESSAGE, sizeof (GRUB_ENVBLK_MESSAGE) - 1);
61 pbuf += sizeof (GRUB_ENVBLK_MESSAGE) - 1;
62 memset (pbuf , '#',
63 DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) - sizeof (GRUB_ENVBLK_MESSAGE) + 2);
64
65 if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
66 grub_util_error (_("cannot write to `%s': %s"), namenew,
67 strerror (errno));
68
69
70 if (grub_util_file_sync (fp) < 0)
71 grub_util_error (_("cannot sync `%s': %s"), namenew, strerror (errno));
72 free (buf);
73 fclose (fp);
74
75 #if defined(_WIN32)
76 if (grub_util_rename (namenew, name) < 0)
77 grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
78 #else
79 while (1)
80 {
81 char *linkbuf;
82 ssize_t retsize;
83
84 linkbuf = xmalloc (size + 1);
85 retsize = grub_util_readlink (rename_target, linkbuf, size);
86 if (retsize < 0 && (errno == ENOENT || errno == EINVAL))
87 {
88 free (linkbuf);
89 break;
90 }
91 else if (retsize < 0)
92 {
93 free (linkbuf);
94 grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
95 }
96 else if (retsize == size)
97 {
98 free (linkbuf);
99 size += 128;
100 continue;
101 }
102
103 linkbuf[retsize] = '\0';
104 if (linkbuf[0] == '/')
105 {
106 free (rename_target);
107 rename_target = linkbuf;
108 }
109 else
110 {
111 char *dbuf = xstrdup (rename_target);
112 const char *dir = dirname (dbuf);
113
114 free (rename_target);
115 rename_target = xasprintf ("%s/%s", dir, linkbuf);
116 free (dbuf);
117 free (linkbuf);
118 }
119 }
120
121 rc = grub_util_rename (namenew, rename_target);
122 if (rc < 0 && errno == EXDEV)
123 {
124 rc = grub_install_copy_file (namenew, rename_target, 1);
125 grub_util_unlink (namenew);
126 }
127
128 free (rename_target);
129
130 if (rc < 0)
131 grub_util_error (_("cannot rename the file %s to %s: %m"), namenew, name);
132 #endif
133
134 free (namenew);
135 }