[PATCH 22/28] elf: Add extension mechanism to ld.so.cache
Adhemerval Zanella
adhemerval.zanella@linaro.org
Tue Nov 3 12:45:43 GMT 2020
On 30/10/2020 09:22, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
>
>>> +/* Print the extension information at the cache at start address
>>> + FILE_BASE, of ltength FILE_SIZE bytes. The new-format cache header
>>
>> s/ltength/length
>
> Fixed.
>
>>> + is at CACHE, and the file name for diagnostics is CACHE_NAME. */
>>> +static void
>>> +print_extensions (struct cache_extension_all_loaded *ext)
>>> +{
>>> + if (ext->sections[cache_extension_tag_generator].base != NULL)
>>> + {
>>> + fputs (_("Cache generated by: "), stdout);
>>> + fwrite (ext->sections[cache_extension_tag_generator].base, 1,
>>> + ext->sections[cache_extension_tag_generator].size, stdout);
>>> + putchar ('\n');
>>> + }
>>> +}
>>> +
>>
>> Ok. Will be the extension tag data always comprised of ascii printable
>> characters?
>
> For the generator extension: Yes. But not for other extensions.
> This code only deals with the generator extension.
>
>>> +/* Size of the cache extension directory. All tags are assumed to be
>>> + present. */
>>> +enum
>>> + {
>>> + cache_extension_size = (offsetof (struct cache_extension, sections)
>>> + + (cache_extension_count
>>> + * sizeof (struct cache_extension_section)))
>>> + };
>>> +
>>> +/* Write the cache extensions to FD. The extension directory is
>>> + assumed to be located at CACHE_EXTENSION_OFFSET. */
>>> +static void
>>> +write_extensions (int fd, uint32_t cache_extension_offset)
>>> +{
>>> + assert ((cache_extension_offset % 4) == 0);
>>
>> Maybe a proper error msg instead of an assert here?
>
> This is in the cache generator, so it's a code bug if the assert fires,
> not corrupted input.
Ack.
>
>>> @@ -435,6 +497,25 @@ save_cache (const char *cache_name)
>>> && idx_old < cache_entry_old_count)
>>> file_entries->libs[idx_old] = file_entries->libs[idx_old - 1];
>>>
>>> + /* Compute the location of the extension directory. This
>>> + implementation puts the directory after the string table. The
>>> + size computation matches the write calls below. The extension
>>> + directory does not exist with format 0, so the value does not
>>> + matter. */
>>> + uint32_t extension_offset = 0;
>>> + if (opt_format != 2)
>>> + extension_offset += file_entries_size;
>>> + if (opt_format != 0)
>>> + {
>>> + if (opt_format != 2)
>>> + extension_offset += pad;
>>> + extension_offset += file_entries_new_size;
>>> + }
>>
>> Ok, although I think we should be good move the 'opt_format' definition to
>> a proper enumeration.
>
> Can this wait? Something for a future patch?
Alright, although it does simplify reading this patchset and the
change should most mechanical.
>
>>> +static bool __attribute__ ((unused))
>>
>> Maybe use inline and let the compiler decide? Or the function is
>> really duplicate in a lot of places?
>
> The compiler already decides for static functions and will inline them
> if they are only used once. Given the size of the function, I think
> that's the appropriate approach here.
Ack.
>
>>> +cache_extension_load (const struct cache_file_new *cache,
>>> + const void *file_base, size_t file_size,
>>> + struct cache_extension_all_loaded *loaded)
>>> +{
>>> + memset (loaded, 0, sizeof (*loaded));
>>> + if (cache->extension_offset == 0)
>>> + /* No extensions present. This is not a format error. */
>>> + return true;
>>> + if ((cache->extension_offset % 4) != 0)
>>> + /* Extension offset is misaligned. */
>>> + return false;
>>> + size_t size_tmp;
>>> + if (__builtin_add_overflow (cache->extension_offset,
>>> + sizeof (struct cache_extension), &size_tmp)
>>> + || size_tmp > file_size)
>>> + /* Extension extends beyond the end of the file. */
>>> + return false;
>>> + const struct cache_extension *ext = file_base + cache->extension_offset;
>>
>> Maybe we should add an alignment check for 'file_base' as well (to
>> avoid unaligned struct member deference)?
>
> This pointer comes from mmap, so it's not something that can be wrong as
> the result of the file data. I could add an assert, but I don't think
> this is likely to go wrong.
Ack.
More information about the Libc-alpha
mailing list