crypto: Fix use after free.
[grub.git] / grub-core / normal / crypto.c
1 /* crypto.c - support crypto autoload */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 2009  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 <grub/dl.h>
21 #include <grub/mm.h>
22 #include <grub/env.h>
23 #include <grub/misc.h>
24 #include <grub/crypto.h>
25 #include <grub/normal.h>
26
27 struct load_spec
28 {
29   struct load_spec *next;
30   char *name;
31   char *modname;
32 };
33
34 static struct load_spec *crypto_specs = NULL;
35
36 static void 
37 grub_crypto_autoload (const char *name)
38 {
39   struct load_spec *cur;
40   grub_dl_t mod;
41   static int depth = 0;
42
43   /* Some bufio of filesystems may want some crypto modules.
44      It may result in infinite recursion. Hence this check.  */
45   if (depth)
46     return;
47   depth++;
48
49   for (cur = crypto_specs; cur; cur = cur->next)
50     if (grub_strcasecmp (name, cur->name) == 0)
51       {
52         mod = grub_dl_load (cur->modname);
53         if (mod)
54           grub_dl_ref (mod);
55         grub_errno = GRUB_ERR_NONE;
56       }
57   depth--;
58 }
59
60 static void 
61 grub_crypto_spec_free (void)
62 {
63   struct load_spec *cur, *next;
64   for (cur = crypto_specs; cur; cur = next)
65     {
66       next = cur->next;
67       grub_free (cur->name);
68       grub_free (cur->modname);
69       grub_free (cur);
70     }
71   crypto_specs = NULL;
72 }
73
74
75 /* Read the file crypto.lst for auto-loading.  */
76 void
77 read_crypto_list (const char *prefix)
78 {
79   char *filename;
80   grub_file_t file;
81   char *buf = NULL;
82
83   if (!prefix)
84     {
85       grub_errno = GRUB_ERR_NONE;
86       return;
87     }
88   
89   filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
90                              "/crypto.lst", prefix);
91   if (!filename)
92     {
93       grub_errno = GRUB_ERR_NONE;
94       return;
95     }
96
97   file = grub_file_open (filename);
98   grub_free (filename);
99   if (!file)
100     {
101       grub_errno = GRUB_ERR_NONE;
102       return;
103     }
104
105   /* Override previous crypto.lst.  */
106   grub_crypto_spec_free ();
107
108   for (;; grub_free (buf))
109     {
110       char *p, *name;
111       struct load_spec *cur;
112       
113       buf = grub_file_getline (file);
114         
115       if (! buf)
116         break;
117       
118       name = buf;
119       while (grub_isspace (name[0]))
120         name++;
121
122       p = grub_strchr (name, ':');
123       if (! p)
124         continue;
125       
126       *p = '\0';
127       p++;
128       while (*p == ' ' || *p == '\t')
129         p++;
130
131       cur = grub_malloc (sizeof (*cur));
132       if (!cur)
133         {
134           grub_errno = GRUB_ERR_NONE;
135           continue;
136         }
137       
138       cur->name = grub_strdup (name);
139       if (! cur->name)
140         {
141           grub_errno = GRUB_ERR_NONE;
142           grub_free (cur);
143           continue;
144         }
145         
146       cur->modname = grub_strdup (p);
147       if (! cur->modname)
148         {
149           grub_errno = GRUB_ERR_NONE;
150           grub_free (cur->name);
151           grub_free (cur);
152           continue;
153         }
154       cur->next = crypto_specs;
155       crypto_specs = cur;
156     }
157   
158   grub_file_close (file);
159
160   grub_errno = GRUB_ERR_NONE;
161
162   grub_crypto_autoload_hook = grub_crypto_autoload;
163 }