public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] localedef: check magic value on archive load [BZ #28650]
@ 2021-12-05 14:45 Aurelien Jarno
  2021-12-06 18:48 ` Adhemerval Zanella
  0 siblings, 1 reply; 2+ messages in thread
From: Aurelien Jarno @ 2021-12-05 14:45 UTC (permalink / raw)
  To: libc-alpha; +Cc: Aurelien Jarno

localedef currently blindly trust the archive header. When passed an
archive file with the wrong endianess, this leads to a segmentation
fault:

  $ localedef --big-endian --list-archive /usr/lib/locale/locale-archive
  Segmentation fault (core dumped)

When passed non-archive files, asserts are reported on the best case,
but sometimes it can lead to a segmentation fault:

  $ localedef --list-archive /bin/true
  localedef: programs/locarchive.c:1643: show_archive_content: Assertion `used < GET (head->namehash_used)' failed.
  Aborted (core dumped)

  $ localedef --list-archive /usr/lib/locale/C.utf8/LC_COLLATE
  Segmentation fault (core dumped)

This patch improves the user experience by looking at the magic value,
which is always written, but never checked. It should still be possible
to trigger a segmentation fault with crafted files, but this already
catch many cases.
---
 locale/programs/locarchive.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/locale/programs/locarchive.c b/locale/programs/locarchive.c
index eeec3ab648..477499bd40 100644
--- a/locale/programs/locarchive.c
+++ b/locale/programs/locarchive.c
@@ -654,6 +654,13 @@ open_archive (struct locarhandle *ah, bool readonly)
       error (EXIT_FAILURE, errno, _("cannot read archive header"));
     }
 
+  /* Check the magic value */
+  if (GET (head.magic) != AR_MAGIC)
+    {
+      (void) lockf64 (fd, F_ULOCK, sizeof (struct locarhead));
+      error (EXIT_FAILURE, 0, _("bad magic value in archive header"));
+    }
+
   ah->fd = fd;
   ah->mmaped = st.st_size;
 
-- 
2.30.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] localedef: check magic value on archive load [BZ #28650]
  2021-12-05 14:45 [PATCH] localedef: check magic value on archive load [BZ #28650] Aurelien Jarno
@ 2021-12-06 18:48 ` Adhemerval Zanella
  0 siblings, 0 replies; 2+ messages in thread
From: Adhemerval Zanella @ 2021-12-06 18:48 UTC (permalink / raw)
  To: Aurelien Jarno, libc-alpha



On 05/12/2021 11:45, Aurelien Jarno wrote:
> localedef currently blindly trust the archive header. When passed an
> archive file with the wrong endianess, this leads to a segmentation
> fault:
> 
>   $ localedef --big-endian --list-archive /usr/lib/locale/locale-archive
>   Segmentation fault (core dumped)
> 
> When passed non-archive files, asserts are reported on the best case,
> but sometimes it can lead to a segmentation fault:
> 
>   $ localedef --list-archive /bin/true
>   localedef: programs/locarchive.c:1643: show_archive_content: Assertion `used < GET (head->namehash_used)' failed.
>   Aborted (core dumped)
> 
>   $ localedef --list-archive /usr/lib/locale/C.utf8/LC_COLLATE
>   Segmentation fault (core dumped)
> 
> This patch improves the user experience by looking at the magic value,
> which is always written, but never checked. It should still be possible
> to trigger a segmentation fault with crafted files, but this already
> catch many cases.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  locale/programs/locarchive.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/locale/programs/locarchive.c b/locale/programs/locarchive.c
> index eeec3ab648..477499bd40 100644
> --- a/locale/programs/locarchive.c
> +++ b/locale/programs/locarchive.c
> @@ -654,6 +654,13 @@ open_archive (struct locarhandle *ah, bool readonly)
>        error (EXIT_FAILURE, errno, _("cannot read archive header"));
>      }
>  
> +  /* Check the magic value */
> +  if (GET (head.magic) != AR_MAGIC)
> +    {
> +      (void) lockf64 (fd, F_ULOCK, sizeof (struct locarhead));
> +      error (EXIT_FAILURE, 0, _("bad magic value in archive header"));
> +    }
> +
>    ah->fd = fd;
>    ah->mmaped = st.st_size;
>  
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-06 18:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-05 14:45 [PATCH] localedef: check magic value on archive load [BZ #28650] Aurelien Jarno
2021-12-06 18:48 ` Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).