]> git.ozlabs.org Git - petitboot/blob - discover/syslinux-parser.c
Fix pb-discover segfaults caused by list corruption.
[petitboot] / discover / syslinux-parser.c
1 #if defined(HAVE_CONFIG_H)
2 #include "config.h"
3 #endif
4
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <i18n/i18n.h>
9 #include <libgen.h>
10
11 #include "log/log.h"
12 #include "list/list.h"
13 #include "file/file.h"
14 #include "talloc/talloc.h"
15 #include "types/types.h"
16 #include "parser-conf.h"
17 #include "parser-utils.h"
18 #include "resource.h"
19 #include "paths.h"
20
21 struct syslinux_boot_option {
22         char *label;
23         char *image;
24         char *append;
25         char *initrd;
26         struct list_item list;
27 };
28
29 /* by spec 16 is allowed */
30 #define INCLUDE_NEST_LIMIT 16
31
32 struct syslinux_options {
33         struct list processed_options;
34         struct syslinux_boot_option *current_option;
35         int include_nest_level;
36         char *cfg_dir;
37 };
38
39 struct conf_file_stat {
40         char *name;
41         struct stat stat;
42         struct list_item list;
43 };
44
45 static const char *const syslinux_conf_files[] = {
46         "/boot/syslinux/syslinux.cfg",
47         "/syslinux/syslinux.cfg",
48         "/syslinux.cfg",
49         "/BOOT/SYSLINUX/SYSLINUX.CFG",
50         "/SYSLINUX/SYSLINUX.CFG",
51         "/SYSLINUX.CFG",
52         NULL
53 };
54
55 static const char *const syslinux_kernel_unsupported_extensions[] = {
56         ".0", /* eventually support PXE here? */
57         ".bin",
58         ".bs",
59         ".bss",
60         ".c32",
61         ".cbt",
62         ".com",
63         ".img",
64         NULL
65 };
66
67 static const char *const syslinux_ignored_names[] = {
68         "config",
69         "sysapend",
70         "localboot",
71         "ui",
72         "prompt",
73         "noescape",
74         "nocomplete",
75         "allowoptions",
76         "timeout",
77         "totaltimeout",
78         "ontimeout",
79         "onerror",
80         "serial",
81         "nohalt",
82         "console",
83         "font",
84         "kbdmap",
85         "say",
86         "display",
87         "f1",
88         "f2",
89         "f3",
90         "f4",
91         "f5",
92         "f6",
93         "f7",
94         "f8",
95         "f9",
96         "f10",
97         "f11",
98         "f12",
99         NULL
100 };
101
102 static const char *const syslinux_unsupported_boot_names[] = {
103         "boot",
104         "bss",
105         "pxe",
106         "fdimage",
107         "comboot",
108         "com32",
109         NULL
110 };
111
112 static struct conf_global_option syslinux_global_options[] = {
113         { .name = "default" },
114         { .name = "implicit" },
115         { .name = "append" },
116         { .name = NULL }
117 };
118
119
120 static void finish_boot_option(struct syslinux_options *state,
121                                bool free_if_unused)
122 {
123         /*
124          * in the normal this function signals a new image block which means
125          * move the current block to the list of processed items
126          * the special case is a label before an image block which we need to
127          * know whether to keep it for further processing or junk it
128          */
129         if (state->current_option) {
130                 if (state->current_option->image) {
131                         list_add(&state->processed_options,
132                                  &state->current_option->list);
133                         state->current_option = NULL;
134                 } else if (free_if_unused) {
135                         talloc_free(state->current_option);
136                         state->current_option = NULL;
137                 }
138         }
139 }
140
141 static bool start_new_option(struct syslinux_options *state)
142 {
143         bool ret = false;
144
145         finish_boot_option(state, false);
146         if (!state->current_option)
147                 state->current_option = talloc_zero(state, struct syslinux_boot_option);
148
149         if (state->current_option)
150                 ret = true;
151
152         return ret;
153 }
154
155 static void syslinux_process_pair(struct conf_context *conf, const char *name, char *value)
156 {
157         struct syslinux_options *state = conf->parser_info;
158         char *buf, *pos, *path;
159         int len, rc;
160
161         /* ignore bare values */
162         if (!name)
163                 return;
164
165         if (conf_param_in_list(syslinux_ignored_names, name))
166                 return;
167
168         /* a new boot entry needs to terminate any prior one */
169         if (conf_param_in_list(syslinux_unsupported_boot_names, name)) {
170                 finish_boot_option(state, true);
171                 return;
172         }
173
174         if (streq(name, "label")) {
175                 finish_boot_option(state, true);
176                 state->current_option = talloc_zero(state,
177                                             struct syslinux_boot_option);
178                 if (state->current_option)
179                         state->current_option->label = talloc_strdup(state, value);
180                 return;
181         }
182
183         if (streq(name, "linux")) {
184
185                 if (start_new_option(state)) {
186                         state->current_option->image = talloc_strdup(state, value);
187                         if (!state->current_option->image) {
188                                 talloc_free(state->current_option);
189                                 state->current_option = NULL;
190                         }
191                 }
192
193                 return;
194         }
195
196         if (streq(name, "kernel")) {
197
198                 if (start_new_option(state)) {
199                 /*
200                  * by spec a linux image can not have any of these
201                  * extensions, it can have no extension or anything not
202                  * in this list
203                  */
204                         pos = strrchr(value, '.');
205                         if (!pos ||
206                         !conf_param_in_list(syslinux_kernel_unsupported_extensions, pos)) {
207                                 state->current_option->image = talloc_strdup(state, value);
208                                 if (!state->current_option->image) {
209                                         talloc_free(state->current_option);
210                                         state->current_option = NULL;
211                                 }
212                         } else  /* clean up any possible trailing label */
213                                 finish_boot_option(state, true);
214                 }
215                 return;
216         }
217
218
219
220         /* APPEND can be global and/or local so need special handling */
221         if (streq(name, "append")) {
222                 if (state->current_option) {
223                         /* by spec only take last if multiple APPENDs */
224                         if (state->current_option->append)
225                                 talloc_free(state->current_option->append);
226                         state->current_option->append = talloc_strdup(state, value);
227                         if (!state->current_option->append) {
228                                 talloc_free(state->current_option);
229                                 state->current_option = NULL;
230                         }
231                 } else {
232                         finish_boot_option(state, true);
233                         conf_set_global_option(conf, name, value);
234                 }
235                 return;
236         }
237
238         /* now the general globals */
239         if (conf_set_global_option(conf, name, value)) {
240                 finish_boot_option(state, true);
241                 return;
242         }
243
244         if (streq(name, "initrd")) {
245                 if (state->current_option) {
246                         state->current_option->initrd = talloc_strdup(state, value);
247                         if (!state->current_option->initrd) {
248                                 talloc_free(state->current_option);
249                                 state->current_option = NULL;
250                         }
251                 }
252                 return;
253         }
254
255         if (streq(name, "include")) {
256                 if (state->include_nest_level < INCLUDE_NEST_LIMIT) {
257                         state->include_nest_level++;
258
259                         /* if absolute in as-is */
260                         if (value[0] == '/')
261                                 path = talloc_strdup(state, value);
262                         else /* otherwise relative to the root config file */
263                                 path = join_paths(state, state->cfg_dir, value);
264
265                         rc = parser_request_file(conf->dc, conf->dc->device, path, &buf, &len);
266                         if (!rc) {
267                                 conf_parse_buf(conf, buf, len);
268
269                                 device_handler_status_dev_info(conf->dc->handler, conf->dc->device,
270                                 _("Parsed nested syslinux configuration from %s"), value);
271                                 talloc_free(buf);
272                         } else {
273                                 device_handler_status_dev_info(conf->dc->handler, conf->dc->device,
274                                 _("Failed to parse nested syslinux configuration from %s"), value);
275                         }
276
277                         talloc_free(path);
278
279                         state->include_nest_level--;
280                 } else {
281                         device_handler_status_dev_err(conf->dc->handler, conf->dc->device,
282                                 _("Nested syslinux INCLUDE exceeds limit...ignored"));
283                 }
284                 return;
285         }
286
287         pb_debug("%s: unknown name: %s\n", __func__, name);
288 }
289
290 static void syslinux_finalize(struct conf_context *conf)
291 {
292         struct syslinux_options *state = conf->parser_info;
293         struct syslinux_boot_option *syslinux_opt, *tmp;
294         struct discover_context *dc = conf->dc;
295         struct discover_boot_option *d_opt;
296         bool implicit_image = true;
297         char *args_sigfile_default;
298         const char *global_default;
299         const char *global_append;
300         struct boot_option *opt;
301         const char *image;
302         const char *label;
303
304         /* clean up any lingering boot entries */
305         finish_boot_option(state, true);
306
307         global_append  = conf_get_global_option(conf, "append");
308         global_default = conf_get_global_option(conf, "default");
309
310         /*
311          * by spec '0' means disable
312          * note we set the default to '1' (which is by spec) in syslinux_parse
313          */
314         if (conf_get_global_option(conf, "implicit"), "0")
315                 implicit_image = false;
316
317         list_for_each_entry(&state->processed_options, syslinux_opt, list) {
318                 /* need a valid image */
319                 if (!syslinux_opt->image)
320                         continue;
321
322                 image = syslinux_opt->image;
323                 label = syslinux_opt->label;
324
325                 /* if implicit is disabled we must have a label */
326                 if (!label && !implicit_image)
327                         continue;
328
329                 d_opt = discover_boot_option_create(dc, dc->device);
330                 if (!d_opt)
331                         continue;
332                 if (!d_opt->option)
333                         goto fail;
334
335                 opt = d_opt->option;
336
337                 if (syslinux_opt->append) {
338                         /* '-' can signal do not use global APPEND */
339                         if (!strcmp(syslinux_opt->append, "-"))
340                                 opt->boot_args = talloc_strdup(opt, "");
341                         else
342                                 opt->boot_args = talloc_asprintf(opt, "%s %s",
343                                                                  global_append,
344                                                                  syslinux_opt->append);
345                 } else
346                         opt->boot_args = talloc_strdup(opt, global_append);
347
348                 if (!opt->boot_args)
349                         goto fail;
350
351                 opt->id = talloc_asprintf(opt, "%s#%s", dc->device->device->id, image);
352
353                 if (!opt->id)
354                         goto fail;
355
356                 if (label) {
357                         opt->name = talloc_strdup(opt, label);
358                         if (!strcmp(label, global_default))
359                                 opt->is_default = true;
360
361                         opt->description = talloc_asprintf(opt, "(%s) %s", label, image);
362                 } else {
363                         opt->name = talloc_strdup(opt, image);
364                         opt->description = talloc_strdup(opt, image);
365                 }
366
367                 if (!opt->name)
368                         goto fail;
369
370                 d_opt->boot_image = create_devpath_resource(d_opt, dc->device, image);
371
372                 if(!d_opt->boot_image)
373                         goto fail;
374
375                 if (syslinux_opt->initrd) {
376                         d_opt->initrd = create_devpath_resource(d_opt, dc->device,
377                                                                 syslinux_opt->initrd);
378                         opt->description = talloc_asprintf_append(opt->description, 
379                                                                   " initrd=%s",
380                                                                   syslinux_opt->initrd);
381
382                         if (!d_opt->initrd)
383                                 goto fail;
384                 }
385
386                 opt->description = talloc_asprintf_append(opt->description,
387                                                           " args=%s",
388                                                           opt->boot_args);
389
390                 if (!opt->description)
391                         goto fail;
392
393                 args_sigfile_default = talloc_asprintf(d_opt, "%s.cmdline.sig",
394                                                        image);
395
396                 if (!args_sigfile_default)
397                         goto fail;
398
399                 d_opt->args_sig_file = create_devpath_resource(d_opt, dc->device,
400                                                                args_sigfile_default);
401
402                 if (!d_opt->args_sig_file)
403                         goto fail;
404
405                 talloc_free(args_sigfile_default);
406
407                 conf_strip_str(opt->boot_args);
408                 conf_strip_str(opt->description);
409
410                 discover_context_add_boot_option(dc, d_opt);
411                 d_opt = NULL;
412                 continue;
413
414 fail:
415                 talloc_free(d_opt);
416         }
417
418         list_for_each_entry_safe(&state->processed_options, syslinux_opt, tmp, list)
419                 talloc_free(syslinux_opt);
420         list_init(&state->processed_options);
421 }
422
423 static int syslinux_parse(struct discover_context *dc)
424 {
425         struct conf_file_stat *confcmp, *confdat;
426         struct list processed_conf_files;
427         struct syslinux_options *state;
428         const char * const *filename;
429         struct conf_context *conf;
430         struct stat statbuf;
431         char *cfg_dir;
432         int len, rc;
433         char *buf;
434
435         /* Support block device boot only at present */
436         if (dc->event)
437                 return -1;
438
439         conf = talloc_zero(dc, struct conf_context);
440
441         if (!conf)
442                 return -1;
443
444         conf->dc = dc;
445         conf->global_options = syslinux_global_options,
446         conf_init_global_options(conf);
447         conf->get_pair = conf_get_pair_space;
448         conf->process_pair = syslinux_process_pair;
449
450         conf->parser_info = state = talloc_zero(conf, struct syslinux_options);
451         list_init(&state->processed_options);
452
453         list_init(&processed_conf_files);
454
455         /*
456          * set the global defaults
457          * by spec 'default' defaults to 'linux' and
458          * 'implicit' defaults to '1', we also just set
459          * and empty string in 'append' to make it easier
460          * in syslinux_finish
461          */
462         conf_set_global_option(conf, "default", "linux");
463         conf_set_global_option(conf, "implicit", "1");
464         conf_set_global_option(conf, "append", "");
465
466         for (filename = syslinux_conf_files; *filename; filename++) {
467                 /*
468                  * guard against duplicate entries in case-insensitive
469                  * filesystems, mainly vfat boot partitions
470                  */
471                 rc = parser_stat_path(dc, dc->device, *filename, &statbuf);
472                 if (rc)
473                         continue;
474
475                 rc = 0;
476
477                 list_for_each_entry(&processed_conf_files, confcmp, list) {
478                         if (confcmp->stat.st_ino == statbuf.st_ino) {
479                                 pb_log("conf file %s is a path duplicate of %s..skipping\n",
480                                        *filename, confcmp->name);
481                                 rc = 1;
482                                 break;
483                         }
484                 }
485
486                 if (rc)
487                         continue;
488
489                 rc = parser_request_file(dc, dc->device, *filename, &buf, &len);
490                 if (rc)
491                         continue;
492
493                 confdat = talloc_zero(conf, struct conf_file_stat);
494                 confdat->stat = statbuf;
495                 confdat->name = talloc_strdup(confdat, *filename);
496                 list_add(&processed_conf_files, &confdat->list);
497
498                 /*
499                  * save location of root config file for possible
500                  * INCLUDE directives later
501                  *
502                  * dirname can overwrite so need local copy to work on
503                  */
504                 cfg_dir = talloc_strdup(conf, *filename);
505                 state->cfg_dir = talloc_strdup(state, dirname(cfg_dir));
506                 talloc_free(cfg_dir);
507
508                 conf_parse_buf(conf, buf, len);
509                 device_handler_status_dev_info(dc->handler, dc->device,
510                                 _("Parsed syslinux configuration from %s"),
511                                 *filename);
512                 talloc_free(buf);
513
514                 syslinux_finalize(conf);
515         }
516
517         talloc_free(conf);
518         return 0;
519 }
520
521 static struct parser syslinux_parser = {
522         .name                   = "syslinux",
523         .parse                  = syslinux_parse,
524         .resolve_resource       = resolve_devpath_resource,
525 };
526
527 register_parser(syslinux_parser);