Skip to content

Commit 09c6f35

Browse files
zhuangelrbradford
authored andcommitted
loader: fix incorrect alignment for ELF notes
Fix incorrect alignment for ELF notes, starting address of name field and descriptor field have a 4-byte alignment. See refer from: - 'Note Section' of 'Executable and Linking Format (ELF) Specification' v1.2. - Linux implementations, https://elixir.bootlin.com/linux/v6.1/source/include/linux/elfnote.h#L56 Fixes: #71 Reported-by: Iulian Barbu <[email protected]> Signed-off-by: Yong He <[email protected]> Co-authored-by: Rob Bradford <[email protected]> Signed-off-by: Yong He <[email protected]>
1 parent 741965d commit 09c6f35

File tree

5 files changed

+152
-12
lines changed

5 files changed

+152
-12
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# [Upcoming Release]
2+
3+
## Fixed
4+
- [[#71]](https://github.com/rust-vmm/linux-loader/issues/71) Fix incorrect
5+
alignment for ELF notes, starting address of name field and descriptor
6+
field have a 4-byte alignment.
7+
18
# [v0.8.1]
29

310
## Fixed

docs/TESTING.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ the binaries used in the unit tests.
6262
| invalid_pvh_note_writer.cpp | test_invalid_pvh_note.bin |
6363
| dummy_note.cpp | test_dummy_note.bin |
6464
| basic_elf.cpp | test_elf.bin |
65-
| pvh_note.cpp | test_elfnote.bin |
65+
| ignored_phv.cpp | test_elfnote.bin |
66+
| ignored_phv_8byte_align.cpp | test_elfnote_8byte_align.bin |
6667

6768
#### Example for generating `test_bad_align.bin`
6869

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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( 0x400000 );
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( 0x4000b0 );
50+
note_seg->set_physical_address( 0x4000b0 );
51+
note_seg->set_flags( PF_R );
52+
note_seg->set_align( 0x8 );
53+
54+
// Create a .note.dummy section, and add it to the note segment.
55+
section* dummy_note_sec = writer.sections.add( ".note.dummy" );
56+
dummy_note_sec->set_type( SHT_NOTE );
57+
dummy_note_sec->set_addr_align( 0x4 );
58+
dummy_note_sec->set_flags( SHF_ALLOC );
59+
note_section_accessor dummy_note_writer( writer, dummy_note_sec );
60+
61+
unsigned char dummy_desc[8] = { 0xfe, 0xca, 0xfe, 0xca, 0x00, 0x00 };
62+
dummy_note_writer.add_note( 0x01, "dummy", dummy_desc, sizeof( dummy_desc ) );
63+
64+
note_seg->add_section_index( dummy_note_sec->get_index(),
65+
dummy_note_sec->get_addr_align() );
66+
67+
// Create a .note.Xen section, and add it to the note segment.
68+
section* xen_note_sec = writer.sections.add( ".note.Xen" );
69+
xen_note_sec->set_type( SHT_NOTE );
70+
xen_note_sec->set_addr_align( 0x4 );
71+
xen_note_sec->set_flags( SHF_ALLOC );
72+
note_section_accessor xen_note_writer( writer, xen_note_sec );
73+
74+
unsigned char xen_descr[8] = { 0x1f, 0xfe, 0xe1, 0x01 };
75+
xen_note_writer.add_note( 0x12, "Xen", xen_descr, sizeof( xen_descr ) );
76+
77+
note_seg->add_section_index( xen_note_sec->get_index(),
78+
xen_note_sec->get_addr_align() );
79+
80+
// Create a .note.gnu.build-id section, and add it to the note segment.
81+
section* gnu_note_sec = writer.sections.add( ".note.gnu.build-id" );
82+
gnu_note_sec->set_type( SHT_NOTE );
83+
gnu_note_sec->set_addr_align( 0x4 );
84+
gnu_note_sec->set_flags( SHF_ALLOC );
85+
note_section_accessor gnu_note_writer( writer, gnu_note_sec );
86+
87+
unsigned char gnu_descr[20] = { 0x28, 0xcc, 0x3d, 0x3d, 0x89, 0xe5, 0xbf,
88+
0xc6, 0x07, 0xa8, 0xce, 0xe3, 0x29, 0xcc,
89+
0x70, 0xd0, 0xbf, 0x34, 0x69, 0x2b };
90+
gnu_note_writer.add_note( 0x03, "GNU", gnu_descr, sizeof( gnu_descr ) );
91+
92+
note_seg->add_section_index( gnu_note_sec->get_index(),
93+
gnu_note_sec->get_addr_align() );
94+
// Setup entry point. Usually, a linker sets this address on base of
95+
// ‘_start’ label.
96+
writer.set_entry( 0x400108 );
97+
98+
// Create ELF file
99+
writer.save( "test_elfnote_8byte_align.bin" );
100+
101+
return 0;
102+
}

src/loader/x86_64/elf/mod.rs

+41-11
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ where
322322
// the PVH boot protocol.
323323
const XEN_ELFNOTE_PHYS32_ENTRY: u32 = 18;
324324

325+
// Alignment of ELF notes, starting address of name field and descriptor field have a 4-byte
326+
// alignment.
327+
//
328+
// See refer from:
329+
// - 'Note Section' of 'Executable and Linking Format (ELF) Specification' v1.2.
330+
// - Linux implementations, https://elixir.bootlin.com/linux/v6.1/source/include/linux/elfnote.h#L56
331+
const ELFNOTE_ALIGN: u64 = 4;
332+
325333
// Seek to the beginning of the note segment.
326334
kernel_image
327335
.seek(SeekFrom::Start(phdr.p_offset))
@@ -350,8 +358,8 @@ where
350358
}
351359

352360
// Skip the note header plus the size of its fields (with alignment).
353-
let namesz_aligned = align_up(u64::from(nhdr.n_namesz), phdr.p_align)?;
354-
let descsz_aligned = align_up(u64::from(nhdr.n_descsz), phdr.p_align)?;
361+
let namesz_aligned = align_up(u64::from(nhdr.n_namesz), ELFNOTE_ALIGN)?;
362+
let descsz_aligned = align_up(u64::from(nhdr.n_descsz), ELFNOTE_ALIGN)?;
355363

356364
// `namesz` and `descsz` are both `u32`s. We need to also verify for overflow, to be sure
357365
// we do not lose information.
@@ -385,7 +393,7 @@ where
385393
kernel_image
386394
.seek(SeekFrom::Current(
387395
// Safe conversion since it is not losing data.
388-
align_up(u64::from(nhdr.n_namesz), phdr.p_align)? as i64 - PVH_NOTE_STR_SZ as i64,
396+
align_up(u64::from(nhdr.n_namesz), ELFNOTE_ALIGN)? as i64 - PVH_NOTE_STR_SZ as i64,
389397
))
390398
.map_err(|_| Error::SeekNoteHeader)?;
391399

@@ -449,6 +457,10 @@ mod tests {
449457
include_bytes!("test_elfnote.bin").to_vec()
450458
}
451459

460+
fn make_elfnote_8byte_align() -> Vec<u8> {
461+
include_bytes!("test_elfnote_8byte_align.bin").to_vec()
462+
}
463+
452464
fn make_dummy_elfnote() -> Vec<u8> {
453465
include_bytes!("test_dummy_note.bin").to_vec()
454466
}
@@ -457,7 +469,7 @@ mod tests {
457469
include_bytes!("test_invalid_pvh_note.bin").to_vec()
458470
}
459471

460-
fn make_bad_align() -> Vec<u8> {
472+
fn make_elfnote_bad_align() -> Vec<u8> {
461473
include_bytes!("test_bad_align.bin").to_vec()
462474
}
463475

@@ -585,13 +597,31 @@ mod tests {
585597
}
586598

587599
#[test]
588-
fn test_bad_align() {
589-
let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (0x1000_0000_usize))]).unwrap();
590-
let bad_align_image = make_bad_align();
591-
assert_eq!(
592-
Some(KernelLoaderError::Elf(Error::Align)),
593-
Elf::load(&gm, None, &mut Cursor::new(&bad_align_image), None).err()
594-
);
600+
fn test_load_pvh_with_align() {
601+
// Alignment of ELF notes is always const value (4-bytes), ELF notes parse should not get Align
602+
// error.
603+
{
604+
let gm =
605+
GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (0x1000_0000_usize))]).unwrap();
606+
let bad_align_image = make_elfnote_bad_align();
607+
assert_ne!(
608+
Some(KernelLoaderError::Elf(Error::Align)),
609+
Elf::load(&gm, None, &mut Cursor::new(&bad_align_image), None).err()
610+
);
611+
}
612+
613+
// Alignment of ELF notes is always const value (4-byte), ELF notes parse should always
614+
// success even there is incorrect p_align in phdr.
615+
{
616+
let gm = create_guest_mem();
617+
let pvhnote_image = make_elfnote_8byte_align();
618+
let loader_result =
619+
Elf::load(&gm, None, &mut Cursor::new(&pvhnote_image), None).unwrap();
620+
assert_eq!(
621+
loader_result.pvh_boot_cap,
622+
PvhBootCapability::PvhEntryPresent(GuestAddress(0x1e1fe1f))
623+
);
624+
}
595625
}
596626

597627
#[test]
636 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)