1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-08-05 19:35:52 +03:00
Files
glibc/debug/tst-sprintf-fortify-rdonly-mod.c
Adhemerval Zanella ed6a68bac7 debug: Improve '%n' fortify detection (BZ 30932)
The 7bb8045ec0 path made the '%n' fortify check ignore EMFILE errors
while trying to open /proc/self/maps, and this added a security
issue where EMFILE can be attacker-controlled thus making it
ineffective for some cases.

The EMFILE failure is reinstated but with a different error
message.  Also, to improve the false positive of the hardening for
the cases where no new files can be opened, the
_dl_readonly_area now uses  _dl_find_object to check if the
memory area is within a writable ELF segment.  The procfs method is
still used as fallback.

Checked on x86_64-linux-gnu and i686-linux-gnu.
Reviewed-by: Arjun Shankar <arjun@redhat.com>
2025-03-21 15:46:48 -03:00

57 lines
1.5 KiB
C

/* Testcase for BZ 30932.
Copyright (C) 2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static const char *str2 = "F";
static char writeable_format[10] = "%s";
static char relro_format[10] __attribute__ ((section (".data.rel.ro"))) =
"%s%n%s%n";
void
init_writable (void)
{
strcpy (writeable_format + 2, "%n%s%n");
}
int
sprintf_writable (int *n1, int *n2)
{
char buf[128];
return sprintf (buf, writeable_format, str2, n1, str2, n2);
}
int
sprintf_relro (int *n1, int *n2)
{
char buf[128];
return sprintf (buf, relro_format, str2, n1, str2, n2);
}
int
sprintf_writable_malloc (int *n1, int *n2)
{
char buf[128];
char *buf2_malloc = strdup (writeable_format);
if (buf2_malloc == NULL)
abort ();
return sprintf (buf, buf2_malloc, str2, n1, str2, n2);
}