From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 10F0B385780B for ; Fri, 30 Oct 2020 12:22:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 10F0B385780B Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-577-F_KyFh7jOlyi2vc16dDqsg-1; Fri, 30 Oct 2020 08:22:43 -0400 X-MC-Unique: F_KyFh7jOlyi2vc16dDqsg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 76BE2107AFA5; Fri, 30 Oct 2020 12:22:42 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-113-60.ams2.redhat.com [10.36.113.60]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BC3255C1CF; Fri, 30 Oct 2020 12:22:41 +0000 (UTC) From: Florian Weimer To: Adhemerval Zanella via Libc-alpha Subject: Re: [PATCH 22/28] elf: Add extension mechanism to ld.so.cache References: Date: Fri, 30 Oct 2020 13:22:40 +0100 In-Reply-To: (Adhemerval Zanella via Libc-alpha's message of "Thu, 15 Oct 2020 14:52:25 -0300") Message-ID: <87sg9vnbsf.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-6.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Oct 2020 12:22:47 -0000 * 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. >> @@ -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? >> +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. >> +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. Thanks, Florian -- Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn, Commercial register: Amtsgericht Muenchen, HRB 153243, Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill