Skip to content

Commit 1b13420

Browse files
andreeaflorescualexandruag
authored andcommitted
regenerate the invalid PVH image using ELFIO
This is needed so that we can easily reproduce the image build. Also, included only the needed sections in the image to have a tinier one. Signed-off-by: Andreea Florescu <[email protected]>
1 parent e398772 commit 1b13420

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

docs/TESTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ example, with minimal changes on top.
5656
| Source File | Generated Binary File |
5757
|-------------|-----------------------|
5858
| bad_align_writer.cpp | test_bad_align.bin |
59+
| invalid_pvh_note_writer.cpp | test_invalid_pvh_note.bin |
5960

6061
#### Example for generating `test_bad_align.bin`
6162

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright (C) 2001-present by Serge Lamikhov-Center
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
22+
23+
#include <elfio/elfio.hpp>
24+
25+
using namespace ELFIO;
26+
27+
int main( void )
28+
{
29+
elfio writer;
30+
31+
// You can't proceed without this function call!
32+
writer.create( ELFCLASS64, ELFDATA2LSB );
33+
34+
writer.set_os_abi( ELFOSABI_LINUX );
35+
writer.set_type( ET_EXEC );
36+
writer.set_machine( EM_X86_64 );
37+
38+
// Create a loadable segment
39+
segment* load_seg = writer.segments.add();
40+
load_seg->set_type( PT_LOAD );
41+
load_seg->set_virtual_address( 0x400000 );
42+
load_seg->set_physical_address( 0x0 );
43+
load_seg->set_flags( PF_R );
44+
load_seg->set_align( 0x200000 );
45+
46+
// Create a note segment
47+
segment* note_seg = writer.segments.add();
48+
note_seg->set_type( PT_NOTE );
49+
note_seg->set_virtual_address( 0x04000e0 );
50+
note_seg->set_physical_address( 0x04000e0 );
51+
note_seg->set_flags( PF_R );
52+
note_seg->set_align( 0x4 );
53+
54+
// Create a .note.Xen section, and add it to the note segment.
55+
section* xen_note_sec = writer.sections.add( ".note.Xen" );
56+
xen_note_sec->set_type( SHT_NOTE );
57+
xen_note_sec->set_addr_align( 0x4 );
58+
xen_note_sec->set_flags( SHF_ALLOC );
59+
note_section_accessor xen_note_writer( writer, xen_note_sec );
60+
61+
char descr[2] = { 0x1f, 0x1e };
62+
xen_note_writer.add_note( 0x12, "Xen", descr, sizeof( descr ) );
63+
64+
note_seg->add_section_index( xen_note_sec->get_index(),
65+
xen_note_sec->get_addr_align() );
66+
67+
// Setup entry point. Usually, a linker sets this address on base of
68+
// ‘_start’ label.
69+
writer.set_entry( 0x400104 );
70+
71+
// Create ELF file
72+
writer.save( "test_invalid_pvh_note.bin" );
73+
74+
return 0;
75+
}

src/loader/x86_64/elf/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ mod tests {
443443
include_bytes!("test_dummynote.bin").to_vec()
444444
}
445445

446-
fn make_bad_elfnote() -> Vec<u8> {
447-
include_bytes!("test_badnote.bin").to_vec()
446+
fn make_invalid_pvh_note() -> Vec<u8> {
447+
include_bytes!("test_invalid_pvh_note.bin").to_vec()
448448
}
449449

450450
fn make_bad_align() -> Vec<u8> {
@@ -567,7 +567,7 @@ mod tests {
567567
#[test]
568568
fn test_bad_elfnote() {
569569
let gm = create_guest_mem();
570-
let badnote_image = make_bad_elfnote();
570+
let badnote_image = make_invalid_pvh_note();
571571
assert_eq!(
572572
Some(KernelLoaderError::Elf(Error::InvalidPvhNote)),
573573
Elf::load(&gm, None, &mut Cursor::new(&badnote_image), None).err()
-632 Bytes
Binary file not shown.
412 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)