Skip to content

Conversation

iulianbarbu
Copy link

@iulianbarbu iulianbarbu commented Mar 8, 2021

In response to #66.

  • While using aling_up we're converting the function result
    to other data types. The conversions were double checked and
    comments were added to clarify why they are safe.

@iulianbarbu iulianbarbu force-pushed the fix_truncate_address branch 2 times, most recently from 18760a4 to f5fe183 Compare March 8, 2021 11:28
let descsz_aligned = align_up(u64::from(nhdr.n_descsz), n_align)?;
// How to compute next notes fields can be found in this man page:
// https://man7.org/linux/man-pages/man5/elf.5.html.
let namesz_aligned = align_up(u64::from(nhdr.n_namesz), 4)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should discard n_align here. The man page linked doesn't specifically call out the alignment being 4 (it's 4 in the example snippet but not in the definition) -

       Note sections contain a series of notes (see the struct
       definitions below).  Each note is followed by the name field
       (whose length is defined in n_namesz) and then by the descriptor
       field (whose length is defined in n_descsz) and whose starting
       address has a 4 byte alignment. 

However, the alignment used throughout the file (and which should be consistent with the one in memory) is in this case n_align, read from the program header -

       p_align
              This member holds the value to which the segments are
              aligned in memory and in the file. 

Copy link
Author

@iulianbarbu iulianbarbu Mar 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done some research and my understanding is that p_align refers to segments alignment rather than name or desc notes fields alignment. There are also other resources that point to 4-byte alignment [1], [2], [3], [4]. [1] mentions about an alignment dependent on the arch reg size (4 bytes vs 8 bytes), so I think we lack this sort of logic as well. [3] is an old mail thread regarding how should notes be aligned on 64bit systems and the overall idea was that most applications (gdb, gcc and other binutils tools) and the linux kernel expect 4 bytes alignment even on 64bit systems.

I suggest having only the alignment adjustment in this PR, letting arch dependent implementation arise as a requirement, if needed (it is unlikely, low priority at the moment and it is also dependent on the gABI implementation of the build toolchain used).

[1] https://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html#note_section - LSB details on ELF notes
[2] http://www.sco.com/developers/devspecs/gabi41.pdf - Generic ABI, page 81
[3] http://lkml.iu.edu/hypermail/linux/kernel/0611.1/0538.html - discussion around modifying note alignment on 64 bit platforms.
[4] linux kernel elf core dumper notesize - https://elixir.bootlin.com/linux/latest/source/fs/binfmt_elf.c#L1429

let descsz_aligned = align_up(u64::from(nhdr.n_descsz), 4)?;

// `namesz` and `descsz` are both `u32`s. They refer also to 4 byte sized and aligned memory
// area. We need to also verify for overflow, to be sure we do not loose information.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:typo: s/loose/lose. Similar typos in the commit title (s/loos/loss) and in a few comments below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@iulianbarbu iulianbarbu changed the title elf: align_up usage susceptible to data loos elf: align_up usage susceptible to data loss Mar 8, 2021
@iulianbarbu iulianbarbu force-pushed the fix_truncate_address branch 3 times, most recently from bae4fb3 to 43d873e Compare March 9, 2021 11:08
let namesz_aligned = align_up(u64::from(nhdr.n_namesz), phdr.p_align)?;
let descsz_aligned = align_up(u64::from(nhdr.n_descsz), phdr.p_align)?;

// `namesz` and `descsz` are both `u32`s. They refer also to 4 byte sized and aligned memory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commnet also needs to be updated as it is not 4-bytes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

* While using `aling_up` we're converting the function result
to other data types. The conversions were double checked and
comments were added to clarify why they are safe.

Signed-off-by: Iulian Barbu <iul@amazon.com>
@iulianbarbu iulianbarbu force-pushed the fix_truncate_address branch from 00530e7 to 91ea159 Compare March 9, 2021 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants