Patchwork may go down for maintenance today
Hello, FYI, I'm working on a django and patchwork upgrade on sourceware today so you might see some downtime in the morning and afternoon EDT. Sid
Re: [RFC][PATCH] * bits/confname.h: Define _CS_POSIX_V7_THREADS_CFLAGS, _CS_POSIX_V7_THREADS_LDFLAGS
On 10/27/20 5:03 AM, Érico Nogueira via Libc-alpha wrote: From: Érico Rolim I would like to add these enums in order to conform to the POSIX specification: https://www.man7.org/linux/man-pages/man0/unistd.h.0p.html This change also helps musl-libc, since they want to have these defines with the same values as glibc. I tried to add them for the bits/confname.h and conform/data/unistd.h-data files following the example of the other values, but didn't know how to plug them into the confstr / __sysconf implementations. It's been a while since I touched this code, but IIRC you need to add your new variables to posix/posix-conf-vars.list. That said, could you please let me know if you have a copyright assignment agreement with the FSF? I suppose this change may be OK given that it is under 20 lines but one of the stewards could confirm for sure. Thanks, Siddhesh bits/confname.h| 7 ++- conform/data/unistd.h-data | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bits/confname.h b/bits/confname.h index 5dc8215093..fdc5403697 100644 --- a/bits/confname.h +++ b/bits/confname.h @@ -670,6 +670,11 @@ enum _CS_V6_ENV, #define _CS_V6_ENV_CS_V6_ENV -_CS_V7_ENV +_CS_V7_ENV, #define _CS_V7_ENV_CS_V7_ENV + +_CS_POSIX_V7_THREADS_CFLAGS, +#define _CS_POSIX_V7_THREADS_CFLAGS _CS_POSIX_V7_THREADS_CFLAGS +_CS_POSIX_V7_THREADS_LDFLAGS +#define _CS_POSIX_V7_THREADS_LDFLAGS _CS_POSIX_V7_THREADS_LDFLAGS }; diff --git a/conform/data/unistd.h-data b/conform/data/unistd.h-data index aa070528e8..6c4b52a733 100644 --- a/conform/data/unistd.h-data +++ b/conform/data/unistd.h-data @@ -367,6 +367,8 @@ constant _CS_POSIX_V7_LP64_OFF64_LIBS constant _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS constant _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS constant _CS_POSIX_V7_LPBIG_OFFBIG_LIBS +constant _CS_POSIX_V7_THREADS_CFLAGS +constant _CS_POSIX_V7_THREADS_LDFLAGS constant _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS constant _CS_V7_ENV # endif
Re: [PATCH] libdwfl: Rewrite reading of ar_size in elf_begin_rand
On 2022-07-28 09:48, Mark Wielaard wrote: With GCC 12.1.1, glibc 2.3a, -fsanitize=undefined and -D_FORTIFY_SOURCE=3 we get the following error message: In file included from /usr/include/ar.h:22, from ../libelf/libelfP.h:33, from core-file.c:31: In function ‘pread’, inlined from ‘pread_retry’ at ../lib/system.h:188:21, inlined from ‘elf_begin_rand’ at core-file.c:86:16, inlined from ‘core_file_read_eagerly’ at core-file.c:205:15: /usr/include/bits/unistd.h:74:10: error: ‘__pread_alias’ writing 58 or more bytes into a region of size 10 overflows the destination [-Werror=stringop-overflow=] 74 | return __glibc_fortify (pread, __nbytes, sizeof (char), | ^~~ /usr/include/ar.h: In function ‘core_file_read_eagerly’: /usr/include/ar.h:41:10: note: destination object ‘ar_size’ of size 10 41 | char ar_size[10]; /* File size, in ASCII decimal. */ | ^~~ /usr/include/bits/unistd.h:50:16: note: in a call to function ‘__pread_alias’ declared with attribute ‘access (write_only, 2, 3)’ 50 | extern ssize_t __REDIRECT (__pread_alias, |^~ cc1: all warnings being treated as errors The warning disappears when dropping either -fsanitize=undefined or when using -D_FORTIFY_SOURCE=2. It looks like a false positive. But I haven't figured out how/why it happens. Interesting, I'll take a closer look at this from the gcc context. I obviously don't have any strong opinions about the elfutils patch :) Thanks, Sid The code is a little tricky to proof correct though. The ar_size field is a not-zero terminated string ASCII decimal, right-paddedr with spaces. Which is then converted with strtoll. Relying on the fact that the struct ar_hdr is zero initialized, so there will be a zero byte after the ar_size field. Rewrite the code to just use a zero byte terminated char array. Which is much easier to reason about. As a bonus the error disappears. Signed-off-by: Mark Wielaard --- libdwfl/ChangeLog | 5 + libdwfl/core-file.c | 26 -- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index 75c53948..acdaa013 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,8 @@ +2022-07-28 Mark Wielaard + + * core-file.c (elf_begin_rand): Replace struct ar_hdr h with + a char ar_size[AR_SIZE_CHARS + 1] array to read size. + 2022-07-18 Shahab Vahedi * debuginfod-client.c (dwfl_get_debuginfod_client stub): diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c index cefc3db0..4418ef33 100644 --- a/libdwfl/core-file.c +++ b/libdwfl/core-file.c @@ -75,26 +75,32 @@ elf_begin_rand (Elf *parent, off_t offset, off_t size, off_t *next) from the archive header to override SIZE. */ if (parent->kind == ELF_K_AR) { - struct ar_hdr h = { .ar_size = "" }; - - if (unlikely (parent->maximum_size - offset < sizeof h)) + /* File size, in ASCII decimal, right-padded with ASCII spaces. + Max 10 characters. Not zero terminated. So make this ar_size + array one larger and explicitly zero terminate it. As needed + for strtoll. */ + #define AR_SIZE_CHARS 10 + char ar_size[AR_SIZE_CHARS + 1]; + ar_size[AR_SIZE_CHARS] = '\0'; + + if (unlikely (parent->maximum_size - offset < sizeof (struct ar_hdr))) return fail (ELF_E_RANGE); if (parent->map_address != NULL) - memcpy (h.ar_size, parent->map_address + parent->start_offset + offset, - sizeof h.ar_size); + memcpy (ar_size, parent->map_address + parent->start_offset + offset, + AR_SIZE_CHARS); else if (unlikely (pread_retry (parent->fildes, - h.ar_size, sizeof (h.ar_size), + ar_size, AR_SIZE_CHARS, parent->start_offset + offset + offsetof (struct ar_hdr, ar_size)) -!= sizeof (h.ar_size))) +!= AR_SIZE_CHARS)) return fail (ELF_E_READ_ERROR); - offset += sizeof h; + offset += sizeof (struct ar_hdr); char *endp; - size = strtoll (h.ar_size, &endp, 10); - if (unlikely (endp == h.ar_size) + size = strtoll (ar_size, &endp, 10); + if (unlikely (endp == ar_size) || unlikely ((off_t) parent->maximum_size - offset < size)) return fail (ELF_E_INVALID_ARCHIVE); }
Re: [PATCH] libdwfl: Rewrite reading of ar_size in elf_begin_rand
On 2022-07-28 13:27, Siddhesh Poyarekar wrote: Interesting, I'll take a closer look at this from the gcc context. I obviously don't have any strong opinions about the elfutils patch :) I reduced this to the below program and I see it warns with `-D_FORTIFY_SOURCE=3 -O2 -fsanitize=undefined` as well as `-D_FORTIFY_SOURCE=2 -O2 -fsanitize=undefined`. This is definitely a false positive on unreachable code; __pread_alias will never be called when nbytes is greater than object size. So I can confirm that this is harmless. Sid ~~~ typedef long ssize_t; typedef unsigned long size_t; typedef long off_t; struct ar_hdr { char ar_mode; char ar_size[10]; }; extern ssize_t __pread_chk (int fd, void *buf, size_t nbytes, off_t offset, size_t bufsize) __attribute__((__access__ (__write_only__, 2, 3))); extern ssize_t __pread_alias (int fd, void *buf, size_t nbytes, off_t offset) __attribute__((__access__ (__write_only__, 2, 3))); extern ssize_t __pread_chk_warn (int fd, void *buf, size_t nbytes, off_t offset, size_t bufsize); extern __inline __attribute__((__always_inline__)) __attribute__((__gnu_inline__)) ssize_t pread (int fd, void *buf, size_t nbytes, off_t offset) { size_t osz = __builtin_dynamic_object_size (buf, 0); if (__builtin_constant_p (osz) && osz == (size_t) -1) return __pread_alias (fd, buf, nbytes, offset); return (((__typeof (nbytes)) 0 < (__typeof (nbytes)) - 1 || (__builtin_constant_p (nbytes) && (nbytes) > 0)) && __builtin_constant_p (nbytes <= osz / 1) && nbytes <= osz / 1) ? __pread_alias (fd, buf, nbytes, offset) : __pread_chk (fd, buf, nbytes, offset, osz); } ssize_t pread_retry (int fd, off_t start_offset, off_t offset) { ssize_t recvd = 0; struct ar_hdr h = {.ar_size = {0} }; void *buf = h.ar_size; size_t len = sizeof (h.ar_size); off_t off = start_offset + offset + __builtin_offsetof (struct ar_hdr, ar_size); do { long int res; do { res = pread (fd, ((char *) buf) + recvd, len - recvd, off + recvd); } while (res == -1L); recvd += res; } while ((size_t) recvd < len); return recvd; }