Skip to content

Commit dabe786

Browse files
rhvgoyaltorvalds
authored andcommitted
kexec: move segment verification code in a separate function
Previously do_kimage_alloc() will allocate a kimage structure, copy segment list from user space and then do the segment list sanity verification. Break down this function in 3 parts. do_kimage_alloc_init() to do actual allocation and basic initialization of kimage structure. copy_user_segment_list() to copy segment list from user space and sanity_check_segment_list() to verify the sanity of segment list as passed by user space. In later patches, I need to only allocate kimage and not copy segment list from user space. So breaking down in smaller functions enables re-use of code at other places. Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Cc: Borislav Petkov <bp@suse.de> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Cc: Yinghai Lu <yinghai@kernel.org> Cc: Eric Biederman <ebiederm@xmission.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Matthew Garrett <mjg59@srcf.ucam.org> Cc: Greg Kroah-Hartman <greg@kroah.com> Cc: Dave Young <dyoung@redhat.com> Cc: WANG Chao <chaowang@redhat.com> Cc: Baoquan He <bhe@redhat.com> Cc: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7d3e2bc commit dabe786

1 file changed

Lines changed: 100 additions & 82 deletions

File tree

‎kernel/kexec.c‎

Lines changed: 100 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -125,45 +125,27 @@ static struct page *kimage_alloc_page(struct kimage *image,
125125
gfp_t gfp_mask,
126126
unsigned long dest);
127127

128-
static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
129-
unsigned long nr_segments,
130-
struct kexec_segment __user *segments)
128+
static int copy_user_segment_list(struct kimage *image,
129+
unsigned long nr_segments,
130+
struct kexec_segment __user *segments)
131131
{
132+
int ret;
132133
size_t segment_bytes;
133-
struct kimage *image;
134-
unsigned long i;
135-
int result;
136-
137-
/* Allocate a controlling structure */
138-
result = -ENOMEM;
139-
image = kzalloc(sizeof(*image), GFP_KERNEL);
140-
if (!image)
141-
goto out;
142-
143-
image->head = 0;
144-
image->entry = &image->head;
145-
image->last_entry = &image->head;
146-
image->control_page = ~0; /* By default this does not apply */
147-
image->start = entry;
148-
image->type = KEXEC_TYPE_DEFAULT;
149-
150-
/* Initialize the list of control pages */
151-
INIT_LIST_HEAD(&image->control_pages);
152-
153-
/* Initialize the list of destination pages */
154-
INIT_LIST_HEAD(&image->dest_pages);
155-
156-
/* Initialize the list of unusable pages */
157-
INIT_LIST_HEAD(&image->unusable_pages);
158134

159135
/* Read in the segments */
160136
image->nr_segments = nr_segments;
161137
segment_bytes = nr_segments * sizeof(*segments);
162-
result = copy_from_user(image->segment, segments, segment_bytes);
163-
if (result) {
164-
result = -EFAULT;
165-
goto out;
166-
}
138+
ret = copy_from_user(image->segment, segments, segment_bytes);
139+
if (ret)
140+
ret = -EFAULT;
141+
142+
return ret;
143+
}
144+
145+
static int sanity_check_segment_list(struct kimage *image)
146+
{
147+
int result, i;
148+
unsigned long nr_segments = image->nr_segments;
167149

168150
/*
169151
* Verify we have good destination addresses. The caller is
@@ -185,9 +167,9 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
185167
mstart = image->segment[i].mem;
186168
mend = mstart + image->segment[i].memsz;
187169
if ((mstart & ~PAGE_MASK) || (mend & ~PAGE_MASK))
188-
goto out;
170+
return result;
189171
if (mend >= KEXEC_DESTINATION_MEMORY_LIMIT)
190-
goto out;
172+
return result;
191173
}
192174

193175
/* Verify our destination addresses do not overlap.
@@ -208,7 +190,7 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
208190
pend = pstart + image->segment[j].memsz;
209191
/* Do the segments overlap ? */
210192
if ((mend > pstart) && (mstart < pend))
211-
goto out;
193+
return result;
212194
}
213195
}
214196

@@ -220,18 +202,61 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
220202
result = -EINVAL;
221203
for (i = 0; i < nr_segments; i++) {
222204
if (image->segment[i].bufsz > image->segment[i].memsz)
223-
goto out;
205+
return result;
224206
}
225207

226-
result = 0;
227-
out:
228-
if (result == 0)
229-
*rimage = image;
230-
else
231-
kfree(image);
208+
/*
209+
* Verify we have good destination addresses. Normally
210+
* the caller is responsible for making certain we don't
211+
* attempt to load the new image into invalid or reserved
212+
* areas of RAM. But crash kernels are preloaded into a
213+
* reserved area of ram. We must ensure the addresses
214+
* are in the reserved area otherwise preloading the
215+
* kernel could corrupt things.
216+
*/
232217

233-
return result;
218+
if (image->type == KEXEC_TYPE_CRASH) {
219+
result = -EADDRNOTAVAIL;
220+
for (i = 0; i < nr_segments; i++) {
221+
unsigned long mstart, mend;
234222

223+
mstart = image->segment[i].mem;
224+
mend = mstart + image->segment[i].memsz - 1;
225+
/* Ensure we are within the crash kernel limits */
226+
if ((mstart < crashk_res.start) ||
227+
(mend > crashk_res.end))
228+
return result;
229+
}
230+
}
231+
232+
return 0;
233+
}
234+
235+
static struct kimage *do_kimage_alloc_init(void)
236+
{
237+
struct kimage *image;
238+
239+
/* Allocate a controlling structure */
240+
image = kzalloc(sizeof(*image), GFP_KERNEL);
241+
if (!image)
242+
return NULL;
243+
244+
image->head = 0;
245+
image->entry = &image->head;
246+
image->last_entry = &image->head;
247+
image->control_page = ~0; /* By default this does not apply */
248+
image->type = KEXEC_TYPE_DEFAULT;
249+
250+
/* Initialize the list of control pages */
251+
INIT_LIST_HEAD(&image->control_pages);
252+
253+
/* Initialize the list of destination pages */
254+
INIT_LIST_HEAD(&image->dest_pages);
255+
256+
/* Initialize the list of unusable pages */
257+
INIT_LIST_HEAD(&image->unusable_pages);
258+
259+
return image;
235260
}
236261

237262
static void kimage_free_page_list(struct list_head *list);
@@ -244,10 +269,19 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
244269
struct kimage *image;
245270

246271
/* Allocate and initialize a controlling structure */
247-
image = NULL;
248-
result = do_kimage_alloc(&image, entry, nr_segments, segments);
272+
image = do_kimage_alloc_init();
273+
if (!image)
274+
return -ENOMEM;
275+
276+
image->start = entry;
277+
278+
result = copy_user_segment_list(image, nr_segments, segments);
249279
if (result)
250-
goto out;
280+
goto out_free_image;
281+
282+
result = sanity_check_segment_list(image);
283+
if (result)
284+
goto out_free_image;
251285

252286
/*
253287
* Find a location for the control code buffer, and add it
@@ -259,22 +293,21 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
259293
get_order(KEXEC_CONTROL_PAGE_SIZE));
260294
if (!image->control_code_page) {
261295
pr_err("Could not allocate control_code_buffer\n");
262-
goto out_free;
296+
goto out_free_image;
263297
}
264298

265299
image->swap_page = kimage_alloc_control_pages(image, 0);
266300
if (!image->swap_page) {
267301
pr_err("Could not allocate swap buffer\n");
268-
goto out_free;
302+
goto out_free_control_pages;
269303
}
270304

271305
*rimage = image;
272306
return 0;
273-
274-
out_free:
307+
out_free_control_pages:
275308
kimage_free_page_list(&image->control_pages);
309+
out_free_image:
276310
kfree(image);
277-
out:
278311
return result;
279312
}
280313

@@ -284,45 +317,31 @@ static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
284317
{
285318
int result;
286319
struct kimage *image;
287-
unsigned long i;
288320

289-
image = NULL;
290321
/* Verify we have a valid entry point */
291-
if ((entry < crashk_res.start) || (entry > crashk_res.end)) {
292-
result = -EADDRNOTAVAIL;
293-
goto out;
294-
}
322+
if ((entry < crashk_res.start) || (entry > crashk_res.end))
323+
return -EADDRNOTAVAIL;
295324

296325
/* Allocate and initialize a controlling structure */
297-
result = do_kimage_alloc(&image, entry, nr_segments, segments);
298-
if (result)
299-
goto out;
326+
image = do_kimage_alloc_init();
327+
if (!image)
328+
return -ENOMEM;
329+
330+
image->start = entry;
300331

301332
/* Enable the special crash kernel control page
302333
* allocation policy.
303334
*/
304335
image->control_page = crashk_res.start;
305336
image->type = KEXEC_TYPE_CRASH;
306337

307-
/*
308-
* Verify we have good destination addresses. Normally
309-
* the caller is responsible for making certain we don't
310-
* attempt to load the new image into invalid or reserved
311-
* areas of RAM. But crash kernels are preloaded into a
312-
* reserved area of ram. We must ensure the addresses
313-
* are in the reserved area otherwise preloading the
314-
* kernel could corrupt things.
315-
*/
316-
result = -EADDRNOTAVAIL;
317-
for (i = 0; i < nr_segments; i++) {
318-
unsigned long mstart, mend;
338+
result = copy_user_segment_list(image, nr_segments, segments);
339+
if (result)
340+
goto out_free_image;
319341

320-
mstart = image->segment[i].mem;
321-
mend = mstart + image->segment[i].memsz - 1;
322-
/* Ensure we are within the crash kernel limits */
323-
if ((mstart < crashk_res.start) || (mend > crashk_res.end))
324-
goto out_free;
325-
}
342+
result = sanity_check_segment_list(image);
343+
if (result)
344+
goto out_free_image;
326345

327346
/*
328347
* Find a location for the control code buffer, and add
@@ -334,15 +353,14 @@ static int kimage_crash_alloc(struct kimage **rimage, unsigned long entry,
334353
get_order(KEXEC_CONTROL_PAGE_SIZE));
335354
if (!image->control_code_page) {
336355
pr_err("Could not allocate control_code_buffer\n");
337-
goto out_free;
356+
goto out_free_image;
338357
}
339358

340359
*rimage = image;
341360
return 0;
342361

343-
out_free:
362+
out_free_image:
344363
kfree(image);
345-
out:
346364
return result;
347365
}
348366

0 commit comments

Comments
 (0)