]> git.proxmox.com Git - grub2.git/blob - util/grub-mount.c
fs/ntfs: Fix various OOB reads and writes (CVE-2023-4692, CVE-2023-4693)
[grub2.git] / util / grub-mount.c
1 /* grub-mount.c - FUSE driver for filesystems that GRUB understands */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008,2009,2010 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 #define FUSE_USE_VERSION 26
20 #include <config.h>
21 #include <grub/types.h>
22 #include <grub/emu/misc.h>
23 #include <grub/util/misc.h>
24 #include <grub/misc.h>
25 #include <grub/device.h>
26 #include <grub/disk.h>
27 #include <grub/file.h>
28 #include <grub/fs.h>
29 #include <grub/env.h>
30 #include <grub/term.h>
31 #include <grub/mm.h>
32 #include <grub/lib/hexdump.h>
33 #include <grub/crypto.h>
34 #include <grub/command.h>
35 #include <grub/zfs/zfs.h>
36 #include <grub/i18n.h>
37 #include <fuse/fuse.h>
38
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
45 #pragma GCC diagnostic ignored "-Wmissing-declarations"
46 #include <argp.h>
47 #pragma GCC diagnostic error "-Wmissing-prototypes"
48 #pragma GCC diagnostic error "-Wmissing-declarations"
49
50 #include "progname.h"
51
52 static const char *root = NULL;
53 grub_device_t dev = NULL;
54 grub_fs_t fs = NULL;
55 static char **images = NULL;
56 static char *debug_str = NULL;
57 static char **fuse_args = NULL;
58 static int fuse_argc = 0;
59 static int num_disks = 0;
60 static int mount_crypt = 0;
61
62 static grub_err_t
63 execute_command (const char *name, int n, char **args)
64 {
65 grub_command_t cmd;
66
67 cmd = grub_command_find (name);
68 if (! cmd)
69 grub_util_error (_("can't find command `%s'"), name);
70
71 return (cmd->func) (cmd, n, args);
72 }
73
74 /* Translate GRUB error numbers into OS error numbers. Print any unexpected
75 errors. */
76 static int
77 translate_error (void)
78 {
79 int ret;
80
81 switch (grub_errno)
82 {
83 case GRUB_ERR_NONE:
84 ret = 0;
85 break;
86
87 case GRUB_ERR_OUT_OF_MEMORY:
88 grub_print_error ();
89 ret = -ENOMEM;
90 break;
91
92 case GRUB_ERR_BAD_FILE_TYPE:
93 /* This could also be EISDIR. Take a guess. */
94 ret = -ENOTDIR;
95 break;
96
97 case GRUB_ERR_FILE_NOT_FOUND:
98 ret = -ENOENT;
99 break;
100
101 case GRUB_ERR_FILE_READ_ERROR:
102 case GRUB_ERR_READ_ERROR:
103 case GRUB_ERR_IO:
104 grub_print_error ();
105 ret = -EIO;
106 break;
107
108 case GRUB_ERR_SYMLINK_LOOP:
109 ret = -ELOOP;
110 break;
111
112 default:
113 grub_print_error ();
114 ret = -EINVAL;
115 break;
116 }
117
118 /* Any previous errors were handled. */
119 grub_errno = GRUB_ERR_NONE;
120
121 return ret;
122 }
123
124 /* Context for fuse_getattr. */
125 struct fuse_getattr_ctx
126 {
127 char *filename;
128 struct grub_dirhook_info file_info;
129 int file_exists;
130 };
131
132 /* A hook for iterating directories. */
133 static int
134 fuse_getattr_find_file (const char *cur_filename,
135 const struct grub_dirhook_info *info, void *data)
136 {
137 struct fuse_getattr_ctx *ctx = data;
138
139 if ((info->case_insensitive ? grub_strcasecmp (cur_filename, ctx->filename)
140 : grub_strcmp (cur_filename, ctx->filename)) == 0)
141 {
142 ctx->file_info = *info;
143 ctx->file_exists = 1;
144 return 1;
145 }
146 return 0;
147 }
148
149 static int
150 fuse_getattr (const char *path, struct stat *st)
151 {
152 struct fuse_getattr_ctx ctx;
153 char *pathname, *path2;
154
155 if (path[0] == '/' && path[1] == 0)
156 {
157 st->st_dev = 0;
158 st->st_ino = 0;
159 st->st_mode = 0555 | S_IFDIR;
160 st->st_uid = 0;
161 st->st_gid = 0;
162 st->st_rdev = 0;
163 st->st_size = 0;
164 st->st_blksize = 512;
165 st->st_blocks = (st->st_blksize + 511) >> 9;
166 st->st_atime = st->st_mtime = st->st_ctime = 0;
167 return 0;
168 }
169
170 ctx.file_exists = 0;
171
172 pathname = xstrdup (path);
173
174 /* Remove trailing '/'. */
175 while (*pathname && pathname[grub_strlen (pathname) - 1] == '/')
176 pathname[grub_strlen (pathname) - 1] = 0;
177
178 /* Split into path and filename. */
179 ctx.filename = grub_strrchr (pathname, '/');
180 if (! ctx.filename)
181 {
182 path2 = grub_strdup ("/");
183 ctx.filename = pathname;
184 }
185 else
186 {
187 ctx.filename++;
188 path2 = grub_strdup (pathname);
189 path2[ctx.filename - pathname] = 0;
190 }
191
192 /* It's the whole device. */
193 (fs->fs_dir) (dev, path2, fuse_getattr_find_file, &ctx);
194
195 grub_free (path2);
196 if (!ctx.file_exists)
197 {
198 grub_errno = GRUB_ERR_NONE;
199 return -ENOENT;
200 }
201 st->st_dev = 0;
202 st->st_ino = 0;
203 st->st_mode = ctx.file_info.dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
204 st->st_uid = 0;
205 st->st_gid = 0;
206 st->st_rdev = 0;
207 st->st_size = 0;
208 if (!ctx.file_info.dir)
209 {
210 grub_file_t file;
211 file = grub_file_open (path, GRUB_FILE_TYPE_GET_SIZE);
212 if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
213 {
214 grub_errno = GRUB_ERR_NONE;
215 st->st_mode = (0555 | S_IFDIR);
216 }
217 else if (! file)
218 return translate_error ();
219 else
220 {
221 st->st_size = file->size;
222 grub_file_close (file);
223 }
224 }
225 st->st_blksize = 512;
226 st->st_blocks = (st->st_size + 511) >> 9;
227 st->st_atime = st->st_mtime = st->st_ctime = ctx.file_info.mtimeset
228 ? ctx.file_info.mtime : 0;
229 grub_errno = GRUB_ERR_NONE;
230 return 0;
231 }
232
233 static int
234 fuse_opendir (const char *path, struct fuse_file_info *fi)
235 {
236 return 0;
237 }
238
239 /* FIXME */
240 static grub_file_t files[65536];
241 static int first_fd = 1;
242
243 static int
244 fuse_open (const char *path, struct fuse_file_info *fi __attribute__ ((unused)))
245 {
246 grub_file_t file;
247 file = grub_file_open (path, GRUB_FILE_TYPE_MOUNT);
248 if (! file)
249 return translate_error ();
250 files[first_fd++] = file;
251 fi->fh = first_fd;
252 files[first_fd++] = file;
253 grub_errno = GRUB_ERR_NONE;
254 return 0;
255 }
256
257 static int
258 fuse_read (const char *path, char *buf, size_t sz, off_t off,
259 struct fuse_file_info *fi)
260 {
261 grub_file_t file = files[fi->fh];
262 grub_ssize_t size;
263
264 if (off > file->size)
265 return -EINVAL;
266
267 file->offset = off;
268
269 size = grub_file_read (file, buf, sz);
270 if (size < 0)
271 return translate_error ();
272 else
273 {
274 grub_errno = GRUB_ERR_NONE;
275 return size;
276 }
277 }
278
279 static int
280 fuse_release (const char *path, struct fuse_file_info *fi)
281 {
282 grub_file_close (files[fi->fh]);
283 files[fi->fh] = NULL;
284 grub_errno = GRUB_ERR_NONE;
285 return 0;
286 }
287
288 /* Context for fuse_readdir. */
289 struct fuse_readdir_ctx
290 {
291 const char *path;
292 void *buf;
293 fuse_fill_dir_t fill;
294 };
295
296 /* Helper for fuse_readdir. */
297 static int
298 fuse_readdir_call_fill (const char *filename,
299 const struct grub_dirhook_info *info, void *data)
300 {
301 struct fuse_readdir_ctx *ctx = data;
302 struct stat st;
303
304 grub_memset (&st, 0, sizeof (st));
305 st.st_mode = info->dir ? (0555 | S_IFDIR) : (0444 | S_IFREG);
306 if (!info->dir)
307 {
308 grub_file_t file;
309 char *tmp;
310 tmp = xasprintf ("%s/%s", ctx->path, filename);
311 file = grub_file_open (tmp, GRUB_FILE_TYPE_GET_SIZE);
312 free (tmp);
313 /* Symlink to directory. */
314 if (! file && grub_errno == GRUB_ERR_BAD_FILE_TYPE)
315 {
316 grub_errno = GRUB_ERR_NONE;
317 st.st_mode = (0555 | S_IFDIR);
318 }
319 else if (!file)
320 {
321 grub_errno = GRUB_ERR_NONE;
322 }
323 else
324 {
325 st.st_size = file->size;
326 grub_file_close (file);
327 }
328 }
329 st.st_blksize = 512;
330 st.st_blocks = (st.st_size + 511) >> 9;
331 st.st_atime = st.st_mtime = st.st_ctime
332 = info->mtimeset ? info->mtime : 0;
333 ctx->fill (ctx->buf, filename, &st, 0);
334 return 0;
335 }
336
337 static int
338 fuse_readdir (const char *path, void *buf,
339 fuse_fill_dir_t fill, off_t off, struct fuse_file_info *fi)
340 {
341 struct fuse_readdir_ctx ctx = {
342 .path = path,
343 .buf = buf,
344 .fill = fill
345 };
346 char *pathname;
347
348 pathname = xstrdup (path);
349
350 /* Remove trailing '/'. */
351 while (pathname [0] && pathname[1]
352 && pathname[grub_strlen (pathname) - 1] == '/')
353 pathname[grub_strlen (pathname) - 1] = 0;
354
355 (fs->fs_dir) (dev, pathname, fuse_readdir_call_fill, &ctx);
356 free (pathname);
357 grub_errno = GRUB_ERR_NONE;
358 return 0;
359 }
360
361 struct fuse_operations grub_opers = {
362 .getattr = fuse_getattr,
363 .open = fuse_open,
364 .release = fuse_release,
365 .opendir = fuse_opendir,
366 .readdir = fuse_readdir,
367 .read = fuse_read
368 };
369
370 static grub_err_t
371 fuse_init (void)
372 {
373 int i;
374
375 for (i = 0; i < num_disks; i++)
376 {
377 char *argv[2];
378 char *host_file;
379 char *loop_name;
380 loop_name = grub_xasprintf ("loop%d", i);
381 if (!loop_name)
382 grub_util_error ("%s", grub_errmsg);
383
384 host_file = grub_xasprintf ("(host)%s", images[i]);
385 if (!host_file)
386 grub_util_error ("%s", grub_errmsg);
387
388 argv[0] = loop_name;
389 argv[1] = host_file;
390
391 if (execute_command ("loopback", 2, argv))
392 grub_util_error (_("`loopback' command fails: %s"), grub_errmsg);
393
394 grub_free (loop_name);
395 grub_free (host_file);
396 }
397
398 if (mount_crypt)
399 {
400 char *argv[2] = { xstrdup ("-a"), NULL};
401 if (execute_command ("cryptomount", 1, argv))
402 grub_util_error (_("`cryptomount' command fails: %s"),
403 grub_errmsg);
404 free (argv[0]);
405 }
406
407 grub_lvm_fini ();
408 grub_mdraid09_fini ();
409 grub_mdraid1x_fini ();
410 grub_diskfilter_fini ();
411 grub_diskfilter_init ();
412 grub_mdraid09_init ();
413 grub_mdraid1x_init ();
414 grub_lvm_init ();
415
416 dev = grub_device_open (0);
417 if (! dev)
418 return grub_errno;
419
420 fs = grub_fs_probe (dev);
421 if (! fs)
422 {
423 grub_device_close (dev);
424 return grub_errno;
425 }
426
427 if (fuse_main (fuse_argc, fuse_args, &grub_opers, NULL))
428 grub_error (GRUB_ERR_IO, "fuse_main failed");
429
430 for (i = 0; i < num_disks; i++)
431 {
432 char *argv[2];
433 char *loop_name;
434
435 loop_name = grub_xasprintf ("loop%d", i);
436 if (!loop_name)
437 grub_util_error ("%s", grub_errmsg);
438
439 argv[0] = xstrdup ("-d");
440 argv[1] = loop_name;
441
442 execute_command ("loopback", 2, argv);
443
444 grub_free (argv[0]);
445 grub_free (loop_name);
446 }
447
448 return grub_errno;
449 }
450
451 static struct argp_option options[] = {
452 {"root", 'r', N_("DEVICE_NAME"), 0, N_("Set root device."), 2},
453 {"debug", 'd', N_("STRING"), 0, N_("Set debug environment variable."), 2},
454 {"crypto", 'C', NULL, 0, N_("Mount crypto devices."), 2},
455 {"zfs-key", 'K',
456 /* TRANSLATORS: "prompt" is a keyword. */
457 N_("FILE|prompt"), 0, N_("Load zfs crypto key."), 2},
458 {"verbose", 'v', NULL, 0, N_("print verbose messages."), 2},
459 {0, 0, 0, 0, 0, 0}
460 };
461
462 /* Print the version information. */
463 static void
464 print_version (FILE *stream, struct argp_state *state)
465 {
466 fprintf (stream, "%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
467 }
468 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
469
470 static error_t
471 argp_parser (int key, char *arg, struct argp_state *state)
472 {
473 switch (key)
474 {
475 case 'r':
476 root = arg;
477 return 0;
478
479 case 'K':
480 if (strcmp (arg, "prompt") == 0)
481 {
482 char buf[1024];
483 grub_printf ("%s", _("Enter ZFS password: "));
484 if (grub_password_get (buf, 1023))
485 {
486 grub_zfs_add_key ((grub_uint8_t *) buf, grub_strlen (buf), 1);
487 }
488 }
489 else
490 {
491 FILE *f;
492 ssize_t real_size;
493 grub_uint8_t buf[1024];
494 f = grub_util_fopen (arg, "rb");
495 if (!f)
496 {
497 printf (_("%s: error:"), program_name);
498 printf (_("cannot open `%s': %s"), arg, strerror (errno));
499 printf ("\n");
500 return 0;
501 }
502 real_size = fread (buf, 1, 1024, f);
503 if (real_size < 0)
504 {
505 printf (_("%s: error:"), program_name);
506 printf (_("cannot read `%s': %s"), arg,
507 strerror (errno));
508 printf ("\n");
509 fclose (f);
510 return 0;
511 }
512 grub_zfs_add_key (buf, real_size, 0);
513 fclose (f);
514 }
515 return 0;
516
517 case 'C':
518 mount_crypt = 1;
519 return 0;
520
521 case 'd':
522 debug_str = arg;
523 return 0;
524
525 case 'v':
526 verbosity++;
527 return 0;
528
529 case ARGP_KEY_ARG:
530 if (arg[0] != '-')
531 break;
532
533 /* FALLTHROUGH */
534 default:
535 if (!arg)
536 return 0;
537
538 fuse_args = xrealloc (fuse_args, (fuse_argc + 1) * sizeof (fuse_args[0]));
539 fuse_args[fuse_argc] = xstrdup (arg);
540 fuse_argc++;
541 return 0;
542 }
543
544 images = xrealloc (images, (num_disks + 1) * sizeof (images[0]));
545 images[num_disks] = grub_canonicalize_file_name (arg);
546 num_disks++;
547
548 return 0;
549 }
550
551 struct argp argp = {
552 options, argp_parser, N_("IMAGE1 [IMAGE2 ...] MOUNTPOINT"),
553 N_("Debug tool for filesystem driver."),
554 NULL, NULL, NULL
555 };
556
557 int
558 main (int argc, char *argv[])
559 {
560 const char *default_root;
561 char *alloc_root;
562
563 grub_util_host_init (&argc, &argv);
564
565 fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
566 fuse_args[fuse_argc] = xstrdup (argv[0]);
567 fuse_argc++;
568 /* Run single-threaded. */
569 fuse_args[fuse_argc] = xstrdup ("-s");
570 fuse_argc++;
571
572 argp_parse (&argp, argc, argv, 0, 0, 0);
573
574 if (num_disks < 2)
575 grub_util_error ("%s", _("need an image and mountpoint"));
576 fuse_args = xrealloc (fuse_args, (fuse_argc + 2) * sizeof (fuse_args[0]));
577 fuse_args[fuse_argc] = images[num_disks - 1];
578 fuse_argc++;
579 num_disks--;
580 fuse_args[fuse_argc] = NULL;
581
582 /* Initialize all modules. */
583 grub_init_all ();
584
585 if (debug_str)
586 grub_env_set ("debug", debug_str);
587
588 default_root = (num_disks == 1) ? "loop0" : "md0";
589 alloc_root = 0;
590 if (root)
591 {
592 if ((*root >= '0') && (*root <= '9'))
593 {
594 alloc_root = xmalloc (strlen (default_root) + strlen (root) + 2);
595
596 sprintf (alloc_root, "%s,%s", default_root, root);
597 root = alloc_root;
598 }
599 }
600 else
601 root = default_root;
602
603 grub_env_set ("root", root);
604
605 if (alloc_root)
606 free (alloc_root);
607
608 /* Do it. */
609 fuse_init ();
610 if (grub_errno)
611 {
612 grub_print_error ();
613 return 1;
614 }
615
616 /* Free resources. */
617 grub_fini_all ();
618
619 return 0;
620 }